UNPKG

@palmares/databases

Version:

Add support for working with databases with palmares framework

435 lines 21.6 kB
import { Field } from './field'; import type { CustomImportsForFieldType, FieldWithOperationTypeForSearch } from './types'; import type { CompareCallback, NewInstanceArgumentsCallback, OptionsCallback, ToStringCallback } from './utils'; import type { DatabaseAdapter } from '../../engine'; /** * Functional approach for the creation of an Integer field. * * A integer field is a field that is used to store integers. On your database it should store * only INTEGER numbers. For decimals/float use Decimal. * * @example * ```ts * const integerField = int(); * ``` * * @example * ``` * const integerField = int().default(2); * ``` */ export declare function int(): IntegerField<{ create: number; read: number; update: number; }, { unique: false; allowNull: false; dbIndex: false; underscored: true; isPrimaryKey: false; auto: false; hasDefaultValue: false; defaultValue: undefined; typeName: string; databaseName: undefined; engineInstance: DatabaseAdapter; customAttributes: any; }, Pick<FieldWithOperationTypeForSearch<number>, 'eq' | 'is' | 'greaterThan' | 'lessThan' | 'between' | 'and' | 'or'>>; /** * A IntegerField supports only numbers, and not big numbers. * * @example * ```ts * const IntegerField = IntegerField.new(); * ``` * * @example * ``` * const IntegerField = IntegerField.new().databaseName('user_id'); * ``` */ export declare class IntegerField<out TType extends { create: any; read: any; update: any; } = { create: number; read: number; update: number; }, out TDefinitions extends { unique: boolean; auto: boolean; allowNull: boolean; dbIndex: boolean; isPrimaryKey: boolean; hasDefaultValue: boolean; defaultValue: undefined; underscored: boolean; typeName: string; databaseName: string | undefined; engineInstance: DatabaseAdapter; customAttributes: any; } & Record<string, any> = { unique: true; allowNull: true; dbIndex: true; underscored: true; hasDefaultValue: false; isPrimaryKey: true; auto: true; defaultValue: undefined; typeName: string; databaseName: undefined; engineInstance: DatabaseAdapter; customAttributes: any; }, out TFieldOperationTypes = Pick<FieldWithOperationTypeForSearch<number>, 'eq' | 'is' | 'greaterThan' | 'lessThan' | 'between' | 'and' | 'or'>> extends Field<TType, TDefinitions, TFieldOperationTypes> { protected $$type: string; protected __typeName: string; protected __allowedQueryOperations: Set<any>; protected __inputParsers: Map<string, (args: import("../..").AdapterFieldParserInputAndOutputArgs<"field" | "auto" | "big-auto" | "big-integer" | "boolean" | "char" | "date" | "decimal" | "enum" | "foreign-key" | "integer" | "text" | "uuid">) => Promise<any>>; protected __outputParsers: Map<string, (args: import("../..").AdapterFieldParserInputAndOutputArgs<"field" | "auto" | "big-auto" | "big-integer" | "boolean" | "char" | "date" | "decimal" | "enum" | "foreign-key" | "integer" | "text" | "uuid">) => Promise<any>>; /** * Supposed to be used by library maintainers. * * When you custom create a field, you might want to take advantage of the builder pattern we already support. * This let's you create functions that can be chained together to create a new field. It should be used * alongside the `_setPartialAttributes` method like * * @example * ```ts * const customBigInt = TextField.overrideType< * { create: bigint; read: bigint; update: bigint }, * { * customAttributes: { name: string }; * unique: boolean; * auto: boolean; * allowNull: true; * dbIndex: boolean; * isPrimaryKey: boolean; * defaultValue: any; * typeName: string; * engineInstance: DatabaseAdapter; * } * >({ * typeName: 'CustomBigInt' * }); * * const customBuilder = () => { * const field = textField.new(); * class Builder { * test<TTest extends { age: number }>(param: TTest) { * return field * ._setPartialAttributes<{ create: TTest }, { create: 'union' }>()(param) * ._setNewBuilderMethods<Builder>(); * } * value<const TValue extends string>(value: TValue) { * return field * ._setPartialAttributes<{ create: TValue }, { create: 'replace' }>()(value) * ._setNewBuilderMethods<Builder>(); * } * } * * const builder = new Builder(); * return field._setNewBuilderMethods(builder); * }; * * // Then your user can use it like: * * const field = customBuilder({ name: 'test' }).test({ age: 2 }); * ``` * * **Important**: `customBuilder` will be used by the end user and you are responsible for documenting it. */ _setNewBuilderMethods<const TFunctions extends InstanceType<any>>(functions?: TFunctions): IntegerField<TType, { [TKey in Exclude<keyof TDefinitions, 'underscored' | 'allowNull' | 'dbIndex' | 'unique' | 'isPrimaryKey' | 'auto' | 'defaultValue' | 'databaseName' | 'typeName' | 'engineInstance' | 'customAttributes'>]: TDefinitions[TKey]; } & { unique: TDefinitions['unique']; allowNull: TDefinitions['allowNull']; dbIndex: TDefinitions['dbIndex']; underscored: TDefinitions['underscored']; isPrimaryKey: TDefinitions['isPrimaryKey']; auto: TDefinitions['auto']; hasDefaultValue: TDefinitions['hasDefaultValue']; defaultValue: TDefinitions['defaultValue']; databaseName: TDefinitions['databaseName']; typeName: TDefinitions['typeName']; engineInstance: TDefinitions['engineInstance']; customAttributes: TDefinitions['customAttributes']; }, TFieldOperationTypes> & TFunctions; /** * FOR LIBRARY MAINTAINERS ONLY * * Focused for library maintainers that want to support a custom field type not supported by palmares. * This let's them partially update the custom attributes of the field. By default setCustomAttributes * will override the custom attributes entirely. */ _setPartialAttributes<TNewType extends { create?: any; read?: any; update?: any; }, TActions extends { create?: 'merge' | 'union' | 'replace'; read?: 'merge' | 'union' | 'replace'; update?: 'merge' | 'union' | 'replace'; }, TNewAllowedQueryOperations extends FieldWithOperationTypeForSearch<TActions['read'] extends 'merge' ? TType['read'] & TNewType['read'] : TActions['read'] extends 'union' ? TType['read'] | TNewType['read'] : TActions['read'] extends 'replace' ? TNewType['read'] : TType['read']> = Pick<FieldWithOperationTypeForSearch<TActions['read'] extends 'merge' ? TType['read'] & TNewType['read'] : TActions['read'] extends 'union' ? TType['read'] | TNewType['read'] : TActions['read'] extends 'replace' ? TNewType['read'] : TType['read']>, 'eq' | 'is' | 'greaterThan' | 'lessThan' | 'between' | 'and' | 'or'>>(): <const TCustomPartialAttributes>(partialCustomAttributes: TCustomPartialAttributes) => IntegerField<{ create: TActions['create'] extends 'merge' ? TType['create'] & TNewType['create'] : TActions['create'] extends 'union' ? TType['create'] | TNewType['create'] : TActions['create'] extends 'replace' ? TNewType['create'] : TType['create']; read: TActions['read'] extends 'merge' ? TType['read'] & TNewType['read'] : TActions['read'] extends 'union' ? TType['read'] | TNewType['read'] : TActions['read'] extends 'replace' ? TNewType['read'] : TType['read']; update: TActions['update'] extends 'merge' ? TType['update'] & TNewType['update'] : TActions['update'] extends 'union' ? TType['update'] | TNewType['update'] : TActions['update'] extends 'replace' ? TNewType['update'] : TType['update']; }, { [TKey in Exclude<keyof TDefinitions, 'underscored' | 'allowNull' | 'dbIndex' | 'unique' | 'isPrimaryKey' | 'auto' | 'defaultValue' | 'databaseName' | 'typeName' | 'engineInstance' | 'customAttributes'>]: TDefinitions[TKey]; } & { unique: TDefinitions['unique']; allowNull: TDefinitions['allowNull']; dbIndex: TDefinitions['dbIndex']; underscored: TDefinitions['underscored']; isPrimaryKey: TDefinitions['isPrimaryKey']; auto: TDefinitions['auto']; hasDefaultValue: TDefinitions['hasDefaultValue']; defaultValue: TDefinitions['defaultValue']; databaseName: TDefinitions['databaseName']; typeName: TDefinitions['typeName']; engineInstance: TDefinitions['engineInstance']; customAttributes: TDefinitions['customAttributes'] & TCustomPartialAttributes; }, TNewAllowedQueryOperations>; setCustomAttributes<const TCustomAttributes extends Parameters<TDefinitions['engineInstance']['fields']['integerFieldParser']['translate']>[0]['customAttributes']>(customAttributes: TCustomAttributes): IntegerField<TType, { [TKey in Exclude<keyof TDefinitions, 'underscored' | 'allowNull' | 'dbIndex' | 'unique' | 'isPrimaryKey' | 'auto' | 'defaultValue' | 'databaseName' | 'typeName' | 'engineInstance' | 'customAttributes'>]: TDefinitions[TKey]; } & { unique: TDefinitions['unique']; allowNull: TDefinitions['allowNull']; dbIndex: TDefinitions['dbIndex']; underscored: TDefinitions['underscored']; isPrimaryKey: TDefinitions['isPrimaryKey']; auto: TDefinitions['auto']; hasDefaultValue: TDefinitions['hasDefaultValue']; defaultValue: TDefinitions['defaultValue']; databaseName: TDefinitions['databaseName']; typeName: TDefinitions['typeName']; engineInstance: TDefinitions['engineInstance']; customAttributes: TCustomAttributes; }, TFieldOperationTypes>; unique<TUnique extends boolean = true>(isUnique?: TUnique): IntegerField<TType, { [TKey in Exclude<keyof TDefinitions, 'underscored' | 'allowNull' | 'dbIndex' | 'unique' | 'isPrimaryKey' | 'auto' | 'defaultValue' | 'databaseName' | 'typeName' | 'engineInstance' | 'customAttributes'>]: TDefinitions[TKey]; } & { unique: TUnique extends false ? false : true; allowNull: TDefinitions['allowNull']; dbIndex: TDefinitions['dbIndex']; underscored: TDefinitions['underscored']; isPrimaryKey: TDefinitions['isPrimaryKey']; auto: TDefinitions['auto']; hasDefaultValue: TDefinitions['hasDefaultValue']; defaultValue: TDefinitions['defaultValue']; databaseName: TDefinitions['databaseName']; typeName: TDefinitions['typeName']; engineInstance: TDefinitions['engineInstance']; customAttributes: TDefinitions['customAttributes']; }, TFieldOperationTypes>; allowNull<TNull extends boolean = true>(isNull?: TNull): IntegerField<{ create: TType['create'] | null | undefined; read: TType['read'] | null | undefined; update: TType['update'] | null | undefined; }, { [TKey in Exclude<keyof TDefinitions, 'underscored' | 'allowNull' | 'dbIndex' | 'unique' | 'isPrimaryKey' | 'auto' | 'defaultValue' | 'databaseName' | 'typeName' | 'engineInstance' | 'customAttributes'>]: TDefinitions[TKey]; } & { unique: TDefinitions['unique']; allowNull: TNull extends false ? false : true; dbIndex: TDefinitions['dbIndex']; underscored: TDefinitions['underscored']; isPrimaryKey: TDefinitions['isPrimaryKey']; auto: TDefinitions['auto']; hasDefaultValue: TDefinitions['hasDefaultValue']; defaultValue: TDefinitions['defaultValue']; databaseName: TDefinitions['databaseName']; typeName: TDefinitions['typeName']; engineInstance: TDefinitions['engineInstance']; customAttributes: TDefinitions['customAttributes']; }, Pick<FieldWithOperationTypeForSearch<TType['read'] | null>, 'eq' | 'is' | 'greaterThan' | 'lessThan' | 'between' | 'and' | 'or'>>; /** * This method is used to create an index on the database for this field. */ dbIndex<TDbIndex extends boolean = true>(isDbIndex?: TDbIndex): IntegerField<TType, { [TKey in Exclude<keyof TDefinitions, 'underscored' | 'allowNull' | 'dbIndex' | 'unique' | 'isPrimaryKey' | 'auto' | 'defaultValue' | 'databaseName' | 'typeName' | 'engineInstance' | 'customAttributes'>]: TDefinitions[TKey]; } & { unique: TDefinitions['unique']; allowNull: TDefinitions['allowNull']; dbIndex: TDbIndex extends false ? false : true; underscored: TDefinitions['underscored']; isPrimaryKey: TDefinitions['isPrimaryKey']; auto: TDefinitions['auto']; hasDefaultValue: TDefinitions['hasDefaultValue']; defaultValue: TDefinitions['defaultValue']; databaseName: TDefinitions['databaseName']; typeName: TDefinitions['typeName']; engineInstance: TDefinitions['engineInstance']; customAttributes: TDefinitions['customAttributes']; }, TFieldOperationTypes>; underscored<TUnderscored extends boolean = true>(isUnderscored?: TUnderscored): IntegerField<TType, { [TKey in Exclude<keyof TDefinitions, 'underscored' | 'allowNull' | 'dbIndex' | 'unique' | 'isPrimaryKey' | 'auto' | 'defaultValue' | 'databaseName' | 'typeName' | 'engineInstance' | 'customAttributes'>]: TDefinitions[TKey]; } & { unique: TDefinitions['unique']; allowNull: TDefinitions['allowNull']; dbIndex: TDefinitions['dbIndex']; underscored: TUnderscored extends false ? false : true; isPrimaryKey: TDefinitions['isPrimaryKey']; auto: TDefinitions['auto']; hasDefaultValue: TDefinitions['hasDefaultValue']; defaultValue: TDefinitions['defaultValue']; databaseName: TDefinitions['databaseName']; typeName: TDefinitions['typeName']; engineInstance: TDefinitions['engineInstance']; customAttributes: TDefinitions['customAttributes']; }, TFieldOperationTypes>; primaryKey<TIsPrimaryKey extends boolean = true>(isPrimaryKey?: TIsPrimaryKey): IntegerField<TType, { [TKey in Exclude<keyof TDefinitions, 'underscored' | 'allowNull' | 'dbIndex' | 'unique' | 'isPrimaryKey' | 'auto' | 'defaultValue' | 'databaseName' | 'typeName' | 'engineInstance' | 'customAttributes'>]: TDefinitions[TKey]; } & { unique: TDefinitions['unique']; allowNull: TDefinitions['allowNull']; dbIndex: TDefinitions['dbIndex']; underscored: TDefinitions['underscored']; isPrimaryKey: TIsPrimaryKey extends false ? false : true; auto: TDefinitions['auto']; hasDefaultValue: TDefinitions['hasDefaultValue']; defaultValue: TDefinitions['defaultValue']; databaseName: TDefinitions['databaseName']; typeName: TDefinitions['typeName']; engineInstance: TDefinitions['engineInstance']; customAttributes: TDefinitions['customAttributes']; }, TFieldOperationTypes>; auto<TIsAuto extends boolean = true>(isAuto?: TIsAuto): IntegerField<{ create: TType['create'] | undefined; read: TType['read']; update: TType['update'] | undefined; }, { [TKey in Exclude<keyof TDefinitions, 'underscored' | 'allowNull' | 'dbIndex' | 'unique' | 'isPrimaryKey' | 'auto' | 'defaultValue' | 'databaseName' | 'typeName' | 'engineInstance' | 'customAttributes'>]: TDefinitions[TKey]; } & { unique: TDefinitions['unique']; allowNull: TDefinitions['allowNull']; dbIndex: TDefinitions['dbIndex']; underscored: TDefinitions['underscored']; isPrimaryKey: TDefinitions['isPrimaryKey']; auto: TIsAuto extends false ? false : true; hasDefaultValue: TDefinitions['hasDefaultValue']; defaultValue: TDefinitions['defaultValue']; databaseName: TDefinitions['databaseName']; typeName: TDefinitions['typeName']; engineInstance: TDefinitions['engineInstance']; customAttributes: TDefinitions['customAttributes']; }, TFieldOperationTypes>; default<TDefault extends TType['create']>(defaultValue: TDefault): IntegerField<{ create: TType['create'] | TDefault | undefined; read: TType['read']; update: TType['update'] | undefined; }, { [TKey in Exclude<keyof TDefinitions, 'underscored' | 'allowNull' | 'dbIndex' | 'unique' | 'isPrimaryKey' | 'auto' | 'defaultValue' | 'databaseName' | 'typeName' | 'engineInstance' | 'customAttributes'>]: TDefinitions[TKey]; } & { unique: TDefinitions['unique']; allowNull: TDefinitions['allowNull']; dbIndex: TDefinitions['dbIndex']; underscored: TDefinitions['underscored']; isPrimaryKey: TDefinitions['isPrimaryKey']; auto: TDefinitions['auto']; hasDefaultValue: true; defaultValue: TDefault; databaseName: TDefinitions['databaseName']; typeName: TDefinitions['typeName']; engineInstance: TDefinitions['engineInstance']; customAttributes: TDefinitions['customAttributes']; }, TFieldOperationTypes>; databaseName<TDatabaseName extends string>(databaseName: TDatabaseName): IntegerField<TType, { [TKey in Exclude<keyof TDefinitions, 'underscored' | 'allowNull' | 'dbIndex' | 'unique' | 'isPrimaryKey' | 'auto' | 'defaultValue' | 'databaseName' | 'typeName' | 'engineInstance' | 'customAttributes'>]: TDefinitions[TKey]; } & { hasDefaultValue: TDefinitions['hasDefaultValue']; unique: TDefinitions['unique']; allowNull: TDefinitions['allowNull']; dbIndex: TDefinitions['dbIndex']; underscored: TDefinitions['underscored']; isPrimaryKey: TDefinitions['isPrimaryKey']; auto: TDefinitions['auto']; defaultValue: TDefinitions['defaultValue']; databaseName: TDatabaseName; typeName: TDefinitions['typeName']; engineInstance: TDefinitions['engineInstance']; customAttributes: TDefinitions['customAttributes']; }, TFieldOperationTypes>; /** * This method can be used to override the type of a field. This is useful for library * maintainers that want to support a custom field type not supported by palmares. * * ### Note * Your library should provide documentation of the fields that are supported. */ static _overrideType<const TNewType extends { create: any; update: any; read: any; }, const TDefinitions extends { customAttributes: any; unique: boolean; auto: boolean; hasDefaultValue: boolean; allowNull: boolean; dbIndex: boolean; isPrimaryKey: boolean; defaultValue: any; typeName: string; engineInstance: DatabaseAdapter; }, const TFieldOperationTypes extends FieldWithOperationTypeForSearch<any> | Pick<FieldWithOperationTypeForSearch<any>, any>>(args: { typeName: string; toStringCallback?: ToStringCallback; compareCallback?: CompareCallback; optionsCallback?: OptionsCallback; newInstanceCallback?: NewInstanceArgumentsCallback; customImports?: CustomImportsForFieldType[]; allowedQueryOperations?: (keyof TFieldOperationTypes)[]; definitions?: Omit<TDefinitions, 'typeName' | 'engineInstance' | 'customAttributes'>; }): TDefinitions['customAttributes'] extends undefined ? { new: () => IntegerField<TNewType, { unique: TDefinitions['unique']; auto: TDefinitions['auto']; hasDefaultValue: TDefinitions['hasDefaultValue']; allowNull: TDefinitions['allowNull']; dbIndex: TDefinitions['dbIndex']; isPrimaryKey: TDefinitions['isPrimaryKey']; defaultValue: TDefinitions['defaultValue']; underscored: boolean; databaseName: string | undefined; engineInstance: TDefinitions['engineInstance']; customAttributes: TDefinitions['customAttributes']; typeName: TDefinitions['typeName']; }, TFieldOperationTypes>; } : { new: (params: TDefinitions['customAttributes']) => IntegerField<TNewType, { unique: TDefinitions['unique']; auto: TDefinitions['auto']; hasDefaultValue: TDefinitions['hasDefaultValue']; allowNull: TDefinitions['allowNull']; dbIndex: TDefinitions['dbIndex']; isPrimaryKey: TDefinitions['isPrimaryKey']; defaultValue: TDefinitions['defaultValue']; underscored: boolean; databaseName: string | undefined; engineInstance: TDefinitions['engineInstance']; customAttributes: TDefinitions['customAttributes']; typeName: TDefinitions['typeName']; }, TFieldOperationTypes>; }; static new(..._args: any[]): IntegerField<{ create: number; read: number; update: number; }, { unique: false; allowNull: false; dbIndex: false; underscored: true; isPrimaryKey: false; auto: false; defaultValue: undefined; hasDefaultValue: false; typeName: string; databaseName: undefined; engineInstance: DatabaseAdapter; customAttributes: any; }, Pick<FieldWithOperationTypeForSearch<number>, 'eq' | 'is' | 'greaterThan' | 'lessThan' | 'between' | 'and' | 'or'>>; } //# sourceMappingURL=integer.d.ts.map