@palbase/backend 25.0.4 → 27.0.0
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/bin/palbase-backend.cjs +2622 -1012
- package/dist/bin/palbase-backend.cjs.map +1 -1
- package/dist/bin/palbase-backend.js +87 -51
- package/dist/bin/palbase-backend.js.map +1 -1
- package/dist/chunk-CGNN2PUH.js +213 -0
- package/dist/chunk-CGNN2PUH.js.map +1 -0
- package/dist/chunk-DRZFQRJI.js +370 -0
- package/dist/chunk-DRZFQRJI.js.map +1 -0
- package/dist/chunk-GYK6QYS4.js +708 -0
- package/dist/chunk-GYK6QYS4.js.map +1 -0
- package/dist/chunk-I3C4PFIW.js +930 -0
- package/dist/chunk-I3C4PFIW.js.map +1 -0
- package/dist/{chunk-AILPKEK5.js → chunk-OO7R25AI.js} +1433 -563
- package/dist/chunk-OO7R25AI.js.map +1 -0
- package/dist/chunk-TS4U7NBD.js +885 -0
- package/dist/chunk-TS4U7NBD.js.map +1 -0
- package/dist/{chunk-YOY5DFQS.js → chunk-TWX6JTGJ.js} +76 -34
- package/dist/{chunk-YOY5DFQS.js.map → chunk-TWX6JTGJ.js.map} +1 -1
- package/dist/{chunk-35PNTIRN.js → chunk-VVMJEVQP.js} +63 -162
- package/dist/chunk-VVMJEVQP.js.map +1 -0
- package/dist/{chunk-7D4SUZUM.js → chunk-VXPNPVAG.js} +3 -1
- package/dist/db/env.cjs.map +1 -1
- package/dist/db/env.d.cts +2 -2
- package/dist/db/env.d.ts +2 -2
- package/dist/db/index.cjs +754 -344
- package/dist/db/index.cjs.map +1 -1
- package/dist/db/index.d.cts +2 -2
- package/dist/db/index.d.ts +2 -2
- package/dist/db/index.js +7 -4
- package/dist/engine/index.cjs +2562 -981
- package/dist/engine/index.cjs.map +1 -1
- package/dist/engine/index.d.cts +6 -6
- package/dist/engine/index.d.ts +6 -6
- package/dist/engine/index.js +7 -6
- package/dist/index-BrvvxSpn.d.ts +4844 -0
- package/dist/index-Bve7BBTL.d.cts +4844 -0
- package/dist/{index-CUomTA3e.d.ts → index-NuzRCuxe.d.ts} +171 -296
- package/dist/{index-ClpDeSos.d.cts → index-VtToZmUm.d.cts} +171 -296
- package/dist/index.cjs +2694 -1169
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +165 -19
- package/dist/index.d.ts +165 -19
- package/dist/index.js +738 -477
- package/dist/index.js.map +1 -1
- package/dist/module-Dl1KFVtc.d.cts +54 -0
- package/dist/module-Dl1KFVtc.d.ts +54 -0
- package/dist/openapi/index.cjs +1330 -484
- package/dist/openapi/index.cjs.map +1 -1
- package/dist/openapi/index.d.cts +4 -2
- package/dist/openapi/index.d.ts +4 -2
- package/dist/openapi/index.js +1264 -474
- package/dist/openapi/index.js.map +1 -1
- package/dist/{registry-dZZ5JKYg.d.ts → registry-B0eyOF9x.d.ts} +1 -1
- package/dist/{registry-CC0WBQq6.d.cts → registry-Bk9_rbNd.d.cts} +1 -1
- package/dist/stack.cjs.map +1 -1
- package/dist/test/index.cjs +705 -141
- package/dist/test/index.cjs.map +1 -1
- package/dist/test/index.d.cts +30 -4
- package/dist/test/index.d.ts +30 -4
- package/dist/test/index.js +472 -124
- package/dist/test/index.js.map +1 -1
- package/docs/README.md +34 -18
- package/docs/auth.md +1 -1
- package/docs/background.md +2 -2
- package/docs/database.md +221 -50
- package/docs/endpoints.md +3 -4
- package/docs/events.md +3 -3
- package/docs/getting-started.md +1 -1
- package/docs/llms-full.txt +402 -117
- package/docs/migrations.md +2 -2
- package/docs/schema.md +19 -10
- package/docs/services.md +116 -26
- package/package.json +8 -4
- package/stager/generics.js +205 -0
- package/stager/stage.js +39 -3
- package/template/AGENTS.md +110 -70
- package/template/db/public.ts +1 -1
- package/template/{controllers → modules/health}/health.controller.ts +1 -1
- package/template/modules/health/health.module.ts +24 -0
- package/template/modules/notes/note.service.test.ts +49 -0
- package/template/modules/notes/note.service.ts +108 -0
- package/template/{controllers → modules/notes}/notes.controller.ts +17 -11
- package/template/modules/notes/notes.module.ts +37 -0
- package/template/package.json +5 -3
- package/template/scripts/test.sh +33 -0
- package/template/tsconfig.json +29 -30
- package/dist/chunk-35PNTIRN.js.map +0 -1
- package/dist/chunk-AILPKEK5.js.map +0 -1
- package/dist/chunk-CJSKYY76.js +0 -627
- package/dist/chunk-CJSKYY76.js.map +0 -1
- package/dist/chunk-CRQKCRGF.js +0 -276
- package/dist/chunk-CRQKCRGF.js.map +0 -1
- package/dist/chunk-G4R6BTLV.js +0 -662
- package/dist/chunk-G4R6BTLV.js.map +0 -1
- package/dist/chunk-XABBC7JP.js +0 -55
- package/dist/chunk-XABBC7JP.js.map +0 -1
- package/dist/endpoint-CTEHhb7A.d.ts +0 -2386
- package/dist/endpoint-DYHMo6cC.d.cts +0 -2386
- package/dist/index-CW21M9Z3.d.ts +0 -1222
- package/dist/index-CmBK76nx.d.cts +0 -1222
- package/template/services/note.service.test.ts +0 -45
- package/template/services/note.service.ts +0 -74
- /package/dist/{chunk-7D4SUZUM.js.map → chunk-VXPNPVAG.js.map} +0 -0
- /package/template/{models/notes → modules/notes/dto}/create.ts +0 -0
package/dist/chunk-CJSKYY76.js
DELETED
|
@@ -1,627 +0,0 @@
|
|
|
1
|
-
// src/db/tx-plan.ts
|
|
2
|
-
var TxRefError = class extends Error {
|
|
3
|
-
constructor(message) {
|
|
4
|
-
super(message);
|
|
5
|
-
this.name = "TxRefError";
|
|
6
|
-
}
|
|
7
|
-
};
|
|
8
|
-
var TxPlanError = class extends Error {
|
|
9
|
-
constructor(message) {
|
|
10
|
-
super(message);
|
|
11
|
-
this.name = "TxPlanError";
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
var EXPR = /* @__PURE__ */ Symbol.for("palbase.tx.expr");
|
|
15
|
-
var REF = /* @__PURE__ */ Symbol.for("palbase.tx.ref");
|
|
16
|
-
var ROW = /* @__PURE__ */ Symbol.for("palbase.tx.row");
|
|
17
|
-
var ROWS = /* @__PURE__ */ Symbol.for("palbase.tx.rows");
|
|
18
|
-
var TRAPPED_PROPS = [
|
|
19
|
-
"then",
|
|
20
|
-
"valueOf",
|
|
21
|
-
"toString",
|
|
22
|
-
"toJSON",
|
|
23
|
-
Symbol.toPrimitive
|
|
24
|
-
];
|
|
25
|
-
function trap(prop, what, hint) {
|
|
26
|
-
const name = typeof prop === "symbol" ? prop.description ?? String(prop) : prop;
|
|
27
|
-
throw new TxRefError(
|
|
28
|
-
`${what} was used as a value (via \`${name}\`). Nothing in a transaction callback has run yet, so there is no value to read. ${hint}`
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
function now() {
|
|
32
|
-
return makeExpr({ fn: "now" });
|
|
33
|
-
}
|
|
34
|
-
function inc(by) {
|
|
35
|
-
assertFiniteNumber(by, "inc");
|
|
36
|
-
return makeExpr({ fn: "inc", by });
|
|
37
|
-
}
|
|
38
|
-
function dec(by) {
|
|
39
|
-
assertFiniteNumber(by, "dec");
|
|
40
|
-
return makeExpr({ fn: "dec", by });
|
|
41
|
-
}
|
|
42
|
-
function assertFiniteNumber(by, fn) {
|
|
43
|
-
if (typeof by !== "number" || !Number.isFinite(by)) {
|
|
44
|
-
throw new TxPlanError(`${fn}() needs a finite number, got ${String(by)}`);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
function makeExpr(expr) {
|
|
48
|
-
return new Proxy(
|
|
49
|
-
{ [EXPR]: expr },
|
|
50
|
-
{
|
|
51
|
-
get(target, prop) {
|
|
52
|
-
if (prop === EXPR) return target[EXPR];
|
|
53
|
-
if (TRAPPED_PROPS.includes(prop)) {
|
|
54
|
-
trap(prop, "A plan expression", "Write it into an operation instead.");
|
|
55
|
-
}
|
|
56
|
-
return void 0;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
function makeRef(op, field) {
|
|
62
|
-
const target = { [REF]: { op, field } };
|
|
63
|
-
return new Proxy(target, {
|
|
64
|
-
get(t, prop) {
|
|
65
|
-
if (prop === REF) return t[REF];
|
|
66
|
-
if (TRAPPED_PROPS.includes(prop)) {
|
|
67
|
-
trap(
|
|
68
|
-
prop,
|
|
69
|
-
`\`${field}\` of a row this transaction has not written yet`,
|
|
70
|
-
"Pass it to another operation in the same plan, or return it from the callback and read it after `transaction()` resolves."
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
return void 0;
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
function makeRowHandle(op) {
|
|
78
|
-
const target = { [ROW]: op };
|
|
79
|
-
return new Proxy(target, {
|
|
80
|
-
get(t, prop) {
|
|
81
|
-
if (prop === ROW) return t[ROW];
|
|
82
|
-
if (TRAPPED_PROPS.includes(prop)) {
|
|
83
|
-
trap(
|
|
84
|
-
prop,
|
|
85
|
-
"A row this transaction has not written yet",
|
|
86
|
-
"Read one of its columns to reference it, or return the row from the callback and read it after `transaction()` resolves."
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
if (typeof prop === "symbol") return void 0;
|
|
90
|
-
return makeRef(op, prop);
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
function refDescriptor(v) {
|
|
95
|
-
if (typeof v !== "object" || v === null) return null;
|
|
96
|
-
const d = v[REF];
|
|
97
|
-
return isRefDescriptor(d) ? d : null;
|
|
98
|
-
}
|
|
99
|
-
function isRefDescriptor(d) {
|
|
100
|
-
return typeof d === "object" && d !== null && typeof d.op === "number" && typeof d.field === "string";
|
|
101
|
-
}
|
|
102
|
-
function rowOpIndex(v) {
|
|
103
|
-
if (typeof v !== "object" || v === null) return null;
|
|
104
|
-
const op = v[ROW];
|
|
105
|
-
return typeof op === "number" ? op : null;
|
|
106
|
-
}
|
|
107
|
-
function exprOf(v) {
|
|
108
|
-
if (typeof v !== "object" || v === null) return null;
|
|
109
|
-
const e = v[EXPR];
|
|
110
|
-
return typeof e === "object" && e !== null ? e : null;
|
|
111
|
-
}
|
|
112
|
-
function isRowsHandle(v) {
|
|
113
|
-
return typeof v === "object" && v !== null && v[ROWS] !== void 0;
|
|
114
|
-
}
|
|
115
|
-
function encodeValue(value, column, allowColumnExpr) {
|
|
116
|
-
const ref = refDescriptor(value);
|
|
117
|
-
if (ref) return { $ref: { op: ref.op, field: ref.field } };
|
|
118
|
-
const expr = exprOf(value);
|
|
119
|
-
if (expr) {
|
|
120
|
-
if (expr.fn !== "now" && !allowColumnExpr) {
|
|
121
|
-
throw new TxPlanError(
|
|
122
|
-
`\`${column}\`: ${expr.fn}() reads the column's current value, so it is only valid in updateWhere(where, set).`
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
return { $expr: expr };
|
|
126
|
-
}
|
|
127
|
-
if (rowOpIndex(value) !== null) {
|
|
128
|
-
throw new TxPlanError(
|
|
129
|
-
`\`${column}\`: a row handle is not a value. Read the column you meant (e.g. \`row.id\`).`
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
if (isRowsHandle(value)) {
|
|
133
|
-
throw new TxPlanError(
|
|
134
|
-
`\`${column}\`: an operation result is not a value. Declare an expectation first (\`.expectOne(err)\`) and read a column from the row.`
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
assertNoNestedHandles(value, column);
|
|
138
|
-
return value;
|
|
139
|
-
}
|
|
140
|
-
function assertNoNestedHandles(value, column) {
|
|
141
|
-
if (typeof value !== "object" || value === null) return;
|
|
142
|
-
if (value instanceof Date) return;
|
|
143
|
-
if (refDescriptor(value) || exprOf(value) || rowOpIndex(value) !== null || isRowsHandle(value)) {
|
|
144
|
-
throw new TxPlanError(
|
|
145
|
-
`\`${column}\`: a plan handle is nested inside a value. The server would store it as literal JSON, not resolve it. Put the reference directly in the column.`
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
if (Array.isArray(value)) {
|
|
149
|
-
for (const item of value) assertNoNestedHandles(item, column);
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
for (const item of Object.values(value)) {
|
|
153
|
-
assertNoNestedHandles(item, column);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
function encodeMap(map, allowColumnExpr) {
|
|
157
|
-
const out = {};
|
|
158
|
-
for (const key of Object.keys(map).sort()) {
|
|
159
|
-
const value = map[key];
|
|
160
|
-
if (value === void 0) continue;
|
|
161
|
-
out[key] = encodeValue(value, key, allowColumnExpr);
|
|
162
|
-
}
|
|
163
|
-
return out;
|
|
164
|
-
}
|
|
165
|
-
var SKIPPED_OP = -1;
|
|
166
|
-
var TxRowsImpl = class {
|
|
167
|
-
constructor(builder, opIndex, what) {
|
|
168
|
-
this.builder = builder;
|
|
169
|
-
this.opIndex = opIndex;
|
|
170
|
-
this.what = what;
|
|
171
|
-
}
|
|
172
|
-
builder;
|
|
173
|
-
opIndex;
|
|
174
|
-
what;
|
|
175
|
-
// Present so `isRowsHandle` recognises the object; never read for its value.
|
|
176
|
-
[ROWS] = true;
|
|
177
|
-
guarded = false;
|
|
178
|
-
// The type-level `await` guard made real: TS rejects `await rows` at compile
|
|
179
|
-
// time, and reaching this means someone called `.then(...)` by hand.
|
|
180
|
-
then() {
|
|
181
|
-
throw new TxRefError(
|
|
182
|
-
`${this.what} cannot be awaited: a transaction callback builds a plan, it does not run statements. Remove the \`await\`.`
|
|
183
|
-
);
|
|
184
|
-
}
|
|
185
|
-
expectOne(error) {
|
|
186
|
-
this.declareGuard("one", 1, error);
|
|
187
|
-
if (this.opIndex === SKIPPED_OP) throw error;
|
|
188
|
-
return makeRowHandle(this.opIndex);
|
|
189
|
-
}
|
|
190
|
-
expectNone(error) {
|
|
191
|
-
this.declareGuard("none", 0, error);
|
|
192
|
-
}
|
|
193
|
-
expectAtLeast(n, error) {
|
|
194
|
-
assertGuardCount(n, "expectAtLeast");
|
|
195
|
-
this.declareGuard("atLeast", n, error);
|
|
196
|
-
if (this.opIndex === SKIPPED_OP && n > 0) throw error;
|
|
197
|
-
}
|
|
198
|
-
expectAtMost(n, error) {
|
|
199
|
-
assertGuardCount(n, "expectAtMost");
|
|
200
|
-
this.declareGuard("atMost", n, error);
|
|
201
|
-
}
|
|
202
|
-
declareGuard(kind, n, error) {
|
|
203
|
-
if (!(error instanceof Error)) {
|
|
204
|
-
throw new TxPlanError(
|
|
205
|
-
`${this.what}: an expectation needs the Error to throw when it does not hold (e.g. \`.expect\u2026(new Conflict("already accepted"))\`).`
|
|
206
|
-
);
|
|
207
|
-
}
|
|
208
|
-
if (this.guarded) {
|
|
209
|
-
throw new TxPlanError(
|
|
210
|
-
`${this.what} already has an expectation. One operation carries one expectation; declare the second one on its own operation.`
|
|
211
|
-
);
|
|
212
|
-
}
|
|
213
|
-
this.guarded = true;
|
|
214
|
-
if (this.opIndex === SKIPPED_OP) return;
|
|
215
|
-
this.builder.attachGuard(this.opIndex, kind, n, error);
|
|
216
|
-
}
|
|
217
|
-
};
|
|
218
|
-
function assertGuardCount(n, fn) {
|
|
219
|
-
if (!Number.isInteger(n) || n < 0) {
|
|
220
|
-
throw new TxPlanError(`${fn}(n) needs a non-negative integer, got ${String(n)}`);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
var MAX_OPS = 1e3;
|
|
224
|
-
var MAX_ROWS = 5e3;
|
|
225
|
-
var TxPlanBuilder = class {
|
|
226
|
-
ops = [];
|
|
227
|
-
/** Errors handed to expectations, indexed by the `slot` the server echoes. */
|
|
228
|
-
slots = [];
|
|
229
|
-
/** The table surface handed to the callback. Untyped here; the public
|
|
230
|
-
* `transaction()` signatures put the schema types on top. */
|
|
231
|
-
table(name) {
|
|
232
|
-
return {
|
|
233
|
-
insert: (values) => {
|
|
234
|
-
const encoded = encodeMap(values, false);
|
|
235
|
-
if (Object.keys(encoded).length === 0) {
|
|
236
|
-
throw new TxPlanError(`${name}.insert() needs at least one column`);
|
|
237
|
-
}
|
|
238
|
-
return this.push({ op: "insert", table: name, values: encoded }, `${name}.insert()`);
|
|
239
|
-
},
|
|
240
|
-
upsert: (values, options) => {
|
|
241
|
-
const encoded = encodeMap(values, false);
|
|
242
|
-
if (Object.keys(encoded).length === 0) {
|
|
243
|
-
throw new TxPlanError(`${name}.upsert() needs at least one column`);
|
|
244
|
-
}
|
|
245
|
-
if (options.onConflict.length === 0) {
|
|
246
|
-
throw new TxPlanError(`${name}.upsert() needs at least one onConflict column`);
|
|
247
|
-
}
|
|
248
|
-
return this.push(
|
|
249
|
-
{ op: "upsert", table: name, values: encoded, onConflict: options.onConflict },
|
|
250
|
-
`${name}.upsert()`
|
|
251
|
-
);
|
|
252
|
-
},
|
|
253
|
-
insertMany: (rows, opts) => {
|
|
254
|
-
if (rows.length === 0) {
|
|
255
|
-
return new TxRowsImpl(this, SKIPPED_OP, `${name}.insertMany()`);
|
|
256
|
-
}
|
|
257
|
-
if (rows.length > MAX_ROWS) {
|
|
258
|
-
throw new TxPlanError(
|
|
259
|
-
`${name}.insertMany() has ${rows.length} rows; the limit is ${MAX_ROWS}. Split the write across requests.`
|
|
260
|
-
);
|
|
261
|
-
}
|
|
262
|
-
const encoded = rows.map((row) => encodeMap(row, false));
|
|
263
|
-
assertUniformRows(encoded, name);
|
|
264
|
-
if (opts !== void 0 && opts.onConflict.length === 0) {
|
|
265
|
-
throw new TxPlanError(
|
|
266
|
-
`${name}.insertMany() was given a conflict action with no onConflict columns. Postgres matches a collision on columns, so name them.`
|
|
267
|
-
);
|
|
268
|
-
}
|
|
269
|
-
return this.push(
|
|
270
|
-
{
|
|
271
|
-
op: "insertMany",
|
|
272
|
-
table: name,
|
|
273
|
-
rows: encoded,
|
|
274
|
-
// Omitted entirely when no options were given, so the op a plain
|
|
275
|
-
// insertMany produces is byte-identical to the one it produced
|
|
276
|
-
// before this option existed.
|
|
277
|
-
...opts !== void 0 ? { onConflict: opts.onConflict, action: opts.action ?? "ignore" } : {}
|
|
278
|
-
},
|
|
279
|
-
`${name}.insertMany()`
|
|
280
|
-
);
|
|
281
|
-
},
|
|
282
|
-
updateWhere: (where, set) => {
|
|
283
|
-
const encodedWhere = encodeMap(where, false);
|
|
284
|
-
const encodedSet = encodeMap(set, true);
|
|
285
|
-
if (Object.keys(encodedWhere).length === 0) {
|
|
286
|
-
throw new TxPlanError(
|
|
287
|
-
`${name}.updateWhere() needs a filter. An update with no filter rewrites the whole table.`
|
|
288
|
-
);
|
|
289
|
-
}
|
|
290
|
-
if (Object.keys(encodedSet).length === 0) {
|
|
291
|
-
throw new TxPlanError(`${name}.updateWhere() needs at least one column to set`);
|
|
292
|
-
}
|
|
293
|
-
return this.push(
|
|
294
|
-
{ op: "update", table: name, set: encodedSet, where: encodedWhere },
|
|
295
|
-
`${name}.updateWhere()`
|
|
296
|
-
);
|
|
297
|
-
},
|
|
298
|
-
deleteWhere: (where) => {
|
|
299
|
-
const encodedWhere = encodeMap(where, false);
|
|
300
|
-
if (Object.keys(encodedWhere).length === 0) {
|
|
301
|
-
throw new TxPlanError(
|
|
302
|
-
`${name}.deleteWhere() needs a filter. A delete with no filter empties the table.`
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
return this.push(
|
|
306
|
-
{ op: "delete", table: name, where: encodedWhere },
|
|
307
|
-
`${name}.deleteWhere()`
|
|
308
|
-
);
|
|
309
|
-
},
|
|
310
|
-
select: (where, options) => {
|
|
311
|
-
const op = { op: "select", table: name };
|
|
312
|
-
const encodedWhere = encodeMap(where ?? {}, false);
|
|
313
|
-
if (Object.keys(encodedWhere).length > 0) op.where = encodedWhere;
|
|
314
|
-
if (options?.limit !== void 0) {
|
|
315
|
-
if (!Number.isInteger(options.limit) || options.limit < 0) {
|
|
316
|
-
throw new TxPlanError(
|
|
317
|
-
`${name}.select(): limit needs a non-negative integer, got ${String(options.limit)}`
|
|
318
|
-
);
|
|
319
|
-
}
|
|
320
|
-
op.limit = options.limit;
|
|
321
|
-
}
|
|
322
|
-
if (options?.lock !== void 0) op.lock = options.lock;
|
|
323
|
-
return this.push(op, `${name}.select()`);
|
|
324
|
-
}
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
push(op, what) {
|
|
328
|
-
if (this.ops.length >= MAX_OPS) {
|
|
329
|
-
throw new TxPlanError(
|
|
330
|
-
`this transaction has ${MAX_OPS} operations, which is the limit. Use insertMany() for bulk writes, or split the work across requests.`
|
|
331
|
-
);
|
|
332
|
-
}
|
|
333
|
-
const index = this.ops.length;
|
|
334
|
-
this.ops.push(op);
|
|
335
|
-
return new TxRowsImpl(this, index, what);
|
|
336
|
-
}
|
|
337
|
-
/** Attach an expectation to an op and record its error in the slot table. */
|
|
338
|
-
attachGuard(opIndex, kind, n, error) {
|
|
339
|
-
const op = this.ops[opIndex];
|
|
340
|
-
if (!op) throw new TxPlanError(`internal: expectation on unknown operation ${opIndex}`);
|
|
341
|
-
const slot = this.slots.length;
|
|
342
|
-
this.slots.push(error);
|
|
343
|
-
op.guard = { kind, n, slot };
|
|
344
|
-
}
|
|
345
|
-
/** The serialisable plan. Empty when the callback described no writes. */
|
|
346
|
-
body() {
|
|
347
|
-
return { ops: this.ops };
|
|
348
|
-
}
|
|
349
|
-
/** The error the server's `slot` selects, or `null` when it names one this
|
|
350
|
-
* plan never declared (a server/client disagreement, not a tenant error). */
|
|
351
|
-
errorForSlot(slot) {
|
|
352
|
-
return this.slots[slot] ?? null;
|
|
353
|
-
}
|
|
354
|
-
};
|
|
355
|
-
function assertUniformRows(rows, table) {
|
|
356
|
-
const first = rows[0];
|
|
357
|
-
if (!first) return;
|
|
358
|
-
const want = Object.keys(first);
|
|
359
|
-
const wantKey = want.join(",");
|
|
360
|
-
for (let i = 1; i < rows.length; i++) {
|
|
361
|
-
const got = Object.keys(rows[i]);
|
|
362
|
-
if (got.join(",") !== wantKey) {
|
|
363
|
-
throw new TxPlanError(
|
|
364
|
-
`${table}.insertMany(): every row must set the same columns. Row 0 sets [${want.join(", ")}] but row ${i} sets [${got.join(", ")}]. (A property set to \`undefined\` counts as absent \u2014 use \`null\`.)`
|
|
365
|
-
);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
function materializeResult(value, results) {
|
|
370
|
-
const ref = refDescriptor(value);
|
|
371
|
-
if (ref) {
|
|
372
|
-
const row = rowOf(results, ref.op, `\`${ref.field}\``);
|
|
373
|
-
if (!(ref.field in row)) {
|
|
374
|
-
throw new TxPlanError(
|
|
375
|
-
`the transaction's operation ${ref.op} returned no column \`${ref.field}\`.`
|
|
376
|
-
);
|
|
377
|
-
}
|
|
378
|
-
return row[ref.field];
|
|
379
|
-
}
|
|
380
|
-
const rowOp = rowOpIndex(value);
|
|
381
|
-
if (rowOp !== null) return rowOf(results, rowOp, "a row");
|
|
382
|
-
if (isRowsHandle(value)) {
|
|
383
|
-
throw new TxPlanError(
|
|
384
|
-
"an operation result cannot be returned from a transaction callback: its row count is not known until the plan runs. Declare an expectation (`.expectOne(err)`) and return the row, or a column of it."
|
|
385
|
-
);
|
|
386
|
-
}
|
|
387
|
-
if (Array.isArray(value)) return value.map((item) => materializeResult(item, results));
|
|
388
|
-
if (isPlainObject(value)) {
|
|
389
|
-
const out = {};
|
|
390
|
-
for (const [key, item] of Object.entries(value)) out[key] = materializeResult(item, results);
|
|
391
|
-
return out;
|
|
392
|
-
}
|
|
393
|
-
return value;
|
|
394
|
-
}
|
|
395
|
-
function rowOf(results, opIndex, what) {
|
|
396
|
-
const result = results[opIndex];
|
|
397
|
-
if (!result) {
|
|
398
|
-
throw new TxPlanError(
|
|
399
|
-
`the transaction returned no result for operation ${opIndex}, so ${what} cannot be read.`
|
|
400
|
-
);
|
|
401
|
-
}
|
|
402
|
-
const row = result.rows[0];
|
|
403
|
-
if (!row) {
|
|
404
|
-
throw new TxPlanError(
|
|
405
|
-
`the transaction's operation ${opIndex} returned no row, so ${what} cannot be read.`
|
|
406
|
-
);
|
|
407
|
-
}
|
|
408
|
-
return row;
|
|
409
|
-
}
|
|
410
|
-
function isPlainObject(value) {
|
|
411
|
-
if (typeof value !== "object" || value === null) return false;
|
|
412
|
-
const proto = Object.getPrototypeOf(value);
|
|
413
|
-
return proto === Object.prototype || proto === null;
|
|
414
|
-
}
|
|
415
|
-
async function runTxPlan(transport, tables, builder, fn) {
|
|
416
|
-
const returned = fn({ tables });
|
|
417
|
-
const body = builder.body();
|
|
418
|
-
if (body.ops.length === 0) {
|
|
419
|
-
return materializeResult(returned, []);
|
|
420
|
-
}
|
|
421
|
-
let response;
|
|
422
|
-
try {
|
|
423
|
-
response = await transport.txPlan(body);
|
|
424
|
-
} catch (err) {
|
|
425
|
-
throw translateRejection(err, builder);
|
|
426
|
-
}
|
|
427
|
-
return materializeResult(returned, response.results);
|
|
428
|
-
}
|
|
429
|
-
function translateRejection(err, builder) {
|
|
430
|
-
if (typeof err !== "object" || err === null) return err;
|
|
431
|
-
const rejection = err;
|
|
432
|
-
if (rejection.error_code !== "tx_guard_failed" || typeof rejection.slot !== "number") {
|
|
433
|
-
return err;
|
|
434
|
-
}
|
|
435
|
-
return builder.errorForSlot(rejection.slot) ?? err;
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
// src/db/schema-json.ts
|
|
439
|
-
function defOf(column) {
|
|
440
|
-
return "_def" in column ? column._def : column;
|
|
441
|
-
}
|
|
442
|
-
function columnToJSON(column) {
|
|
443
|
-
const def = defOf(column);
|
|
444
|
-
const out = {
|
|
445
|
-
type: def.type,
|
|
446
|
-
nullable: def.nullable,
|
|
447
|
-
primaryKey: def.primaryKey
|
|
448
|
-
};
|
|
449
|
-
if (def.defaultValue !== void 0) out.defaultValue = def.defaultValue;
|
|
450
|
-
if (def.defaultRandom === true) out.defaultRandom = true;
|
|
451
|
-
if (def.defaultNow === true) out.defaultNow = true;
|
|
452
|
-
if (def.renamedFrom !== void 0) out.renamedFrom = def.renamedFrom;
|
|
453
|
-
if (def.ignored === true) out.ignored = true;
|
|
454
|
-
if (def.owns === true) out.owns = true;
|
|
455
|
-
if (def.references !== void 0) {
|
|
456
|
-
out.references = { table: def.references.table, column: def.references.column };
|
|
457
|
-
}
|
|
458
|
-
if (def.onDeleteAction !== void 0) out.onDeleteAction = def.onDeleteAction;
|
|
459
|
-
if (def.enumName !== void 0) out.enumName = def.enumName;
|
|
460
|
-
if (def.enumValues !== void 0) out.enumValues = [...def.enumValues];
|
|
461
|
-
if (def.unique === true) out.unique = true;
|
|
462
|
-
if (def.dimensions !== void 0) out.dimensions = def.dimensions;
|
|
463
|
-
return out;
|
|
464
|
-
}
|
|
465
|
-
function policyToJSON(policy) {
|
|
466
|
-
return {
|
|
467
|
-
name: policy.name,
|
|
468
|
-
command: policy.command ?? "all",
|
|
469
|
-
roles: policy.roles ? [...policy.roles] : [],
|
|
470
|
-
// null rather than omitted: a policy with no USING clause is a different
|
|
471
|
-
// thing from one whose clause the emitter forgot, and Go reads the
|
|
472
|
-
// difference.
|
|
473
|
-
using: policy.using ?? null,
|
|
474
|
-
withCheck: policy.withCheck ?? null,
|
|
475
|
-
permissive: policy.permissive !== false
|
|
476
|
-
};
|
|
477
|
-
}
|
|
478
|
-
function commonSearchFields(search, out) {
|
|
479
|
-
if (search.synonyms !== void 0 && Object.keys(search.synonyms).length > 0) {
|
|
480
|
-
out.synonyms = Object.fromEntries(
|
|
481
|
-
Object.entries(search.synonyms).map(([word, alts]) => [word, [...alts]])
|
|
482
|
-
);
|
|
483
|
-
}
|
|
484
|
-
if (search.validity === true) out.validity = true;
|
|
485
|
-
}
|
|
486
|
-
function searchToJSON(search, vectorColumn) {
|
|
487
|
-
if (search.from !== void 0 && search.model !== void 0) {
|
|
488
|
-
const out2 = {};
|
|
489
|
-
const textCols = search.text === false ? void 0 : Array.isArray(search.text) && search.text.length > 0 ? search.text : search.from;
|
|
490
|
-
if (textCols !== void 0) out2.text = { columns: [...textCols] };
|
|
491
|
-
const v = { metric: search.metric ?? "cosine" };
|
|
492
|
-
if (vectorColumn !== void 0) v.column = vectorColumn;
|
|
493
|
-
v.embed = {
|
|
494
|
-
provider: search.model.provider,
|
|
495
|
-
model: search.model.model,
|
|
496
|
-
from: [...search.from],
|
|
497
|
-
...search.model.apiKeyName !== void 0 ? { apiKeyName: search.model.apiKeyName } : {},
|
|
498
|
-
...search.model.dimensions !== void 0 ? { dimensions: search.model.dimensions } : {},
|
|
499
|
-
...search.model.baseURL !== void 0 ? { baseURL: search.model.baseURL } : {}
|
|
500
|
-
};
|
|
501
|
-
if (search.staleness !== void 0) v.staleness = search.staleness;
|
|
502
|
-
if (vectorColumn === void 0) {
|
|
503
|
-
v.mode = "chunks";
|
|
504
|
-
if (search.chunks !== void 0) {
|
|
505
|
-
const c = {};
|
|
506
|
-
if (search.chunks.size !== void 0 && search.chunks.size > 0) c.sizeChars = search.chunks.size;
|
|
507
|
-
if (search.chunks.overlap !== void 0 && search.chunks.overlap > 0) c.overlapChars = search.chunks.overlap;
|
|
508
|
-
if (Object.keys(c).length > 0) v.chunks = c;
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
out2.vector = [v];
|
|
512
|
-
commonSearchFields(search, out2);
|
|
513
|
-
return out2;
|
|
514
|
-
}
|
|
515
|
-
const out = {};
|
|
516
|
-
if (Array.isArray(search.text) && search.text.length > 0) out.text = { columns: [...search.text] };
|
|
517
|
-
const legs = search.vector === void 0 ? [] : Array.isArray(search.vector) ? search.vector : [search.vector];
|
|
518
|
-
if (legs.length > 0) {
|
|
519
|
-
out.vector = legs.map((leg) => {
|
|
520
|
-
const v = { metric: leg.metric ?? "cosine" };
|
|
521
|
-
if (leg.column !== void 0) v.column = leg.column;
|
|
522
|
-
if (leg.staleness !== void 0) v.staleness = leg.staleness;
|
|
523
|
-
if (leg.model !== void 0) {
|
|
524
|
-
v.embed = {
|
|
525
|
-
provider: leg.model.provider,
|
|
526
|
-
model: leg.model.model,
|
|
527
|
-
from: [...leg.from ?? []],
|
|
528
|
-
...leg.model.apiKeyName !== void 0 ? { apiKeyName: leg.model.apiKeyName } : {},
|
|
529
|
-
...leg.model.dimensions !== void 0 ? { dimensions: leg.model.dimensions } : {},
|
|
530
|
-
...leg.model.baseURL !== void 0 ? { baseURL: leg.model.baseURL } : {}
|
|
531
|
-
};
|
|
532
|
-
}
|
|
533
|
-
return v;
|
|
534
|
-
});
|
|
535
|
-
}
|
|
536
|
-
commonSearchFields(search, out);
|
|
537
|
-
return out;
|
|
538
|
-
}
|
|
539
|
-
function memoryToJSON(m) {
|
|
540
|
-
return {
|
|
541
|
-
from: [...m.from],
|
|
542
|
-
into: m.into,
|
|
543
|
-
...m.subject !== void 0 ? { subject: m.subject } : {},
|
|
544
|
-
extract: { provider: m.extract.provider, model: m.extract.model }
|
|
545
|
-
};
|
|
546
|
-
}
|
|
547
|
-
function tableToJSON(table, schemaName) {
|
|
548
|
-
const columns = {};
|
|
549
|
-
for (const [name, column] of Object.entries(table.columns)) {
|
|
550
|
-
columns[name] = columnToJSON(column);
|
|
551
|
-
}
|
|
552
|
-
const out = {
|
|
553
|
-
name: table.name,
|
|
554
|
-
schema: schemaName,
|
|
555
|
-
columns,
|
|
556
|
-
// Read, not re-derived. `defineSchema` already resolves the fail-closed
|
|
557
|
-
// default (RLS on unless the author wrote `rls: false`, and forced on by any
|
|
558
|
-
// policy), and a second copy of a SECURITY default is exactly the thing that
|
|
559
|
-
// drifts — the direction it drifted last time was "expose everything", and
|
|
560
|
-
// the live proof was one user reading another's rows.
|
|
561
|
-
rls: table.rls,
|
|
562
|
-
policies: (table.policies ?? []).map(policyToJSON)
|
|
563
|
-
};
|
|
564
|
-
if (table.primaryKey !== void 0 && table.primaryKey.length > 0) {
|
|
565
|
-
out.primaryKey = [...table.primaryKey];
|
|
566
|
-
}
|
|
567
|
-
if (table.unique !== void 0 && table.unique.length > 0) {
|
|
568
|
-
out.uniqueConstraints = table.unique.map((u) => ({ name: u.name, columns: [...u.columns] }));
|
|
569
|
-
}
|
|
570
|
-
if (table.raw !== void 0 && table.raw.length > 0) {
|
|
571
|
-
out.rawConstraints = table.raw.map((r) => ({
|
|
572
|
-
name: r.name,
|
|
573
|
-
up: r.up,
|
|
574
|
-
down: r.down ?? null
|
|
575
|
-
}));
|
|
576
|
-
}
|
|
577
|
-
if (table.checks !== void 0 && table.checks.length > 0) {
|
|
578
|
-
out.checks = table.checks.map((c) => ({ name: c.name, expr: c.expr }));
|
|
579
|
-
}
|
|
580
|
-
if (table.indexes !== void 0 && table.indexes.length > 0) {
|
|
581
|
-
out.indexes = table.indexes.map((i) => ({ name: i.name, columns: [...i.columns] }));
|
|
582
|
-
}
|
|
583
|
-
if (table.search !== void 0) {
|
|
584
|
-
const vectorColumn = Object.entries(columns).find(([, c]) => c.dimensions !== void 0)?.[0];
|
|
585
|
-
const sj = searchToJSON(table.search, vectorColumn);
|
|
586
|
-
if (sj.text !== void 0 || sj.vector !== void 0) out.search = sj;
|
|
587
|
-
}
|
|
588
|
-
if (table.memory !== void 0) {
|
|
589
|
-
out.memory = memoryToJSON(table.memory);
|
|
590
|
-
}
|
|
591
|
-
return out;
|
|
592
|
-
}
|
|
593
|
-
function qualifiedTableKey(schemaName, tableName) {
|
|
594
|
-
return schemaName === "" || schemaName === "public" ? tableName : `${schemaName}.${tableName}`;
|
|
595
|
-
}
|
|
596
|
-
function toSchemaJSON(schemas) {
|
|
597
|
-
const tables = {};
|
|
598
|
-
const extensions = [];
|
|
599
|
-
const meta = [];
|
|
600
|
-
const seen = /* @__PURE__ */ new Set();
|
|
601
|
-
for (const schema of schemas) {
|
|
602
|
-
if (seen.has(schema.name)) {
|
|
603
|
-
throw new Error(`two schemas declare the name "${schema.name}" \u2014 schema names must be unique`);
|
|
604
|
-
}
|
|
605
|
-
seen.add(schema.name);
|
|
606
|
-
for (const table of Object.values(schema.tables)) {
|
|
607
|
-
const json = tableToJSON(table, schema.name);
|
|
608
|
-
tables[qualifiedTableKey(schema.name, json.name)] = json;
|
|
609
|
-
}
|
|
610
|
-
extensions.push(...schema.extensions ?? []);
|
|
611
|
-
meta.push({ name: schema.name, exposed: schema.exposed });
|
|
612
|
-
}
|
|
613
|
-
return { tables, extensions: [...new Set(extensions)], schemas: meta };
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
export {
|
|
617
|
-
TxRefError,
|
|
618
|
-
TxPlanError,
|
|
619
|
-
now,
|
|
620
|
-
inc,
|
|
621
|
-
dec,
|
|
622
|
-
TxPlanBuilder,
|
|
623
|
-
runTxPlan,
|
|
624
|
-
qualifiedTableKey,
|
|
625
|
-
toSchemaJSON
|
|
626
|
-
};
|
|
627
|
-
//# sourceMappingURL=chunk-CJSKYY76.js.map
|