@rebasepro/server-postgresql 0.0.1-canary.eae7889 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/dist/index.es.js +458 -201
  2. package/dist/index.es.js.map +1 -1
  3. package/dist/index.umd.js +458 -201
  4. package/dist/index.umd.js.map +1 -1
  5. package/dist/server-postgresql/src/PostgresBackendDriver.d.ts +8 -1
  6. package/dist/server-postgresql/src/schema/introspect-db-inference.d.ts +5 -0
  7. package/dist/server-postgresql/src/schema/introspect-db-logic.d.ts +117 -0
  8. package/dist/server-postgresql/src/schema/introspect-db.d.ts +1 -0
  9. package/dist/server-postgresql/src/services/EntityPersistService.d.ts +9 -0
  10. package/dist/types/src/controllers/auth.d.ts +8 -2
  11. package/dist/types/src/controllers/client.d.ts +13 -0
  12. package/dist/types/src/controllers/collection_registry.d.ts +2 -1
  13. package/dist/types/src/controllers/data_driver.d.ts +36 -1
  14. package/dist/types/src/controllers/navigation.d.ts +18 -6
  15. package/dist/types/src/controllers/registry.d.ts +9 -1
  16. package/dist/types/src/controllers/side_entity_controller.d.ts +7 -0
  17. package/dist/types/src/rebase_context.d.ts +17 -0
  18. package/dist/types/src/types/backend_hooks.d.ts +187 -0
  19. package/dist/types/src/types/collections.d.ts +31 -11
  20. package/dist/types/src/types/component_ref.d.ts +47 -0
  21. package/dist/types/src/types/cron.d.ts +1 -1
  22. package/dist/types/src/types/entity_views.d.ts +6 -7
  23. package/dist/types/src/types/formex.d.ts +40 -0
  24. package/dist/types/src/types/index.d.ts +3 -0
  25. package/dist/types/src/types/plugins.d.ts +6 -3
  26. package/dist/types/src/types/properties.d.ts +72 -88
  27. package/dist/types/src/types/slots.d.ts +20 -10
  28. package/dist/types/src/types/translations.d.ts +6 -0
  29. package/examples/sdk-demo/node_modules/esbuild/LICENSE.md +21 -0
  30. package/examples/sdk-demo/node_modules/esbuild/README.md +3 -0
  31. package/examples/sdk-demo/node_modules/esbuild/bin/esbuild +223 -0
  32. package/examples/sdk-demo/node_modules/esbuild/install.js +289 -0
  33. package/examples/sdk-demo/node_modules/esbuild/lib/main.d.ts +716 -0
  34. package/examples/sdk-demo/node_modules/esbuild/lib/main.js +2242 -0
  35. package/examples/sdk-demo/node_modules/esbuild/package.json +49 -0
  36. package/package.json +6 -5
  37. package/src/PostgresBackendDriver.ts +32 -6
  38. package/src/cli.ts +68 -2
  39. package/src/data-transformer.ts +84 -1
  40. package/src/schema/doctor.ts +14 -2
  41. package/src/schema/generate-drizzle-schema-logic.ts +59 -30
  42. package/src/schema/introspect-db-inference.ts +238 -0
  43. package/src/schema/introspect-db-logic.ts +896 -0
  44. package/src/schema/introspect-db.ts +254 -0
  45. package/src/services/EntityFetchService.ts +16 -0
  46. package/src/services/EntityPersistService.ts +95 -13
  47. package/test/generate-drizzle-schema.test.ts +342 -0
  48. package/test/introspect-db-generation.test.ts +458 -0
  49. package/test/introspect-db-utils.test.ts +392 -0
  50. package/test/property-ordering.test.ts +395 -0
  51. package/test/relations.test.ts +4 -4
  52. package/test/unmapped-tables-safety.test.ts +345 -0
  53. package/jest-all.log +0 -3128
  54. package/jest.log +0 -49
  55. package/scratch.ts +0 -41
  56. package/test-drizzle-bug.ts +0 -18
  57. package/test-drizzle-out/0000_cultured_freak.sql +0 -7
  58. package/test-drizzle-out/0001_tiresome_professor_monster.sql +0 -1
  59. package/test-drizzle-out/meta/0000_snapshot.json +0 -55
  60. package/test-drizzle-out/meta/0001_snapshot.json +0 -63
  61. package/test-drizzle-out/meta/_journal.json +0 -20
  62. package/test-drizzle-prompt.sh +0 -2
  63. package/test-policy-prompt.sh +0 -3
  64. package/test-programmatic.ts +0 -30
  65. package/test-programmatic2.ts +0 -59
  66. package/test-schema-no-policies.ts +0 -12
  67. package/test_drizzle_mock.js +0 -3
  68. package/test_find_changed.mjs +0 -32
  69. package/test_hash.js +0 -14
  70. package/test_output.txt +0 -3145
@@ -3,7 +3,7 @@ import { BranchService } from "./services/BranchService";
3
3
  import { RealtimeService } from "./services/realtimeService";
4
4
  import { DatabasePoolManager } from "./databasePoolManager";
5
5
  import { DrizzleClient } from "./interfaces";
6
- import { User } from "@rebasepro/types";
6
+ import { User, RebaseClient } from "@rebasepro/types";
7
7
  import { PostgresCollectionRegistry } from "./collections/PostgresCollectionRegistry";
8
8
  import { DataDriver, DeleteEntityProps, Entity, EntityCollection, FetchCollectionProps, FetchEntityProps, ListenCollectionProps, ListenEntityProps, SaveEntityProps, RebaseData, TableMetadata, DatabaseAdmin } from "@rebasepro/types";
9
9
  import { HistoryService } from "./history/HistoryService";
@@ -19,6 +19,7 @@ export declare class PostgresBackendDriver implements DataDriver {
19
19
  branchService?: BranchService;
20
20
  user?: User;
21
21
  data: RebaseData;
22
+ client?: RebaseClient;
22
23
  /**
23
24
  * When true, realtime notifications are deferred until after the
24
25
  * wrapping transaction commits. Set by `withAuth` → `withTransaction`.
@@ -37,6 +38,12 @@ export declare class PostgresBackendDriver implements DataDriver {
37
38
  * allowing test spies applied after construction to take effect.
38
39
  */
39
40
  get admin(): DatabaseAdmin;
41
+ /**
42
+ * REST-optimised fetch service (include-aware eager-loading).
43
+ * Delegates to the underlying EntityFetchService which already
44
+ * implements the matching method signatures.
45
+ */
46
+ get restFetchService(): import("./services").EntityFetchService;
40
47
  private resolveCollectionCallbacks;
41
48
  fetchCollection<M extends Record<string, unknown>>({ path, collection, filter, limit, offset, startAfter, orderBy, searchString, order }: FetchCollectionProps<M>): Promise<Entity<M>[]>;
42
49
  listenCollection<M extends Record<string, unknown>>({ path, collection, filter, limit, offset, startAfter, orderBy, searchString, order, onUpdate, onError }: ListenCollectionProps<M>): () => void;
@@ -0,0 +1,5 @@
1
+ export interface InferenceResult {
2
+ propType?: string;
3
+ extra?: string;
4
+ }
5
+ export declare function inferPropertyFromData(columnName: string, pgDataType: string, currentPropType: string, sampleValues: unknown[], isPk: boolean): InferenceResult;
@@ -0,0 +1,117 @@
1
+ export interface TableRow {
2
+ table_name: string;
3
+ }
4
+ export interface TableColumn {
5
+ table_name: string;
6
+ column_name: string;
7
+ data_type: string;
8
+ udt_name: string;
9
+ is_nullable: string;
10
+ column_default: string | null;
11
+ }
12
+ export interface EnumValue {
13
+ enum_name: string;
14
+ enum_value: string;
15
+ sort_order: number;
16
+ }
17
+ export interface PrimaryKeyRow {
18
+ table_name: string;
19
+ column_name: string;
20
+ }
21
+ export interface ForeignKeyRow {
22
+ table_name: string;
23
+ column_name: string;
24
+ foreign_table_name: string;
25
+ foreign_column_name: string;
26
+ }
27
+ export interface TableMeta {
28
+ name: string;
29
+ columns: TableColumn[];
30
+ pks: string[];
31
+ fks: ForeignKeyRow[];
32
+ }
33
+ export declare function singularize(word: string): string;
34
+ /**
35
+ * Convert a snake_case name to a human-readable Title Case label.
36
+ * e.g. "created_at" -> "Created At", "customer_id" -> "Customer Id"
37
+ */
38
+ export declare function humanize(snakeName: string): string;
39
+ /**
40
+ * Convert a snake_case table name to a camelCase + "Collection" variable name.
41
+ * e.g. "company_token" -> "companyTokenCollection"
42
+ */
43
+ export declare function toCollectionVarName(tableName: string): string;
44
+ export declare function getIconForTable(tableName: string): string;
45
+ /**
46
+ * Map a PostgreSQL data type to a Rebase property type.
47
+ */
48
+ export declare function mapPgType(dataType: string): string;
49
+ export declare function buildEnumMap(enumValues: EnumValue[]): Map<string, string[]>;
50
+ export declare function buildTablesMap(tables: TableRow[], columns: TableColumn[], pks: PrimaryKeyRow[], fks: ForeignKeyRow[]): Map<string, TableMeta>;
51
+ export declare function identifyJoinTables(tablesMap: Map<string, TableMeta>): Set<string>;
52
+ /**
53
+ * Property metadata used to compute display priority.
54
+ * Keeps computePropertyPriority free of any TableMeta coupling.
55
+ */
56
+ export interface PropertyOrderingContext {
57
+ /** The resolved Rebase property type (e.g. "string", "number", "date", "relation"). */
58
+ propType: string;
59
+ /** Whether this column is a primary key. */
60
+ isPk: boolean;
61
+ /** Whether this column is an enum (USER-DEFINED with matching values). */
62
+ isEnum: boolean;
63
+ /** Whether this is a storage/file-upload field (detected from column name). */
64
+ isStorage: boolean;
65
+ /** The PostgreSQL data_type (e.g. "text", "character varying", "jsonb"). */
66
+ pgDataType: string;
67
+ /** The original column index in PostgreSQL (for stable tiebreaking). */
68
+ originalIndex: number;
69
+ }
70
+ /**
71
+ * Compute a numeric priority score for a property.
72
+ * Lower scores appear first in the generated `propertiesOrder` array.
73
+ *
74
+ * The system uses 14 tiers (0–139), with the original column index
75
+ * added as a fractional tiebreaker (originalIndex / 10000) to
76
+ * guarantee stable ordering within the same tier.
77
+ *
78
+ * Pure function — no side effects.
79
+ */
80
+ export declare function computePropertyPriority(columnName: string, ctx: PropertyOrderingContext): number;
81
+ /**
82
+ * Sort a `propertiesOrder` array using the priority heuristic.
83
+ * Returns a new sorted array; does not mutate the input.
84
+ *
85
+ * @param entries - Array of { key, columnName, propType, ... } objects
86
+ * carrying the information needed to compute priority.
87
+ */
88
+ export interface PropertyOrderEntry {
89
+ /** The property key in the generated collection (may differ from columnName for relations). */
90
+ key: string;
91
+ /** The ordering context for this property. */
92
+ ctx: PropertyOrderingContext;
93
+ }
94
+ export declare function sortPropertiesOrder(entries: PropertyOrderEntry[]): string[];
95
+ export interface GeneratedFile {
96
+ tableName: string;
97
+ fileName: string;
98
+ content: string;
99
+ }
100
+ /**
101
+ * Generate the full TypeScript file content for a single collection.
102
+ * Pure function — no I/O.
103
+ */
104
+ export declare function generateCollectionFile(tableName: string, meta: TableMeta, allFks: ForeignKeyRow[], joinTables: Set<string>, tablesMap: Map<string, TableMeta>, enumMap: Map<string, string[]>, sampleData?: Record<string, unknown>[]): string;
105
+ /**
106
+ * Generate the content for an index.ts file that re-exports all collections.
107
+ */
108
+ export declare function generateIndexContent(fileNames: string[]): string;
109
+ /**
110
+ * Merge new exports into existing index.ts content.
111
+ * Returns the merged content string.
112
+ */
113
+ export declare function mergeIndexContent(existingContent: string, newFileNames: string[]): string;
114
+ /**
115
+ * Safely extract the host portion of a database URL for logging.
116
+ */
117
+ export declare function safeHostFromUrl(url: string): string;
@@ -33,6 +33,15 @@ export declare class EntityPersistService {
33
33
  * Translate raw PostgreSQL / Drizzle errors into user-friendly messages.
34
34
  */
35
35
  private toUserFriendlyError;
36
+ /**
37
+ * Walk the error cause chain and return the deepest meaningful message.
38
+ */
39
+ private extractCauseMessage;
40
+ /**
41
+ * Strip the raw SQL query from a Drizzle "Failed query: ..." message,
42
+ * keeping only the error description.
43
+ */
44
+ private stripSqlFromMessage;
36
45
  /**
37
46
  * Extract the underlying PostgreSQL error from a Drizzle wrapper.
38
47
  * Drizzle wraps PG errors in a `cause` property.
@@ -80,8 +80,14 @@ export type AuthController<USER extends User = User, ExtraData = unknown> = {
80
80
  export interface AuthControllerExtended<USER extends User = User, ExtraData = unknown> extends AuthController<USER, ExtraData> {
81
81
  /** Login with email and password */
82
82
  emailPasswordLogin?(email: string, password: string): Promise<void>;
83
- /** Login with a Google ID token or trigger Google popup */
84
- googleLogin?(idToken: string): Promise<void>;
83
+ /** Login with a Google token or authorization code */
84
+ googleLogin?: {
85
+ (token: string, tokenType?: "idToken" | "accessToken"): Promise<void>;
86
+ (payload: {
87
+ code: string;
88
+ redirectUri: string;
89
+ }): Promise<void>;
90
+ };
85
91
  /** Register a new user */
86
92
  register?(email: string, password: string, displayName?: string): Promise<void>;
87
93
  /** Skip login (for anonymous access if enabled) */
@@ -167,4 +167,17 @@ export interface RebaseClient<DB = unknown> {
167
167
  email?: EmailService;
168
168
  /** Admin API for user and role management */
169
169
  admin?: AdminAPI;
170
+ /**
171
+ * The base HTTP URL of the backend server.
172
+ * Exposed by the SDK client (`@rebasepro/client`) and used to auto-derive
173
+ * the `ApiConfigProvider` URL.
174
+ */
175
+ baseUrl?: string;
176
+ /**
177
+ * WebSocket client for realtime subscriptions and admin capabilities.
178
+ * Exposed by the SDK client (`@rebasepro/client`). The shape is intentionally
179
+ * left as `unknown` in the base interface — callers should narrow via feature
180
+ * detection (e.g. `typeof ws.executeSql === "function"`).
181
+ */
182
+ ws?: unknown;
170
183
  }
@@ -36,7 +36,8 @@ export type CollectionRegistryController<DB = Record<string, unknown>, EC extend
36
36
  * Retrieve all the related parent collection ids for a given path
37
37
  * @param path
38
38
  */
39
- getParentCollectionIds: (path: string) => string[];
39
+ getParentCollectionSlugs: (path: string) => string[];
40
+ getParentEntityIds: (path: string) => string[];
40
41
  /**
41
42
  * Resolve paths from a list of ids
42
43
  * @param ids
@@ -144,12 +144,19 @@ export interface DataDriver {
144
144
  path: string;
145
145
  databaseId?: string;
146
146
  collection: EntityCollection;
147
- parentCollectionIds?: string[];
147
+ parentCollectionSlugs?: string[];
148
+ parentEntityIds?: string[];
148
149
  }) => Promise<boolean>;
149
150
  /**
150
151
  * Flag to indicate if the driver has requested the initialization of the text search index
151
152
  */
152
153
  needsInitTextSearch?: boolean;
154
+ /**
155
+ * Optional REST-optimised fetch service. When present, the REST API
156
+ * generator uses these methods instead of the generic `fetchEntity` /
157
+ * `fetchCollection` pipeline, enabling include-aware eager-loading.
158
+ */
159
+ restFetchService?: RestFetchService;
153
160
  /**
154
161
  * Return the admin capabilities of this driver.
155
162
  * @see SQLAdmin
@@ -158,3 +165,31 @@ export interface DataDriver {
158
165
  */
159
166
  admin?: import("../types/backend").DatabaseAdmin;
160
167
  }
168
+ /**
169
+ * REST-optimised fetch service exposed by drivers that support
170
+ * eager-loading of relations via `include`.
171
+ *
172
+ * The methods return flattened rows (`{ id, ...columns }`) rather
173
+ * than the `Entity<M>` wrapper used by the generic DataDriver API.
174
+ *
175
+ * @group DataDriver
176
+ */
177
+ export interface RestFetchService {
178
+ /**
179
+ * Fetch a collection of flattened entities with optional relation includes.
180
+ */
181
+ fetchCollectionForRest(collectionPath: string, options?: {
182
+ filter?: FilterValues<string>;
183
+ orderBy?: string;
184
+ order?: "desc" | "asc";
185
+ limit?: number;
186
+ offset?: number;
187
+ startAfter?: Record<string, unknown>;
188
+ searchString?: string;
189
+ databaseId?: string;
190
+ }, include?: string[]): Promise<Record<string, unknown>[]>;
191
+ /**
192
+ * Fetch a single flattened entity with optional relation includes.
193
+ */
194
+ fetchEntityForRest(collectionPath: string, entityId: string | number, include?: string[], databaseId?: string): Promise<Record<string, unknown> | null>;
195
+ }
@@ -144,17 +144,18 @@ export interface AppView {
144
144
  * It will still be accessible if you reach the specified path
145
145
  */
146
146
  hideFromNavigation?: boolean;
147
+ /**
148
+ * Navigation group for this view.
149
+ * Views sharing the same group name will be visually grouped
150
+ * together in the drawer and home page. If not set, the view
151
+ * falls into the default "Views" group.
152
+ */
153
+ group?: string;
147
154
  /**
148
155
  * Component to be rendered. This can be any React component, and can use
149
156
  * any of the provided hooks
150
157
  */
151
158
  view: React.ReactNode;
152
- /**
153
- * Optional field used to group top level navigation entries under a
154
- * navigation view.
155
- * This prop is ignored for admin views.
156
- */
157
- group?: string;
158
159
  /**
159
160
  * If true, a wildcard route (slug/*) is automatically registered
160
161
  * alongside the base route, enabling nested navigation within this view.
@@ -193,6 +194,17 @@ export interface NavigationGroupMapping {
193
194
  * List of collection ids or view paths that belong to this group.
194
195
  */
195
196
  entries: string[];
197
+ /**
198
+ * Configure which groups start collapsed.
199
+ * Set to `true` to collapse in both drawer and home page,
200
+ * or use an object to control each independently.
201
+ *
202
+ * @defaultValue false (expanded)
203
+ */
204
+ collapsedByDefault?: boolean | {
205
+ drawer?: boolean;
206
+ home?: boolean;
207
+ };
196
208
  }
197
209
  export interface NavigationEntry {
198
210
  id: string;
@@ -3,7 +3,7 @@ import type { EntityCollection } from "../types/collections";
3
3
  import type { EntityCollectionsBuilder } from "../types/builders";
4
4
  import type { EntityCustomView } from "../types/entity_views";
5
5
  import type { EntityAction } from "../types/entity_actions";
6
- import type { AppView } from "./navigation";
6
+ import type { AppView, NavigationGroupMapping } from "./navigation";
7
7
  /**
8
8
  * Options to enable the built-in collection editor.
9
9
  * When provided to `<RebaseCMS>`, the editor is auto-wired as a native feature.
@@ -25,6 +25,14 @@ export interface RebaseCMSConfig<EC extends EntityCollection = any> {
25
25
  entityViews?: EntityCustomView<any>[];
26
26
  entityActions?: EntityAction[];
27
27
  plugins?: any[];
28
+ /**
29
+ * Centralized configuration for how collections and views are grouped
30
+ * in the navigation sidebar and home page.
31
+ * Each mapping defines a named group and the collection/view slugs
32
+ * that belong to it. The array order determines group display order.
33
+ * Entry order within each group determines card order.
34
+ */
35
+ navigationGroupMappings?: NavigationGroupMapping[];
28
36
  /**
29
37
  * Enable the built-in visual collection/schema editor.
30
38
  * Pass `true` for zero-config, or an options object for fine-grained control.
@@ -62,6 +62,13 @@ export interface EntitySidePanelProps<M extends Record<string, unknown> = Record
62
62
  * Allow the user to open the entity fullscreen
63
63
  */
64
64
  allowFullScreen?: boolean;
65
+ /**
66
+ * Pre-populate the form with these values when creating a new entity.
67
+ * Only applied when `entityId` is not set (i.e. the form is in "new" mode).
68
+ * Useful for actions that fetch data from an external source (e.g. a URL)
69
+ * and want to pre-fill the document before the user saves.
70
+ */
71
+ defaultValues?: Partial<M>;
65
72
  }
66
73
  /**
67
74
  * Controller to open the side dialog displaying entity forms
@@ -3,6 +3,7 @@ import type { AuthController } from "./controllers/auth";
3
3
  import type { StorageSource } from "./controllers/storage";
4
4
  import type { UserConfigurationPersistence } from "./controllers/local_config_persistence";
5
5
  import type { DatabaseAdmin } from "./types/backend";
6
+ import type { RebaseClient } from "./controllers/client";
6
7
  import type { RebaseData } from "./controllers/data";
7
8
  import type { User } from "./users";
8
9
  import type { UserManagementDelegate } from "./types/user_management_delegate";
@@ -12,6 +13,22 @@ import type { UserManagementDelegate } from "./types/user_management_delegate";
12
13
  * @group Hooks and utilities
13
14
  */
14
15
  export type RebaseCallContext<USER extends User = User> = {
16
+ /**
17
+ * The Rebase client instance.
18
+ * Available in all entity callbacks (beforeSave, afterSave, afterRead,
19
+ * beforeDelete, afterDelete) and in CollectionActionsProps via context.
20
+ * Use it to call backend functions, access data, storage, etc.
21
+ *
22
+ * @example
23
+ * // In a beforeSave callback:
24
+ * const result = await context.client.functions.invoke('my-function', { ... });
25
+ *
26
+ * @example
27
+ * // In a CollectionAction component:
28
+ * const { client } = props.context;
29
+ * const result = await client.functions.invoke('extract-job', { url });
30
+ */
31
+ client: RebaseClient<any>;
15
32
  /**
16
33
  * Unified data access — `context.data.products.create(...)`.
17
34
  * Access any collection as a dynamic property.
@@ -0,0 +1,187 @@
1
+ import type { AdminUser, AdminRole } from "../controllers/client";
2
+ /**
3
+ * Context passed to every backend hook.
4
+ * Provides information about the request that triggered the hook.
5
+ * @group Backend Hooks
6
+ */
7
+ export interface BackendHookContext {
8
+ /** The currently authenticated user making the request (if any) */
9
+ requestUser?: {
10
+ userId: string;
11
+ roles: string[];
12
+ };
13
+ /** The HTTP method of the request */
14
+ method: "GET" | "POST" | "PUT" | "DELETE";
15
+ }
16
+ /**
17
+ * Hooks for intercepting Admin User data at the API boundary.
18
+ *
19
+ * These hooks run on the server after the database operation completes
20
+ * but before the response is sent to the client.
21
+ *
22
+ * @group Backend Hooks
23
+ */
24
+ export interface UserHooks {
25
+ /**
26
+ * Transform a user record after it's read from the database,
27
+ * before it's returned to the client.
28
+ *
29
+ * Return the modified user, or `null` to filter it out entirely
30
+ * (the user won't appear in listings or individual fetches).
31
+ */
32
+ afterRead?(user: AdminUser, context: BackendHookContext): AdminUser | null | Promise<AdminUser | null>;
33
+ /**
34
+ * Transform user data before it's written to the database.
35
+ * Runs on POST (create) and PUT (update).
36
+ *
37
+ * Return the (possibly modified) data to proceed with the save.
38
+ * Throw an error to abort the operation.
39
+ */
40
+ beforeSave?(data: {
41
+ email?: string;
42
+ displayName?: string;
43
+ roles?: string[];
44
+ }, context: BackendHookContext): {
45
+ email?: string;
46
+ displayName?: string;
47
+ roles?: string[];
48
+ } | Promise<{
49
+ email?: string;
50
+ displayName?: string;
51
+ roles?: string[];
52
+ }>;
53
+ /**
54
+ * Called after a user is successfully created or updated.
55
+ * Useful for side-effects like sending notifications.
56
+ */
57
+ afterSave?(user: AdminUser, context: BackendHookContext): void | Promise<void>;
58
+ /**
59
+ * Called before a user is deleted. Throw to prevent deletion.
60
+ */
61
+ beforeDelete?(userId: string, context: BackendHookContext): void | Promise<void>;
62
+ /**
63
+ * Called after a user is successfully deleted.
64
+ */
65
+ afterDelete?(userId: string, context: BackendHookContext): void | Promise<void>;
66
+ }
67
+ /**
68
+ * Hooks for intercepting Admin Role data at the API boundary.
69
+ * @group Backend Hooks
70
+ */
71
+ export interface RoleHooks {
72
+ /**
73
+ * Transform a role record after it's read from the database,
74
+ * before it's returned to the client.
75
+ *
76
+ * Return the modified role, or `null` to filter it out entirely.
77
+ */
78
+ afterRead?(role: AdminRole, context: BackendHookContext): AdminRole | null | Promise<AdminRole | null>;
79
+ }
80
+ /**
81
+ * Hooks for intercepting collection entity data at the REST API boundary.
82
+ *
83
+ * These run **after** per-collection `EntityCallbacks` (which execute inside
84
+ * the DataDriver) and provide a single cross-cutting interception point for
85
+ * ALL collections flowing through the REST API.
86
+ *
87
+ * Every callback receives the collection `slug` so you can target specific
88
+ * collections or apply logic globally.
89
+ *
90
+ * @group Backend Hooks
91
+ */
92
+ export interface DataHooks {
93
+ /**
94
+ * Transform an entity after it's read from the database,
95
+ * before it's returned to the client.
96
+ *
97
+ * Runs for both list (GET /:slug) and single (GET /:slug/:id) fetches.
98
+ * Return the modified entity, or `null` to filter it out.
99
+ *
100
+ * @param slug - The collection slug (e.g. "orders", "products")
101
+ * @param entity - The flattened entity object (id + values merged)
102
+ * @param context - Request context (authenticated user, HTTP method)
103
+ */
104
+ afterRead?(slug: string, entity: Record<string, unknown>, context: BackendHookContext): Record<string, unknown> | null | Promise<Record<string, unknown> | null>;
105
+ /**
106
+ * Transform entity values before they are written to the database.
107
+ * Runs on POST (create) and PUT (update).
108
+ *
109
+ * Return the (possibly modified) values. Throw to abort the save.
110
+ *
111
+ * @param slug - The collection slug
112
+ * @param values - The raw request body values
113
+ * @param entityId - The entity ID (only present on updates)
114
+ * @param context - Request context
115
+ */
116
+ beforeSave?(slug: string, values: Record<string, unknown>, entityId: string | undefined, context: BackendHookContext): Record<string, unknown> | Promise<Record<string, unknown>>;
117
+ /**
118
+ * Called after an entity is successfully saved (created or updated).
119
+ * Useful for side-effects like syncing to external systems.
120
+ *
121
+ * @param slug - The collection slug
122
+ * @param entity - The saved entity (flattened)
123
+ * @param context - Request context
124
+ */
125
+ afterSave?(slug: string, entity: Record<string, unknown>, context: BackendHookContext): void | Promise<void>;
126
+ /**
127
+ * Called before an entity is deleted. Throw to prevent deletion.
128
+ *
129
+ * @param slug - The collection slug
130
+ * @param entityId - The entity ID being deleted
131
+ * @param context - Request context
132
+ */
133
+ beforeDelete?(slug: string, entityId: string, context: BackendHookContext): void | Promise<void>;
134
+ /**
135
+ * Called after an entity is successfully deleted.
136
+ *
137
+ * @param slug - The collection slug
138
+ * @param entityId - The deleted entity ID
139
+ * @param context - Request context
140
+ */
141
+ afterDelete?(slug: string, entityId: string, context: BackendHookContext): void | Promise<void>;
142
+ }
143
+ /**
144
+ * Backend-level hooks for intercepting data at the API boundary.
145
+ *
146
+ * These hooks run server-side after database operations complete and before
147
+ * API responses are sent.
148
+ *
149
+ * - `users` / `roles` — intercept admin user and role management endpoints
150
+ * - `data` — intercept ALL collection entity data flowing through the REST API
151
+ *
152
+ * `data` hooks complement per-collection `EntityCallbacks`. Entity callbacks
153
+ * run inside the DataDriver (close to the DB); data hooks run at the HTTP
154
+ * boundary (close to the client). Use data hooks for cross-cutting concerns
155
+ * like audit logging, response enrichment, or field masking.
156
+ *
157
+ * @example
158
+ * ```typescript
159
+ * const hooks: BackendHooks = {
160
+ * data: {
161
+ * afterRead(slug, entity, ctx) {
162
+ * // Mask PII for non-admin users across all collections
163
+ * if (!ctx.requestUser?.roles.includes("admin") && entity.email) {
164
+ * return { ...entity, email: "***" };
165
+ * }
166
+ * return entity;
167
+ * }
168
+ * },
169
+ * users: {
170
+ * afterRead(user, ctx) {
171
+ * if (user.email.endsWith("@system.internal")) return null;
172
+ * return user;
173
+ * }
174
+ * }
175
+ * };
176
+ * ```
177
+ *
178
+ * @group Backend Hooks
179
+ */
180
+ export interface BackendHooks {
181
+ /** Hooks for intercepting user management data */
182
+ users?: UserHooks;
183
+ /** Hooks for intercepting role management data */
184
+ roles?: RoleHooks;
185
+ /** Hooks for intercepting ALL collection entity data via the REST API */
186
+ data?: DataHooks;
187
+ }