UNPKG

@decaf-ts/core

Version:

Core persistence module for the decaf framework

84 lines (83 loc) 6.11 kB
import type { ContextOfRepository, Contextual, FlagsOf as ContextualFlagsOf } from "@decaf-ts/db-decorators"; import { IRepository, OperationKeys, type PrimaryKeyType } from "@decaf-ts/db-decorators"; import type { Constructor } from "@decaf-ts/decoration"; import type { ContextualArgs, ContextualizedArgs, MaybeContextualArg } from "./ContextualLoggedClass"; import { type AdapterFlags, type FlagsOf } from "../persistence/types"; import { Model, type ModelConstructor } from "@decaf-ts/decorator-validation"; import { Repository } from "../repository/Repository"; import { Context } from "../persistence/Context"; import { OrderDirection } from "../repository/constants"; import { DirectionLimitOffset } from "../query/index"; export declare abstract class Service<C extends Context<AdapterFlags> = Context<AdapterFlags>> { readonly name?: string | undefined; protected constructor(name?: string | undefined); /** * @description Creates repository flags for an operation * @summary Generates a set of flags that describe a database operation, combining default flags with overrides * @template F - The Repository Flags type * @template M - The model type * @param {OperationKeys} operation - The type of operation being performed * @param {Constructor<M>} model - The model constructor * @param {Partial<F>} flags - Custom flag overrides * @param {...any[]} args - Additional arguments * @return {Promise<F>} The complete set of flags */ protected flags(operation: OperationKeys | string, flags: Partial<FlagsOf<C>>, ...args: any[]): Promise<FlagsOf<C>>; /** * @description The context constructor for this adapter * @summary Reference to the context class constructor used by this adapter */ protected readonly Context: Constructor<C>; context(operation: OperationKeys.CREATE | OperationKeys.READ | OperationKeys.UPDATE | OperationKeys.DELETE | string, overrides: Partial<ContextualFlagsOf<C>>, ...args: any[]): Promise<C>; protected logCtx<ARGS extends any[]>(args: ARGS, method: ((...args: any[]) => any) | string, allowCreate?: boolean): Promise<ContextualizedArgs<any, ARGS>>; protected static logCtx<CONTEXT extends Context<any>, ARGS extends any[]>(this: Contextual, args: ARGS, operation: ((...args: any[]) => any) | string, allowCreate?: boolean, ...argz: any[]): Promise<ContextualizedArgs<CONTEXT, ARGS>>; /** * @description Retrieves a Service instance by name/class * @summary Looks up and returns a cached API instance by its name or constructor * @template A Type extending Api * @param {string | Constructor<A>} name - Name of the API or its constructor * @return {A} The requested API instance */ static get<A extends Service>(name: string | symbol | Constructor<A>): A; static boot<C extends Context<any> = any>(...args: MaybeContextualArg<C>): Promise<void>; } export declare abstract class ClientBasedService<CLIENT, CONF, C extends Context<any> = any> extends Service { protected _client?: CLIENT; protected _config?: CONF; protected constructor(); boot(...args: MaybeContextualArg<C>): Promise<void>; abstract initialize(...args: ContextualArgs<C>): Promise<{ config: CONF; client: CLIENT; }>; protected get config(): CONF; get client(): CLIENT; shutdown(...args: MaybeContextualArg<C>): Promise<void>; } export type ArrayMode = "one" | "many"; export declare class ModelService<M extends Model<boolean>, R extends Repository<M, any> = Repository<M, any>> extends Service implements IRepository<M, ContextOfRepository<R>> { private readonly clazz; protected _repository: R; get class(): Constructor<M>; get repo(): R; constructor(clazz: Constructor<M>, name?: string); static getService<M extends Model<boolean>, S extends ModelService<M>>(name: string | symbol | Constructor<M>): S; for(conf: any, ...args: any[]): this; create(model: M, ...args: MaybeContextualArg<ContextOfRepository<R>>): Promise<M>; createAll(models: M[], ...args: MaybeContextualArg<ContextOfRepository<R>>): Promise<M[]>; delete(key: PrimaryKeyType, ...args: MaybeContextualArg<ContextOfRepository<R>>): Promise<M>; deleteAll(keys: PrimaryKeyType[], ...args: MaybeContextualArg<ContextOfRepository<R>>): Promise<M[]>; read(key: PrimaryKeyType, ...args: MaybeContextualArg<ContextOfRepository<R>>): Promise<M>; readAll(keys: PrimaryKeyType[], ...args: MaybeContextualArg<ContextOfRepository<R>>): Promise<M[]>; query<M, R extends ArrayMode = "one">(methodName: string, ...args: unknown[]): Promise<R extends "one" ? M : M[]>; update(model: M, ...args: MaybeContextualArg<ContextOfRepository<R>>): Promise<M>; updateAll(models: M[], ...args: any[]): Promise<M[]>; listBy(key: keyof M, order: OrderDirection, ...args: MaybeContextualArg<ContextOfRepository<R>>): Promise<M[]>; paginateBy(key: keyof M, order: OrderDirection, ref: Omit<DirectionLimitOffset, "direction">, ...args: MaybeContextualArg<ContextOfRepository<R>>): Promise<import("../query/Paginator").SerializedPage<M>>; findOneBy(key: keyof M, value: any, ...args: MaybeContextualArg<ContextOfRepository<R>>): Promise<M>; findBy(key: keyof M, value: any, ...args: MaybeContextualArg<ContextOfRepository<R>>): Promise<M[]>; statement(name: string, ...args: MaybeContextualArg<ContextOfRepository<R>>): Promise<any>; protected logCtx<ARGS extends any[]>(args: ARGS, method: ((...args: any[]) => any) | string, allowCreate?: boolean): Promise<ContextualizedArgs<any, ARGS>>; static forModel<M extends Model<boolean>, S extends ModelService<M>>(this: new (model: ModelConstructor<M>) => S, model: ModelConstructor<M>, alias?: string | symbol): S; protected static logCtx<CONTEXT extends Context<any>, ARGS extends any[]>(this: Contextual, args: ARGS, operation: ((...args: any[]) => any) | string, allowCreate: boolean | undefined, overrides: Partial<FlagsOf<CONTEXT>> | undefined, constructor: ModelConstructor<any>): Promise<ContextualizedArgs<CONTEXT, ARGS>>; }