UNPKG

trilogy

Version:

TypeScript SQLite layer with support for both native C++ & pure JavaScript drivers.

132 lines (131 loc) 5.8 kB
import { Fn, Criteria, CriteriaNormalized, CreateOptions, UpdateOptions } from './types'; export declare type HookOptions = CreateOptions | UpdateOptions | {}; export declare type OnQueryOptions = { includeInternal?: boolean; }; export declare type OnQueryContext = [string, boolean]; export declare type OnQueryCallback = Fn<[string]>; export declare type BeforeCreateCallback<D> = Fn<[D | Partial<D>, CreateOptions]>; export declare type AfterCreateCallback<D> = Fn<[D, CreateOptions]>; export declare type BeforeUpdateCallback<D> = Fn<[D | Partial<D>, CriteriaNormalized<D>, UpdateOptions]>; export declare type AfterUpdateCallback<D> = Fn<[D[], UpdateOptions]>; export declare type BeforeRemoveCallback<D> = Fn<[CriteriaNormalized<D>, {}]>; export declare type AfterRemoveCallback<D> = Fn<[D[], {}]>; export declare type HookCallback<D> = OnQueryCallback | BeforeCreateCallback<D> | AfterCreateCallback<D> | BeforeUpdateCallback<D> | AfterUpdateCallback<D> | BeforeRemoveCallback<D> | AfterRemoveCallback<D>; export declare type HookResult = { prevented: boolean; }; export declare enum Hook { OnQuery = "ON_QUERY", BeforeCreate = "BEFORE_CREATE", AfterCreate = "AFTER_CREATE", BeforeUpdate = "BEFORE_UPDATE", AfterUpdate = "AFTER_UPDATE", BeforeRemove = "BEFORE_REMOVE", AfterRemove = "AFTER_REMOVE" } export declare const EventCancellation: unique symbol; /** * Base implementation of lifecycle hooks inherited by all model instances. */ export declare class Hooks<D> { private _onQuery; private _onQueryAll; private _beforeCreate; private _afterCreate; private _beforeUpdate; private _afterUpdate; private _beforeRemove; private _afterRemove; /** * The `onQuery` hook is called each time a query is run on the database, * and receives the query in string form. * * @param fn Function called when the hook is triggered * @param [options] * * @returns Unsubscribe function that removes the subscriber when called */ onQuery(fn: OnQueryCallback, options?: OnQueryOptions): () => boolean; /** * Before an object is created, the beforeCreate hook is called with the * object. * * @remarks * This hook occurs before casting, so if a subscriber to this hook * modifies the incoming object those changes will be subject to casting. * It's also possible to prevent the object from being created entirely * by returning the EventCancellation symbol from a subscriber callback. * * @param fn Function called when the hook is triggered * * @returns Unsubscribe function that removes the subscriber when called */ beforeCreate(fn: BeforeCreateCallback<D>): () => boolean; /** * When an object is created, that object is returned to you and the * `afterCreate` hook is called with it. * * @param fn Function called when the hook is triggered * * @returns Unsubscribe function that removes the subscriber when called */ afterCreate(fn: AfterCreateCallback<D>): () => boolean; /** * Prior to an object being updated the `beforeUpdate` hook is called with the * update delta, or the incoming changes to be made, as well as the criteria. * * @remarks * Casting occurs after this hook. A subscriber could choose to cancel the * update by returning the EventCancellation symbol or alter the selection * criteria. * * @param fn Function called when the hook is triggered * * @returns Unsubscribe function that removes the subscriber when called */ beforeUpdate(fn: BeforeUpdateCallback<D>): () => boolean; /** * Subscribers to the `afterUpdate` hook receive modified objects after they * are updated. * * @param fn Function called when the hook is triggered * * @returns Unsubscribe function that removes the subscriber when called */ afterUpdate(fn: AfterUpdateCallback<D>): () => boolean; /** * Before object removal, the criteria for selecting those objects is passed * to the `beforeRemove` hook. * * @remarks * Casting occurs after this hook. Subscribers can modify the selection * criteria or prevent the removal entirely by returning the `EventCancellation` * symbol. * * @param fn Function called when the hook is triggered * * @returns Unsubscribe function that removes the subscriber when called */ beforeRemove(fn: BeforeRemoveCallback<D>): () => boolean; /** * A list of any removed objects is passed to the `afterRemove` hook. * * @param fn Function called when the hook is triggered * * @returns Unsubscribe function that removes the subscriber when called */ afterRemove(fn: AfterRemoveCallback<D>): () => boolean; /** * The main handler of all hooks. * * @internal */ _callHook<T = D>(hook: Hook.OnQuery, arg: OnQueryContext): Promise<HookResult>; _callHook<T = D>(hook: Hook.BeforeCreate, arg: T | Partial<T>, options?: CreateOptions): Promise<HookResult>; _callHook<T = D>(hook: Hook.AfterCreate, arg: T, options?: CreateOptions): Promise<HookResult>; _callHook<T = D>(hook: Hook.BeforeUpdate, arg: [T | Partial<T>, Criteria<T>], options?: UpdateOptions): Promise<HookResult>; _callHook<T = D>(hook: Hook.AfterUpdate, arg: T[], options?: UpdateOptions): Promise<HookResult>; _callHook<T = D>(hook: Hook.BeforeRemove, arg: Criteria<T>, options?: {}): Promise<HookResult>; _callHook<T = D>(hook: Hook.AfterRemove, arg: T[], options?: {}): Promise<HookResult>; }