@rebasepro/server-postgres 0.9.1-canary.73476f2 → 0.9.1-canary.7ba0e49

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 (43) hide show
  1. package/dist/PostgresBackendDriver.d.ts +25 -2
  2. package/dist/PostgresBootstrapper.d.ts +10 -0
  3. package/dist/auth/services.d.ts +16 -0
  4. package/dist/collections/buildRegistry.d.ts +27 -0
  5. package/dist/connection.d.ts +21 -0
  6. package/dist/data-transformer.d.ts +9 -2
  7. package/dist/index.es.js +1445 -571
  8. package/dist/index.es.js.map +1 -1
  9. package/dist/schema/doctor.d.ts +1 -1
  10. package/dist/services/FetchService.d.ts +4 -24
  11. package/dist/services/PersistService.d.ts +27 -1
  12. package/dist/services/RelationService.d.ts +34 -1
  13. package/dist/services/collection-helpers.d.ts +79 -14
  14. package/dist/services/dataService.d.ts +3 -1
  15. package/dist/services/index.d.ts +1 -1
  16. package/dist/services/realtimeService.d.ts +7 -0
  17. package/dist/services/row-pipeline.d.ts +63 -0
  18. package/package.json +11 -9
  19. package/src/PostgresBackendDriver.ts +127 -13
  20. package/src/PostgresBootstrapper.ts +62 -25
  21. package/src/auth/ensure-tables.ts +73 -11
  22. package/src/auth/services.ts +49 -19
  23. package/src/collections/buildRegistry.ts +59 -0
  24. package/src/connection.ts +61 -1
  25. package/src/data-transformer.ts +11 -9
  26. package/src/databasePoolManager.ts +2 -0
  27. package/src/schema/doctor.ts +45 -20
  28. package/src/schema/generate-drizzle-schema-logic.ts +24 -29
  29. package/src/schema/generate-postgres-ddl-logic.ts +76 -28
  30. package/src/schema/introspect-db.ts +19 -2
  31. package/src/services/BranchService.ts +42 -10
  32. package/src/services/FetchService.ts +65 -270
  33. package/src/services/PersistService.ts +130 -14
  34. package/src/services/RelationService.ts +153 -94
  35. package/src/services/collection-helpers.ts +164 -47
  36. package/src/services/dataService.ts +3 -2
  37. package/src/services/index.ts +1 -0
  38. package/src/services/realtimeService.ts +40 -19
  39. package/src/services/row-pipeline.ts +215 -0
  40. package/src/utils/drizzle-conditions.ts +13 -0
  41. package/src/websocket.ts +4 -1
  42. package/dist/schema/auth-default-policies.d.ts +0 -10
  43. package/src/schema/auth-default-policies.ts +0 -132
@@ -2,7 +2,7 @@ import { CollectionConfig, Property } from "@rebasepro/types";
2
2
  export type IssueSeverity = "error" | "warning" | "info";
3
3
  export interface DoctorIssue {
4
4
  severity: IssueSeverity;
5
- category: "missing_table" | "missing_column" | "type_mismatch" | "missing_constraint" | "schema_stale" | "missing_enum" | "enum_value_mismatch" | "missing_foreign_key" | "sdk_stale";
5
+ category: "missing_table" | "missing_column" | "type_mismatch" | "missing_constraint" | "schema_stale" | "missing_enum" | "enum_value_mismatch" | "missing_foreign_key" | "sdk_stale" | "sdk_not_generated";
6
6
  table?: string;
7
7
  column?: string;
8
8
  expected?: string;
@@ -40,44 +40,22 @@ export declare class FetchService {
40
40
  * the target relation so actual row data is returned.
41
41
  */
42
42
  private buildWithConfig;
43
- /**
44
- * Detect if a many-to-many relation uses a junction table in the Drizzle schema.
45
- */
46
- private isJunctionRelation;
47
43
  /**
48
44
  * Get the Drizzle relation name on the junction table that points to the actual target row.
49
45
  * For example, for posts_tags junction, this returns "tag_id" (the relation pointing to tags).
50
46
  */
51
47
  private getJunctionTargetRelationName;
52
- /**
53
- * Convert a db.query result row (with nested relation objects) to a flat row.
54
- * Handles:
55
- * - Placing `id` at the top level as a string
56
- * - Type normalization (dates, numbers, NaN) via normalizeDbValues
57
- * - Converting nested relation objects to { id, path, __type: "relation" } for CMS
58
- * - Flattening junction-table many-to-many results
59
- */
60
- private drizzleResultToRow;
61
48
  /**
62
49
  * Post-fetch joinPath relations for a single flat row.
63
50
  * joinPath relations cannot be expressed via Drizzle's `with` config,
64
51
  * so they must be loaded separately after the primary query.
65
52
  */
66
53
  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;
72
54
  /**
73
55
  * Resolves joinPath relations for raw REST rows and directly injects them.
74
56
  * Uses RelationService to query the database and maps results back to the flattened objects.
75
57
  */
76
58
  private resolveJoinPathRelationsBatchRest;
77
- /**
78
- * Convert a db.query result row to a flat REST-style object with populated relations.
79
- */
80
- private drizzleResultToRestRow;
81
59
  /**
82
60
  * Build db.query-compatible options from standard fetch options.
83
61
  * Handles filter, search, orderBy, limit, and cursor-based pagination.
@@ -108,8 +86,10 @@ export declare class FetchService {
108
86
  }): Promise<Record<string, unknown>[]>;
109
87
  /**
110
88
  * Fallback path used when db.query is unavailable.
111
- * The primary path uses drizzleResultToRow which handles relation
112
- * mapping without N+1 queries.
89
+ *
90
+ * The primary path runs the results through `toCmsRow`, which maps
91
+ * relations from what drizzle already nested — no query per row. This one
92
+ * has no nesting to read, so it resolves relations itself, in batches.
113
93
  *
114
94
  * Process raw database results into flat rows with relations.
115
95
  */
@@ -12,6 +12,24 @@ 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;
15
33
  /**
16
34
  * Delete an row by ID
17
35
  */
@@ -22,8 +40,16 @@ export declare class PersistService {
22
40
  deleteAll(collectionPath: string, _databaseId?: string): Promise<void>;
23
41
  /**
24
42
  * 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.
25
49
  */
26
- save<M extends Record<string, unknown>>(collectionPath: string, values: Partial<M>, id?: string | number, databaseId?: string): Promise<Record<string, unknown>>;
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>>;
27
53
  /**
28
54
  * Get the RelationService instance for external use
29
55
  */
@@ -20,6 +20,40 @@ export declare class RelationService {
20
20
  private db;
21
21
  private registry;
22
22
  constructor(db: DrizzleClient, registry: PostgresCollectionRegistry);
23
+ /**
24
+ * One target row, as the {@link RelatedRow} everything here returns.
25
+ *
26
+ * Eight sites built this by hand, which is how the address came to be the
27
+ * target's first key column in all eight — one edit, eight places to miss.
28
+ *
29
+ * `resolveNested` is the one thing they did not agree on, and the
30
+ * disagreement was invisible: the single-parent fetches pass `db` and
31
+ * `registry` to `parseDataFromServer`, so the target's *own* relations get
32
+ * resolved too, while the batch paths deliberately do not — a query per
33
+ * target row is the N+1 the batching exists to avoid. Naming the parameter
34
+ * makes that a decision rather than a difference between two call sites
35
+ * nobody was comparing.
36
+ */
37
+ private toRelatedRow;
38
+ /**
39
+ * A WHERE matching any of `parentIds`, by the whole key.
40
+ *
41
+ * A single key is an `IN (…)`. A composite one cannot be: matching
42
+ * `tenant_id IN (1, 1)` collects every row of tenant 1, so two parents that
43
+ * share their first column each receive the other's relations. It becomes
44
+ * an OR of ANDs — one exact address per parent — which Postgres indexes the
45
+ * same way it would a multi-column key lookup.
46
+ */
47
+ private parentKeyCondition;
48
+ /**
49
+ * Reject a relation that cannot express a composite-keyed parent.
50
+ *
51
+ * `localKey` and `foreignKeyOnTarget` are single column names: one column
52
+ * cannot reference a two-column key, so such a relation has no correct
53
+ * reading. Left alone it would silently match on the first key column and
54
+ * hand a tenant's rows to its neighbour — say so instead.
55
+ */
56
+ private assertSingleKeyAddressable;
23
57
  /**
24
58
  * Fetch rows related to a parent row through a specific relation
25
59
  */
@@ -72,7 +106,6 @@ export declare class RelationService {
72
106
  relationKey: string;
73
107
  relation: Relation;
74
108
  newValue: unknown;
75
- currentId?: string | number;
76
109
  }>): Promise<void>;
77
110
  /**
78
111
  * Handle inverse relations with joinPath
@@ -1,6 +1,9 @@
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";
4
7
  /**
5
8
  * Shared helper functions for row operations.
6
9
  * These are used by FetchService, PersistService, and RelationService.
@@ -21,18 +24,80 @@ export interface DrizzleColumnMeta {
21
24
  export declare function getColumnMeta(col: AnyPgColumn): DrizzleColumnMeta;
22
25
  export declare function getCollectionByPath(collectionPath: string, registry: PostgresCollectionRegistry): CollectionConfig;
23
26
  export declare function getTableForCollection(collection: CollectionConfig, registry: PostgresCollectionRegistry): PgTable<any>;
24
- export declare function getPrimaryKeys(collection: CollectionConfig, registry: PostgresCollectionRegistry): {
25
- fieldName: string;
26
- type: "string" | "number";
27
- isUUID?: boolean;
27
+ /**
28
+ * The key columns a collection's rows are addressed by.
29
+ *
30
+ * Three tiers, in order: properties marked `isId`, the primary keys of the
31
+ * drizzle schema, and finally a column literally named `id`. Only the first is
32
+ * visible to the browser, which is why a key known only to drizzle is reported
33
+ * at boot — see {@link warnOnKeysTheAdminCannotResolve}.
34
+ *
35
+ * Returns `[]` when nothing resolves, rather than throwing. It used to open by
36
+ * resolving the table, which throws when there is none — so the `isId` tier,
37
+ * which needs no table at all, was unreachable for exactly the collections
38
+ * most likely to have no table registered. Every caller that wanted "no keys"
39
+ * to mean "no keys" had to spell that out in a try/catch.
40
+ *
41
+ * Callers that cannot proceed without a key must say so themselves, naming the
42
+ * collection: an empty array here means "this collection has no address", which
43
+ * is a different answer in a notification (broadcast a wildcard) than in a save
44
+ * (fail).
45
+ */
46
+ export declare function getPrimaryKeys(collection: CollectionConfig, registry: PostgresCollectionRegistry): PrimaryKeyInfo[];
47
+ /**
48
+ * The key columns, for callers that cannot do their job without one.
49
+ *
50
+ * {@link getPrimaryKeys} answers "what keys, if any" and returns `[]` for a
51
+ * collection with no address. Most of this driver, though, is building a WHERE
52
+ * clause and has no meaning without a key — for those, an empty array is not an
53
+ * answer, and indexing `[0]` into it produces `Cannot read properties of
54
+ * undefined` three frames from where the real problem is. This says what is
55
+ * wrong and which collection it is wrong about.
56
+ */
57
+ export declare function requirePrimaryKeys(collection: CollectionConfig, registry: PostgresCollectionRegistry): PrimaryKeyInfo[];
58
+ /**
59
+ * Collections whose key the *browser* cannot resolve, and what it will do
60
+ * instead.
61
+ *
62
+ * The two sides resolve keys from different evidence. This driver reads, in
63
+ * order: properties marked `isId`, the primary keys of the Drizzle schema, then
64
+ * a column literally named `id`. The admin shares the `CollectionConfig` — it
65
+ * compiles the same collection files into its bundle — but never the Drizzle
66
+ * schema, so the middle tier is invisible to it.
67
+ *
68
+ * Nothing can normalize this at runtime: the server does not serve the admin
69
+ * its collections, so a key resolved here cannot be handed over there. The
70
+ * config files are the only thing both sides read, so the fix is an edit to
71
+ * them, and the most this can do is say exactly which edit.
72
+ *
73
+ * Two shapes, and the second is the dangerous one:
74
+ *
75
+ * - No `isId`, no `id` property → the admin resolves no address, warns in the
76
+ * console, and rows cannot be opened or linked.
77
+ * - No `isId`, but an `id` property that is *not* the key → the admin addresses
78
+ * rows by `id` while this driver reads the address as the real key. Nothing
79
+ * errors: the addresses look right and route wrong.
80
+ */
81
+ export declare function findUnresolvableKeyCollections(collections: CollectionConfig[], registry: PostgresCollectionRegistry): {
82
+ collection: CollectionConfig;
83
+ keys: PrimaryKeyInfo[];
84
+ shadowedByIdProperty: boolean;
28
85
  }[];
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;
86
+ /**
87
+ * Report the collections from {@link findUnresolvableKeyCollections} at boot,
88
+ * with the edit that fixes each one.
89
+ *
90
+ * Grouped by failure, not by collection: the shadowed case is a routing bug and
91
+ * the silent case is a missing feature, and they deserve different urgency.
92
+ */
93
+ export declare function warnOnKeysTheAdminCannotResolve(collections: CollectionConfig[], registry: PostgresCollectionRegistry): void;
94
+ /**
95
+ * The address of a row: derived from the collection's primary keys, because a
96
+ * row does not carry one — it is exactly its columns.
97
+ *
98
+ * Falls back to a literal `id` column, for a row that reached us from somewhere
99
+ * other than this driver. Returns `""` when there is no key and no `id` —
100
+ * callers decide what that means, since "unaddressable" is a different answer
101
+ * in a notification (broadcast a wildcard) than in a save (fail).
102
+ */
103
+ export declare function deriveRowAddress(row: Record<string, unknown>, collection: CollectionConfig, registry: PostgresCollectionRegistry): string;
@@ -82,7 +82,9 @@ 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): Promise<Record<string, unknown>>;
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>>;
86
88
  /**
87
89
  * Delete an row by ID
88
90
  */
@@ -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, parseIdValues, buildCompositeId } from "./collection-helpers";
4
+ export { getCollectionByPath, getTableForCollection, getPrimaryKeys, deriveRowAddress, parseIdValues, buildCompositeId } from "./collection-helpers";
@@ -177,8 +177,15 @@ 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.
180
185
  */
181
186
  private sendCollectionPatch;
187
+ /** The key columns of the collection at `path`, if they can be resolved. */
188
+ private primaryKeysForPath;
182
189
  private sendError;
183
190
  private sendMessage;
184
191
  /**
@@ -0,0 +1,63 @@
1
+ import { CollectionConfig, Relation } from "@rebasepro/types";
2
+ import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry";
3
+ /**
4
+ * Turning a drizzle result into a row we serve.
5
+ *
6
+ * There are two shapes, and they are the same walk. Both take a row whose
7
+ * relation fields hold nested objects, both unwrap junction rows to reach the
8
+ * target behind them, and both leave every other column alone. They differ only
9
+ * in what they put where the relation was:
10
+ *
11
+ * - `"ref"` — a `{ id, path, __type: "relation" }` reference carrying the
12
+ * target's values. This is what the admin renders.
13
+ * - `"inline"` — the target's own columns, flat. This is what REST serves.
14
+ *
15
+ * They used to be two functions that happened to agree, and the agreement was
16
+ * not enforced by anything: the row-identity bug had to be fixed five times
17
+ * across differently-shaped copies of this walk, and one of the copies was
18
+ * dead code nobody had noticed. Whatever the next cross-cutting change is, it
19
+ * is one edit here.
20
+ */
21
+ export type RelationStyle = "ref" | "inline";
22
+ /**
23
+ * Whether a many-relation reaches its target through a junction table.
24
+ *
25
+ * Also used to build the drizzle `with` config, which is why it is exported:
26
+ * the query has to nest one level deeper for a junction, and the row walk has
27
+ * to unwrap that same level back out.
28
+ */
29
+ export declare function isJunctionRelation(relation: Relation): boolean;
30
+ /**
31
+ * The address a relation ref points at.
32
+ *
33
+ * The whole key, not its first column: a composite-keyed target addressed by
34
+ * `tenant_id` alone points at every row that shares it. A target whose key
35
+ * cannot be resolved at all used to throw here — reading `[0]` of an empty
36
+ * array — taking down the parent's fetch over a relation it may not even have
37
+ * asked for. The first column is a guess, but a ref that resolves to nothing
38
+ * beats no rows at all.
39
+ */
40
+ export declare function relationTargetAddress(targetRow: Record<string, unknown>, targetCollection: CollectionConfig, registry: PostgresCollectionRegistry): string;
41
+ /**
42
+ * The row the admin renders: every column, with relations as references.
43
+ *
44
+ * Values are normalized (dates, numbers, NaN) because the admin's view-model
45
+ * expects real types. The row's own address is *not* among the columns — it is
46
+ * derived by the consumer from the collection's primary keys.
47
+ */
48
+ export declare function toCmsRow(row: Record<string, unknown>, collection: CollectionConfig, registry: PostgresCollectionRegistry): Record<string, unknown>;
49
+ /**
50
+ * The row REST serves: every column under its own name, with the value Postgres
51
+ * returned, and relations inlined as the target's columns.
52
+ *
53
+ * Values are the ones the database returned, except where that contradicts the
54
+ * declared type: a `number` property is served as a number (see
55
+ * {@link coerceDeclaredNumber}). Dates stay as the database returned them —
56
+ * JSON has its own opinions about dates that the admin's view-model does not
57
+ * share.
58
+ *
59
+ * Keyed by the row rather than by the relation list — a REST fetch only loads
60
+ * the relations `include` asked for, so the row is the authority on which are
61
+ * actually there.
62
+ */
63
+ export declare function toRestRow(row: Record<string, unknown>, collection: CollectionConfig, registry: PostgresCollectionRegistry): Record<string, unknown>;
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.73476f2",
4
+ "version": "0.9.1-canary.7ba0e49",
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,21 +69,22 @@
69
69
  "dotenv": "^17.4.2",
70
70
  "drizzle-orm": "^0.45.2",
71
71
  "execa": "^9.6.1",
72
- "hono": "^4.12.25",
73
72
  "pg": "^8.21.0",
74
73
  "ws": "^8.21.0",
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"
74
+ "@rebasepro/codegen": "0.9.1-canary.7ba0e49",
75
+ "@rebasepro/common": "0.9.1-canary.7ba0e49",
76
+ "@rebasepro/server": "0.9.1-canary.7ba0e49",
77
+ "@rebasepro/types": "0.9.1-canary.7ba0e49",
78
+ "@rebasepro/utils": "0.9.1-canary.7ba0e49"
80
79
  },
81
80
  "devDependencies": {
81
+ "@hono/node-server": "^2.0.9",
82
82
  "@types/jest": "^30.0.0",
83
83
  "@types/node": "^25.9.3",
84
84
  "@types/pg": "^8.20.0",
85
85
  "@types/ws": "^8.18.1",
86
86
  "@vitejs/plugin-react": "^6.0.2",
87
+ "hono": "^4.12.25",
87
88
  "jest": "^30.4.2",
88
89
  "ts-jest": "^29.4.11",
89
90
  "typescript": "^6.0.3",
@@ -100,10 +101,11 @@
100
101
  ],
101
102
  "scripts": {
102
103
  "watch": "vite build --watch",
103
- "build": "vite build && tsc --emitDeclarationOnly -p tsconfig.prod.json",
104
+ "build": "vite build && tsc --emitDeclarationOnly -p tsconfig.prod.json && node ../../scripts/assert-build-output.mjs",
104
105
  "test:lint": "eslint \"src/**\" --quiet",
105
106
  "test": "jest --passWithNoTests",
106
107
  "test:e2e": "vitest run --config vitest.e2e.config.ts",
107
- "clean": "rm -rf dist && find ./src -name '*.js' -type f | xargs rm -f"
108
+ "clean": "rm -rf dist && find ./src -name '*.js' -type f | xargs rm -f",
109
+ "smoke:baas": "tsx scripts/smoke-baas.ts"
108
110
  }
109
111
  }
@@ -17,6 +17,7 @@ import {
17
17
  RebaseData,
18
18
  RebaseSdkData,
19
19
  RestFetchService,
20
+ SaveManyProps,
20
21
  SaveProps,
21
22
  TableColumnInfo,
22
23
  TableForeignKeyInfo,
@@ -28,6 +29,7 @@ import {
28
29
  import { sql as drizzleSql } from "drizzle-orm";
29
30
  import { buildPropertyCallbacks, buildSdkData, resolveCollectionRelations, updateDateAutoValues } from "@rebasepro/common";
30
31
  import { PostgresCollectionRegistry } from "./collections/PostgresCollectionRegistry";
32
+ import { deriveRowAddress } from "./services/collection-helpers";
31
33
  import { HistoryService } from "./history/HistoryService";
32
34
  import { mergeDeep } from "@rebasepro/utils";
33
35
  import { logger } from "@rebasepro/server";
@@ -532,7 +534,8 @@ export class PostgresBackendDriver implements DataDriver {
532
534
  id,
533
535
  values,
534
536
  collection,
535
- status
537
+ status,
538
+ upsert
536
539
  }: SaveProps<M>): Promise<Record<string, unknown>> {
537
540
 
538
541
  const {
@@ -545,13 +548,26 @@ export class PostgresBackendDriver implements DataDriver {
545
548
  let updatedValues = values;
546
549
  const contextForCallback = this.buildCallContext();
547
550
 
548
- // Fetch previous values for callbacks AND history recording
551
+ // Fetch previous values for callbacks AND history recording. Same walk
552
+ // as the saved row the callbacks receive (`fetchOneForRest`), so
553
+ // `values` and `previousValues` compare like with like — a Date on one
554
+ // side and its ISO string on the other reads as a change that never
555
+ // happened.
549
556
  let previousValuesForHistory: Partial<M> | undefined;
550
557
  if (status === "existing" && id) {
551
- const existing = await this.dataService.fetchOne<M>(path, id, resolvedCollection?.databaseId);
552
- if (existing) {
553
- const { id: _existingId, ...existingValues } = existing;
554
- previousValuesForHistory = existingValues as Partial<M>;
558
+ try {
559
+ const existing = await this.dataService.getFetchService()
560
+ .fetchOneForRest(path, id, undefined, resolvedCollection?.databaseId);
561
+ if (existing) {
562
+ const { id: _existingId, ...existingValues } = existing;
563
+ previousValuesForHistory = existingValues as Partial<M>;
564
+ }
565
+ } catch (err) {
566
+ // Best-effort enrichment: callbacks and history run without
567
+ // previous values rather than the save failing on a read the
568
+ // write itself does not need (e.g. a collection whose key the
569
+ // registry cannot resolve).
570
+ logger.debug(`[save] Could not fetch previous values for "${path}"`, { detail: err instanceof Error ? err.message : String(err) });
555
571
  }
556
572
  }
557
573
 
@@ -616,7 +632,8 @@ export class PostgresBackendDriver implements DataDriver {
616
632
  path,
617
633
  updatedValues,
618
634
  id,
619
- resolvedCollection?.databaseId
635
+ resolvedCollection?.databaseId,
636
+ { upsert }
620
637
  );
621
638
 
622
639
  if (savedRow && (globalCallbacks?.afterRead || callbacks?.afterRead || propertyCallbacks?.afterRead)) {
@@ -649,8 +666,19 @@ export class PostgresBackendDriver implements DataDriver {
649
666
  }
650
667
  }
651
668
 
652
- const savedId = savedRow.id as string | number;
653
- const { id: _savedId, ...savedValues } = savedRow;
669
+ // The row is exactly its columns, so its address is derived, not read
670
+ // off it: `savedRow.id` is undefined for every table whose key is not
671
+ // literally named `id`, and is ordinary data for a table that has such
672
+ // a column without it being the key.
673
+ const savedId = deriveRowAddress(
674
+ savedRow,
675
+ (resolvedCollection ?? collection) as CollectionConfig,
676
+ this.registry
677
+ );
678
+ // `values` are the row's columns — all of them. For an `id`-keyed table
679
+ // that includes `id`, which used to be stripped here because it was the
680
+ // synthesized address rather than the column it now is.
681
+ const savedValues = savedRow;
654
682
 
655
683
  if (globalCallbacks?.afterSave || callbacks?.afterSave || propertyCallbacks?.afterSave) {
656
684
  // 1. Global callbacks first
@@ -695,7 +723,7 @@ export class PostgresBackendDriver implements DataDriver {
695
723
  if (this.historyService && resolvedCollection?.history) {
696
724
  this.historyService.recordHistory({
697
725
  tableName: path,
698
- id: savedId.toString(),
726
+ id: savedId,
699
727
  action: status === "new" ? "create" : "update",
700
728
  values: savedValues as Record<string, unknown>,
701
729
  previousValues: previousValuesForHistory as Record<string, unknown> | undefined,
@@ -707,14 +735,14 @@ export class PostgresBackendDriver implements DataDriver {
707
735
  if (this._deferNotifications) {
708
736
  this._pendingNotifications.push({
709
737
  path,
710
- id: savedId.toString(),
738
+ id: savedId,
711
739
  row: savedRow,
712
740
  databaseId: resolvedCollection?.databaseId
713
741
  });
714
742
  } else {
715
743
  await this.realtimeService.notifyUpdate(
716
744
  path,
717
- savedId.toString(),
745
+ savedId,
718
746
  savedRow,
719
747
  resolvedCollection?.databaseId
720
748
  );
@@ -764,13 +792,86 @@ export class PostgresBackendDriver implements DataDriver {
764
792
  }
765
793
  }
766
794
 
795
+ /**
796
+ * Write many rows through the same pipeline as {@link save}.
797
+ *
798
+ * The batch runs in one transaction of its own, so a failure part-way leaves
799
+ * nothing behind — the point of a batch is that a re-run starts from a known
800
+ * state. When this driver is already inside a transaction (the authenticated
801
+ * path, via `withTransaction`) the nested call becomes a savepoint, which is
802
+ * still atomic and still commits once.
803
+ *
804
+ * Rows are applied in order, so a batch that touches the same key twice ends
805
+ * with the last write winning, exactly as separate calls would.
806
+ */
807
+ async saveMany<M extends Record<string, unknown>>({
808
+ path,
809
+ rows,
810
+ collection,
811
+ upsert
812
+ }: SaveManyProps<M>): Promise<Record<string, unknown>[]> {
813
+ return this.db.transaction(async (tx) => {
814
+ // Bind the whole batch to the transaction handle. Without this the
815
+ // rows would be written through `this.db` and survive a rollback.
816
+ const txDriver = new PostgresBackendDriver(
817
+ tx, this.realtimeService, this.registry, this.user, this.poolManager, this.historyService
818
+ );
819
+ txDriver.dataService = new DataService(tx, this.registry);
820
+ txDriver.client = this.client;
821
+ // Carry the caller's notification batching through, so a bulk write
822
+ // nested in an outer transaction still holds its events until commit.
823
+ txDriver._deferNotifications = this._deferNotifications;
824
+ txDriver._pendingNotifications = this._pendingNotifications;
825
+
826
+ const saved: Record<string, unknown>[] = [];
827
+
828
+ for (let i = 0; i < rows.length; i++) {
829
+ const values = rows[i];
830
+ const id = (values as Record<string, unknown>)?.id as string | number | undefined;
831
+ try {
832
+ saved.push(await txDriver.save<M>({
833
+ path,
834
+ values,
835
+ // No `id` argument, deliberately: passing one selects the
836
+ // UPDATE path, and an import's rows usually carry a natural
837
+ // key for a row that does not exist yet — which would 404 on
838
+ // every one. Leaving the key inside `values` is what
839
+ // single-row `create(data, id)` does, and it inserts.
840
+ // Callers who want existing rows overwritten pass `upsert`.
841
+ collection,
842
+ status: "new",
843
+ upsert
844
+ }));
845
+ } catch (error) {
846
+ // One bad row in ten thousand is impossible to find from a
847
+ // message that only says the batch failed. Say which row, and
848
+ // keep the original error as the cause so its status survives.
849
+ const label = id !== undefined ? `id ${JSON.stringify(id)}` : "no id";
850
+ throw Object.assign(
851
+ new Error(`Row ${i} of ${rows.length} (${label}) failed: ${(error as Error)?.message ?? error}`, { cause: error }),
852
+ {
853
+ statusCode: (error as { statusCode?: number })?.statusCode,
854
+ code: (error as { code?: string })?.code,
855
+ name: (error as Error)?.name
856
+ }
857
+ );
858
+ }
859
+ }
860
+
861
+ return saved;
862
+ });
863
+ }
864
+
767
865
  async delete<M extends Record<string, unknown>>({
768
866
  row,
769
867
  collection
770
868
  }: DeleteProps<M>): Promise<void> {
771
869
 
772
870
  const targetPath = row.path;
773
- const targetRow: Record<string, unknown> = { id: row.id, ...(row.values ?? {}) };
871
+ // The callbacks' `row` is the row: its columns, nothing else. The address
872
+ // travels beside it as `id`, so merging it in here only ever invented an
873
+ // `id` field for tables that have no such column.
874
+ const targetRow: Record<string, unknown> = { ...(row.values ?? {}) };
774
875
 
775
876
  // Resolve from backend registry to restore callbacks lost during WebSocket serialization
776
877
  const {
@@ -1362,6 +1463,19 @@ export class AuthenticatedPostgresBackendDriver implements DataDriver {
1362
1463
  return this.withTransaction((delegate) => delegate.save(props));
1363
1464
  }
1364
1465
 
1466
+ /**
1467
+ * One transaction for the whole batch, rather than one per row.
1468
+ *
1469
+ * This is the point of the method: `save` opens a transaction per call, so
1470
+ * importing 10k rows through it means 10k transactions (and, over HTTP, 10k
1471
+ * round trips). Here the RLS context is established once and every row lands
1472
+ * or none does. Realtime notifications are already deferred to commit by
1473
+ * `withTransaction`, so a batch does not flood subscribers mid-flight.
1474
+ */
1475
+ async saveMany<M extends Record<string, unknown>>(props: SaveManyProps<M>): Promise<Record<string, unknown>[]> {
1476
+ return this.withTransaction((delegate) => delegate.saveMany(props));
1477
+ }
1478
+
1365
1479
  async delete<M extends Record<string, unknown>>(props: DeleteProps<M>): Promise<void> {
1366
1480
  return this.withTransaction((delegate) => delegate.delete(props));
1367
1481
  }