docorm
Version:
Persistence layer with ORM features for JSON documents
156 lines • 5.79 kB
TypeScript
import { type PropertyPathStr, type Schema } from 'schema-fun';
import { Dao } from './dao.js';
import { QueryClause, QueryOrder } from './queries.js';
type SyncOrAsync<T extends (...args: any) => any> = ((...args: Parameters<T>) => ReturnType<T>) | ((...args: Parameters<T>) => Promise<ReturnType<T>>);
export type HttpMethod = 'get' | 'post' | 'put' | 'delete';
export type Id = string | number;
export type EntityTypeName = string;
export type Entity = {
_id: Id;
[key: string]: any;
};
export interface Collection {
name: string;
subpath: string;
entityType: EntityTypeName;
persistence: 'inverse-ref' | 'ref' | 'subdocument';
foreignKeyPath?: string;
}
export interface User {
_id: string;
initials: string;
}
export interface DeleteDaoCallbackOptions {
dao: Dao;
}
export interface SaveDaoCallbackOptions {
dao: Dao;
draftBatchId?: string;
}
export interface BeforeSaveRestCallbackOptions {
user?: User;
draftBatchId?: string;
parentCollections: Collection[];
parentDaos: Dao[];
parentIds: string[];
dao: Dao;
}
export interface AfterSaveRestCallbackOptions {
user?: User;
draftBatchId?: string;
parentCollections: Collection[];
parentDaos: Dao[];
parentIds: string[];
}
export interface BeforeListItemsRestCallbackOptions {
user?: User;
draftBatchId?: string;
parentCollections: Collection[];
parentDaos: Dao[];
parentIds: string[];
query?: QueryClause;
order?: QueryOrder;
offset?: number;
limit?: number;
}
export interface BeforeValidateRestCallbackOptions {
user?: User;
draftBatchId?: string;
parentCollections: Collection[];
parentDaos: Dao[];
parentIds: string[];
dao: Dao;
}
export interface DbCallbacks {
beforeInsert?: (SyncOrAsync<(item: Entity, options: SaveDaoCallbackOptions) => void>)[];
afterInsert?: (SyncOrAsync<(item: Entity, options: SaveDaoCallbackOptions) => void>)[];
beforeUpdate?: (SyncOrAsync<(oldItem: Entity, newItem: Entity, options: SaveDaoCallbackOptions) => void>)[];
beforeUpdateWithoutOriginal?: (SyncOrAsync<(newItem: Entity, options: SaveDaoCallbackOptions) => void>)[];
afterUpdateWithoutOriginal?: (SyncOrAsync<(newItem: Entity, options: SaveDaoCallbackOptions) => void>)[];
afterUpdate?: (SyncOrAsync<(oldItem: Entity, newItem: Entity, options: SaveDaoCallbackOptions) => void>)[];
beforeDelete?: (SyncOrAsync<(id: Id, options: DeleteDaoCallbackOptions) => void>)[];
afterDelete?: (SyncOrAsync<(id: Id, options: DeleteDaoCallbackOptions) => void>)[];
beforeEmbedRelatedItemCopy?: (SyncOrAsync<(item: Entity, relatedItemPath: string, relatedItemEntityType: EntityType, relatedItem: Entity) => Entity>)[];
}
export interface RestCallbacks {
beforeCreate?: (SyncOrAsync<(item: Entity, options: BeforeSaveRestCallbackOptions) => void>)[];
afterCreate?: (SyncOrAsync<(item: Entity, options: AfterSaveRestCallbackOptions) => void>)[];
beforeListItems?: (SyncOrAsync<(options: BeforeListItemsRestCallbackOptions) => void>)[];
beforeUpdate?: (SyncOrAsync<(item: Entity, options: BeforeSaveRestCallbackOptions) => Entity>)[];
afterUpdate?: (SyncOrAsync<(item: Entity, options: AfterSaveRestCallbackOptions) => void>)[];
beforeValidateCreate?: (SyncOrAsync<(item: Entity, options: BeforeValidateRestCallbackOptions) => void>)[];
beforeValidateUpdate?: (SyncOrAsync<(item: Entity, options: BeforeValidateRestCallbackOptions) => void>)[];
}
export interface EntityTypeDefinition {
parent?: EntityTypeName;
name: EntityTypeName;
abstract?: boolean;
restCollection: string;
commonTitle: string;
title: string;
allowsDrafts?: boolean;
schemaId: string;
table?: string;
mapping?: {
idColumn?: string;
jsonColumn?: string;
readonly?: boolean;
};
import?: {
propertyMappings?: [];
};
dbCallbacks?: DbCallbacks;
history?: {
trackChange?: boolean | ((oldItem: Entity, newItem: Entity) => boolean | Promise<boolean>);
};
derivedProperties?: {
[propertyName: string]: (item: Entity) => any;
};
restCallbacks?: RestCallbacks;
extraCollectionActions: {
[actionName: string]: {
method: HttpMethod;
handler: (req: any, res: any, next: any, dao: Dao) => void | Promise<void>;
};
};
extraInstanceActions: {
[actionName: string]: {
method: HttpMethod;
handler: (req: any, res: any, next: any, item: Entity, dao: Dao) => void | Promise<void>;
};
};
}
export interface PropertyMapping {
column: string;
propertyPath: PropertyPathStr;
}
export interface EntityTypeMapping {
table: string;
idColumn: string;
jsonColumn?: string;
propertyMappings: PropertyMapping[];
readonly: boolean;
}
export interface EntityType extends Omit<EntityTypeDefinition, 'abstract' | 'parent'> {
parent?: EntityType;
abstract: boolean;
schema: Schema;
mapping?: EntityTypeMapping;
}
export declare function registerEntityTypes(dirPath: string): Promise<void>;
export declare function getEntityType(name: string, { required }?: {
required?: boolean | undefined;
}): EntityType;
export declare function getEntityTypes(): Promise<{
[name: string]: EntityType;
}>;
export interface BuildColumnMapState {
knownSubschemas: {
[schemaId: string]: Schema;
};
}
export declare function makeUnproxiedEntityType(definition: EntityTypeDefinition): EntityType;
export declare function makeEntityType(definition: EntityTypeDefinition): EntityType;
export declare function calculateDerivedProperties(entityType: EntityType, item: Entity): Promise<void>;
export {};
//# sourceMappingURL=entity-types.d.ts.map