@rebasepro/server-postgresql 0.5.0 → 0.6.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 (165) hide show
  1. package/dist/{server-postgresql/src/PostgresAdapter.d.ts → PostgresAdapter.d.ts} +1 -1
  2. package/dist/{server-postgresql/src/PostgresBackendDriver.d.ts → PostgresBackendDriver.d.ts} +2 -2
  3. package/dist/{server-postgresql/src/PostgresBootstrapper.d.ts → PostgresBootstrapper.d.ts} +11 -1
  4. package/dist/{server-postgresql/src/collections → collections}/PostgresCollectionRegistry.d.ts +4 -0
  5. package/dist/index.es.js +10168 -11145
  6. package/dist/index.es.js.map +1 -1
  7. package/dist/index.umd.js +10735 -11429
  8. package/dist/index.umd.js.map +1 -1
  9. package/dist/{server-postgresql/src/services → services}/EntityPersistService.d.ts +0 -14
  10. package/dist/utils/pg-error-utils.d.ts +55 -0
  11. package/package.json +24 -21
  12. package/src/PostgresAdapter.ts +9 -10
  13. package/src/PostgresBackendDriver.ts +134 -121
  14. package/src/PostgresBootstrapper.ts +86 -13
  15. package/src/auth/ensure-tables.ts +28 -5
  16. package/src/auth/services.ts +28 -18
  17. package/src/cli.ts +99 -96
  18. package/src/collections/PostgresCollectionRegistry.ts +7 -0
  19. package/src/connection.ts +11 -6
  20. package/src/data-transformer.ts +16 -14
  21. package/src/databasePoolManager.ts +3 -2
  22. package/src/history/HistoryService.ts +3 -2
  23. package/src/history/ensure-history-table.ts +5 -4
  24. package/src/schema/auth-schema.ts +1 -2
  25. package/src/schema/doctor-cli.ts +2 -1
  26. package/src/schema/doctor.ts +40 -37
  27. package/src/schema/generate-drizzle-schema-logic.ts +56 -18
  28. package/src/schema/generate-drizzle-schema.ts +11 -11
  29. package/src/schema/introspect-db-inference.ts +25 -25
  30. package/src/schema/introspect-db-logic.ts +38 -38
  31. package/src/schema/introspect-db.ts +28 -27
  32. package/src/services/BranchService.ts +14 -0
  33. package/src/services/EntityFetchService.ts +28 -25
  34. package/src/services/EntityPersistService.ts +11 -141
  35. package/src/services/RelationService.ts +57 -37
  36. package/src/services/entity-helpers.ts +6 -2
  37. package/src/services/realtimeService.ts +45 -32
  38. package/src/utils/drizzle-conditions.ts +31 -15
  39. package/src/utils/pg-error-utils.ts +211 -0
  40. package/src/websocket.ts +15 -12
  41. package/test/auth-services.test.ts +36 -19
  42. package/test/batch-many-to-many-regression.test.ts +119 -39
  43. package/test/data-transformer-hardening.test.ts +67 -33
  44. package/test/data-transformer.test.ts +4 -2
  45. package/test/doctor.test.ts +10 -5
  46. package/test/drizzle-conditions.test.ts +59 -6
  47. package/test/generate-drizzle-schema.test.ts +65 -40
  48. package/test/introspect-db-generation.test.ts +179 -81
  49. package/test/introspect-db-utils.test.ts +92 -37
  50. package/test/mocks/chalk.cjs +7 -0
  51. package/test/pg-error-utils.test.ts +221 -0
  52. package/test/postgresDataDriver.test.ts +14 -5
  53. package/test/property-ordering.test.ts +126 -79
  54. package/test/realtimeService.test.ts +6 -2
  55. package/test/relation-pipeline-gaps.test.ts +84 -36
  56. package/test/relations.test.ts +247 -0
  57. package/test/unmapped-tables-safety.test.ts +14 -6
  58. package/test/websocket.test.ts +1 -1
  59. package/tsconfig.json +5 -0
  60. package/tsconfig.prod.json +3 -0
  61. package/vite.config.ts +5 -5
  62. package/dist/common/src/collections/CollectionRegistry.d.ts +0 -56
  63. package/dist/common/src/collections/default-collections.d.ts +0 -9
  64. package/dist/common/src/collections/index.d.ts +0 -2
  65. package/dist/common/src/data/buildRebaseData.d.ts +0 -14
  66. package/dist/common/src/data/query_builder.d.ts +0 -55
  67. package/dist/common/src/index.d.ts +0 -4
  68. package/dist/common/src/util/builders.d.ts +0 -57
  69. package/dist/common/src/util/callbacks.d.ts +0 -6
  70. package/dist/common/src/util/collections.d.ts +0 -11
  71. package/dist/common/src/util/common.d.ts +0 -2
  72. package/dist/common/src/util/conditions.d.ts +0 -26
  73. package/dist/common/src/util/entities.d.ts +0 -58
  74. package/dist/common/src/util/enums.d.ts +0 -3
  75. package/dist/common/src/util/index.d.ts +0 -16
  76. package/dist/common/src/util/navigation_from_path.d.ts +0 -34
  77. package/dist/common/src/util/navigation_utils.d.ts +0 -20
  78. package/dist/common/src/util/parent_references_from_path.d.ts +0 -6
  79. package/dist/common/src/util/paths.d.ts +0 -14
  80. package/dist/common/src/util/permissions.d.ts +0 -14
  81. package/dist/common/src/util/references.d.ts +0 -2
  82. package/dist/common/src/util/relations.d.ts +0 -22
  83. package/dist/common/src/util/resolutions.d.ts +0 -72
  84. package/dist/common/src/util/storage.d.ts +0 -24
  85. package/dist/types/src/controllers/analytics_controller.d.ts +0 -7
  86. package/dist/types/src/controllers/auth.d.ts +0 -104
  87. package/dist/types/src/controllers/client.d.ts +0 -168
  88. package/dist/types/src/controllers/collection_registry.d.ts +0 -46
  89. package/dist/types/src/controllers/customization_controller.d.ts +0 -60
  90. package/dist/types/src/controllers/data.d.ts +0 -207
  91. package/dist/types/src/controllers/data_driver.d.ts +0 -218
  92. package/dist/types/src/controllers/database_admin.d.ts +0 -11
  93. package/dist/types/src/controllers/dialogs_controller.d.ts +0 -36
  94. package/dist/types/src/controllers/effective_role.d.ts +0 -4
  95. package/dist/types/src/controllers/email.d.ts +0 -36
  96. package/dist/types/src/controllers/index.d.ts +0 -18
  97. package/dist/types/src/controllers/local_config_persistence.d.ts +0 -20
  98. package/dist/types/src/controllers/navigation.d.ts +0 -225
  99. package/dist/types/src/controllers/registry.d.ts +0 -63
  100. package/dist/types/src/controllers/side_dialogs_controller.d.ts +0 -67
  101. package/dist/types/src/controllers/side_entity_controller.d.ts +0 -97
  102. package/dist/types/src/controllers/snackbar.d.ts +0 -24
  103. package/dist/types/src/controllers/storage.d.ts +0 -171
  104. package/dist/types/src/index.d.ts +0 -4
  105. package/dist/types/src/rebase_context.d.ts +0 -122
  106. package/dist/types/src/types/auth_adapter.d.ts +0 -301
  107. package/dist/types/src/types/backend.d.ts +0 -571
  108. package/dist/types/src/types/backend_hooks.d.ts +0 -172
  109. package/dist/types/src/types/builders.d.ts +0 -15
  110. package/dist/types/src/types/chips.d.ts +0 -5
  111. package/dist/types/src/types/collections.d.ts +0 -961
  112. package/dist/types/src/types/component_ref.d.ts +0 -47
  113. package/dist/types/src/types/cron.d.ts +0 -102
  114. package/dist/types/src/types/data_source.d.ts +0 -64
  115. package/dist/types/src/types/database_adapter.d.ts +0 -94
  116. package/dist/types/src/types/entities.d.ts +0 -145
  117. package/dist/types/src/types/entity_actions.d.ts +0 -104
  118. package/dist/types/src/types/entity_callbacks.d.ts +0 -173
  119. package/dist/types/src/types/entity_link_builder.d.ts +0 -7
  120. package/dist/types/src/types/entity_overrides.d.ts +0 -10
  121. package/dist/types/src/types/entity_views.d.ts +0 -87
  122. package/dist/types/src/types/export_import.d.ts +0 -21
  123. package/dist/types/src/types/formex.d.ts +0 -40
  124. package/dist/types/src/types/index.d.ts +0 -28
  125. package/dist/types/src/types/locales.d.ts +0 -4
  126. package/dist/types/src/types/modify_collections.d.ts +0 -5
  127. package/dist/types/src/types/plugins.d.ts +0 -282
  128. package/dist/types/src/types/properties.d.ts +0 -1173
  129. package/dist/types/src/types/property_config.d.ts +0 -74
  130. package/dist/types/src/types/relations.d.ts +0 -336
  131. package/dist/types/src/types/slots.d.ts +0 -262
  132. package/dist/types/src/types/translations.d.ts +0 -900
  133. package/dist/types/src/types/user_management_delegate.d.ts +0 -86
  134. package/dist/types/src/types/websockets.d.ts +0 -78
  135. package/dist/types/src/users/index.d.ts +0 -1
  136. package/dist/types/src/users/user.d.ts +0 -50
  137. /package/dist/{server-postgresql/src/auth → auth}/ensure-tables.d.ts +0 -0
  138. /package/dist/{server-postgresql/src/auth → auth}/services.d.ts +0 -0
  139. /package/dist/{server-postgresql/src/cli.d.ts → cli.d.ts} +0 -0
  140. /package/dist/{server-postgresql/src/connection.d.ts → connection.d.ts} +0 -0
  141. /package/dist/{server-postgresql/src/data-transformer.d.ts → data-transformer.d.ts} +0 -0
  142. /package/dist/{server-postgresql/src/databasePoolManager.d.ts → databasePoolManager.d.ts} +0 -0
  143. /package/dist/{server-postgresql/src/history → history}/HistoryService.d.ts +0 -0
  144. /package/dist/{server-postgresql/src/history → history}/ensure-history-table.d.ts +0 -0
  145. /package/dist/{server-postgresql/src/index.d.ts → index.d.ts} +0 -0
  146. /package/dist/{server-postgresql/src/interfaces.d.ts → interfaces.d.ts} +0 -0
  147. /package/dist/{server-postgresql/src/schema → schema}/auth-schema.d.ts +0 -0
  148. /package/dist/{server-postgresql/src/schema → schema}/doctor-cli.d.ts +0 -0
  149. /package/dist/{server-postgresql/src/schema → schema}/doctor.d.ts +0 -0
  150. /package/dist/{server-postgresql/src/schema → schema}/generate-drizzle-schema-logic.d.ts +0 -0
  151. /package/dist/{server-postgresql/src/schema → schema}/generate-drizzle-schema.d.ts +0 -0
  152. /package/dist/{server-postgresql/src/schema → schema}/introspect-db-inference.d.ts +0 -0
  153. /package/dist/{server-postgresql/src/schema → schema}/introspect-db-logic.d.ts +0 -0
  154. /package/dist/{server-postgresql/src/schema → schema}/introspect-db.d.ts +0 -0
  155. /package/dist/{server-postgresql/src/schema → schema}/test-schema.d.ts +0 -0
  156. /package/dist/{server-postgresql/src/services → services}/BranchService.d.ts +0 -0
  157. /package/dist/{server-postgresql/src/services → services}/EntityFetchService.d.ts +0 -0
  158. /package/dist/{server-postgresql/src/services → services}/RelationService.d.ts +0 -0
  159. /package/dist/{server-postgresql/src/services → services}/entity-helpers.d.ts +0 -0
  160. /package/dist/{server-postgresql/src/services → services}/entityService.d.ts +0 -0
  161. /package/dist/{server-postgresql/src/services → services}/index.d.ts +0 -0
  162. /package/dist/{server-postgresql/src/services → services}/realtimeService.d.ts +0 -0
  163. /package/dist/{server-postgresql/src/types.d.ts → types.d.ts} +0 -0
  164. /package/dist/{server-postgresql/src/utils → utils}/drizzle-conditions.d.ts +0 -0
  165. /package/dist/{server-postgresql/src/websocket.d.ts → websocket.d.ts} +0 -0
@@ -1,571 +0,0 @@
1
- import type { Entity } from "./entities";
2
- import type { EntityCollection, FilterValues, WhereFilterOp } from "./collections";
3
- import type { AuthAdapter } from "./auth_adapter";
4
- /**
5
- * Abstract database connection interface.
6
- * Represents a connection to any database system.
7
- */
8
- export interface DatabaseConnection {
9
- /**
10
- * Type identifier for this database (e.g., 'postgres', 'mongodb', 'mysql')
11
- */
12
- readonly type: string;
13
- /**
14
- * Whether the connection is currently active
15
- */
16
- readonly isConnected?: boolean;
17
- /**
18
- * Close the database connection and release resources.
19
- */
20
- close?(): Promise<void>;
21
- }
22
- /**
23
- * A single filter condition for database queries
24
- */
25
- export interface QueryFilter {
26
- field: string;
27
- operator: WhereFilterOp;
28
- value: unknown;
29
- }
30
- /**
31
- * Options for fetching a collection of entities
32
- */
33
- export interface FetchCollectionOptions<M extends Record<string, unknown> = Record<string, unknown>> {
34
- filter?: FilterValues<Extract<keyof M, string>>;
35
- orderBy?: string;
36
- order?: "desc" | "asc";
37
- limit?: number;
38
- offset?: number;
39
- startAfter?: unknown;
40
- searchString?: string;
41
- databaseId?: string;
42
- collection?: EntityCollection;
43
- }
44
- /**
45
- * Options for searching entities
46
- */
47
- export interface SearchOptions<M extends Record<string, unknown> = Record<string, unknown>> {
48
- filter?: FilterValues<Extract<keyof M, string>>;
49
- orderBy?: string;
50
- order?: "desc" | "asc";
51
- limit?: number;
52
- databaseId?: string;
53
- collection?: EntityCollection;
54
- }
55
- /**
56
- * Options for counting entities
57
- */
58
- export interface CountOptions<M extends Record<string, unknown> = Record<string, unknown>> {
59
- filter?: FilterValues<Extract<keyof M, string>>;
60
- searchString?: string;
61
- databaseId?: string;
62
- }
63
- /**
64
- * Abstract condition builder interface.
65
- * Implementations translate Rebase filter conditions to database-specific queries.
66
- *
67
- * Note: This interface can be implemented as instance methods or as a class with static methods.
68
- * For static implementations (like DrizzleConditionBuilder), use the ConditionBuilderStatic type.
69
- *
70
- * @template T The type of condition returned by the builder (e.g., SQL for PostgreSQL, Filter<Document> for MongoDB)
71
- */
72
- export interface ConditionBuilder<T = unknown> {
73
- /**
74
- * Build filter conditions from Rebase FilterValues
75
- */
76
- buildFilterConditions<M extends Record<string, unknown>>(filter: FilterValues<Extract<keyof M, string>>, collectionPath: string, ...args: unknown[]): T[];
77
- /**
78
- * Build search conditions for text search
79
- */
80
- buildSearchConditions(searchString: string, properties: Record<string, unknown>, ...args: unknown[]): T[];
81
- /**
82
- * Combine multiple conditions with AND operator
83
- */
84
- combineConditionsWithAnd(conditions: T[]): T | undefined;
85
- /**
86
- * Combine multiple conditions with OR operator
87
- */
88
- combineConditionsWithOr(conditions: T[]): T | undefined;
89
- }
90
- /**
91
- * Static condition builder type for implementations using static methods.
92
- * Use this type when the class provides static methods rather than instance methods.
93
- *
94
- * @example
95
- * // DrizzleConditionBuilder satisfies this type
96
- * const builder: ConditionBuilderStatic<SQL> = DrizzleConditionBuilder;
97
- */
98
- export type ConditionBuilderStatic<T = unknown> = {
99
- buildFilterConditions<M extends Record<string, unknown>>(filter: FilterValues<Extract<keyof M, string>>, ...args: unknown[]): T[];
100
- buildSearchConditions(searchString: string, properties: Record<string, unknown>, ...args: unknown[]): T[];
101
- combineConditionsWithAnd(conditions: T[]): T | undefined;
102
- combineConditionsWithOr(conditions: T[]): T | undefined;
103
- };
104
- /**
105
- * Abstract entity repository interface.
106
- * Handles all CRUD operations for entities in the database.
107
- *
108
- * Implementations should handle:
109
- * - Entity serialization/deserialization
110
- * - Relation resolution
111
- * - ID generation and conversion
112
- */
113
- export interface EntityRepository {
114
- /**
115
- * Fetch a single entity by ID
116
- */
117
- fetchEntity<M extends Record<string, unknown>>(collectionPath: string, entityId: string | number, databaseId?: string): Promise<Entity<M> | undefined>;
118
- /**
119
- * Fetch a collection of entities with optional filtering, ordering, and pagination
120
- */
121
- fetchCollection<M extends Record<string, unknown>>(collectionPath: string, options?: FetchCollectionOptions<M>): Promise<Entity<M>[]>;
122
- /**
123
- * Search entities by text
124
- */
125
- searchEntities<M extends Record<string, unknown>>(collectionPath: string, searchString: string, options?: SearchOptions<M>): Promise<Entity<M>[]>;
126
- /**
127
- * Count entities in a collection
128
- */
129
- countEntities<M extends Record<string, unknown>>(collectionPath: string, options?: CountOptions<M>): Promise<number>;
130
- /**
131
- * Save an entity (create or update)
132
- */
133
- saveEntity<M extends Record<string, unknown>>(collectionPath: string, values: Partial<M>, entityId?: string | number, databaseId?: string): Promise<Entity<M>>;
134
- /**
135
- * Delete an entity by ID
136
- */
137
- deleteEntity(collectionPath: string, entityId: string | number, databaseId?: string): Promise<void>;
138
- /**
139
- * Check if a field value is unique in a collection
140
- */
141
- checkUniqueField(collectionPath: string, fieldName: string, value: unknown, excludeEntityId?: string, databaseId?: string): Promise<boolean>;
142
- }
143
- /**
144
- * Configuration for subscribing to a collection
145
- */
146
- export interface CollectionSubscriptionConfig {
147
- clientId: string;
148
- path: string;
149
- filter?: unknown;
150
- orderBy?: string;
151
- order?: "desc" | "asc";
152
- limit?: number;
153
- startAfter?: unknown;
154
- databaseId?: string;
155
- searchString?: string;
156
- }
157
- /**
158
- * Configuration for subscribing to a single entity
159
- */
160
- export interface EntitySubscriptionConfig {
161
- clientId: string;
162
- path: string;
163
- entityId: string | number;
164
- }
165
- /**
166
- * Abstract realtime provider interface.
167
- * Handles real-time subscriptions and notifications for entity changes.
168
- */
169
- export interface RealtimeProvider {
170
- /**
171
- * Subscribe to collection changes
172
- */
173
- subscribeToCollection(subscriptionId: string, config: CollectionSubscriptionConfig, callback?: (entities: Entity[]) => void): void;
174
- /**
175
- * Subscribe to single entity changes
176
- */
177
- subscribeToEntity(subscriptionId: string, config: EntitySubscriptionConfig, callback?: (entity: Entity | null) => void): void;
178
- /**
179
- * Unsubscribe from a subscription
180
- */
181
- unsubscribe(subscriptionId: string): void;
182
- /**
183
- * Notify all relevant subscribers of an entity update
184
- */
185
- notifyEntityUpdate(path: string, entityId: string, entity: Entity | null, databaseId?: string): Promise<void>;
186
- /**
187
- * Called when the HTTP server is ready and listening.
188
- * Useful for providers that need the server address for callbacks.
189
- */
190
- onServerReady?(serverInfo: {
191
- port: number;
192
- hostname?: string;
193
- }): void;
194
- /**
195
- * Gracefully shut down the realtime provider.
196
- * Called during server shutdown to clean up resources.
197
- */
198
- destroy?(): Promise<void>;
199
- /**
200
- * Stop the internal LISTEN client (e.g., PostgreSQL LISTEN/NOTIFY).
201
- * Called during graceful shutdown before closing database connections.
202
- */
203
- stopListening?(): Promise<void>;
204
- }
205
- /**
206
- * Abstract collection registry interface.
207
- * Manages registration and lookup of entity collections.
208
- */
209
- export interface CollectionRegistryInterface {
210
- /**
211
- * Register a collection
212
- */
213
- register(collection: EntityCollection): void;
214
- /**
215
- * Get a collection by its path
216
- */
217
- getCollectionByPath(path: string): EntityCollection | undefined;
218
- /**
219
- * Get all registered collections
220
- */
221
- getCollections(): EntityCollection[];
222
- }
223
- /**
224
- * Abstract data transformer interface.
225
- * Handles serialization/deserialization between frontend and database formats.
226
- */
227
- export interface DataTransformer {
228
- /**
229
- * Transform entity data for storage in the database
230
- */
231
- serializeToDatabase<M extends Record<string, unknown>>(entity: M, collection: EntityCollection): Record<string, unknown>;
232
- /**
233
- * Transform database data back to entity format
234
- */
235
- deserializeFromDatabase<M extends Record<string, unknown>>(data: Record<string, unknown>, collection: EntityCollection): Promise<M>;
236
- }
237
- /**
238
- * Administrative operations for SQL-based databases (PostgreSQL, MySQL, etc.).
239
- * Used by the SQL Editor, RLS Editor, and schema browser.
240
- *
241
- * @group Admin
242
- */
243
- export interface SQLAdmin {
244
- /**
245
- * Execute raw SQL against the database.
246
- */
247
- executeSql(sql: string, options?: {
248
- database?: string;
249
- role?: string;
250
- }): Promise<Record<string, unknown>[]>;
251
- /**
252
- * Fetch the available databases on the server.
253
- */
254
- fetchAvailableDatabases?(): Promise<string[]>;
255
- /**
256
- * Fetch the available database roles.
257
- */
258
- fetchAvailableRoles?(): Promise<string[]>;
259
- /**
260
- * Fetch the current database name.
261
- */
262
- fetchCurrentDatabase?(): Promise<string | undefined>;
263
- }
264
- /**
265
- * Administrative operations for document-based databases (MongoDB, Firestore, etc.).
266
- * Used by future document administration tools.
267
- *
268
- * @group Admin
269
- */
270
- export interface DocumentAdmin {
271
- /**
272
- * Execute an aggregation pipeline or equivalent query.
273
- */
274
- executeAggregate?(pipeline: Record<string, unknown>[]): Promise<Record<string, unknown>[]>;
275
- /**
276
- * Fetch statistics for a collection (document count, size, etc.).
277
- */
278
- fetchCollectionStats?(collectionName: string): Promise<{
279
- count: number;
280
- sizeBytes?: number;
281
- }>;
282
- }
283
- /**
284
- * Administrative operations for schema management.
285
- * Shared across SQL and document databases.
286
- *
287
- * @group Admin
288
- */
289
- export interface SchemaAdmin {
290
- /**
291
- * Fetch database tables/collections not yet mapped to a Rebase collection.
292
- */
293
- fetchUnmappedTables?(mappedPaths?: string[]): Promise<string[]>;
294
- /**
295
- * Fetch column/field metadata for a single table/collection.
296
- * The return type is generic — SQL backends return TableMetadata,
297
- * document backends may return a different shape.
298
- */
299
- fetchTableMetadata?(tableName: string): Promise<unknown>;
300
- }
301
- /**
302
- * Metadata for a database branch.
303
- * @group Admin
304
- */
305
- export interface BranchInfo {
306
- /** Branch name (without prefix). */
307
- name: string;
308
- /** The database this branch was created from. */
309
- parentDatabase: string;
310
- /** When the branch was created. */
311
- createdAt: Date;
312
- /** Size in bytes, if available from the server. */
313
- sizeBytes?: number;
314
- }
315
- /**
316
- * Administrative operations for database branching.
317
- * Allows creating isolated database copies for development/preview workflows.
318
- *
319
- * @group Admin
320
- */
321
- export interface BranchAdmin {
322
- /** Create a new branch (database copy) from the current or specified source database. */
323
- createBranch(name: string, options?: {
324
- source?: string;
325
- }): Promise<BranchInfo>;
326
- /** Delete a branch database. Cannot delete the main/default database. */
327
- deleteBranch(name: string): Promise<void>;
328
- /** List all branches (databases that were created via branching). */
329
- listBranches(): Promise<BranchInfo[]>;
330
- /** Get info about a specific branch. */
331
- getBranchInfo(name: string): Promise<BranchInfo | undefined>;
332
- }
333
- /**
334
- * Union type for all admin capabilities.
335
- * A backend may implement any combination of these interfaces.
336
- *
337
- * Use type guards (`isSQLAdmin`, `isDocumentAdmin`, `isSchemaAdmin`, `isBranchAdmin`)
338
- * to safely narrow the type before calling methods.
339
- *
340
- * @group Admin
341
- */
342
- export type DatabaseAdmin = Partial<SQLAdmin> & Partial<DocumentAdmin> & Partial<SchemaAdmin> & Partial<BranchAdmin>;
343
- /**
344
- * Type guard: does this admin support SQL operations?
345
- * @group Admin
346
- */
347
- export declare function isSQLAdmin(admin: DatabaseAdmin | undefined): admin is SQLAdmin;
348
- /**
349
- * Type guard: does this admin support document operations?
350
- * @group Admin
351
- */
352
- export declare function isDocumentAdmin(admin: DatabaseAdmin | undefined): admin is DocumentAdmin;
353
- /**
354
- * Type guard: does this admin support schema management?
355
- * @group Admin
356
- */
357
- export declare function isSchemaAdmin(admin: DatabaseAdmin | undefined): admin is SchemaAdmin;
358
- /**
359
- * Type guard: does this admin support database branching?
360
- * @group Admin
361
- */
362
- export declare function isBranchAdmin(admin: DatabaseAdmin | undefined): admin is BranchAdmin;
363
- /**
364
- * Health check result returned by `healthCheck()`.
365
- * @group Lifecycle
366
- */
367
- export interface HealthCheckResult {
368
- /** Whether the backend is healthy and able to serve requests. */
369
- healthy: boolean;
370
- /** Round-trip latency to the database in milliseconds. */
371
- latencyMs: number;
372
- /** Optional details (e.g., pool stats, replication lag). */
373
- details?: Record<string, unknown>;
374
- }
375
- /**
376
- * Lifecycle contract for backend components that hold resources
377
- * (database connections, WebSocket pools, timers, etc.).
378
- *
379
- * All methods are optional — simple backends (e.g., in-memory) can skip them.
380
- * @group Lifecycle
381
- */
382
- export interface BackendLifecycle {
383
- /**
384
- * Initialize the backend: open connections, run migrations, seed data.
385
- * Called once during startup. Idempotent.
386
- */
387
- initialize?(): Promise<void>;
388
- /**
389
- * Check whether the backend is healthy and reachable.
390
- * Should be fast (< 1 s) and safe to call frequently.
391
- */
392
- healthCheck?(): Promise<HealthCheckResult>;
393
- /**
394
- * Gracefully shut down: close connections, flush buffers, cancel timers.
395
- * After calling `destroy()`, no other methods should be called.
396
- */
397
- destroy?(): Promise<void>;
398
- }
399
- /**
400
- * Configuration for creating a database backend
401
- */
402
- export interface BackendConfig {
403
- /**
404
- * Type of database backend
405
- */
406
- type: string;
407
- /**
408
- * Database connection (implementation-specific)
409
- */
410
- connection: unknown;
411
- /**
412
- * Schema definition (implementation-specific, e.g., Drizzle schema for PostgreSQL)
413
- */
414
- schema?: unknown;
415
- }
416
- /**
417
- * A complete backend instance with all required services.
418
- *
419
- * Now includes optional lifecycle management and admin capabilities.
420
- */
421
- export interface BackendInstance extends BackendLifecycle {
422
- /**
423
- * Entity repository for CRUD operations
424
- */
425
- entityRepository: EntityRepository;
426
- /**
427
- * Realtime provider for subscriptions
428
- */
429
- realtimeProvider: RealtimeProvider;
430
- /**
431
- * Collection registry
432
- */
433
- collectionRegistry: CollectionRegistryInterface;
434
- /**
435
- * The underlying database connection
436
- */
437
- connection: DatabaseConnection;
438
- /**
439
- * Administrative operations (SQL, schema, documents).
440
- * What's available depends on the backend type — use type guards
441
- * (`isSQLAdmin`, `isSchemaAdmin`, etc.) to narrow.
442
- */
443
- admin?: DatabaseAdmin;
444
- }
445
- /**
446
- * Factory function type for creating backend instances
447
- */
448
- export type BackendFactory<TConfig extends BackendConfig = BackendConfig> = (config: TConfig) => BackendInstance;
449
- /**
450
- * A `BackendBootstrapper` encapsulates all driver-specific initialization logic.
451
- *
452
- * Instead of hard-coding Postgres setup into `initializeRebaseBackend()`,
453
- * each database backend provides its own bootstrapper that knows how to:
454
- * - Create the DataDriver from a config object
455
- * - Optionally initialize auth tables
456
- * - Optionally create a realtime service
457
- * - Mount driver-specific API routes
458
- *
459
- * The main `initializeRebaseBackend()` becomes a **coordinator** that iterates
460
- * registered bootstrappers, calls their hooks, and wires the results together.
461
- *
462
- * @group Backend
463
- *
464
- * @example
465
- * ```typescript
466
- * // Third-party MySQL bootstrapper
467
- * const mysqlBootstrapper: BackendBootstrapper = {
468
- * type: "mysql",
469
- * initializeDriver: async (config) => new MySQLDataDriver(config.connection),
470
- * initializeRealtime: async (config) => new MySQLChangeStreamRealtime(config.connection),
471
- * };
472
- *
473
- * initializeRebaseBackend({
474
- * ...config,
475
- * bootstrappers: [postgresBootstrapper, mysqlBootstrapper]
476
- * });
477
- * ```
478
- */
479
- export interface BackendBootstrapper {
480
- /**
481
- * Which driver type this bootstrapper handles.
482
- * Must match the `type` field on the driver config object
483
- * (e.g., `"postgres"`, `"mongodb"`, `"mysql"`).
484
- */
485
- type: string;
486
- /**
487
- * Unique identifier for this bootstrapper instance.
488
- * Used to register the driver in the driver registry.
489
- * Defaults to `type` if not set.
490
- */
491
- id?: string;
492
- /**
493
- * Whether this bootstrapper provides the default driver.
494
- * When true, the coordinator uses this driver as the primary one.
495
- */
496
- isDefault?: boolean;
497
- /**
498
- * Run database migrations for this driver.
499
- * Called by the coordinator after all drivers are initialized.
500
- */
501
- runMigrations?(config: unknown, driverResult: InitializedDriver): Promise<void>;
502
- /**
503
- * Create a DataDriver from the given config.
504
- * This is the only **required** method.
505
- */
506
- initializeDriver(config: unknown): Promise<InitializedDriver>;
507
- /**
508
- * Initialize auth tables / services if this driver supports them.
509
- * Return undefined if auth is not supported by this backend.
510
- */
511
- initializeAuth?(config: unknown, driverResult: InitializedDriver): Promise<BootstrappedAuth | undefined>;
512
- /**
513
- * Initialize history tables / services if this driver supports them.
514
- * Return undefined if history is not supported by this backend.
515
- */
516
- initializeHistory?(config: unknown, driverResult: InitializedDriver): Promise<{
517
- historyService: unknown;
518
- } | undefined>;
519
- /**
520
- * Create a realtime provider for this driver.
521
- * Return undefined if the driver does not support realtime.
522
- */
523
- initializeRealtime?(config: unknown, driverResult: InitializedDriver): Promise<RealtimeProvider | undefined>;
524
- /**
525
- * Mount any driver-specific HTTP routes (e.g., custom admin endpoints).
526
- * Called after all drivers are initialized.
527
- */
528
- mountRoutes?(app: unknown, basePath: string, driverResult: InitializedDriver): void;
529
- /**
530
- * Return admin capabilities for this driver.
531
- */
532
- getAdmin?(driverResult: InitializedDriver): DatabaseAdmin | undefined;
533
- /**
534
- * Initialize WebSocket server for realtime operations.
535
- */
536
- initializeWebsockets?(server: unknown, realtimeService: RealtimeProvider, driver: import("../controllers/data_driver").DataDriver, config?: unknown, authAdapter?: AuthAdapter): Promise<void> | void;
537
- }
538
- /**
539
- * Result of `BackendBootstrapper.initializeDriver()`.
540
- * @group Backend
541
- */
542
- export interface InitializedDriver {
543
- /** The DataDriver instance, ready for use. */
544
- driver: import("../controllers/data_driver").DataDriver;
545
- /** The realtime service, if the driver created one during init. */
546
- realtimeProvider?: RealtimeProvider;
547
- /** A collection registry to register schema / tables into. */
548
- collectionRegistry?: CollectionRegistryInterface;
549
- /** The underlying database connection (for lifecycle management). */
550
- connection?: DatabaseConnection;
551
- /**
552
- * Opaque handle that the bootstrapper can use in subsequent hooks
553
- * (e.g., `initializeAuth`, `mountRoutes`) to access driver internals.
554
- * Not used by the coordinator.
555
- */
556
- internals?: unknown;
557
- }
558
- /**
559
- * Result of `BackendBootstrapper.initializeAuth()`.
560
- * @group Backend
561
- */
562
- export interface BootstrappedAuth {
563
- /** User management service. */
564
- userService: unknown;
565
- /** Role management service (optional, roles are now simple strings). */
566
- roleService?: unknown;
567
- /** Email service (optional). */
568
- emailService?: unknown;
569
- /** Combined Auth Repository for unified token and user management. */
570
- authRepository?: unknown;
571
- }