@rebasepro/server-postgresql 0.0.1-canary.09e5ec5

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 (196) hide show
  1. package/LICENSE +6 -0
  2. package/README.md +106 -0
  3. package/build-errors.txt +37 -0
  4. package/dist/common/src/collections/CollectionRegistry.d.ts +56 -0
  5. package/dist/common/src/collections/index.d.ts +1 -0
  6. package/dist/common/src/data/buildRebaseData.d.ts +14 -0
  7. package/dist/common/src/index.d.ts +3 -0
  8. package/dist/common/src/util/builders.d.ts +57 -0
  9. package/dist/common/src/util/callbacks.d.ts +6 -0
  10. package/dist/common/src/util/collections.d.ts +11 -0
  11. package/dist/common/src/util/common.d.ts +2 -0
  12. package/dist/common/src/util/conditions.d.ts +26 -0
  13. package/dist/common/src/util/entities.d.ts +58 -0
  14. package/dist/common/src/util/enums.d.ts +3 -0
  15. package/dist/common/src/util/index.d.ts +16 -0
  16. package/dist/common/src/util/navigation_from_path.d.ts +34 -0
  17. package/dist/common/src/util/navigation_utils.d.ts +20 -0
  18. package/dist/common/src/util/parent_references_from_path.d.ts +6 -0
  19. package/dist/common/src/util/paths.d.ts +14 -0
  20. package/dist/common/src/util/permissions.d.ts +5 -0
  21. package/dist/common/src/util/references.d.ts +2 -0
  22. package/dist/common/src/util/relations.d.ts +22 -0
  23. package/dist/common/src/util/resolutions.d.ts +72 -0
  24. package/dist/common/src/util/storage.d.ts +24 -0
  25. package/dist/index.es.js +11298 -0
  26. package/dist/index.es.js.map +1 -0
  27. package/dist/index.umd.js +11306 -0
  28. package/dist/index.umd.js.map +1 -0
  29. package/dist/server-postgresql/src/PostgresBackendDriver.d.ts +100 -0
  30. package/dist/server-postgresql/src/PostgresBootstrapper.d.ts +40 -0
  31. package/dist/server-postgresql/src/auth/ensure-tables.d.ts +6 -0
  32. package/dist/server-postgresql/src/auth/services.d.ts +192 -0
  33. package/dist/server-postgresql/src/cli.d.ts +1 -0
  34. package/dist/server-postgresql/src/collections/PostgresCollectionRegistry.d.ts +43 -0
  35. package/dist/server-postgresql/src/connection.d.ts +40 -0
  36. package/dist/server-postgresql/src/data-transformer.d.ts +58 -0
  37. package/dist/server-postgresql/src/databasePoolManager.d.ts +20 -0
  38. package/dist/server-postgresql/src/history/HistoryService.d.ts +71 -0
  39. package/dist/server-postgresql/src/history/ensure-history-table.d.ts +7 -0
  40. package/dist/server-postgresql/src/index.d.ts +13 -0
  41. package/dist/server-postgresql/src/interfaces.d.ts +18 -0
  42. package/dist/server-postgresql/src/schema/auth-schema.d.ts +868 -0
  43. package/dist/server-postgresql/src/schema/doctor-cli.d.ts +2 -0
  44. package/dist/server-postgresql/src/schema/doctor.d.ts +43 -0
  45. package/dist/server-postgresql/src/schema/generate-drizzle-schema-logic.d.ts +2 -0
  46. package/dist/server-postgresql/src/schema/generate-drizzle-schema.d.ts +1 -0
  47. package/dist/server-postgresql/src/schema/introspect-db-logic.d.ts +82 -0
  48. package/dist/server-postgresql/src/schema/introspect-db.d.ts +1 -0
  49. package/dist/server-postgresql/src/schema/test-schema.d.ts +24 -0
  50. package/dist/server-postgresql/src/services/BranchService.d.ts +47 -0
  51. package/dist/server-postgresql/src/services/EntityFetchService.d.ts +209 -0
  52. package/dist/server-postgresql/src/services/EntityPersistService.d.ts +41 -0
  53. package/dist/server-postgresql/src/services/RelationService.d.ts +98 -0
  54. package/dist/server-postgresql/src/services/entity-helpers.d.ts +38 -0
  55. package/dist/server-postgresql/src/services/entityService.d.ts +104 -0
  56. package/dist/server-postgresql/src/services/index.d.ts +4 -0
  57. package/dist/server-postgresql/src/services/realtimeService.d.ts +188 -0
  58. package/dist/server-postgresql/src/utils/drizzle-conditions.d.ts +116 -0
  59. package/dist/server-postgresql/src/websocket.d.ts +5 -0
  60. package/dist/types/src/controllers/analytics_controller.d.ts +7 -0
  61. package/dist/types/src/controllers/auth.d.ts +119 -0
  62. package/dist/types/src/controllers/client.d.ts +170 -0
  63. package/dist/types/src/controllers/collection_registry.d.ts +45 -0
  64. package/dist/types/src/controllers/customization_controller.d.ts +60 -0
  65. package/dist/types/src/controllers/data.d.ts +168 -0
  66. package/dist/types/src/controllers/data_driver.d.ts +160 -0
  67. package/dist/types/src/controllers/database_admin.d.ts +11 -0
  68. package/dist/types/src/controllers/dialogs_controller.d.ts +36 -0
  69. package/dist/types/src/controllers/effective_role.d.ts +4 -0
  70. package/dist/types/src/controllers/email.d.ts +34 -0
  71. package/dist/types/src/controllers/index.d.ts +18 -0
  72. package/dist/types/src/controllers/local_config_persistence.d.ts +20 -0
  73. package/dist/types/src/controllers/navigation.d.ts +213 -0
  74. package/dist/types/src/controllers/registry.d.ts +54 -0
  75. package/dist/types/src/controllers/side_dialogs_controller.d.ts +67 -0
  76. package/dist/types/src/controllers/side_entity_controller.d.ts +90 -0
  77. package/dist/types/src/controllers/snackbar.d.ts +24 -0
  78. package/dist/types/src/controllers/storage.d.ts +171 -0
  79. package/dist/types/src/index.d.ts +4 -0
  80. package/dist/types/src/rebase_context.d.ts +105 -0
  81. package/dist/types/src/types/backend.d.ts +536 -0
  82. package/dist/types/src/types/builders.d.ts +15 -0
  83. package/dist/types/src/types/chips.d.ts +5 -0
  84. package/dist/types/src/types/collections.d.ts +856 -0
  85. package/dist/types/src/types/cron.d.ts +102 -0
  86. package/dist/types/src/types/data_source.d.ts +64 -0
  87. package/dist/types/src/types/entities.d.ts +145 -0
  88. package/dist/types/src/types/entity_actions.d.ts +98 -0
  89. package/dist/types/src/types/entity_callbacks.d.ts +173 -0
  90. package/dist/types/src/types/entity_link_builder.d.ts +7 -0
  91. package/dist/types/src/types/entity_overrides.d.ts +10 -0
  92. package/dist/types/src/types/entity_views.d.ts +61 -0
  93. package/dist/types/src/types/export_import.d.ts +21 -0
  94. package/dist/types/src/types/index.d.ts +23 -0
  95. package/dist/types/src/types/locales.d.ts +4 -0
  96. package/dist/types/src/types/modify_collections.d.ts +5 -0
  97. package/dist/types/src/types/plugins.d.ts +279 -0
  98. package/dist/types/src/types/properties.d.ts +1176 -0
  99. package/dist/types/src/types/property_config.d.ts +70 -0
  100. package/dist/types/src/types/relations.d.ts +336 -0
  101. package/dist/types/src/types/slots.d.ts +252 -0
  102. package/dist/types/src/types/translations.d.ts +870 -0
  103. package/dist/types/src/types/user_management_delegate.d.ts +121 -0
  104. package/dist/types/src/types/websockets.d.ts +78 -0
  105. package/dist/types/src/users/index.d.ts +2 -0
  106. package/dist/types/src/users/roles.d.ts +22 -0
  107. package/dist/types/src/users/user.d.ts +46 -0
  108. package/drizzle-test/0000_woozy_junta.sql +6 -0
  109. package/drizzle-test/0001_youthful_arachne.sql +1 -0
  110. package/drizzle-test/0002_lively_dragon_lord.sql +2 -0
  111. package/drizzle-test/0003_mean_king_cobra.sql +2 -0
  112. package/drizzle-test/meta/0000_snapshot.json +47 -0
  113. package/drizzle-test/meta/0001_snapshot.json +48 -0
  114. package/drizzle-test/meta/0002_snapshot.json +38 -0
  115. package/drizzle-test/meta/0003_snapshot.json +48 -0
  116. package/drizzle-test/meta/_journal.json +34 -0
  117. package/drizzle-test-out/0000_tan_trauma.sql +6 -0
  118. package/drizzle-test-out/0001_rapid_drax.sql +1 -0
  119. package/drizzle-test-out/meta/0000_snapshot.json +44 -0
  120. package/drizzle-test-out/meta/0001_snapshot.json +54 -0
  121. package/drizzle-test-out/meta/_journal.json +20 -0
  122. package/drizzle.test.config.ts +10 -0
  123. package/jest-all.log +3128 -0
  124. package/jest.log +49 -0
  125. package/package.json +92 -0
  126. package/scratch.ts +41 -0
  127. package/src/PostgresBackendDriver.ts +1008 -0
  128. package/src/PostgresBootstrapper.ts +231 -0
  129. package/src/auth/ensure-tables.ts +381 -0
  130. package/src/auth/services.ts +799 -0
  131. package/src/cli.ts +648 -0
  132. package/src/collections/PostgresCollectionRegistry.ts +96 -0
  133. package/src/connection.ts +84 -0
  134. package/src/data-transformer.ts +608 -0
  135. package/src/databasePoolManager.ts +85 -0
  136. package/src/history/HistoryService.ts +248 -0
  137. package/src/history/ensure-history-table.ts +45 -0
  138. package/src/index.ts +13 -0
  139. package/src/interfaces.ts +60 -0
  140. package/src/schema/auth-schema.ts +169 -0
  141. package/src/schema/doctor-cli.ts +47 -0
  142. package/src/schema/doctor.ts +595 -0
  143. package/src/schema/generate-drizzle-schema-logic.ts +765 -0
  144. package/src/schema/generate-drizzle-schema.ts +151 -0
  145. package/src/schema/introspect-db-logic.ts +542 -0
  146. package/src/schema/introspect-db.ts +211 -0
  147. package/src/schema/test-schema.ts +11 -0
  148. package/src/services/BranchService.ts +237 -0
  149. package/src/services/EntityFetchService.ts +1576 -0
  150. package/src/services/EntityPersistService.ts +349 -0
  151. package/src/services/RelationService.ts +1274 -0
  152. package/src/services/entity-helpers.ts +147 -0
  153. package/src/services/entityService.ts +211 -0
  154. package/src/services/index.ts +13 -0
  155. package/src/services/realtimeService.ts +1034 -0
  156. package/src/utils/drizzle-conditions.ts +1000 -0
  157. package/src/websocket.ts +518 -0
  158. package/test/auth-services.test.ts +661 -0
  159. package/test/batch-many-to-many-regression.test.ts +573 -0
  160. package/test/branchService.test.ts +367 -0
  161. package/test/data-transformer-hardening.test.ts +417 -0
  162. package/test/data-transformer.test.ts +175 -0
  163. package/test/doctor.test.ts +182 -0
  164. package/test/drizzle-conditions.test.ts +895 -0
  165. package/test/entityService.errors.test.ts +367 -0
  166. package/test/entityService.relations.test.ts +1008 -0
  167. package/test/entityService.subcollection-search.test.ts +566 -0
  168. package/test/entityService.test.ts +1035 -0
  169. package/test/generate-drizzle-schema.test.ts +988 -0
  170. package/test/historyService.test.ts +141 -0
  171. package/test/introspect-db-generation.test.ts +436 -0
  172. package/test/introspect-db-utils.test.ts +389 -0
  173. package/test/n-plus-one-regression.test.ts +314 -0
  174. package/test/postgresDataDriver.test.ts +648 -0
  175. package/test/realtimeService.test.ts +307 -0
  176. package/test/relation-pipeline-gaps.test.ts +637 -0
  177. package/test/relations.test.ts +1115 -0
  178. package/test/unmapped-tables-safety.test.ts +345 -0
  179. package/test-drizzle-bug.ts +18 -0
  180. package/test-drizzle-out/0000_cultured_freak.sql +7 -0
  181. package/test-drizzle-out/0001_tiresome_professor_monster.sql +1 -0
  182. package/test-drizzle-out/meta/0000_snapshot.json +55 -0
  183. package/test-drizzle-out/meta/0001_snapshot.json +63 -0
  184. package/test-drizzle-out/meta/_journal.json +20 -0
  185. package/test-drizzle-prompt.sh +2 -0
  186. package/test-policy-prompt.sh +3 -0
  187. package/test-programmatic.ts +30 -0
  188. package/test-programmatic2.ts +59 -0
  189. package/test-schema-no-policies.ts +12 -0
  190. package/test_drizzle_mock.js +3 -0
  191. package/test_find_changed.mjs +32 -0
  192. package/test_hash.js +14 -0
  193. package/test_output.txt +3145 -0
  194. package/tsconfig.json +49 -0
  195. package/tsconfig.prod.json +20 -0
  196. package/vite.config.ts +82 -0
@@ -0,0 +1,104 @@
1
+ import { Entity, FilterValues } from "@rebasepro/types";
2
+ import { EntityFetchService } from "./EntityFetchService";
3
+ import { EntityPersistService } from "./EntityPersistService";
4
+ import { RelationService } from "./RelationService";
5
+ import { EntityRepository, DrizzleClient } from "../interfaces";
6
+ import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry";
7
+ export { sanitizeAndConvertDates, serializeDataToServer, parseDataFromServer } from "../data-transformer";
8
+ export { EntityFetchService } from "./EntityFetchService";
9
+ export { EntityPersistService } from "./EntityPersistService";
10
+ export { RelationService } from "./RelationService";
11
+ export * from "../interfaces";
12
+ /**
13
+ * EntityService - Facade for entity operations.
14
+ *
15
+ * This class provides a unified API for entity CRUD operations by delegating
16
+ * to specialized services:
17
+ * - EntityFetchService: Read operations (fetch, search, count)
18
+ * - EntityPersistService: Write operations (save, delete)
19
+ * - RelationService: Relation operations (fetch related, update relations)
20
+ *
21
+ * Implements the EntityRepository interface for database abstraction.
22
+ */
23
+ export declare class EntityService implements EntityRepository {
24
+ private db;
25
+ private registry;
26
+ private fetchService;
27
+ private persistService;
28
+ constructor(db: DrizzleClient, registry: PostgresCollectionRegistry);
29
+ /**
30
+ * Fetch a single entity by ID
31
+ */
32
+ fetchEntity<M extends Record<string, unknown>>(collectionPath: string, entityId: string | number, databaseId?: string): Promise<Entity<M> | undefined>;
33
+ /**
34
+ * Fetch a collection of entities with optional filtering, ordering, and pagination
35
+ */
36
+ fetchCollection<M extends Record<string, unknown>>(collectionPath: string, options?: {
37
+ filter?: FilterValues<Extract<keyof M, string>>;
38
+ orderBy?: string;
39
+ order?: "desc" | "asc";
40
+ limit?: number;
41
+ offset?: number;
42
+ startAfter?: Record<string, unknown>;
43
+ searchString?: string;
44
+ databaseId?: string;
45
+ }): Promise<Entity<M>[]>;
46
+ /**
47
+ * Search entities by text
48
+ */
49
+ searchEntities<M extends Record<string, unknown>>(collectionPath: string, searchString: string, options?: {
50
+ filter?: FilterValues<Extract<keyof M, string>>;
51
+ orderBy?: string;
52
+ order?: "desc" | "asc";
53
+ limit?: number;
54
+ databaseId?: string;
55
+ }): Promise<Entity<M>[]>;
56
+ /**
57
+ * Count entities in a collection
58
+ */
59
+ countEntities<M extends Record<string, unknown>>(collectionPath: string, options?: {
60
+ filter?: FilterValues<Extract<keyof M, string>>;
61
+ searchString?: string;
62
+ databaseId?: string;
63
+ }): Promise<number>;
64
+ /**
65
+ * Check if a field value is unique in a collection
66
+ */
67
+ checkUniqueField(collectionPath: string, fieldName: string, value: unknown, excludeEntityId?: string, databaseId?: string): Promise<boolean>;
68
+ /**
69
+ * Fetch entities related to a parent entity
70
+ */
71
+ fetchRelatedEntities<M extends Record<string, unknown>>(parentCollectionPath: string, parentEntityId: string | number, relationKey: string, options?: {
72
+ filter?: FilterValues<Extract<keyof M, string>>;
73
+ orderBy?: string;
74
+ order?: "desc" | "asc";
75
+ limit?: number;
76
+ startAfter?: Record<string, unknown>;
77
+ searchString?: string;
78
+ databaseId?: string;
79
+ }): Promise<Entity<M>[]>;
80
+ /**
81
+ * Save an entity (create or update)
82
+ */
83
+ saveEntity<M extends Record<string, unknown>>(collectionPath: string, values: Partial<M>, entityId?: string | number, databaseId?: string): Promise<Entity<M>>;
84
+ /**
85
+ * Delete an entity by ID
86
+ */
87
+ deleteEntity(collectionPath: string, entityId: string | number, databaseId?: string): Promise<void>;
88
+ /**
89
+ * Execute raw SQL
90
+ */
91
+ executeSql(sqlText: string): Promise<Record<string, unknown>[]>;
92
+ /**
93
+ * Get the underlying EntityFetchService for advanced use
94
+ */
95
+ getFetchService(): EntityFetchService;
96
+ /**
97
+ * Get the underlying EntityPersistService for advanced use
98
+ */
99
+ getPersistService(): EntityPersistService;
100
+ /**
101
+ * Get the underlying RelationService for advanced use
102
+ */
103
+ getRelationService(): RelationService;
104
+ }
@@ -0,0 +1,4 @@
1
+ export { EntityFetchService } from "./EntityFetchService";
2
+ export { EntityPersistService } from "./EntityPersistService";
3
+ export { RelationService } from "./RelationService";
4
+ export { getCollectionByPath, getTableForCollection, getPrimaryKeys, parseIdValues, buildCompositeId } from "./entity-helpers";
@@ -0,0 +1,188 @@
1
+ import { WebSocket } from "ws";
2
+ import { EventEmitter } from "events";
3
+ import { Entity, DataDriver, WebSocketMessage } from "@rebasepro/types";
4
+ import { NodePgDatabase } from "drizzle-orm/node-postgres";
5
+ import { RealtimeProvider, CollectionSubscriptionConfig, EntitySubscriptionConfig } from "../interfaces";
6
+ import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry";
7
+ /**
8
+ * Auth context stored per-subscription so real-time refetches respect RLS.
9
+ * Mirrors the session variables set by PostgresBackendDriver.withAuth().
10
+ */
11
+ export interface SubscriptionAuthContext {
12
+ userId: string;
13
+ roles: string[];
14
+ }
15
+ /**
16
+ * PostgreSQL-specific realtime service.
17
+ * Handles WebSocket connections and subscriptions for real-time entity updates.
18
+ *
19
+ * Implements the RealtimeProvider interface for database abstraction.
20
+ */
21
+ export declare class RealtimeService extends EventEmitter implements RealtimeProvider {
22
+ private db;
23
+ private registry;
24
+ private clients;
25
+ private entityService;
26
+ private _subscriptions;
27
+ private subscriptionCallbacks;
28
+ private driver?;
29
+ /** Unique identifier for this process instance, used to skip own notifications. */
30
+ private readonly instanceId;
31
+ /** Dedicated pg.Client for LISTEN (outside the Drizzle pool). */
32
+ private listenClient?;
33
+ /** Connection string used for reconnecting the LISTEN client. */
34
+ private listenConnectionString?;
35
+ /** Whether cross-instance broadcasting is active. */
36
+ private broadcasting;
37
+ /** Reconnection timer handle. */
38
+ private reconnectTimer?;
39
+ /** Debounce timers for collection refetches to prevent refetch storms. */
40
+ private refetchTimers;
41
+ /** Debounce window (ms) for coalescing rapid entity updates into a single correctness refetch. */
42
+ private static readonly REFETCH_DEBOUNCE_MS;
43
+ constructor(db: NodePgDatabase<any>, registry: PostgresCollectionRegistry);
44
+ /** Whether to emit verbose debug logs (disabled in production). */
45
+ private static readonly DEBUG;
46
+ private debugLog;
47
+ setDataDriver(driver: DataDriver): void;
48
+ get subscriptions(): Map<string, {
49
+ clientId: string;
50
+ type: "collection" | "entity";
51
+ path: string;
52
+ entityId?: string | number;
53
+ collectionRequest?: {
54
+ filter?: Record<string, unknown>;
55
+ orderBy?: string;
56
+ order?: "desc" | "asc";
57
+ limit?: number;
58
+ offset?: number;
59
+ startAfter?: Record<string, unknown>;
60
+ databaseId?: string;
61
+ searchString?: string;
62
+ };
63
+ authContext?: SubscriptionAuthContext;
64
+ }>;
65
+ registerDataDriverSubscription(subscriptionId: string, subscription: {
66
+ clientId: string;
67
+ type: "collection" | "entity";
68
+ path: string;
69
+ entityId?: string | number;
70
+ collectionRequest?: {
71
+ filter?: Record<string, unknown>;
72
+ orderBy?: string;
73
+ order?: "desc" | "asc";
74
+ limit?: number;
75
+ offset?: number;
76
+ startAfter?: Record<string, unknown>;
77
+ databaseId?: string;
78
+ searchString?: string;
79
+ };
80
+ authContext?: SubscriptionAuthContext;
81
+ }): void;
82
+ addSubscriptionCallback(subscriptionId: string, callback: (data: Entity[] | Entity | null) => void): void;
83
+ removeSubscriptionCallback(subscriptionId: string): void;
84
+ /**
85
+ * Subscribe to collection changes (RealtimeProvider interface)
86
+ */
87
+ subscribeToCollection(subscriptionId: string, config: CollectionSubscriptionConfig, callback?: (entities: Entity[]) => void): void;
88
+ /**
89
+ * Subscribe to single entity changes (RealtimeProvider interface)
90
+ */
91
+ subscribeToEntity(subscriptionId: string, config: EntitySubscriptionConfig, callback?: (entity: Entity | null) => void): void;
92
+ /**
93
+ * Unsubscribe from a subscription (RealtimeProvider interface)
94
+ */
95
+ unsubscribe(subscriptionId: string): void;
96
+ addClient(clientId: string, ws: WebSocket): void;
97
+ handleClientMessage(clientId: string, message: WebSocketMessage, authContext?: SubscriptionAuthContext): Promise<void>;
98
+ removeClient(clientId: string): Promise<void>;
99
+ private handleMessage;
100
+ private handleCollectionSubscription;
101
+ private handleEntitySubscription;
102
+ private handleUnsubscribe;
103
+ /**
104
+ * Enhanced notification method that handles nested relation updates.
105
+ * @param broadcast When true (default), also sends a pg_notify so other instances
106
+ * pick up the change. Set to false when handling an incoming
107
+ * cross-instance notification to avoid infinite loops.
108
+ */
109
+ notifyEntityUpdate(path: string, entityId: string, entity: Entity | null, databaseId?: string, broadcast?: boolean): Promise<void>;
110
+ /**
111
+ * Notify subscriptions for a specific path
112
+ */
113
+ private notifyPathUpdate;
114
+ /**
115
+ * Debounce a collection refetch for a WebSocket subscription.
116
+ * Coalesces rapid entity mutations into a single database query.
117
+ */
118
+ private debouncedCollectionRefetch;
119
+ /**
120
+ * Debounce a collection refetch for a DataDriver callback subscription.
121
+ */
122
+ private debouncedDriverRefetch;
123
+ /**
124
+ * Fetch a collection with optional RLS auth context.
125
+ * When authContext is provided, the fetch runs inside a transaction
126
+ * with set_config calls so PostgreSQL RLS policies are enforced.
127
+ */
128
+ private fetchCollectionWithAuth;
129
+ /**
130
+ * Debounce an entity refetch for a WebSocket subscription.
131
+ */
132
+ private debouncedEntityRefetch;
133
+ /**
134
+ * Debounce an entity refetch for a Driver callback subscription.
135
+ */
136
+ private debouncedEntityDriverRefetch;
137
+ /**
138
+ * Fetch a single entity with optional RLS auth context.
139
+ */
140
+ private fetchEntityWithAuth;
141
+ private sendCollectionUpdate;
142
+ private sendEntityUpdate;
143
+ /**
144
+ * Send a lightweight entity-level patch to a collection subscriber.
145
+ * The client can merge this into its cached data for instant feedback.
146
+ */
147
+ private sendCollectionEntityPatch;
148
+ private sendError;
149
+ private sendMessage;
150
+ /**
151
+ * Extract parent paths from a nested path like "posts/70/tags"
152
+ * Returns ["posts", "posts/70"] for the example above
153
+ */
154
+ private getParentPaths;
155
+ /**
156
+ * Enable cross-instance realtime broadcasting via Postgres LISTEN/NOTIFY.
157
+ * Creates a dedicated pg.Client (outside the Drizzle pool) that stays
158
+ * connected and listens for change notifications from other instances.
159
+ *
160
+ * This is an **optional** feature — if never called, the backend operates
161
+ * in single-instance mode (the default, perfectly fine for most setups).
162
+ *
163
+ * @param connectionString Raw Postgres connection string for the LISTEN client.
164
+ */
165
+ startListening(connectionString: string): Promise<void>;
166
+ /**
167
+ * Stop listening and clean up the dedicated LISTEN connection.
168
+ */
169
+ stopListening(): Promise<void>;
170
+ /**
171
+ * Broadcast a change notification to other instances via pg_notify.
172
+ * Uses the main Drizzle connection (pooled) — NOT the LISTEN client.
173
+ */
174
+ private broadcastChange;
175
+ /**
176
+ * Create and connect the dedicated LISTEN client with auto-reconnect.
177
+ */
178
+ private connectListenClient;
179
+ /**
180
+ * Schedule a reconnection attempt with a fixed 3s delay.
181
+ */
182
+ private scheduleReconnect;
183
+ }
184
+ /**
185
+ * Alias for RealtimeService for consistent naming with other database implementations.
186
+ * This allows code to use PostgresRealtimeProvider alongside future MongoRealtimeProvider, etc.
187
+ */
188
+ export declare const PostgresRealtimeProvider: typeof RealtimeService;
@@ -0,0 +1,116 @@
1
+ import { SQL } from "drizzle-orm";
2
+ import { AnyPgColumn, PgTable } from "drizzle-orm/pg-core";
3
+ import { FilterValues, WhereFilterOp, Relation } from "@rebasepro/types";
4
+ import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry";
5
+ /** Drizzle dynamic query builder — accepts innerJoin + where chaining */
6
+ export interface DrizzleDynamicQuery {
7
+ innerJoin(table: PgTable<any>, condition: SQL): this;
8
+ where(condition: SQL | undefined): this;
9
+ limit(limit: number): this;
10
+ }
11
+ /**
12
+ * Unified condition builder for Drizzle/PostgreSQL queries.
13
+ *
14
+ * This class uses static methods and satisfies the ConditionBuilderStatic<SQL> type.
15
+ * It translates Rebase filter conditions to Drizzle SQL conditions.
16
+ *
17
+ * @example
18
+ * const builder: ConditionBuilderStatic<SQL> = DrizzleConditionBuilder;
19
+ */
20
+ export declare class DrizzleConditionBuilder {
21
+ /**
22
+ * Build filter conditions from FilterValues
23
+ */
24
+ static buildFilterConditions<M extends Record<string, unknown>>(filter: FilterValues<Extract<keyof M, string>>, table: PgTable<any>, collectionPath: string): SQL[];
25
+ /**
26
+ * Build a single filter condition for a specific operator and value
27
+ */
28
+ static buildSingleFilterCondition(column: AnyPgColumn, op: WhereFilterOp, value: unknown): SQL | null;
29
+ /**
30
+ * Build relation-based conditions for different relation types
31
+ */
32
+ static buildRelationConditions(relation: Relation, parentEntityId: string | number | (string | number)[], targetTable: PgTable<any>, parentTable: PgTable<any>, parentIdColumn: AnyPgColumn, targetIdColumn: AnyPgColumn, registry: PostgresCollectionRegistry): {
33
+ joinConditions: {
34
+ table: PgTable<any>;
35
+ condition: SQL;
36
+ }[];
37
+ whereConditions: SQL[];
38
+ };
39
+ /**
40
+ * Build conditions for join path relations
41
+ */
42
+ private static buildJoinPathConditions;
43
+ /**
44
+ * Build a single join condition between tables
45
+ */
46
+ private static buildSingleJoinCondition;
47
+ /**
48
+ * Try to build a junction table join when direct foreign key relationship is not found
49
+ */
50
+ private static tryBuildJunctionJoin;
51
+ /**
52
+ * Build conditions for junction table (many-to-many) relations
53
+ */
54
+ private static buildJunctionTableConditions;
55
+ /**
56
+ * Build conditions for inverse junction table (many-to-many) relations
57
+ */
58
+ private static buildInverseJunctionTableConditions;
59
+ /**
60
+ * Build conditions for simple relations (owning/inverse without join paths)
61
+ */
62
+ private static buildSimpleRelationCondition;
63
+ /**
64
+ * Combine multiple conditions with AND operator
65
+ */
66
+ static combineConditionsWithAnd(conditions: SQL[]): SQL | undefined;
67
+ /**
68
+ * Combine multiple conditions with OR operator
69
+ */
70
+ static combineConditionsWithOr(conditions: SQL[]): SQL | undefined;
71
+ /**
72
+ * Build search conditions for text fields
73
+ */
74
+ static buildSearchConditions(searchString: string, properties: Record<string, unknown>, table: PgTable<any>): SQL[];
75
+ /**
76
+ * Build a unique field check condition
77
+ */
78
+ static buildUniqueFieldCondition(fieldColumn: AnyPgColumn, value: unknown, idColumn?: AnyPgColumn, excludeId?: string | number): SQL[];
79
+ /**
80
+ * Build relation-based query with joins and conditions
81
+ */
82
+ static buildRelationQuery<T extends DrizzleDynamicQuery>(baseQuery: T, relation: Relation, parentEntityId: string | number | (string | number)[], targetTable: PgTable<any>, parentTable: PgTable<any>, parentIdColumn: AnyPgColumn, targetIdColumn: AnyPgColumn, registry: PostgresCollectionRegistry, additionalFilters?: SQL[]): T;
83
+ /**
84
+ * Build count query for relations with proper joins and conditions
85
+ */
86
+ static buildRelationCountQuery<T extends DrizzleDynamicQuery>(baseCountQuery: T, relation: Relation, parentEntityId: string | number, targetTable: PgTable<any>, parentTable: PgTable<any>, parentIdColumn: AnyPgColumn, targetIdColumn: AnyPgColumn, registry: PostgresCollectionRegistry, additionalFilters?: SQL[]): T;
87
+ /**
88
+ * Build join path conditions for count queries
89
+ */
90
+ private static buildJoinPathCountQuery;
91
+ /**
92
+ * Build junction table conditions for count queries
93
+ */
94
+ private static buildJunctionCountQuery;
95
+ /**
96
+ * Build inverse junction table conditions for count queries
97
+ */
98
+ private static buildInverseJunctionCountQuery;
99
+ /**
100
+ * Helper method to extract table names from columns
101
+ */
102
+ static getTableNamesFromColumns(columns: string | string[]): string[];
103
+ /**
104
+ * Helper method to extract column names from columns
105
+ */
106
+ static getColumnNamesFromColumns(columns: string | string[]): string[];
107
+ /**
108
+ * Find the corresponding junction table for an inverse many-to-many relation
109
+ */
110
+ private static findCorrespondingJunctionTable;
111
+ }
112
+ /**
113
+ * Alias for DrizzleConditionBuilder for consistent naming with other database implementations.
114
+ * This allows code to use PostgresConditionBuilder alongside future MongoConditionBuilder, etc.
115
+ */
116
+ export declare const PostgresConditionBuilder: typeof DrizzleConditionBuilder;
@@ -0,0 +1,5 @@
1
+ import { RealtimeService } from "./services/realtimeService";
2
+ import { PostgresBackendDriver } from "./PostgresBackendDriver";
3
+ import { Server } from "http";
4
+ import { AuthConfig } from "@rebasepro/server-core";
5
+ export declare function createPostgresWebSocket(server: Server, realtimeService: RealtimeService, driver: PostgresBackendDriver, authConfig?: AuthConfig): void;
@@ -0,0 +1,7 @@
1
+ export type AnalyticsController = {
2
+ /**
3
+ * Callback used to get analytics events from the CMS
4
+ */
5
+ onAnalyticsEvent?: (event: AnalyticsEvent, data?: object) => void;
6
+ };
7
+ export type AnalyticsEvent = "entity_click" | "entity_click_from_reference" | "reference_selection_clear" | "reference_selection_toggle" | "reference_selected_single" | "reference_selection_new_entity" | "edit_entity_clicked" | "entity_edited" | "new_entity_click" | "new_entity_saved" | "copy_entity_click" | "entity_copied" | "single_delete_dialog_open" | "multiple_delete_dialog_open" | "single_entity_deleted" | "multiple_entities_deleted" | "drawer_navigate_to_home" | "drawer_navigate_to_collection" | "drawer_navigate_to_view" | "home_navigate_to_collection" | "home_favorite_navigate_to_collection" | "home_navigate_to_view" | "home_navigate_to_admin_view" | "home_favorite_navigate_to_view" | "home_move_card" | "home_move_group" | "home_drop_new_group" | "collection_inline_editing" | "view_mode_changed" | "kanban_card_moved" | "kanban_column_reorder" | "kanban_property_changed" | "kanban_new_entity_in_column" | "kanban_backfill_order" | "card_view_entity_click" | "unmapped_event";
@@ -0,0 +1,119 @@
1
+ import { StorageSource } from "./storage";
2
+ import { Role, User } from "../users";
3
+ import { RebaseData } from "./data";
4
+ /**
5
+ * Capabilities advertised by an auth provider.
6
+ * UI components use this to show/hide features dynamically
7
+ * (e.g. password reset, registration, session management).
8
+ * @group Hooks and utilities
9
+ */
10
+ export interface AuthCapabilities {
11
+ emailPasswordLogin?: boolean;
12
+ googleLogin?: boolean;
13
+ registration?: boolean;
14
+ passwordReset?: boolean;
15
+ sessionManagement?: boolean;
16
+ profileUpdate?: boolean;
17
+ emailVerification?: boolean;
18
+ /** List of enabled OAuth provider IDs (e.g. ["google", "github", "discord"]) */
19
+ enabledProviders?: string[];
20
+ }
21
+ /**
22
+ * Controller for retrieving the logged user or performing auth related operations.
23
+ * Note that if you are implementing your AuthController, you probably will want
24
+ * to do it as the result of a hook.
25
+ * @group Hooks and utilities
26
+ */
27
+ export type AuthController<USER extends User = User, ExtraData = unknown> = {
28
+ /**
29
+ * The user currently logged in
30
+ * The values can be: the user object, null if they skipped login
31
+ */
32
+ user: USER | null;
33
+ /**
34
+ * Initial loading flag. It is used not to display the login screen
35
+ * when the app first loads, and it has not been checked whether the user
36
+ * is logged in or not.
37
+ */
38
+ initialLoading?: boolean;
39
+ /**
40
+ * Loading flag. It is used to display a loading screen when the user is
41
+ * logging in or out.
42
+ */
43
+ authLoading: boolean;
44
+ /**
45
+ * Sign out
46
+ */
47
+ signOut: () => Promise<void>;
48
+ /**
49
+ * Error initializing the authentication
50
+ */
51
+ authError?: unknown;
52
+ /**
53
+ * Error dispatched by the auth provider
54
+ */
55
+ authProviderError?: unknown;
56
+ /**
57
+ * You can use this method to retrieve the auth token for the current user.
58
+ */
59
+ getAuthToken: () => Promise<string>;
60
+ /**
61
+ * Has the user skipped the login process
62
+ */
63
+ loginSkipped: boolean;
64
+ extra: ExtraData;
65
+ setExtra: (extra: ExtraData) => void;
66
+ setUser?(user: USER | null): void;
67
+ setUserRoles?(roles: Role[]): void;
68
+ /**
69
+ * Capabilities advertised by the auth provider.
70
+ * UI components use this to feature-detect what the backend supports.
71
+ */
72
+ capabilities?: AuthCapabilities;
73
+ };
74
+ /**
75
+ * Extended auth controller with common optional auth methods.
76
+ * Backend implementations (Rebase backend, Firebase, Supabase, etc.)
77
+ * extend this with their own backend-specific extras.
78
+ * @group Hooks and utilities
79
+ */
80
+ export interface AuthControllerExtended<USER extends User = User, ExtraData = unknown> extends AuthController<USER, ExtraData> {
81
+ /** Login with email and password */
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>;
85
+ /** Register a new user */
86
+ register?(email: string, password: string, displayName?: string): Promise<void>;
87
+ /** Skip login (for anonymous access if enabled) */
88
+ skipLogin?(): void;
89
+ /** Request password reset email */
90
+ forgotPassword?(email: string): Promise<void>;
91
+ /** Reset password using a token */
92
+ resetPassword?(token: string, password: string): Promise<void>;
93
+ /** Change password for the authenticated user */
94
+ changePassword?(oldPassword: string, newPassword: string): Promise<void>;
95
+ /** Update user profile */
96
+ updateProfile?(displayName?: string, photoURL?: string): Promise<USER>;
97
+ }
98
+ /**
99
+ * Implement this function to allow access to specific users.
100
+ * @group Hooks and utilities
101
+ */
102
+ export type Authenticator<USER extends User = User> = (props: {
103
+ /**
104
+ * Logged-in user or null
105
+ */
106
+ user: USER | null;
107
+ /**
108
+ * AuthController
109
+ */
110
+ authController: AuthController<USER>;
111
+ /**
112
+ * Unified data access API
113
+ */
114
+ data: RebaseData;
115
+ /**
116
+ * Used storage implementation
117
+ */
118
+ storageSource: StorageSource;
119
+ }) => boolean | Promise<boolean>;