import type { RequiredModelMeta } from './compileModel'; import type { BaseSchemaType, SchemaTypeDefinitionBoolean, SchemaTypeDefinitionISOCalendarDate, SchemaTypeDefinitionISOCalendarDateTime, SchemaTypeDefinitionISOTime, SchemaTypeDefinitionNumber, SchemaTypeDefinitionScalar, SchemaTypeDefinitionString, SchemaTypePath } from './schemaType'; import type { DataTransformer, DecryptFn, EncryptFn, FlattenObject, ISOCalendarDate, ISOCalendarDateTime, ISOTime, MarkRequired, MvRecord, Remap } from './types'; export type SchemaTypeDefinition = Schema | SchemaTypeDefinitionScalar | SchemaDefinition | SchemaTypeDefinitionArray; type SchemaTypeDefinitionArray = readonly [Schema] | readonly [SchemaTypeDefinitionScalar] | readonly [[SchemaTypeDefinitionScalar]] | readonly [SchemaDefinition]; export interface SchemaDefinition { [x: string]: SchemaTypeDefinition; } export interface SchemaForeignKeyDefinition { file: string | string[]; keysToIgnore?: string[]; entityName: string; } export interface SchemaCompoundForeignKeyDefinition { [key: number]: SchemaForeignKeyDefinition; splitCharacter: string; } type PickAndMark = MarkRequired, 'dictionary'>; export type DictionaryTypeDefinitionString = Remap>; export type DictionaryTypeDefinitionNumber = Remap>; export type DictionaryTypeDefinitionBoolean = Remap>; export type DictionaryTypeDefinitionISOCalendarDate = Remap>; export type DictionaryTypeDefinitionISOCalendarDateTime = Remap>; export type DictionaryTypeDefinitionISOTime = Remap>; export type DictionaryTypeDefinition = DictionaryTypeDefinitionString | DictionaryTypeDefinitionNumber | DictionaryTypeDefinitionBoolean | DictionaryTypeDefinitionISOCalendarDate | DictionaryTypeDefinitionISOCalendarDateTime | DictionaryTypeDefinitionISOTime; export type DictionaryDefinition = string | DictionaryTypeDefinition; export type DictionariesOption = Record; export interface SchemaConstructorOptions { dictionaries?: TDictionaries; idMatch?: RegExp; idForeignKey?: SchemaForeignKeyDefinition | SchemaCompoundForeignKeyDefinition; encrypt?: EncryptFn; decrypt?: DecryptFn; } interface DictionaryTypeDetail { dictionary: string; dataTransformer: DataTransformer; } /** Infer whether a schema type definition is required and union the result with null if it is not */ type InferRequiredType = TScalar extends { required: true; } ? TType : TType | null; /** * Infer the output of a string type definition with specific handling for enumerations * If an enumeration is a readonly array, the return type of the definition will be a union * of the array elements. Otherwise, the return type will be a string. */ type InferStringType = TString['enum'] extends readonly (infer E)[] ? E : string; /** * Infer the output type of an array schema definition * * Allows a constraint to be specified to filter the output to only include scalar arrays of a specific type */ type InferArraySchemaType = TSchemaTypeDefinition extends [infer TArrayDefinition] ? TArrayDefinition extends SchemaTypeDefinitionScalar ? TArrayDefinition extends TConstraint ? InferSchemaType[] : never : InferSchemaType[] : never; /** Infer the output type of a schema type definition */ type InferSchemaType = TSchemaTypeDefinition extends SchemaTypeDefinitionBoolean ? InferRequiredType : TSchemaTypeDefinition extends SchemaTypeDefinitionString ? InferRequiredType> : TSchemaTypeDefinition extends SchemaTypeDefinitionNumber ? InferRequiredType : TSchemaTypeDefinition extends SchemaTypeDefinitionISOCalendarDate ? InferRequiredType : TSchemaTypeDefinition extends SchemaTypeDefinitionISOCalendarDateTime ? InferRequiredType : TSchemaTypeDefinition extends SchemaTypeDefinitionISOTime ? InferRequiredType : TSchemaTypeDefinition extends Schema ? InferDocumentObject, TConstraint> : TSchemaTypeDefinition extends SchemaTypeDefinitionArray ? InferArraySchemaType : TSchemaTypeDefinition extends SchemaDefinition ? InferDocumentObject, TConstraint> : never; /** * Infer the shape of a `Document` instance based upon the Schema it was instantiated with * * Allows a constraint to be specified to filter the output to only include properties of a specific type */ export type InferDocumentObject = TSchema extends Schema ? { [K in keyof TSchemaDefinition as TSchemaDefinition[K] extends TConstraint ? K : never]: InferSchemaType; } : { _raw: MvRecord; }; /** Infer the shape of a `Model` instance based upon the Schema it was instantiated with */ export type InferModelObject = Remap & RequiredModelMeta>; /** * Flatten a document to string keyPath (i.e. { "foo.bar.baz": number }) * * Allows a constraint to be specified to filter the output to only include properties of a specific type */ export type FlattenDocument = InferDocumentObject extends infer O extends Record ? FlattenObject : never; /** Infer the string keyPaths of a schema */ export type InferSchemaPaths = Extract, string>; /** Schema constructor */ declare class Schema> { /** Key/value pairs of schema object path structure and associated multivalue dictionary ids */ dictPaths: Map; /** The compiled schema object path structure */ readonly paths: Map; /** Foreign key definition for the record id */ readonly idForeignKey?: SchemaForeignKeyDefinition | SchemaCompoundForeignKeyDefinition; /** Regular expression to validate the record id against */ readonly idMatch?: RegExp; /** The schema definition passed to the constructor */ private readonly definition; /** Map of the compiled schema object position paths */ private readonly positionPaths; /** Map of all subdocument schemas represented in this Schema with parentPath as key */ private readonly subdocumentSchemas; /** Optional function to use for encryption of sensitive data */ private readonly encrypt?; /** Optional function to use for decryption of sensitive data */ private readonly decrypt?; constructor(definition: TSchemaDefinition, { dictionaries, idForeignKey, idMatch, encrypt, decrypt, }?: SchemaConstructorOptions); /** Get all multivalue data paths in this schema and its subdocument schemas */ getMvPaths(): SchemaTypePath[]; /** Transform the paths to positions */ transformPathsToDbPositions(paths: readonly string[]): number[]; /** * Transform the path to its ordinal position. Returning a '.' delimited string. Ex. "1.2.3" * @throws {Error} Invalid path provided */ transformPathToOrdinalPosition(path: InferSchemaPaths>): `${number}.${number}.${number}`; /** Build the dictionary path map for additional dictionaries provided as schema options */ private buildDictionaryPaths; /** * Get all positionPaths with path as key and position array as value including children schemas * e.g. parent field 'foo' has a child schema which has ["bar",[2]], the returned positionPath will be ["foo.bar",[2]]. */ private getPositionPaths; /** Construct instance member paths */ private buildPaths; /** * Cast an array to a schemaType * @throws {@link InvalidParameterError} An invalid parameter was passed to the function */ private castArray; /** * Cast a scalar definition to a scalar schemaType * @throws {@link InvalidParameterError} An invalid parameter was passed to the function */ private castScalar; /** Perform ancillary updates needed when a subdocument is in the Schema definition */ private handleSubDocumentSchemas; /** Determine if an object matches the structure of a scalar definition */ private isScalarDefinition; /** Merge subdocument schema dictionaries with the parent schema's dictionaries */ private mergeSchemaDictionaries; } export default Schema;