@rebasepro/server-postgres 0.9.1-canary.58368ce → 0.9.1-canary.73476f2

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.
@@ -0,0 +1,10 @@
1
+ import { CollectionConfig, SecurityRule } from "@rebasepro/types";
2
+ /**
3
+ * Returns the security rules that should be applied to a collection: the
4
+ * author's explicit `securityRules` plus the framework defaults described in
5
+ * the module doc (baseline server/admin read for all collections; self-read
6
+ * and the admin write gate for auth collections).
7
+ *
8
+ * Collections that opt out via `disableDefaultPolicies` are returned unchanged.
9
+ */
10
+ export declare function getEffectiveSecurityRules(collection: CollectionConfig): SecurityRule[];
@@ -49,26 +49,13 @@ export declare class FetchService {
49
49
  * For example, for posts_tags junction, this returns "tag_id" (the relation pointing to tags).
50
50
  */
51
51
  private getJunctionTargetRelationName;
52
- /**
53
- * The address a relation ref points at.
54
- *
55
- * The whole key, not its first column: a composite-keyed target addressed
56
- * by `tenant_id` alone points at every row that shares it. And a target
57
- * whose key cannot be resolved at all used to throw here — reading
58
- * `targetPks[0]` of an empty array — taking down the parent's fetch over a
59
- * relation it may not even have asked for; the first column is a guess, but
60
- * a ref that resolves to nothing beats no rows at all.
61
- */
62
- private relationTargetAddress;
63
52
  /**
64
53
  * Convert a db.query result row (with nested relation objects) to a flat row.
65
54
  * Handles:
55
+ * - Placing `id` at the top level as a string
66
56
  * - Type normalization (dates, numbers, NaN) via normalizeDbValues
67
57
  * - Converting nested relation objects to { id, path, __type: "relation" } for CMS
68
58
  * - Flattening junction-table many-to-many results
69
- *
70
- * The row's own address is not among them: it is derived by the consumer
71
- * from the collection's primary keys.
72
59
  */
73
60
  private drizzleResultToRow;
74
61
  /**
@@ -77,19 +64,18 @@ export declare class FetchService {
77
64
  * so they must be loaded separately after the primary query.
78
65
  */
79
66
  private resolveJoinPathRelations;
67
+ /**
68
+ * Post-fetch joinPath relations for a batch of flat rows.
69
+ * Uses batch fetching to avoid N+1 queries for list views.
70
+ */
71
+ private resolveJoinPathRelationsBatch;
80
72
  /**
81
73
  * Resolves joinPath relations for raw REST rows and directly injects them.
82
74
  * Uses RelationService to query the database and maps results back to the flattened objects.
83
75
  */
84
76
  private resolveJoinPathRelationsBatchRest;
85
77
  /**
86
- * Convert a db.query result row to a flat REST-style row with populated relations.
87
- *
88
- * Every column is copied through under its own name, with the value Postgres
89
- * returned. This used to open with a synthesized `id` and then skip the key
90
- * column, which renamed it (a `sku` primary key was served as `id`, and `sku`
91
- * did not appear at all) and restringified it (`42` → `"42"`). Consumers that
92
- * need an address derive it from the collection's primary keys.
78
+ * Convert a db.query result row to a flat REST-style object with populated relations.
93
79
  */
94
80
  private drizzleResultToRestRow;
95
81
  /**
@@ -12,24 +12,6 @@ export declare class PersistService {
12
12
  private relationService;
13
13
  private fetchService;
14
14
  constructor(db: DrizzleClient, registry: PostgresCollectionRegistry);
15
- /**
16
- * Explain a write that matched no rows.
17
- *
18
- * Row-level security filters UPDATE and DELETE through the policy's USING
19
- * clause instead of raising: a denied write is reported by Postgres exactly
20
- * like a successful one that happened to match nothing. Left unchecked, a
21
- * caller cannot tell "denied" from "done" — the write returns 200/204 and
22
- * the row is untouched.
23
- *
24
- * Re-reading the target over the *same* RLS-scoped handle separates the two
25
- * cases. A visible row means the policy rejected the write (403); an
26
- * invisible one means there is nothing there to write for this caller (404,
27
- * matching what a GET would say). The re-read is bound by the caller's own
28
- * policies, so it discloses nothing a plain read wouldn't.
29
- *
30
- * Only reached when zero rows matched, so the happy path pays nothing.
31
- */
32
- private explainZeroRowWrite;
33
15
  /**
34
16
  * Delete an row by ID
35
17
  */
@@ -40,16 +22,8 @@ export declare class PersistService {
40
22
  deleteAll(collectionPath: string, _databaseId?: string): Promise<void>;
41
23
  /**
42
24
  * Save an row (create or update)
43
- *
44
- * With `options.upsert`, the row is written with INSERT ... ON CONFLICT DO
45
- * UPDATE against the primary key rather than a plain UPDATE. That is one
46
- * statement, so it cannot lose a race the way a read-then-write can, and it
47
- * does not care whether the row already exists — which is what a re-runnable
48
- * import needs.
49
25
  */
50
- save<M extends Record<string, unknown>>(collectionPath: string, values: Partial<M>, id?: string | number, databaseId?: string, options?: {
51
- upsert?: boolean;
52
- }): Promise<Record<string, unknown>>;
26
+ save<M extends Record<string, unknown>>(collectionPath: string, values: Partial<M>, id?: string | number, databaseId?: string): Promise<Record<string, unknown>>;
53
27
  /**
54
28
  * Get the RelationService instance for external use
55
29
  */
@@ -72,6 +72,7 @@ export declare class RelationService {
72
72
  relationKey: string;
73
73
  relation: Relation;
74
74
  newValue: unknown;
75
+ currentId?: string | number;
75
76
  }>): Promise<void>;
76
77
  /**
77
78
  * Handle inverse relations with joinPath
@@ -1,9 +1,6 @@
1
1
  import { PgTable, AnyPgColumn } from "drizzle-orm/pg-core";
2
2
  import { CollectionConfig } from "@rebasepro/types";
3
3
  import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry";
4
- export { buildCompositeId, parseIdValues, COMPOSITE_ID_SEPARATOR } from "@rebasepro/common";
5
- export type { PrimaryKeyInfo } from "@rebasepro/common";
6
- import type { PrimaryKeyInfo } from "@rebasepro/common";
7
4
  /**
8
5
  * Shared helper functions for row operations.
9
6
  * These are used by FetchService, PersistService, and RelationService.
@@ -24,53 +21,18 @@ export interface DrizzleColumnMeta {
24
21
  export declare function getColumnMeta(col: AnyPgColumn): DrizzleColumnMeta;
25
22
  export declare function getCollectionByPath(collectionPath: string, registry: PostgresCollectionRegistry): CollectionConfig;
26
23
  export declare function getTableForCollection(collection: CollectionConfig, registry: PostgresCollectionRegistry): PgTable<any>;
27
- export declare function getPrimaryKeys(collection: CollectionConfig, registry: PostgresCollectionRegistry): PrimaryKeyInfo[];
28
- /**
29
- * Collections whose key the *browser* cannot resolve, and what it will do
30
- * instead.
31
- *
32
- * The two sides resolve keys from different evidence. This driver reads, in
33
- * order: properties marked `isId`, the primary keys of the Drizzle schema, then
34
- * a column literally named `id`. The admin shares the `CollectionConfig` — it
35
- * compiles the same collection files into its bundle — but never the Drizzle
36
- * schema, so the middle tier is invisible to it.
37
- *
38
- * Nothing can normalize this at runtime: the server does not serve the admin
39
- * its collections, so a key resolved here cannot be handed over there. The
40
- * config files are the only thing both sides read, so the fix is an edit to
41
- * them, and the most this can do is say exactly which edit.
42
- *
43
- * Two shapes, and the second is the dangerous one:
44
- *
45
- * - No `isId`, no `id` property → the admin resolves no address, warns in the
46
- * console, and rows cannot be opened or linked.
47
- * - No `isId`, but an `id` property that is *not* the key → the admin addresses
48
- * rows by `id` while this driver reads the address as the real key. Nothing
49
- * errors: the addresses look right and route wrong.
50
- */
51
- export declare function findUnresolvableKeyCollections(collections: CollectionConfig[], registry: PostgresCollectionRegistry): {
52
- collection: CollectionConfig;
53
- keys: PrimaryKeyInfo[];
54
- shadowedByIdProperty: boolean;
24
+ export declare function getPrimaryKeys(collection: CollectionConfig, registry: PostgresCollectionRegistry): {
25
+ fieldName: string;
26
+ type: "string" | "number";
27
+ isUUID?: boolean;
55
28
  }[];
56
- /**
57
- * Report the collections from {@link findUnresolvableKeyCollections} at boot,
58
- * with the edit that fixes each one.
59
- *
60
- * Grouped by failure, not by collection: the shadowed case is a routing bug and
61
- * the silent case is a missing feature, and they deserve different urgency.
62
- */
63
- export declare function warnOnKeysTheAdminCannotResolve(collections: CollectionConfig[], registry: PostgresCollectionRegistry): void;
64
- /**
65
- * The address of a row: derived from the collection's primary keys, because a
66
- * row does not carry one — it is exactly its columns.
67
- *
68
- * Falls back to a literal `id` column, which covers the two cases where the
69
- * keys cannot be resolved: a row that reached us from somewhere other than the
70
- * postgres driver, and a collection whose table the registry cannot look up
71
- * (`getPrimaryKeys` throws for an unregistered table rather than returning
72
- * nothing). Returns `""` when there is no key and no `id` — callers decide what
73
- * that means, since "unaddressable" is a different answer in a notification
74
- * (broadcast a wildcard) than in a save (fail).
75
- */
76
- export declare function deriveRowAddress(row: Record<string, unknown>, collection: CollectionConfig, registry: PostgresCollectionRegistry): string;
29
+ export declare function parseIdValues(idValue: string | number, primaryKeys: {
30
+ fieldName: string;
31
+ type: "string" | "number";
32
+ isUUID?: boolean;
33
+ }[]): Record<string, string | number>;
34
+ export declare function buildCompositeId(values: Record<string, unknown>, primaryKeys: {
35
+ fieldName: string;
36
+ type: "string" | "number";
37
+ isUUID?: boolean;
38
+ }[]): string;
@@ -82,9 +82,7 @@ export declare class DataService implements DataRepository {
82
82
  /**
83
83
  * Save an row (create or update)
84
84
  */
85
- save<M extends Record<string, unknown>>(collectionPath: string, values: Partial<M>, id?: string | number, databaseId?: string, options?: {
86
- upsert?: boolean;
87
- }): Promise<Record<string, unknown>>;
85
+ save<M extends Record<string, unknown>>(collectionPath: string, values: Partial<M>, id?: string | number, databaseId?: string): Promise<Record<string, unknown>>;
88
86
  /**
89
87
  * Delete an row by ID
90
88
  */
@@ -1,4 +1,4 @@
1
1
  export { FetchService } from "./FetchService";
2
2
  export { PersistService } from "./PersistService";
3
3
  export { RelationService } from "./RelationService";
4
- export { getCollectionByPath, getTableForCollection, getPrimaryKeys, deriveRowAddress, parseIdValues, buildCompositeId } from "./collection-helpers";
4
+ export { getCollectionByPath, getTableForCollection, getPrimaryKeys, parseIdValues, buildCompositeId } from "./collection-helpers";
@@ -177,15 +177,8 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
177
177
  /**
178
178
  * Send a lightweight row-level patch to a collection subscriber.
179
179
  * The client can merge this into its cached data for instant feedback.
180
- *
181
- * The key columns ride along: the patch names a row by address, and the
182
- * client has to find that row among the ones it cached — which carry
183
- * columns and no address. The SDK holds no collection config to derive one
184
- * from, so this is the only place the mapping can come from.
185
180
  */
186
181
  private sendCollectionPatch;
187
- /** The key columns of the collection at `path`, if they can be resolved. */
188
- private primaryKeysForPath;
189
182
  private sendError;
190
183
  private sendMessage;
191
184
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rebasepro/server-postgres",
3
3
  "type": "module",
4
- "version": "0.9.1-canary.58368ce",
4
+ "version": "0.9.1-canary.73476f2",
5
5
  "description": "PostgreSQL data source backend implementation for Rebase with Drizzle ORM",
6
6
  "funding": {
7
7
  "url": "https://github.com/sponsors/rebaseco"
@@ -69,13 +69,14 @@
69
69
  "dotenv": "^17.4.2",
70
70
  "drizzle-orm": "^0.45.2",
71
71
  "execa": "^9.6.1",
72
+ "hono": "^4.12.25",
72
73
  "pg": "^8.21.0",
73
74
  "ws": "^8.21.0",
74
- "@rebasepro/common": "0.9.1-canary.58368ce",
75
- "@rebasepro/server": "0.9.1-canary.58368ce",
76
- "@rebasepro/types": "0.9.1-canary.58368ce",
77
- "@rebasepro/codegen": "0.9.1-canary.58368ce",
78
- "@rebasepro/utils": "0.9.1-canary.58368ce"
75
+ "@rebasepro/common": "0.9.1-canary.73476f2",
76
+ "@rebasepro/codegen": "0.9.1-canary.73476f2",
77
+ "@rebasepro/types": "0.9.1-canary.73476f2",
78
+ "@rebasepro/server": "0.9.1-canary.73476f2",
79
+ "@rebasepro/utils": "0.9.1-canary.73476f2"
79
80
  },
80
81
  "devDependencies": {
81
82
  "@types/jest": "^30.0.0",
@@ -99,7 +100,7 @@
99
100
  ],
100
101
  "scripts": {
101
102
  "watch": "vite build --watch",
102
- "build": "vite build && tsc --emitDeclarationOnly -p tsconfig.prod.json && node ../../scripts/assert-build-output.mjs",
103
+ "build": "vite build && tsc --emitDeclarationOnly -p tsconfig.prod.json",
103
104
  "test:lint": "eslint \"src/**\" --quiet",
104
105
  "test": "jest --passWithNoTests",
105
106
  "test:e2e": "vitest run --config vitest.e2e.config.ts",
@@ -17,7 +17,6 @@ import {
17
17
  RebaseData,
18
18
  RebaseSdkData,
19
19
  RestFetchService,
20
- SaveManyProps,
21
20
  SaveProps,
22
21
  TableColumnInfo,
23
22
  TableForeignKeyInfo,
@@ -29,7 +28,6 @@ import {
29
28
  import { sql as drizzleSql } from "drizzle-orm";
30
29
  import { buildPropertyCallbacks, buildSdkData, resolveCollectionRelations, updateDateAutoValues } from "@rebasepro/common";
31
30
  import { PostgresCollectionRegistry } from "./collections/PostgresCollectionRegistry";
32
- import { deriveRowAddress } from "./services/collection-helpers";
33
31
  import { HistoryService } from "./history/HistoryService";
34
32
  import { mergeDeep } from "@rebasepro/utils";
35
33
  import { logger } from "@rebasepro/server";
@@ -534,8 +532,7 @@ export class PostgresBackendDriver implements DataDriver {
534
532
  id,
535
533
  values,
536
534
  collection,
537
- status,
538
- upsert
535
+ status
539
536
  }: SaveProps<M>): Promise<Record<string, unknown>> {
540
537
 
541
538
  const {
@@ -619,8 +616,7 @@ export class PostgresBackendDriver implements DataDriver {
619
616
  path,
620
617
  updatedValues,
621
618
  id,
622
- resolvedCollection?.databaseId,
623
- { upsert }
619
+ resolvedCollection?.databaseId
624
620
  );
625
621
 
626
622
  if (savedRow && (globalCallbacks?.afterRead || callbacks?.afterRead || propertyCallbacks?.afterRead)) {
@@ -653,19 +649,8 @@ export class PostgresBackendDriver implements DataDriver {
653
649
  }
654
650
  }
655
651
 
656
- // The row is exactly its columns, so its address is derived, not read
657
- // off it: `savedRow.id` is undefined for every table whose key is not
658
- // literally named `id`, and is ordinary data for a table that has such
659
- // a column without it being the key.
660
- const savedId = deriveRowAddress(
661
- savedRow,
662
- (resolvedCollection ?? collection) as CollectionConfig,
663
- this.registry
664
- );
665
- // `values` are the row's columns — all of them. For an `id`-keyed table
666
- // that includes `id`, which used to be stripped here because it was the
667
- // synthesized address rather than the column it now is.
668
- const savedValues = savedRow;
652
+ const savedId = savedRow.id as string | number;
653
+ const { id: _savedId, ...savedValues } = savedRow;
669
654
 
670
655
  if (globalCallbacks?.afterSave || callbacks?.afterSave || propertyCallbacks?.afterSave) {
671
656
  // 1. Global callbacks first
@@ -710,7 +695,7 @@ export class PostgresBackendDriver implements DataDriver {
710
695
  if (this.historyService && resolvedCollection?.history) {
711
696
  this.historyService.recordHistory({
712
697
  tableName: path,
713
- id: savedId,
698
+ id: savedId.toString(),
714
699
  action: status === "new" ? "create" : "update",
715
700
  values: savedValues as Record<string, unknown>,
716
701
  previousValues: previousValuesForHistory as Record<string, unknown> | undefined,
@@ -722,14 +707,14 @@ export class PostgresBackendDriver implements DataDriver {
722
707
  if (this._deferNotifications) {
723
708
  this._pendingNotifications.push({
724
709
  path,
725
- id: savedId,
710
+ id: savedId.toString(),
726
711
  row: savedRow,
727
712
  databaseId: resolvedCollection?.databaseId
728
713
  });
729
714
  } else {
730
715
  await this.realtimeService.notifyUpdate(
731
716
  path,
732
- savedId,
717
+ savedId.toString(),
733
718
  savedRow,
734
719
  resolvedCollection?.databaseId
735
720
  );
@@ -779,86 +764,13 @@ export class PostgresBackendDriver implements DataDriver {
779
764
  }
780
765
  }
781
766
 
782
- /**
783
- * Write many rows through the same pipeline as {@link save}.
784
- *
785
- * The batch runs in one transaction of its own, so a failure part-way leaves
786
- * nothing behind — the point of a batch is that a re-run starts from a known
787
- * state. When this driver is already inside a transaction (the authenticated
788
- * path, via `withTransaction`) the nested call becomes a savepoint, which is
789
- * still atomic and still commits once.
790
- *
791
- * Rows are applied in order, so a batch that touches the same key twice ends
792
- * with the last write winning, exactly as separate calls would.
793
- */
794
- async saveMany<M extends Record<string, unknown>>({
795
- path,
796
- rows,
797
- collection,
798
- upsert
799
- }: SaveManyProps<M>): Promise<Record<string, unknown>[]> {
800
- return this.db.transaction(async (tx) => {
801
- // Bind the whole batch to the transaction handle. Without this the
802
- // rows would be written through `this.db` and survive a rollback.
803
- const txDriver = new PostgresBackendDriver(
804
- tx, this.realtimeService, this.registry, this.user, this.poolManager, this.historyService
805
- );
806
- txDriver.dataService = new DataService(tx, this.registry);
807
- txDriver.client = this.client;
808
- // Carry the caller's notification batching through, so a bulk write
809
- // nested in an outer transaction still holds its events until commit.
810
- txDriver._deferNotifications = this._deferNotifications;
811
- txDriver._pendingNotifications = this._pendingNotifications;
812
-
813
- const saved: Record<string, unknown>[] = [];
814
-
815
- for (let i = 0; i < rows.length; i++) {
816
- const values = rows[i];
817
- const id = (values as Record<string, unknown>)?.id as string | number | undefined;
818
- try {
819
- saved.push(await txDriver.save<M>({
820
- path,
821
- values,
822
- // No `id` argument, deliberately: passing one selects the
823
- // UPDATE path, and an import's rows usually carry a natural
824
- // key for a row that does not exist yet — which would 404 on
825
- // every one. Leaving the key inside `values` is what
826
- // single-row `create(data, id)` does, and it inserts.
827
- // Callers who want existing rows overwritten pass `upsert`.
828
- collection,
829
- status: "new",
830
- upsert
831
- }));
832
- } catch (error) {
833
- // One bad row in ten thousand is impossible to find from a
834
- // message that only says the batch failed. Say which row, and
835
- // keep the original error as the cause so its status survives.
836
- const label = id !== undefined ? `id ${JSON.stringify(id)}` : "no id";
837
- throw Object.assign(
838
- new Error(`Row ${i} of ${rows.length} (${label}) failed: ${(error as Error)?.message ?? error}`, { cause: error }),
839
- {
840
- statusCode: (error as { statusCode?: number })?.statusCode,
841
- code: (error as { code?: string })?.code,
842
- name: (error as Error)?.name
843
- }
844
- );
845
- }
846
- }
847
-
848
- return saved;
849
- });
850
- }
851
-
852
767
  async delete<M extends Record<string, unknown>>({
853
768
  row,
854
769
  collection
855
770
  }: DeleteProps<M>): Promise<void> {
856
771
 
857
772
  const targetPath = row.path;
858
- // The callbacks' `row` is the row: its columns, nothing else. The address
859
- // travels beside it as `id`, so merging it in here only ever invented an
860
- // `id` field for tables that have no such column.
861
- const targetRow: Record<string, unknown> = { ...(row.values ?? {}) };
773
+ const targetRow: Record<string, unknown> = { id: row.id, ...(row.values ?? {}) };
862
774
 
863
775
  // Resolve from backend registry to restore callbacks lost during WebSocket serialization
864
776
  const {
@@ -1450,19 +1362,6 @@ export class AuthenticatedPostgresBackendDriver implements DataDriver {
1450
1362
  return this.withTransaction((delegate) => delegate.save(props));
1451
1363
  }
1452
1364
 
1453
- /**
1454
- * One transaction for the whole batch, rather than one per row.
1455
- *
1456
- * This is the point of the method: `save` opens a transaction per call, so
1457
- * importing 10k rows through it means 10k transactions (and, over HTTP, 10k
1458
- * round trips). Here the RLS context is established once and every row lands
1459
- * or none does. Realtime notifications are already deferred to commit by
1460
- * `withTransaction`, so a batch does not flood subscribers mid-flight.
1461
- */
1462
- async saveMany<M extends Record<string, unknown>>(props: SaveManyProps<M>): Promise<Record<string, unknown>[]> {
1463
- return this.withTransaction((delegate) => delegate.saveMany(props));
1464
- }
1465
-
1466
1365
  async delete<M extends Record<string, unknown>>(props: DeleteProps<M>): Promise<void> {
1467
1366
  return this.withTransaction((delegate) => delegate.delete(props));
1468
1367
  }
@@ -4,7 +4,7 @@
4
4
  * Implements the `BackendBootstrapper` interface for PostgreSQL.
5
5
  */
6
6
 
7
- import { Relations, sql } from "drizzle-orm";
7
+ import { getTableName, isTable, Relations, sql } from "drizzle-orm";
8
8
  import { NodePgDatabase } from "drizzle-orm/node-postgres";
9
9
  import { PgEnum, PgTable } from "drizzle-orm/pg-core";
10
10
  import type { RebasePgTable } from "./types";
@@ -21,7 +21,6 @@ import {
21
21
  } from "@rebasepro/types";
22
22
  import { PostgresBackendDriver } from "./PostgresBackendDriver";
23
23
  import { RealtimeService } from "./services/realtimeService";
24
- import { buildCollectionRegistry } from "./collections/buildRegistry";
25
24
  import { DatabasePoolManager } from "./databasePoolManager";
26
25
  import { PostgresCollectionRegistry } from "./collections/PostgresCollectionRegistry";
27
26
  import { createEmailService, type EmailConfig, type EmailService, logger } from "@rebasepro/server";
@@ -162,17 +161,30 @@ export function createPostgresBootstrapper(pgConfig: PostgresDriverConfig): Back
162
161
  }
163
162
 
164
163
  const activeCollections = introspectedCollections ?? collections;
164
+
165
+ // Create a fresh registry for this driver
166
+ const registry = new PostgresCollectionRegistry();
167
+ if (activeCollections) {
168
+ registry.registerMultiple(activeCollections);
169
+ logger.info(`📋 [PostgresRegistry] Registered ${registry.getCollections().length} collections: [${registry.getCollections().map(c => c.slug).join(", ")}]`);
170
+ }
171
+
165
172
  const schemaTables = introspectedTables ?? pgConfig.schema?.tables;
166
- const schemaRelations = introspectedRelations ?? (pgConfig.schema?.relations as Record<string, Relations> | undefined);
167
173
 
168
- // Create a fresh registry for this driver. Registration order is
169
- // load-bearing, so it lives in one place — see `buildCollectionRegistry`.
170
- const registry = buildCollectionRegistry({
171
- collections: activeCollections,
172
- tables: schemaTables,
173
- enums: pgConfig.schema?.enums as Record<string, PgEnum<[string, ...string[]]>> | undefined,
174
- relations: schemaRelations
175
- });
174
+ // Register tables
175
+ if (schemaTables) {
176
+ Object.values(schemaTables).forEach((table) => {
177
+ if (isTable(table)) {
178
+ const tableName = getTableName(table);
179
+ registry.registerTable(table as PgTable, tableName);
180
+ }
181
+ });
182
+ }
183
+
184
+ if (pgConfig.schema?.enums) registry.registerEnums(pgConfig.schema.enums as Record<string, PgEnum<[string, ...string[]]>>);
185
+
186
+ const schemaRelations = introspectedRelations ?? (pgConfig.schema?.relations as Record<string, Relations> | undefined);
187
+ if (schemaRelations) registry.registerRelations(schemaRelations);
176
188
 
177
189
  // Patch Drizzle's PgArray columns to handle NULL values safely.
178
190
  // Drizzle's mapFromDriverValue crashes with "value.map is not a function"
@@ -20,19 +20,12 @@ import { logger } from "@rebasepro/server";
20
20
  export interface SerializedEntityData {
21
21
  /** Scalar column values ready for INSERT/UPDATE. */
22
22
  scalarData: Record<string, unknown>;
23
- /**
24
- * Inverse relation updates that must be applied to target tables.
25
- *
26
- * No address here: the row being written does not know its own. These are
27
- * applied by `PersistService`, which addresses them with the id it holds —
28
- * the one it was given for an update, or the one the INSERT returned — and
29
- * that is the authority. This item used to carry a `currentId` derived from
30
- * the *input* values, which nothing ever read.
31
- */
23
+ /** Inverse relation updates that must be applied to target tables. */
32
24
  inverseRelationUpdates: Array<{
33
25
  relationKey: string;
34
26
  relation: Relation;
35
27
  newValue: unknown;
28
+ currentId?: string | number;
36
29
  }>;
37
30
  /** JoinPath relation updates that require multi-hop writes. */
38
31
  joinPathRelationUpdates: Array<{
@@ -112,6 +105,7 @@ joinPathRelationUpdates: [] };
112
105
  relationKey: string;
113
106
  relation: Relation;
114
107
  newValue: unknown;
108
+ currentId?: string | number;
115
109
  }> = [];
116
110
  const joinPathRelationUpdates: Array<{
117
111
  relationKey: string;
@@ -152,10 +146,12 @@ joinPathRelationUpdates: [] };
152
146
  } else if (relation.direction === "inverse" && relation.foreignKeyOnTarget) {
153
147
  // Inverse relation: Need to update the target table's FK
154
148
  const serializedValue = serializePropertyToServer(effectiveValue, property);
149
+ const pks = getPrimaryKeys(collection, registry!);
155
150
  inverseRelationUpdates.push({
156
151
  relationKey: key,
157
152
  relation,
158
- newValue: serializedValue
153
+ newValue: serializedValue,
154
+ currentId: (row.id as string | number | undefined) || buildCompositeId(row, pks)
159
155
  });
160
156
  // Don't add the original relation property to the result
161
157
  continue;
@@ -174,10 +170,12 @@ joinPathRelationUpdates: [] };
174
170
  });
175
171
  } else {
176
172
  // Many inverse joinPath: capture as inverse relation update
173
+ const pks = getPrimaryKeys(collection, registry!);
177
174
  inverseRelationUpdates.push({
178
175
  relationKey: key,
179
176
  relation,
180
- newValue: serializedValue
177
+ newValue: serializedValue,
178
+ currentId: (row.id as string | number | undefined) || buildCompositeId(row, pks)
181
179
  });
182
180
  }
183
181
  // Don't add the original relation property to the result