@rebasepro/server-postgres 0.9.1-canary.ad870eb → 0.9.1-canary.b10dcdf
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/PostgresBootstrapper.d.ts +10 -0
- package/dist/auth/services.d.ts +16 -0
- package/dist/collections/buildRegistry.d.ts +27 -0
- package/dist/connection.d.ts +21 -0
- package/dist/data-transformer.d.ts +9 -2
- package/dist/index.es.js +1445 -571
- package/dist/index.es.js.map +1 -1
- package/dist/services/FetchService.d.ts +4 -24
- package/dist/services/PersistService.d.ts +27 -1
- package/dist/services/RelationService.d.ts +34 -1
- package/dist/services/collection-helpers.d.ts +79 -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/dist/services/row-pipeline.d.ts +63 -0
- package/package.json +11 -9
- package/src/PostgresBackendDriver.ts +127 -13
- package/src/PostgresBootstrapper.ts +62 -25
- package/src/auth/ensure-tables.ts +73 -11
- package/src/auth/services.ts +49 -19
- package/src/collections/buildRegistry.ts +59 -0
- package/src/connection.ts +61 -1
- package/src/data-transformer.ts +11 -9
- package/src/databasePoolManager.ts +2 -0
- package/src/schema/generate-drizzle-schema-logic.ts +24 -29
- package/src/schema/generate-postgres-ddl-logic.ts +76 -28
- package/src/schema/introspect-db.ts +19 -2
- package/src/services/BranchService.ts +42 -10
- package/src/services/FetchService.ts +65 -270
- package/src/services/PersistService.ts +130 -14
- package/src/services/RelationService.ts +153 -94
- package/src/services/collection-helpers.ts +164 -47
- package/src/services/dataService.ts +3 -2
- package/src/services/index.ts +1 -0
- package/src/services/realtimeService.ts +40 -19
- package/src/services/row-pipeline.ts +215 -0
- package/src/utils/drizzle-conditions.ts +13 -0
- 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
|
@@ -2,6 +2,15 @@ import { PgTable, AnyPgColumn } from "drizzle-orm/pg-core";
|
|
|
2
2
|
import { CollectionConfig, Property } from "@rebasepro/types";
|
|
3
3
|
import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry";
|
|
4
4
|
import { getTableName } from "@rebasepro/common";
|
|
5
|
+
import { logger } from "@rebasepro/server";
|
|
6
|
+
|
|
7
|
+
// Row identity is derived on both sides of the wire — the driver parses an
|
|
8
|
+
// incoming address into key columns, the admin derives one from a served row —
|
|
9
|
+
// so the implementation lives in `common` and both agree by construction.
|
|
10
|
+
export { buildCompositeId, parseIdValues, COMPOSITE_ID_SEPARATOR } from "@rebasepro/common";
|
|
11
|
+
export type { PrimaryKeyInfo } from "@rebasepro/common";
|
|
12
|
+
import { buildCompositeId, COMPOSITE_ID_SEPARATOR, getDeclaredPrimaryKeys } from "@rebasepro/common";
|
|
13
|
+
import type { PrimaryKeyInfo } from "@rebasepro/common";
|
|
5
14
|
|
|
6
15
|
/**
|
|
7
16
|
* Shared helper functions for row operations.
|
|
@@ -49,10 +58,27 @@ export function getTableForCollection(collection: CollectionConfig, registry: Po
|
|
|
49
58
|
return table;
|
|
50
59
|
}
|
|
51
60
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
61
|
+
/**
|
|
62
|
+
* The key columns a collection's rows are addressed by.
|
|
63
|
+
*
|
|
64
|
+
* Three tiers, in order: properties marked `isId`, the primary keys of the
|
|
65
|
+
* drizzle schema, and finally a column literally named `id`. Only the first is
|
|
66
|
+
* visible to the browser, which is why a key known only to drizzle is reported
|
|
67
|
+
* at boot — see {@link warnOnKeysTheAdminCannotResolve}.
|
|
68
|
+
*
|
|
69
|
+
* Returns `[]` when nothing resolves, rather than throwing. It used to open by
|
|
70
|
+
* resolving the table, which throws when there is none — so the `isId` tier,
|
|
71
|
+
* which needs no table at all, was unreachable for exactly the collections
|
|
72
|
+
* most likely to have no table registered. Every caller that wanted "no keys"
|
|
73
|
+
* to mean "no keys" had to spell that out in a try/catch.
|
|
74
|
+
*
|
|
75
|
+
* Callers that cannot proceed without a key must say so themselves, naming the
|
|
76
|
+
* collection: an empty array here means "this collection has no address", which
|
|
77
|
+
* is a different answer in a notification (broadcast a wildcard) than in a save
|
|
78
|
+
* (fail).
|
|
79
|
+
*/
|
|
80
|
+
export function getPrimaryKeys(collection: CollectionConfig, registry: PostgresCollectionRegistry): PrimaryKeyInfo[] {
|
|
81
|
+
// Explicitly declared `isId` properties win, and need no table.
|
|
56
82
|
if (collection.properties) {
|
|
57
83
|
const idProps = Object.entries(collection.properties)
|
|
58
84
|
.filter(([_, prop]) => "isId" in (prop as object) && Boolean((prop as { isId?: unknown }).isId))
|
|
@@ -67,8 +93,14 @@ export function getPrimaryKeys(collection: CollectionConfig, registry: PostgresC
|
|
|
67
93
|
}
|
|
68
94
|
}
|
|
69
95
|
|
|
96
|
+
// The remaining tiers read the drizzle schema, so they need the table. A
|
|
97
|
+
// collection without one — another engine's, or simply unregistered — has
|
|
98
|
+
// nothing more to offer.
|
|
99
|
+
const table = registry.getTable(getTableName(collection));
|
|
100
|
+
if (!table) return [];
|
|
101
|
+
|
|
70
102
|
// Otherwise infer from Drizzle schema
|
|
71
|
-
const keys:
|
|
103
|
+
const keys: PrimaryKeyInfo[] = [];
|
|
72
104
|
for (const [key, colRaw] of Object.entries(table)) {
|
|
73
105
|
const col = colRaw as AnyPgColumn;
|
|
74
106
|
if (col && typeof col === "object" && "primary" in col && col.primary) {
|
|
@@ -96,56 +128,141 @@ isUUID });
|
|
|
96
128
|
return keys;
|
|
97
129
|
}
|
|
98
130
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
131
|
+
/**
|
|
132
|
+
* The key columns, for callers that cannot do their job without one.
|
|
133
|
+
*
|
|
134
|
+
* {@link getPrimaryKeys} answers "what keys, if any" and returns `[]` for a
|
|
135
|
+
* collection with no address. Most of this driver, though, is building a WHERE
|
|
136
|
+
* clause and has no meaning without a key — for those, an empty array is not an
|
|
137
|
+
* answer, and indexing `[0]` into it produces `Cannot read properties of
|
|
138
|
+
* undefined` three frames from where the real problem is. This says what is
|
|
139
|
+
* wrong and which collection it is wrong about.
|
|
140
|
+
*/
|
|
141
|
+
export function requirePrimaryKeys(collection: CollectionConfig, registry: PostgresCollectionRegistry): PrimaryKeyInfo[] {
|
|
142
|
+
const keys = getPrimaryKeys(collection, registry);
|
|
143
|
+
if (keys.length === 0) {
|
|
144
|
+
throw new Error(
|
|
145
|
+
`Collection '${collection.slug}' has no primary key, so its rows cannot be addressed. ` +
|
|
146
|
+
`Mark the key property with \`isId\` in its config, or register a table whose schema declares one.`
|
|
147
|
+
);
|
|
104
148
|
}
|
|
149
|
+
return keys;
|
|
150
|
+
}
|
|
105
151
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
152
|
+
/**
|
|
153
|
+
* Collections whose key the *browser* cannot resolve, and what it will do
|
|
154
|
+
* instead.
|
|
155
|
+
*
|
|
156
|
+
* The two sides resolve keys from different evidence. This driver reads, in
|
|
157
|
+
* order: properties marked `isId`, the primary keys of the Drizzle schema, then
|
|
158
|
+
* a column literally named `id`. The admin shares the `CollectionConfig` — it
|
|
159
|
+
* compiles the same collection files into its bundle — but never the Drizzle
|
|
160
|
+
* schema, so the middle tier is invisible to it.
|
|
161
|
+
*
|
|
162
|
+
* Nothing can normalize this at runtime: the server does not serve the admin
|
|
163
|
+
* its collections, so a key resolved here cannot be handed over there. The
|
|
164
|
+
* config files are the only thing both sides read, so the fix is an edit to
|
|
165
|
+
* them, and the most this can do is say exactly which edit.
|
|
166
|
+
*
|
|
167
|
+
* Two shapes, and the second is the dangerous one:
|
|
168
|
+
*
|
|
169
|
+
* - No `isId`, no `id` property → the admin resolves no address, warns in the
|
|
170
|
+
* console, and rows cannot be opened or linked.
|
|
171
|
+
* - No `isId`, but an `id` property that is *not* the key → the admin addresses
|
|
172
|
+
* rows by `id` while this driver reads the address as the real key. Nothing
|
|
173
|
+
* errors: the addresses look right and route wrong.
|
|
174
|
+
*/
|
|
175
|
+
export function findUnresolvableKeyCollections(
|
|
176
|
+
collections: CollectionConfig[],
|
|
177
|
+
registry: PostgresCollectionRegistry
|
|
178
|
+
): { collection: CollectionConfig; keys: PrimaryKeyInfo[]; shadowedByIdProperty: boolean }[] {
|
|
179
|
+
const findings: { collection: CollectionConfig; keys: PrimaryKeyInfo[]; shadowedByIdProperty: boolean }[] = [];
|
|
119
180
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
throw new Error(`Composite ID parts mismatch. Expected ${primaryKeys.length}, got ${parts.length} for ID: ${idValue}`);
|
|
124
|
-
}
|
|
181
|
+
for (const collection of collections) {
|
|
182
|
+
// Declared `isId` is the tier both sides share: if it is there, they agree.
|
|
183
|
+
if (getDeclaredPrimaryKeys(collection).length > 0) continue;
|
|
125
184
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const
|
|
129
|
-
if (
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
185
|
+
// No registered table resolves to no keys, and a collection this cannot
|
|
186
|
+
// resolve a key for is one it has nothing to say about.
|
|
187
|
+
const keys = getPrimaryKeys(collection, registry);
|
|
188
|
+
if (keys.length === 0) continue;
|
|
189
|
+
|
|
190
|
+
// A single key named `id` is the last tier on both sides: they agree
|
|
191
|
+
// without anything being declared.
|
|
192
|
+
if (keys.length === 1 && keys[0].fieldName === "id") continue;
|
|
193
|
+
|
|
194
|
+
findings.push({
|
|
195
|
+
collection,
|
|
196
|
+
keys,
|
|
197
|
+
shadowedByIdProperty: Boolean(collection.properties?.id)
|
|
198
|
+
});
|
|
138
199
|
}
|
|
139
200
|
|
|
140
|
-
return
|
|
201
|
+
return findings;
|
|
141
202
|
}
|
|
142
203
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
204
|
+
/**
|
|
205
|
+
* Report the collections from {@link findUnresolvableKeyCollections} at boot,
|
|
206
|
+
* with the edit that fixes each one.
|
|
207
|
+
*
|
|
208
|
+
* Grouped by failure, not by collection: the shadowed case is a routing bug and
|
|
209
|
+
* the silent case is a missing feature, and they deserve different urgency.
|
|
210
|
+
*/
|
|
211
|
+
export function warnOnKeysTheAdminCannotResolve(
|
|
212
|
+
collections: CollectionConfig[],
|
|
213
|
+
registry: PostgresCollectionRegistry
|
|
214
|
+
): void {
|
|
215
|
+
const findings = findUnresolvableKeyCollections(collections, registry);
|
|
216
|
+
if (findings.length === 0) return;
|
|
217
|
+
|
|
218
|
+
const edit = (f: { collection: CollectionConfig; keys: PrimaryKeyInfo[] }) =>
|
|
219
|
+
`${f.collection.slug}: mark ${f.keys.map(k => `\`${k.fieldName}\``).join(" and ")} with ` +
|
|
220
|
+
`\`isId: ${f.keys[0].isUUID ? "\"uuid\"" : f.keys[0].type === "number" ? "\"increment\"" : "true"}\``;
|
|
221
|
+
|
|
222
|
+
const shadowed = findings.filter(f => f.shadowedByIdProperty);
|
|
223
|
+
const silent = findings.filter(f => !f.shadowedByIdProperty);
|
|
224
|
+
|
|
225
|
+
if (shadowed.length > 0) {
|
|
226
|
+
logger.warn(
|
|
227
|
+
`⚠️ These collections declare no \`isId\`, and their key is only in the drizzle schema — but they ` +
|
|
228
|
+
`do have a property called \`id\`. The admin has no way to know \`id\` is not the key, so it will ` +
|
|
229
|
+
`address rows by it while this server reads the address as the real key: the links look right and ` +
|
|
230
|
+
`route wrong. Nothing will error.\n\n` +
|
|
231
|
+
shadowed.map(f => ` • ${edit(f)}`).join("\n") + "\n"
|
|
232
|
+
);
|
|
146
233
|
}
|
|
147
|
-
|
|
148
|
-
|
|
234
|
+
|
|
235
|
+
if (silent.length > 0) {
|
|
236
|
+
logger.warn(
|
|
237
|
+
`⚠️ These collections declare no \`isId\`, and their key is only in the drizzle schema, which the ` +
|
|
238
|
+
`admin never sees. It will resolve no address for their rows, so detail links, caching and ` +
|
|
239
|
+
`relations will not work for them:\n\n` +
|
|
240
|
+
silent.map(f => ` • ${edit(f)}`).join("\n") + "\n"
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* The address of a row: derived from the collection's primary keys, because a
|
|
247
|
+
* row does not carry one — it is exactly its columns.
|
|
248
|
+
*
|
|
249
|
+
* Falls back to a literal `id` column, for a row that reached us from somewhere
|
|
250
|
+
* other than this driver. Returns `""` when there is no key and no `id` —
|
|
251
|
+
* callers decide what that means, since "unaddressable" is a different answer
|
|
252
|
+
* in a notification (broadcast a wildcard) than in a save (fail).
|
|
253
|
+
*/
|
|
254
|
+
export function deriveRowAddress(
|
|
255
|
+
row: Record<string, unknown>,
|
|
256
|
+
collection: CollectionConfig,
|
|
257
|
+
registry: PostgresCollectionRegistry
|
|
258
|
+
): string {
|
|
259
|
+
const composite = buildCompositeId(row, getPrimaryKeys(collection, registry));
|
|
260
|
+
// An all-empty composite is indistinguishable from a missing key, and
|
|
261
|
+
// `buildCompositeId` returns "" for no keys at all.
|
|
262
|
+
if (composite && composite.split(COMPOSITE_ID_SEPARATOR).some(part => part !== "")) {
|
|
263
|
+
return composite;
|
|
149
264
|
}
|
|
150
|
-
|
|
265
|
+
if (row.id !== undefined && row.id !== null) return String(row.id);
|
|
266
|
+
return "";
|
|
151
267
|
}
|
|
268
|
+
|
|
@@ -154,9 +154,10 @@ export class DataService implements DataRepository {
|
|
|
154
154
|
collectionPath: string,
|
|
155
155
|
values: Partial<M>,
|
|
156
156
|
id?: string | number,
|
|
157
|
-
databaseId?: string
|
|
157
|
+
databaseId?: string,
|
|
158
|
+
options?: { upsert?: boolean }
|
|
158
159
|
): Promise<Record<string, unknown>> {
|
|
159
|
-
return this.persistService.save<M>(collectionPath, values, id, databaseId);
|
|
160
|
+
return this.persistService.save<M>(collectionPath, values, id, databaseId, options);
|
|
160
161
|
}
|
|
161
162
|
|
|
162
163
|
/**
|
package/src/services/index.ts
CHANGED
|
@@ -14,7 +14,7 @@ import { applyAuthContext } from "../security/rls-enforcement";
|
|
|
14
14
|
import { logger } from "@rebasepro/server";
|
|
15
15
|
import { sanitizeErrorForClient } from "../utils/pg-error-utils";
|
|
16
16
|
import { CdcListener, type CdcChangeEvent } from "./cdc/CdcListener";
|
|
17
|
-
import { getPrimaryKeys,
|
|
17
|
+
import { deriveRowAddress, getPrimaryKeys, type PrimaryKeyInfo } from "./collection-helpers";
|
|
18
18
|
|
|
19
19
|
/** Channel name used for Postgres LISTEN/NOTIFY cross-instance realtime. */
|
|
20
20
|
const PG_NOTIFY_CHANNEL = "rebase_entity_changes";
|
|
@@ -390,7 +390,7 @@ export class RealtimeService extends EventEmitter implements RealtimeProvider {
|
|
|
390
390
|
authContext
|
|
391
391
|
);
|
|
392
392
|
|
|
393
|
-
this.sendCollectionUpdate(clientId, subscriptionId, rows);
|
|
393
|
+
this.sendCollectionUpdate(clientId, subscriptionId, rows, request.path);
|
|
394
394
|
|
|
395
395
|
} catch (error) {
|
|
396
396
|
const sanitized = sanitizeErrorForClient(error, request.path);
|
|
@@ -554,7 +554,7 @@ export class RealtimeService extends EventEmitter implements RealtimeProvider {
|
|
|
554
554
|
// Phase 1: Send instant row-level patch (no DB query)
|
|
555
555
|
// This gives immediate cross-tab feedback
|
|
556
556
|
if (!row || !(row as Record<string, unknown>)?._rebase_invalidated) {
|
|
557
|
-
this.sendCollectionPatch(subscription.clientId, subscriptionId, id, row);
|
|
557
|
+
this.sendCollectionPatch(subscription.clientId, subscriptionId, id, row, notifyPath);
|
|
558
558
|
}
|
|
559
559
|
|
|
560
560
|
// Phase 2: Schedule a deferred full refetch for correctness
|
|
@@ -609,7 +609,7 @@ export class RealtimeService extends EventEmitter implements RealtimeProvider {
|
|
|
609
609
|
if (!this._subscriptions.has(subscriptionId)) return;
|
|
610
610
|
try {
|
|
611
611
|
const rows = await this.fetchCollectionWithAuth(notifyPath, subscription.collectionRequest!, subscription.authContext);
|
|
612
|
-
this.sendCollectionUpdate(subscription.clientId, subscriptionId, rows);
|
|
612
|
+
this.sendCollectionUpdate(subscription.clientId, subscriptionId, rows, notifyPath);
|
|
613
613
|
} catch (error) {
|
|
614
614
|
const sanitized = sanitizeErrorForClient(error, notifyPath);
|
|
615
615
|
this.sendError(subscription.clientId, sanitized.message, subscriptionId, sanitized.code);
|
|
@@ -910,11 +910,12 @@ roles: activeAuth.roles },
|
|
|
910
910
|
return await this.dataService.fetchOne(notifyPath, id);
|
|
911
911
|
}
|
|
912
912
|
|
|
913
|
-
private sendCollectionUpdate(clientId: string, subscriptionId: string, rows: Record<string, unknown>[]) {
|
|
913
|
+
private sendCollectionUpdate(clientId: string, subscriptionId: string, rows: Record<string, unknown>[], path: string) {
|
|
914
914
|
const message: CollectionUpdateMessage = {
|
|
915
915
|
type: "collection_update",
|
|
916
916
|
subscriptionId,
|
|
917
|
-
rows: rows
|
|
917
|
+
rows: rows,
|
|
918
|
+
pks: this.primaryKeysForPath(path)
|
|
918
919
|
};
|
|
919
920
|
this.sendMessage(clientId, message);
|
|
920
921
|
}
|
|
@@ -931,17 +932,45 @@ roles: activeAuth.roles },
|
|
|
931
932
|
/**
|
|
932
933
|
* Send a lightweight row-level patch to a collection subscriber.
|
|
933
934
|
* The client can merge this into its cached data for instant feedback.
|
|
935
|
+
*
|
|
936
|
+
* The key columns ride along: the patch names a row by address, and the
|
|
937
|
+
* client has to find that row among the ones it cached — which carry
|
|
938
|
+
* columns and no address. The SDK holds no collection config to derive one
|
|
939
|
+
* from, so this is the only place the mapping can come from.
|
|
934
940
|
*/
|
|
935
|
-
private sendCollectionPatch(
|
|
941
|
+
private sendCollectionPatch(
|
|
942
|
+
clientId: string,
|
|
943
|
+
subscriptionId: string,
|
|
944
|
+
id: string,
|
|
945
|
+
row: Record<string, unknown> | null,
|
|
946
|
+
notifyPath: string
|
|
947
|
+
) {
|
|
936
948
|
const message: CollectionPatchMessage = {
|
|
937
949
|
type: "collection_patch",
|
|
938
950
|
subscriptionId,
|
|
939
951
|
id,
|
|
940
|
-
row: row
|
|
952
|
+
row: row,
|
|
953
|
+
pks: this.primaryKeysForPath(notifyPath)
|
|
941
954
|
};
|
|
942
955
|
this.sendMessage(clientId, message);
|
|
943
956
|
}
|
|
944
957
|
|
|
958
|
+
/** The key columns of the collection at `path`, if they can be resolved. */
|
|
959
|
+
private primaryKeysForPath(path: string): PrimaryKeyInfo[] | undefined {
|
|
960
|
+
try {
|
|
961
|
+
const collection = this.registry.getCollectionByPath(path);
|
|
962
|
+
if (!collection) return undefined;
|
|
963
|
+
const keys = getPrimaryKeys(collection, this.registry);
|
|
964
|
+
return keys.length > 0 ? keys : undefined;
|
|
965
|
+
} catch {
|
|
966
|
+
// `getCollectionByPath` throws on a path it cannot walk — and this
|
|
967
|
+
// is called for parent paths too, which include entity paths like
|
|
968
|
+
// `posts/1` that name no collection. Telling the subscriber nothing
|
|
969
|
+
// is right here; letting it throw would drop the notification.
|
|
970
|
+
return undefined;
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
945
974
|
private sendError(clientId: string, error: string, subscriptionId?: string, code?: string) {
|
|
946
975
|
const message = {
|
|
947
976
|
type: "error" as const,
|
|
@@ -1294,17 +1323,9 @@ lastSeen: Date.now() });
|
|
|
1294
1323
|
|
|
1295
1324
|
/** Compute the canonical (possibly composite) id string from a captured row. */
|
|
1296
1325
|
private extractIdFromCdcRow(collection: CollectionConfig, row: Record<string, unknown>): string {
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
if (composite && composite !== ":::") return composite;
|
|
1301
|
-
} catch {
|
|
1302
|
-
/* fall through to id-field / wildcard */
|
|
1303
|
-
}
|
|
1304
|
-
// Fallbacks: a plain `id` column (common), else a collection-level
|
|
1305
|
-
// invalidation ("*") — single-row subs won't match but collection subs refetch.
|
|
1306
|
-
if (row.id !== undefined && row.id !== null) return String(row.id);
|
|
1307
|
-
return "*";
|
|
1326
|
+
// Unaddressable falls back to a collection-level invalidation: single-row
|
|
1327
|
+
// subs won't match, but collection subs still refetch.
|
|
1328
|
+
return deriveRowAddress(row, collection, this.registry) || "*";
|
|
1308
1329
|
}
|
|
1309
1330
|
|
|
1310
1331
|
// ── App/CDC de-duplication ──
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { CollectionConfig, Property, Relation } from "@rebasepro/types";
|
|
2
|
+
import { resolveCollectionRelations, findRelation, createRelationRefWithData } from "@rebasepro/common";
|
|
3
|
+
import { normalizeDbValues } from "../data-transformer";
|
|
4
|
+
import { deriveRowAddress } from "./collection-helpers";
|
|
5
|
+
import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Turning a drizzle result into a row we serve.
|
|
9
|
+
*
|
|
10
|
+
* There are two shapes, and they are the same walk. Both take a row whose
|
|
11
|
+
* relation fields hold nested objects, both unwrap junction rows to reach the
|
|
12
|
+
* target behind them, and both leave every other column alone. They differ only
|
|
13
|
+
* in what they put where the relation was:
|
|
14
|
+
*
|
|
15
|
+
* - `"ref"` — a `{ id, path, __type: "relation" }` reference carrying the
|
|
16
|
+
* target's values. This is what the admin renders.
|
|
17
|
+
* - `"inline"` — the target's own columns, flat. This is what REST serves.
|
|
18
|
+
*
|
|
19
|
+
* They used to be two functions that happened to agree, and the agreement was
|
|
20
|
+
* not enforced by anything: the row-identity bug had to be fixed five times
|
|
21
|
+
* across differently-shaped copies of this walk, and one of the copies was
|
|
22
|
+
* dead code nobody had noticed. Whatever the next cross-cutting change is, it
|
|
23
|
+
* is one edit here.
|
|
24
|
+
*/
|
|
25
|
+
export type RelationStyle = "ref" | "inline";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Whether a many-relation reaches its target through a junction table.
|
|
29
|
+
*
|
|
30
|
+
* Also used to build the drizzle `with` config, which is why it is exported:
|
|
31
|
+
* the query has to nest one level deeper for a junction, and the row walk has
|
|
32
|
+
* to unwrap that same level back out.
|
|
33
|
+
*/
|
|
34
|
+
export function isJunctionRelation(relation: Relation): boolean {
|
|
35
|
+
// An explicit `through` says so outright.
|
|
36
|
+
if (relation.through) return true;
|
|
37
|
+
// A joinPath with an intermediate table is the same thing, spelled longhand.
|
|
38
|
+
if (relation.joinPath && relation.joinPath.length > 1) return true;
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Reach the target row inside a junction row.
|
|
44
|
+
*
|
|
45
|
+
* A junction row looks like `{ post_id: 1, tag_id: { id: 5, name: "ts" } }` —
|
|
46
|
+
* the foreign keys, and the target nested under one of them. The target is the
|
|
47
|
+
* only object among them, so that is how it is found. A junction row that has
|
|
48
|
+
* not been nested (no `with` on the join) has no object and is returned as-is.
|
|
49
|
+
*/
|
|
50
|
+
function unwrapJunctionRow(item: Record<string, unknown>): Record<string, unknown> {
|
|
51
|
+
const nestedKey = Object.keys(item).find(
|
|
52
|
+
key => typeof item[key] === "object" && item[key] !== null && !Array.isArray(item[key])
|
|
53
|
+
);
|
|
54
|
+
return nestedKey ? item[nestedKey] as Record<string, unknown> : item;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Give back the number a `number` property was declared to be.
|
|
59
|
+
*
|
|
60
|
+
* Postgres returns NUMERIC as a string and drizzle keeps it that way, since a
|
|
61
|
+
* numeric can hold more precision than a double. But the collection declared
|
|
62
|
+
* this property `number` and the OpenAPI spec this server publishes says
|
|
63
|
+
* `type: number`, so serving `"9.99"` breaks its own contract — and breaks it
|
|
64
|
+
* asymmetrically, because a create answers with the number and the read that
|
|
65
|
+
* follows answers with the string. Any client that multiplies a price works
|
|
66
|
+
* until the first refresh.
|
|
67
|
+
*
|
|
68
|
+
* Declaring a property `number` already accepts double precision — that is what
|
|
69
|
+
* the admin has always parsed it to. Columns REST serves that no property
|
|
70
|
+
* declares are left exactly as the database returned them.
|
|
71
|
+
*/
|
|
72
|
+
function coerceDeclaredNumber(value: unknown, property: Property | undefined): unknown {
|
|
73
|
+
if (property?.type !== "number" || typeof value !== "string") return value;
|
|
74
|
+
const parsed = parseFloat(value);
|
|
75
|
+
return isNaN(parsed) ? null : parsed;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Apply {@link coerceDeclaredNumber} across a row, leaving every other column alone. */
|
|
79
|
+
function coerceDeclaredNumbers(
|
|
80
|
+
row: Record<string, unknown>,
|
|
81
|
+
collection: CollectionConfig
|
|
82
|
+
): Record<string, unknown> {
|
|
83
|
+
const properties = collection.properties;
|
|
84
|
+
if (!properties) return row;
|
|
85
|
+
|
|
86
|
+
const out: Record<string, unknown> = {};
|
|
87
|
+
for (const [key, value] of Object.entries(row)) {
|
|
88
|
+
out[key] = coerceDeclaredNumber(value, properties[key] as Property | undefined);
|
|
89
|
+
}
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Render one target row in the requested style. */
|
|
94
|
+
function renderTarget(
|
|
95
|
+
targetRow: Record<string, unknown>,
|
|
96
|
+
targetCollection: CollectionConfig,
|
|
97
|
+
style: RelationStyle,
|
|
98
|
+
registry: PostgresCollectionRegistry
|
|
99
|
+
): unknown {
|
|
100
|
+
if (style === "inline") {
|
|
101
|
+
// The target's columns, and only those: its address is the consumer's
|
|
102
|
+
// to derive, and merging one in overwrites a real `id` column.
|
|
103
|
+
return coerceDeclaredNumbers({ ...targetRow }, targetCollection);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const address = relationTargetAddress(targetRow, targetCollection, registry);
|
|
107
|
+
const path = targetCollection.slug;
|
|
108
|
+
return createRelationRefWithData(address, path, {
|
|
109
|
+
id: address,
|
|
110
|
+
path,
|
|
111
|
+
values: normalizeDbValues(targetRow, targetCollection)
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The address a relation ref points at.
|
|
117
|
+
*
|
|
118
|
+
* The whole key, not its first column: a composite-keyed target addressed by
|
|
119
|
+
* `tenant_id` alone points at every row that shares it. A target whose key
|
|
120
|
+
* cannot be resolved at all used to throw here — reading `[0]` of an empty
|
|
121
|
+
* array — taking down the parent's fetch over a relation it may not even have
|
|
122
|
+
* asked for. The first column is a guess, but a ref that resolves to nothing
|
|
123
|
+
* beats no rows at all.
|
|
124
|
+
*/
|
|
125
|
+
export function relationTargetAddress(
|
|
126
|
+
targetRow: Record<string, unknown>,
|
|
127
|
+
targetCollection: CollectionConfig,
|
|
128
|
+
registry: PostgresCollectionRegistry
|
|
129
|
+
): string {
|
|
130
|
+
const address = deriveRowAddress(targetRow, targetCollection, registry);
|
|
131
|
+
if (address) return address;
|
|
132
|
+
return String(targetRow[Object.keys(targetRow)[0]] ?? "");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The row the admin renders: every column, with relations as references.
|
|
137
|
+
*
|
|
138
|
+
* Values are normalized (dates, numbers, NaN) because the admin's view-model
|
|
139
|
+
* expects real types. The row's own address is *not* among the columns — it is
|
|
140
|
+
* derived by the consumer from the collection's primary keys.
|
|
141
|
+
*/
|
|
142
|
+
export function toCmsRow(
|
|
143
|
+
row: Record<string, unknown>,
|
|
144
|
+
collection: CollectionConfig,
|
|
145
|
+
registry: PostgresCollectionRegistry
|
|
146
|
+
): Record<string, unknown> {
|
|
147
|
+
const resolvedRelations = resolveCollectionRelations(collection);
|
|
148
|
+
const normalized = normalizeDbValues(row, collection) as Record<string, unknown>;
|
|
149
|
+
|
|
150
|
+
// Keyed by relation, reading the drizzle relation name off the raw row: the
|
|
151
|
+
// relation's property key and the name drizzle nested it under are not
|
|
152
|
+
// always the same, and the value is written back under the property key.
|
|
153
|
+
for (const [key, relation] of Object.entries(resolvedRelations)) {
|
|
154
|
+
const relData = row[relation.relationName || key];
|
|
155
|
+
if (relData === undefined || relData === null) continue;
|
|
156
|
+
|
|
157
|
+
if (relation.cardinality === "many" && Array.isArray(relData)) {
|
|
158
|
+
const targetCollection = relation.target();
|
|
159
|
+
normalized[key] = relData.map((item: Record<string, unknown>) =>
|
|
160
|
+
renderTarget(
|
|
161
|
+
isJunctionRelation(relation) ? unwrapJunctionRow(item) : item,
|
|
162
|
+
targetCollection,
|
|
163
|
+
"ref",
|
|
164
|
+
registry
|
|
165
|
+
));
|
|
166
|
+
} else if (relation.cardinality === "one" && typeof relData === "object" && !Array.isArray(relData)) {
|
|
167
|
+
normalized[key] = renderTarget(relData as Record<string, unknown>, relation.target(), "ref", registry);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return normalized;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* The row REST serves: every column under its own name, with the value Postgres
|
|
176
|
+
* returned, and relations inlined as the target's columns.
|
|
177
|
+
*
|
|
178
|
+
* Values are the ones the database returned, except where that contradicts the
|
|
179
|
+
* declared type: a `number` property is served as a number (see
|
|
180
|
+
* {@link coerceDeclaredNumber}). Dates stay as the database returned them —
|
|
181
|
+
* JSON has its own opinions about dates that the admin's view-model does not
|
|
182
|
+
* share.
|
|
183
|
+
*
|
|
184
|
+
* Keyed by the row rather than by the relation list — a REST fetch only loads
|
|
185
|
+
* the relations `include` asked for, so the row is the authority on which are
|
|
186
|
+
* actually there.
|
|
187
|
+
*/
|
|
188
|
+
export function toRestRow(
|
|
189
|
+
row: Record<string, unknown>,
|
|
190
|
+
collection: CollectionConfig,
|
|
191
|
+
registry: PostgresCollectionRegistry
|
|
192
|
+
): Record<string, unknown> {
|
|
193
|
+
const resolvedRelations = resolveCollectionRelations(collection);
|
|
194
|
+
const flat: Record<string, unknown> = {};
|
|
195
|
+
|
|
196
|
+
for (const [key, value] of Object.entries(row)) {
|
|
197
|
+
const relation = findRelation(resolvedRelations, key);
|
|
198
|
+
|
|
199
|
+
if (relation && Array.isArray(value)) {
|
|
200
|
+
flat[key] = value.map((item: Record<string, unknown>) =>
|
|
201
|
+
renderTarget(
|
|
202
|
+
isJunctionRelation(relation) ? unwrapJunctionRow(item) : item,
|
|
203
|
+
relation.target(),
|
|
204
|
+
"inline",
|
|
205
|
+
registry
|
|
206
|
+
));
|
|
207
|
+
} else if (relation && typeof value === "object" && value !== null) {
|
|
208
|
+
flat[key] = renderTarget(value as Record<string, unknown>, relation.target(), "inline", registry);
|
|
209
|
+
} else {
|
|
210
|
+
flat[key] = coerceDeclaredNumber(value, collection.properties?.[key] as Property | undefined);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return flat;
|
|
215
|
+
}
|
|
@@ -1126,6 +1126,19 @@ export class DrizzleConditionBuilder {
|
|
|
1126
1126
|
throw new Error(`Vector column '${vectorSearch.property}' not found in table`);
|
|
1127
1127
|
}
|
|
1128
1128
|
|
|
1129
|
+
// The vector is interpolated as a raw SQL literal below (pgvector has no
|
|
1130
|
+
// bind form for the `::vector` cast), so every element must be a finite
|
|
1131
|
+
// number. The REST query parser already enforces this, but this builder
|
|
1132
|
+
// is a shared entry point — validate here too so no future caller can
|
|
1133
|
+
// turn an unchecked value into SQL injection.
|
|
1134
|
+
if (
|
|
1135
|
+
!Array.isArray(vectorSearch.vector) ||
|
|
1136
|
+
vectorSearch.vector.length === 0 ||
|
|
1137
|
+
!vectorSearch.vector.every((n) => typeof n === "number" && Number.isFinite(n))
|
|
1138
|
+
) {
|
|
1139
|
+
throw new Error("Vector search requires a non-empty array of finite numbers");
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1129
1142
|
const vectorLiteral = `'[${vectorSearch.vector.join(",")}]'::vector`;
|
|
1130
1143
|
const distanceFn = vectorSearch.distance || "cosine";
|
|
1131
1144
|
|
package/src/websocket.ts
CHANGED
|
@@ -620,9 +620,12 @@ roles: ["anon"] };
|
|
|
620
620
|
if (error instanceof Error) {
|
|
621
621
|
logger.error("Stack trace", { detail: error.stack });
|
|
622
622
|
}
|
|
623
|
+
// Unwrap the cause chain: a Drizzle failure reports itself as
|
|
624
|
+
// "Failed query: <sql> params:", which tells the user nothing and
|
|
625
|
+
// echoes the statement back at them. The reason is in the cause.
|
|
623
626
|
const errorMessage = process.env.NODE_ENV === "production"
|
|
624
627
|
? "An unexpected error occurred"
|
|
625
|
-
: (error
|
|
628
|
+
: extractErrorMessage(error);
|
|
626
629
|
const errorResponse = {
|
|
627
630
|
type: "ERROR",
|
|
628
631
|
requestId,
|
|
@@ -1,10 +0,0 @@
|
|
|
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[];
|