@rebasepro/server-postgresql 0.0.1-canary.dbf160a → 0.0.1-canary.e17585f

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 (74) hide show
  1. package/dist/index.es.js +683 -1362
  2. package/dist/index.es.js.map +1 -1
  3. package/dist/index.umd.js +614 -1293
  4. package/dist/index.umd.js.map +1 -1
  5. package/dist/server-postgresql/src/PostgresAdapter.d.ts +6 -0
  6. package/dist/server-postgresql/src/PostgresBackendDriver.d.ts +8 -1
  7. package/dist/server-postgresql/src/PostgresBootstrapper.d.ts +0 -5
  8. package/dist/server-postgresql/src/index.d.ts +1 -0
  9. package/dist/server-postgresql/src/schema/introspect-db-inference.d.ts +5 -0
  10. package/dist/server-postgresql/src/schema/introspect-db-logic.d.ts +44 -9
  11. package/dist/server-postgresql/src/services/EntityPersistService.d.ts +9 -0
  12. package/dist/server-postgresql/src/services/realtimeService.d.ts +12 -0
  13. package/dist/server-postgresql/src/websocket.d.ts +2 -1
  14. package/dist/types/src/controllers/auth.d.ts +8 -2
  15. package/dist/types/src/controllers/client.d.ts +13 -0
  16. package/dist/types/src/controllers/collection_registry.d.ts +2 -1
  17. package/dist/types/src/controllers/data_driver.d.ts +36 -1
  18. package/dist/types/src/controllers/navigation.d.ts +18 -6
  19. package/dist/types/src/controllers/registry.d.ts +9 -1
  20. package/dist/types/src/controllers/side_entity_controller.d.ts +7 -0
  21. package/dist/types/src/rebase_context.d.ts +17 -0
  22. package/dist/types/src/types/auth_adapter.d.ts +354 -0
  23. package/dist/types/src/types/backend_hooks.d.ts +187 -0
  24. package/dist/types/src/types/collections.d.ts +75 -11
  25. package/dist/types/src/types/component_ref.d.ts +47 -0
  26. package/dist/types/src/types/cron.d.ts +1 -1
  27. package/dist/types/src/types/database_adapter.d.ts +90 -0
  28. package/dist/types/src/types/entity_views.d.ts +6 -7
  29. package/dist/types/src/types/formex.d.ts +40 -0
  30. package/dist/types/src/types/index.d.ts +5 -0
  31. package/dist/types/src/types/plugins.d.ts +6 -3
  32. package/dist/types/src/types/properties.d.ts +72 -88
  33. package/dist/types/src/types/slots.d.ts +20 -10
  34. package/dist/types/src/types/translations.d.ts +12 -0
  35. package/package.json +5 -5
  36. package/src/PostgresAdapter.ts +52 -0
  37. package/src/PostgresBackendDriver.ts +49 -7
  38. package/src/PostgresBootstrapper.ts +4 -7
  39. package/src/auth/ensure-tables.ts +3 -121
  40. package/src/cli.ts +10 -2
  41. package/src/data-transformer.ts +84 -1
  42. package/src/index.ts +1 -0
  43. package/src/schema/doctor.ts +14 -2
  44. package/src/schema/generate-drizzle-schema-logic.ts +59 -30
  45. package/src/schema/introspect-db-inference.ts +238 -0
  46. package/src/schema/introspect-db-logic.ts +365 -61
  47. package/src/schema/introspect-db.ts +66 -23
  48. package/src/services/EntityFetchService.ts +16 -0
  49. package/src/services/EntityPersistService.ts +95 -13
  50. package/src/services/realtimeService.ts +35 -0
  51. package/src/utils/drizzle-conditions.ts +6 -0
  52. package/src/websocket.ts +60 -11
  53. package/test/generate-drizzle-schema.test.ts +342 -0
  54. package/test/introspect-db-generation.test.ts +32 -10
  55. package/test/property-ordering.test.ts +395 -0
  56. package/test/relations.test.ts +4 -4
  57. package/jest-all.log +0 -3128
  58. package/jest.log +0 -49
  59. package/scratch.ts +0 -41
  60. package/test-drizzle-bug.ts +0 -18
  61. package/test-drizzle-out/0000_cultured_freak.sql +0 -7
  62. package/test-drizzle-out/0001_tiresome_professor_monster.sql +0 -1
  63. package/test-drizzle-out/meta/0000_snapshot.json +0 -55
  64. package/test-drizzle-out/meta/0001_snapshot.json +0 -63
  65. package/test-drizzle-out/meta/_journal.json +0 -20
  66. package/test-drizzle-prompt.sh +0 -2
  67. package/test-policy-prompt.sh +0 -3
  68. package/test-programmatic.ts +0 -30
  69. package/test-programmatic2.ts +0 -59
  70. package/test-schema-no-policies.ts +0 -12
  71. package/test_drizzle_mock.js +0 -3
  72. package/test_find_changed.mjs +0 -32
  73. package/test_hash.js +0 -14
  74. package/test_output.txt +0 -3145
@@ -0,0 +1,354 @@
1
+ /**
2
+ * @module AuthAdapter
3
+ *
4
+ * Pluggable authentication abstraction for Rebase.
5
+ *
6
+ * An `AuthAdapter` decouples authentication from the database layer,
7
+ * allowing users to bring their own auth system (Clerk, Auth0, Firebase Auth,
8
+ * custom JWT, etc.) while keeping the Rebase admin frontend fully functional.
9
+ *
10
+ * @example Built-in auth (default — zero config change)
11
+ * ```ts
12
+ * initializeRebaseBackend({
13
+ * auth: { jwtSecret: "...", google: { clientId: "..." } },
14
+ * database: createPostgresAdapter({ ... }),
15
+ * });
16
+ * ```
17
+ *
18
+ * @example Custom auth
19
+ * ```ts
20
+ * import { createCustomAuthAdapter } from "@rebasepro/server-core";
21
+ *
22
+ * initializeRebaseBackend({
23
+ * auth: createCustomAuthAdapter({
24
+ * verifyRequest: async (req) => { ... },
25
+ * }),
26
+ * database: createPostgresAdapter({ ... }),
27
+ * });
28
+ * ```
29
+ *
30
+ * @group Auth
31
+ */
32
+ import type { Hono } from "hono";
33
+ /**
34
+ * The normalized user object returned by `AuthAdapter.verifyRequest()`.
35
+ *
36
+ * Regardless of the auth provider, every request is resolved to this shape
37
+ * so that downstream middleware (RLS scoping, route guards) can work uniformly.
38
+ *
39
+ * @group Auth
40
+ */
41
+ export interface AuthenticatedUser {
42
+ /** Unique user identifier (provider-specific). */
43
+ uid: string;
44
+ /** Primary email address. */
45
+ email: string;
46
+ /** Human-readable display name. */
47
+ displayName?: string | null;
48
+ /** Avatar URL. */
49
+ photoUrl?: string | null;
50
+ /** Role identifiers the user holds. */
51
+ roles: string[];
52
+ /** Whether the user has admin privileges. */
53
+ isAdmin: boolean;
54
+ /** Raw bearer token from the request (for forwarding). */
55
+ rawToken?: string;
56
+ /** Extra claims/metadata from the auth provider. */
57
+ claims?: Record<string, unknown>;
58
+ }
59
+ /**
60
+ * Feature flags advertised by an auth adapter.
61
+ *
62
+ * The frontend reads these from `GET /api/auth/config` to dynamically
63
+ * show/hide UI elements (login form, registration, password reset, etc.).
64
+ *
65
+ * @group Auth
66
+ */
67
+ export interface AuthAdapterCapabilities {
68
+ /**
69
+ * Whether this adapter mounts its own `/auth/*` routes.
70
+ *
71
+ * - `true` for the built-in Rebase auth (login, register, refresh, etc.)
72
+ * - `false` for external providers like Clerk or Auth0 that handle
73
+ * auth flows outside of the Rebase backend.
74
+ */
75
+ hasBuiltInAuthRoutes: boolean;
76
+ /** Supports email/password login. */
77
+ emailPasswordLogin: boolean;
78
+ /** Supports new user registration. */
79
+ registration: boolean;
80
+ /** Supports password reset flow. */
81
+ passwordReset: boolean;
82
+ /** Supports session listing/revocation. */
83
+ sessionManagement: boolean;
84
+ /** Supports profile updates (display name, photo). */
85
+ profileUpdate: boolean;
86
+ /** Supports email verification. */
87
+ emailVerification: boolean;
88
+ /** List of enabled OAuth provider IDs (e.g. `["google", "github"]`). */
89
+ enabledProviders: string[];
90
+ /**
91
+ * For external auth (Clerk, Auth0, etc.): the URL where the user should
92
+ * be redirected for login. The Rebase frontend will navigate here instead
93
+ * of showing its own login form.
94
+ */
95
+ externalLoginUrl?: string;
96
+ /**
97
+ * True when no users exist yet — first-user bootstrap mode.
98
+ * Only applicable for built-in auth.
99
+ */
100
+ needsSetup?: boolean;
101
+ /** Whether new user registration is enabled (may differ from `registration` capability at runtime). */
102
+ registrationEnabled?: boolean;
103
+ }
104
+ /**
105
+ * Options for paginated user listing.
106
+ * @group Auth
107
+ */
108
+ export interface AuthUserListOptions {
109
+ limit?: number;
110
+ offset?: number;
111
+ search?: string;
112
+ orderBy?: string;
113
+ orderDir?: "asc" | "desc";
114
+ roleId?: string;
115
+ }
116
+ /**
117
+ * Paginated user listing result.
118
+ * @group Auth
119
+ */
120
+ export interface AuthUserListResult {
121
+ users: AuthUserData[];
122
+ total: number;
123
+ limit: number;
124
+ offset: number;
125
+ }
126
+ /**
127
+ * User data exposed by the auth adapter.
128
+ * @group Auth
129
+ */
130
+ export interface AuthUserData {
131
+ id: string;
132
+ email: string;
133
+ displayName?: string | null;
134
+ photoUrl?: string | null;
135
+ emailVerified?: boolean;
136
+ createdAt?: Date;
137
+ updatedAt?: Date;
138
+ }
139
+ /**
140
+ * Data for creating a user.
141
+ * @group Auth
142
+ */
143
+ export interface AuthCreateUserData {
144
+ email: string;
145
+ password?: string;
146
+ displayName?: string;
147
+ photoUrl?: string;
148
+ }
149
+ /**
150
+ * Role data exposed by the auth adapter.
151
+ * @group Auth
152
+ */
153
+ export interface AuthRoleData {
154
+ id: string;
155
+ name: string;
156
+ isAdmin: boolean;
157
+ defaultPermissions?: {
158
+ read?: boolean;
159
+ create?: boolean;
160
+ edit?: boolean;
161
+ delete?: boolean;
162
+ } | null;
163
+ collectionPermissions?: Record<string, {
164
+ read?: boolean;
165
+ create?: boolean;
166
+ edit?: boolean;
167
+ delete?: boolean;
168
+ }> | null;
169
+ config?: Record<string, unknown> | null;
170
+ }
171
+ /**
172
+ * Data for creating a role.
173
+ * @group Auth
174
+ */
175
+ export interface AuthCreateRoleData {
176
+ id: string;
177
+ name: string;
178
+ isAdmin?: boolean;
179
+ defaultPermissions?: AuthRoleData["defaultPermissions"];
180
+ collectionPermissions?: AuthRoleData["collectionPermissions"];
181
+ config?: AuthRoleData["config"];
182
+ }
183
+ /**
184
+ * User management operations for the admin panel.
185
+ *
186
+ * Optional — if not provided by the adapter, the user management UI is hidden.
187
+ *
188
+ * @group Auth
189
+ */
190
+ export interface UserManagementAdapter {
191
+ listUsers(options?: AuthUserListOptions): Promise<AuthUserListResult>;
192
+ getUserById(id: string): Promise<AuthUserData | null>;
193
+ createUser(data: AuthCreateUserData): Promise<AuthUserData>;
194
+ updateUser(id: string, data: Partial<AuthCreateUserData>): Promise<AuthUserData | null>;
195
+ deleteUser(id: string): Promise<void>;
196
+ getUserRoles(userId: string): Promise<AuthRoleData[]>;
197
+ setUserRoles(userId: string, roleIds: string[]): Promise<void>;
198
+ }
199
+ /**
200
+ * Role management operations for the admin panel.
201
+ *
202
+ * Optional — if not provided by the adapter, role management is disabled.
203
+ *
204
+ * @group Auth
205
+ */
206
+ export interface RoleManagementAdapter {
207
+ listRoles(): Promise<AuthRoleData[]>;
208
+ getRoleById(id: string): Promise<AuthRoleData | null>;
209
+ createRole(data: AuthCreateRoleData): Promise<AuthRoleData>;
210
+ updateRole(id: string, data: Partial<AuthRoleData>): Promise<AuthRoleData | null>;
211
+ deleteRole(id: string): Promise<void>;
212
+ }
213
+ /**
214
+ * Pluggable authentication adapter for Rebase.
215
+ *
216
+ * This is the **key interface** that decouples authentication from the
217
+ * database layer. Each auth adapter knows how to:
218
+ *
219
+ * 1. Verify incoming HTTP requests (`verifyRequest`)
220
+ * 2. Optionally manage users and roles (for the admin panel)
221
+ * 3. Optionally mount auth-specific routes (login, register, etc.)
222
+ * 4. Advertise its capabilities so the frontend can adapt
223
+ *
224
+ * The built-in Rebase auth implements this interface internally.
225
+ * External providers (Clerk, Auth0, Firebase Auth) provide their own adapters.
226
+ * Users with custom auth can use `createCustomAuthAdapter()` for a minimal setup.
227
+ *
228
+ * @group Auth
229
+ */
230
+ export interface AuthAdapter {
231
+ /**
232
+ * Unique identifier for this auth adapter.
233
+ *
234
+ * @example "rebase-builtin", "clerk", "auth0", "firebase", "custom"
235
+ */
236
+ readonly id: string;
237
+ /**
238
+ * Verify an incoming request and extract the authenticated user.
239
+ *
240
+ * This replaces the hardcoded JWT verification in server-core's middleware.
241
+ * Each adapter implements its own token verification strategy:
242
+ * - Built-in: verify Rebase JWT
243
+ * - Clerk: call Clerk's `verifyToken()`
244
+ * - Auth0: validate Auth0 JWT with JWKS
245
+ * - Custom: whatever logic the user provides
246
+ *
247
+ * @param request - The raw `Request` object (portable across Hono, Express, Fastify)
248
+ * @returns The authenticated user, or `null` for unauthenticated requests.
249
+ * Throw an error to reject the request with 401.
250
+ */
251
+ verifyRequest(request: Request): Promise<AuthenticatedUser | null>;
252
+ /**
253
+ * Verify a raw bearer token and extract the authenticated user.
254
+ *
255
+ * Used for **WebSocket authentication**, where there is no HTTP `Request`
256
+ * object — only a token string sent over the socket.
257
+ *
258
+ * If not implemented, the default behavior synthesizes a minimal `Request`
259
+ * with an `Authorization: Bearer <token>` header and delegates to
260
+ * `verifyRequest()`. Adapters should override this if their token
261
+ * verification logic doesn't depend on request headers/cookies.
262
+ *
263
+ * @param token - The raw bearer token string.
264
+ * @returns The authenticated user, or `null` if the token is invalid.
265
+ */
266
+ verifyToken?(token: string): Promise<AuthenticatedUser | null>;
267
+ /**
268
+ * User CRUD for the admin panel's user management UI.
269
+ * Optional — if not provided, user management UI is hidden.
270
+ */
271
+ userManagement?: UserManagementAdapter;
272
+ /**
273
+ * Role CRUD for the admin panel.
274
+ * Optional — if not provided, role management is disabled.
275
+ */
276
+ roleManagement?: RoleManagementAdapter;
277
+ /**
278
+ * Mount adapter-specific auth routes (login, register, refresh, etc.).
279
+ *
280
+ * - Built-in adapter: mounts `/auth/login`, `/auth/register`, etc.
281
+ * - External adapter: typically returns `undefined` (auth is handled externally).
282
+ * - Custom adapter: user mounts their own routes.
283
+ *
284
+ * The return type uses `Hono<any, any, any>` because this sub-app will be
285
+ * mounted into a parent app via `.route()`, which accepts any Hono env type.
286
+ * Adapter implementations are free to use their own env (e.g. `Hono<HonoEnv>`).
287
+ *
288
+ * @returns A Hono sub-app with auth routes, or `undefined` to skip route mounting.
289
+ */
290
+ createAuthRoutes?(): Hono<any, any, any> | undefined;
291
+ /**
292
+ * Mount admin routes for user/role management.
293
+ *
294
+ * Same typing rationale as `createAuthRoutes` — the sub-app env is
295
+ * unconstrained to support arbitrary adapter implementations.
296
+ *
297
+ * @returns A Hono sub-app with admin routes, or `undefined` to skip.
298
+ */
299
+ createAdminRoutes?(): Hono<any, any, any> | undefined;
300
+ /**
301
+ * Advertise what this auth adapter supports.
302
+ *
303
+ * The frontend reads this from `GET /api/auth/config` to dynamically
304
+ * show/hide UI elements. This is the bridge between backend capabilities
305
+ * and the frontend's `AuthCapabilities` type.
306
+ */
307
+ getCapabilities(): AuthAdapterCapabilities | Promise<AuthAdapterCapabilities>;
308
+ /**
309
+ * Called during backend initialization.
310
+ * Use for running migrations, creating tables, seeding initial data, etc.
311
+ */
312
+ initialize?(): Promise<void>;
313
+ /**
314
+ * Called during graceful shutdown.
315
+ * Use for closing connections, flushing caches, etc.
316
+ */
317
+ destroy?(): Promise<void>;
318
+ /**
319
+ * A static secret key for server-to-server / script authentication.
320
+ *
321
+ * When set, requests with `Authorization: Bearer <serviceKey>` bypass
322
+ * normal token verification and are granted admin-level access.
323
+ */
324
+ serviceKey?: string;
325
+ }
326
+ /**
327
+ * Options for creating a minimal custom auth adapter via `createCustomAuthAdapter()`.
328
+ *
329
+ * This is the simplest way to plug an existing auth system into Rebase.
330
+ * Only `verifyRequest` is required — everything else is optional.
331
+ *
332
+ * @group Auth
333
+ */
334
+ export interface CustomAuthAdapterOptions {
335
+ /**
336
+ * Verify an incoming request and return the authenticated user.
337
+ * This is the only required method.
338
+ */
339
+ verifyRequest: (request: Request) => Promise<AuthenticatedUser | null>;
340
+ /**
341
+ * Verify a raw bearer token for WebSocket authentication.
342
+ * Optional — if omitted, a synthetic `Request` is constructed and passed
343
+ * to `verifyRequest`.
344
+ */
345
+ verifyToken?: (token: string) => Promise<AuthenticatedUser | null>;
346
+ /** Optional user management for the admin panel. */
347
+ userManagement?: UserManagementAdapter;
348
+ /** Optional role management for the admin panel. */
349
+ roleManagement?: RoleManagementAdapter;
350
+ /** Static service key for server-to-server auth. */
351
+ serviceKey?: string;
352
+ /** Override default capabilities. */
353
+ capabilities?: Partial<AuthAdapterCapabilities>;
354
+ }
@@ -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
+ }
@@ -9,6 +9,7 @@ import type { RebaseContext } from "../rebase_context";
9
9
  import type { Relation } from "./relations";
10
10
  import type { EntityCustomView } from "./entity_views";
11
11
  import type { EntityAction } from "./entity_actions";
12
+ import type { ComponentRef } from "./component_ref";
12
13
  /**
13
14
  * Base interface containing all driver-agnostic collection properties.
14
15
  * Use {@link PostgresCollection} or {@link FirebaseCollection} for
@@ -19,9 +20,8 @@ import type { EntityAction } from "./entity_actions";
19
20
  */
20
21
  export interface BaseEntityCollection<M extends Record<string, unknown> = Record<string, unknown>, USER extends User = User> {
21
22
  /**
22
- * You can set an alias that will be used internally instead of the `path`.
23
- * The `alias` value will be used to determine the URL of the collection,
24
- * while `path` will still be used in the driver.
23
+ * You can set an alias that will be used internally instead of the collection name.
24
+ * The `slug` value will be used to determine the URL of the collection.
25
25
  * Note that you can use this value in reference properties too.
26
26
  */
27
27
  slug: string;
@@ -87,6 +87,9 @@ export interface BaseEntityCollection<M extends Record<string, unknown> = Record
87
87
  icon?: string | React.ReactNode;
88
88
  /**
89
89
  * Navigation group for this collection.
90
+ * Collections sharing the same group name will be visually grouped
91
+ * together in the drawer and home page. If not set, the collection
92
+ * falls into the default "Views" group.
90
93
  */
91
94
  group?: string;
92
95
  /**
@@ -158,17 +161,35 @@ export interface BaseEntityCollection<M extends Record<string, unknown> = Record
158
161
  /**
159
162
  * Force a filter in this view. If applied, the rest of the filters will
160
163
  * be disabled. Filters applied with this prop cannot be changed.
161
- * e.g. `forceFilter: { age: [">", 18] }`
162
- * e.g. `forceFilter: { related_user: ["==", new EntityReference("sdc43dsw2", "users")] }`
164
+ * e.g. `fixedFilter: { age: [">", 18] }`
165
+ * e.g. `fixedFilter: { related_user: ["==", new EntityReference("sdc43dsw2", "users")] }`
163
166
  */
164
- readonly forceFilter?: FilterValues<Extract<keyof M, string> | (string & {})>;
167
+ readonly fixedFilter?: FilterValues<Extract<keyof M, string> | (string & {})>;
165
168
  /**
166
169
  * Initial filters applied to the collection this collection is related to.
167
170
  * Defaults to none. Filters applied with this prop can be changed.
168
- * e.g. `filter: { age: [">", 18] }`
169
- * e.g. `filter: { related_user: ["==", new EntityReference("sdc43dsw2", "users")] }`
171
+ * e.g. `defaultFilter: { age: [">", 18] }`
172
+ * e.g. `defaultFilter: { related_user: ["==", new EntityReference("sdc43dsw2", "users")] }`
170
173
  */
171
- readonly filter?: FilterValues<Extract<keyof M, string> | (string & {})>;
174
+ readonly defaultFilter?: FilterValues<Extract<keyof M, string> | (string & {})>;
175
+ /**
176
+ * Pre-defined filter presets that appear as quick-access options in the
177
+ * collection toolbar. Each preset applies a set of filters (and
178
+ * optionally a sort order) with a single click.
179
+ *
180
+ * ```ts
181
+ * filterPresets: [
182
+ * {
183
+ * label: "Shipped this month",
184
+ * filterValues: {
185
+ * status: ["==", "shipped"],
186
+ * order_date: [">=", new Date(Date.now() - 30 * 86400000)]
187
+ * }
188
+ * }
189
+ * ]
190
+ * ```
191
+ */
192
+ readonly filterPresets?: FilterPreset<Extract<keyof M, string> | (string & {})>[];
172
193
  /**
173
194
  * Default sort applied to this collection.
174
195
  * When setting this prop, entities will have a default order
@@ -302,7 +323,7 @@ export interface BaseEntityCollection<M extends Record<string, unknown> = Record
302
323
  /**
303
324
  * Builder for the collection actions rendered in the toolbar
304
325
  */
305
- Actions?: React.ComponentType<any>[];
326
+ Actions?: ComponentRef<CollectionActionsProps>[];
306
327
  }
307
328
  /**
308
329
  * A collection backed by PostgreSQL (or any SQL database).
@@ -314,6 +335,7 @@ export interface BaseEntityCollection<M extends Record<string, unknown> = Record
314
335
  * @group Models
315
336
  */
316
337
  export interface PostgresCollection<M extends Record<string, unknown> = Record<string, unknown>, USER extends User = User> extends BaseEntityCollection<M, USER> {
338
+ properties: Properties;
317
339
  /**
318
340
  * The driver for this collection. For Postgres collections this
319
341
  * can be omitted (Postgres is the default) or set to `"postgres"`.
@@ -457,7 +479,8 @@ export interface CollectionActionsProps<M extends Record<string, unknown> = Reco
457
479
  /**
458
480
  * Array of the parent path segments like `['users']`
459
481
  */
460
- parentCollectionIds: string[];
482
+ parentCollectionSlugs: string[];
483
+ parentEntityIds: string[];
461
484
  /**
462
485
  * The collection configuration
463
486
  */
@@ -481,6 +504,21 @@ export interface CollectionActionsProps<M extends Record<string, unknown> = Reco
481
504
  * undefined means the count is still loading.
482
505
  */
483
506
  collectionEntitiesCount?: number;
507
+ /**
508
+ * Programmatically open the new-document form for this collection,
509
+ * optionally pre-populating it with initial field values.
510
+ * The form opens in the same mode configured for the collection
511
+ * (side panel, full screen, or split).
512
+ *
513
+ * This is the primary hook for workflows that need to create a document
514
+ * from external data — e.g. fetching content from a URL, importing from
515
+ * a third-party API, or duplicating from another system.
516
+ *
517
+ * @example
518
+ * // Inside a custom CollectionAction component:
519
+ * openNewDocument({ title: "Fetched title", body: "..." });
520
+ */
521
+ openNewDocument: (defaultValues?: Record<string, unknown>) => void;
484
522
  }
485
523
  /**
486
524
  * Use this controller to retrieve the selected entities or modify them in
@@ -508,6 +546,32 @@ export type WhereFilterOp = "<" | "<=" | "==" | "!=" | ">=" | ">" | "array-conta
508
546
  * @group Models
509
547
  */
510
548
  export type FilterValues<Key extends string> = Partial<Record<Key, [WhereFilterOp, unknown]>>;
549
+ /**
550
+ * A pre-defined filter preset for quick access in the collection toolbar.
551
+ * Users can select a preset to instantly apply a set of filters and
552
+ * optionally a sort order.
553
+ *
554
+ * @group Models
555
+ */
556
+ export interface FilterPreset<Key extends string = string> {
557
+ /**
558
+ * Display label shown in the preset menu.
559
+ * If omitted, a summary is auto-generated from the filter keys.
560
+ */
561
+ label?: string;
562
+ /**
563
+ * The filter values to apply when this preset is selected.
564
+ */
565
+ filterValues: FilterValues<Key>;
566
+ /**
567
+ * Optional sort override to apply alongside the filter values.
568
+ */
569
+ sort?: [Key, "asc" | "desc"];
570
+ }
571
+ /**
572
+ * @deprecated Use {@link FilterPreset} instead.
573
+ */
574
+ export type QuickFilter<Key extends string = string> = FilterPreset<Key>;
511
575
  /**
512
576
  * Used to indicate valid filter combinations (e.g. created in Firestore)
513
577
  * If the user selects a specific filter/sort combination, the CMS checks if it's