/* Model.d.ts -- Hand crafted type definitions for Model Supports dynamic definition of types based on the Schema.js */ import {Expression} from './Expression.js' /* Possible types for a schema field "type" property */ export type OneType = | ArrayConstructor | BooleanConstructor | DateConstructor | NumberConstructor | ObjectConstructor | StringConstructor | SetConstructor | ArrayBufferConstructor | string /* Schema.indexes signature */ export type OneIndex = { hash?: string sort?: string description?: string project?: string | readonly string[] follow?: boolean type?: string } /* Schema.models.Model.Field signature */ export type OneField = { crypt?: boolean default?: string | number | boolean | object | Array encode?: readonly (string | RegExp | number)[] | string enum?: readonly string[] filter?: boolean generate?: string | boolean | function hidden?: boolean items?: OneField map?: string nulls?: boolean partial?: boolean reference?: string required?: boolean schema?: OneModel scope?: string timestamp?: boolean ttl?: boolean type: OneType unique?: boolean validate?: RegExp | string | boolean value?: boolean | string // DEPRECATE 2.3 uuid?: boolean | string } /* Schema.models signature */ export type OneModel = { [key: string]: OneField } /* Schema signature */ export type OneSchema = { name?: string version: string format?: string params?: OneSchemaParams models: { [key: string]: OneModel } process?: object indexes: { [key: string]: OneIndex } queries?: {} } export type OneSchemaParams = { createdField?: string // Name of "created" timestamp attribute. Default to 'created'. hidden?: boolean // Hide key attributes in Javascript properties. Default false. isoDates?: boolean // Set to true to store dates as Javascript ISO Date strings. Default false. nulls?: boolean // Store nulls in database attributes. Default false. timestamps?: boolean | string // Make "created" and "updated" timestamps. Set to true, 'create' or 'update'. Default true. separator?: string // Separator string uses in value templates typeField?: string // Name of model type attribute. Default "_type". updatedField?: string // Name of "updated" timestamp attribute. Default 'updated'. warn?: boolean // Emit warnings for some conditions. Default false. legacyEmpties?: boolean // Remove empty strings } /* Entity field signature generated from the schema */ type EntityField = T['enum'] extends readonly EntityFieldFromType[] ? T['enum'][number] : EntityFieldFromType type EntityFieldFromType = T['type'] extends ArrayConstructor | 'array' ? ArrayItemType[] : T['type'] extends BooleanConstructor | 'boolean' ? boolean : T['type'] extends NumberConstructor | 'number' ? number : T['type'] extends ObjectConstructor | 'object' ? (T['schema'] extends object ? Entity> : Record) : T['type'] extends DateConstructor | 'date' ? Date : T['type'] extends ArrayBufferConstructor ? ArrayBuffer : T['type'] extends StringConstructor | 'string' ? string : T['type'] extends SetConstructor | 'set' ? Set : T['type'] extends 'typed-array' ? EntityFieldFromType>[] : never type ArrayItemType = T extends {items: OneField} ? EntityField : any /* Select the required properties from a model */ export type Required = { -readonly [P in keyof T as T[P]['required'] extends true ? P : never]: EntityField } /* Select the optional properties from a model */ export type Optional = { -readonly [P in keyof T as T[P]['required'] extends true ? never : P]?: EntityField } type OptionalOrNull = { -readonly [P in keyof T as T[P]['required'] extends true ? never : P]?: EntityField | null } type OptionalOrUndefined = { -readonly [P in keyof T as T[P]['required'] extends true ? never : P]?: EntityField | undefined } /* Select properties with generated values */ export type Generated = { -readonly [P in keyof T as T[P]['generate'] extends string | boolean ? P : never]?: EntityField } /* Select properties with default values */ type DefinedValue = string | number | bigint | boolean | symbol | object export type Defaulted = { -readonly [P in keyof T as T[P]['default'] extends DefinedValue ? P : never]: EntityField } /* Select value template properties */ export type ValueTemplates = { -readonly [P in keyof T as T[P]['value'] extends string ? P : never]: EntityField } /* Select timestamp properties */ export type TimestampValue = { -readonly [P in keyof T as T[P]['timestamp'] extends true ? P : never]: EntityField } /* Merge the properties of two types given preference to A. */ type Merge = { [P in keyof (A & B)]: P extends keyof A ? A[P] : P extends keyof B ? B[P] : never } /* Create entity type which includes required and optional types An entity type is not used by the user and is only required internally. Merge gives better intellisense, but requires Flatten to make work. */ type Flatten = {[P in keyof T]: T[P]} type Entity = Flatten, OptionalOrUndefined>> /* Entity Parameters are partial Entities. */ type EntityParameters = Partial /* Special case for find to allow query operators */ type EntityParametersForFind = Partial<{ [K in keyof T]: | T[K] | Begins | BeginsWith | Between | LessThan | LessThanOrEqual | Equal | NotEqual | GreaterThanOrEqual | GreaterThan }> type Begins = {begins: T[K]} type BeginsWith = {begins_with: T[K]} type Between = {between: [T[K], T[K]]} type LessThan = {'<': T[K]} type LessThanOrEqual = {'<=': T[K]} type Equal = {'=': T[K]} type NotEqual = {'<>': T[K]} type GreaterThanOrEqual = {'>=': T[K]} type GreaterThan = {'>': T[K]} /* Any entity. Essentially untyped. */ export type AnyEntity = { [key: string]: any } type ModelConstructorOptions = { fields?: OneModel indexes?: { [key: string]: OneIndex } timestamps?: boolean | string } /* Possible params options for all APIs */ export type OneParams = { add?: object batch?: object capacity?: string consistent?: boolean context?: object count?: boolean delete?: object execute?: boolean exists?: boolean | null fields?: string[] follow?: boolean hidden?: boolean index?: string limit?: number log?: boolean many?: boolean maxPages?: number next?: object // DEPRECATED noerror?: boolean parse?: boolean partial?: boolean postFormat?: (model: AnyModel, cmd: {}) => {} prev?: object push?: object remove?: string[] reprocess?: boolean return?: string | boolean reverse?: boolean segment?: number segments?: number select?: string set?: object stats?: object substitutions?: object timestamps?: boolean throw?: boolean transform?: (model: AnyModel, op: string, name: string, value: any, properties: OneProperties) => any transaction?: object type?: string tunnel?: object warn?: Boolean where?: string profile?: string } /* Properties for most APIs. Essentially untyped. */ export type OneProperties = { [key: string]: any } export class Paged extends Array { count?: number next?: object prev?: object } export type AnyModel = { constructor(table: any, name: string, options?: ModelConstructorOptions): AnyModel create(properties: OneProperties, params?: OneParams): Promise find(properties?: OneProperties, params?: OneParams): Promise> get(properties: OneProperties, params?: OneParams): Promise load(properties: OneProperties, params?: OneParams): Promise init(properties?: OneProperties, params?: OneParams): AnyEntity remove(properties: OneProperties, params?: OneParams): Promise | undefined> scan(properties?: OneProperties, params?: OneParams): Promise> update(properties: OneProperties, params?: OneParams): Promise upsert(properties: OneProperties, params?: OneParams): Promise } type ExtractModel = M extends Entity ? X : never type GetKeys = T extends T ? keyof T : never /* Create the type for create properties. Allow, but not require: generated, defaulted and value templates Require all other required properties and allow all optional properties type EntityParametersForCreate = Required & Optional */ type EntityParametersForCreate = Omit< Omit, GetKeys>>, GetKeys>>, GetKeys>>, GetKeys> > & Optional & Partial> & Partial> & Partial> & Partial> type EntityParametersForUpdate = Partial & OptionalOrNull> type TransactionalOneParams = OneParams & {transaction: object} export class Model { constructor(table: any, name: string, options?: ModelConstructorOptions) create(properties: EntityParametersForCreate>, params?: OneParams): Promise find(properties?: EntityParametersForFind, params?: OneParams): Promise> get(properties: EntityParameters, params?: OneParams): Promise load(properties: EntityParameters, params?: OneParams): Promise init(properties?: EntityParameters, params?: OneParams): T remove(properties: EntityParameters, params?: OneParams): Promise | undefined> scan(properties?: EntityParameters, params?: OneParams): Promise> update(properties: EntityParametersForUpdate>, params?: OneParams): Promise upsert(properties: EntityParameters, params?: OneParams): Promise check(properties: EntityParameters, params: TransactionalOneParams): void }