@rebasepro/server-postgres 0.0.1-canary.4829d6e

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 (121) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +86 -0
  3. package/dist/PostgresAdapter.d.ts +6 -0
  4. package/dist/PostgresBackendDriver.d.ts +150 -0
  5. package/dist/PostgresBootstrapper.d.ts +51 -0
  6. package/dist/auth/ensure-tables.d.ts +10 -0
  7. package/dist/auth/services.d.ts +250 -0
  8. package/dist/backup/backup-cli.d.ts +3 -0
  9. package/dist/backup/backup-cron.d.ts +53 -0
  10. package/dist/backup/backup-service.d.ts +85 -0
  11. package/dist/backup/index.d.ts +12 -0
  12. package/dist/backup/pg-tools.d.ts +110 -0
  13. package/dist/backup/retention.d.ts +35 -0
  14. package/dist/chunk-DSJWtz9O.js +40 -0
  15. package/dist/cli-errors.d.ts +42 -0
  16. package/dist/cli-helpers.d.ts +7 -0
  17. package/dist/cli.d.ts +1 -0
  18. package/dist/collections/PostgresCollectionRegistry.d.ts +47 -0
  19. package/dist/connection.d.ts +65 -0
  20. package/dist/data-transformer.d.ts +55 -0
  21. package/dist/databasePoolManager.d.ts +20 -0
  22. package/dist/history/HistoryService.d.ts +71 -0
  23. package/dist/history/ensure-history-table.d.ts +7 -0
  24. package/dist/index.d.ts +15 -0
  25. package/dist/index.es.js +23535 -0
  26. package/dist/index.es.js.map +1 -0
  27. package/dist/interfaces.d.ts +18 -0
  28. package/dist/schema/auth-bootstrap-sql.d.ts +24 -0
  29. package/dist/schema/auth-default-policies.d.ts +10 -0
  30. package/dist/schema/auth-schema.d.ts +2376 -0
  31. package/dist/schema/doctor-cli.d.ts +2 -0
  32. package/dist/schema/doctor.d.ts +58 -0
  33. package/dist/schema/dynamic-tables.d.ts +31 -0
  34. package/dist/schema/generate-drizzle-schema-logic.d.ts +2 -0
  35. package/dist/schema/generate-drizzle-schema.d.ts +1 -0
  36. package/dist/schema/generate-postgres-ddl-logic.d.ts +5 -0
  37. package/dist/schema/generate-postgres-ddl.d.ts +1 -0
  38. package/dist/schema/introspect-db-inference.d.ts +5 -0
  39. package/dist/schema/introspect-db-logic.d.ts +118 -0
  40. package/dist/schema/introspect-db.d.ts +1 -0
  41. package/dist/schema/introspect-runtime.d.ts +57 -0
  42. package/dist/schema/test-schema.d.ts +24 -0
  43. package/dist/security/policy-drift.d.ts +57 -0
  44. package/dist/security/rls-enforcement.d.ts +122 -0
  45. package/dist/services/BranchService.d.ts +47 -0
  46. package/dist/services/FetchService.d.ts +214 -0
  47. package/dist/services/PersistService.d.ts +39 -0
  48. package/dist/services/RelationService.d.ts +109 -0
  49. package/dist/services/cdc/CdcListener.d.ts +54 -0
  50. package/dist/services/cdc/trigger-cdc.d.ts +64 -0
  51. package/dist/services/collection-helpers.d.ts +38 -0
  52. package/dist/services/dataService.d.ts +110 -0
  53. package/dist/services/index.d.ts +4 -0
  54. package/dist/services/realtimeService.d.ts +298 -0
  55. package/dist/src-Eh-CZosp.js +595 -0
  56. package/dist/src-Eh-CZosp.js.map +1 -0
  57. package/dist/types.d.ts +3 -0
  58. package/dist/utils/drizzle-conditions.d.ts +138 -0
  59. package/dist/utils/pg-array-null-patch.d.ts +16 -0
  60. package/dist/utils/pg-error-utils.d.ts +65 -0
  61. package/dist/utils/table-classification.d.ts +8 -0
  62. package/dist/websocket.d.ts +18 -0
  63. package/package.json +113 -0
  64. package/src/PostgresAdapter.ts +58 -0
  65. package/src/PostgresBackendDriver.ts +1387 -0
  66. package/src/PostgresBootstrapper.ts +581 -0
  67. package/src/auth/ensure-tables.ts +367 -0
  68. package/src/auth/services.ts +1321 -0
  69. package/src/backup/backup-cli.ts +383 -0
  70. package/src/backup/backup-cron.ts +189 -0
  71. package/src/backup/backup-service.ts +299 -0
  72. package/src/backup/index.ts +12 -0
  73. package/src/backup/pg-tools.ts +231 -0
  74. package/src/backup/retention.ts +75 -0
  75. package/src/cli-errors.ts +265 -0
  76. package/src/cli-helpers.ts +196 -0
  77. package/src/cli.ts +786 -0
  78. package/src/collections/PostgresCollectionRegistry.ts +103 -0
  79. package/src/connection.ts +166 -0
  80. package/src/data-transformer.ts +733 -0
  81. package/src/databasePoolManager.ts +87 -0
  82. package/src/history/HistoryService.ts +272 -0
  83. package/src/history/ensure-history-table.ts +46 -0
  84. package/src/index.ts +15 -0
  85. package/src/interfaces.ts +60 -0
  86. package/src/schema/auth-bootstrap-sql.ts +41 -0
  87. package/src/schema/auth-default-policies.ts +125 -0
  88. package/src/schema/auth-schema.ts +233 -0
  89. package/src/schema/doctor-cli.ts +113 -0
  90. package/src/schema/doctor.ts +733 -0
  91. package/src/schema/dynamic-tables.test.ts +302 -0
  92. package/src/schema/dynamic-tables.ts +293 -0
  93. package/src/schema/generate-drizzle-schema-logic.ts +850 -0
  94. package/src/schema/generate-drizzle-schema.ts +131 -0
  95. package/src/schema/generate-postgres-ddl-logic.ts +490 -0
  96. package/src/schema/generate-postgres-ddl.ts +92 -0
  97. package/src/schema/introspect-db-inference.ts +238 -0
  98. package/src/schema/introspect-db-logic.ts +910 -0
  99. package/src/schema/introspect-db.ts +266 -0
  100. package/src/schema/introspect-runtime.test.ts +212 -0
  101. package/src/schema/introspect-runtime.ts +293 -0
  102. package/src/schema/test-schema.ts +11 -0
  103. package/src/security/policy-drift.test.ts +122 -0
  104. package/src/security/policy-drift.ts +159 -0
  105. package/src/security/rls-enforcement.ts +295 -0
  106. package/src/services/BranchService.ts +251 -0
  107. package/src/services/FetchService.ts +1661 -0
  108. package/src/services/PersistService.ts +329 -0
  109. package/src/services/RelationService.ts +1306 -0
  110. package/src/services/cdc/CdcListener.ts +167 -0
  111. package/src/services/cdc/trigger-cdc.ts +169 -0
  112. package/src/services/collection-helpers.ts +151 -0
  113. package/src/services/dataService.ts +246 -0
  114. package/src/services/index.ts +13 -0
  115. package/src/services/realtimeService.ts +1502 -0
  116. package/src/types.ts +4 -0
  117. package/src/utils/drizzle-conditions.ts +1162 -0
  118. package/src/utils/pg-array-null-patch.ts +42 -0
  119. package/src/utils/pg-error-utils.ts +227 -0
  120. package/src/utils/table-classification.ts +16 -0
  121. package/src/websocket.ts +640 -0
@@ -0,0 +1,246 @@
1
+ // import { NodePgDatabase } from "drizzle-orm/node-postgres";
2
+ import { FilterValues } from "@rebasepro/types";
3
+ import type { VectorSearchParams } from "@rebasepro/types";
4
+ import { FetchService } from "./FetchService";
5
+ import { PersistService } from "./PersistService";
6
+ import { RelationService } from "./RelationService";
7
+ import { DataRepository, FetchCollectionOptions, SearchOptions, CountOptions, DrizzleClient } from "../interfaces";
8
+ import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry";
9
+
10
+ // Re-export data transformer functions for external use
11
+ export { sanitizeAndConvertDates, serializeDataToServer, parseDataFromServer } from "../data-transformer";
12
+
13
+ // Re-export service classes for direct use
14
+ export { FetchService } from "./FetchService";
15
+ export { PersistService } from "./PersistService";
16
+ export { RelationService } from "./RelationService";
17
+
18
+ // Re-export interfaces
19
+ export * from "../interfaces";
20
+
21
+ /**
22
+ * DataService - Facade for row operations.
23
+ *
24
+ * This class provides a unified API for row CRUD operations by delegating
25
+ * to specialized services:
26
+ * - FetchService: Read operations (fetch, search, count)
27
+ * - PersistService: Write operations (save, delete)
28
+ * - RelationService: Relation operations (fetch related, update relations)
29
+ *
30
+ * Implements the DataRepository interface for database abstraction.
31
+ */
32
+ export class DataService implements DataRepository {
33
+ private fetchService: FetchService;
34
+ private persistService: PersistService;
35
+
36
+ constructor(private db: DrizzleClient, private registry: PostgresCollectionRegistry) {
37
+ this.fetchService = new FetchService(db, registry);
38
+ this.persistService = new PersistService(db, registry);
39
+ }
40
+
41
+ // =============================================================
42
+ // READ OPERATIONS - Delegated to FetchService
43
+ // =============================================================
44
+
45
+ /**
46
+ * Fetch a single row by ID
47
+ */
48
+ async fetchOne<M extends Record<string, unknown>>(
49
+ collectionPath: string,
50
+ id: string | number,
51
+ databaseId?: string
52
+ ): Promise<Record<string, unknown> | undefined> {
53
+ return this.fetchService.fetchOne<M>(collectionPath, id, databaseId);
54
+ }
55
+
56
+ /**
57
+ * Fetch a collection of rows with optional filtering, ordering, and pagination
58
+ */
59
+ async fetchCollection<M extends Record<string, unknown>>(
60
+ collectionPath: string,
61
+ options: {
62
+ filter?: FilterValues<Extract<keyof M, string>>;
63
+ orderBy?: string;
64
+ order?: "desc" | "asc";
65
+ limit?: number;
66
+ offset?: number;
67
+ startAfter?: Record<string, unknown>;
68
+ searchString?: string;
69
+ databaseId?: string;
70
+ vectorSearch?: VectorSearchParams;
71
+ } = {}
72
+ ): Promise<Record<string, unknown>[]> {
73
+ return this.fetchService.fetchCollection<M>(collectionPath, options);
74
+ }
75
+
76
+ /**
77
+ * Search rows by text
78
+ */
79
+ async searchRows<M extends Record<string, unknown>>(
80
+ collectionPath: string,
81
+ searchString: string,
82
+ options: {
83
+ filter?: FilterValues<Extract<keyof M, string>>;
84
+ orderBy?: string;
85
+ order?: "desc" | "asc";
86
+ limit?: number;
87
+ databaseId?: string;
88
+ } = {}
89
+ ): Promise<Record<string, unknown>[]> {
90
+ return this.fetchService.searchRows<M>(collectionPath, searchString, options);
91
+ }
92
+
93
+ /**
94
+ * Count rows in a collection
95
+ */
96
+ async count<M extends Record<string, unknown>>(
97
+ collectionPath: string,
98
+ options: {
99
+ filter?: FilterValues<Extract<keyof M, string>>;
100
+ searchString?: string;
101
+ databaseId?: string;
102
+ } = {}
103
+ ): Promise<number> {
104
+ return this.fetchService.count<M>(collectionPath, options);
105
+ }
106
+
107
+ /**
108
+ * Check if a field value is unique in a collection
109
+ */
110
+ async checkUniqueField(
111
+ collectionPath: string,
112
+ fieldName: string,
113
+ value: unknown,
114
+ excludeEntityId?: string,
115
+ databaseId?: string
116
+ ): Promise<boolean> {
117
+ return this.fetchService.checkUniqueField(collectionPath, fieldName, value, excludeEntityId, databaseId);
118
+ }
119
+
120
+ /**
121
+ * Fetch rows related to a parent row
122
+ */
123
+ async fetchRelatedEntities<M extends Record<string, unknown>>(
124
+ parentCollectionPath: string,
125
+ parentId: string | number,
126
+ relationKey: string,
127
+ options: {
128
+ filter?: FilterValues<Extract<keyof M, string>>;
129
+ orderBy?: string;
130
+ order?: "desc" | "asc";
131
+ limit?: number;
132
+ startAfter?: Record<string, unknown>;
133
+ searchString?: string;
134
+ databaseId?: string;
135
+ } = {}
136
+ ): Promise<Record<string, unknown>[]> {
137
+ const rows = await this.fetchService.getRelationService().fetchRelatedEntities<M>(
138
+ parentCollectionPath,
139
+ parentId,
140
+ relationKey,
141
+ options
142
+ );
143
+ return rows.map(e => ({ ...e.values, id: e.id }));
144
+ }
145
+
146
+ // =============================================================
147
+ // WRITE OPERATIONS - Delegated to PersistService
148
+ // =============================================================
149
+
150
+ /**
151
+ * Save an row (create or update)
152
+ */
153
+ async save<M extends Record<string, unknown>>(
154
+ collectionPath: string,
155
+ values: Partial<M>,
156
+ id?: string | number,
157
+ databaseId?: string
158
+ ): Promise<Record<string, unknown>> {
159
+ return this.persistService.save<M>(collectionPath, values, id, databaseId);
160
+ }
161
+
162
+ /**
163
+ * Delete an row by ID
164
+ */
165
+ async delete(
166
+ collectionPath: string,
167
+ id: string | number,
168
+ databaseId?: string
169
+ ): Promise<void> {
170
+ return this.persistService.delete(collectionPath, id, databaseId);
171
+ }
172
+
173
+ /**
174
+ * Delete all rows from a collection
175
+ */
176
+ async deleteAll(collectionPath: string, databaseId?: string): Promise<void> {
177
+ return this.persistService.deleteAll(collectionPath, databaseId);
178
+ }
179
+
180
+
181
+ /**
182
+ * Execute raw SQL
183
+ */
184
+ async executeSql(sqlText: string, params?: unknown[]): Promise<Record<string, unknown>[]> {
185
+ if (process.env.NODE_ENV !== "production") {
186
+ console.debug("Executing raw SQL:", sqlText, params?.length ? `with ${params.length} params` : "");
187
+ }
188
+ const { sql } = await import("drizzle-orm");
189
+
190
+ let result;
191
+ if (params && params.length > 0) {
192
+ // Build a parameterized query using Drizzle's sql tagged template.
193
+ // Split the SQL text on $1, $2, … placeholders and interleave
194
+ // with sql.param() calls so the underlying pg driver binds them safely.
195
+ const parts = sqlText.split(/\$(\d+)/);
196
+ const chunks: ReturnType<typeof sql.raw | typeof sql.param>[] = [];
197
+ for (let i = 0; i < parts.length; i++) {
198
+ if (i % 2 === 0) {
199
+ // Literal SQL text fragment
200
+ if (parts[i].length > 0) {
201
+ chunks.push(sql.raw(parts[i]));
202
+ }
203
+ } else {
204
+ // Parameter reference — $N (1-indexed)
205
+ const paramIndex = Number(parts[i]) - 1;
206
+ chunks.push(sql.param(params[paramIndex]));
207
+ }
208
+ }
209
+ const query = sql.join(chunks, sql.raw(""));
210
+ result = await this.db.execute(query);
211
+ } else {
212
+ result = await this.db.execute(sql.raw(sqlText));
213
+ }
214
+
215
+ const rows = result.rows;
216
+ if (process.env.NODE_ENV !== "production") {
217
+ console.debug(`SQL executed successfully. Returned ${Array.isArray(rows) ? rows.length : "non-array"} rows.`);
218
+ }
219
+ return rows as Record<string, unknown>[];
220
+ }
221
+
222
+ // =============================================================
223
+ // SERVICE ACCESSORS
224
+ // =============================================================
225
+
226
+ /**
227
+ * Get the underlying FetchService for advanced use
228
+ */
229
+ getFetchService(): FetchService {
230
+ return this.fetchService;
231
+ }
232
+
233
+ /**
234
+ * Get the underlying PersistService for advanced use
235
+ */
236
+ getPersistService(): PersistService {
237
+ return this.persistService;
238
+ }
239
+
240
+ /**
241
+ * Get the underlying RelationService for advanced use
242
+ */
243
+ getRelationService(): RelationService {
244
+ return this.fetchService.getRelationService();
245
+ }
246
+ }
@@ -0,0 +1,13 @@
1
+ // Re-export all service classes
2
+ export { FetchService } from "./FetchService";
3
+ export { PersistService } from "./PersistService";
4
+ export { RelationService } from "./RelationService";
5
+
6
+ // Re-export helper functions
7
+ export {
8
+ getCollectionByPath,
9
+ getTableForCollection,
10
+ getPrimaryKeys,
11
+ parseIdValues,
12
+ buildCompositeId
13
+ } from "./collection-helpers";