import { IHookContext, IRunnerOptions, IDataPatch, Driver, Runner, Plugin, CurrentRunnerScope, RunnerContext, THookDeps, THookNames, Hook, AsyncState, Computed, Watcher, ISource, ReactiveChain, InputCompute, FComputedFunc, FComputedFuncAsync, FComputedFuncGenerator, IModifyFunction, AsyncInputComputeFn, GeneratorInputComputeFn, InputComputeFn } from '@polymita/signal'; export * from '@polymita/signal'; interface IModelRunnerOptions extends IRunnerOptions { modelIndexes?: IModelIndexesBase; runtime?: 'nodejs' | 'edge'; } interface IStackUnit { value: { [k: string]: any; }; source: { [k: string]: any; }; currentFieldPath: string; } interface IDiff { create: IStackUnit[]; update: IStackUnit[]; remove: IStackUnit[]; } /** * append types definition for signal plugin */ declare module '@polymita/signal' { interface IPlugins { Model?: { find(from: string, entity: string, query: IModelQuery['query']): Promise; update(from: string, entity: string, query: IModelData): Promise; updateMany(from: string, entity: string, query: IModelData): Promise<{ count: number; }>; upsert(from: string, entity: string, query: IModelData): Promise; create(from: string, entity: string, data: IModelCreateData): Promise; remove(from: string, entity: string, data: Omit, 'data'>): Promise; executeDiff(from: string, entity: string, d: IDiff): Promise; }; Context?: { postDiffToServer(entity: string, d: IDiff): Promise; postComputeToServer(c: IHookContext): Promise; postQueryToServer(c: IHookContext): Promise; }; } } interface IModelHookContext extends IHookContext { patch?: [string, IModelPatchRecord[]][]; } type IPatch = IDataPatch | IModelPatch; type IModelCreateData = Omit, 'where'> | Omit, 'where'>[]; type Enumerable = T | Array; type NestedIntFilter = { equals?: number; in?: Enumerable | number; notIn?: Enumerable | number; lt?: number; lte?: number; gt?: number; gte?: number; not?: NestedIntFilter | number; }; type IntFilter = { equals?: number; in?: Enumerable | number; notIn?: Enumerable | number; lt?: number; lte?: number; gt?: number; gte?: number; not?: NestedIntFilter | number; }; interface IModelData { where: { id?: number | IntFilter; } & Partial; data: { [k in keyof T]?: T[k] | { connect?: Partial; create?: Partial; }; }; include?: Record; } interface UpsertModelData { where: { id?: number | IntFilter; } & Partial; create: IModelData['data']; update: IModelData['data']; } type IModelPatchCreate = { op: 'create'; value: IModelCreateData; }; type IModelPatchUpdate = { op: 'update'; value: IModelData; }; type IModelPatchUpdateMany = { op: 'updateMany'; value: IModelData; }; type IModelPatchUpsert = { op: 'upsert'; value: UpsertModelData; }; type IModelPatchRemove = { op: 'remove'; value: Omit, 'data'>; }; type IModelPatch = IModelPatchCreate | IModelPatchUpdate | IModelPatchUpdateMany | IModelPatchRemove | IModelPatchUpsert; interface IModelOption { immediate?: boolean; unique?: boolean; autoRollback?: boolean; pessimisticUpdate?: boolean; ignoreClientEnable?: boolean; checkRefresh?: (ps: IPatch[]) => boolean; } interface IQuerySelect { [k: string]: boolean | { select: IQuerySelect; }; } interface IQueryInclude { [k: string]: boolean | IQueryWhere; } interface IQueryWhere { where?: { [k: string]: any; }; skip?: number; take?: number; include?: IQueryInclude; select?: IQuerySelect; orderBy?: { [k: string]: 'desc' | 'asc'; }; cursor?: { id?: number; }; } interface IModelQuery { entity: string; query: IQueryWhere; } interface IModelIndexesBase { [k: string]: string | IModelIndexesBase; } interface IModelPatchRecord { timing: number; patch: IModelPatch[]; } declare class ModelRunner extends Runner { driver: T; ScopeConstructor: typeof RunnerModelScope; scope: RunnerModelScope; constructor(driver: T, options?: Partial); prepareScope(args?: Parameters, initialContext?: IHookContext, plugin?: Plugin): RunnerModelScope; } declare function getGlobalModelEvent(): ModelEvent; declare function setGlobalModelEvent(me: ModelEvent | null): void; declare class ModelEvent { private data; listeners: Function[]; subscribe(f: () => void): () => void; from(arr: IModelHookContext['patch']): void; toArray(): [string, IModelPatchRecord[]][]; getRecord(m: { entity: string; }): IModelPatchRecord[]; pushPatch(m: { entity: string; }, p: IModelPatch[]): void; } declare function getModelRunnerScope(): RunnerModelScope; declare class RunnerModelScope extends CurrentRunnerScope { runnerContext: RunnerContext; initialContextDeps: THookDeps; initialContextNames: THookNames; plugin: Plugin; modelIndexes: IModelIndexesBase; modelIndexesPath: string[]; modelPatchEvents: ModelEvent; modelHookFactory: { model: typeof mountPrisma; prisma: typeof mountPrisma; writePrisma: typeof mountWritePrisma; writeModel: typeof writeModel; createPrisma: typeof mountCreatePrisma; updatePrisma: typeof mountUpdatePrisma; removePrisma: typeof mountRemovePrisma; computedInServer: typeof mountComputedInServer; inputComputeInServer: typeof mountInputComputeInServer; }; runtime: 'nodejs' | 'edge'; constructor(runnerContext: RunnerContext, initialContextDeps: THookDeps, initialContextNames: THookNames, plugin: Plugin, op: Partial); readyRelated(h: Hook): Promise; private notifyAllModel; getRealEntityName(entityKey: string): string; applyContextFromServer(c: IModelHookContext): void; triggerEnterComposeDriver(driverNamespace: string, driverName: string): () => void; hookNumberIndexDeps(): THookDeps; /** * get all related hook index according to passive hookIndex * design logic: * 1.getD -> getD -> getD * 2.setD in who's getD -> getD */ getRelatedHookIndexes(hookIndex: number): Set; getShallowRelatedHookIndexes(hookIndex: number): Set; getDependenceByModel(indexes: Set): Set; createBaseContext(): IHookContext; getRelatedIndexesByHook(h: Hook, excludeSelf?: boolean): Set; /** * as a response while receive a input context */ createPatchContext(): IModelHookContext; /** * as a input of other's Runner and trigger * need deliver context principles, sort by priority: * 1.model/cache(server) needn't * 2.state * 3.related set/get */ createActionContext(h?: Hook, args?: any[]): IHookContext; createShallowActionContext(h?: Hook, args?: any[]): IHookContext; createInputComputeContext(h?: Hook, args?: any[]): IHookContext; } declare abstract class Model extends AsyncState { options: IModelOption; scope: RunnerModelScope; queryWhereComputed: Computed | null; watcher: Watcher; entity: string; findGetters: Array<() => IModelQuery['query'] | undefined>; constructor(entity: string, getter: (() => IModelQuery['query'] | undefined) | undefined, options: IModelOption, scope: RunnerModelScope); injectFindGetter(fn: () => IModelQuery['query'] | undefined): void; setGetter(fn: () => IModelQuery['query'] | undefined): void; addDep(source: ISource, path?: (string | number)[]): void; notify(h?: Hook, p?: IPatch[], reactiveChain?: ReactiveChain): void; getQueryWhere(reactiveChain?: ReactiveChain): Promise; get value(): T; ready(): Promise; query(reactiveChain?: ReactiveChain): void; enableQuery(): Promise; abstract executeQuery(reactiveChain?: ReactiveChain): Promise; abstract exist(obj: Partial): Promise; abstract refresh(): Promise; abstract checkAndRefresh(): Promise; applyComputePatches(ic: InputCompute, reactiveChain?: ReactiveChain): Promise; abstract updateWithPatches(v: T, patches: IDataPatch[], silent: boolean, reactiveChain?: ReactiveChain): Promise; } declare enum EnumWriteMethods { create = "create", update = "update", updateMany = "updateMany", upsert = "upsert", remove = "remove", find = "find" } declare const writeInitialSymbol = "@@writePrismaInitial"; declare abstract class WriteModel extends AsyncState { sourceModelGetter: { _hook: Model; } | string; basicGetData: (() => Partial) | undefined; protected scope: RunnerModelScope; abstract identifier: string; entity: string; sourceModel?: Model; extraGetters: Record Partial>>; inputComputeModelPatchesMap: Map[]]>; constructor(sourceModelGetter: { _hook: Model; } | string, basicGetData: (() => Partial) | undefined, scope: RunnerModelScope); hasPatches(ic: InputCompute): boolean; refresh(): Promise; injectGetter(fn: () => Partial, method: EnumWriteMethods): void; getData(method: EnumWriteMethods): T[0]; setGetter(fn: () => Partial): void; abstract createRow(obj?: Partial): Promise; abstract updateRow(where: number, obj?: { [k: string]: any; }): Promise; abstract updateManyRows(where: number[], obj?: { [k: string]: any; }): Promise; abstract upsertRow(where: number | { [k: string]: any; }, obj?: { [k: string]: any; }): Promise; abstract removeRow(where: number): Promise; abstract executeModelPath(ps: IModelPatch[]): Promise; addModelPatches(value: T[0], patches: IModelPatch[]): void; applyComputePatches(ic: InputCompute, reactiveChain?: ReactiveChain): Promise; } /** TIP: code for example */ declare abstract class ClientModel extends Model { } /** TIP: code for example */ declare abstract class ClientWriteModel extends WriteModel { } /** * only used in writing data to model entity */ declare const writePrismaInitialSymbol: unique symbol; declare class Prisma extends Model { identifier: string; executeQuery(reactiveChain?: ReactiveChain): Promise; exist(obj: Partial): Promise; refresh(): Promise; updateWithPatches(v: T, patches: IDataPatch[], silent: boolean, reactiveChain?: ReactiveChain): Promise; checkAndRefresh(): Promise; } declare class WritePrisma extends WriteModel { identifier: string; executeModelPath(ps: IModelPatch[]): Promise; createRow(obj?: IModelData['data'], include?: { [k in keyof T[0]]: boolean; }): Promise; updateManyRows(whereIds: number[], obj?: IModelData['data']): Promise; upsertRow(where: (number | Record), obj?: IModelData['data']): Promise; updateRow(where: number, obj?: IModelData['data']): Promise; removeRow(where?: number): Promise; } declare class ClientPrisma extends Prisma { executeQuery(): Promise; updateWithPatches(): Promise; checkAndRefresh(): Promise; } /** * writePrisma in client will record the changing */ declare class ClientWritePrisma extends WritePrisma { createRow(obj?: Partial): Promise; updateRow(whereId: number, obj?: { [k: string]: any; }): Promise; updateManyRows(whereId: number[], obj?: { [k: string]: any; }): Promise; upsertRow(whereId: number | { [k: string]: any; }, obj?: { [k: string]: any; }): Promise; removeRow(whereId: number): Promise; } interface AsyncHook { init: boolean; getterPromise: Promise | null; startAsyncGetter: () => { end: () => void; valid: () => boolean; }; pending: boolean; } declare class AsyncInputCompute extends InputCompute implements AsyncHook { getter: InputComputeFn | AsyncInputComputeFn | GeneratorInputComputeFn; scope: RunnerModelScope; init: boolean; getterPromise: Promise | null; asyncCount: number; constructor(getter: InputComputeFn | AsyncInputComputeFn | GeneratorInputComputeFn, scope: RunnerModelScope); startAsyncGetter(): { end: () => void; valid: () => boolean; }; get pending(): boolean; } declare class InputComputeInServer

extends AsyncInputCompute

{ run(...args: any[]): Promise; } declare class ClientComputed extends Computed { getter: FComputedFunc | FComputedFuncAsync | FComputedFuncGenerator; protected scope: RunnerModelScope; constructor(getter: FComputedFunc | FComputedFuncAsync | FComputedFuncGenerator, scope: RunnerModelScope); run(): void; } /** * * * * * * original hooks api for "InServer" * * * * * * */ declare const mountModelHookFactory: { model: typeof mountPrisma; prisma: typeof mountPrisma; writePrisma: typeof mountWritePrisma; writeModel: typeof writeModel; createPrisma: typeof mountCreatePrisma; updatePrisma: typeof mountUpdatePrisma; removePrisma: typeof mountRemovePrisma; computedInServer: typeof mountComputedInServer; inputComputeInServer: typeof mountInputComputeInServer; }; declare const updateModelHookFactory: { model: typeof updateCyclePrisma; prisma: typeof updateCyclePrisma; writePrisma: typeof mountWritePrisma; writeModel: typeof writeModel; createPrisma: typeof mountCreatePrisma; updatePrisma: typeof mountUpdatePrisma; removePrisma: typeof mountRemovePrisma; computedInServer: typeof updateComputedInServer; inputComputeInServer: typeof updateInputComputeInServer; }; declare const modelHookFactoryFeatures: { /** * all hooks name list */ all: string[]; /** * need other hook as data source */ withSource: string[]; /** * manual calling by User or System */ initiativeCompute: string[]; /** * only compatibility with server * "model" & "prisma" maybe still run in client because of their query compute */ serverOnly: string[]; }; declare const hookFactoryNames: string[]; declare const hasSourceHookFactoryNames: string[]; declare const initiativeComputeHookFactoryNames: string[]; declare function updateCyclePrisma(e: string, q?: () => IModelQuery['query'] | undefined, op?: IModelOption): any; declare function updateInputComputeInServer(func: any): { (...args: any): Promise; _hook: InputComputeInServer; } | ((() => any) & { _hook: any; }); declare function mountInputComputeInServer(func: any): { (...args: any): Promise; _hook: InputComputeInServer; }; declare function updateComputedInServer(fn: FComputedFuncGenerator): (() => T) & { _hook: Computed; }; declare function updateComputedInServer(fn: FComputedFuncAsync): (() => T) & { _hook: Computed; }; declare function updateComputedInServer(fn: FComputedFunc): (() => T) & { _hook: Computed; }; declare function mountComputedInServer(fn: FComputedFuncGenerator): (() => T) & { _hook: Computed; }; declare function mountComputedInServer(fn: FComputedFuncAsync): (() => T) & { _hook: Computed; }; declare function mountComputedInServer(fn: FComputedFunc): (() => T) & { _hook: Computed; }; declare function mountPrisma(e: string, q?: () => IModelQuery['query'] | undefined, op?: IModelOption): { (): T; (parameter: IModifyFunction): [T, IDataPatch[]]; } & { _hook: Prisma; exist: (obj: Partial) => Promise; refresh: () => Promise; }; declare function mountWritePrisma(source: { _hook: Model; } | string, q: () => Partial): (() => never) & { _hook: WritePrisma; create: (obj?: T[0] extends infer T_1 ? { [k in keyof T_1]?: T[0][k] | { connect?: Partial; create?: Partial; }; } : never, include?: T[0] extends infer T_2 ? { [k_1 in keyof T_2]: boolean; } : never) => Promise; update: (where: number, obj?: T[0] extends infer T_1 ? { [k in keyof T_1]?: T[0][k] | { connect?: Partial; create?: Partial; }; } : never) => Promise; updateMany: (whereIds: number[], obj?: T[0] extends infer T_1 ? { [k in keyof T_1]?: T[0][k] | { connect?: Partial; create?: Partial; }; } : never) => Promise; upsert: (where: number | Record, obj?: T[0] extends infer T_1 ? { [k in keyof T_1]?: T[0][k] | { connect?: Partial; create?: Partial; }; } : never) => Promise; remove: (where?: number) => Promise; }; declare function mountCreatePrisma(source: { _hook: Model; } | string, q: () => Partial): ((receivedData?: Partial) => Promise) & { _method: string; _hook: WritePrisma; }; declare function mountUpdatePrisma(source: { _hook: Model; } | string, q: () => Partial): ((where: number, receivedData?: Partial) => Promise) & { _method: string; _hook: WritePrisma; }; declare function mountRemovePrisma(source: { _hook: Model; }, q: () => Partial): ((where?: number) => Promise) & { _method: string; _hook: WritePrisma; }; /** * * * * open user api * * * */ declare function computedInServer(fn: FComputedFuncGenerator): (() => T) & { _hook: Computed; }; declare function computedInServer(fn: FComputedFuncAsync): (() => T) & { _hook: Computed; }; declare function computedInServer(fn: FComputedFunc): (() => T) & { _hook: Computed; }; declare function inputComputeInServer(func: AsyncInputComputeFn): AsyncInputComputeFn & { _hook: Hook; }; declare function inputComputeInServer(func: GeneratorInputComputeFn): AsyncInputComputeFn & { _hook: Hook; }; declare function inputComputeInServer(func: InputComputeFn): AsyncInputComputeFn & { _hook: Hook; }; declare function model(e: string, q?: () => IModelQuery['query'] | undefined, op?: IModelOption): { (): T; (parameter: IModifyFunction): [T, IDataPatch[]]; } & { _hook: Prisma; exist: (obj: Partial) => Promise; refresh: () => Promise; }; declare function writeModel(source: { _hook: Model; }, q: () => Partial): (() => never) & { _hook: WritePrisma; create: (obj?: T[0] extends infer T_1 ? { [k in keyof T_1]?: T[0][k] | { connect?: Partial; create?: Partial; }; } : never, include?: T[0] extends infer T_2 ? { [k_1 in keyof T_2]: boolean; } : never) => Promise; update: (where: number, obj?: T[0] extends infer T_1 ? { [k in keyof T_1]?: T[0][k] | { connect?: Partial; create?: Partial; }; } : never) => Promise; updateMany: (whereIds: number[], obj?: T[0] extends infer T_1 ? { [k in keyof T_1]?: T[0][k] | { connect?: Partial; create?: Partial; }; } : never) => Promise; upsert: (where: number | Record, obj?: T[0] extends infer T_1 ? { [k in keyof T_1]?: T[0][k] | { connect?: Partial; create?: Partial; }; } : never) => Promise; remove: (where?: number) => Promise; }; declare function prisma(e: string, q?: () => IModelQuery['query'] | undefined, op?: IModelOption): { (): T; (parameter: IModifyFunction): [T, IDataPatch[]]; } & { _hook: Prisma; exist: (obj: Partial) => Promise; refresh: () => Promise; }; declare function writePrisma(source: { _hook: Model; } | string, q?: () => Partial): (() => never) & { _hook: WritePrisma; create: (obj?: T[0] extends infer T_1 ? { [k in keyof T_1]?: T[0][k] | { connect?: Partial; create?: Partial; }; } : never, include?: T[0] extends infer T_2 ? { [k_1 in keyof T_2]: boolean; } : never) => Promise; update: (where: number, obj?: T[0] extends infer T_1 ? { [k in keyof T_1]?: T[0][k] | { connect?: Partial; create?: Partial; }; } : never) => Promise; updateMany: (whereIds: number[], obj?: T[0] extends infer T_1 ? { [k in keyof T_1]?: T[0][k] | { connect?: Partial; create?: Partial; }; } : never) => Promise; upsert: (where: number | Record, obj?: T[0] extends infer T_1 ? { [k in keyof T_1]?: T[0][k] | { connect?: Partial; create?: Partial; }; } : never) => Promise; remove: (where?: number) => Promise; }; declare function createPrisma(source: { _hook: Model; } | string, q?: () => Partial): ((receivedData?: Partial) => Promise) & { _method: string; _hook: WritePrisma; }; declare function updatePrisma(source: { _hook: Model; } | string, q?: () => Partial): ((where: number, receivedData?: Partial) => Promise) & { _method: string; _hook: WritePrisma; }; declare function removePrisma(source: { _hook: Model; }, q?: () => Partial): ((where?: number) => Promise) & { _method: string; _hook: WritePrisma; }; /** * inject input data to Model as initial value */ type PartialGetter = { [K in keyof T]?: T[K]; }; type TGetterData = () => PartialGetter; type TModelGetter = ReturnType; declare function connectModel(modelGetter: TModelGetter, dataGetter: TGetterData): void; declare function injectWrite(modelGetter: ReturnType | ReturnType | ReturnType, dataGetter: TGetterData): void; declare function injectWrite(modelGetter: ReturnType>, dataGetter: () => IModelQuery['query'] | undefined): void; declare function injectWrite(modelGetter: ReturnType, method: EnumWriteMethods, dataGetter: TGetterData): void; declare const injectModel: typeof injectWrite; declare const isModelPatch: (p: IPatch) => boolean; declare function checkQueryWhere(where: IQueryWhere['where']): boolean; declare class DataGraphNode { id: number; type: THookDeps[0][0]; getBy: Set; toSet: Set; toCall: Set; constructor(id: number, type: THookDeps[0][0]); addGetBy(n: DataGraphNode): void; addToSet(n: DataGraphNode): void; addToCall(n: DataGraphNode): void; get children(): Set; getAllChildren(all?: Set): Set; } declare function dataGrachTraverse(source: DataGraphNode | DataGraphNode[], callback: (n: DataGraphNode, ancestors: DataGraphNode[]) => boolean | void): void; declare function getDependencies(rootNodes: Set, id: number): Set; declare function mapGraph(s: Set): Map; declare function mapGraphSetToIds(s: Set): Set; declare function getNextNodes(current: DataGraphNode): Set; declare function getPrevNodes(rootNodes: Set, current: { id: number; }): Set; declare function getDependentPrevNodes(rootNodes: Set, current: { id: number; }): Set; declare function getDependentPrevNodesWithBlock(rootNodes: Set, current: { id: number; }, blocks?: Set): Set; declare function getShallowDependentPrevNodes(rootNodes: Set, current: { id: number; }): Set; declare function getInfluencedNextNodes(rootNodes: Set, current: { id: number; }): Set; declare function getShallowInfluencedNextNodes(rootNodes: Set, current: { id: number; }): Set; /** * 根据依赖组构建图数据结构 * [ ['ic', 2, [1], [0]], ['ic', 3, [1], [2, 1]] * ] * 转化为: * 1 -(get by)-> 2 -(set to)-> 0 * -> 3 -(set to)-> 1 * -(call to)-> 2 **/ declare function constructDataGraph(contextDeps: THookDeps): Set; declare function getRelatedIndexes(index: number[] | number, contextDeps: THookDeps): Set; declare function getShallowRelatedIndexes(index: number[] | number, contextDeps: THookDeps): Set; /** * 根据patch计算diff,决定要进行的数据库操作 */ declare function calculateDiff(data: any | any[], ps: IDataPatch[]): { create: IStackUnit[]; update: IStackUnit[]; remove: IStackUnit[]; }; export { ClientComputed, ClientModel, ClientPrisma, ClientWriteModel, ClientWritePrisma, DataGraphNode, EnumWriteMethods, Enumerable, IDiff, IModelCreateData, IModelData, IModelHookContext, IModelIndexesBase, IModelOption, IModelPatch, IModelPatchCreate, IModelPatchRecord, IModelPatchRemove, IModelPatchUpdate, IModelPatchUpdateMany, IModelPatchUpsert, IModelQuery, IModelRunnerOptions, IPatch, IQueryWhere, IStackUnit, IntFilter, Model, ModelEvent, ModelRunner, NestedIntFilter, Prisma, RunnerModelScope, UpsertModelData, WriteModel, WritePrisma, calculateDiff, checkQueryWhere, computedInServer, connectModel, constructDataGraph, createPrisma, dataGrachTraverse, getDependencies, getDependentPrevNodes, getDependentPrevNodesWithBlock, getGlobalModelEvent, getInfluencedNextNodes, getModelRunnerScope, getNextNodes, getPrevNodes, getRelatedIndexes, getShallowDependentPrevNodes, getShallowInfluencedNextNodes, getShallowRelatedIndexes, hasSourceHookFactoryNames, hookFactoryNames, initiativeComputeHookFactoryNames, injectModel, injectWrite, inputComputeInServer, isModelPatch, mapGraph, mapGraphSetToIds, model, modelHookFactoryFeatures, mountModelHookFactory, prisma, removePrisma, setGlobalModelEvent, updateModelHookFactory, updatePrisma, writeInitialSymbol, writeModel, writePrisma, writePrismaInitialSymbol };