@rebasepro/server-postgres 0.9.1-canary.7dddf96 → 0.9.1-canary.a57c262
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/index.es.js +253 -212
- package/dist/index.es.js.map +1 -1
- package/dist/services/FetchService.d.ts +4 -38
- package/dist/services/RelationService.d.ts +34 -0
- package/dist/services/collection-helpers.d.ts +34 -7
- package/dist/services/row-pipeline.d.ts +60 -0
- package/package.json +10 -7
- package/src/schema/introspect-db.ts +8 -1
- package/src/services/FetchService.ts +29 -163
- package/src/services/RelationService.ts +153 -93
- package/src/services/collection-helpers.ts +59 -27
- package/src/services/realtimeService.ts +4 -2
- package/src/services/row-pipeline.ts +176 -0
|
@@ -40,37 +40,11 @@ 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
|
-
* The address a relation ref points at.
|
|
54
|
-
*
|
|
55
|
-
* The whole key, not its first column: a composite-keyed target addressed
|
|
56
|
-
* by `tenant_id` alone points at every row that shares it. And a target
|
|
57
|
-
* whose key cannot be resolved at all used to throw here — reading
|
|
58
|
-
* `targetPks[0]` of an empty array — taking down the parent's fetch over a
|
|
59
|
-
* relation it may not even have asked for; the first column is a guess, but
|
|
60
|
-
* a ref that resolves to nothing beats no rows at all.
|
|
61
|
-
*/
|
|
62
|
-
private relationTargetAddress;
|
|
63
|
-
/**
|
|
64
|
-
* Convert a db.query result row (with nested relation objects) to a flat row.
|
|
65
|
-
* Handles:
|
|
66
|
-
* - Type normalization (dates, numbers, NaN) via normalizeDbValues
|
|
67
|
-
* - Converting nested relation objects to { id, path, __type: "relation" } for CMS
|
|
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.
|
|
72
|
-
*/
|
|
73
|
-
private drizzleResultToRow;
|
|
74
48
|
/**
|
|
75
49
|
* Post-fetch joinPath relations for a single flat row.
|
|
76
50
|
* joinPath relations cannot be expressed via Drizzle's `with` config,
|
|
@@ -82,16 +56,6 @@ export declare class FetchService {
|
|
|
82
56
|
* Uses RelationService to query the database and maps results back to the flattened objects.
|
|
83
57
|
*/
|
|
84
58
|
private resolveJoinPathRelationsBatchRest;
|
|
85
|
-
/**
|
|
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.
|
|
93
|
-
*/
|
|
94
|
-
private drizzleResultToRestRow;
|
|
95
59
|
/**
|
|
96
60
|
* Build db.query-compatible options from standard fetch options.
|
|
97
61
|
* Handles filter, search, orderBy, limit, and cursor-based pagination.
|
|
@@ -122,8 +86,10 @@ export declare class FetchService {
|
|
|
122
86
|
}): Promise<Record<string, unknown>[]>;
|
|
123
87
|
/**
|
|
124
88
|
* Fallback path used when db.query is unavailable.
|
|
125
|
-
*
|
|
126
|
-
*
|
|
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.
|
|
127
93
|
*
|
|
128
94
|
* Process raw database results into flat rows with relations.
|
|
129
95
|
*/
|
|
@@ -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
|
*/
|
|
@@ -24,7 +24,37 @@ export interface DrizzleColumnMeta {
|
|
|
24
24
|
export declare function getColumnMeta(col: AnyPgColumn): DrizzleColumnMeta;
|
|
25
25
|
export declare function getCollectionByPath(collectionPath: string, registry: PostgresCollectionRegistry): CollectionConfig;
|
|
26
26
|
export declare function getTableForCollection(collection: CollectionConfig, registry: PostgresCollectionRegistry): PgTable<any>;
|
|
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
|
+
*/
|
|
27
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[];
|
|
28
58
|
/**
|
|
29
59
|
* Collections whose key the *browser* cannot resolve, and what it will do
|
|
30
60
|
* instead.
|
|
@@ -65,12 +95,9 @@ export declare function warnOnKeysTheAdminCannotResolve(collections: CollectionC
|
|
|
65
95
|
* The address of a row: derived from the collection's primary keys, because a
|
|
66
96
|
* row does not carry one — it is exactly its columns.
|
|
67
97
|
*
|
|
68
|
-
* Falls back to a literal `id` column,
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* (
|
|
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).
|
|
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).
|
|
75
102
|
*/
|
|
76
103
|
export declare function deriveRowAddress(row: Record<string, unknown>, collection: CollectionConfig, registry: PostgresCollectionRegistry): string;
|
|
@@ -0,0 +1,60 @@
|
|
|
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
|
+
* Deliberately not normalized: REST serves what the database holds, and JSON
|
|
54
|
+
* has its own opinions about dates that the admin's view-model does not share.
|
|
55
|
+
*
|
|
56
|
+
* Keyed by the row rather than by the relation list — a REST fetch only loads
|
|
57
|
+
* the relations `include` asked for, so the row is the authority on which are
|
|
58
|
+
* actually there.
|
|
59
|
+
*/
|
|
60
|
+
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.
|
|
4
|
+
"version": "0.9.1-canary.a57c262",
|
|
5
5
|
"description": "PostgreSQL data source backend implementation for Rebase with Drizzle ORM",
|
|
6
6
|
"funding": {
|
|
7
7
|
"url": "https://github.com/sponsors/rebaseco"
|
|
@@ -71,18 +71,20 @@
|
|
|
71
71
|
"execa": "^9.6.1",
|
|
72
72
|
"pg": "^8.21.0",
|
|
73
73
|
"ws": "^8.21.0",
|
|
74
|
-
"@rebasepro/
|
|
75
|
-
"@rebasepro/
|
|
76
|
-
"@rebasepro/
|
|
77
|
-
"@rebasepro/
|
|
78
|
-
"@rebasepro/
|
|
74
|
+
"@rebasepro/codegen": "0.9.1-canary.a57c262",
|
|
75
|
+
"@rebasepro/common": "0.9.1-canary.a57c262",
|
|
76
|
+
"@rebasepro/utils": "0.9.1-canary.a57c262",
|
|
77
|
+
"@rebasepro/server": "0.9.1-canary.a57c262",
|
|
78
|
+
"@rebasepro/types": "0.9.1-canary.a57c262"
|
|
79
79
|
},
|
|
80
80
|
"devDependencies": {
|
|
81
|
+
"@hono/node-server": "^2.0.9",
|
|
81
82
|
"@types/jest": "^30.0.0",
|
|
82
83
|
"@types/node": "^25.9.3",
|
|
83
84
|
"@types/pg": "^8.20.0",
|
|
84
85
|
"@types/ws": "^8.18.1",
|
|
85
86
|
"@vitejs/plugin-react": "^6.0.2",
|
|
87
|
+
"hono": "^4.12.25",
|
|
86
88
|
"jest": "^30.4.2",
|
|
87
89
|
"ts-jest": "^29.4.11",
|
|
88
90
|
"typescript": "^6.0.3",
|
|
@@ -103,6 +105,7 @@
|
|
|
103
105
|
"test:lint": "eslint \"src/**\" --quiet",
|
|
104
106
|
"test": "jest --passWithNoTests",
|
|
105
107
|
"test:e2e": "vitest run --config vitest.e2e.config.ts",
|
|
106
|
-
"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"
|
|
107
110
|
}
|
|
108
111
|
}
|
|
@@ -30,6 +30,7 @@ async function main() {
|
|
|
30
30
|
"--force": Boolean,
|
|
31
31
|
"--schema": String,
|
|
32
32
|
"--data-inference": Boolean,
|
|
33
|
+
"--no-data-inference": Boolean,
|
|
33
34
|
"-o": "--output",
|
|
34
35
|
"-c": "--collections",
|
|
35
36
|
"-f": "--force"
|
|
@@ -166,8 +167,14 @@ async function main() {
|
|
|
166
167
|
logger.info(chalk.blue(`Found ${tablesMap.size} tables (including ${joinTables.size} detected join tables).`));
|
|
167
168
|
|
|
168
169
|
let runDataInference = false;
|
|
169
|
-
if (args["--data-inference"]
|
|
170
|
+
if (args["--no-data-inference"]) {
|
|
171
|
+
runDataInference = false;
|
|
172
|
+
} else if (args["--data-inference"] !== undefined) {
|
|
170
173
|
runDataInference = args["--data-inference"];
|
|
174
|
+
} else if (!process.stdin.isTTY) {
|
|
175
|
+
// No terminal to answer the question below (scaffolding scripts, CI,
|
|
176
|
+
// `rebase init --introspect`) — asking would hang forever.
|
|
177
|
+
logger.info(chalk.gray("Skipping data inference (non-interactive run; pass --data-inference to enable)."));
|
|
171
178
|
} else {
|
|
172
179
|
const rl = readline.createInterface({
|
|
173
180
|
input: process.stdin,
|
|
@@ -7,16 +7,18 @@ import { DrizzleConditionBuilder } from "../utils/drizzle-conditions";
|
|
|
7
7
|
import {
|
|
8
8
|
getCollectionByPath,
|
|
9
9
|
getTableForCollection,
|
|
10
|
-
|
|
10
|
+
requirePrimaryKeys,
|
|
11
11
|
deriveRowAddress,
|
|
12
12
|
parseIdValues,
|
|
13
|
-
buildCompositeId
|
|
13
|
+
buildCompositeId,
|
|
14
|
+
COMPOSITE_ID_SEPARATOR
|
|
14
15
|
} from "./collection-helpers";
|
|
15
16
|
import { parseDataFromServer, normalizeDbValues } from "../data-transformer";
|
|
16
17
|
import { RelationService } from "./RelationService";
|
|
17
18
|
import { RelationalQueryBuilder } from "drizzle-orm/pg-core/query-builders/query";
|
|
18
19
|
import { DrizzleClient } from "../interfaces";
|
|
19
20
|
import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry";
|
|
21
|
+
import { toCmsRow, toRestRow, isJunctionRelation } from "./row-pipeline";
|
|
20
22
|
import { logger } from "@rebasepro/server";
|
|
21
23
|
|
|
22
24
|
/** Type-safe accessor for Drizzle's relational query API via dynamic table name */
|
|
@@ -110,7 +112,7 @@ export class FetchService {
|
|
|
110
112
|
// Detect many-to-many junction tables:
|
|
111
113
|
// If the relation goes through a junction table (relation.through exists or
|
|
112
114
|
// the Drizzle schema maps to a junction table), we need two-level with.
|
|
113
|
-
if (relation.cardinality === "many" &&
|
|
115
|
+
if (relation.cardinality === "many" && isJunctionRelation(relation)) {
|
|
114
116
|
// The Drizzle relation points to the junction table.
|
|
115
117
|
// We need: { [junctionRelName]: { with: { [targetFkName]: true } } }
|
|
116
118
|
// The target FK name is the relation on the junction table that points to the actual target.
|
|
@@ -128,17 +130,6 @@ export class FetchService {
|
|
|
128
130
|
return withConfig;
|
|
129
131
|
}
|
|
130
132
|
|
|
131
|
-
/**
|
|
132
|
-
* Detect if a many-to-many relation uses a junction table in the Drizzle schema.
|
|
133
|
-
*/
|
|
134
|
-
private isJunctionRelation(relation: Relation, _collection: CollectionConfig): boolean {
|
|
135
|
-
// If `through` is defined, it's explicitly a junction relation
|
|
136
|
-
if (relation.through) return true;
|
|
137
|
-
// If joinPath has an intermediate table, it's likely junction-based
|
|
138
|
-
if (relation.joinPath && relation.joinPath.length > 1) return true;
|
|
139
|
-
return false;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
133
|
/**
|
|
143
134
|
* Get the Drizzle relation name on the junction table that points to the actual target row.
|
|
144
135
|
* For example, for posts_tags junction, this returns "tag_id" (the relation pointing to tags).
|
|
@@ -152,98 +143,6 @@ export class FetchService {
|
|
|
152
143
|
return null;
|
|
153
144
|
}
|
|
154
145
|
|
|
155
|
-
/**
|
|
156
|
-
* The address a relation ref points at.
|
|
157
|
-
*
|
|
158
|
-
* The whole key, not its first column: a composite-keyed target addressed
|
|
159
|
-
* by `tenant_id` alone points at every row that shares it. And a target
|
|
160
|
-
* whose key cannot be resolved at all used to throw here — reading
|
|
161
|
-
* `targetPks[0]` of an empty array — taking down the parent's fetch over a
|
|
162
|
-
* relation it may not even have asked for; the first column is a guess, but
|
|
163
|
-
* a ref that resolves to nothing beats no rows at all.
|
|
164
|
-
*/
|
|
165
|
-
private relationTargetAddress(targetRow: Record<string, unknown>, targetCollection: CollectionConfig): string {
|
|
166
|
-
const address = deriveRowAddress(targetRow, targetCollection, this.registry);
|
|
167
|
-
if (address) return address;
|
|
168
|
-
const firstColumn = targetRow[Object.keys(targetRow)[0]];
|
|
169
|
-
return String(firstColumn ?? "");
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Convert a db.query result row (with nested relation objects) to a flat row.
|
|
174
|
-
* Handles:
|
|
175
|
-
* - Type normalization (dates, numbers, NaN) via normalizeDbValues
|
|
176
|
-
* - Converting nested relation objects to { id, path, __type: "relation" } for CMS
|
|
177
|
-
* - Flattening junction-table many-to-many results
|
|
178
|
-
*
|
|
179
|
-
* The row's own address is not among them: it is derived by the consumer
|
|
180
|
-
* from the collection's primary keys.
|
|
181
|
-
*/
|
|
182
|
-
private drizzleResultToRow<M extends Record<string, unknown>>(
|
|
183
|
-
row: Record<string, unknown>,
|
|
184
|
-
collection: CollectionConfig
|
|
185
|
-
): Record<string, unknown> {
|
|
186
|
-
const resolvedRelations = resolveCollectionRelations(collection);
|
|
187
|
-
|
|
188
|
-
// Normalize non-relation values (dates, numbers, etc.)
|
|
189
|
-
const normalizedValues = normalizeDbValues(row as M, collection) as Record<string, unknown>;
|
|
190
|
-
|
|
191
|
-
// Convert nested relation objects to CMS-style { id, path, __type: "relation" }
|
|
192
|
-
for (const [key, relation] of Object.entries(resolvedRelations)) {
|
|
193
|
-
const drizzleRelName = relation.relationName || key;
|
|
194
|
-
const relData = row[drizzleRelName];
|
|
195
|
-
|
|
196
|
-
if (relData === undefined || relData === null) continue;
|
|
197
|
-
|
|
198
|
-
if (relation.cardinality === "many" && Array.isArray(relData)) {
|
|
199
|
-
const targetCollection = relation.target();
|
|
200
|
-
const targetPath = targetCollection.slug;
|
|
201
|
-
|
|
202
|
-
normalizedValues[key] = relData.map((item: Record<string, unknown>) => {
|
|
203
|
-
// Handle junction table flattening:
|
|
204
|
-
// Junction rows look like { post_id: 1, tag_id: { id: 5, name: "ts" } }
|
|
205
|
-
let targetRow = item;
|
|
206
|
-
if (this.isJunctionRelation(relation, collection)) {
|
|
207
|
-
// Find the nested target object in the junction row
|
|
208
|
-
const nestedKey = Object.keys(item).find(
|
|
209
|
-
nk => typeof item[nk] === "object" && item[nk] !== null && !Array.isArray(item[nk])
|
|
210
|
-
);
|
|
211
|
-
if (nestedKey) {
|
|
212
|
-
targetRow = item[nestedKey] as Record<string, unknown>;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const relId = this.relationTargetAddress(targetRow, targetCollection);
|
|
217
|
-
const targetValues = normalizeDbValues(targetRow, targetCollection);
|
|
218
|
-
|
|
219
|
-
return createRelationRefWithData(relId, targetPath, {
|
|
220
|
-
id: relId,
|
|
221
|
-
path: targetPath,
|
|
222
|
-
values: targetValues
|
|
223
|
-
});
|
|
224
|
-
});
|
|
225
|
-
} else if (relation.cardinality === "one" && typeof relData === "object" && !Array.isArray(relData)) {
|
|
226
|
-
const targetCollection = relation.target();
|
|
227
|
-
const targetPath = targetCollection.slug;
|
|
228
|
-
const relObj = relData as Record<string, unknown>;
|
|
229
|
-
|
|
230
|
-
const relId = this.relationTargetAddress(relObj, targetCollection);
|
|
231
|
-
const targetValues = normalizeDbValues(relObj, targetCollection);
|
|
232
|
-
|
|
233
|
-
normalizedValues[key] = createRelationRefWithData(relId, targetPath, {
|
|
234
|
-
id: relId,
|
|
235
|
-
path: targetPath,
|
|
236
|
-
values: targetValues
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// A row is exactly its columns. The address is derived by the consumer
|
|
242
|
-
// from the collection's primary keys — writing it here would rename the
|
|
243
|
-
// key column (`sku` → `id`) and restringify it (`42` → `"42"`).
|
|
244
|
-
return normalizedValues;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
146
|
/**
|
|
248
147
|
* Post-fetch joinPath relations for a single flat row.
|
|
249
148
|
* joinPath relations cannot be expressed via Drizzle's `with` config,
|
|
@@ -308,13 +207,19 @@ export class FetchService {
|
|
|
308
207
|
|
|
309
208
|
if (joinPathRelations.length === 0) return;
|
|
310
209
|
|
|
311
|
-
|
|
312
|
-
//
|
|
313
|
-
// straight off them. It used to be parsed back out of a synthesized
|
|
210
|
+
// These rows carry their key columns verbatim, so the parent's address
|
|
211
|
+
// is derived from them. It used to be parsed back out of a synthesized
|
|
314
212
|
// `id` — which no longer exists on a row, and threw (composite: parts
|
|
315
213
|
// mismatch; numeric: NaN) into the catch below, where a warning is all
|
|
316
214
|
// that separates "no relations" from "relations dropped".
|
|
317
|
-
|
|
215
|
+
//
|
|
216
|
+
// The whole key, because that is what the batch groups its results by:
|
|
217
|
+
// both sides derive the token the same way, so they agree by
|
|
218
|
+
// construction rather than by both happening to pick column zero.
|
|
219
|
+
const parentIdOf = (row: Record<string, unknown>): string | undefined => {
|
|
220
|
+
const address = buildCompositeId(row, idInfoArray);
|
|
221
|
+
return address && address.split(COMPOSITE_ID_SEPARATOR).some(part => part !== "") ? address : undefined;
|
|
222
|
+
};
|
|
318
223
|
|
|
319
224
|
for (const [key, relation] of joinPathRelations) {
|
|
320
225
|
try {
|
|
@@ -355,47 +260,6 @@ export class FetchService {
|
|
|
355
260
|
}
|
|
356
261
|
}
|
|
357
262
|
|
|
358
|
-
/**
|
|
359
|
-
* Convert a db.query result row to a flat REST-style row with populated relations.
|
|
360
|
-
*
|
|
361
|
-
* Every column is copied through under its own name, with the value Postgres
|
|
362
|
-
* returned. This used to open with a synthesized `id` and then skip the key
|
|
363
|
-
* column, which renamed it (a `sku` primary key was served as `id`, and `sku`
|
|
364
|
-
* did not appear at all) and restringified it (`42` → `"42"`). Consumers that
|
|
365
|
-
* need an address derive it from the collection's primary keys.
|
|
366
|
-
*/
|
|
367
|
-
private drizzleResultToRestRow(
|
|
368
|
-
row: Record<string, unknown>,
|
|
369
|
-
collection: CollectionConfig
|
|
370
|
-
): Record<string, unknown> {
|
|
371
|
-
const flat: Record<string, unknown> = {};
|
|
372
|
-
const resolvedRelations = resolveCollectionRelations(collection);
|
|
373
|
-
|
|
374
|
-
for (const [k, v] of Object.entries(row)) {
|
|
375
|
-
const relation = findRelation(resolvedRelations, k);
|
|
376
|
-
if (Array.isArray(v) && relation) {
|
|
377
|
-
// Many relation — inline each nested row, unwrapping junction rows
|
|
378
|
-
flat[k] = v.map((item: Record<string, unknown>) => {
|
|
379
|
-
if (this.isJunctionRelation(relation, collection)) {
|
|
380
|
-
const nestedKey = Object.keys(item).find(
|
|
381
|
-
nk => typeof item[nk] === "object" && item[nk] !== null && !Array.isArray(item[nk])
|
|
382
|
-
);
|
|
383
|
-
if (nestedKey) {
|
|
384
|
-
return { ...(item[nestedKey] as Record<string, unknown>) };
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
return { ...item };
|
|
388
|
-
});
|
|
389
|
-
} else if (typeof v === "object" && v !== null && !Array.isArray(v) && relation) {
|
|
390
|
-
// One-to-one relation — inline the target's columns
|
|
391
|
-
flat[k] = { ...(v as Record<string, unknown>) };
|
|
392
|
-
} else {
|
|
393
|
-
flat[k] = v;
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
return flat;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
263
|
/**
|
|
400
264
|
* Build db.query-compatible options from standard fetch options.
|
|
401
265
|
* Handles filter, search, orderBy, limit, and cursor-based pagination.
|
|
@@ -537,7 +401,7 @@ export class FetchService {
|
|
|
537
401
|
): Promise<Record<string, unknown> | undefined> {
|
|
538
402
|
const collection = getCollectionByPath(collectionPath, this.registry);
|
|
539
403
|
const table = getTableForCollection(collection, this.registry);
|
|
540
|
-
const idInfoArray =
|
|
404
|
+
const idInfoArray = requirePrimaryKeys(collection, this.registry);
|
|
541
405
|
const idInfo = idInfoArray[0];
|
|
542
406
|
const idField = table[idInfo.fieldName as keyof typeof table] as AnyPgColumn;
|
|
543
407
|
|
|
@@ -564,7 +428,7 @@ export class FetchService {
|
|
|
564
428
|
|
|
565
429
|
if (!row) return undefined;
|
|
566
430
|
|
|
567
|
-
const flatRow =
|
|
431
|
+
const flatRow = toCmsRow(row, collection, this.registry);
|
|
568
432
|
|
|
569
433
|
// Post-fetch joinPath relations that Drizzle's `with` can't express
|
|
570
434
|
await this.resolveJoinPathRelations<M>(flatRow, collection, collectionPath, parsedId, databaseId);
|
|
@@ -656,7 +520,7 @@ export class FetchService {
|
|
|
656
520
|
): Promise<Record<string, unknown>[]> {
|
|
657
521
|
const collection = getCollectionByPath(collectionPath, this.registry);
|
|
658
522
|
const table = getTableForCollection(collection, this.registry);
|
|
659
|
-
const idInfoArray =
|
|
523
|
+
const idInfoArray = requirePrimaryKeys(collection, this.registry);
|
|
660
524
|
const idInfo = idInfoArray[0];
|
|
661
525
|
const idField = table[idInfo.fieldName as keyof typeof table] as AnyPgColumn;
|
|
662
526
|
|
|
@@ -688,7 +552,7 @@ export class FetchService {
|
|
|
688
552
|
const results = await qb.findMany(queryOpts as Parameters<NonNullable<typeof qb>["findMany"]>[0]);
|
|
689
553
|
|
|
690
554
|
const rows = (results as Record<string, unknown>[]).map(row =>
|
|
691
|
-
|
|
555
|
+
toCmsRow(row, collection, this.registry)
|
|
692
556
|
);
|
|
693
557
|
|
|
694
558
|
return rows;
|
|
@@ -788,8 +652,10 @@ _distance: vectorMeta.distanceSelect }).from(table).$dynamic()
|
|
|
788
652
|
|
|
789
653
|
/**
|
|
790
654
|
* Fallback path used when db.query is unavailable.
|
|
791
|
-
*
|
|
792
|
-
*
|
|
655
|
+
*
|
|
656
|
+
* The primary path runs the results through `toCmsRow`, which maps
|
|
657
|
+
* relations from what drizzle already nested — no query per row. This one
|
|
658
|
+
* has no nesting to read, so it resolves relations itself, in batches.
|
|
793
659
|
*
|
|
794
660
|
* Process raw database results into flat rows with relations.
|
|
795
661
|
*/
|
|
@@ -1093,7 +959,7 @@ _distance: vectorMeta.distanceSelect }).from(table).$dynamic()
|
|
|
1093
959
|
|
|
1094
960
|
const collection = getCollectionByPath(collectionPath, this.registry);
|
|
1095
961
|
const table = getTableForCollection(collection, this.registry);
|
|
1096
|
-
const idInfoArray =
|
|
962
|
+
const idInfoArray = requirePrimaryKeys(collection, this.registry);
|
|
1097
963
|
const idInfo = idInfoArray[0];
|
|
1098
964
|
const idField = table[idInfo.fieldName as keyof typeof table] as AnyPgColumn;
|
|
1099
965
|
const field = table[fieldName as keyof typeof table] as AnyPgColumn;
|
|
@@ -1153,7 +1019,7 @@ _distance: vectorMeta.distanceSelect }).from(table).$dynamic()
|
|
|
1153
1019
|
): Promise<Record<string, unknown>[]> {
|
|
1154
1020
|
const collection = getCollectionByPath(collectionPath, this.registry);
|
|
1155
1021
|
const table = getTableForCollection(collection, this.registry);
|
|
1156
|
-
const idInfoArray =
|
|
1022
|
+
const idInfoArray = requirePrimaryKeys(collection, this.registry);
|
|
1157
1023
|
const idInfo = idInfoArray[0];
|
|
1158
1024
|
const idField = table[idInfo.fieldName as keyof typeof table] as AnyPgColumn;
|
|
1159
1025
|
|
|
@@ -1180,7 +1046,7 @@ _distance: vectorMeta.distanceSelect }).from(table).$dynamic()
|
|
|
1180
1046
|
const results = await qb.findMany(queryOpts as Parameters<NonNullable<typeof qb>["findMany"]>[0]);
|
|
1181
1047
|
|
|
1182
1048
|
const restRows = (results as Record<string, unknown>[]).map(row =>
|
|
1183
|
-
|
|
1049
|
+
toRestRow(row, collection, this.registry)
|
|
1184
1050
|
);
|
|
1185
1051
|
|
|
1186
1052
|
// Drizzle relational query API doesn't resolve joinPath relations, fetch manually
|
|
@@ -1259,7 +1125,7 @@ _distance: vectorMeta.distanceSelect }).from(table).$dynamic()
|
|
|
1259
1125
|
): Promise<Record<string, unknown> | null> {
|
|
1260
1126
|
const collection = getCollectionByPath(collectionPath, this.registry);
|
|
1261
1127
|
const table = getTableForCollection(collection, this.registry);
|
|
1262
|
-
const idInfoArray =
|
|
1128
|
+
const idInfoArray = requirePrimaryKeys(collection, this.registry);
|
|
1263
1129
|
const idInfo = idInfoArray[0];
|
|
1264
1130
|
const idField = table[idInfo.fieldName as keyof typeof table] as AnyPgColumn;
|
|
1265
1131
|
|
|
@@ -1285,7 +1151,7 @@ _distance: vectorMeta.distanceSelect }).from(table).$dynamic()
|
|
|
1285
1151
|
|
|
1286
1152
|
if (!row) return null;
|
|
1287
1153
|
|
|
1288
|
-
const restRow =
|
|
1154
|
+
const restRow = toRestRow(row, collection, this.registry);
|
|
1289
1155
|
|
|
1290
1156
|
// Drizzle relational query API doesn't resolve joinPath relations, fetch manually
|
|
1291
1157
|
await this.resolveJoinPathRelationsBatchRest([restRow], collection, collectionPath, idInfoArray, include);
|
|
@@ -1367,7 +1233,7 @@ _distance: vectorMeta.distanceSelect }).from(table).$dynamic()
|
|
|
1367
1233
|
): Promise<Record<string, unknown>[]> {
|
|
1368
1234
|
const collection = getCollectionByPath(collectionPath, this.registry);
|
|
1369
1235
|
const table = getTableForCollection(collection, this.registry);
|
|
1370
|
-
const idInfoArray =
|
|
1236
|
+
const idInfoArray = requirePrimaryKeys(collection, this.registry);
|
|
1371
1237
|
const idInfo = idInfoArray[0];
|
|
1372
1238
|
const idField = table[idInfo.fieldName as keyof typeof table] as AnyPgColumn;
|
|
1373
1239
|
|