@rebasepro/server-postgres 0.9.1-canary.1d2d8b5 → 0.9.1-canary.58368ce
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.
- package/dist/PostgresBackendDriver.d.ts +25 -2
- package/dist/collections/buildRegistry.d.ts +27 -0
- package/dist/data-transformer.d.ts +9 -2
- package/dist/index.es.js +942 -373
- package/dist/index.es.js.map +1 -1
- package/dist/services/FetchService.d.ts +21 -7
- package/dist/services/PersistService.d.ts +9 -1
- package/dist/services/RelationService.d.ts +0 -1
- package/dist/services/collection-helpers.d.ts +52 -14
- package/dist/services/dataService.d.ts +3 -1
- package/dist/services/index.d.ts +1 -1
- package/dist/services/realtimeService.d.ts +7 -0
- package/package.json +6 -7
- package/src/PostgresBackendDriver.ts +109 -8
- package/src/PostgresBootstrapper.ts +11 -23
- package/src/collections/buildRegistry.ts +59 -0
- package/src/data-transformer.ts +11 -9
- package/src/schema/generate-drizzle-schema-logic.ts +24 -29
- package/src/schema/generate-postgres-ddl-logic.ts +76 -28
- package/src/services/BranchService.ts +42 -10
- package/src/services/FetchService.ts +84 -155
- package/src/services/PersistService.ts +53 -7
- package/src/services/RelationService.ts +10 -11
- package/src/services/collection-helpers.ts +129 -44
- package/src/services/dataService.ts +3 -2
- package/src/services/index.ts +1 -0
- package/src/services/realtimeService.ts +38 -19
- package/src/websocket.ts +4 -1
- package/dist/schema/auth-default-policies.d.ts +0 -10
- package/src/schema/auth-default-policies.ts +0 -132
|
@@ -49,13 +49,26 @@ 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;
|
|
52
63
|
/**
|
|
53
64
|
* Convert a db.query result row (with nested relation objects) to a flat row.
|
|
54
65
|
* Handles:
|
|
55
|
-
* - Placing `id` at the top level as a string
|
|
56
66
|
* - Type normalization (dates, numbers, NaN) via normalizeDbValues
|
|
57
67
|
* - Converting nested relation objects to { id, path, __type: "relation" } for CMS
|
|
58
68
|
* - 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.
|
|
59
72
|
*/
|
|
60
73
|
private drizzleResultToRow;
|
|
61
74
|
/**
|
|
@@ -64,18 +77,19 @@ export declare class FetchService {
|
|
|
64
77
|
* so they must be loaded separately after the primary query.
|
|
65
78
|
*/
|
|
66
79
|
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
80
|
/**
|
|
73
81
|
* Resolves joinPath relations for raw REST rows and directly injects them.
|
|
74
82
|
* Uses RelationService to query the database and maps results back to the flattened objects.
|
|
75
83
|
*/
|
|
76
84
|
private resolveJoinPathRelationsBatchRest;
|
|
77
85
|
/**
|
|
78
|
-
* Convert a db.query result row to a flat REST-style
|
|
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.
|
|
79
93
|
*/
|
|
80
94
|
private drizzleResultToRestRow;
|
|
81
95
|
/**
|
|
@@ -40,8 +40,16 @@ export declare class PersistService {
|
|
|
40
40
|
deleteAll(collectionPath: string, _databaseId?: string): Promise<void>;
|
|
41
41
|
/**
|
|
42
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.
|
|
43
49
|
*/
|
|
44
|
-
save<M extends Record<string, unknown>>(collectionPath: string, values: Partial<M>, id?: string | number, databaseId?: string
|
|
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>>;
|
|
45
53
|
/**
|
|
46
54
|
* Get the RelationService instance for external use
|
|
47
55
|
*/
|
|
@@ -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,53 @@ 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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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;
|
|
28
55
|
}[];
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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;
|
|
@@ -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
|
|
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
|
*/
|
package/dist/services/index.d.ts
CHANGED
|
@@ -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
|
/**
|
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.
|
|
4
|
+
"version": "0.9.1-canary.58368ce",
|
|
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,14 +69,13 @@
|
|
|
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/
|
|
76
|
-
"@rebasepro/
|
|
77
|
-
"@rebasepro/
|
|
78
|
-
"@rebasepro/
|
|
79
|
-
"@rebasepro/utils": "0.9.1-canary.
|
|
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"
|
|
80
79
|
},
|
|
81
80
|
"devDependencies": {
|
|
82
81
|
"@types/jest": "^30.0.0",
|
|
@@ -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 {
|
|
@@ -616,7 +619,8 @@ export class PostgresBackendDriver implements DataDriver {
|
|
|
616
619
|
path,
|
|
617
620
|
updatedValues,
|
|
618
621
|
id,
|
|
619
|
-
resolvedCollection?.databaseId
|
|
622
|
+
resolvedCollection?.databaseId,
|
|
623
|
+
{ upsert }
|
|
620
624
|
);
|
|
621
625
|
|
|
622
626
|
if (savedRow && (globalCallbacks?.afterRead || callbacks?.afterRead || propertyCallbacks?.afterRead)) {
|
|
@@ -649,8 +653,19 @@ export class PostgresBackendDriver implements DataDriver {
|
|
|
649
653
|
}
|
|
650
654
|
}
|
|
651
655
|
|
|
652
|
-
|
|
653
|
-
|
|
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;
|
|
654
669
|
|
|
655
670
|
if (globalCallbacks?.afterSave || callbacks?.afterSave || propertyCallbacks?.afterSave) {
|
|
656
671
|
// 1. Global callbacks first
|
|
@@ -695,7 +710,7 @@ export class PostgresBackendDriver implements DataDriver {
|
|
|
695
710
|
if (this.historyService && resolvedCollection?.history) {
|
|
696
711
|
this.historyService.recordHistory({
|
|
697
712
|
tableName: path,
|
|
698
|
-
id: savedId
|
|
713
|
+
id: savedId,
|
|
699
714
|
action: status === "new" ? "create" : "update",
|
|
700
715
|
values: savedValues as Record<string, unknown>,
|
|
701
716
|
previousValues: previousValuesForHistory as Record<string, unknown> | undefined,
|
|
@@ -707,14 +722,14 @@ export class PostgresBackendDriver implements DataDriver {
|
|
|
707
722
|
if (this._deferNotifications) {
|
|
708
723
|
this._pendingNotifications.push({
|
|
709
724
|
path,
|
|
710
|
-
id: savedId
|
|
725
|
+
id: savedId,
|
|
711
726
|
row: savedRow,
|
|
712
727
|
databaseId: resolvedCollection?.databaseId
|
|
713
728
|
});
|
|
714
729
|
} else {
|
|
715
730
|
await this.realtimeService.notifyUpdate(
|
|
716
731
|
path,
|
|
717
|
-
savedId
|
|
732
|
+
savedId,
|
|
718
733
|
savedRow,
|
|
719
734
|
resolvedCollection?.databaseId
|
|
720
735
|
);
|
|
@@ -764,13 +779,86 @@ export class PostgresBackendDriver implements DataDriver {
|
|
|
764
779
|
}
|
|
765
780
|
}
|
|
766
781
|
|
|
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
|
+
|
|
767
852
|
async delete<M extends Record<string, unknown>>({
|
|
768
853
|
row,
|
|
769
854
|
collection
|
|
770
855
|
}: DeleteProps<M>): Promise<void> {
|
|
771
856
|
|
|
772
857
|
const targetPath = row.path;
|
|
773
|
-
|
|
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 ?? {}) };
|
|
774
862
|
|
|
775
863
|
// Resolve from backend registry to restore callbacks lost during WebSocket serialization
|
|
776
864
|
const {
|
|
@@ -1362,6 +1450,19 @@ export class AuthenticatedPostgresBackendDriver implements DataDriver {
|
|
|
1362
1450
|
return this.withTransaction((delegate) => delegate.save(props));
|
|
1363
1451
|
}
|
|
1364
1452
|
|
|
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
|
+
|
|
1365
1466
|
async delete<M extends Record<string, unknown>>(props: DeleteProps<M>): Promise<void> {
|
|
1366
1467
|
return this.withTransaction((delegate) => delegate.delete(props));
|
|
1367
1468
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Implements the `BackendBootstrapper` interface for PostgreSQL.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { 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,6 +21,7 @@ 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";
|
|
24
25
|
import { DatabasePoolManager } from "./databasePoolManager";
|
|
25
26
|
import { PostgresCollectionRegistry } from "./collections/PostgresCollectionRegistry";
|
|
26
27
|
import { createEmailService, type EmailConfig, type EmailService, logger } from "@rebasepro/server";
|
|
@@ -161,30 +162,17 @@ export function createPostgresBootstrapper(pgConfig: PostgresDriverConfig): Back
|
|
|
161
162
|
}
|
|
162
163
|
|
|
163
164
|
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
|
-
|
|
172
165
|
const schemaTables = introspectedTables ?? pgConfig.schema?.tables;
|
|
173
|
-
|
|
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
166
|
const schemaRelations = introspectedRelations ?? (pgConfig.schema?.relations as Record<string, Relations> | undefined);
|
|
187
|
-
|
|
167
|
+
|
|
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
|
+
});
|
|
188
176
|
|
|
189
177
|
// Patch Drizzle's PgArray columns to handle NULL values safely.
|
|
190
178
|
// Drizzle's mapFromDriverValue crashes with "value.map is not a function"
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { isTable, getTableName, Relations } from "drizzle-orm";
|
|
2
|
+
import { PgEnum, PgTable } from "drizzle-orm/pg-core";
|
|
3
|
+
import { CollectionConfig } from "@rebasepro/types";
|
|
4
|
+
import { logger } from "@rebasepro/server";
|
|
5
|
+
import { PostgresCollectionRegistry } from "./PostgresCollectionRegistry";
|
|
6
|
+
import { warnOnKeysTheAdminCannotResolve } from "../services/collection-helpers";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Everything a registry is built from: the collections, and the drizzle schema
|
|
10
|
+
* they are backed by. In BaaS mode all of it is introspected from the live
|
|
11
|
+
* database; in CMS mode it comes from the config and the generated schema.
|
|
12
|
+
*/
|
|
13
|
+
export interface RegistrySchema {
|
|
14
|
+
collections?: CollectionConfig[];
|
|
15
|
+
tables?: Record<string, unknown>;
|
|
16
|
+
enums?: Record<string, PgEnum<[string, ...string[]]>>;
|
|
17
|
+
relations?: Record<string, Relations>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Build the collection registry for a driver.
|
|
22
|
+
*
|
|
23
|
+
* The order matters and is the reason this is one function rather than a run of
|
|
24
|
+
* statements in the bootstrapper. Keys are resolved from the drizzle schema, so
|
|
25
|
+
* anything that inspects them has to run *after* the tables are registered —
|
|
26
|
+
* and `warnOnKeysTheAdminCannotResolve` fails open if it does not, because a
|
|
27
|
+
* collection whose table it cannot look up is one it has nothing to say about.
|
|
28
|
+
* Warned too early, it would skip every collection and report nothing, which
|
|
29
|
+
* reads exactly like having nothing to report.
|
|
30
|
+
*/
|
|
31
|
+
export function buildCollectionRegistry(schema: RegistrySchema): PostgresCollectionRegistry {
|
|
32
|
+
const registry = new PostgresCollectionRegistry();
|
|
33
|
+
|
|
34
|
+
if (schema.collections) {
|
|
35
|
+
registry.registerMultiple(schema.collections);
|
|
36
|
+
logger.info(
|
|
37
|
+
`📋 [PostgresRegistry] Registered ${registry.getCollections().length} collections: ` +
|
|
38
|
+
`[${registry.getCollections().map(c => c.slug).join(", ")}]`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (schema.tables) {
|
|
43
|
+
Object.values(schema.tables).forEach((table) => {
|
|
44
|
+
if (isTable(table)) {
|
|
45
|
+
registry.registerTable(table as PgTable, getTableName(table));
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (schema.enums) registry.registerEnums(schema.enums);
|
|
51
|
+
if (schema.relations) registry.registerRelations(schema.relations);
|
|
52
|
+
|
|
53
|
+
// Now that the keys resolve: say which of them the admin cannot see. It
|
|
54
|
+
// compiles the same collection files into its bundle but never the drizzle
|
|
55
|
+
// schema, and nothing serves it one, so only an edit to the config fixes it.
|
|
56
|
+
warnOnKeysTheAdminCannotResolve(registry.getCollections(), registry);
|
|
57
|
+
|
|
58
|
+
return registry;
|
|
59
|
+
}
|
package/src/data-transformer.ts
CHANGED
|
@@ -20,12 +20,19 @@ 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
|
-
/**
|
|
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
|
+
*/
|
|
24
32
|
inverseRelationUpdates: Array<{
|
|
25
33
|
relationKey: string;
|
|
26
34
|
relation: Relation;
|
|
27
35
|
newValue: unknown;
|
|
28
|
-
currentId?: string | number;
|
|
29
36
|
}>;
|
|
30
37
|
/** JoinPath relation updates that require multi-hop writes. */
|
|
31
38
|
joinPathRelationUpdates: Array<{
|
|
@@ -105,7 +112,6 @@ joinPathRelationUpdates: [] };
|
|
|
105
112
|
relationKey: string;
|
|
106
113
|
relation: Relation;
|
|
107
114
|
newValue: unknown;
|
|
108
|
-
currentId?: string | number;
|
|
109
115
|
}> = [];
|
|
110
116
|
const joinPathRelationUpdates: Array<{
|
|
111
117
|
relationKey: string;
|
|
@@ -146,12 +152,10 @@ joinPathRelationUpdates: [] };
|
|
|
146
152
|
} else if (relation.direction === "inverse" && relation.foreignKeyOnTarget) {
|
|
147
153
|
// Inverse relation: Need to update the target table's FK
|
|
148
154
|
const serializedValue = serializePropertyToServer(effectiveValue, property);
|
|
149
|
-
const pks = getPrimaryKeys(collection, registry!);
|
|
150
155
|
inverseRelationUpdates.push({
|
|
151
156
|
relationKey: key,
|
|
152
157
|
relation,
|
|
153
|
-
newValue: serializedValue
|
|
154
|
-
currentId: (row.id as string | number | undefined) || buildCompositeId(row, pks)
|
|
158
|
+
newValue: serializedValue
|
|
155
159
|
});
|
|
156
160
|
// Don't add the original relation property to the result
|
|
157
161
|
continue;
|
|
@@ -170,12 +174,10 @@ joinPathRelationUpdates: [] };
|
|
|
170
174
|
});
|
|
171
175
|
} else {
|
|
172
176
|
// Many inverse joinPath: capture as inverse relation update
|
|
173
|
-
const pks = getPrimaryKeys(collection, registry!);
|
|
174
177
|
inverseRelationUpdates.push({
|
|
175
178
|
relationKey: key,
|
|
176
179
|
relation,
|
|
177
|
-
newValue: serializedValue
|
|
178
|
-
currentId: (row.id as string | number | undefined) || buildCompositeId(row, pks)
|
|
180
|
+
newValue: serializedValue
|
|
179
181
|
});
|
|
180
182
|
}
|
|
181
183
|
// Don't add the original relation property to the result
|