mvom
Version:
Multivalue Object Mapper
147 lines (146 loc) • 8.15 kB
TypeScript
import type Connection from './Connection';
import type { BuildForeignKeyDefinitionsResult, DocumentData } from './Document';
import type LogHandler from './LogHandler';
import { type Filter, type QueryConstructorOptions } from './Query';
import type Schema from './Schema';
import type { FlattenDocument, InferModelObject, SchemaDefinition } from './Schema';
import type { SchemaTypeDefinitionNumber } from './schemaType';
import type { DbServerDelimiters, DbSubroutineInputIncrementOperation, DbSubroutineUserDefinedOptions, Remap } from './types';
export interface ModelConstructorOptions<TSchema extends Schema | null> {
_id?: string | null;
__v?: string | null;
data?: DocumentData<TSchema>;
record?: string;
}
export type ModelConstructor<TSchema extends Schema | null> = ReturnType<typeof compileModel<TSchema>>;
export type Model<TSchema extends Schema | null> = InstanceType<ModelConstructor<TSchema>>;
type RequiredModelMetaProperties = '_id' | '__v';
/** Used as an intersection type to make specific model properties required */
export type RequiredModelMeta = {
[K in RequiredModelMetaProperties]: NonNullable<Model<Schema>[K]>;
};
/**
* An intersection type that combines the `Model` class instance with the
* inferred shape of the model object based on the schema definition.
*/
export type ModelCompositeValue<TSchema extends Schema | null> = Remap<Model<TSchema> & InferModelObject<TSchema>>;
export interface ModelFindAndCountResult<TSchema extends Schema | null> {
/** Number of documents returned */
count: number;
/** Model instances for the returned documents */
documents: ModelCompositeValue<TSchema>[];
}
export interface ModelIncrementResult<TSchema extends Schema | null> {
originalDocument: ModelCompositeValue<TSchema>;
updatedDocument: ModelCompositeValue<TSchema>;
}
export interface ModelDatabaseExecutionOptions {
userDefined?: DbSubroutineUserDefinedOptions;
requestId?: string;
/** Maximum allowed return payload size in bytes */
maxReturnPayloadSize?: number;
}
export type ModelDeleteByIdOptions = ModelDatabaseExecutionOptions;
export type ModelFindOptions<TSchema extends Schema | null> = QueryConstructorOptions<TSchema> & ModelDatabaseExecutionOptions;
export interface ModelFindByIdOptions extends ModelDatabaseExecutionOptions {
/** Array of projection properties */
projection?: string[];
}
export interface ModelIncrementOptions extends ModelDatabaseExecutionOptions {
/**
* Number of retries to perform when record is locked
* @defaultValue 5
*/
retry?: number;
/**
* Delay between retries in seconds when record is locked
* @defaultValue 1
*/
retryDelay?: number;
}
export type IncrementOperation<TSchema extends Schema | null> = TSchema extends Schema ? {
path: Extract<keyof FlattenDocument<TSchema, Schema | SchemaDefinition | SchemaTypeDefinitionNumber>, string>;
value: number;
} : never;
export type ModelReadFileContentsByIdOptions = ModelDatabaseExecutionOptions;
export type ModelSaveOptions = ModelDatabaseExecutionOptions;
export type ModelCheckForRecordLockByIdOptions = ModelDatabaseExecutionOptions;
/** Define a new model */
declare const compileModel: <TSchema extends Schema | null>(connection: Connection, schema: TSchema, file: string, dbServerDelimiters: DbServerDelimiters, logHandler: LogHandler) => {
new (options: ModelConstructorOptions<TSchema>): {
[key: string]: unknown;
/** Document version hash */
readonly __v: string | null;
/** Id of model instance */
_id: string | null;
/** Original record string that model was generated from */
readonly _originalRecordString: string | null;
/** Private id tracking property */
"__#2@#_id": string | null;
/** Save a document to the database */
save(options?: ModelSaveOptions): Promise<ModelCompositeValue<TSchema>>;
/** Convert model instance to multivalue string */
"__#2@#convertToMvString"(): string;
/** Validate the model instance */
"__#2@#validate"(): asserts this is {
_id: string;
};
/** Build a list of foreign key definitions to be used by the database for foreign key validation */
"__#2@#buildForeignKeyDefinitions"(): BuildForeignKeyDefinitionsResult[];
_raw: TSchema extends Schema<SchemaDefinition, Record<never, never>> ? never : import("./types").MvRecord;
_transformationErrors: import("./errors").TransformDataError[];
readonly "__#1@#schema": TSchema;
"__#1@#record": import("./types").MvRecord;
readonly "__#1@#isSubdocument": boolean;
transformDocumentToRecord(): import("./types").MvRecord;
buildForeignKeyDefinitions(): BuildForeignKeyDefinitionsResult[];
validate(): Map<string, string[]>;
"__#1@#transformRecordToDocument"(): void;
};
/** Connection instance which constructed this model definition */
readonly connection: Connection;
/** Database file this model acts against */
readonly file: string;
/** Schema that defines this model */
readonly schema: TSchema;
/** Log handler instance used for diagnostic logging */
readonly "__#2@#logHandler": LogHandler;
/** Database server delimiters */
readonly "__#2@#dbServerDelimiters": DbServerDelimiters;
/**
* Check to see if a record is locked by another user/process
* @returns `true` when record is locked
*/
checkForRecordLockById(id: string, options?: ModelCheckForRecordLockByIdOptions): Promise<boolean>;
/** Delete a document */
deleteById(id: string, options?: ModelDeleteByIdOptions): Promise<ModelCompositeValue<TSchema> | null>;
/** Find documents via query */
find(selectionCriteria?: Filter<TSchema>, options?: ModelFindOptions<TSchema>): Promise<ModelCompositeValue<TSchema>[]>;
/** Find documents via query, returning them along with a count */
findAndCount(selectionCriteria?: Filter<TSchema>, options?: ModelFindOptions<TSchema>): Promise<ModelFindAndCountResult<TSchema>>;
/** Find a document by its id */
findById(id: string, options?: ModelFindByIdOptions): Promise<ModelCompositeValue<TSchema> | null>;
/** Find multiple documents by their ids */
findByIds(ids: string | string[], options?: ModelFindByIdOptions): Promise<(ModelCompositeValue<TSchema> | null)[]>;
/**
* Increment fields in a document by values
* @throws {Error} if schema is not defined
* @throws {Error} if no result is returned from increment operation
*/
increment(id: string, operations: IncrementOperation<TSchema>[], options?: ModelIncrementOptions): Promise<ModelIncrementResult<TSchema>>;
/** Read a DIR file type record directly from file system as Base64 string by its id */
readFileContentsById(id: string, options?: ModelReadFileContentsByIdOptions): Promise<string>;
/** Create a new Model instance from a record string */
"__#2@#createModelFromRecordString"(recordString: string, _id: string, __v?: string | null): ModelCompositeValue<TSchema>;
/** Format projection option */
"__#2@#formatProjection"(projection?: string[]): number[] | null;
/**
* Format increment operations to be sent to the database
*/
"__#2@#formatIncrementOperations"(operations: IncrementOperation<TSchema>[]): DbSubroutineInputIncrementOperation[];
createSubdocumentFromRecord<TSchema_1 extends Schema | null>(schema: TSchema_1, record: import("./types").MvRecord): import("./Document").DocumentCompositeValue<TSchema_1>;
createSubdocumentFromData<TSchema_1 extends Schema>(schema: TSchema_1, data: DocumentData<TSchema_1>): import("./Document").DocumentCompositeValue<TSchema_1>;
createDocumentFromRecordString<TSchema_1 extends Schema | null>(schema: TSchema_1, recordString: string, dbServerDelimiters: DbServerDelimiters): import("./Document").DocumentCompositeValue<TSchema_1>;
convertMvStringToArray(recordString: string, dbServerDelimiters: DbServerDelimiters): import("./types").MvRecord;
};
export default compileModel;