import type { CollectionConfig } from "./collections"; import type { EntityStatus, EntityValues } from "./entities"; import type { User } from "../users"; import type { RebaseCallContext } from "../call_context"; /** * Lifecycle callbacks for entity CRUD operations. * * Register per-collection on the collection's `callbacks` field, or globally * via `initializeRebaseBackend({ callbacks })`. Fires on **every** data path — REST API, * WebSocket / realtime subscriptions, and server-side writes through * `rebase.dataAsAdmin`. * * When both global and per-collection callbacks are registered, execution * order is: **global → collection → property callbacks**. * * @group Models */ export type CollectionCallbacks = Record, USER extends User = User> = { /** * Callback used after fetching data. * * Fires on every read path. Use this for security-critical redaction * (PII masking, row filtering) — no read path bypasses it. * * @param props */ afterRead?(props: AfterReadProps) : Promise> | Record; /** * Callback used before saving, you need to return the values that will get * saved. If you throw an error in this method the process stops, and an * HTTP error response is returned to the client. * This runs after schema validation. * * @param props */ beforeSave?(props: BeforeSaveProps) : Promise>> | Partial>; /** * Callback used when save is successful. * * @param props */ afterSave?(props: AfterSaveProps) : Promise | void; /** * Callback used when saving fails * @param props */ afterSaveError?(props: AfterSaveErrorProps) : Promise | void; /** * Callback used before the entity is deleted. * If you throw an error in this method the process stops, and an * HTTP error response is returned to the client. * * @param props */ beforeDelete?(props: BeforeDeleteProps): Promise | boolean | void; /** * Callback used after the entity is deleted. * * @param props */ afterDelete?(props: AfterDeleteProps): Promise | void; } /** * Parameters passed to hooks when a entity is fetched * @group Models */ export interface AfterReadProps = Record, USER extends User = User> { /** * Collection of the entity */ collection: CollectionConfig; /** * Full path of the admin where this collection is being fetched. * Might contain unresolved aliases. */ path: string; /** * Fetched row (flat — the table's columns) */ row: Record /** * Context of the app status */ context: RebaseCallContext; } /** * Parameters passed to hooks before a entity is saved * @group Models */ export type BeforeSaveProps = Record, USER extends User = User> = Omit, "id"> & { id?: string | number; } /** * Parameters passed to hooks before a entity is saved * @group Models */ export type AfterSaveErrorProps = Record, USER extends User = User> = Omit, "id"> & { id?: string | number; } /** * Parameters passed to hooks when a entity is saved * @group Models */ export interface AfterSaveProps = Record, USER extends User = User> { /** * Resolved collection of the entity */ collection: CollectionConfig; /** * Full path of the admin where this entity is being saved. * Might contain unresolved aliases. */ path: string; /** * ID of the entity */ id: string | number; /** * Values being saved */ values: Partial>; /** * Previous values */ previousValues?: Partial>; /** * New or existing entity */ status: EntityStatus; /** * Context of the app status */ context: RebaseCallContext; } /** * Parameters passed to hooks when a entity is deleted * @group Models */ export interface BeforeDeleteProps = Record, USER extends User = User> { /** * collection of the entity being deleted */ collection: CollectionConfig; /** * Path of the parent collection */ path: string; /** * Deleted entity id */ id: string | number; /** * Deleted row (flat — the table's columns) */ row: Record; /** * Context of the app status */ context: RebaseCallContext; } /** * Parameters passed to hooks after a entity is deleted * @group Models */ export interface AfterDeleteProps = Record, USER extends User = User> { /** * collection of the entity being deleted */ collection: CollectionConfig; /** * Path of the parent collection */ path: string; /** * Deleted entity id */ id: string | number; /** * Deleted row (flat — the table's columns) */ row: Record; /** * Context of the app status */ context: RebaseCallContext; }