@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,608 @@
1
+ import { eq, SQL } from "drizzle-orm";
2
+ import { AnyPgColumn } from "drizzle-orm/pg-core";
3
+ import { NodePgDatabase } from "drizzle-orm/node-postgres";
4
+ import { CollectionWithRelations, EntityCollection, Properties, Property, Relation, RelationProperty } from "@rebasepro/types";
5
+ import { getTableName, resolveCollectionRelations, findRelation, createRelationRef, DEFAULT_ONE_OF_TYPE, DEFAULT_ONE_OF_VALUE } from "@rebasepro/common";
6
+ import { PostgresCollectionRegistry } from "./collections/PostgresCollectionRegistry";
7
+ import { DrizzleConditionBuilder } from "./utils/drizzle-conditions";
8
+ import { getPrimaryKeys, buildCompositeId } from "./services/entity-helpers";
9
+
10
+ /**
11
+ * Data transformation utilities for converting between frontend and database formats.
12
+ */
13
+
14
+ /**
15
+ * Typed result from `serializeDataToServer`.
16
+ * Replaces the hidden `__inverseRelationUpdates` / `__joinPathRelationUpdates`
17
+ * dunder-property mutation pattern with explicit, typed state management.
18
+ */
19
+ export interface SerializedEntityData {
20
+ /** Scalar column values ready for INSERT/UPDATE. */
21
+ scalarData: Record<string, unknown>;
22
+ /** Inverse relation updates that must be applied to target tables. */
23
+ inverseRelationUpdates: Array<{
24
+ relationKey: string;
25
+ relation: Relation;
26
+ newValue: unknown;
27
+ currentEntityId?: string | number;
28
+ }>;
29
+ /** JoinPath relation updates that require multi-hop writes. */
30
+ joinPathRelationUpdates: Array<{
31
+ relationKey: string;
32
+ relation: Relation;
33
+ newTargetId: string | number | null;
34
+ }>;
35
+ }
36
+ /**
37
+ * Helper function to sanitize and convert dates to ISO strings
38
+ */
39
+ export function sanitizeAndConvertDates(obj: unknown): unknown {
40
+ if (obj === null || obj === undefined) {
41
+ return null;
42
+ }
43
+
44
+ if (typeof obj === "number" && isNaN(obj)) {
45
+ return null;
46
+ }
47
+
48
+ if (typeof obj === "string" && obj.toLowerCase() === "nan") {
49
+ return null;
50
+ }
51
+
52
+ if (Array.isArray(obj)) {
53
+ return obj.map(v => sanitizeAndConvertDates(v));
54
+ }
55
+
56
+ if (obj instanceof Date) {
57
+ return obj.toISOString();
58
+ }
59
+
60
+ if (typeof obj === "object") {
61
+ const newObj: Record<string, unknown> = {};
62
+ for (const key in obj) {
63
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
64
+ newObj[key] = sanitizeAndConvertDates((obj as Record<string, unknown>)[key]);
65
+ }
66
+ }
67
+ return newObj;
68
+ }
69
+
70
+ if (typeof obj === "string") {
71
+ const isoDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/;
72
+ const jsDateRegex = /^\w{3} \w{3} \d{2} \d{4} \d{2}:\d{2}:\d{2} GMT[+-]\d{4} \(.+\)$/;
73
+ if (isoDateRegex.test(obj) || jsDateRegex.test(obj)) {
74
+ const date = new Date(obj);
75
+ if (!isNaN(date.getTime())) {
76
+ return date.toISOString();
77
+ }
78
+ }
79
+ }
80
+
81
+ return obj;
82
+ }
83
+
84
+ /**
85
+ * Transform relations for database storage (relation objects to IDs)
86
+ */
87
+ export function serializeDataToServer<M extends Record<string, unknown>>(
88
+ entity: M,
89
+ properties: Properties,
90
+ collection?: EntityCollection,
91
+ registry?: PostgresCollectionRegistry
92
+ ): SerializedEntityData {
93
+ if (!entity || !properties) return { scalarData: entity ?? {}, inverseRelationUpdates: [], joinPathRelationUpdates: [] };
94
+
95
+ const result: Record<string, unknown> = {};
96
+
97
+ // Get normalized relations if collection is provided
98
+ const resolvedRelations = collection ? resolveCollectionRelations(collection) : {};
99
+
100
+ // Track inverse relations that need to be handled separately
101
+ const inverseRelationUpdates: Array<{
102
+ relationKey: string;
103
+ relation: Relation;
104
+ newValue: unknown;
105
+ currentEntityId?: string | number;
106
+ }> = [];
107
+ const joinPathRelationUpdates: Array<{
108
+ relationKey: string;
109
+ relation: Relation;
110
+ newTargetId: string | number | null;
111
+ }> = [];
112
+
113
+ // Pre-calculate all local keys used as foreign keys
114
+ const foreignKeys = new Set<string>();
115
+ Object.values(resolvedRelations).forEach(relation => {
116
+ if (relation.localKey) foreignKeys.add(relation.localKey);
117
+ });
118
+
119
+ for (const [key, value] of Object.entries(entity)) {
120
+ const property = properties[key as keyof M] as Property;
121
+
122
+ // Coerce empty strings to null for any field that acts as a foreign key
123
+ const effectiveValue = (foreignKeys.has(key) && value === "") ? null : value;
124
+
125
+ if (!property) {
126
+ result[key] = effectiveValue;
127
+ continue;
128
+ }
129
+
130
+ // Handle relation properties specially
131
+ if (property.type === "relation" && collection) {
132
+ const relation = findRelation(resolvedRelations, key);
133
+ if (relation) {
134
+ if (relation.direction === "owning" && relation.localKey) {
135
+ // Owning relation: Map relation object to FK column on current table
136
+ const serializedValue = serializePropertyToServer(effectiveValue, property);
137
+ if (serializedValue !== undefined) {
138
+ result[relation.localKey] = serializedValue;
139
+ }
140
+ // Don't add the original relation property to the result
141
+ continue;
142
+ } else if (relation.direction === "inverse" && relation.foreignKeyOnTarget) {
143
+ // Inverse relation: Need to update the target table's FK
144
+ const serializedValue = serializePropertyToServer(effectiveValue, property);
145
+ const pks = getPrimaryKeys(collection, registry!);
146
+ inverseRelationUpdates.push({
147
+ relationKey: key,
148
+ relation,
149
+ newValue: serializedValue,
150
+ currentEntityId: (entity.id as string | number | undefined) || buildCompositeId(entity, pks)
151
+ });
152
+ // Don't add the original relation property to the result
153
+ continue;
154
+ } else if (relation.direction === "inverse" && relation.joinPath && relation.joinPath.length > 0) {
155
+ const serializedValue = serializePropertyToServer(effectiveValue, property);
156
+ if (relation.cardinality === "one") {
157
+ // One-to-one inverse joinPath: route through joinPathRelationUpdates.
158
+ // The write ordering in EntityPersistService ensures these are processed
159
+ // BEFORE the main UPDATE, so parentSourceCol reads the pre-update FK value.
160
+ // This prevents stale values from corrupting related entities when an
161
+ // intermediate FK (e.g. author_id) changes in the same save.
162
+ joinPathRelationUpdates.push({
163
+ relationKey: key,
164
+ relation,
165
+ newTargetId: serializedValue as string | number | null
166
+ });
167
+ } else {
168
+ // Many inverse joinPath: capture as inverse relation update
169
+ const pks = getPrimaryKeys(collection, registry!);
170
+ inverseRelationUpdates.push({
171
+ relationKey: key,
172
+ relation,
173
+ newValue: serializedValue,
174
+ currentEntityId: (entity.id as string | number | undefined) || buildCompositeId(entity, pks)
175
+ });
176
+ }
177
+ // Don't add the original relation property to the result
178
+ continue;
179
+ } else if (relation.cardinality === "one" && relation.direction === "owning" && relation.joinPath && relation.joinPath.length > 0) {
180
+ // Owning one-to-one via joinPath: capture as a write intent
181
+ const serializedValue = serializePropertyToServer(effectiveValue, property);
182
+ joinPathRelationUpdates.push({
183
+ relationKey: key,
184
+ relation,
185
+ newTargetId: serializedValue as string | number | null
186
+ });
187
+ // Don't include this property directly in payload
188
+ continue;
189
+ }
190
+ }
191
+ }
192
+
193
+ result[key] = serializePropertyToServer(effectiveValue, property);
194
+ }
195
+
196
+ return {
197
+ scalarData: result,
198
+ inverseRelationUpdates,
199
+ joinPathRelationUpdates
200
+ };
201
+ }
202
+
203
+ /**
204
+ * Serialize a single property value for database storage
205
+ */
206
+ export function serializePropertyToServer(value: unknown, property: Property): unknown {
207
+ if (value === null || value === undefined) {
208
+ return value;
209
+ }
210
+
211
+ const propertyType = property.type;
212
+
213
+ switch (propertyType) {
214
+ case "relation":
215
+ if (Array.isArray(value)) {
216
+ return value.map(v => serializePropertyToServer(v, property));
217
+ } else if (typeof value === "object" && value !== null && "id" in value) {
218
+ return (value as Record<string, unknown>).id;
219
+ }
220
+ if (value === "") return null;
221
+ return value;
222
+
223
+ case "array":
224
+ if (Array.isArray(value)) {
225
+ if (property.of) {
226
+ return value.map(item => serializePropertyToServer(item, property.of as Property));
227
+ } else if (property.oneOf) {
228
+ const typeField = property.oneOf.typeField ?? DEFAULT_ONE_OF_TYPE;
229
+ const valueField = property.oneOf.valueField ?? DEFAULT_ONE_OF_VALUE;
230
+ return value.map((e) => {
231
+ if (e === null) return null;
232
+ if (typeof e !== "object") return e;
233
+ const rec = e as Record<string, unknown>;
234
+ const type = rec[typeField] as string;
235
+ const childProperty = property.oneOf?.properties[type];
236
+ if (!type || !childProperty) return e;
237
+ return {
238
+ [typeField]: type,
239
+ [valueField]: serializePropertyToServer(rec[valueField], childProperty)
240
+ };
241
+ });
242
+ }
243
+ }
244
+ return value;
245
+
246
+ case "map":
247
+ if (typeof value === "object" && property.properties) {
248
+ const result: Record<string, unknown> = {};
249
+ for (const [subKey, subValue] of Object.entries(value)) {
250
+ const subProperty = (property.properties as Properties)[subKey];
251
+ if (subProperty) {
252
+ result[subKey] = serializePropertyToServer(subValue, subProperty);
253
+ } else {
254
+ result[subKey] = subValue;
255
+ }
256
+ }
257
+ return result;
258
+ }
259
+ return value;
260
+
261
+ default:
262
+ return value;
263
+ }
264
+ }
265
+
266
+ /**
267
+ * Transform IDs back to relation objects for frontend
268
+ */
269
+ export async function parseDataFromServer<M extends Record<string, unknown>>(
270
+ data: M,
271
+ collection: EntityCollection,
272
+ db?: NodePgDatabase<Record<string, unknown>>,
273
+ registry?: PostgresCollectionRegistry
274
+ ): Promise<M> {
275
+ const properties = collection.properties;
276
+ if (!data || !properties) return data;
277
+
278
+ // Get the normalized relations once
279
+ const resolvedRelations = resolveCollectionRelations(collection);
280
+
281
+ // Shared scalar + relation value normalization
282
+ const result = normalizeScalarValues(data, properties, collection, resolvedRelations, { skipRelations: false });
283
+
284
+ // Add relation properties that should be populated from FK values or inverse queries
285
+ for (const [propKey, property] of Object.entries(properties)) {
286
+ if (property.type === "relation" && !(propKey in result)) {
287
+ // Find the normalized relation for this property
288
+ const relation = findRelation(resolvedRelations, propKey);
289
+ if (relation) {
290
+ if (relation.direction === "owning" && relation.localKey && relation.localKey in data) {
291
+ // Owning relation: FK is in current table
292
+ const fkValue = data[relation.localKey as keyof M];
293
+ if (fkValue !== null && fkValue !== undefined) {
294
+ try {
295
+ const targetCollection = relation.target();
296
+ result[propKey] = createRelationRef(fkValue.toString(), targetCollection.slug);
297
+ } catch (e) {
298
+ console.warn(`Could not resolve target collection for relation property: ${propKey}`, e);
299
+ }
300
+ }
301
+ } else if (relation.direction === "inverse" && relation.foreignKeyOnTarget && db && registry) {
302
+ // Inverse relation: FK is in target table, need to query for it
303
+ try {
304
+ const targetCollection = relation.target();
305
+ const targetTable = registry.getTable(getTableName(targetCollection));
306
+ const pks = getPrimaryKeys(collection, registry!);
307
+ const currentEntityId = buildCompositeId(data, pks);
308
+
309
+ if (targetTable && currentEntityId) {
310
+ const foreignKeyColumn = targetTable[relation.foreignKeyOnTarget as keyof typeof targetTable] as AnyPgColumn;
311
+ if (foreignKeyColumn) {
312
+ // Query the target table to find entity that references this entity
313
+ const relatedEntities = await db
314
+ .select()
315
+ .from(targetTable)
316
+ .where(eq(foreignKeyColumn, currentEntityId))
317
+ .limit(relation.cardinality === "one" ? 1 : 100); // Limit for one-to-one vs one-to-many
318
+
319
+ if (relatedEntities.length > 0) {
320
+ if (relation.cardinality === "one") {
321
+ // One-to-one: return single relation object
322
+ const targetPks = getPrimaryKeys(targetCollection, registry!);
323
+ const relatedEntity = relatedEntities[0] as Record<string, unknown>;
324
+ result[propKey] = createRelationRef(buildCompositeId(relatedEntity, targetPks), targetCollection.slug);
325
+ } else {
326
+ // One-to-many: return array of relation objects
327
+ const targetPks = getPrimaryKeys(targetCollection, registry!);
328
+ result[propKey] = relatedEntities.map((entity: Record<string, unknown>) =>
329
+ createRelationRef(buildCompositeId(entity, targetPks), targetCollection.slug)
330
+ );
331
+ }
332
+ }
333
+ }
334
+ }
335
+ } catch (e) {
336
+ console.warn(`Could not resolve inverse relation property: ${propKey}`, e);
337
+ }
338
+ } else if (relation.direction === "inverse" && relation.joinPath && db && registry) {
339
+ // Join path relation: Multi-hop relation using joins
340
+ try {
341
+ const targetCollection = relation.target();
342
+ const pks = getPrimaryKeys(collection, registry!);
343
+ const currentEntityId = buildCompositeId(data, pks);
344
+
345
+ if (currentEntityId) {
346
+ // Build the join query following the join path
347
+ const sourceTable = registry.getTable(getTableName(collection));
348
+ if (!sourceTable) {
349
+ console.warn(`Source table not found for collection: ${collection.slug}`);
350
+ continue;
351
+ }
352
+
353
+ let query = db.select().from(sourceTable);
354
+ let currentTable = sourceTable;
355
+
356
+ // Apply each join in the path
357
+ for (const join of relation.joinPath) {
358
+ const joinTable = registry.getTable(join.table);
359
+ if (!joinTable) {
360
+ console.warn(`Join table not found: ${join.table}`);
361
+ break;
362
+ }
363
+
364
+ // Parse the join condition - handle both string and array formats
365
+ const fromColumn = Array.isArray(join.on.from) ? join.on.from[0] : join.on.from;
366
+ const toColumn = Array.isArray(join.on.to) ? join.on.to[0] : join.on.to;
367
+
368
+ const fromParts = fromColumn.split(".");
369
+ const toParts = toColumn.split(".");
370
+
371
+ const fromColName = fromParts[fromParts.length - 1];
372
+ const toColName = toParts[toParts.length - 1];
373
+
374
+ const fromCol = currentTable[fromColName as keyof typeof currentTable] as AnyPgColumn;
375
+ const toCol = joinTable[toColName as keyof typeof joinTable] as AnyPgColumn;
376
+
377
+ if (!fromCol || !toCol) {
378
+ console.warn(`Join columns not found: ${fromColumn} -> ${toColumn}`);
379
+ break;
380
+ }
381
+
382
+ query = query.innerJoin(joinTable, eq(fromCol, toCol)) as unknown as typeof query;
383
+ currentTable = joinTable;
384
+ }
385
+
386
+ // Add where condition for the current entity
387
+ if (pks.length === 1) {
388
+ const sourceIdField = sourceTable[pks[0].fieldName as keyof typeof sourceTable] as AnyPgColumn;
389
+ query = query.where(eq(sourceIdField, currentEntityId)) as unknown as typeof query;
390
+ } else {
391
+ // For composite keys, we would need to map the split parts. For now log a warning.
392
+ console.warn(`Join path resolution for composite primary keys is not yet fully supported: ${collection.slug}`);
393
+ }
394
+
395
+ // Build additional conditions array
396
+ const additionalFilters: SQL[] = [];
397
+
398
+ // Combine parent condition with additional filters using AND
399
+ let combinedWhere: SQL | undefined;
400
+
401
+ if (pks.length === 1) {
402
+ const sourceIdField = sourceTable[pks[0].fieldName as keyof typeof sourceTable] as AnyPgColumn;
403
+ combinedWhere = DrizzleConditionBuilder.combineConditionsWithAnd([
404
+ eq(sourceIdField, currentEntityId),
405
+ ...additionalFilters
406
+ ].filter(Boolean) as SQL[]);
407
+ }
408
+
409
+ // Execute the query
410
+ const joinResults = await query.where(combinedWhere).limit(relation.cardinality === "one" ? 1 : 100);
411
+
412
+ if (joinResults.length > 0) {
413
+ const targetPks = getPrimaryKeys(targetCollection, registry!);
414
+ const targetTableName = relation.joinPath[relation.joinPath.length - 1].table;
415
+
416
+ if (relation.cardinality === "one") {
417
+ // One-to-one: return single relation object
418
+ const joinResult = joinResults[0] as Record<string, unknown>;
419
+ const targetEntity = (joinResult[targetTableName] || joinResult) as Record<string, unknown>;
420
+ result[propKey] = createRelationRef(buildCompositeId(targetEntity, targetPks), targetCollection.slug);
421
+ } else {
422
+ // One-to-many: return array of relation objects
423
+ result[propKey] = joinResults.map((joinResult: Record<string, unknown>) => {
424
+ const targetEntity = (joinResult[targetTableName] || joinResult) as Record<string, unknown>;
425
+ return createRelationRef(buildCompositeId(targetEntity, targetPks), targetCollection.slug);
426
+ });
427
+ }
428
+ }
429
+ }
430
+ } catch (e) {
431
+ console.warn(`Could not resolve join path relation property: ${propKey}`, e);
432
+ }
433
+ }
434
+ }
435
+ }
436
+ }
437
+
438
+ return result as M;
439
+ }
440
+
441
+ /**
442
+ * Parse a single property value from database format to frontend format
443
+ */
444
+ export function parsePropertyFromServer(value: unknown, property: Property, collection: EntityCollection, propertyKey?: string): unknown {
445
+ if (value === null || value === undefined) {
446
+ return value;
447
+ }
448
+
449
+ switch (property.type) {
450
+ case "relation":
451
+ // Transform ID back to relation object with type information
452
+ if (typeof value === "string" || typeof value === "number") {
453
+ let relationDef: Relation | undefined = (property as RelationProperty).relation;
454
+ if (!relationDef && propertyKey) {
455
+ const resolvedRelations = resolveCollectionRelations(collection);
456
+ relationDef = findRelation(resolvedRelations, propertyKey);
457
+ }
458
+ if (!relationDef) {
459
+ relationDef = (collection as CollectionWithRelations).relations?.find((rel: Relation) => rel.relationName === (property as RelationProperty).relationName);
460
+ }
461
+
462
+ if (!relationDef) {
463
+ console.warn(`Relation not defined in property for key: ${propertyKey || "unknown"}`);
464
+ return value;
465
+ }
466
+
467
+ try {
468
+ const targetCollection = relationDef.target();
469
+ return createRelationRef(value.toString(), targetCollection.slug);
470
+ } catch (e) {
471
+ console.warn(`Could not resolve target collection for relation property: ${propertyKey || "unknown"}`, e);
472
+ return value;
473
+ }
474
+ }
475
+ return value;
476
+
477
+ case "array":
478
+ if (Array.isArray(value)) {
479
+ if (property.of) {
480
+ return value.map(item => parsePropertyFromServer(item, property.of as Property, collection));
481
+ } else if (property.oneOf) {
482
+ const typeField = property.oneOf.typeField ?? DEFAULT_ONE_OF_TYPE;
483
+ const valueField = property.oneOf.valueField ?? DEFAULT_ONE_OF_VALUE;
484
+ return value.map((e) => {
485
+ if (e === null) return null;
486
+ if (typeof e !== "object") return e;
487
+ const rec = e as Record<string, unknown>;
488
+ const type = rec[typeField] as string;
489
+ const childProperty = property.oneOf?.properties[type];
490
+ if (!type || !childProperty) return e;
491
+ return {
492
+ [typeField]: type,
493
+ [valueField]: parsePropertyFromServer(rec[valueField], childProperty, collection)
494
+ };
495
+ });
496
+ }
497
+ }
498
+ return value;
499
+
500
+ case "map":
501
+ if (typeof value === "object" && property.properties) {
502
+ const result: Record<string, unknown> = {};
503
+ for (const [subKey, subValue] of Object.entries(value)) {
504
+ const subProperty = (property.properties as Properties)[subKey];
505
+ if (subProperty) {
506
+ result[subKey] = parsePropertyFromServer(subValue, subProperty, collection);
507
+ } else {
508
+ result[subKey] = subValue;
509
+ }
510
+ }
511
+ return result;
512
+ }
513
+ return value;
514
+
515
+ case "number":
516
+ if (typeof value === "string") {
517
+ const parsed = parseFloat(value);
518
+ return isNaN(parsed) ? null : parsed;
519
+ }
520
+ return value;
521
+
522
+ case "date": {
523
+ let date: Date | undefined;
524
+ if (value instanceof Date) {
525
+ date = value;
526
+ } else if (typeof value === "string" || typeof value === "number") {
527
+ const parsedDate = new Date(value);
528
+ if (!isNaN(parsedDate.getTime())) {
529
+ date = parsedDate;
530
+ }
531
+ }
532
+ if (date) {
533
+ return {
534
+ __type: "date",
535
+ value: date.toISOString()
536
+ };
537
+ }
538
+ return null;
539
+ }
540
+
541
+ default:
542
+ return value;
543
+ }
544
+ }
545
+
546
+ /**
547
+ * Shared internal helper: normalizes scalar column values from a DB row
548
+ * into frontend format. Handles FK-column preservation, type coercion
549
+ * (dates, numbers, NaN), and property filtering.
550
+ *
551
+ * @param skipRelations When `true`, relation-typed properties are omitted
552
+ * from the result (used by `normalizeDbValues` where
553
+ * Drizzle's relational API already hydrates them).
554
+ */
555
+ function normalizeScalarValues<M extends Record<string, unknown>>(
556
+ data: M,
557
+ properties: Properties,
558
+ collection: EntityCollection,
559
+ resolvedRelations: Record<string, Relation>,
560
+ options: { skipRelations: boolean }
561
+ ): Record<string, unknown> {
562
+ const result: Record<string, unknown> = {};
563
+
564
+ // Identify FK columns used only for relations and not exposed as properties
565
+ const internalFKColumns = new Set<string>();
566
+ Object.values(resolvedRelations).forEach(relation => {
567
+ if (relation.localKey && !properties[relation.localKey]) {
568
+ internalFKColumns.add(relation.localKey);
569
+ }
570
+ });
571
+
572
+ for (const [key, value] of Object.entries(data)) {
573
+ // Keep internal FK columns as primitives
574
+ if (internalFKColumns.has(key)) {
575
+ result[key] = value === null ? null : (typeof value === "number" ? value : String(value));
576
+ continue;
577
+ }
578
+
579
+ const property = properties[key as keyof M] as Property;
580
+ if (!property) continue; // Skip DB columns not defined in properties
581
+
582
+ if (options.skipRelations && property.type === "relation") continue;
583
+
584
+ result[key] = parsePropertyFromServer(value, property, collection, key);
585
+ }
586
+
587
+ return result;
588
+ }
589
+
590
+ /**
591
+ * Lightweight value normalization for db.query results.
592
+ * Only handles type coercion (dates, numbers, NaN) and property filtering.
593
+ * Does NOT query the database for relations — those are already resolved
594
+ * by Drizzle's relational query API.
595
+ *
596
+ * Use this instead of `parseDataFromServer` when processing results from
597
+ * `db.query.findFirst/findMany` which return pre-hydrated relation data.
598
+ */
599
+ export function normalizeDbValues<M extends Record<string, unknown>>(
600
+ data: M,
601
+ collection: EntityCollection
602
+ ): M {
603
+ const properties = collection.properties;
604
+ if (!data || !properties) return data;
605
+
606
+ const resolvedRelations = resolveCollectionRelations(collection);
607
+ return normalizeScalarValues(data, properties, collection, resolvedRelations, { skipRelations: true }) as M;
608
+ }