@voltro/database 0.43.0 → 0.43.2
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/CHANGELOG.md +100 -0
- package/dist/index.d.ts +155 -0
- package/dist/index.js +734 -718
- package/dist/sql.js +699 -670
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { $ as e, A as t, At as n, B as r, Bt as i, C as a, Ct as o, D as s, Dt as c, E as l, Et as u, F as d, Ft as f, G as p, Gt as m, H as h, Ht as g, I as
|
|
2
|
-
import { timestampMs as
|
|
3
|
-
import { Cause as
|
|
4
|
-
import { AsyncLocalStorage as
|
|
5
|
-
import { TransactionConnection as
|
|
6
|
-
import { ansi as
|
|
7
|
-
import { SqlClient as
|
|
8
|
-
import { createHash as
|
|
1
|
+
import { $ as e, A as t, At as n, B as r, Bt as i, C as a, Ct as o, D as s, Dt as c, E as l, Et as u, F as d, Ft as f, G as p, Gt as m, H as h, Ht as g, I as ee, It as te, J as _, Jt as v, K as ne, Kt as y, L as re, Lt as b, M as ie, Mt as ae, N as oe, Nt as se, O as ce, Ot as le, P as ue, Pt as de, Q as fe, R as pe, Rt as x, S as me, St as he, T as ge, Tt as _e, U as ve, Ut as ye, V as be, Vt as xe, W as Se, Wt as Ce, X as we, Xt as Te, Y as Ee, Yt as S, Z as De, _ as Oe, _t as ke, a as Ae, at as je, b as Me, bt as Ne, c as Pe, ct as Fe, d as Ie, dt as Le, et as Re, f as ze, ft as Be, g as Ve, gt as He, h as Ue, ht as We, i as Ge, it as Ke, j as C, jt as qe, k as Je, kt as Ye, l as Xe, lt as Ze, m as Qe, mt as $e, n as et, nt as tt, o as nt, ot as rt, p as w, pt as it, q as at, qt as ot, rt as st, s as ct, st as lt, t as ut, tt as T, u as dt, ut as ft, v as pt, vt as mt, w as ht, wt as gt, x as _t, xt as vt, y as yt, yt as bt, z as xt, zt as St } from "./frameworkLiveTables-XPFqj8n3.js";
|
|
2
|
+
import { timestampMs as Ct, timestampMsOrNull as wt } from "./wire.js";
|
|
3
|
+
import { Cause as Tt, Chunk as Et, Data as Dt, Effect as E, Exit as Ot, Metric as D, MetricBoundaries as kt, Option as O, Runtime as At, Schedule as jt, Schema as k, Stream as Mt } from "effect";
|
|
4
|
+
import { AsyncLocalStorage as Nt } from "node:async_hooks";
|
|
5
|
+
import { TransactionConnection as Pt } from "@effect/sql/SqlClient";
|
|
6
|
+
import { ansi as A } from "@voltro/logger";
|
|
7
|
+
import { SqlClient as Ft } from "@effect/sql";
|
|
8
|
+
import { createHash as It } from "node:crypto";
|
|
9
9
|
//#region src/actorSnapshot.ts
|
|
10
|
-
var
|
|
10
|
+
var Lt = (e) => typeof e == "string" && e !== "" ? e : null, Rt = async (e, t, n) => {
|
|
11
11
|
if (t != null && t !== "") try {
|
|
12
12
|
let r = (await e.query({
|
|
13
13
|
table: "actors",
|
|
@@ -23,20 +23,20 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
23
23
|
}))[0];
|
|
24
24
|
return r === void 0 ? void 0 : {
|
|
25
25
|
id: t,
|
|
26
|
-
type:
|
|
27
|
-
displayName:
|
|
28
|
-
email:
|
|
26
|
+
type: Lt(r.kind) ?? n,
|
|
27
|
+
displayName: Lt(r.displayName),
|
|
28
|
+
email: Lt(r.email)
|
|
29
29
|
};
|
|
30
30
|
} catch {
|
|
31
31
|
return;
|
|
32
32
|
}
|
|
33
|
-
},
|
|
33
|
+
}, zt = (e) => e === "mysql" || e === "mariadb" || e === "mssql" || e === "sqlite", Bt = (e) => {
|
|
34
34
|
let t = [];
|
|
35
35
|
for (let [n, r] of Object.entries(e.fields)) r.type === "array" && t.push(n);
|
|
36
36
|
return t;
|
|
37
|
-
},
|
|
38
|
-
if (!
|
|
39
|
-
let r =
|
|
37
|
+
}, Vt = (e, t, n) => {
|
|
38
|
+
if (!zt(n) || !t) return e;
|
|
39
|
+
let r = Bt(t);
|
|
40
40
|
if (r.length === 0) return e;
|
|
41
41
|
let i = { ...e };
|
|
42
42
|
for (let e of r) {
|
|
@@ -44,9 +44,9 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
44
44
|
Array.isArray(t) && (i[e] = JSON.stringify(t));
|
|
45
45
|
}
|
|
46
46
|
return i;
|
|
47
|
-
},
|
|
48
|
-
if (!
|
|
49
|
-
let r =
|
|
47
|
+
}, Ht = (e, t, n) => {
|
|
48
|
+
if (!zt(n) || !t) return e;
|
|
49
|
+
let r = Bt(t);
|
|
50
50
|
return r.length === 0 ? e : e.map((e) => {
|
|
51
51
|
let t;
|
|
52
52
|
for (let n of r) {
|
|
@@ -58,7 +58,7 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
58
58
|
}
|
|
59
59
|
return t ?? e;
|
|
60
60
|
});
|
|
61
|
-
},
|
|
61
|
+
}, Ut = "enc:v1:", Wt = class extends Error {
|
|
62
62
|
_tag = "FieldDecryptionError";
|
|
63
63
|
table;
|
|
64
64
|
column;
|
|
@@ -66,7 +66,7 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
66
66
|
constructor(e) {
|
|
67
67
|
super(`column "${e.table}.${e.column}" is .encrypted() but could not be decrypted (${e.reason}) — the stored value was most likely encrypted under a different key`), this.name = "FieldDecryptionError", this.table = e.table, this.column = e.column, this.reason = e.reason;
|
|
68
68
|
}
|
|
69
|
-
},
|
|
69
|
+
}, Gt = (e) => typeof e == "object" && !!e && e._tag === "FieldDecryptionError", Kt = (e) => typeof e == "string" && e.startsWith("enc:v1:"), qt = (e, t) => e.encrypt(JSON.stringify(t)), Jt = /* @__PURE__ */ new Set([
|
|
70
70
|
"text",
|
|
71
71
|
"id",
|
|
72
72
|
"reference",
|
|
@@ -74,51 +74,51 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
74
74
|
"uuid",
|
|
75
75
|
"email",
|
|
76
76
|
"url"
|
|
77
|
-
]),
|
|
77
|
+
]), Yt = (e, t, n) => {
|
|
78
78
|
let r = e.decrypt(t), i;
|
|
79
79
|
try {
|
|
80
80
|
i = JSON.parse(r);
|
|
81
81
|
} catch {
|
|
82
82
|
return r;
|
|
83
83
|
}
|
|
84
|
-
return typeof i == "string" ? i : n === void 0 ||
|
|
85
|
-
},
|
|
84
|
+
return typeof i == "string" ? i : n === void 0 || Jt.has(n) ? r : i;
|
|
85
|
+
}, Xt = (e, t, n) => {
|
|
86
86
|
let r = e.decrypt(t);
|
|
87
87
|
try {
|
|
88
|
-
return typeof JSON.parse(r) == "string" ? !1 : n === void 0 ||
|
|
88
|
+
return typeof JSON.parse(r) == "string" ? !1 : n === void 0 || Jt.has(n) ? !0 : void 0;
|
|
89
89
|
} catch {
|
|
90
90
|
return !0;
|
|
91
91
|
}
|
|
92
|
-
},
|
|
92
|
+
}, Zt = (e, t) => e.fields[t]?.type, Qt = (e) => {
|
|
93
93
|
let t = [];
|
|
94
94
|
for (let [n, r] of Object.entries(e.fields)) r.encrypted === !0 && t.push(n);
|
|
95
95
|
return t;
|
|
96
|
-
},
|
|
96
|
+
}, $t = (e, t, n) => {
|
|
97
97
|
if (!t) return e;
|
|
98
|
-
let r =
|
|
98
|
+
let r = Qt(t);
|
|
99
99
|
if (r.length === 0) return e;
|
|
100
100
|
let i = { ...e };
|
|
101
101
|
for (let e of r) {
|
|
102
102
|
let r = i[e];
|
|
103
|
-
if (!(r == null ||
|
|
103
|
+
if (!(r == null || Kt(r))) {
|
|
104
104
|
if (!n) throw Error(`column "${t.tableName ?? "?"}.${e}" is .encrypted() but no field cipher is registered — wire @voltro/plugin-governance's fieldEncryption (key via the Secrets-Resolver)`);
|
|
105
|
-
i[e] =
|
|
105
|
+
i[e] = qt(n, r);
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
return i;
|
|
109
|
-
},
|
|
109
|
+
}, en = (e, t, n, r) => {
|
|
110
110
|
if (!t) return e;
|
|
111
|
-
let i =
|
|
111
|
+
let i = Qt(t);
|
|
112
112
|
if (i.length === 0 || !n) return e;
|
|
113
113
|
let a = r?.onError ?? "throw", o = t.tableName ?? "?";
|
|
114
114
|
return e.map((e) => {
|
|
115
115
|
let s;
|
|
116
116
|
for (let c of i) {
|
|
117
117
|
let i = e[c];
|
|
118
|
-
if (
|
|
118
|
+
if (Kt(i)) {
|
|
119
119
|
s ??= { ...e };
|
|
120
120
|
try {
|
|
121
|
-
s[c] =
|
|
121
|
+
s[c] = Yt(n, i, Zt(t, c));
|
|
122
122
|
} catch (e) {
|
|
123
123
|
let t = String(e?.message ?? e);
|
|
124
124
|
if (a === "null") {
|
|
@@ -129,7 +129,7 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
129
129
|
});
|
|
130
130
|
continue;
|
|
131
131
|
}
|
|
132
|
-
throw new
|
|
132
|
+
throw new Wt({
|
|
133
133
|
table: o,
|
|
134
134
|
column: c,
|
|
135
135
|
reason: t
|
|
@@ -139,12 +139,12 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
139
139
|
}
|
|
140
140
|
return s ?? e;
|
|
141
141
|
});
|
|
142
|
-
},
|
|
142
|
+
}, tn = (e) => {
|
|
143
143
|
let t = e;
|
|
144
|
-
for (let e = 0; e < 4 &&
|
|
144
|
+
for (let e = 0; e < 4 && At.isFiberFailure(t); e++) t = Tt.squash(t[At.FiberFailureCauseId]);
|
|
145
145
|
return t;
|
|
146
|
-
},
|
|
147
|
-
let t = {}, n = [], r =
|
|
146
|
+
}, nn = (e) => {
|
|
147
|
+
let t = {}, n = [], r = tn(e);
|
|
148
148
|
for (let e = 0; e < 8 && typeof r == "object" && r; e++) {
|
|
149
149
|
let e = r;
|
|
150
150
|
t.dbCode === void 0 && (typeof e.code == "string" || typeof e.code == "number") && (t.dbCode = e.code), t.dbErrno === void 0 && typeof e.errno == "number" && (t.dbErrno = e.errno), t.dbNumber === void 0 && typeof e.number == "number" && (t.dbNumber = e.number), t.dbSqlState === void 0 && typeof e.sqlState == "string" && (t.dbSqlState = e.sqlState), t.dbFatal === void 0 && typeof e.fatal == "boolean" && (t.dbFatal = e.fatal), t.dbConstraint === void 0 && typeof e.constraint == "string" && (t.dbConstraint = e.constraint), t.dbTable === void 0 && typeof e.table == "string" && (t.dbTable = e.table), t.dbColumn === void 0 && typeof e.column == "string" && (t.dbColumn = e.column), t.dbSchema === void 0 && typeof e.schema == "string" && (t.dbSchema = e.schema);
|
|
@@ -154,93 +154,93 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
154
154
|
(o || s) && n.push(`${o || "Error"}${s ? `: ${s.slice(0, 200)}` : ""}`), r = e.cause;
|
|
155
155
|
}
|
|
156
156
|
return n.length > 0 && (t.dbCauseChain = n.join(" ← "), t.dbMessage === void 0 && (t.dbMessage = n[n.length - 1])), t;
|
|
157
|
-
},
|
|
157
|
+
}, rn = /^[A-Za-z_][A-Za-z0-9_$]*$/, j = (e) => e !== void 0 && e.length <= 128 && rn.test(e) ? e : void 0, an = (e) => e === void 0 ? void 0 : e.split(".").pop() ?? e, M = (e, ...t) => {
|
|
158
158
|
for (let n of t) {
|
|
159
159
|
let t = n.exec(e);
|
|
160
160
|
if (t?.[1] !== void 0) return t[1];
|
|
161
161
|
}
|
|
162
|
-
},
|
|
162
|
+
}, on = (e, t) => {
|
|
163
163
|
let n = e.dbCode, r = e.dbErrno, i = e.dbNumber, a = typeof e.dbMessage == "string" ? e.dbMessage : "", o = `${typeof e.dbCauseChain == "string" ? e.dbCauseChain : ""} ${a}`;
|
|
164
164
|
if (n === "23503") {
|
|
165
|
-
let t =
|
|
165
|
+
let t = j(e.dbConstraint);
|
|
166
166
|
return {
|
|
167
167
|
kind: /is still referenced from table/i.test(o) ? "foreignKeyInUse" : "foreignKey",
|
|
168
168
|
...t === void 0 ? {} : { constraint: t }
|
|
169
169
|
};
|
|
170
170
|
}
|
|
171
171
|
if (n === "23505") {
|
|
172
|
-
let t =
|
|
172
|
+
let t = j(e.dbConstraint);
|
|
173
173
|
return {
|
|
174
174
|
kind: "unique",
|
|
175
175
|
...t === void 0 ? {} : { constraint: t }
|
|
176
176
|
};
|
|
177
177
|
}
|
|
178
178
|
if (n === "23502") {
|
|
179
|
-
let t =
|
|
179
|
+
let t = j(e.dbColumn);
|
|
180
180
|
return {
|
|
181
181
|
kind: "notNull",
|
|
182
182
|
...t === void 0 ? {} : { column: t }
|
|
183
183
|
};
|
|
184
184
|
}
|
|
185
185
|
if (n === "23514") {
|
|
186
|
-
let t =
|
|
186
|
+
let t = j(e.dbConstraint);
|
|
187
187
|
return {
|
|
188
188
|
kind: "check",
|
|
189
189
|
...t === void 0 ? {} : { constraint: t }
|
|
190
190
|
};
|
|
191
191
|
}
|
|
192
192
|
if (r === 1452 || r === 1451) {
|
|
193
|
-
let e = M(
|
|
193
|
+
let e = j(M(o, /CONSTRAINT `([^`]+)`/));
|
|
194
194
|
return {
|
|
195
195
|
kind: r === 1451 ? "foreignKeyInUse" : "foreignKey",
|
|
196
196
|
...e === void 0 ? {} : { constraint: e }
|
|
197
197
|
};
|
|
198
198
|
}
|
|
199
199
|
if (r === 1062) {
|
|
200
|
-
let e =
|
|
200
|
+
let e = j(an(M(o, /for key '([^']+)'/)));
|
|
201
201
|
return {
|
|
202
202
|
kind: "unique",
|
|
203
203
|
...e === void 0 ? {} : { constraint: e }
|
|
204
204
|
};
|
|
205
205
|
}
|
|
206
206
|
if (r === 1048 || r === 1364) {
|
|
207
|
-
let e = M(
|
|
207
|
+
let e = j(M(o, /Column '([^']+)' cannot be null/, /Field '([^']+)' doesn't have a default value/));
|
|
208
208
|
return {
|
|
209
209
|
kind: "notNull",
|
|
210
210
|
...e === void 0 ? {} : { column: e }
|
|
211
211
|
};
|
|
212
212
|
}
|
|
213
213
|
if (r === 3819 || r === 4025) {
|
|
214
|
-
let e = M(
|
|
214
|
+
let e = j(M(o, /Check constraint '([^']+)'/, /CONSTRAINT `([^`]+)` failed/));
|
|
215
215
|
return {
|
|
216
216
|
kind: "check",
|
|
217
217
|
...e === void 0 ? {} : { constraint: e }
|
|
218
218
|
};
|
|
219
219
|
}
|
|
220
220
|
if (i === 547) {
|
|
221
|
-
let e =
|
|
221
|
+
let e = M(o, /(?:FOREIGN KEY|REFERENCE) constraint "([^"]+)"/), t = M(o, /CHECK constraint "([^"]+)"/);
|
|
222
222
|
if (t !== void 0 && e === void 0) {
|
|
223
|
-
let e =
|
|
223
|
+
let e = j(t);
|
|
224
224
|
return {
|
|
225
225
|
kind: "check",
|
|
226
226
|
...e === void 0 ? {} : { constraint: e }
|
|
227
227
|
};
|
|
228
228
|
}
|
|
229
|
-
let n = /REFERENCE constraint/.test(o), r =
|
|
229
|
+
let n = /REFERENCE constraint/.test(o), r = j(e);
|
|
230
230
|
return {
|
|
231
231
|
kind: n ? "foreignKeyInUse" : "foreignKey",
|
|
232
232
|
...r === void 0 ? {} : { constraint: r }
|
|
233
233
|
};
|
|
234
234
|
}
|
|
235
235
|
if (i === 2627 || i === 2601) {
|
|
236
|
-
let e = M(
|
|
236
|
+
let e = j(M(o, /(?:UNIQUE KEY|PRIMARY KEY) constraint '([^']+)'/, /index '([^']+)'/));
|
|
237
237
|
return {
|
|
238
238
|
kind: "unique",
|
|
239
239
|
...e === void 0 ? {} : { constraint: e }
|
|
240
240
|
};
|
|
241
241
|
}
|
|
242
242
|
if (i === 515) {
|
|
243
|
-
let e = M(
|
|
243
|
+
let e = j(M(o, /Cannot insert the value NULL into column '([^']+)'/));
|
|
244
244
|
return {
|
|
245
245
|
kind: "notNull",
|
|
246
246
|
...e === void 0 ? {} : { column: e }
|
|
@@ -248,88 +248,97 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
248
248
|
}
|
|
249
249
|
if (n === "SQLITE_CONSTRAINT_FOREIGNKEY") return { kind: t === "delete" || t === "deleteMany" || t === "hardDelete" ? "foreignKeyInUse" : "foreignKey" };
|
|
250
250
|
if (n === "SQLITE_CONSTRAINT_UNIQUE" || n === "SQLITE_CONSTRAINT_PRIMARYKEY") {
|
|
251
|
-
let e =
|
|
251
|
+
let e = j(an(M(o, /UNIQUE constraint failed: ([^,\s]+)/)));
|
|
252
252
|
return {
|
|
253
253
|
kind: "unique",
|
|
254
254
|
...e === void 0 ? {} : { column: e }
|
|
255
255
|
};
|
|
256
256
|
}
|
|
257
257
|
if (n === "SQLITE_CONSTRAINT_NOTNULL") {
|
|
258
|
-
let e =
|
|
258
|
+
let e = j(an(M(o, /NOT NULL constraint failed: ([^,\s]+)/)));
|
|
259
259
|
return {
|
|
260
260
|
kind: "notNull",
|
|
261
261
|
...e === void 0 ? {} : { column: e }
|
|
262
262
|
};
|
|
263
263
|
}
|
|
264
264
|
if (n === "SQLITE_CONSTRAINT_CHECK") {
|
|
265
|
-
let e = M(
|
|
265
|
+
let e = j(M(a, /CHECK constraint failed: (.+)$/));
|
|
266
266
|
return {
|
|
267
267
|
kind: "check",
|
|
268
268
|
...e === void 0 ? {} : { constraint: e }
|
|
269
269
|
};
|
|
270
270
|
}
|
|
271
|
-
}, on = (e) => {
|
|
272
|
-
let t = an(e)?.kind;
|
|
273
|
-
return t === "foreignKey" || t === "foreignKeyInUse";
|
|
274
271
|
}, sn = (e) => {
|
|
272
|
+
let t = on(e)?.kind;
|
|
273
|
+
return t === "foreignKey" || t === "foreignKeyInUse";
|
|
274
|
+
}, cn = (e) => {
|
|
275
275
|
let t = e.dbCode, n = e.dbErrno;
|
|
276
276
|
return t === "57014" || n === 3024 || n === 1969 || t === "ETIMEOUT" || t === "SQLITE_INTERRUPT";
|
|
277
|
-
},
|
|
277
|
+
}, ln = (e) => {
|
|
278
|
+
let t = nn(e), n = [
|
|
279
|
+
t.dbCode === void 0 ? void 0 : `code=${String(t.dbCode)}`,
|
|
280
|
+
t.dbErrno === void 0 ? void 0 : `errno=${String(t.dbErrno)}`,
|
|
281
|
+
t.dbNumber === void 0 ? void 0 : `number=${String(t.dbNumber)}`,
|
|
282
|
+
t.dbSqlState === void 0 ? void 0 : `sqlState=${String(t.dbSqlState)}`,
|
|
283
|
+
t.dbConstraint === void 0 ? void 0 : `constraint=${String(t.dbConstraint)}`
|
|
284
|
+
].filter((e) => e !== void 0), r = typeof e?.message == "string" ? e.message : void 0, i = typeof t.dbMessage == "string" ? t.dbMessage : void 0, a = i === void 0 ? void 0 : n.length > 0 || i !== r && i !== `Error: ${String(r)}` ? i : void 0;
|
|
285
|
+
if (a !== void 0 || n.length !== 0) return [a, n.length > 0 ? `[${n.join(" ")}]` : void 0].filter((e) => e !== void 0 && e !== "").join(" ");
|
|
286
|
+
}, un = (e) => {
|
|
278
287
|
let t = (() => {
|
|
279
288
|
switch (e.type) {
|
|
280
289
|
case "id":
|
|
281
290
|
case "reference":
|
|
282
291
|
case "text":
|
|
283
|
-
case "enum": return
|
|
292
|
+
case "enum": return k.String;
|
|
284
293
|
case "integer":
|
|
285
294
|
case "real":
|
|
286
|
-
case "decimal": return
|
|
287
|
-
case "boolean": return
|
|
295
|
+
case "decimal": return k.Number;
|
|
296
|
+
case "boolean": return k.Boolean;
|
|
288
297
|
case "timestamp":
|
|
289
|
-
case "date": return
|
|
290
|
-
case "bigint": return
|
|
291
|
-
case "bytes": return
|
|
292
|
-
case "array": return
|
|
293
|
-
case "interval": return
|
|
294
|
-
default: return
|
|
298
|
+
case "date": return Ct;
|
|
299
|
+
case "bigint": return k.BigInt;
|
|
300
|
+
case "bytes": return k.Uint8ArrayFromBase64;
|
|
301
|
+
case "array": return k.Array(k.Unknown);
|
|
302
|
+
case "interval": return k.String;
|
|
303
|
+
default: return k.Unknown;
|
|
295
304
|
}
|
|
296
305
|
})();
|
|
297
|
-
return e.nullable === !0 ?
|
|
298
|
-
},
|
|
306
|
+
return e.nullable === !0 ? k.NullOr(t) : t;
|
|
307
|
+
}, dn = (e, t) => {
|
|
299
308
|
let n = new Set(t?.omit ?? []), r = {};
|
|
300
|
-
for (let [t, i] of Object.entries(e.fields)) n.has(t) || (r[t] =
|
|
301
|
-
return
|
|
302
|
-
},
|
|
309
|
+
for (let [t, i] of Object.entries(e.fields)) n.has(t) || (r[t] = un(i));
|
|
310
|
+
return k.Struct(r);
|
|
311
|
+
}, fn = (e) => typeof e == "function" ? e : () => e, pn = (e, t) => ({
|
|
303
312
|
kind: "one",
|
|
304
|
-
target:
|
|
313
|
+
target: fn(e),
|
|
305
314
|
...t?.foreignKey === void 0 ? {} : { foreignKey: t.foreignKey },
|
|
306
315
|
...t?.sourceKey === void 0 ? {} : { sourceKey: t.sourceKey }
|
|
307
|
-
}),
|
|
316
|
+
}), mn = (e, t) => ({
|
|
308
317
|
kind: "many",
|
|
309
|
-
target:
|
|
318
|
+
target: fn(e),
|
|
310
319
|
...t?.foreignKey === void 0 ? {} : { foreignKey: t.foreignKey },
|
|
311
320
|
...t?.sourceKey === void 0 ? {} : { sourceKey: t.sourceKey }
|
|
312
|
-
}),
|
|
321
|
+
}), hn = (e, t) => ({
|
|
313
322
|
kind: "manyToMany",
|
|
314
|
-
target:
|
|
315
|
-
through:
|
|
323
|
+
target: fn(e),
|
|
324
|
+
through: fn(t.through),
|
|
316
325
|
sourceKey: t.sourceKey,
|
|
317
326
|
targetKey: t.targetKey
|
|
318
|
-
}),
|
|
319
|
-
one:
|
|
320
|
-
many:
|
|
321
|
-
manyToMany:
|
|
322
|
-
},
|
|
327
|
+
}), gn = {
|
|
328
|
+
one: pn,
|
|
329
|
+
many: mn,
|
|
330
|
+
manyToMany: hn
|
|
331
|
+
}, _n = (e, t) => {
|
|
323
332
|
let n = typeof e == "function" ? e : () => e;
|
|
324
333
|
return {
|
|
325
334
|
source: n,
|
|
326
|
-
relations: t(
|
|
335
|
+
relations: t(gn, n())
|
|
327
336
|
};
|
|
328
|
-
},
|
|
337
|
+
}, vn = (e) => {
|
|
329
338
|
if (typeof e != "object" || !e) return !1;
|
|
330
339
|
let t = e;
|
|
331
340
|
return typeof t.source != "function" || typeof t.relations != "object" || t.relations === null ? !1 : Object.keys(t.relations).length === 0;
|
|
332
|
-
},
|
|
341
|
+
}, yn = (e) => {
|
|
333
342
|
if (typeof e != "object" || !e) return !1;
|
|
334
343
|
let t = e;
|
|
335
344
|
if (typeof t.source != "function" || typeof t.relations != "object" || t.relations === null) return !1;
|
|
@@ -338,32 +347,32 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
338
347
|
let t = e?.kind;
|
|
339
348
|
return t === "one" || t === "many" || t === "manyToMany";
|
|
340
349
|
});
|
|
341
|
-
},
|
|
350
|
+
}, bn = pn, xn = mn, Sn = hn, N = (e, t, n) => {
|
|
342
351
|
let r = [];
|
|
343
352
|
for (let [n, i] of Object.entries(e.fields)) i.type === "reference" && i.references?.()?.tableName === t && r.push(n);
|
|
344
353
|
if (r.length === 0) throw Error(`relation '${n}': no reference() column on '${e.tableName}' points at '${t}'. Either add one or pass an explicit { foreignKey: '<column>' }.`);
|
|
345
354
|
if (r.length > 1) throw Error(`relation '${n}': multiple reference() columns on '${e.tableName}' point at '${t}' (${r.join(", ")}). Pass an explicit { foreignKey: '<column>' } to disambiguate.`);
|
|
346
355
|
return r[0];
|
|
347
|
-
},
|
|
348
|
-
let t =
|
|
356
|
+
}, Cn = (e) => Object.keys(e.fields), wn = (e, t) => Object.prototype.hasOwnProperty.call(e.fields, t), Tn = (e) => {
|
|
357
|
+
let t = Cn(e);
|
|
349
358
|
return t.length === 0 ? "<none>" : t.join(", ");
|
|
350
|
-
},
|
|
359
|
+
}, En = (e, t, n) => {
|
|
351
360
|
let r = `relation '${t}' on '${e.tableName}'`, i = n.target();
|
|
352
361
|
if (n.kind === "manyToMany") {
|
|
353
362
|
let t = n.through();
|
|
354
|
-
for (let [a, o] of [["sourceKey", n.sourceKey], ["targetKey", n.targetKey]]) if (!
|
|
363
|
+
for (let [a, o] of [["sourceKey", n.sourceKey], ["targetKey", n.targetKey]]) if (!wn(t, o)) throw Error(`${r}: manyToMany ${a} '${o}' is not a column on the junction table '${t.tableName}'. Both keys name columns on the JUNCTION — ${a === "sourceKey" ? `sourceKey points at '${e.tableName}'` : `targetKey points at '${i.tableName}'`}. Columns on '${t.tableName}': ${Tn(t)}.`);
|
|
355
364
|
return;
|
|
356
365
|
}
|
|
357
366
|
if (n.sourceKey !== void 0 && n.foreignKey !== void 0) throw Error(`${r}: pass EITHER sourceKey OR foreignKey, not both. sourceKey '${n.sourceKey}' names a column on the source ('${e.tableName}') holding the target's id; foreignKey '${n.foreignKey}' names a column on the target ('${i.tableName}') holding the source's id. With both, foreignKey is ignored.`);
|
|
358
|
-
if (n.sourceKey !== void 0 && !
|
|
359
|
-
let t =
|
|
360
|
-
throw Error(`${r}: sourceKey '${n.sourceKey}' is not a column on the source table '${e.tableName}'.` + (t ? ` It IS a column on the target '${i.tableName}' — you probably want { foreignKey: '${n.sourceKey}' }, which names the column on the TARGET that holds this table's id.` : ` Columns on '${e.tableName}': ${
|
|
367
|
+
if (n.sourceKey !== void 0 && !wn(e, n.sourceKey)) {
|
|
368
|
+
let t = wn(i, n.sourceKey);
|
|
369
|
+
throw Error(`${r}: sourceKey '${n.sourceKey}' is not a column on the source table '${e.tableName}'.` + (t ? ` It IS a column on the target '${i.tableName}' — you probably want { foreignKey: '${n.sourceKey}' }, which names the column on the TARGET that holds this table's id.` : ` Columns on '${e.tableName}': ${Tn(e)}.`));
|
|
361
370
|
}
|
|
362
|
-
if (n.foreignKey !== void 0 && !
|
|
363
|
-
let t =
|
|
364
|
-
throw Error(`${r}: foreignKey '${n.foreignKey}' is not a column on the target table '${i.tableName}'.` + (t ? ` It IS a column on the source '${e.tableName}' — you want { sourceKey: '${n.foreignKey}' }. foreignKey names the column on the TARGET that points back at this table; sourceKey names the column on THIS table that holds the target's id.` : ` Columns on '${i.tableName}': ${
|
|
371
|
+
if (n.foreignKey !== void 0 && !wn(i, n.foreignKey)) {
|
|
372
|
+
let t = wn(e, n.foreignKey);
|
|
373
|
+
throw Error(`${r}: foreignKey '${n.foreignKey}' is not a column on the target table '${i.tableName}'.` + (t ? ` It IS a column on the source '${e.tableName}' — you want { sourceKey: '${n.foreignKey}' }. foreignKey names the column on the TARGET that points back at this table; sourceKey names the column on THIS table that holds the target's id.` : ` Columns on '${i.tableName}': ${Tn(i)}.`));
|
|
365
374
|
}
|
|
366
|
-
},
|
|
375
|
+
}, Dn = Symbol.for("@voltro/database/relationsRegistry"), On = globalThis, P = On[Dn] ?? (On[Dn] = /* @__PURE__ */ new Map()), kn = (e) => {
|
|
367
376
|
let t = e.source(), n = P.get(t.tableName);
|
|
368
377
|
if (n === void 0) {
|
|
369
378
|
P.set(t.tableName, new Map(Object.entries(e.relations)));
|
|
@@ -376,14 +385,14 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
376
385
|
n.set(r, i);
|
|
377
386
|
}
|
|
378
387
|
}
|
|
379
|
-
},
|
|
388
|
+
}, An = (e) => {
|
|
380
389
|
let t = 0;
|
|
381
|
-
for (let n of Object.values(e))
|
|
390
|
+
for (let n of Object.values(e)) yn(n) && (kn(n), t += 1);
|
|
382
391
|
return t;
|
|
383
|
-
},
|
|
392
|
+
}, jn = (e) => {
|
|
384
393
|
let t = P.get(e);
|
|
385
394
|
if (t !== void 0) return Object.fromEntries(t);
|
|
386
|
-
},
|
|
395
|
+
}, Mn = (e, t) => P.get(e)?.get(t), Nn = () => {
|
|
387
396
|
let e = [];
|
|
388
397
|
for (let [t, n] of P) for (let [r, i] of n) e.push({
|
|
389
398
|
source: t,
|
|
@@ -391,18 +400,18 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
391
400
|
relation: i
|
|
392
401
|
});
|
|
393
402
|
return e;
|
|
394
|
-
},
|
|
403
|
+
}, Pn = () => {
|
|
395
404
|
for (let [e, t] of P) {
|
|
396
405
|
let n = p(e);
|
|
397
|
-
if (n !== void 0) for (let [e, r] of t)
|
|
406
|
+
if (n !== void 0) for (let [e, r] of t) En(n, e, r);
|
|
398
407
|
}
|
|
399
|
-
},
|
|
408
|
+
}, Fn = () => {
|
|
400
409
|
P.clear();
|
|
401
|
-
},
|
|
410
|
+
}, In = 4, Ln = (e) => Object.keys(e.fields), Rn = (e, t) => Object.prototype.hasOwnProperty.call(e.fields, t), zn = (e, t) => {
|
|
402
411
|
let n = [];
|
|
403
412
|
for (let [r, i] of Object.entries(e.fields)) i.type === "reference" && i.references?.()?.tableName === t && n.push(r);
|
|
404
413
|
return n;
|
|
405
|
-
},
|
|
414
|
+
}, Bn = (e, t) => {
|
|
406
415
|
let n = [];
|
|
407
416
|
for (let r of t) {
|
|
408
417
|
if (r.source !== e) continue;
|
|
@@ -419,7 +428,7 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
419
428
|
} catch {
|
|
420
429
|
continue;
|
|
421
430
|
}
|
|
422
|
-
if (!
|
|
431
|
+
if (!Rn(e, t.sourceKey)) continue;
|
|
423
432
|
n.push({
|
|
424
433
|
table: e.tableName,
|
|
425
434
|
foreignKey: t.sourceKey,
|
|
@@ -428,24 +437,24 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
428
437
|
continue;
|
|
429
438
|
}
|
|
430
439
|
if (t.sourceKey !== void 0) continue;
|
|
431
|
-
let a =
|
|
432
|
-
o === void 0 || !
|
|
440
|
+
let a = zn(i, e), o = t.foreignKey ?? (a.length === 1 ? a[0] : void 0);
|
|
441
|
+
o === void 0 || !Rn(i, o) || n.push({
|
|
433
442
|
table: i.tableName,
|
|
434
443
|
foreignKey: o,
|
|
435
444
|
parentKey: "id"
|
|
436
445
|
});
|
|
437
446
|
}
|
|
438
447
|
return n;
|
|
439
|
-
},
|
|
448
|
+
}, Vn = (e, t) => {
|
|
440
449
|
let n = [];
|
|
441
|
-
for (let r of t) if (r.tableName !== e) for (let t of
|
|
450
|
+
for (let r of t) if (r.tableName !== e) for (let t of zn(r, e)) n.push({
|
|
442
451
|
table: r.tableName,
|
|
443
452
|
foreignKey: t,
|
|
444
453
|
parentKey: "id"
|
|
445
454
|
});
|
|
446
455
|
return n;
|
|
447
|
-
},
|
|
448
|
-
let t = e.tables ?? h(), n = e.relations ??
|
|
456
|
+
}, Hn = (e) => `${e.table}\u0000${e.hops.map((e) => `${e.table}.${e.foreignKey}<-${e.parentKey}`).join("|")}`, Un = (e) => {
|
|
457
|
+
let t = e.tables ?? h(), n = e.relations ?? Nn(), r = e.maxDepth ?? 4, i = new Set(e.exclude ?? []), a = new Map(t.map((e) => [e.tableName, e])), o = [];
|
|
449
458
|
if (!a.has(e.subjectTable)) return {
|
|
450
459
|
subjectTable: e.subjectTable,
|
|
451
460
|
paths: [],
|
|
@@ -460,11 +469,11 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
460
469
|
hops: [],
|
|
461
470
|
depth: 0,
|
|
462
471
|
via: "relation"
|
|
463
|
-
}, c = [s], l = /* @__PURE__ */ new Set([
|
|
472
|
+
}, c = [s], l = /* @__PURE__ */ new Set([Hn(s)]), u = /* @__PURE__ */ new Set(), d = [s];
|
|
464
473
|
for (let e = 1; e <= r; e++) {
|
|
465
474
|
let r = [];
|
|
466
475
|
for (let a of d) {
|
|
467
|
-
let s =
|
|
476
|
+
let s = Bn(a.table, n), d = Vn(a.table, t), f = [...s.map((e) => [e, "relation"]), ...d.map((e) => [e, "reference"])];
|
|
468
477
|
for (let [t, n] of f) {
|
|
469
478
|
if (i.has(t.table)) {
|
|
470
479
|
o.push({
|
|
@@ -482,7 +491,7 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
482
491
|
hops: [...a.hops, t],
|
|
483
492
|
depth: e,
|
|
484
493
|
via: n
|
|
485
|
-
}, f =
|
|
494
|
+
}, f = Hn(d);
|
|
486
495
|
l.has(f) || (l.add(f), c.push(d), r.push(d));
|
|
487
496
|
}
|
|
488
497
|
}
|
|
@@ -492,7 +501,7 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
492
501
|
}
|
|
493
502
|
d = r;
|
|
494
503
|
}
|
|
495
|
-
for (let e of d) (
|
|
504
|
+
for (let e of d) (Bn(e.table, n).length > 0 || Vn(e.table, t).length > 0) && o.push({
|
|
496
505
|
kind: "depth-truncated",
|
|
497
506
|
table: e.table,
|
|
498
507
|
detail: `stopped at maxDepth ${r}; tables linked FROM '${e.table}' were not explored`
|
|
@@ -501,14 +510,14 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
501
510
|
for (let n of t) f.has(n.tableName) || i.has(n.tableName) || o.push({
|
|
502
511
|
kind: "unreachable",
|
|
503
512
|
table: n.tableName,
|
|
504
|
-
detail: `no relation() and no reference() column links '${n.tableName}' to '${e.subjectTable}'. If it holds subject data through a plain column, a polymorphic (type,id) pair, JSON or free text, add an explicit subjectScopes entry — this derivation cannot see it. Columns: ${
|
|
513
|
+
detail: `no relation() and no reference() column links '${n.tableName}' to '${e.subjectTable}'. If it holds subject data through a plain column, a polymorphic (type,id) pair, JSON or free text, add an explicit subjectScopes entry — this derivation cannot see it. Columns: ${Ln(n).join(", ")}`
|
|
505
514
|
});
|
|
506
515
|
return {
|
|
507
516
|
subjectTable: e.subjectTable,
|
|
508
517
|
paths: c,
|
|
509
518
|
limitations: o
|
|
510
519
|
};
|
|
511
|
-
},
|
|
520
|
+
}, Wn = (e) => {
|
|
512
521
|
let t = [`subject graph · ${e.subjectTable} · ${e.paths.length} table(s) reachable`];
|
|
513
522
|
for (let n of e.paths) {
|
|
514
523
|
let r = n.hops.length === 0 ? `${e.subjectTable} (the subject row itself)` : [e.subjectTable, ...n.hops.map((e) => `${e.table}.${e.foreignKey}`)].join(" → ");
|
|
@@ -516,7 +525,7 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
516
525
|
}
|
|
517
526
|
let n = e.limitations.filter((e) => e.kind === "depth-truncated"), r = e.limitations.filter((e) => e.kind === "unreachable"), i = e.limitations.filter((e) => e.kind === "excluded");
|
|
518
527
|
return t.push(""), t.push(" WHAT THIS CANNOT SEE — a DSAR is only as complete as this list is short:"), n.length > 0 && t.push(` depth-truncated (${n.length}): ${n.map((e) => e.table).join(", ")}`), i.length > 0 && t.push(` excluded (${i.length}): ${[...new Set(i.map((e) => e.table))].join(", ")}`), t.push(r.length === 0 ? " unreachable: none — every registered table is linked to the subject." : ` unreachable (${r.length}): ${r.map((e) => e.table).join(", ")}`), t.push(" Always outside this graph: object storage / uploaded files, external processors,"), t.push(" log and metric sinks, backups, and any subject id embedded in JSON or free text."), t.join("\n");
|
|
519
|
-
},
|
|
528
|
+
}, Gn = (e) => e == null || typeof e == "boolean" ? e : typeof e == "number" ? e !== 0 : typeof e == "bigint" ? e !== 0n : typeof e == "string" ? e === "1" || e.toLowerCase() === "true" : e, Kn = (e) => {
|
|
520
529
|
if (e == null || typeof e != "string") return e;
|
|
521
530
|
let t = e.trim();
|
|
522
531
|
if (t.length === 0) return e;
|
|
@@ -527,7 +536,7 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
527
536
|
} catch {
|
|
528
537
|
return e;
|
|
529
538
|
}
|
|
530
|
-
},
|
|
539
|
+
}, qn = (e) => e == null || typeof e == "string" ? e : typeof e == "number" || typeof e == "bigint" ? String(e) : e, Jn = (e) => {
|
|
531
540
|
if (e == null || e instanceof Date) return e;
|
|
532
541
|
if (typeof e == "string") {
|
|
533
542
|
let t = new Date(e);
|
|
@@ -538,19 +547,19 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
538
547
|
return isNaN(t.getTime()) ? e : t;
|
|
539
548
|
}
|
|
540
549
|
return e;
|
|
541
|
-
},
|
|
550
|
+
}, Yn = (e, t, n) => {
|
|
542
551
|
if (e == null) return e;
|
|
543
552
|
switch (t) {
|
|
544
|
-
case "boolean": return n === "postgres" ? e :
|
|
553
|
+
case "boolean": return n === "postgres" ? e : Gn(e);
|
|
545
554
|
case "json":
|
|
546
|
-
case "array": return n === "postgres" ? e :
|
|
555
|
+
case "array": return n === "postgres" ? e : Kn(e);
|
|
547
556
|
case "timestamp":
|
|
548
|
-
case "date": return n === "sqlite" || n === "turso" ?
|
|
557
|
+
case "date": return n === "sqlite" || n === "turso" ? Jn(e) : e;
|
|
549
558
|
case "decimal":
|
|
550
|
-
case "bigint": return
|
|
559
|
+
case "bigint": return qn(e);
|
|
551
560
|
default: return e;
|
|
552
561
|
}
|
|
553
|
-
},
|
|
562
|
+
}, Xn = (e, t, n) => {
|
|
554
563
|
if (e.length === 0) return e;
|
|
555
564
|
let r = p(t);
|
|
556
565
|
if (r === void 0) return e;
|
|
@@ -561,36 +570,36 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
561
570
|
});
|
|
562
571
|
return e.map((e) => {
|
|
563
572
|
let t = { ...e };
|
|
564
|
-
for (let { key: e, type: r } of a) e in t && (t[e] =
|
|
573
|
+
for (let { key: e, type: r } of a) e in t && (t[e] = Yn(t[e], r, n));
|
|
565
574
|
return t;
|
|
566
575
|
});
|
|
567
|
-
},
|
|
576
|
+
}, Zn = (e, t) => {
|
|
568
577
|
if (e == null) return e;
|
|
569
578
|
switch (t) {
|
|
570
579
|
case "json":
|
|
571
580
|
case "array": return typeof e == "string" ? e : JSON.stringify(e);
|
|
572
581
|
default: return e;
|
|
573
582
|
}
|
|
574
|
-
},
|
|
583
|
+
}, Qn = (e, t, n = ["json", "array"]) => {
|
|
575
584
|
let r = p(t);
|
|
576
585
|
if (r === void 0) return e;
|
|
577
586
|
let i = r.fields, a = null;
|
|
578
587
|
for (let [t, r] of Object.entries(i)) if (n.includes(r.type) && t in e) {
|
|
579
|
-
let n =
|
|
588
|
+
let n = Zn(e[t], r.type);
|
|
580
589
|
n !== e[t] && (a === null && (a = { ...e }), a[t] = n);
|
|
581
590
|
}
|
|
582
591
|
return a ?? e;
|
|
583
|
-
},
|
|
592
|
+
}, $n = class extends Error {
|
|
584
593
|
_tag = "EagerCardinalityError";
|
|
585
594
|
constructor(e) {
|
|
586
595
|
super(e), this.name = "EagerCardinalityError";
|
|
587
596
|
}
|
|
588
|
-
},
|
|
597
|
+
}, er = (e) => {
|
|
589
598
|
let { relationName: t, sourceTableName: n, targetTableName: r, foreignKey: i, sourceKey: a } = e, o = r === n;
|
|
590
599
|
return `relation '${t}' on '${n}' is declared \`one\`, but ${r}.${i} matches MORE than one row for the same ${n}.${a} — so there is no single related row to return.` + (o ? ` Because this relation is self-referential, check which side the key is on: \`foreignKey: '${i}'\` means "rows whose ${i} points AT me" (my children — there can be many). If you meant "the row my ${i} points to" (my parent), that is \`sourceKey: '${i}'\`.` : " Use `many(...)` if several are expected, or narrow the key.");
|
|
591
|
-
},
|
|
600
|
+
}, tr = async (e, t, n, r) => {
|
|
592
601
|
if (e.length === 0) return e;
|
|
593
|
-
let i =
|
|
602
|
+
let i = jn(n.tableName);
|
|
594
603
|
if (i === void 0) throw Error(`cannot eager-load on '${n.tableName}': no relations registered. Did you forget to declare \`relations(${n.tableName}, ...)\` or import the file that does?`);
|
|
595
604
|
let a = e.map((e) => ({ ...e }));
|
|
596
605
|
for (let [e, o] of Object.entries(t)) {
|
|
@@ -599,36 +608,36 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
599
608
|
let t = Object.keys(i).join(", ") || "<none>";
|
|
600
609
|
throw Error(`unknown relation '${e}' on '${n.tableName}'. Registered relations: ${t}.`);
|
|
601
610
|
}
|
|
602
|
-
|
|
611
|
+
En(n, e, t), await nr(a, e, t, o === !0 ? {} : o, n, r);
|
|
603
612
|
}
|
|
604
613
|
return a;
|
|
605
|
-
},
|
|
614
|
+
}, nr = async (e, t, n, r, i, a) => {
|
|
606
615
|
switch (n.kind) {
|
|
607
616
|
case "one":
|
|
608
|
-
await
|
|
617
|
+
await rr(e, t, n, r, i, a);
|
|
609
618
|
return;
|
|
610
619
|
case "many":
|
|
611
|
-
await
|
|
620
|
+
await ir(e, t, n, r, i, a);
|
|
612
621
|
return;
|
|
613
622
|
case "manyToMany":
|
|
614
|
-
await
|
|
623
|
+
await or(e, t, n, r, i, a);
|
|
615
624
|
return;
|
|
616
625
|
}
|
|
617
|
-
},
|
|
618
|
-
let o = n.target(), s = n.sourceKey ??
|
|
626
|
+
}, rr = async (e, t, n, r, i, a) => {
|
|
627
|
+
let o = n.target(), s = n.sourceKey ?? sr(i, o.tableName);
|
|
619
628
|
if (s !== void 0) {
|
|
620
|
-
let n =
|
|
629
|
+
let n = cr(e, s), i = n.length === 0 ? [] : await lr(o, "id", n, r, a), c = new Map(i.map((e) => [e.id, e]));
|
|
621
630
|
for (let n of e) {
|
|
622
631
|
let e = n[s];
|
|
623
632
|
n[t] = e == null ? null : c.get(e) ?? null;
|
|
624
633
|
}
|
|
625
|
-
await
|
|
634
|
+
await ur(i, r.with, o, a);
|
|
626
635
|
return;
|
|
627
636
|
}
|
|
628
|
-
let c = n.foreignKey ??
|
|
637
|
+
let c = n.foreignKey ?? N(o, i.tableName, t), l = n.sourceKey ?? "id", u = cr(e, l), d = u.length === 0 ? [] : await lr(o, c, u, r, a), f = /* @__PURE__ */ new Map();
|
|
629
638
|
for (let e of d) {
|
|
630
639
|
let n = e[c];
|
|
631
|
-
if (f.has(n)) throw new
|
|
640
|
+
if (f.has(n)) throw new $n(er({
|
|
632
641
|
relationName: t,
|
|
633
642
|
sourceTableName: i.tableName,
|
|
634
643
|
targetTableName: o.tableName,
|
|
@@ -641,9 +650,9 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
641
650
|
let e = n[l];
|
|
642
651
|
n[t] = e == null ? null : f.get(e) ?? null;
|
|
643
652
|
}
|
|
644
|
-
await
|
|
645
|
-
},
|
|
646
|
-
let o = n.target(), s = n.foreignKey ??
|
|
653
|
+
await ur(d, r.with, o, a);
|
|
654
|
+
}, ir = async (e, t, n, r, i, a) => {
|
|
655
|
+
let o = n.target(), s = n.foreignKey ?? N(o, i.tableName, t), c = n.sourceKey ?? "id", l = cr(e, c), u = ar(r), d = l.length === 0 ? [] : await lr(o, s, l, u, a), f = /* @__PURE__ */ new Map();
|
|
647
656
|
for (let e of d) {
|
|
648
657
|
let t = e[s], n = f.get(t);
|
|
649
658
|
n ? n.push(e) : f.set(t, [e]);
|
|
@@ -659,12 +668,12 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
659
668
|
let e = n[c];
|
|
660
669
|
n[t] = f.get(e) ?? [];
|
|
661
670
|
}
|
|
662
|
-
await
|
|
663
|
-
},
|
|
671
|
+
await ur(d, r.with, o, a);
|
|
672
|
+
}, ar = (e) => {
|
|
664
673
|
let { limit: t, offset: n, ...r } = e;
|
|
665
674
|
return r;
|
|
666
|
-
},
|
|
667
|
-
let o = n.target(), s = n.through(), c =
|
|
675
|
+
}, or = async (e, t, n, r, i, a) => {
|
|
676
|
+
let o = n.target(), s = n.through(), c = cr(e, "id");
|
|
668
677
|
if (c.length === 0) {
|
|
669
678
|
for (let n of e) n[t] = [];
|
|
670
679
|
return;
|
|
@@ -685,8 +694,8 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
685
694
|
for (let n of e) n[t] = [];
|
|
686
695
|
return;
|
|
687
696
|
}
|
|
688
|
-
let f = await
|
|
689
|
-
await
|
|
697
|
+
let f = await lr(o, "id", cr(d, n.targetKey), r, a), p = new Map(f.map((e) => [e.id, e]));
|
|
698
|
+
await ur(f, r.with, o, a);
|
|
690
699
|
let m = r.junction === void 0 ? void 0 : r.junction === !0 ? Object.keys(s.fields) : r.junction, h = /* @__PURE__ */ new Map();
|
|
691
700
|
for (let e of d) {
|
|
692
701
|
let t = e[n.sourceKey], r = e[n.targetKey], i = p.get(r);
|
|
@@ -698,16 +707,16 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
698
707
|
o ? o.push(a) : h.set(t, [a]);
|
|
699
708
|
}
|
|
700
709
|
for (let n of e) n[t] = h.get(n.id) ?? [];
|
|
701
|
-
}, or = (e, t) => {
|
|
702
|
-
for (let [n, r] of Object.entries(e.fields)) if (r.type === "reference" && r.references?.()?.tableName === t) return n;
|
|
703
710
|
}, sr = (e, t) => {
|
|
711
|
+
for (let [n, r] of Object.entries(e.fields)) if (r.type === "reference" && r.references?.()?.tableName === t) return n;
|
|
712
|
+
}, cr = (e, t) => {
|
|
704
713
|
let n = /* @__PURE__ */ new Set();
|
|
705
714
|
for (let r of e) {
|
|
706
715
|
let e = r[t];
|
|
707
716
|
e != null && n.add(e);
|
|
708
717
|
}
|
|
709
718
|
return [...n];
|
|
710
|
-
},
|
|
719
|
+
}, lr = (e, t, n, r, i) => {
|
|
711
720
|
let a = {
|
|
712
721
|
column: t,
|
|
713
722
|
op: "in",
|
|
@@ -721,63 +730,63 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
721
730
|
skip: r.offset,
|
|
722
731
|
projection: void 0
|
|
723
732
|
});
|
|
724
|
-
},
|
|
725
|
-
t !== void 0 && e.length !== 0 && (await
|
|
733
|
+
}, ur = async (e, t, n, r) => {
|
|
734
|
+
t !== void 0 && e.length !== 0 && (await tr(e, t, n, r)).forEach((t, n) => {
|
|
726
735
|
let r = e[n];
|
|
727
736
|
for (let [e, n] of Object.entries(t)) e in r || (r[e] = n);
|
|
728
737
|
});
|
|
729
|
-
},
|
|
738
|
+
}, dr = (e) => e.eager !== void 0 && Object.keys(e.eager).length > 0, fr = class extends Error {
|
|
730
739
|
_tag = "TenantNamespaceUnresolved";
|
|
731
740
|
constructor(e) {
|
|
732
741
|
super(e), this.name = "TenantNamespaceUnresolved";
|
|
733
742
|
}
|
|
734
|
-
},
|
|
743
|
+
}, pr = class extends Error {
|
|
735
744
|
_tag = "TenantNamespaceInvalid";
|
|
736
745
|
constructor(e) {
|
|
737
746
|
super(e), this.name = "TenantNamespaceInvalid";
|
|
738
747
|
}
|
|
739
|
-
},
|
|
740
|
-
if (e == null || e === "") throw new
|
|
741
|
-
let t =
|
|
742
|
-
if (t.replace(/_/g, "") === "") throw new
|
|
743
|
-
let n = `${
|
|
744
|
-
if (n.length >
|
|
748
|
+
}, mr = 63, hr = "tenant_", gr = (e) => e.toLowerCase().replace(/[^a-z0-9_]/g, "_"), _r = (e) => {
|
|
749
|
+
if (e == null || e === "") throw new fr("namespace isolation is on but the request has no resolvable tenant (subject.tenantId is null/empty). Refusing to run against a shared/default namespace — this would be a cross-tenant read. Authenticate with a tenant-scoped subject, or disable namespace isolation for this surface.");
|
|
750
|
+
let t = gr(e);
|
|
751
|
+
if (t.replace(/_/g, "") === "") throw new pr(`tenant id '${e}' has no usable identifier characters after sanitisation — cannot derive a distinct namespace.`);
|
|
752
|
+
let n = `${hr}${t}`;
|
|
753
|
+
if (n.length > mr) throw new pr(`namespace '${n}' for tenant '${e}' exceeds the ${mr}-byte identifier limit. Use a shorter tenant id.`);
|
|
745
754
|
return n;
|
|
746
|
-
},
|
|
747
|
-
if (e === void 0 || t == null || !
|
|
755
|
+
}, vr = (e, t) => e === null ? t : `${e}.${t}`, yr = /* @__PURE__ */ new Set(["timestamp", "date"]), br = /^\d{4}-\d{2}-\d{2}([T ]\d{2}:\d{2})?/, xr = (e, t) => {
|
|
756
|
+
if (e === void 0 || t == null || !yr.has(e) || t instanceof Date) return t;
|
|
748
757
|
if (typeof t == "number" && Number.isFinite(t)) return new Date(t);
|
|
749
|
-
if (typeof t == "string" &&
|
|
758
|
+
if (typeof t == "string" && br.test(t)) {
|
|
750
759
|
let e = new Date(t);
|
|
751
760
|
return Number.isNaN(e.getTime()) ? t : e;
|
|
752
761
|
}
|
|
753
762
|
return t;
|
|
754
|
-
},
|
|
763
|
+
}, Sr = (e, t) => e === void 0 || !yr.has(e) ? t : t.map((t) => xr(e, t)), Cr = (e, t) => {
|
|
755
764
|
let { strings: n, values: r } = e, i = t.unsafe(n[0] ?? "");
|
|
756
765
|
for (let e = 0; e < r.length; e++) i = t`${i}${r[e]}${t.unsafe(n[e + 1] ?? "")}`;
|
|
757
766
|
return t`${i}`;
|
|
758
|
-
},
|
|
767
|
+
}, wr = (e) => e.replace(/[\\%_]/g, (e) => `\\${e}`), F = (e, t, n = null, r) => {
|
|
759
768
|
if ("not" in e) return t`NOT (${F(e.not, t, n, r)})`;
|
|
760
769
|
if ("and" in e) return e.and.length === 0 ? t`TRUE` : t.and(e.and.map((e) => F(e, t, n, r)));
|
|
761
770
|
if ("or" in e) return e.or.length === 0 ? t`FALSE` : t.or(e.or.map((e) => F(e, t, n, r)));
|
|
762
771
|
if (e.op === "subquery-in" || e.op === "subquery-not-in") {
|
|
763
|
-
let r = e, i =
|
|
772
|
+
let r = e, i = Fr(r.subquery, t, n);
|
|
764
773
|
return r.op === "subquery-in" ? t`${t(r.column)} IN (${i})` : t`${t(r.column)} NOT IN (${i})`;
|
|
765
774
|
}
|
|
766
775
|
if (e.op === "exists" || e.op === "not-exists") {
|
|
767
|
-
let r = e, i =
|
|
776
|
+
let r = e, i = Fr(r.subquery, t, n);
|
|
768
777
|
return r.op === "exists" ? t`EXISTS (${i})` : t`NOT EXISTS (${i})`;
|
|
769
778
|
}
|
|
770
779
|
let i = e, a = t(i.column), o = i.path, s = o !== void 0 && o.length > 0, c = () => t.onDialectOrElse({
|
|
771
|
-
mysql: () => t.unsafe(
|
|
772
|
-
mssql: () => t.unsafe(
|
|
773
|
-
sqlite: () => t.unsafe(
|
|
774
|
-
orElse: () => t.unsafe(
|
|
780
|
+
mysql: () => t.unsafe(w(i.column, o, "mysql")),
|
|
781
|
+
mssql: () => t.unsafe(w(i.column, o, "mssql")),
|
|
782
|
+
sqlite: () => t.unsafe(w(i.column, o, "sqlite")),
|
|
783
|
+
orElse: () => t.unsafe(w(i.column, o, "postgres"))
|
|
775
784
|
}), l = () => t.onDialectOrElse({
|
|
776
|
-
mysql: () => t.unsafe(
|
|
777
|
-
mssql: () => t.unsafe(
|
|
778
|
-
sqlite: () => t.unsafe(
|
|
779
|
-
orElse: () => t.unsafe(
|
|
780
|
-
}), u = s ? c() : a, d = s ? l() : a, f = (e) => s && e != null ? String(e) : e, m = (e) => s ? Number(e) : e, h = s || r === void 0 ? void 0 : p(r)?.fields?.[i.column]?.type, g = (e) =>
|
|
785
|
+
mysql: () => t.unsafe(w(i.column, o, "mysql", { numeric: !0 })),
|
|
786
|
+
mssql: () => t.unsafe(w(i.column, o, "mssql", { numeric: !0 })),
|
|
787
|
+
sqlite: () => t.unsafe(w(i.column, o, "sqlite", { numeric: !0 })),
|
|
788
|
+
orElse: () => t.unsafe(w(i.column, o, "postgres", { numeric: !0 }))
|
|
789
|
+
}), u = s ? c() : a, d = s ? l() : a, f = (e) => s && e != null ? String(e) : e, m = (e) => s ? Number(e) : e, h = s || r === void 0 ? void 0 : p(r)?.fields?.[i.column]?.type, g = (e) => xr(h, e);
|
|
781
790
|
switch (i.op) {
|
|
782
791
|
case "eq": return t`${u} = ${f(g(i.value))}`;
|
|
783
792
|
case "neq": return t`${u} <> ${f(g(i.value))}`;
|
|
@@ -794,7 +803,7 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
794
803
|
return e.length === 0 ? t`TRUE` : t`${u} NOT IN ${t.in(s ? e.map((e) => f(e)) : e)}`;
|
|
795
804
|
}
|
|
796
805
|
case "contains": {
|
|
797
|
-
let e = `%${
|
|
806
|
+
let e = `%${wr(typeof i.value == "string" ? i.value : String(i.value ?? ""))}%`;
|
|
798
807
|
return t.onDialectOrElse({
|
|
799
808
|
mysql: () => t`LOWER(${u}) LIKE LOWER(${e})`,
|
|
800
809
|
mssql: () => t`LOWER(${u}) LIKE LOWER(${e})`,
|
|
@@ -803,7 +812,7 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
803
812
|
});
|
|
804
813
|
}
|
|
805
814
|
case "startsWith": {
|
|
806
|
-
let e = `${
|
|
815
|
+
let e = `${wr(typeof i.value == "string" ? i.value : String(i.value ?? ""))}%`;
|
|
807
816
|
return t.onDialectOrElse({
|
|
808
817
|
mysql: () => t`${u} LIKE ${e}`,
|
|
809
818
|
orElse: () => t`${u} LIKE ${e} ESCAPE '\\'`
|
|
@@ -851,13 +860,13 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
851
860
|
});
|
|
852
861
|
}
|
|
853
862
|
}
|
|
854
|
-
},
|
|
863
|
+
}, Tr = (e, t) => {
|
|
855
864
|
let n = [];
|
|
856
865
|
return e.partitionBy && e.partitionBy.length > 0 && n.push(t`PARTITION BY ${t.csv(e.partitionBy.map((e) => t`${t(e)}`))}`), e.orderBy && e.orderBy.length > 0 && n.push(t`ORDER BY ${t.csv(e.orderBy.map((e) => e.direction === "desc" ? t`${t(e.column)} DESC` : t`${t(e.column)} ASC`))}`), n.length === 0 ? t`` : n.length === 1 ? n[0] : n.reduce((e, n, r) => r === 0 ? n : t`${e} ${n}`);
|
|
857
|
-
},
|
|
866
|
+
}, Er = (e, t) => t.csv(e.map((e) => {
|
|
858
867
|
let n = e.column === void 0 ? t.unsafe("*") : t`${t(e.column)}`;
|
|
859
868
|
if (e.op.startsWith("window-")) {
|
|
860
|
-
let r =
|
|
869
|
+
let r = Tr(e.window ?? {}, t);
|
|
861
870
|
switch (e.op) {
|
|
862
871
|
case "window-row-number": return t`ROW_NUMBER() OVER (${r}) AS ${t(e.alias)}`;
|
|
863
872
|
case "window-rank": return t`RANK() OVER (${r}) AS ${t(e.alias)}`;
|
|
@@ -881,44 +890,44 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
881
890
|
return t`${t(e.column)} AS ${t(e.alias)}`;
|
|
882
891
|
default: throw Error(`compileAggregateProjection: unknown op '${e.op}'`);
|
|
883
892
|
}
|
|
884
|
-
})),
|
|
893
|
+
})), Dr = (e) => `[${e.join(",")}]`, Or = (e, t) => {
|
|
885
894
|
if (e.queryVector === void 0) return null;
|
|
886
|
-
let n = t(e.column), r =
|
|
895
|
+
let n = t(e.column), r = Dr(e.queryVector), i = e.distance === "cosine" ? "<=>" : e.distance === "l2" ? "<->" : "<#>", a = e.distance === "cosine" ? "VEC_DISTANCE_COSINE" : "VEC_DISTANCE_EUCLIDEAN";
|
|
887
896
|
return t.onDialectOrElse({
|
|
888
897
|
mysql: () => t`${t.unsafe(a)}(${n}, ${r})`,
|
|
889
898
|
mssql: () => t`NULL`,
|
|
890
899
|
sqlite: () => t`NULL`,
|
|
891
900
|
orElse: () => t`${n} ${t.unsafe(i)} ${r}::vector`
|
|
892
901
|
});
|
|
893
|
-
},
|
|
902
|
+
}, kr = (e, t) => {
|
|
894
903
|
let n = t.unsafe(e.useGeography ? "::geography" : "::geometry");
|
|
895
904
|
return t`ST_Distance(${t(e.column)}${n}, ${e.geom}${n})`;
|
|
896
|
-
},
|
|
897
|
-
let r = n.csv(e.columns.map((e) => n`${n(e)}`)), i = `%${
|
|
905
|
+
}, Ar = (e, t) => t`${t(e.column)} <-> ${e.geom}::geometry`, jr = (e, t) => t`${t(`${e.indexName}_tsv`)}`, Mr = (e, t) => `${t}_${e.indexName}_fts`, Nr = (e, t, n) => {
|
|
906
|
+
let r = n.csv(e.columns.map((e) => n`${n(e)}`)), i = `%${wr(e.query)}%`;
|
|
898
907
|
return n.onDialectOrElse({
|
|
899
908
|
mysql: () => n`MATCH(${r}) AGAINST(${e.query} IN NATURAL LANGUAGE MODE)`,
|
|
900
909
|
sqlite: () => {
|
|
901
|
-
let r = n(
|
|
910
|
+
let r = n(Mr(e, t));
|
|
902
911
|
return n`${n("id")} IN (SELECT ${n("row_id")} FROM ${r} WHERE ${r} MATCH ${e.query})`;
|
|
903
912
|
},
|
|
904
913
|
mssql: () => n.or(e.columns.map((e) => n`LOWER(${n(e)}) LIKE LOWER(${i}) ESCAPE '\\'`)),
|
|
905
|
-
orElse: () => n`${
|
|
914
|
+
orElse: () => n`${jr(e, n)} @@ plainto_tsquery(${e.config ?? "english"}, ${e.query})`
|
|
906
915
|
});
|
|
907
|
-
},
|
|
908
|
-
let r = `%${
|
|
916
|
+
}, Pr = (e, t, n) => {
|
|
917
|
+
let r = `%${wr(e.query)}%`;
|
|
909
918
|
return n.onDialectOrElse({
|
|
910
919
|
mysql: () => n`MATCH(${n.csv(e.columns.map((e) => n`${n(e)}`))}) AGAINST(${e.query} IN NATURAL LANGUAGE MODE)`,
|
|
911
920
|
sqlite: () => {
|
|
912
|
-
let r = n(
|
|
921
|
+
let r = n(Mr(e, t));
|
|
913
922
|
return n`COALESCE((SELECT -bm25(${r}) FROM ${r} WHERE ${r} MATCH ${e.query} AND ${r}.${n("row_id")} = ${n("id")}), 0)`;
|
|
914
923
|
},
|
|
915
924
|
mssql: () => e.columns.map((e) => n`(CASE WHEN LOWER(${n(e)}) LIKE LOWER(${r}) ESCAPE '\\' THEN 1 ELSE 0 END)`).reduce((e, t, r) => r === 0 ? t : n`${e} + ${t}`),
|
|
916
|
-
orElse: () => n`ts_rank(${
|
|
925
|
+
orElse: () => n`ts_rank(${jr(e, n)}, plainto_tsquery(${e.config ?? "english"}, ${e.query}))`
|
|
917
926
|
});
|
|
918
|
-
},
|
|
927
|
+
}, Fr = (e, t, n = null) => {
|
|
919
928
|
if (e.setOp && e.setOp.queries.length >= 2) {
|
|
920
|
-
let r = e.setOp.kind === "union" ? "UNION" : e.setOp.kind === "union-all" ? "UNION ALL" : e.setOp.kind === "intersect" ? "INTERSECT" : "EXCEPT", i = e.setOp.queries.map((e) => t`(${
|
|
921
|
-
if (
|
|
929
|
+
let r = e.setOp.kind === "union" ? "UNION" : e.setOp.kind === "union-all" ? "UNION ALL" : e.setOp.kind === "intersect" ? "INTERSECT" : "EXCEPT", i = e.setOp.queries.map((e) => t`(${Fr(e, t, n)})`).reduce((e, n, i) => i === 0 ? n : t`${e} ${t.unsafe(r)} ${n}`), a = [];
|
|
930
|
+
if (C(e).length > 0 && a.push(t` ORDER BY ${t.csv(C(e).map((e) => e.direction === "desc" ? t`${t(e.column)} DESC` : t`${t(e.column)} ASC`))}`), e.take !== void 0) {
|
|
922
931
|
if (!Number.isInteger(e.take) || e.take < 0) throw Error(`compileSelect: invalid take ${e.take}`);
|
|
923
932
|
a.push(t` LIMIT ${t.unsafe(String(e.take))}`);
|
|
924
933
|
}
|
|
@@ -928,7 +937,7 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
928
937
|
}
|
|
929
938
|
return t`${i}${a.reduce((e, n) => t`${e}${n}`, t``)}`;
|
|
930
939
|
}
|
|
931
|
-
let r = e.annClause === void 0 ? null :
|
|
940
|
+
let r = e.annClause === void 0 ? null : Or(e.annClause, t);
|
|
932
941
|
e.spatialClause !== void 0 && t.onDialectOrElse({
|
|
933
942
|
mysql: () => {
|
|
934
943
|
throw Error("@voltro/plugin-postgis: spatial distance / KNN ordering is postgres-only");
|
|
@@ -941,57 +950,57 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
941
950
|
},
|
|
942
951
|
orElse: () => t``
|
|
943
952
|
});
|
|
944
|
-
let i = e.spatialClause?.projectAs === void 0 ? null :
|
|
953
|
+
let i = e.spatialClause?.projectAs === void 0 ? null : kr(e.spatialClause, t), a = e.aggregations && e.aggregations.length > 0 ? Er(e.aggregations, t) : e.joinedProjection && e.joinedProjection.length > 0 ? t.csv(e.joinedProjection.map((e) => t`${t(e.source)} AS ${t(e.outputKey)}`)) : e.projection && e.projection.length > 0 ? t.csv(e.projection.map((e) => t`${t(e)}`)) : t`*`, o = (e.aggregations && e.aggregations.length > 0) === !0, s = r !== null && !o ? t`${a}, ${r} AS ${t("distance")}` : a, c = e.fullTextSearch, l = c?.rank !== void 0 && !o ? Pr(c, e.table, t) : null, u = l !== null && c?.rank !== void 0 ? t`${s}, ${l} AS ${t(c.rank.alias)}` : s, d = i !== null && !o && e.spatialClause?.projectAs !== void 0 ? t`${u}, ${i} AS ${t(e.spatialClause.projectAs)}` : u, f = e.distinctOn && e.distinctOn.length > 0 ? t.onDialectOrElse({
|
|
945
954
|
orElse: () => t`DISTINCT ON (${t.csv(e.distinctOn.map((e) => t`${t(e)}`))}) `,
|
|
946
955
|
mysql: () => t`DISTINCT `,
|
|
947
956
|
mssql: () => t`DISTINCT `,
|
|
948
957
|
sqlite: () => t`DISTINCT `
|
|
949
|
-
}) : e.distinct ? t`DISTINCT ` : t``, p = e.alias === void 0 ? t`${t(
|
|
958
|
+
}) : e.distinct ? t`DISTINCT ` : t``, p = e.alias === void 0 ? t`${t(vr(n, e.table))}` : t`${t(vr(n, e.table))} AS ${t(e.alias)}`, m = (e.joins ?? []).map((e) => {
|
|
950
959
|
let r = e.kind === "inner" ? "INNER" : "LEFT";
|
|
951
|
-
return t` ${t.unsafe(r)} JOIN ${t(
|
|
952
|
-
}), h = m.length > 0 ? m.reduce((e, n) => t`${e}${n}`, p) : p, g = c === void 0 ? null :
|
|
953
|
-
r !== null &&
|
|
954
|
-
for (let n of
|
|
955
|
-
let
|
|
960
|
+
return t` ${t.unsafe(r)} JOIN ${t(vr(n, e.table))} AS ${t(e.alias)} ON ${F(e.on, t, n)}`;
|
|
961
|
+
}), h = m.length > 0 ? m.reduce((e, n) => t`${e}${n}`, p) : p, g = c === void 0 ? null : Nr(c, e.table, t), ee = [e.predicate ? F(e.predicate, t, n, e.table) : null, g].filter((e) => e !== null), te = ee.length > 0 ? t` WHERE ${t.and(ee)}` : t``, _ = e.groupBy && e.groupBy.length > 0 ? t` GROUP BY ${t.csv(e.groupBy.map((e) => t`${t(e)}`))}` : t``, v = e.having ? t` HAVING ${F(e.having, t, n, e.table)}` : t``, ne = e.skip !== void 0, y = [];
|
|
962
|
+
r !== null && y.push(t`${r} ASC`), e.spatialClause?.order !== void 0 && y.push(e.spatialClause.order === "desc" ? t`${Ar(e.spatialClause, t)} DESC` : t`${Ar(e.spatialClause, t)} ASC`), l !== null && c?.rank?.order === !0 && y.push(t`${l} DESC`);
|
|
963
|
+
for (let n of C(e)) y.push(n.direction === "desc" ? t`${t(n.column)} DESC` : t`${t(n.column)} ASC`);
|
|
964
|
+
let re = y.length > 0 ? t` ORDER BY ${t.csv(y)}` : ne ? t.onDialectOrElse({
|
|
956
965
|
mssql: () => t` ORDER BY (SELECT NULL)`,
|
|
957
966
|
orElse: () => t``
|
|
958
|
-
}) : t``,
|
|
967
|
+
}) : t``, b = (e) => {
|
|
959
968
|
if (!Number.isFinite(e) || !Number.isInteger(e) || e < 0) throw Error(`compileSelect: expected non-negative integer take/skip, got ${e}`);
|
|
960
969
|
return t.unsafe(String(e));
|
|
961
|
-
},
|
|
970
|
+
}, ie = (e.ctes ?? []).map((e) => t`${t(e.name)} AS (${Fr(e.descriptor, t, n)})`), ae = (e.ctes ?? []).some((e) => e.recursive === !0), oe = ie.length > 0 ? t`WITH ${t.unsafe(ae ? "RECURSIVE " : "")}${t.csv(ie)} ` : t``;
|
|
962
971
|
return t.onDialectOrElse({
|
|
963
972
|
mssql: () => {
|
|
964
|
-
let n = e.take !== void 0 && !
|
|
965
|
-
return t`${
|
|
973
|
+
let n = e.take !== void 0 && !ne ? t`TOP ${b(e.take)} ` : t``, r = ne ? t` OFFSET ${b(e.skip)} ROWS FETCH NEXT ${b(e.take ?? 2 ** 53 - 1)} ROWS ONLY` : t``;
|
|
974
|
+
return t`${oe}SELECT ${n}${f}${d} FROM ${h}${te}${_}${v}${re}${r}`;
|
|
966
975
|
},
|
|
967
976
|
orElse: () => {
|
|
968
|
-
let n = e.take === void 0 ? t`` : t` LIMIT ${
|
|
969
|
-
return t`${
|
|
977
|
+
let n = e.take === void 0 ? t`` : t` LIMIT ${b(e.take)}`, r = ne ? t` OFFSET ${b(e.skip)}` : t``;
|
|
978
|
+
return t`${oe}SELECT ${f}${d} FROM ${h}${te}${_}${v}${re}${n}${r}`;
|
|
970
979
|
}
|
|
971
980
|
});
|
|
972
|
-
},
|
|
981
|
+
}, Ir = (e, t, n) => {
|
|
973
982
|
if (e.eager === void 0) return null;
|
|
974
|
-
let r =
|
|
983
|
+
let r = zr(e.table, e.eager, e.sourceTable);
|
|
975
984
|
if (r === null) return null;
|
|
976
|
-
let i =
|
|
985
|
+
let i = Kr(n, e, r, Gr(t, n));
|
|
977
986
|
return i === null ? null : {
|
|
978
987
|
fragment: i,
|
|
979
|
-
decode:
|
|
988
|
+
decode: Yr(r, n)
|
|
980
989
|
};
|
|
981
|
-
},
|
|
990
|
+
}, Lr = (e) => e.relation.kind === "one" && e.fkSide === "target", Rr = (e) => ({
|
|
982
991
|
...e,
|
|
983
992
|
branch: {
|
|
984
993
|
...e.branch,
|
|
985
994
|
limit: 2
|
|
986
995
|
}
|
|
987
|
-
}),
|
|
988
|
-
let r = n ??
|
|
996
|
+
}), zr = (e, t, n) => {
|
|
997
|
+
let r = n ?? _(e), i = jn(e);
|
|
989
998
|
if (i === void 0) return null;
|
|
990
999
|
let a = [];
|
|
991
1000
|
for (let [e, n] of Object.entries(t)) {
|
|
992
1001
|
let t = i[e];
|
|
993
1002
|
if (t === void 0) return null;
|
|
994
|
-
let o =
|
|
1003
|
+
let o = Br(e, t, r, n === !0 ? {} : n);
|
|
995
1004
|
if (o === null) return null;
|
|
996
1005
|
a.push(o);
|
|
997
1006
|
}
|
|
@@ -999,30 +1008,30 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
999
1008
|
table: r,
|
|
1000
1009
|
children: a
|
|
1001
1010
|
};
|
|
1002
|
-
},
|
|
1011
|
+
}, Br = (e, t, n, r) => {
|
|
1003
1012
|
let i = t.target(), a = (t.kind === "manyToMany" ? void 0 : t.sourceKey) ?? "id", o, s;
|
|
1004
1013
|
if (t.kind === "manyToMany") o = t.targetKey, s = "target";
|
|
1005
1014
|
else if (t.kind === "one") {
|
|
1006
|
-
let r = t.sourceKey ??
|
|
1015
|
+
let r = t.sourceKey ?? Wr(n, i.tableName);
|
|
1007
1016
|
if (r !== void 0) o = r, s = "source";
|
|
1008
1017
|
else try {
|
|
1009
|
-
o = t.foreignKey ??
|
|
1018
|
+
o = t.foreignKey ?? N(i, n.tableName, e), s = "target";
|
|
1010
1019
|
} catch {
|
|
1011
1020
|
return null;
|
|
1012
1021
|
}
|
|
1013
1022
|
} else try {
|
|
1014
|
-
o = t.foreignKey ??
|
|
1023
|
+
o = t.foreignKey ?? N(i, n.tableName, e), s = "target";
|
|
1015
1024
|
} catch {
|
|
1016
1025
|
return null;
|
|
1017
1026
|
}
|
|
1018
1027
|
let c = r.with === void 0 ? [] : (() => {
|
|
1019
|
-
let e =
|
|
1028
|
+
let e = jn(i.tableName);
|
|
1020
1029
|
if (e === void 0 && Object.keys(r.with).length > 0) return null;
|
|
1021
1030
|
let t = [];
|
|
1022
1031
|
for (let [n, a] of Object.entries(r.with)) {
|
|
1023
1032
|
let r = e?.[n];
|
|
1024
1033
|
if (r === void 0) return null;
|
|
1025
|
-
let o =
|
|
1034
|
+
let o = Br(n, r, i, a === !0 ? {} : a);
|
|
1026
1035
|
if (o === null) return null;
|
|
1027
1036
|
t.push(o);
|
|
1028
1037
|
}
|
|
@@ -1038,9 +1047,9 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1038
1047
|
fkSide: s,
|
|
1039
1048
|
branch: r,
|
|
1040
1049
|
children: c,
|
|
1041
|
-
junction:
|
|
1050
|
+
junction: Vr(t, r)
|
|
1042
1051
|
};
|
|
1043
|
-
},
|
|
1052
|
+
}, Vr = (e, t) => {
|
|
1044
1053
|
if (e.kind !== "manyToMany" || t.junction === void 0) return;
|
|
1045
1054
|
let n = e.through(), r = t.junction === !0 ? Object.keys(n.fields) : t.junction, i = r.filter((e) => e in n.fields);
|
|
1046
1055
|
return {
|
|
@@ -1052,35 +1061,35 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1052
1061
|
return t !== void 0 && (t.type === "timestamp" || t.type === "date");
|
|
1053
1062
|
})
|
|
1054
1063
|
};
|
|
1055
|
-
},
|
|
1064
|
+
}, Hr = (e) => `__j_${e}`, Ur = "__j", Wr = (e, t) => {
|
|
1056
1065
|
for (let [n, r] of Object.entries(e.fields)) if (r.type === "reference" && r.references?.()?.tableName === t) return n;
|
|
1057
|
-
},
|
|
1066
|
+
}, Gr = (e, t) => {
|
|
1058
1067
|
let n = 0;
|
|
1059
1068
|
return {
|
|
1060
1069
|
sql: e,
|
|
1061
1070
|
dialect: t,
|
|
1062
1071
|
nextAlias: () => `__a${n++}`
|
|
1063
1072
|
};
|
|
1064
|
-
},
|
|
1073
|
+
}, Kr = (e, t, n, r) => {
|
|
1065
1074
|
switch (e) {
|
|
1066
|
-
case "postgres": return
|
|
1075
|
+
case "postgres": return Qr(t, n, r);
|
|
1067
1076
|
case "sqlite":
|
|
1068
|
-
case "turso": return
|
|
1077
|
+
case "turso": return oi(t, n, r);
|
|
1069
1078
|
case "mysql":
|
|
1070
|
-
case "mariadb": return
|
|
1071
|
-
case "mssql": return
|
|
1079
|
+
case "mariadb": return gi(t, n, r);
|
|
1080
|
+
case "mssql": return Ci(t, n, r);
|
|
1072
1081
|
}
|
|
1073
|
-
},
|
|
1074
|
-
let n =
|
|
1082
|
+
}, qr = (e, t) => t === "mssql" && !Jr.test(e) ? /* @__PURE__ */ new Date(`${e}Z`) : new Date(e), Jr = /(?:Z|[+-]\d{2}:?\d{2})$/, Yr = (e, t) => {
|
|
1083
|
+
let n = Xr(e.table, e.children, t);
|
|
1075
1084
|
return (e) => e.map((e) => {
|
|
1076
1085
|
let t = e.__row, r = typeof t == "string" ? JSON.parse(t) : t;
|
|
1077
1086
|
return n(r);
|
|
1078
1087
|
});
|
|
1079
|
-
},
|
|
1088
|
+
}, Xr = (e, t, n) => {
|
|
1080
1089
|
let r = [];
|
|
1081
1090
|
for (let [t, n] of Object.entries(e.fields)) (n.type === "timestamp" || n.type === "date") && r.push(t);
|
|
1082
1091
|
let i = t.map((e) => {
|
|
1083
|
-
let t =
|
|
1092
|
+
let t = Xr(e.target, e.children, n), r = e.relation.kind === "one", i = Lr(e), a = i ? er({
|
|
1084
1093
|
relationName: e.relationName,
|
|
1085
1094
|
sourceTableName: e.source.tableName,
|
|
1086
1095
|
targetTableName: e.target.tableName,
|
|
@@ -1093,14 +1102,14 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1093
1102
|
isSingle: r,
|
|
1094
1103
|
fromOverFetch: i,
|
|
1095
1104
|
cardinalityMessage: a,
|
|
1096
|
-
junction:
|
|
1105
|
+
junction: Zr(e.junction, n)
|
|
1097
1106
|
};
|
|
1098
1107
|
});
|
|
1099
1108
|
return (e) => {
|
|
1100
1109
|
let t = { ...e };
|
|
1101
1110
|
for (let e of r) {
|
|
1102
1111
|
let r = t[e];
|
|
1103
|
-
typeof r == "string" && (t[e] =
|
|
1112
|
+
typeof r == "string" && (t[e] = qr(r, n));
|
|
1104
1113
|
}
|
|
1105
1114
|
for (let e of i) {
|
|
1106
1115
|
let n = t[e.name];
|
|
@@ -1110,7 +1119,7 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1110
1119
|
}
|
|
1111
1120
|
if (e.fromOverFetch) {
|
|
1112
1121
|
let r = n;
|
|
1113
|
-
if (r.length > 1) throw new
|
|
1122
|
+
if (r.length > 1) throw new $n(e.cardinalityMessage);
|
|
1114
1123
|
t[e.name] = r.length === 0 ? null : e.decode(r[0]);
|
|
1115
1124
|
continue;
|
|
1116
1125
|
}
|
|
@@ -1122,202 +1131,202 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1122
1131
|
}
|
|
1123
1132
|
return t;
|
|
1124
1133
|
};
|
|
1125
|
-
},
|
|
1134
|
+
}, Zr = (e, t) => {
|
|
1126
1135
|
if (e === void 0) return;
|
|
1127
1136
|
let n = new Set(e.dateColumns), r = e.requested;
|
|
1128
1137
|
return (e) => {
|
|
1129
1138
|
let i = e._junction ?? {}, a = {};
|
|
1130
1139
|
for (let e of r) {
|
|
1131
1140
|
let r = i[e];
|
|
1132
|
-
a[e] = n.has(e) && typeof r == "string" ?
|
|
1141
|
+
a[e] = n.has(e) && typeof r == "string" ? qr(r, t) : r;
|
|
1133
1142
|
}
|
|
1134
1143
|
return {
|
|
1135
1144
|
...e,
|
|
1136
1145
|
_junction: a
|
|
1137
1146
|
};
|
|
1138
1147
|
};
|
|
1139
|
-
},
|
|
1140
|
-
let r = n.sql, i = n.nextAlias(), a = r(t.table.tableName), o = r(i), s =
|
|
1141
|
-
return s === null ? null : r`SELECT ${s} AS "__row" FROM ${a} AS ${o}${e.predicate ? r` WHERE ${V(e.predicate, i, r)}` : r``}${
|
|
1142
|
-
},
|
|
1143
|
-
let i = n.sql, a = r === void 0 ? i`to_jsonb(${i(e)}.*)` : i`(to_jsonb(${i(e)}.*) - ${z(
|
|
1148
|
+
}, Qr = (e, t, n) => {
|
|
1149
|
+
let r = n.sql, i = n.nextAlias(), a = r(t.table.tableName), o = r(i), s = $r(i, t.children, n);
|
|
1150
|
+
return s === null ? null : r`SELECT ${s} AS "__row" FROM ${a} AS ${o}${e.predicate ? r` WHERE ${V(e.predicate, i, r)}` : r``}${C(e).length > 0 ? r` ORDER BY ${r.csv(C(e).map((e) => e.direction === "desc" ? r`${R(i, e.column, r)} DESC` : r`${R(i, e.column, r)} ASC`))}` : r``}${e.take === void 0 ? r`` : r` LIMIT ${B(e.take, r)}`}${e.skip === void 0 ? r`` : r` OFFSET ${B(e.skip, r)}`}`;
|
|
1151
|
+
}, $r = (e, t, n, r) => {
|
|
1152
|
+
let i = n.sql, a = r === void 0 ? i`to_jsonb(${i(e)}.*)` : i`(to_jsonb(${i(e)}.*) - ${z(Ur, i)}::text)`;
|
|
1144
1153
|
if (t.length === 0 && r === void 0) return a;
|
|
1145
1154
|
let o = [];
|
|
1146
|
-
r !== void 0 && o.push(i`${z("_junction", i)}, ${R(e,
|
|
1155
|
+
r !== void 0 && o.push(i`${z("_junction", i)}, ${R(e, Ur, i)}`);
|
|
1147
1156
|
for (let r of t) {
|
|
1148
|
-
let t =
|
|
1157
|
+
let t = ei(r, e, n);
|
|
1149
1158
|
if (t === null) return null;
|
|
1150
1159
|
o.push(i`${z(r.relationName, i)}, ${t}`);
|
|
1151
1160
|
}
|
|
1152
1161
|
return i`${a} || jsonb_build_object(${i.csv(o)})`;
|
|
1153
|
-
},
|
|
1162
|
+
}, ei = (e, t, n) => {
|
|
1154
1163
|
switch (e.relation.kind) {
|
|
1155
|
-
case "one": return
|
|
1156
|
-
case "many": return
|
|
1157
|
-
case "manyToMany": return
|
|
1164
|
+
case "one": return Lr(e) ? ni(Rr(e), t, n) : ti(e, t, n);
|
|
1165
|
+
case "many": return ni(e, t, n);
|
|
1166
|
+
case "manyToMany": return ri(e, t, n);
|
|
1158
1167
|
}
|
|
1159
|
-
},
|
|
1160
|
-
let r = n.sql, i = n.nextAlias(), a =
|
|
1168
|
+
}, ti = (e, t, n) => {
|
|
1169
|
+
let r = n.sql, i = n.nextAlias(), a = $r(i, e.children, n);
|
|
1161
1170
|
if (a === null) return null;
|
|
1162
1171
|
let o = e.fkSide === "source" ? r`${R(i, "id", r)} = ${R(t, e.foreignKey, r)}` : r`${R(i, e.foreignKey, r)} = ${R(t, e.sourceKey, r)}`, s = e.branch.where === void 0 ? r`` : r` AND ${V(e.branch.where, i, r)}`, c = I(i, e.branch.orderBy, r);
|
|
1163
1172
|
return r`(SELECT ${a} FROM ${r(e.target.tableName)} AS ${r(i)} WHERE ${o}${s}${c} LIMIT 1)`;
|
|
1164
|
-
},
|
|
1165
|
-
let r = n.sql, i = n.nextAlias(), a =
|
|
1173
|
+
}, ni = (e, t, n) => {
|
|
1174
|
+
let r = n.sql, i = n.nextAlias(), a = $r(i, e.children, n);
|
|
1166
1175
|
if (a === null) return null;
|
|
1167
|
-
let o = r`${R(void 0, e.foreignKey, r)} = ${R(t, e.sourceKey, r)}`, s = e.branch.where === void 0 ? r`` : r` AND ${F(e.branch.where, r)}`, c =
|
|
1176
|
+
let o = r`${R(void 0, e.foreignKey, r)} = ${R(t, e.sourceKey, r)}`, s = e.branch.where === void 0 ? r`` : r` AND ${F(e.branch.where, r)}`, c = ai(e.branch.orderBy, r), l = e.branch.limit === void 0 ? r`` : r` LIMIT ${B(e.branch.limit, r)}`, u = e.branch.offset === void 0 ? r`` : r` OFFSET ${B(e.branch.offset, r)}`;
|
|
1168
1177
|
return r`COALESCE((SELECT jsonb_agg(${a}) FROM (SELECT * FROM ${r(e.target.tableName)} WHERE ${o}${s}${c}${l}${u}) AS ${r(i)}), '[]'::jsonb)`;
|
|
1169
|
-
},
|
|
1170
|
-
let r = n.sql, i = e.relation, a = i.through(), o = n.nextAlias(), s = n.nextAlias(), c =
|
|
1178
|
+
}, ri = (e, t, n) => {
|
|
1179
|
+
let r = n.sql, i = e.relation, a = i.through(), o = n.nextAlias(), s = n.nextAlias(), c = $r(o, e.children, n, e.junction);
|
|
1171
1180
|
if (c === null) return null;
|
|
1172
|
-
let l = e.branch.where === void 0 ? r`` : r` AND ${F(e.branch.where, r)}`, u =
|
|
1181
|
+
let l = e.branch.where === void 0 ? r`` : r` AND ${F(e.branch.where, r)}`, u = ji(e, s, r), d = I(o, e.branch.orderBy, r), f = e.branch.limit === void 0 ? r`` : r` LIMIT ${B(e.branch.limit, r)}`, p = e.branch.offset === void 0 ? r`` : r` OFFSET ${B(e.branch.offset, r)}`, m = e.junction === void 0 ? r`` : r`, ${ii(e.junction, s, r)} AS ${r(Ur)}`;
|
|
1173
1182
|
return r`COALESCE((SELECT jsonb_agg(${c}) FROM (SELECT ${r(o)}.*${m} FROM ${r(a.tableName)} AS ${r(s)} INNER JOIN ${r(e.target.tableName)} AS ${r(o)} ON ${R(o, "id", r)} = ${R(s, i.targetKey, r)} WHERE ${R(s, i.sourceKey, r)} = ${R(t, "id", r)}${u}${l}${d}${f}${p}) AS ${r(o)}), '[]'::jsonb)`;
|
|
1174
|
-
},
|
|
1175
|
-
let r = n.sql, i = n.nextAlias(), a =
|
|
1183
|
+
}, ii = (e, t, n) => e.present.length === 0 ? n`jsonb_build_object()` : n`jsonb_build_object(${n.csv(e.present.map((e) => n`${z(e, n)}, ${R(t, e, n)}`))})`, I = (e, t, n) => t === void 0 || t.length === 0 ? n`` : n` ORDER BY ${n.csv(t.map((t) => t.direction === "desc" ? n`${R(e, t.column, n)} DESC` : n`${R(e, t.column, n)} ASC`))}`, ai = (e, t) => e === void 0 || e.length === 0 ? t`` : t` ORDER BY ${t.csv(e.map((e) => e.direction === "desc" ? t`${t(e.column)} DESC` : t`${t(e.column)} ASC`))}`, oi = (e, t, n) => {
|
|
1184
|
+
let r = n.sql, i = n.nextAlias(), a = si(i, t.table, t.children, n);
|
|
1176
1185
|
if (a === null) return null;
|
|
1177
|
-
let o = e.predicate ? r` WHERE ${V(e.predicate, i, r)}` : r``, s =
|
|
1186
|
+
let o = e.predicate ? r` WHERE ${V(e.predicate, i, r)}` : r``, s = C(e).length > 0 ? r` ORDER BY ${r.csv(C(e).map((e) => e.direction === "desc" ? r`${R(i, e.column, r)} DESC` : r`${R(i, e.column, r)} ASC`))}` : r``, c = e.take === void 0 ? r`` : r` LIMIT ${B(e.take, r)}`, l = e.skip === void 0 ? r`` : r` OFFSET ${B(e.skip, r)}`;
|
|
1178
1187
|
return r`SELECT ${a} AS "__row" FROM ${r(t.table.tableName)} AS ${r(i)}${o}${s}${c}${l}`;
|
|
1179
|
-
},
|
|
1188
|
+
}, si = (e, t, n, r, i) => {
|
|
1180
1189
|
let a = r.sql, o = [];
|
|
1181
1190
|
for (let n of Object.keys(t.fields)) o.push(a`${z(n, a)}, ${R(e, n, a)}`);
|
|
1182
1191
|
if (i !== void 0) {
|
|
1183
|
-
let e =
|
|
1192
|
+
let e = ci(i.projection, i.source, a);
|
|
1184
1193
|
o.push(a`${z("_junction", a)}, json_object(${e})`);
|
|
1185
1194
|
}
|
|
1186
1195
|
for (let t of n) {
|
|
1187
|
-
let n =
|
|
1196
|
+
let n = fi(t, e, r);
|
|
1188
1197
|
if (n === null) return null;
|
|
1189
1198
|
o.push(a`${z(t.relationName, a)}, ${n}`);
|
|
1190
1199
|
}
|
|
1191
1200
|
return a`json_object(${a.csv(o)})`;
|
|
1192
|
-
},
|
|
1201
|
+
}, ci = (e, t, n) => e.present.length === 0 ? n`` : n.csv(e.present.map((e) => n`${z(e, n)}, ${R(t.alias, t.physical(e), n)}`)), li = (e, t, n) => e.present.length === 0 ? n`` : n`, ${n.csv(e.present.map((e) => n`${R(t, e, n)} AS ${n(Hr(e))}`))}`, ui = (e) => ({
|
|
1193
1202
|
alias: e,
|
|
1194
|
-
physical:
|
|
1195
|
-
}),
|
|
1203
|
+
physical: Hr
|
|
1204
|
+
}), di = (e) => ({
|
|
1196
1205
|
alias: e,
|
|
1197
1206
|
physical: (e) => e
|
|
1198
|
-
}),
|
|
1207
|
+
}), fi = (e, t, n) => {
|
|
1199
1208
|
switch (e.relation.kind) {
|
|
1200
|
-
case "one": return
|
|
1201
|
-
case "many": return
|
|
1202
|
-
case "manyToMany": return
|
|
1209
|
+
case "one": return Lr(e) ? mi(Rr(e), t, n) : pi(e, t, n);
|
|
1210
|
+
case "many": return mi(e, t, n);
|
|
1211
|
+
case "manyToMany": return hi(e, t, n);
|
|
1203
1212
|
}
|
|
1204
|
-
},
|
|
1205
|
-
let r = n.sql, i = n.nextAlias(), a =
|
|
1213
|
+
}, pi = (e, t, n) => {
|
|
1214
|
+
let r = n.sql, i = n.nextAlias(), a = si(i, e.target, e.children, n);
|
|
1206
1215
|
if (a === null) return null;
|
|
1207
1216
|
let o = e.fkSide === "source" ? r`${R(i, "id", r)} = ${R(t, e.foreignKey, r)}` : r`${R(i, e.foreignKey, r)} = ${R(t, e.sourceKey, r)}`;
|
|
1208
1217
|
return r`(SELECT ${a} FROM ${r(e.target.tableName)} AS ${r(i)} WHERE ${o} LIMIT 1)`;
|
|
1209
|
-
},
|
|
1210
|
-
let r = n.sql, i = n.nextAlias(), a =
|
|
1218
|
+
}, mi = (e, t, n) => {
|
|
1219
|
+
let r = n.sql, i = n.nextAlias(), a = si(i, e.target, e.children, n);
|
|
1211
1220
|
if (a === null) return null;
|
|
1212
|
-
let o = r`${R(void 0, e.foreignKey, r)} = ${R(t, e.sourceKey, r)}`, s = e.branch.where === void 0 ? r`` : r` AND ${F(H(e.branch.where), r)}`, c =
|
|
1221
|
+
let o = r`${R(void 0, e.foreignKey, r)} = ${R(t, e.sourceKey, r)}`, s = e.branch.where === void 0 ? r`` : r` AND ${F(H(e.branch.where), r)}`, c = ai(e.branch.orderBy, r), l = e.branch.limit === void 0 ? r`` : r` LIMIT ${B(e.branch.limit, r)}`, u = e.branch.offset === void 0 ? r`` : r` OFFSET ${B(e.branch.offset, r)}`;
|
|
1213
1222
|
return r`COALESCE((SELECT json_group_array(${a}) FROM (SELECT * FROM ${r(e.target.tableName)} WHERE ${o}${s}${c}${l}${u}) AS ${r(i)}), json('[]'))`;
|
|
1214
|
-
},
|
|
1215
|
-
let r = n.sql, i = e.relation, a = i.through(), o = n.nextAlias(), s = n.nextAlias(), c =
|
|
1223
|
+
}, hi = (e, t, n) => {
|
|
1224
|
+
let r = n.sql, i = e.relation, a = i.through(), o = n.nextAlias(), s = n.nextAlias(), c = si(o, e.target, e.children, n, e.junction === void 0 ? void 0 : {
|
|
1216
1225
|
projection: e.junction,
|
|
1217
|
-
source:
|
|
1226
|
+
source: ui(o)
|
|
1218
1227
|
});
|
|
1219
1228
|
if (c === null) return null;
|
|
1220
|
-
let l = e.branch.where === void 0 ? r`` : r` AND ${F(H(e.branch.where), r)}`, u =
|
|
1229
|
+
let l = e.branch.where === void 0 ? r`` : r` AND ${F(H(e.branch.where), r)}`, u = ji(e, s, r, !0), d = I(o, e.branch.orderBy, r), f = e.branch.limit === void 0 ? r`` : r` LIMIT ${B(e.branch.limit, r)}`, p = e.branch.offset === void 0 ? r`` : r` OFFSET ${B(e.branch.offset, r)}`, m = e.junction === void 0 ? r`` : li(e.junction, s, r);
|
|
1221
1230
|
return r`COALESCE((SELECT json_group_array(${c}) FROM (SELECT ${r(o)}.*${m} FROM ${r(a.tableName)} AS ${r(s)} INNER JOIN ${r(e.target.tableName)} AS ${r(o)} ON ${R(o, "id", r)} = ${R(s, i.targetKey, r)} WHERE ${R(s, i.sourceKey, r)} = ${R(t, "id", r)}${u}${l}${d}${f}${p}) AS ${r(o)}), json('[]'))`;
|
|
1222
|
-
},
|
|
1231
|
+
}, gi = (e, t, n) => {
|
|
1223
1232
|
let r = n.sql, i = n.nextAlias(), a = L(i, t.table, t.children, n);
|
|
1224
1233
|
if (a === null) return null;
|
|
1225
|
-
let o = e.predicate ? r` WHERE ${V(e.predicate, i, r)}` : r``, s =
|
|
1234
|
+
let o = e.predicate ? r` WHERE ${V(e.predicate, i, r)}` : r``, s = C(e).length > 0 ? r` ORDER BY ${r.csv(C(e).map((e) => e.direction === "desc" ? r`${R(i, e.column, r)} DESC` : r`${R(i, e.column, r)} ASC`))}` : r``, c = e.take === void 0 ? r`` : r` LIMIT ${B(e.take, r)}`, l = e.skip === void 0 ? r`` : r` OFFSET ${B(e.skip, r)}`;
|
|
1226
1235
|
return r`SELECT ${a} AS \`__row\` FROM ${r(t.table.tableName)} AS ${r(i)}${o}${s}${c}${l}`;
|
|
1227
1236
|
}, L = (e, t, n, r, i) => {
|
|
1228
1237
|
let a = r.sql, o = [];
|
|
1229
1238
|
for (let n of Object.keys(t.fields)) o.push(a`${z(n, a)}, ${R(e, n, a)}`);
|
|
1230
1239
|
if (i !== void 0) {
|
|
1231
|
-
let e =
|
|
1240
|
+
let e = ci(i.projection, i.source, a);
|
|
1232
1241
|
o.push(a`${z("_junction", a)}, JSON_OBJECT(${e})`);
|
|
1233
1242
|
}
|
|
1234
1243
|
for (let t of n) {
|
|
1235
|
-
let n =
|
|
1244
|
+
let n = _i(t, e, r);
|
|
1236
1245
|
if (n === null) return null;
|
|
1237
1246
|
o.push(a`${z(t.relationName, a)}, ${n}`);
|
|
1238
1247
|
}
|
|
1239
1248
|
return a`JSON_OBJECT(${a.csv(o)})`;
|
|
1240
|
-
},
|
|
1249
|
+
}, _i = (e, t, n) => {
|
|
1241
1250
|
switch (e.relation.kind) {
|
|
1242
1251
|
case "one":
|
|
1243
|
-
if (
|
|
1244
|
-
let r =
|
|
1245
|
-
return n.dialect === "mariadb" ?
|
|
1252
|
+
if (Lr(e)) {
|
|
1253
|
+
let r = Rr(e);
|
|
1254
|
+
return n.dialect === "mariadb" ? xi(r, t, n) : yi(r, t, n);
|
|
1246
1255
|
}
|
|
1247
|
-
return
|
|
1248
|
-
case "many": return n.dialect === "mariadb" ?
|
|
1249
|
-
case "manyToMany": return n.dialect === "mariadb" ?
|
|
1256
|
+
return vi(e, t, n);
|
|
1257
|
+
case "many": return n.dialect === "mariadb" ? xi(e, t, n) : yi(e, t, n);
|
|
1258
|
+
case "manyToMany": return n.dialect === "mariadb" ? Si(e, t, n) : bi(e, t, n);
|
|
1250
1259
|
}
|
|
1251
|
-
},
|
|
1260
|
+
}, vi = (e, t, n) => {
|
|
1252
1261
|
let r = n.sql, i = n.nextAlias(), a = L(i, e.target, e.children, n);
|
|
1253
1262
|
if (a === null) return null;
|
|
1254
1263
|
let o = e.fkSide === "source" ? r`${R(i, "id", r)} = ${R(t, e.foreignKey, r)}` : r`${R(i, e.foreignKey, r)} = ${R(t, e.sourceKey, r)}`;
|
|
1255
1264
|
return r`(SELECT ${a} FROM ${r(e.target.tableName)} AS ${r(i)} WHERE ${o} LIMIT 1)`;
|
|
1256
|
-
},
|
|
1265
|
+
}, yi = (e, t, n) => {
|
|
1257
1266
|
let r = n.sql, i = n.nextAlias(), a = L(i, e.target, e.children, n);
|
|
1258
1267
|
if (a === null) return null;
|
|
1259
|
-
let o = r`${R(void 0, e.foreignKey, r)} = ${R(t, e.sourceKey, r)}`, s = e.branch.where === void 0 ? r`` : r` AND ${F(H(e.branch.where), r)}`, c =
|
|
1268
|
+
let o = r`${R(void 0, e.foreignKey, r)} = ${R(t, e.sourceKey, r)}`, s = e.branch.where === void 0 ? r`` : r` AND ${F(H(e.branch.where), r)}`, c = ai(e.branch.orderBy, r), l = e.branch.limit === void 0 ? r`` : r` LIMIT ${B(e.branch.limit, r)}`, u = e.branch.offset === void 0 ? r`` : r` OFFSET ${B(e.branch.offset, r)}`;
|
|
1260
1269
|
return r`COALESCE((SELECT JSON_ARRAYAGG(${a}) FROM (SELECT * FROM ${r(e.target.tableName)} WHERE ${o}${s}${c}${l}${u}) AS ${r(i)}), JSON_ARRAY())`;
|
|
1261
|
-
},
|
|
1270
|
+
}, bi = (e, t, n) => {
|
|
1262
1271
|
let r = n.sql, i = e.relation, a = i.through(), o = n.nextAlias(), s = n.nextAlias(), c = L(o, e.target, e.children, n, e.junction === void 0 ? void 0 : {
|
|
1263
1272
|
projection: e.junction,
|
|
1264
|
-
source:
|
|
1273
|
+
source: ui(o)
|
|
1265
1274
|
});
|
|
1266
1275
|
if (c === null) return null;
|
|
1267
|
-
let l = e.branch.where === void 0 ? r`` : r` AND ${F(H(e.branch.where), r)}`, u =
|
|
1276
|
+
let l = e.branch.where === void 0 ? r`` : r` AND ${F(H(e.branch.where), r)}`, u = ji(e, s, r, !0), d = I(o, e.branch.orderBy, r), f = e.branch.limit === void 0 ? r`` : r` LIMIT ${B(e.branch.limit, r)}`, p = e.branch.offset === void 0 ? r`` : r` OFFSET ${B(e.branch.offset, r)}`, m = e.junction === void 0 ? r`` : li(e.junction, s, r);
|
|
1268
1277
|
return r`COALESCE((SELECT JSON_ARRAYAGG(${c}) FROM (SELECT ${r(o)}.*${m} FROM ${r(a.tableName)} AS ${r(s)} INNER JOIN ${r(e.target.tableName)} AS ${r(o)} ON ${R(o, "id", r)} = ${R(s, i.targetKey, r)} WHERE ${R(s, i.sourceKey, r)} = ${R(t, "id", r)}${u}${l}${d}${f}${p}) AS ${r(o)}), JSON_ARRAY())`;
|
|
1269
|
-
},
|
|
1278
|
+
}, xi = (e, t, n) => {
|
|
1270
1279
|
let r = n.sql, i = n.nextAlias(), a = L(i, e.target, e.children, n);
|
|
1271
1280
|
if (a === null) return null;
|
|
1272
1281
|
let o = r`${R(i, e.foreignKey, r)} = ${R(t, e.sourceKey, r)}`, s = e.branch.where === void 0 ? r`` : r` AND ${F(H(e.branch.where), r)}`, c = e.branch.orderBy ?? [];
|
|
1273
1282
|
if (e.branch.limit === void 0 && e.branch.offset === void 0) return r`COALESCE((SELECT JSON_ARRAYAGG(${a}${c.length > 0 ? r` ORDER BY ${r.csv(c.map((e) => e.direction === "desc" ? r`${R(i, e.column, r)} DESC` : r`${R(i, e.column, r)} ASC`))}` : r``}) FROM ${r(e.target.tableName)} AS ${r(i)} WHERE ${o}${s}), JSON_ARRAY())`;
|
|
1274
1283
|
let l = c.length > 0 ? r` ORDER BY ${r.csv(c.map((e) => e.direction === "desc" ? r`${r(e.column)} DESC` : r`${r(e.column)} ASC`))}` : r` ORDER BY ${r("id")}`, u = e.branch.limit, d = e.branch.offset, f = u !== void 0 && d !== void 0 ? r` AND ${R(i, "rn", r)} > ${B(d, r)} AND ${R(i, "rn", r)} <= ${B(d + u, r)}` : u === void 0 ? r` AND ${R(i, "rn", r)} > ${B(d, r)}` : r` AND ${R(i, "rn", r)} <= ${B(u, r)}`;
|
|
1275
1284
|
return r`COALESCE((SELECT JSON_ARRAYAGG(${a} ORDER BY ${R(i, "rn", r)}) FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY ${r(e.foreignKey)}${l}) AS rn FROM ${r(e.target.tableName)}) AS ${r(i)} WHERE ${o}${s}${f}), JSON_ARRAY())`;
|
|
1276
|
-
},
|
|
1285
|
+
}, Si = (e, t, n) => {
|
|
1277
1286
|
let r = n.sql, i = e.relation, a = i.through(), o = n.nextAlias(), s = n.nextAlias(), c = (t) => e.junction === void 0 ? void 0 : {
|
|
1278
1287
|
projection: e.junction,
|
|
1279
1288
|
source: t
|
|
1280
|
-
}, l = r`${R(s, i.sourceKey, r)} = ${R(t, "id", r)}`, u = r`${R(o, "id", r)} = ${R(s, i.targetKey, r)}`, d = e.branch.where === void 0 ? r`` : r` AND ${F(H(e.branch.where), r)}`, f =
|
|
1289
|
+
}, l = r`${R(s, i.sourceKey, r)} = ${R(t, "id", r)}`, u = r`${R(o, "id", r)} = ${R(s, i.targetKey, r)}`, d = e.branch.where === void 0 ? r`` : r` AND ${F(H(e.branch.where), r)}`, f = ji(e, s, r, !0), p = e.branch.orderBy ?? [];
|
|
1281
1290
|
if (e.branch.limit === void 0 && e.branch.offset === void 0) {
|
|
1282
|
-
let t = L(o, e.target, e.children, n, c(
|
|
1291
|
+
let t = L(o, e.target, e.children, n, c(di(s)));
|
|
1283
1292
|
return t === null ? null : r`COALESCE((SELECT JSON_ARRAYAGG(${t}${p.length > 0 ? r` ORDER BY ${r.csv(p.map((e) => e.direction === "desc" ? r`${R(o, e.column, r)} DESC` : r`${R(o, e.column, r)} ASC`))}` : r``}) FROM ${r(a.tableName)} AS ${r(s)} INNER JOIN ${r(e.target.tableName)} AS ${r(o)} ON ${u} WHERE ${l}${f}${d}), JSON_ARRAY())`;
|
|
1284
1293
|
}
|
|
1285
|
-
let m = p.length > 0 ? r` ORDER BY ${r.csv(p.map((e) => e.direction === "desc" ? r`${R(o, e.column, r)} DESC` : r`${R(o, e.column, r)} ASC`))}` : r` ORDER BY ${R(o, "id", r)}`, h = e.branch.limit, g = e.branch.offset,
|
|
1286
|
-
if (
|
|
1287
|
-
let
|
|
1288
|
-
return r`COALESCE((SELECT JSON_ARRAYAGG(${
|
|
1289
|
-
},
|
|
1290
|
-
let r = n.sql, i = n.nextAlias(), a =
|
|
1294
|
+
let m = p.length > 0 ? r` ORDER BY ${r.csv(p.map((e) => e.direction === "desc" ? r`${R(o, e.column, r)} DESC` : r`${R(o, e.column, r)} ASC`))}` : r` ORDER BY ${R(o, "id", r)}`, h = e.branch.limit, g = e.branch.offset, ee = h !== void 0 && g !== void 0 ? r` AND ${R(o, "rn", r)} > ${B(g, r)} AND ${R(o, "rn", r)} <= ${B(g + h, r)}` : h === void 0 ? r` AND ${R(o, "rn", r)} > ${B(g, r)}` : r` AND ${R(o, "rn", r)} <= ${B(h, r)}`, te = e.branch.onJunction === void 0 ? r`` : r` WHERE ${V(H(e.branch.onJunction), s, r)}`, _ = L(o, e.target, e.children, n, c(ui(o)));
|
|
1295
|
+
if (_ === null) return null;
|
|
1296
|
+
let v = e.junction === void 0 ? r`` : li(e.junction, s, r);
|
|
1297
|
+
return r`COALESCE((SELECT JSON_ARRAYAGG(${_} ORDER BY ${R(o, "rn", r)}) FROM (SELECT ${r(o)}.*${v}, ${R(s, i.sourceKey, r)} AS ${r(`__src_${i.sourceKey}`)}, ROW_NUMBER() OVER (PARTITION BY ${R(s, i.sourceKey, r)}${m}) AS rn FROM ${r(a.tableName)} AS ${r(s)} INNER JOIN ${r(e.target.tableName)} AS ${r(o)} ON ${u}${te}) AS ${r(o)} WHERE ${R(o, `__src_${i.sourceKey}`, r)} = ${R(t, "id", r)}${d}${ee}), JSON_ARRAY())`;
|
|
1298
|
+
}, Ci = (e, t, n) => {
|
|
1299
|
+
let r = n.sql, i = n.nextAlias(), a = wi(i, t.table, t.children, n);
|
|
1291
1300
|
if (a === null) return null;
|
|
1292
|
-
let o = e.predicate ? r` WHERE ${V(e.predicate, i, r)}` : r``, s =
|
|
1301
|
+
let o = e.predicate ? r` WHERE ${V(e.predicate, i, r)}` : r``, s = C(e).length > 0 ? r` ORDER BY ${r.csv(C(e).map((e) => e.direction === "desc" ? r`${R(i, e.column, r)} DESC` : r`${R(i, e.column, r)} ASC`))}` : r``, c = r``, l = r``;
|
|
1293
1302
|
if (e.skip !== void 0) {
|
|
1294
|
-
let t =
|
|
1303
|
+
let t = C(e).length > 0 ? s : r` ORDER BY (SELECT NULL)`;
|
|
1295
1304
|
l = e.take === void 0 ? r`${t} OFFSET ${B(e.skip, r)} ROWS` : r`${t} OFFSET ${B(e.skip, r)} ROWS FETCH NEXT ${B(e.take, r)} ROWS ONLY`;
|
|
1296
1305
|
} else e.take !== void 0 && (c = r`TOP ${B(e.take, r)} `);
|
|
1297
1306
|
let u = e.skip === void 0 ? s : r``;
|
|
1298
1307
|
return r`SELECT ${c}(SELECT ${a} FOR JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER) AS ${r("__row")} FROM ${r(t.table.tableName)} AS ${r(i)}${o}${u}${l}`;
|
|
1299
|
-
},
|
|
1308
|
+
}, wi = (e, t, n, r, i) => {
|
|
1300
1309
|
let a = r.sql, o = [];
|
|
1301
1310
|
for (let n of Object.keys(t.fields)) o.push(a`${R(e, n, a)} AS ${a(n)}`);
|
|
1302
|
-
if (i !== void 0) for (let e of i.projection.present) o.push(a`${R(i.source.alias, i.source.physical(e), a)} AS ${
|
|
1311
|
+
if (i !== void 0) for (let e of i.projection.present) o.push(a`${R(i.source.alias, i.source.physical(e), a)} AS ${Ti(`_junction.${e}`, a)}`);
|
|
1303
1312
|
for (let t of n) {
|
|
1304
|
-
let n =
|
|
1313
|
+
let n = Ei(t, e, r);
|
|
1305
1314
|
if (n === null) return null;
|
|
1306
1315
|
o.push(n);
|
|
1307
1316
|
}
|
|
1308
1317
|
return a.csv(o);
|
|
1309
|
-
},
|
|
1318
|
+
}, Ti = (e, t) => t.unsafe(`[${e.replace(/]/g, "]]")}]`), Ei = (e, t, n) => {
|
|
1310
1319
|
switch (e.relation.kind) {
|
|
1311
|
-
case "one": return
|
|
1312
|
-
case "many": return
|
|
1313
|
-
case "manyToMany": return
|
|
1320
|
+
case "one": return Lr(e) ? ki(Rr(e), t, n) : Di(e, t, n);
|
|
1321
|
+
case "many": return ki(e, t, n);
|
|
1322
|
+
case "manyToMany": return Ai(e, t, n);
|
|
1314
1323
|
}
|
|
1315
|
-
},
|
|
1316
|
-
let r = n.sql, i = n.nextAlias(), a =
|
|
1324
|
+
}, Di = (e, t, n) => {
|
|
1325
|
+
let r = n.sql, i = n.nextAlias(), a = wi(i, e.target, e.children, n);
|
|
1317
1326
|
if (a === null) return null;
|
|
1318
1327
|
let o = e.fkSide === "source" ? r`${R(i, "id", r)} = ${R(t, e.foreignKey, r)}` : r`${R(i, e.foreignKey, r)} = ${R(t, e.sourceKey, r)}`;
|
|
1319
1328
|
return r`JSON_QUERY((SELECT TOP 1 ${a} FROM ${r(e.target.tableName)} AS ${r(i)} WHERE ${o} FOR JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER)) AS ${r(e.relationName)}`;
|
|
1320
|
-
},
|
|
1329
|
+
}, Oi = (e, t, n) => {
|
|
1321
1330
|
if (e.offset !== void 0) {
|
|
1322
1331
|
let r = e.orderBy && e.orderBy.length > 0 ? t : n` ORDER BY (SELECT NULL)`, i = e.limit === void 0 ? n`${r} OFFSET ${B(e.offset, n)} ROWS` : n`${r} OFFSET ${B(e.offset, n)} ROWS FETCH NEXT ${B(e.limit, n)} ROWS ONLY`;
|
|
1323
1332
|
return {
|
|
@@ -1331,29 +1340,29 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1331
1340
|
pagination: n``,
|
|
1332
1341
|
finalOrder: t
|
|
1333
1342
|
};
|
|
1334
|
-
},
|
|
1335
|
-
let r = n.sql, i = n.nextAlias(), a =
|
|
1343
|
+
}, ki = (e, t, n) => {
|
|
1344
|
+
let r = n.sql, i = n.nextAlias(), a = wi(i, e.target, e.children, n);
|
|
1336
1345
|
if (a === null) return null;
|
|
1337
|
-
let o = e.branch.where === void 0 ? r`` : r` AND ${V(e.branch.where, i, r)}`, s = I(i, e.branch.orderBy, r), { top: c, pagination: l, finalOrder: u } =
|
|
1346
|
+
let o = e.branch.where === void 0 ? r`` : r` AND ${V(e.branch.where, i, r)}`, s = I(i, e.branch.orderBy, r), { top: c, pagination: l, finalOrder: u } = Oi(e.branch, s, r);
|
|
1338
1347
|
return r`ISNULL(JSON_QUERY((SELECT ${c}${a} FROM ${r(e.target.tableName)} AS ${r(i)} WHERE ${R(i, e.foreignKey, r)} = ${R(t, e.sourceKey, r)}${o}${u}${l} FOR JSON PATH, INCLUDE_NULL_VALUES)), JSON_QUERY('[]')) AS ${r(e.relationName)}`;
|
|
1339
|
-
},
|
|
1340
|
-
let r = n.sql, i = e.relation, a = i.through(), o = n.nextAlias(), s = n.nextAlias(), c =
|
|
1348
|
+
}, Ai = (e, t, n) => {
|
|
1349
|
+
let r = n.sql, i = e.relation, a = i.through(), o = n.nextAlias(), s = n.nextAlias(), c = wi(o, e.target, e.children, n, e.junction === void 0 ? void 0 : {
|
|
1341
1350
|
projection: e.junction,
|
|
1342
|
-
source:
|
|
1351
|
+
source: di(s)
|
|
1343
1352
|
});
|
|
1344
1353
|
if (c === null) return null;
|
|
1345
|
-
let l = e.branch.where === void 0 ? r`` : r` AND ${V(e.branch.where, o, r)}`, u =
|
|
1354
|
+
let l = e.branch.where === void 0 ? r`` : r` AND ${V(e.branch.where, o, r)}`, u = ji(e, s, r), d = I(o, e.branch.orderBy, r), { top: f, pagination: p, finalOrder: m } = Oi(e.branch, d, r);
|
|
1346
1355
|
return r`ISNULL(JSON_QUERY((SELECT ${f}${c} FROM ${r(a.tableName)} AS ${r(s)} INNER JOIN ${r(e.target.tableName)} AS ${r(o)} ON ${R(o, "id", r)} = ${R(s, i.targetKey, r)} WHERE ${R(s, i.sourceKey, r)} = ${R(t, "id", r)}${u}${l}${m}${p} FOR JSON PATH, INCLUDE_NULL_VALUES)), JSON_QUERY('[]')) AS ${r(e.relationName)}`;
|
|
1347
1356
|
}, R = (e, t, n) => e === void 0 ? n`${n(t)}` : n`${n(e)}.${n(t)}`, z = (e, t) => t.unsafe(`'${e.replace(/'/g, "''")}'`), B = (e, t) => {
|
|
1348
1357
|
if (!Number.isFinite(e) || !Number.isInteger(e) || e < 0) throw Error(`literalInt: expected non-negative integer, got ${e}`);
|
|
1349
1358
|
return t.unsafe(String(e));
|
|
1350
|
-
}, V = (e, t, n) => F(
|
|
1359
|
+
}, V = (e, t, n) => F(Mi(e, t), n), ji = (e, t, n, r = !1) => {
|
|
1351
1360
|
let i = e.branch.onJunction;
|
|
1352
1361
|
return i === void 0 ? n`` : n` AND ${V(r ? H(i) : i, t, n)}`;
|
|
1353
|
-
},
|
|
1354
|
-
if ("not" in e) return { not:
|
|
1355
|
-
if ("and" in e) return { and: e.and.map((e) =>
|
|
1356
|
-
if ("or" in e) return { or: e.or.map((e) =>
|
|
1362
|
+
}, Mi = (e, t) => {
|
|
1363
|
+
if ("not" in e) return { not: Mi(e.not, t) };
|
|
1364
|
+
if ("and" in e) return { and: e.and.map((e) => Mi(e, t)) };
|
|
1365
|
+
if ("or" in e) return { or: e.or.map((e) => Mi(e, t)) };
|
|
1357
1366
|
if (e.op === "subquery-in" || e.op === "subquery-not-in") {
|
|
1358
1367
|
let n = e;
|
|
1359
1368
|
return {
|
|
@@ -1375,91 +1384,91 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1375
1384
|
let t = e;
|
|
1376
1385
|
return {
|
|
1377
1386
|
...t,
|
|
1378
|
-
value:
|
|
1387
|
+
value: Ni(t.value)
|
|
1379
1388
|
};
|
|
1380
|
-
},
|
|
1381
|
-
e.actors !== void 0 && (
|
|
1382
|
-
},
|
|
1389
|
+
}, Ni = (e) => e == null ? e : e instanceof Date ? e.toISOString() : typeof e == "boolean" ? +!!e : Array.isArray(e) ? e.map((e) => Ni(e)) : e, Pi = Symbol.for("@voltro/database/coreTablesRegistry"), Fi = globalThis, Ii = Fi[Pi] ?? (Fi[Pi] = {}), Li = (e) => {
|
|
1390
|
+
e.actors !== void 0 && (Ii.actors = e.actors), e.tenants !== void 0 && (Ii.tenants = e.tenants);
|
|
1391
|
+
}, Ri = (e, t) => () => {
|
|
1383
1392
|
let n = e();
|
|
1384
1393
|
if (n === void 0) throw Error(`core '${t}' table not registered; expected the framework CLI or your migrate step to call registerCoreTables({ ${t}: () => ${t}Table }) before the database handle is constructed`);
|
|
1385
1394
|
return n();
|
|
1386
|
-
},
|
|
1387
|
-
id:
|
|
1388
|
-
kind:
|
|
1395
|
+
}, zi = () => Ri(() => Ii.actors, "actors"), Bi = () => Ri(() => Ii.tenants, "tenants"), Vi = T("actors", {
|
|
1396
|
+
id: b(),
|
|
1397
|
+
kind: v().oneOf([
|
|
1389
1398
|
"user",
|
|
1390
1399
|
"serviceAccount",
|
|
1391
1400
|
"apiKey",
|
|
1392
1401
|
"system"
|
|
1393
1402
|
]),
|
|
1394
|
-
displayName:
|
|
1395
|
-
createdAt:
|
|
1396
|
-
}),
|
|
1403
|
+
displayName: v().nullable(),
|
|
1404
|
+
createdAt: S().default("now")
|
|
1405
|
+
}), Hi = (e, t) => ({
|
|
1397
1406
|
column: e,
|
|
1398
1407
|
op: "eq",
|
|
1399
1408
|
value: t
|
|
1400
|
-
}),
|
|
1409
|
+
}), Ui = (e, t) => ({
|
|
1401
1410
|
column: e,
|
|
1402
1411
|
op: "neq",
|
|
1403
1412
|
value: t
|
|
1404
|
-
}),
|
|
1413
|
+
}), Wi = (e, t) => ({
|
|
1405
1414
|
column: e,
|
|
1406
1415
|
op: "gt",
|
|
1407
1416
|
value: t
|
|
1408
|
-
}),
|
|
1417
|
+
}), Gi = (e, t) => ({
|
|
1409
1418
|
column: e,
|
|
1410
1419
|
op: "gte",
|
|
1411
1420
|
value: t
|
|
1412
|
-
}),
|
|
1421
|
+
}), Ki = (e, t) => ({
|
|
1413
1422
|
column: e,
|
|
1414
1423
|
op: "lt",
|
|
1415
1424
|
value: t
|
|
1416
|
-
}),
|
|
1425
|
+
}), qi = (e, t) => ({
|
|
1417
1426
|
column: e,
|
|
1418
1427
|
op: "lte",
|
|
1419
1428
|
value: t
|
|
1420
|
-
}),
|
|
1429
|
+
}), Ji = (e, t) => ({
|
|
1421
1430
|
column: e,
|
|
1422
1431
|
op: "in",
|
|
1423
1432
|
value: t
|
|
1424
|
-
}),
|
|
1433
|
+
}), Yi = (e, t) => ({
|
|
1425
1434
|
column: e,
|
|
1426
1435
|
op: "notIn",
|
|
1427
1436
|
value: t
|
|
1428
|
-
}),
|
|
1437
|
+
}), Xi = (e, t) => ({
|
|
1429
1438
|
column: e,
|
|
1430
1439
|
op: "contains",
|
|
1431
1440
|
value: t
|
|
1432
|
-
}),
|
|
1441
|
+
}), Zi = (e, t) => ({
|
|
1433
1442
|
column: e,
|
|
1434
1443
|
op: "startsWith",
|
|
1435
1444
|
value: t
|
|
1436
|
-
}),
|
|
1445
|
+
}), Qi = (e, t) => ({
|
|
1437
1446
|
column: e,
|
|
1438
1447
|
op: "arrayContains",
|
|
1439
1448
|
value: t
|
|
1440
|
-
}),
|
|
1449
|
+
}), $i = (e, t) => ({
|
|
1441
1450
|
column: e,
|
|
1442
1451
|
op: "arrayOverlaps",
|
|
1443
1452
|
value: t
|
|
1444
|
-
}),
|
|
1453
|
+
}), ea = (e, t) => ({
|
|
1445
1454
|
column: e,
|
|
1446
1455
|
op: "arrayHas",
|
|
1447
1456
|
value: t
|
|
1448
|
-
}),
|
|
1457
|
+
}), ta = {
|
|
1449
1458
|
column: "id",
|
|
1450
1459
|
op: "in",
|
|
1451
1460
|
value: []
|
|
1452
|
-
},
|
|
1461
|
+
}, na = (e) => ({
|
|
1453
1462
|
column: e,
|
|
1454
1463
|
op: "isNull"
|
|
1455
|
-
}),
|
|
1464
|
+
}), ra = (e) => ({
|
|
1456
1465
|
column: e,
|
|
1457
1466
|
op: "isNotNull"
|
|
1458
|
-
}),
|
|
1467
|
+
}), ia = (e, t) => ({
|
|
1459
1468
|
column: e,
|
|
1460
1469
|
op: "spatial",
|
|
1461
1470
|
value: t
|
|
1462
|
-
}),
|
|
1471
|
+
}), aa = (...e) => ({ and: e }), oa = (...e) => ({ or: e }), sa = (e) => ({ not: e }), ca = (e, ...t) => {
|
|
1463
1472
|
if (t.length === 0) throw Error(`jsonField("${e}"): at least one path segment is required`);
|
|
1464
1473
|
let n = (n, r) => ({
|
|
1465
1474
|
column: e,
|
|
@@ -1481,33 +1490,33 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1481
1490
|
isNull: () => n("isNull"),
|
|
1482
1491
|
isNotNull: () => n("isNotNull")
|
|
1483
1492
|
};
|
|
1484
|
-
},
|
|
1493
|
+
}, la = (e) => "descriptor" in e ? e.descriptor : e, ua = (e, t) => ({
|
|
1485
1494
|
column: e,
|
|
1486
1495
|
op: "subquery-in",
|
|
1487
|
-
subquery:
|
|
1488
|
-
}),
|
|
1496
|
+
subquery: la(t)
|
|
1497
|
+
}), da = (e, t) => ({
|
|
1489
1498
|
column: e,
|
|
1490
1499
|
op: "subquery-not-in",
|
|
1491
|
-
subquery:
|
|
1492
|
-
}), da = (e) => ({
|
|
1493
|
-
op: "exists",
|
|
1494
|
-
subquery: ca(e)
|
|
1500
|
+
subquery: la(t)
|
|
1495
1501
|
}), fa = (e) => ({
|
|
1502
|
+
op: "exists",
|
|
1503
|
+
subquery: la(e)
|
|
1504
|
+
}), pa = (e) => ({
|
|
1496
1505
|
op: "not-exists",
|
|
1497
|
-
subquery:
|
|
1498
|
-
}),
|
|
1506
|
+
subquery: la(e)
|
|
1507
|
+
}), ma = (e, t) => e === t || e instanceof Date && t instanceof Date && e.getTime() === t.getTime(), ha = (e, t) => {
|
|
1499
1508
|
if (e == null || t == null) return null;
|
|
1500
1509
|
let n = e instanceof Date ? e.getTime() : e, r = t instanceof Date ? t.getTime() : t;
|
|
1501
1510
|
return typeof n == typeof r ? n === r ? 0 : n > r ? 1 : -1 : null;
|
|
1502
|
-
},
|
|
1511
|
+
}, ga = (e, t, n = null) => {
|
|
1503
1512
|
if (!t) return !1;
|
|
1504
|
-
if ("not" in e) return !
|
|
1513
|
+
if ("not" in e) return !ga(e.not, t, n);
|
|
1505
1514
|
if ("and" in e) {
|
|
1506
|
-
for (let r of e.and) if (!
|
|
1515
|
+
for (let r of e.and) if (!ga(r, t, n)) return !1;
|
|
1507
1516
|
return !0;
|
|
1508
1517
|
}
|
|
1509
1518
|
if ("or" in e) {
|
|
1510
|
-
for (let r of e.or) if (
|
|
1519
|
+
for (let r of e.or) if (ga(r, t, n)) return !0;
|
|
1511
1520
|
return !1;
|
|
1512
1521
|
}
|
|
1513
1522
|
if (e.op === "subquery-in" || e.op === "subquery-not-in") {
|
|
@@ -1524,14 +1533,14 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1524
1533
|
}
|
|
1525
1534
|
let r = e, i = t[r.column];
|
|
1526
1535
|
switch (r.op) {
|
|
1527
|
-
case "eq": return
|
|
1528
|
-
case "neq": return !
|
|
1529
|
-
case "gt": return (
|
|
1530
|
-
case "gte": return (
|
|
1531
|
-
case "lt": return (
|
|
1532
|
-
case "lte": return (
|
|
1533
|
-
case "in": return Array.isArray(r.value) && r.value.some((e) =>
|
|
1534
|
-
case "notIn": return Array.isArray(r.value) && !r.value.some((e) =>
|
|
1536
|
+
case "eq": return ma(i, r.value);
|
|
1537
|
+
case "neq": return !ma(i, r.value);
|
|
1538
|
+
case "gt": return (ha(i, r.value) ?? -1) > 0;
|
|
1539
|
+
case "gte": return (ha(i, r.value) ?? -1) >= 0;
|
|
1540
|
+
case "lt": return (ha(i, r.value) ?? 1) < 0;
|
|
1541
|
+
case "lte": return (ha(i, r.value) ?? 1) <= 0;
|
|
1542
|
+
case "in": return Array.isArray(r.value) && r.value.some((e) => ma(i, e));
|
|
1543
|
+
case "notIn": return Array.isArray(r.value) && !r.value.some((e) => ma(i, e));
|
|
1535
1544
|
case "contains": return typeof i == "string" && typeof r.value == "string" && i.toLowerCase().includes(r.value.toLowerCase());
|
|
1536
1545
|
case "startsWith": return typeof i == "string" && typeof r.value == "string" && i.startsWith(r.value);
|
|
1537
1546
|
case "arrayContains": {
|
|
@@ -1547,17 +1556,17 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1547
1556
|
case "isNotNull": return i != null;
|
|
1548
1557
|
case "spatial": throw Error("@voltro/plugin-postgis: spatial predicates (ST_DWithin / ST_Contains / ST_Within / ST_Intersects / bbox &&) are not evaluable in-memory — use them in one-shot ctx.store.query reads, not reactive subscriptions.");
|
|
1549
1558
|
}
|
|
1550
|
-
},
|
|
1559
|
+
}, _a = function* (e) {
|
|
1551
1560
|
if ("not" in e) {
|
|
1552
|
-
yield*
|
|
1561
|
+
yield* _a(e.not);
|
|
1553
1562
|
return;
|
|
1554
1563
|
}
|
|
1555
1564
|
if ("and" in e) {
|
|
1556
|
-
for (let t of e.and) yield*
|
|
1565
|
+
for (let t of e.and) yield* _a(t);
|
|
1557
1566
|
return;
|
|
1558
1567
|
}
|
|
1559
1568
|
if ("or" in e) {
|
|
1560
|
-
for (let t of e.or) yield*
|
|
1569
|
+
for (let t of e.or) yield* _a(t);
|
|
1561
1570
|
return;
|
|
1562
1571
|
}
|
|
1563
1572
|
e.op === "subquery-in" || e.op === "subquery-not-in" ? yield {
|
|
@@ -1567,46 +1576,46 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1567
1576
|
kind: "exists",
|
|
1568
1577
|
descriptor: e.subquery
|
|
1569
1578
|
});
|
|
1570
|
-
},
|
|
1579
|
+
}, va = function* (e, t = !0) {
|
|
1571
1580
|
if ("not" in e) {
|
|
1572
|
-
yield*
|
|
1581
|
+
yield* va(e.not, !1);
|
|
1573
1582
|
return;
|
|
1574
1583
|
}
|
|
1575
1584
|
if ("and" in e) {
|
|
1576
|
-
for (let n of e.and) yield*
|
|
1585
|
+
for (let n of e.and) yield* va(n, t);
|
|
1577
1586
|
return;
|
|
1578
1587
|
}
|
|
1579
1588
|
if ("or" in e) {
|
|
1580
|
-
for (let t of e.or) yield*
|
|
1589
|
+
for (let t of e.or) yield* va(t, !1);
|
|
1581
1590
|
return;
|
|
1582
1591
|
}
|
|
1583
1592
|
e.op !== "subquery-in" && e.op !== "subquery-not-in" && e.op !== "exists" && e.op !== "not-exists" && (yield {
|
|
1584
1593
|
...e,
|
|
1585
1594
|
conjunctive: t
|
|
1586
1595
|
});
|
|
1587
|
-
},
|
|
1596
|
+
}, ya = (e, t) => ({
|
|
1588
1597
|
...t,
|
|
1589
1598
|
...e,
|
|
1590
1599
|
origin: e.origin ?? "injected"
|
|
1591
|
-
}),
|
|
1600
|
+
}), ba = (e, t, n = {}) => ({
|
|
1592
1601
|
table: e,
|
|
1593
1602
|
op: "insert",
|
|
1594
1603
|
old: null,
|
|
1595
1604
|
new: t,
|
|
1596
1605
|
...n
|
|
1597
|
-
}),
|
|
1606
|
+
}), xa = (e, t, n, r = {}) => ({
|
|
1598
1607
|
table: e,
|
|
1599
1608
|
op: "update",
|
|
1600
1609
|
old: t,
|
|
1601
1610
|
new: n,
|
|
1602
1611
|
...r
|
|
1603
|
-
}),
|
|
1612
|
+
}), Sa = (e, t, n = {}) => ({
|
|
1604
1613
|
table: e,
|
|
1605
1614
|
op: "delete",
|
|
1606
1615
|
old: t,
|
|
1607
1616
|
new: null,
|
|
1608
1617
|
...n
|
|
1609
|
-
}),
|
|
1618
|
+
}), Ca = (e, t, n = {}) => {
|
|
1610
1619
|
let { at: r = "2026-01-01T00:00:00.000Z", column: i = "deletedAt", ...a } = n;
|
|
1611
1620
|
return {
|
|
1612
1621
|
table: e,
|
|
@@ -1621,26 +1630,26 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1621
1630
|
},
|
|
1622
1631
|
...a
|
|
1623
1632
|
};
|
|
1624
|
-
},
|
|
1625
|
-
let t = e ??
|
|
1633
|
+
}, wa = new Nt(), Ta = (e, t) => wa.run(e, t), Ea = () => wa.getStore(), Da = (e) => {
|
|
1634
|
+
let t = e ?? wa.getStore();
|
|
1626
1635
|
return t === void 0 ? {} : {
|
|
1627
1636
|
...t.traceId === void 0 ? {} : { traceId: t.traceId },
|
|
1628
1637
|
...t.subjectId === void 0 ? {} : { subjectId: t.subjectId },
|
|
1629
1638
|
...t.procedure === void 0 ? {} : { procedure: t.procedure },
|
|
1630
1639
|
...t.via === void 0 ? {} : { via: t.via }
|
|
1631
1640
|
};
|
|
1632
|
-
},
|
|
1633
|
-
let t =
|
|
1634
|
-
return t === void 0 ? e(void 0) :
|
|
1635
|
-
},
|
|
1641
|
+
}, Oa = (e) => {
|
|
1642
|
+
let t = wa.getStore();
|
|
1643
|
+
return t === void 0 ? e(void 0) : wa.run(t, () => e(t));
|
|
1644
|
+
}, ka = 3e4, Aa = 1e4, U = /* @__PURE__ */ new Map(), W = 0, ja = (e, t, n) => `${e}\u0000${t}\u0000${String(n)}`, Ma = (e) => {
|
|
1636
1645
|
for (let [t, n] of U) {
|
|
1637
|
-
let r = n.filter((t) => e - t.at <
|
|
1646
|
+
let r = n.filter((t) => e - t.at < ka);
|
|
1638
1647
|
W -= n.length - r.length, r.length === 0 ? U.delete(t) : r.length !== n.length && U.set(t, r);
|
|
1639
1648
|
}
|
|
1640
|
-
},
|
|
1649
|
+
}, Na = (e, t) => {
|
|
1641
1650
|
if (t.traceId === void 0 && t.subjectId === void 0 && t.procedure === void 0) return;
|
|
1642
1651
|
let n = Date.now();
|
|
1643
|
-
if (W >=
|
|
1652
|
+
if (W >= Aa && Ma(n), W >= Aa) return;
|
|
1644
1653
|
let r = U.get(e);
|
|
1645
1654
|
r === void 0 ? U.set(e, [{
|
|
1646
1655
|
attribution: t,
|
|
@@ -1649,38 +1658,38 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1649
1658
|
attribution: t,
|
|
1650
1659
|
at: n
|
|
1651
1660
|
}), W += 1;
|
|
1652
|
-
},
|
|
1661
|
+
}, Pa = (e) => {
|
|
1653
1662
|
let t = U.get(e);
|
|
1654
1663
|
if (t === void 0 || t.length === 0) return;
|
|
1655
1664
|
let n = t.shift();
|
|
1656
|
-
if (--W, t.length === 0 && U.delete(e), !(Date.now() - n.at >=
|
|
1657
|
-
},
|
|
1658
|
-
let t =
|
|
1665
|
+
if (--W, t.length === 0 && U.delete(e), !(Date.now() - n.at >= ka)) return n.attribution;
|
|
1666
|
+
}, Fa = (e) => (U.get(e)?.length ?? 0) > 0, G = /* @__PURE__ */ new Map(), Ia = /* @__PURE__ */ new Map(), La = /* @__PURE__ */ new Map(), Ra = 250, za = (e) => {
|
|
1667
|
+
let t = Ia.get(e);
|
|
1659
1668
|
if (t === void 0) return;
|
|
1660
|
-
|
|
1661
|
-
let n =
|
|
1662
|
-
n !== void 0 && (clearTimeout(n),
|
|
1669
|
+
Ia.delete(e);
|
|
1670
|
+
let n = La.get(e);
|
|
1671
|
+
n !== void 0 && (clearTimeout(n), La.delete(e));
|
|
1663
1672
|
for (let e of t) e();
|
|
1664
|
-
},
|
|
1673
|
+
}, Ba = (e) => {
|
|
1665
1674
|
G.set(e, (G.get(e) ?? 0) + 1);
|
|
1666
|
-
},
|
|
1675
|
+
}, Va = (e) => {
|
|
1667
1676
|
let t = (G.get(e) ?? 0) - 1;
|
|
1668
1677
|
if (t > 0) {
|
|
1669
1678
|
G.set(e, t);
|
|
1670
1679
|
return;
|
|
1671
1680
|
}
|
|
1672
|
-
G.delete(e),
|
|
1673
|
-
},
|
|
1674
|
-
let i = n == null ? void 0 :
|
|
1681
|
+
G.delete(e), za(e);
|
|
1682
|
+
}, Ha = (e, t, n, r) => {
|
|
1683
|
+
let i = n == null ? void 0 : ja(e, t, n), a = () => r(i === void 0 ? void 0 : Pa(i));
|
|
1675
1684
|
if (i === void 0) {
|
|
1676
1685
|
a();
|
|
1677
1686
|
return;
|
|
1678
1687
|
}
|
|
1679
|
-
if (
|
|
1688
|
+
if (Fa(i)) {
|
|
1680
1689
|
a();
|
|
1681
1690
|
return;
|
|
1682
1691
|
}
|
|
1683
|
-
let o =
|
|
1692
|
+
let o = Ia.get(e);
|
|
1684
1693
|
if (o === void 0 && (G.get(e) ?? 0) === 0) {
|
|
1685
1694
|
a();
|
|
1686
1695
|
return;
|
|
@@ -1689,75 +1698,82 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1689
1698
|
o.push(a);
|
|
1690
1699
|
return;
|
|
1691
1700
|
}
|
|
1692
|
-
|
|
1693
|
-
let s = setTimeout(() =>
|
|
1694
|
-
typeof s == "object" && typeof s.unref == "function" && s.unref(),
|
|
1695
|
-
},
|
|
1701
|
+
Ia.set(e, [a]);
|
|
1702
|
+
let s = setTimeout(() => za(e), Ra);
|
|
1703
|
+
typeof s == "object" && typeof s.unref == "function" && s.unref(), La.set(e, s);
|
|
1704
|
+
}, Ua = () => {
|
|
1696
1705
|
U.clear(), W = 0, G.clear();
|
|
1697
|
-
for (let e of
|
|
1698
|
-
|
|
1699
|
-
},
|
|
1700
|
-
|
|
1706
|
+
for (let e of La.values()) clearTimeout(e);
|
|
1707
|
+
La.clear(), Ia.clear();
|
|
1708
|
+
}, Wa = () => W, K = /* @__PURE__ */ new Map(), Ga = (e, t) => {
|
|
1709
|
+
K.set(e, {
|
|
1701
1710
|
recorder: t.recorder,
|
|
1702
1711
|
tables: new Set(t.tables),
|
|
1703
1712
|
ownTables: new Set(t.ownTables)
|
|
1704
1713
|
});
|
|
1705
|
-
},
|
|
1706
|
-
|
|
1707
|
-
},
|
|
1708
|
-
if (
|
|
1709
|
-
for (let t of
|
|
1714
|
+
}, Ka = (e) => {
|
|
1715
|
+
K.delete(e);
|
|
1716
|
+
}, qa = () => K.clear(), Ja = (e) => {
|
|
1717
|
+
if (K.size === 0) return !1;
|
|
1718
|
+
for (let t of K.values()) if (!t.ownTables.has(e) && t.tables.has(e)) return !0;
|
|
1710
1719
|
return !1;
|
|
1711
|
-
},
|
|
1712
|
-
if (
|
|
1713
|
-
|
|
1720
|
+
}, Ya = async (e, t) => {
|
|
1721
|
+
if (K.size !== 0) {
|
|
1722
|
+
for (let [n, r] of K.entries()) if (!r.ownTables.has(t.table) && r.tables.has(t.table)) try {
|
|
1723
|
+
await r.recorder(e, t);
|
|
1724
|
+
} catch (e) {
|
|
1725
|
+
let r = ln(e);
|
|
1726
|
+
throw r === void 0 ? e : Error(`write recorder '${n}' failed while recording a ${t.op} on '${t.table}': ${r}`, { cause: e });
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
}, Xa = kt.exponential({
|
|
1714
1730
|
start: 5e-4,
|
|
1715
1731
|
factor: 2,
|
|
1716
1732
|
count: 16
|
|
1717
|
-
}),
|
|
1718
|
-
let n = `${e}\u0000${t}`, r =
|
|
1733
|
+
}), Za = D.counter("voltro_db_queries_total", { description: "Database operations executed, by dialect and operation kind." }), Qa = D.histogram("voltro_db_query_duration_seconds", Xa, "Database operation duration in seconds, by dialect and operation kind."), $a = D.counter("voltro_db_errors_total", { description: "Database operations that threw, by dialect and operation kind." }), eo = D.gauge("voltro_db_operations_in_flight", { description: "Database operations currently executing, by dialect. An upper bound on connections the framework holds; NOT the driver pool's waiting count." }), to = D.counter("voltro_db_eager_fallback_total", { description: "Eager-load queries that degraded from the single-roundtrip JSON-agg path to the multi-query walker, by dialect and reason." }), no = [], ro = /* @__PURE__ */ new Map(), io = /* @__PURE__ */ new Map(), ao = /* @__PURE__ */ new Map(), oo = (e, t) => {
|
|
1734
|
+
let n = `${e}\u0000${t}`, r = ro.get(n);
|
|
1719
1735
|
if (r !== void 0) return r;
|
|
1720
|
-
let i = (n) =>
|
|
1721
|
-
count: i(
|
|
1722
|
-
duration: i(
|
|
1723
|
-
errors: i(
|
|
1736
|
+
let i = (n) => D.tagged(D.tagged(n, "dialect", e), "op", t), a = {
|
|
1737
|
+
count: i(Za),
|
|
1738
|
+
duration: i(Qa),
|
|
1739
|
+
errors: i($a)
|
|
1724
1740
|
};
|
|
1725
|
-
return
|
|
1726
|
-
},
|
|
1727
|
-
|
|
1728
|
-
},
|
|
1729
|
-
let n = Math.max(0, (
|
|
1730
|
-
n === 0 ?
|
|
1731
|
-
let r =
|
|
1732
|
-
r === void 0 && (r =
|
|
1733
|
-
},
|
|
1734
|
-
let t =
|
|
1735
|
-
t.count.unsafeUpdate(1,
|
|
1736
|
-
},
|
|
1741
|
+
return ro.set(n, a), a;
|
|
1742
|
+
}, so = () => {
|
|
1743
|
+
ro.clear(), io.clear(), ao.clear();
|
|
1744
|
+
}, co = () => ro.size + io.size + ao.size, lo = /* @__PURE__ */ new Map(), uo = (e, t) => {
|
|
1745
|
+
let n = Math.max(0, (lo.get(e) ?? 0) + t);
|
|
1746
|
+
n === 0 ? lo.delete(e) : lo.set(e, n);
|
|
1747
|
+
let r = io.get(e);
|
|
1748
|
+
r === void 0 && (r = D.tagged(eo, "dialect", e), io.set(e, r)), r.unsafeUpdate(n, no);
|
|
1749
|
+
}, fo = (e) => lo.get(e) ?? 0, po = (e) => {
|
|
1750
|
+
let t = oo(String(e.dialect), e.op);
|
|
1751
|
+
t.count.unsafeUpdate(1, no), t.duration.unsafeUpdate(e.durationMs / 1e3, no), e.status === "error" && t.errors.unsafeUpdate(1, no);
|
|
1752
|
+
}, mo = async (e, t, n) => {
|
|
1737
1753
|
let r = performance.now(), i = String(e);
|
|
1738
|
-
|
|
1754
|
+
uo(i, 1);
|
|
1739
1755
|
let a = "ok";
|
|
1740
1756
|
try {
|
|
1741
1757
|
return await n();
|
|
1742
1758
|
} catch (e) {
|
|
1743
1759
|
throw a = "error", e;
|
|
1744
1760
|
} finally {
|
|
1745
|
-
|
|
1761
|
+
uo(i, -1), po({
|
|
1746
1762
|
dialect: e,
|
|
1747
1763
|
op: t,
|
|
1748
1764
|
durationMs: performance.now() - r,
|
|
1749
1765
|
status: a
|
|
1750
1766
|
});
|
|
1751
1767
|
}
|
|
1752
|
-
},
|
|
1768
|
+
}, ho = 3e5, go = (e) => {
|
|
1753
1769
|
if (e == null || e === "") return;
|
|
1754
1770
|
let t = Number(e);
|
|
1755
1771
|
return Number.isFinite(t) && t >= 0 ? Math.floor(t) : void 0;
|
|
1756
|
-
},
|
|
1757
|
-
let n =
|
|
1772
|
+
}, _o = (e, t = process.env) => go(t.VOLTRO_DB_EAGER_FALLBACK_WARN_INTERVAL_MS) ?? go(e) ?? 3e5, vo = (e, t = {}) => {
|
|
1773
|
+
let n = _o(t.warnIntervalMs), r = t.now ?? (() => Date.now()), i = /* @__PURE__ */ new Map();
|
|
1758
1774
|
return (t) => {
|
|
1759
|
-
let a = `${t.dialect} ${t.reason}`, o =
|
|
1760
|
-
o === void 0 && (o =
|
|
1775
|
+
let a = `${t.dialect} ${t.reason}`, o = ao.get(a);
|
|
1776
|
+
o === void 0 && (o = D.tagged(D.tagged(to, "dialect", String(t.dialect)), "reason", t.reason), ao.set(a, o)), o.unsafeUpdate(1, no);
|
|
1761
1777
|
let s = `${t.table}\u0000${t.reason}`, c = i.get(s), l = c === void 0 || n > 0 && r() - c >= n, u = {
|
|
1762
1778
|
table: t.table,
|
|
1763
1779
|
dialect: String(t.dialect),
|
|
@@ -1771,24 +1787,24 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1771
1787
|
}
|
|
1772
1788
|
i.set(s, r()), e.warn(t.reason === "execute-failed" ? "eager-load JSON-agg statement FAILED and was re-run on the multi-query walker — this query pays both paths on every call. Counted as voltro_db_eager_fallback_total{reason=\"execute-failed\"}." : "eager-load could not be compiled to a single JSON-agg roundtrip and is running on the multi-query walker — one round trip per relation level, on every call. Counted as voltro_db_eager_fallback_total{reason=\"not-compilable\"}.", u);
|
|
1773
1789
|
};
|
|
1774
|
-
},
|
|
1775
|
-
if (
|
|
1776
|
-
throw
|
|
1777
|
-
},
|
|
1778
|
-
let t = 0, n =
|
|
1790
|
+
}, yo = (e) => {
|
|
1791
|
+
if (Ot.isSuccess(e)) return e.value;
|
|
1792
|
+
throw Tt.squash(e.cause);
|
|
1793
|
+
}, bo = "10 millis", xo = 3, So = async (e) => {
|
|
1794
|
+
let t = 0, n = E.suspend(() => {
|
|
1779
1795
|
let n = ++t;
|
|
1780
|
-
return e.withTransaction(
|
|
1781
|
-
if (
|
|
1782
|
-
let r = t.value, i =
|
|
1796
|
+
return e.withTransaction(E.flatMap(E.serviceOption(Pt), (t) => {
|
|
1797
|
+
if (O.isNone(t)) return E.fail(/* @__PURE__ */ Error(`${e.label}: TransactionConnection unexpectedly missing inside withTransaction.`));
|
|
1798
|
+
let r = t.value, i = E.tryPromise({
|
|
1783
1799
|
try: () => e.body(r, n),
|
|
1784
1800
|
catch: (e) => e
|
|
1785
1801
|
});
|
|
1786
|
-
return e.prepare === void 0 ? i :
|
|
1787
|
-
})).pipe(
|
|
1788
|
-
}), r =
|
|
1789
|
-
return
|
|
1790
|
-
},
|
|
1791
|
-
let t =
|
|
1802
|
+
return e.prepare === void 0 ? i : E.flatMap(e.prepare(r), () => i);
|
|
1803
|
+
})).pipe(E.catchAllDefect((t) => e.isRetryable(t) ? E.fail(t) : E.die(t)));
|
|
1804
|
+
}), r = jt.exponential(bo).pipe(jt.compose(jt.recurs(xo)), jt.whileInput(e.isRetryable)), i = n.pipe(E.retry(r), E.withSpan(e.span.name, { attributes: { ...e.span.attributes } }));
|
|
1805
|
+
return mo(e.dialect, "transaction", async () => yo(await e.runPromiseExit(e.wrapProgram === void 0 ? i : e.wrapProgram(i))));
|
|
1806
|
+
}, Co = async (e) => {
|
|
1807
|
+
let t = Ea(), n = t === void 0 ? (e) => e() : (e) => Ta(t, e), r = await So({
|
|
1792
1808
|
...e,
|
|
1793
1809
|
body: (r) => {
|
|
1794
1810
|
let i = e.makeView(r, t);
|
|
@@ -1799,14 +1815,14 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1799
1815
|
}
|
|
1800
1816
|
});
|
|
1801
1817
|
return r.view.commitEvents(), r.result;
|
|
1802
|
-
},
|
|
1803
|
-
table:
|
|
1804
|
-
id:
|
|
1805
|
-
expected:
|
|
1806
|
-
actual:
|
|
1807
|
-
}) {},
|
|
1818
|
+
}, wo = class extends k.TaggedError()("VersionConflict", {
|
|
1819
|
+
table: k.String,
|
|
1820
|
+
id: k.String,
|
|
1821
|
+
expected: k.Number,
|
|
1822
|
+
actual: k.NullOr(k.Number)
|
|
1823
|
+
}) {}, To = (e) => {
|
|
1808
1824
|
for (let [t, n] of Object.entries(e)) if (n?.definition?.versionColumn === !0) return t;
|
|
1809
|
-
},
|
|
1825
|
+
}, Eo = (e) => {
|
|
1810
1826
|
let { versionColumn: t, patch: n } = e;
|
|
1811
1827
|
if (t === void 0) return {
|
|
1812
1828
|
nextVersion: void 0,
|
|
@@ -1819,28 +1835,28 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1819
1835
|
expected: i,
|
|
1820
1836
|
patch: a
|
|
1821
1837
|
};
|
|
1822
|
-
},
|
|
1823
|
-
if (e.expected !== void 0 && (e.current === null || e.current !== e.expected)) return new
|
|
1838
|
+
}, Do = (e) => {
|
|
1839
|
+
if (e.expected !== void 0 && (e.current === null || e.current !== e.expected)) return new wo({
|
|
1824
1840
|
table: e.table,
|
|
1825
1841
|
id: e.id,
|
|
1826
1842
|
expected: e.expected,
|
|
1827
1843
|
actual: e.current
|
|
1828
1844
|
});
|
|
1829
|
-
},
|
|
1830
|
-
id: Do,
|
|
1831
|
-
fields: { expiresAt: C().nullable() },
|
|
1832
|
-
behaviors: { defaultWhere: () => "expiresAt IS NULL OR expiresAt > now()" }
|
|
1833
|
-
}), ko = "voltro/localFirst", Ao = () => ot({
|
|
1845
|
+
}, Oo = 1, ko = "voltro/expires", Ao = () => st({
|
|
1834
1846
|
id: ko,
|
|
1847
|
+
fields: { expiresAt: S().nullable() },
|
|
1848
|
+
behaviors: { defaultWhere: () => "expiresAt IS NULL OR expiresAt > now()" }
|
|
1849
|
+
}), jo = "voltro/localFirst", Mo = () => st({
|
|
1850
|
+
id: jo,
|
|
1835
1851
|
fields: {}
|
|
1836
|
-
}),
|
|
1852
|
+
}), No = (e) => e.appliedMixins.some((e) => e.id === jo), Po = (e) => {
|
|
1837
1853
|
let t = [];
|
|
1838
|
-
for (let n of Object.values(e))
|
|
1854
|
+
for (let n of Object.values(e)) No(n) && t.push(n.tableName);
|
|
1839
1855
|
return t;
|
|
1840
|
-
},
|
|
1841
|
-
let i =
|
|
1856
|
+
}, Fo = class extends Dt.TaggedError("TableStreamError") {}, Io = 1e3, Lo = (e, t, n, r) => {
|
|
1857
|
+
let i = O.match(r, {
|
|
1842
1858
|
onNone: () => e.where,
|
|
1843
|
-
onSome: (n) => e.where ?
|
|
1859
|
+
onSome: (n) => e.where ? aa(e.where, Wi(t, n)) : Wi(t, n)
|
|
1844
1860
|
});
|
|
1845
1861
|
return {
|
|
1846
1862
|
table: e.table,
|
|
@@ -1855,19 +1871,19 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1855
1871
|
...e.includeDeleted === void 0 ? {} : { includeDeleted: e.includeDeleted },
|
|
1856
1872
|
...e.crossTenant === void 0 ? {} : { crossTenant: e.crossTenant }
|
|
1857
1873
|
};
|
|
1858
|
-
},
|
|
1859
|
-
let n = t.pk ?? "id", r = t.chunkSize ??
|
|
1860
|
-
return
|
|
1861
|
-
let a =
|
|
1862
|
-
try: () => e.query(
|
|
1863
|
-
catch: (e) => new
|
|
1874
|
+
}, Ro = (e, t) => {
|
|
1875
|
+
let n = t.pk ?? "id", r = t.chunkSize ?? Io;
|
|
1876
|
+
return Mt.paginateChunkEffect(O.none(), (i) => {
|
|
1877
|
+
let a = E.tryPromise({
|
|
1878
|
+
try: () => e.query(Lo(t, n, r, i)),
|
|
1879
|
+
catch: (e) => new Fo({
|
|
1864
1880
|
table: t.table,
|
|
1865
1881
|
cause: e
|
|
1866
1882
|
})
|
|
1867
1883
|
});
|
|
1868
|
-
return (t.retry ? a.pipe(
|
|
1884
|
+
return (t.retry ? a.pipe(E.retry(t.retry)) : a).pipe(E.map((e) => [Et.fromIterable(e), e.length === r && e.length > 0 ? O.some(O.some(e[e.length - 1][n])) : O.none()]));
|
|
1869
1885
|
});
|
|
1870
|
-
},
|
|
1886
|
+
}, zo = (e) => {
|
|
1871
1887
|
let t = e.alpha ?? .5;
|
|
1872
1888
|
if (t < 0 || t > 1) throw Error(`hybridSearch: alpha must be in [0, 1], got ${t}`);
|
|
1873
1889
|
return (n) => {
|
|
@@ -1881,7 +1897,7 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1881
1897
|
queryText: e.vector.query,
|
|
1882
1898
|
distance: e.vector.distance ?? "cosine",
|
|
1883
1899
|
alpha: t
|
|
1884
|
-
}, i =
|
|
1900
|
+
}, i = ee(n.table, e.fts.indexName, n.fullTextIndexes);
|
|
1885
1901
|
return {
|
|
1886
1902
|
...n,
|
|
1887
1903
|
annClause: r,
|
|
@@ -1894,17 +1910,17 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1894
1910
|
}
|
|
1895
1911
|
};
|
|
1896
1912
|
};
|
|
1897
|
-
},
|
|
1913
|
+
}, Bo = async (e, t) => {
|
|
1898
1914
|
let n = e.as ?? "embedding", r = t.row[e.from];
|
|
1899
1915
|
if (typeof r != "string" || r.length === 0) return {};
|
|
1900
1916
|
let i = await t.embed(r);
|
|
1901
1917
|
return { [n]: i };
|
|
1902
|
-
},
|
|
1918
|
+
}, Vo = (e) => {
|
|
1903
1919
|
if (!Number.isInteger(e.dimensions) || e.dimensions <= 0) throw Error(`vectorEmbedding: dimensions must be a positive integer, got ${String(e.dimensions)}.`);
|
|
1904
|
-
let t = e.as ?? "embedding", n = (t) =>
|
|
1905
|
-
return
|
|
1920
|
+
let t = e.as ?? "embedding", n = (t) => Bo(e, t);
|
|
1921
|
+
return st({
|
|
1906
1922
|
id: "voltro/vectorEmbedding",
|
|
1907
|
-
fields: { [t]:
|
|
1923
|
+
fields: { [t]: Te(e.dimensions) },
|
|
1908
1924
|
...e.index === !1 ? {} : { indexes: [{
|
|
1909
1925
|
fields: [t],
|
|
1910
1926
|
kind: "hnsw",
|
|
@@ -1919,19 +1935,19 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1919
1935
|
beforeUpdate: n
|
|
1920
1936
|
}
|
|
1921
1937
|
});
|
|
1922
|
-
},
|
|
1938
|
+
}, Ho = 63, Uo = "br_pr", q = class extends Error {
|
|
1923
1939
|
_tag = "BranchNameInvalid";
|
|
1924
1940
|
constructor(e) {
|
|
1925
1941
|
super(e), this.name = "BranchNameInvalid";
|
|
1926
1942
|
}
|
|
1927
|
-
},
|
|
1928
|
-
if (!Number.isInteger(t) || t < 0) throw new
|
|
1929
|
-
let n =
|
|
1930
|
-
if (n.replace(/_/g, "") === "") throw new
|
|
1931
|
-
let r = `${
|
|
1932
|
-
if (r.length >
|
|
1943
|
+
}, Wo = (e, t) => {
|
|
1944
|
+
if (!Number.isInteger(t) || t < 0) throw new q(`prNumber must be a non-negative integer, got ${String(t)}`);
|
|
1945
|
+
let n = gr(e);
|
|
1946
|
+
if (n.replace(/_/g, "") === "") throw new q(`app slug '${e}' has no usable identifier characters`);
|
|
1947
|
+
let r = `${Uo}${t}_${n}`;
|
|
1948
|
+
if (r.length > Ho) throw new q(`branch namespace '${r}' exceeds the ${Ho}-byte identifier limit — use a shorter app slug`);
|
|
1933
1949
|
return r;
|
|
1934
|
-
},
|
|
1950
|
+
}, Go = (e) => e.startsWith(Uo), Ko = {
|
|
1935
1951
|
requested: {
|
|
1936
1952
|
provision: "provisioning",
|
|
1937
1953
|
destroy: "destroying"
|
|
@@ -1950,19 +1966,19 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1950
1966
|
fail: "failed"
|
|
1951
1967
|
},
|
|
1952
1968
|
destroyed: {}
|
|
1953
|
-
},
|
|
1969
|
+
}, qo = class extends Error {
|
|
1954
1970
|
from;
|
|
1955
1971
|
event;
|
|
1956
1972
|
_tag = "BranchTransitionInvalid";
|
|
1957
1973
|
constructor(e, t) {
|
|
1958
1974
|
super(`invalid branch transition: ${e} --${t}-->`), this.from = e, this.event = t, this.name = "BranchTransitionInvalid";
|
|
1959
1975
|
}
|
|
1960
|
-
},
|
|
1961
|
-
let n =
|
|
1962
|
-
if (n === void 0) throw new
|
|
1976
|
+
}, Jo = (e, t) => {
|
|
1977
|
+
let n = Ko[e][t];
|
|
1978
|
+
if (n === void 0) throw new qo(e, t);
|
|
1963
1979
|
return n;
|
|
1964
|
-
},
|
|
1965
|
-
if (n === "copy" && (r === void 0 || r === "")) throw new
|
|
1980
|
+
}, Yo = (e, t, n, r, i = [], a = []) => {
|
|
1981
|
+
if (n === "copy" && (r === void 0 || r === "")) throw new q("seed 'copy' requires a parentNamespace to snapshot from");
|
|
1966
1982
|
let o = [{
|
|
1967
1983
|
kind: "create-namespace",
|
|
1968
1984
|
detail: e
|
|
@@ -1991,13 +2007,13 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
1991
2007
|
kind: "seed",
|
|
1992
2008
|
detail: `run boot seeds into ${e}`
|
|
1993
2009
|
}), o;
|
|
1994
|
-
},
|
|
1995
|
-
if (!
|
|
2010
|
+
}, Xo = (e) => {
|
|
2011
|
+
if (!Go(e)) throw new q(`refusing to plan teardown of '${e}' — not a branch namespace`);
|
|
1996
2012
|
return [{
|
|
1997
2013
|
kind: "drop-namespace",
|
|
1998
2014
|
detail: e
|
|
1999
2015
|
}];
|
|
2000
|
-
},
|
|
2016
|
+
}, Zo = (e) => {
|
|
2001
2017
|
if (e === void 0 || e === "") return !1;
|
|
2002
2018
|
try {
|
|
2003
2019
|
let t = new URL(e).hostname.toLowerCase();
|
|
@@ -2005,27 +2021,27 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
2005
2021
|
} catch {
|
|
2006
2022
|
return /\bneon\.(tech|build)\b/i.test(e);
|
|
2007
2023
|
}
|
|
2008
|
-
},
|
|
2024
|
+
}, Qo = (e) => e.prefer === void 0 ? e.seed === "copy" && Zo(e.dbUrl) ? "neon-cow" : "namespace" : e.prefer, $o = (e, t = {}) => [{
|
|
2009
2025
|
kind: "neon-branch-create",
|
|
2010
2026
|
detail: `${t.parentBranch ?? "main"} → ${e} (copy-on-write)`
|
|
2011
|
-
}],
|
|
2012
|
-
if (!
|
|
2027
|
+
}], es = (e) => {
|
|
2028
|
+
if (!Go(e)) throw new q(`refusing to plan teardown of '${e}' — not a branch namespace`);
|
|
2013
2029
|
return [{
|
|
2014
2030
|
kind: "neon-branch-delete",
|
|
2015
2031
|
detail: e
|
|
2016
2032
|
}];
|
|
2017
|
-
},
|
|
2033
|
+
}, ts = (e) => e.mechanism === "neon-cow" ? $o(e.branchId, e.neonParentBranch === void 0 ? {} : { parentBranch: e.neonParentBranch }) : Yo(e.branchId, e.tableNames, e.seed, e.parentNamespace, e.foreignKeys, e.indexNames), ns = (e, t) => e === "neon-cow" ? es(t) : Xo(t), rs = (e, t, n) => {
|
|
2018
2034
|
let r = e.filter((e) => e.state !== "destroyed" && e.state !== "destroying"), i = r.filter((e) => n - e.createdAtMs > t.ttlMs).map((e) => e.namespace), a = new Set(i);
|
|
2019
2035
|
return {
|
|
2020
2036
|
evict: i,
|
|
2021
2037
|
rejectNew: r.filter((e) => !a.has(e.namespace)).length >= t.maxBranches
|
|
2022
2038
|
};
|
|
2023
|
-
},
|
|
2024
|
-
let n =
|
|
2039
|
+
}, is = async (e, t) => {
|
|
2040
|
+
let n = Wo(e.appSlug, e.prNumber), r = Qo({
|
|
2025
2041
|
seed: e.seed,
|
|
2026
2042
|
...e.dbUrl === void 0 ? {} : { dbUrl: e.dbUrl },
|
|
2027
2043
|
...e.prefer === void 0 ? {} : { prefer: e.prefer }
|
|
2028
|
-
}), i =
|
|
2044
|
+
}), i = ts({
|
|
2029
2045
|
mechanism: r,
|
|
2030
2046
|
branchId: n,
|
|
2031
2047
|
tableNames: e.tableNames,
|
|
@@ -2054,16 +2070,16 @@ var It = (e) => typeof e == "string" && e !== "" ? e : null, Lt = async (e, t, n
|
|
|
2054
2070
|
mechanism: r,
|
|
2055
2071
|
steps: i
|
|
2056
2072
|
};
|
|
2057
|
-
},
|
|
2058
|
-
let r =
|
|
2073
|
+
}, as = async (e, t, n) => {
|
|
2074
|
+
let r = ns(t, e);
|
|
2059
2075
|
return t === "neon-cow" ? await n.neonBranchDelete(e) : await n.dropNamespace(e), r;
|
|
2060
|
-
},
|
|
2076
|
+
}, os = (e, t, n) => rs(e, t, n), J = (e) => `"${e.replace(/"/g, "\"\"")}"`, ss = {
|
|
2061
2077
|
cascade: "CASCADE",
|
|
2062
2078
|
restrict: "RESTRICT",
|
|
2063
2079
|
setNull: "SET NULL",
|
|
2064
2080
|
noAction: "NO ACTION"
|
|
2065
|
-
},
|
|
2066
|
-
let n = t.columns.map(
|
|
2081
|
+
}, Y = (e) => `'${e.replace(/'/g, "''")}'`, cs = (e, t) => {
|
|
2082
|
+
let n = t.columns.map(Y).join(", ");
|
|
2067
2083
|
return `DO $voltro_branch$
|
|
2068
2084
|
DECLARE derived name;
|
|
2069
2085
|
BEGIN
|
|
@@ -2072,11 +2088,11 @@ BEGIN
|
|
|
2072
2088
|
JOIN pg_class i ON i.oid = x.indexrelid
|
|
2073
2089
|
JOIN pg_class c ON c.oid = x.indrelid
|
|
2074
2090
|
JOIN pg_namespace nsp ON nsp.oid = c.relnamespace
|
|
2075
|
-
WHERE nsp.nspname = ${
|
|
2076
|
-
AND c.relname = ${
|
|
2091
|
+
WHERE nsp.nspname = ${Y(e)}
|
|
2092
|
+
AND c.relname = ${Y(t.table)}
|
|
2077
2093
|
AND NOT x.indisprimary
|
|
2078
2094
|
AND x.indisunique = ${t.unique ? "true" : "false"}
|
|
2079
|
-
AND i.relname <> ${
|
|
2095
|
+
AND i.relname <> ${Y(t.name)}
|
|
2080
2096
|
AND (
|
|
2081
2097
|
SELECT array_agg(att.attname::text ORDER BY k.ord)
|
|
2082
2098
|
FROM unnest(x.indkey::int2[]) WITH ORDINALITY AS k(attnum, ord)
|
|
@@ -2085,39 +2101,39 @@ BEGIN
|
|
|
2085
2101
|
LIMIT 1;
|
|
2086
2102
|
IF derived IS NOT NULL THEN
|
|
2087
2103
|
EXECUTE format('ALTER INDEX %I.%I RENAME TO %I',
|
|
2088
|
-
${
|
|
2104
|
+
${Y(e)}, derived, ${Y(t.name)});
|
|
2089
2105
|
END IF;
|
|
2090
2106
|
END
|
|
2091
2107
|
$voltro_branch$`;
|
|
2092
|
-
},
|
|
2093
|
-
let n = t.onDelete === void 0 ? "" : ` ON DELETE ${
|
|
2094
|
-
return `ALTER TABLE ${
|
|
2095
|
-
},
|
|
2108
|
+
}, ls = (e, t) => {
|
|
2109
|
+
let n = t.onDelete === void 0 ? "" : ` ON DELETE ${ss[t.onDelete]}`, r = t.onUpdate === void 0 ? "" : ` ON UPDATE ${ss[t.onUpdate]}`;
|
|
2110
|
+
return `ALTER TABLE ${J(e)}.${J(t.table)} ADD FOREIGN KEY (${J(t.column)}) REFERENCES ${J(e)}.${J(t.targetTable)} (${J(t.targetColumn)})${n}${r}`;
|
|
2111
|
+
}, us = (e) => {
|
|
2096
2112
|
let t = () => {
|
|
2097
2113
|
throw Error("namespace branch executor: Neon ops require a neon-cow executor (use a Neon connection for copy-on-write branches)");
|
|
2098
2114
|
};
|
|
2099
2115
|
return {
|
|
2100
|
-
createNamespace: (t) => e.run(`CREATE SCHEMA IF NOT EXISTS ${
|
|
2101
|
-
createTable: (t, n) => e.run(`CREATE TABLE IF NOT EXISTS ${
|
|
2102
|
-
copyTable: (t, n, r) => e.run(`INSERT INTO ${
|
|
2103
|
-
addForeignKey: (t, n) => e.run(
|
|
2104
|
-
restoreIndexName: (t, n) => e.run(
|
|
2105
|
-
dropNamespace: (t) => e.run(`DROP SCHEMA IF EXISTS ${
|
|
2116
|
+
createNamespace: (t) => e.run(`CREATE SCHEMA IF NOT EXISTS ${J(t)}`),
|
|
2117
|
+
createTable: (t, n) => e.run(`CREATE TABLE IF NOT EXISTS ${J(t)}.${J(n)} (LIKE ${J(n)} INCLUDING ALL)`),
|
|
2118
|
+
copyTable: (t, n, r) => e.run(`INSERT INTO ${J(n)}.${J(r)} SELECT * FROM ${J(t)}.${J(r)}`),
|
|
2119
|
+
addForeignKey: (t, n) => e.run(ls(t, n)),
|
|
2120
|
+
restoreIndexName: (t, n) => e.run(cs(t, n)),
|
|
2121
|
+
dropNamespace: (t) => e.run(`DROP SCHEMA IF EXISTS ${J(t)} CASCADE`),
|
|
2106
2122
|
neonBranchCreate: t,
|
|
2107
2123
|
neonBranchDelete: t
|
|
2108
2124
|
};
|
|
2109
|
-
},
|
|
2125
|
+
}, ds = class extends Error {
|
|
2110
2126
|
_tag = "TenantResidencyUnresolved";
|
|
2111
2127
|
constructor(e) {
|
|
2112
2128
|
super(e), this.name = "TenantResidencyUnresolved";
|
|
2113
2129
|
}
|
|
2114
|
-
},
|
|
2130
|
+
}, fs = class extends Error {
|
|
2115
2131
|
region;
|
|
2116
2132
|
_tag = "TenantRegionUnavailable";
|
|
2117
2133
|
constructor(e, t) {
|
|
2118
2134
|
super(t), this.region = e, this.name = "TenantRegionUnavailable";
|
|
2119
2135
|
}
|
|
2120
|
-
},
|
|
2136
|
+
}, ps, ms = (e) => {
|
|
2121
2137
|
if (e.homes === void 0) throw Error("setResidencyConfig: `homes` is required");
|
|
2122
2138
|
if (e.servableRegions === void 0 || e.servableRegions.length === 0) throw Error("setResidencyConfig: `servableRegions` must be non-empty (which regions this deployment serves)");
|
|
2123
2139
|
let t = /* @__PURE__ */ new Map();
|
|
@@ -2126,98 +2142,98 @@ $voltro_branch$`;
|
|
|
2126
2142
|
if (e !== void 0 && e !== n.region) throw Error(`residency: tenant '${n.tenantId}' is mapped to two regions ('${e}' and '${n.region}')`);
|
|
2127
2143
|
t.set(n.tenantId, n.region);
|
|
2128
2144
|
}
|
|
2129
|
-
return
|
|
2130
|
-
},
|
|
2131
|
-
|
|
2132
|
-
},
|
|
2133
|
-
if (e == null || e === "") throw new
|
|
2145
|
+
return ps = e, e;
|
|
2146
|
+
}, hs = () => ps, gs = () => {
|
|
2147
|
+
ps = void 0;
|
|
2148
|
+
}, _s = (e, t) => {
|
|
2149
|
+
if (e == null || e === "") throw new ds("residency is on but the request has no resolvable tenant — refusing to bind a store");
|
|
2134
2150
|
let n = t.homes.find((t) => t.tenantId === e);
|
|
2135
|
-
if (n === void 0) throw new
|
|
2151
|
+
if (n === void 0) throw new ds(`no residency home is mapped for tenant '${e}' — refusing to fall back to a default store (would be a residency violation)`);
|
|
2136
2152
|
return n;
|
|
2137
|
-
},
|
|
2138
|
-
let n =
|
|
2153
|
+
}, vs = (e, t) => t.servableRegions.includes(e), ys = (e, t) => {
|
|
2154
|
+
let n = _s(e.tenantId, t), r = _r(n.tenantId);
|
|
2139
2155
|
return {
|
|
2140
2156
|
region: n.region,
|
|
2141
2157
|
namespace: r,
|
|
2142
2158
|
...n.connectionKey === void 0 ? {} : { connectionKey: n.connectionKey }
|
|
2143
2159
|
};
|
|
2144
|
-
},
|
|
2145
|
-
let r =
|
|
2146
|
-
if (!
|
|
2160
|
+
}, bs = (e, t, n) => {
|
|
2161
|
+
let r = ys(e, t);
|
|
2162
|
+
if (!vs(r.region, t)) throw new fs(r.region, `tenant homed in region '${r.region}', which this deployment does not serve — route to the home region`);
|
|
2147
2163
|
let i = n.get(r.region);
|
|
2148
|
-
if (i === void 0) throw new
|
|
2164
|
+
if (i === void 0) throw new fs(r.region, `no store handle for home region '${r.region}' (declared servable but not wired) — failing closed`);
|
|
2149
2165
|
return {
|
|
2150
2166
|
store: i,
|
|
2151
2167
|
placement: r
|
|
2152
2168
|
};
|
|
2153
|
-
},
|
|
2154
|
-
let { store: i, placement: a } =
|
|
2169
|
+
}, xs = async (e, t, n, r) => {
|
|
2170
|
+
let { store: i, placement: a } = bs(e, t, n);
|
|
2155
2171
|
return await r(i, a), a;
|
|
2156
|
-
},
|
|
2172
|
+
}, X = (e) => {
|
|
2157
2173
|
if (typeof e == "string") return e;
|
|
2158
2174
|
if ("jsonPath" in e) {
|
|
2159
2175
|
let { column: t, path: n, numeric: r } = e.jsonPath;
|
|
2160
2176
|
return `(json:${t}->${n.join(".")}${r ? "::num" : ""})`;
|
|
2161
2177
|
}
|
|
2162
2178
|
return `(${e.expr})`;
|
|
2163
|
-
},
|
|
2179
|
+
}, Z = (e) => e.map(X), Ss = (e, t) => {
|
|
2164
2180
|
if (e.length !== t.length) return !1;
|
|
2165
|
-
for (let n = 0; n < e.length; n++) if (
|
|
2181
|
+
for (let n = 0; n < e.length; n++) if (X(e[n]) !== X(t[n])) return !1;
|
|
2166
2182
|
return !0;
|
|
2167
|
-
},
|
|
2183
|
+
}, Cs = (e, t) => {
|
|
2168
2184
|
if (e.length >= t.length) return !1;
|
|
2169
|
-
for (let n = 0; n < e.length; n++) if (
|
|
2185
|
+
for (let n = 0; n < e.length; n++) if (X(e[n]) !== X(t[n])) return !1;
|
|
2170
2186
|
return !0;
|
|
2171
|
-
},
|
|
2187
|
+
}, ws = (e) => {
|
|
2172
2188
|
let t = [], n = e.appliedIndexes, r = e.tableName;
|
|
2173
2189
|
for (let e = 0; e < n.length; e++) {
|
|
2174
2190
|
let i = n[e];
|
|
2175
2191
|
for (let a = e + 1; a < n.length; a++) {
|
|
2176
2192
|
let e = n[a];
|
|
2177
|
-
if (
|
|
2193
|
+
if (Ss(i.fields, e.fields)) {
|
|
2178
2194
|
let [n, a] = i.name < e.name ? [i, e] : [e, i];
|
|
2179
2195
|
t.push({
|
|
2180
2196
|
kind: "duplicate-fields",
|
|
2181
2197
|
table: r,
|
|
2182
2198
|
redundant: n,
|
|
2183
2199
|
coveredBy: a,
|
|
2184
|
-
message: `'${r}' has two indexes on the same fields [${
|
|
2200
|
+
message: `'${r}' has two indexes on the same fields [${Z(i.fields).join(", ")}]: '${i.name}' and '${e.name}'. The B-tree engine builds both, costing write throughput + disk space for no read benefit. Drop '${n.name}' (or '${a.name}' if you prefer the other name). Applies to every supported dialect (Postgres / MySQL / MariaDB / MSSQL / SQLite).`
|
|
2185
2201
|
});
|
|
2186
2202
|
continue;
|
|
2187
2203
|
}
|
|
2188
|
-
|
|
2204
|
+
Cs(i.fields, e.fields) ? t.push({
|
|
2189
2205
|
kind: "redundant-prefix",
|
|
2190
2206
|
table: r,
|
|
2191
2207
|
redundant: i,
|
|
2192
2208
|
coveredBy: e,
|
|
2193
|
-
message: `'${r}' index '${i.name}' on [${
|
|
2194
|
-
}) :
|
|
2209
|
+
message: `'${r}' index '${i.name}' on [${Z(i.fields).join(", ")}] is a leading prefix of '${e.name}' on [${Z(e.fields).join(", ")}]. Every B-tree dialect we support (Postgres / MySQL / MariaDB / MSSQL / SQLite) can serve a [${Z(i.fields).join(", ")}] query from '${e.name}' too via the leading-prefix rule, so '${i.name}' adds write cost without read benefit. Drop '${i.name}' — unless you specifically need a smaller index for cache locality on a write-heavy table (rare; profile first).`
|
|
2210
|
+
}) : Cs(e.fields, i.fields) && t.push({
|
|
2195
2211
|
kind: "redundant-prefix",
|
|
2196
2212
|
table: r,
|
|
2197
2213
|
redundant: e,
|
|
2198
2214
|
coveredBy: i,
|
|
2199
|
-
message: `'${r}' index '${e.name}' on [${
|
|
2215
|
+
message: `'${r}' index '${e.name}' on [${Z(e.fields).join(", ")}] is a leading prefix of '${i.name}' on [${Z(i.fields).join(", ")}]. Every B-tree dialect we support (Postgres / MySQL / MariaDB / MSSQL / SQLite) can serve a [${Z(e.fields).join(", ")}] query from '${i.name}' too via the leading-prefix rule, so '${e.name}' adds write cost without read benefit. Drop '${e.name}' — unless you specifically need a smaller index for cache locality on a write-heavy table (rare; profile first).`
|
|
2200
2216
|
});
|
|
2201
2217
|
}
|
|
2202
2218
|
}
|
|
2203
2219
|
return t;
|
|
2204
|
-
},
|
|
2220
|
+
}, Ts = (e) => {
|
|
2205
2221
|
let t = [];
|
|
2206
|
-
for (let n of e) t.push(...
|
|
2222
|
+
for (let n of e) t.push(...ws(n));
|
|
2207
2223
|
return t;
|
|
2208
|
-
},
|
|
2209
|
-
let n = t.color ?? !0, r = t.indent ?? " ", i = (e) =>
|
|
2224
|
+
}, Es = "\x1B[", Ds = `${Es}38;2;245;158;11m`, Os = `${Es}38;2;16;185;129m`, ks = (e, t) => `${e}${t}${A.reset}`, Q = (e, t, n) => e ? typeof t == "function" ? t(n) : ks(t, n) : n, As = (e, t = {}) => {
|
|
2225
|
+
let n = t.color ?? !0, r = t.indent ?? " ", i = (e) => Q(n, A.dim, e.padEnd(11)), a = (e) => Q(n, (e) => A.bold(A.brand(e)), e), o = (e) => Q(n, A.brightRed24, e), s = (e) => Q(n, Os, e), c = (e) => Q(n, Ds, e), l = (e) => Q(n, A.dim, e), u = `${c("index audit")} ${l("·")} ${Q(n, A.bold, e.kind)}`, d = e.redundant.fields.length, f = Z(e.coveredBy.fields.slice(d)), p = (e, t) => {
|
|
2210
2226
|
let n = e.slice(0, t), r = e.slice(t), i = [];
|
|
2211
2227
|
return n.length > 0 && i.push(s(`[${n.join(", ")}]`)), r.length > 0 && i.push(c(`+ [${r.join(", ")}]`)), i.join(" ");
|
|
2212
2228
|
}, m = [];
|
|
2213
|
-
return m.push(u), m.push(""), m.push(`${i("table")}${a(e.table)}`), m.push(""), m.push(`${i("redundant")}${o(e.redundant.name)}`), m.push(`${i("")}${p(
|
|
2214
|
-
},
|
|
2229
|
+
return m.push(u), m.push(""), m.push(`${i("table")}${a(e.table)}`), m.push(""), m.push(`${i("redundant")}${o(e.redundant.name)}`), m.push(`${i("")}${p(Z(e.redundant.fields), d)}`), m.push(""), m.push(`${i("covered by")}${s(e.coveredBy.name)}`), m.push(`${i("")}${p(Z(e.coveredBy.fields), d)}`), f.length > 0 && m.push(`${i("")}${l(`└ extra columns: ${f.join(", ")}`)}`), m.push(""), e.kind === "redundant-prefix" ? (m.push(`${i("why")}B-tree indexes serve queries that filter on a LEADING PREFIX of`), m.push(`${i("")}their column list. The wider index already covers every`), m.push(`${i("")}query the narrower one can serve.`), m.push(`${i("")}${l("Same rule on Postgres / MySQL / MariaDB / MSSQL / SQLite.")}`)) : (m.push(`${i("why")}Two indexes on the same columns under different names.`), m.push(`${i("")}The B-tree engine builds both — same coverage, double cost.`), m.push(`${i("")}${l("Same rule on Postgres / MySQL / MariaDB / MSSQL / SQLite.")}`)), m.push(""), m.push(`${i("cost")}Every ${a("INSERT")} / ${a("UPDATE")} / ${a("DELETE")} on this table writes`), m.push(`${i("")}to ${Q(n, A.bold, "both")} B-trees. Pure write overhead, zero read benefit.`), m.push(""), m.push(`${i("fix")}Drop ${o(e.redundant.name)}`), m.push(`${i("")}${l("Keep only if profiling shows the smaller index gives a")}`), m.push(`${i("")}${l("measurable cache-locality boost on a write-heavy hot path.")}`), m.push(`${i("")}${l("Silence with VOLTRO_INDEX_AUDIT=off if intentional.")}`), m.map((e) => `${r}${e}`).join("\n");
|
|
2230
|
+
}, js = (e, { apply: t, undo: n }) => ({
|
|
2215
2231
|
name: e,
|
|
2216
2232
|
apply: t,
|
|
2217
2233
|
undo: n
|
|
2218
|
-
}),
|
|
2234
|
+
}), Ms = (e) => {
|
|
2219
2235
|
if (!/^\d{14}_/.test(e.id)) throw Error(`migration "${e.id}": id must start with a 14-digit timestamp followed by "_" (convention: <YYYYMMDDHHMMSS>_<slug>)`);
|
|
2220
|
-
let t = e.steps({ step:
|
|
2236
|
+
let t = e.steps({ step: js });
|
|
2221
2237
|
if (t.length === 0) throw Error(`migration "${e.id}": at least one step is required`);
|
|
2222
2238
|
return {
|
|
2223
2239
|
__migration: !0,
|
|
@@ -2225,99 +2241,99 @@ $voltro_branch$`;
|
|
|
2225
2241
|
name: e.name,
|
|
2226
2242
|
steps: t
|
|
2227
2243
|
};
|
|
2228
|
-
},
|
|
2229
|
-
id:
|
|
2230
|
-
name:
|
|
2231
|
-
hash:
|
|
2232
|
-
status:
|
|
2233
|
-
appliedAt:
|
|
2234
|
-
durationMs:
|
|
2235
|
-
errorMessage:
|
|
2236
|
-
errorStack:
|
|
2237
|
-
workflowRunId:
|
|
2238
|
-
createdAt:
|
|
2239
|
-
updatedAt:
|
|
2240
|
-
}).index(["status"]),
|
|
2241
|
-
id:
|
|
2242
|
-
name:
|
|
2243
|
-
lifecycle:
|
|
2244
|
-
fingerprint:
|
|
2245
|
-
cronSchedule:
|
|
2244
|
+
}, Ns = (e) => typeof e == "object" && !!e && "__migration" in e && e.__migration === !0, Ps = T("_voltro_migrations", {
|
|
2245
|
+
id: b({ prefix: "mig" }),
|
|
2246
|
+
name: v(),
|
|
2247
|
+
hash: v(),
|
|
2248
|
+
status: v(),
|
|
2249
|
+
appliedAt: S().nullable(),
|
|
2250
|
+
durationMs: x().nullable(),
|
|
2251
|
+
errorMessage: v().nullable(),
|
|
2252
|
+
errorStack: v().nullable(),
|
|
2253
|
+
workflowRunId: v().nullable(),
|
|
2254
|
+
createdAt: S().default("now"),
|
|
2255
|
+
updatedAt: S().default("now").onUpdate("now")
|
|
2256
|
+
}).index(["status"]), Fs = T("_voltro_seeds", {
|
|
2257
|
+
id: b({ prefix: "seed" }),
|
|
2258
|
+
name: v(),
|
|
2259
|
+
lifecycle: v(),
|
|
2260
|
+
fingerprint: v(),
|
|
2261
|
+
cronSchedule: v().nullable(),
|
|
2246
2262
|
watchedTables: i().nullable(),
|
|
2247
|
-
nextRunAt:
|
|
2248
|
-
lastRunStatus:
|
|
2249
|
-
lastRunAt:
|
|
2250
|
-
lastRunDurationMs:
|
|
2251
|
-
lastRunRowsTouched:
|
|
2252
|
-
lastError:
|
|
2253
|
-
workflowRunId:
|
|
2254
|
-
createdAt:
|
|
2255
|
-
updatedAt:
|
|
2256
|
-
}).index(["lifecycle"]).index(["nextRunAt"]),
|
|
2257
|
-
id:
|
|
2258
|
-
fingerprint:
|
|
2259
|
-
liveFingerprint:
|
|
2263
|
+
nextRunAt: S().nullable(),
|
|
2264
|
+
lastRunStatus: v().nullable(),
|
|
2265
|
+
lastRunAt: S().nullable(),
|
|
2266
|
+
lastRunDurationMs: x().nullable(),
|
|
2267
|
+
lastRunRowsTouched: x().nullable(),
|
|
2268
|
+
lastError: v().nullable(),
|
|
2269
|
+
workflowRunId: v().nullable(),
|
|
2270
|
+
createdAt: S().default("now"),
|
|
2271
|
+
updatedAt: S().default("now").onUpdate("now")
|
|
2272
|
+
}).index(["lifecycle"]).index(["nextRunAt"]), Is = T("_voltro_migration_plans", {
|
|
2273
|
+
id: b({ prefix: "plan" }),
|
|
2274
|
+
fingerprint: v(),
|
|
2275
|
+
liveFingerprint: v().nullable(),
|
|
2260
2276
|
operations: i(),
|
|
2261
2277
|
rollbackPlan: i().nullable(),
|
|
2262
|
-
appliedBy:
|
|
2263
|
-
environment:
|
|
2264
|
-
source:
|
|
2265
|
-
onlineStrategy:
|
|
2266
|
-
durationMs:
|
|
2267
|
-
notes:
|
|
2268
|
-
squashedAt:
|
|
2269
|
-
appliedAt:
|
|
2270
|
-
createdAt:
|
|
2271
|
-
updatedAt:
|
|
2272
|
-
}).index(["fingerprint"]).index(["environment"]),
|
|
2273
|
-
id:
|
|
2274
|
-
scope:
|
|
2275
|
-
key:
|
|
2276
|
-
status:
|
|
2278
|
+
appliedBy: v(),
|
|
2279
|
+
environment: v(),
|
|
2280
|
+
source: v(),
|
|
2281
|
+
onlineStrategy: v().nullable(),
|
|
2282
|
+
durationMs: x(),
|
|
2283
|
+
notes: v().nullable(),
|
|
2284
|
+
squashedAt: S().nullable(),
|
|
2285
|
+
appliedAt: S().default("now"),
|
|
2286
|
+
createdAt: S().default("now"),
|
|
2287
|
+
updatedAt: S().default("now").onUpdate("now")
|
|
2288
|
+
}).index(["fingerprint"]).index(["environment"]), Ls = T("_voltro_idempotency", {
|
|
2289
|
+
id: b({ prefix: "idem" }),
|
|
2290
|
+
scope: v(),
|
|
2291
|
+
key: v(),
|
|
2292
|
+
status: v(),
|
|
2277
2293
|
response: i().nullable(),
|
|
2278
|
-
createdAt:
|
|
2279
|
-
}).unique(["scope", "key"]).index(["createdAt"]),
|
|
2280
|
-
id:
|
|
2281
|
-
key:
|
|
2282
|
-
value:
|
|
2283
|
-
expiresAt:
|
|
2284
|
-
createdAt:
|
|
2285
|
-
updatedAt:
|
|
2286
|
-
}).index(["expiresAt"]),
|
|
2287
|
-
id:
|
|
2288
|
-
tenantId:
|
|
2289
|
-
name:
|
|
2290
|
-
hashedKey:
|
|
2291
|
-
keyPrefix:
|
|
2294
|
+
createdAt: S().default("now")
|
|
2295
|
+
}).unique(["scope", "key"]).index(["createdAt"]), Rs = T("_voltro_kv", {
|
|
2296
|
+
id: b({ prefix: "kv" }),
|
|
2297
|
+
key: v().unique(),
|
|
2298
|
+
value: v().nullable(),
|
|
2299
|
+
expiresAt: S().nullable(),
|
|
2300
|
+
createdAt: S().default("now"),
|
|
2301
|
+
updatedAt: S().default("now").onUpdate("now")
|
|
2302
|
+
}).index(["expiresAt"]), zs = T("_voltro_api_keys", {
|
|
2303
|
+
id: b({ prefix: "apikey" }),
|
|
2304
|
+
tenantId: v().nullable(),
|
|
2305
|
+
name: v(),
|
|
2306
|
+
hashedKey: v().maxLength(64).unique(),
|
|
2307
|
+
keyPrefix: v(),
|
|
2292
2308
|
scopes: i().nullable(),
|
|
2293
|
-
lastUsedAt:
|
|
2294
|
-
requestCount:
|
|
2295
|
-
expiresAt:
|
|
2296
|
-
revokedAt:
|
|
2297
|
-
createdBy:
|
|
2298
|
-
onBehalfOf:
|
|
2309
|
+
lastUsedAt: S().nullable(),
|
|
2310
|
+
requestCount: x().default(0),
|
|
2311
|
+
expiresAt: S().nullable(),
|
|
2312
|
+
revokedAt: S().nullable(),
|
|
2313
|
+
createdBy: v().nullable(),
|
|
2314
|
+
onBehalfOf: v().nullable(),
|
|
2299
2315
|
metadata: i().nullable(),
|
|
2300
|
-
createdAt:
|
|
2301
|
-
}).index("byApiKeyTenant", ["tenantId"]),
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
],
|
|
2316
|
+
createdAt: S().default("now")
|
|
2317
|
+
}).index("byApiKeyTenant", ["tenantId"]), Bs = [
|
|
2318
|
+
Ps,
|
|
2319
|
+
Fs,
|
|
2320
|
+
Is
|
|
2321
|
+
], $ = "id", Vs = (e, t) => {
|
|
2306
2322
|
let n = p(e);
|
|
2307
2323
|
if (n === void 0) return t;
|
|
2308
|
-
let r = n.fields[
|
|
2309
|
-
if (r === void 0 || t[
|
|
2324
|
+
let r = n.fields[$]?.idScheme;
|
|
2325
|
+
if (r === void 0 || t[$] != null) return t;
|
|
2310
2326
|
if (r.kind === "numeric") {
|
|
2311
|
-
if (!(
|
|
2327
|
+
if (!($ in t)) return t;
|
|
2312
2328
|
let e = { ...t };
|
|
2313
|
-
return delete e[
|
|
2329
|
+
return delete e[$], e;
|
|
2314
2330
|
}
|
|
2315
|
-
let i =
|
|
2331
|
+
let i = lt(r, e);
|
|
2316
2332
|
return i === null ? t : {
|
|
2317
2333
|
...t,
|
|
2318
|
-
[
|
|
2334
|
+
[$]: i
|
|
2319
2335
|
};
|
|
2320
|
-
},
|
|
2336
|
+
}, Hs = (e, t) => t.map((t) => Vs(e, t)), Us = {
|
|
2321
2337
|
postgres: { maxBindParameters: 65535 },
|
|
2322
2338
|
mysql: { maxBindParameters: 65535 },
|
|
2323
2339
|
mariadb: { maxBindParameters: 65535 },
|
|
@@ -2327,49 +2343,49 @@ $voltro_branch$`;
|
|
|
2327
2343
|
},
|
|
2328
2344
|
sqlite: { maxBindParameters: 32766 },
|
|
2329
2345
|
turso: { maxBindParameters: 32766 }
|
|
2330
|
-
},
|
|
2346
|
+
}, Ws = (e) => Us[e], Gs = (e) => {
|
|
2331
2347
|
let t = /* @__PURE__ */ new Set();
|
|
2332
2348
|
for (let n of e) for (let e of Object.keys(n)) t.add(e);
|
|
2333
2349
|
return t.size;
|
|
2334
|
-
},
|
|
2350
|
+
}, Ks = (e, t) => {
|
|
2335
2351
|
if (t === void 0 || e.length === 0) return [e];
|
|
2336
|
-
let n = Math.max(1,
|
|
2352
|
+
let n = Math.max(1, Gs(e)), r = Math.floor(t.maxBindParameters / n), i = t.maxRowsPerStatement ?? Infinity, a = Math.max(1, Math.min(r, i));
|
|
2337
2353
|
if (e.length <= a) return [e];
|
|
2338
2354
|
let o = [];
|
|
2339
2355
|
for (let t = 0; t < e.length; t += a) o.push(e.slice(t, t + a));
|
|
2340
2356
|
return o;
|
|
2341
|
-
},
|
|
2357
|
+
}, qs = (e, t) => {
|
|
2342
2358
|
let n = [], r = t;
|
|
2343
2359
|
for (let [t, i] of Object.entries(e.fields)) {
|
|
2344
2360
|
let e = i;
|
|
2345
2361
|
e.nullable || e.hasDefault || e.idScheme !== void 0 || e.generatedAs !== void 0 || (r[t] === void 0 || r[t] === null) && n.push(t);
|
|
2346
2362
|
}
|
|
2347
2363
|
return n;
|
|
2348
|
-
},
|
|
2364
|
+
}, Js = (e, t) => {
|
|
2349
2365
|
let n = t;
|
|
2350
2366
|
return e.filter((e) => n[e] === void 0 || n[e] === null);
|
|
2351
|
-
},
|
|
2367
|
+
}, Ys = (e, t, n) => e.insert(t.tableName, n), Xs = (e, t, n, r) => e.upsert(t.tableName, n, {
|
|
2352
2368
|
conflictColumns: r.conflictColumns,
|
|
2353
2369
|
...r.update === void 0 ? {} : { update: r.update }
|
|
2354
|
-
}),
|
|
2370
|
+
}), Zs = (e, t, n, r) => e.updateMany(t.tableName, n, { where: r.where }), Qs = 512, $s = (e) => {
|
|
2355
2371
|
e.setMaxListeners(512);
|
|
2356
|
-
},
|
|
2372
|
+
}, ec = (e, t, n) => ({
|
|
2357
2373
|
_tag: "PrimaryKeyConflictError",
|
|
2358
2374
|
table: e,
|
|
2359
2375
|
key: t,
|
|
2360
2376
|
cause: n
|
|
2361
|
-
}),
|
|
2377
|
+
}), tc = (e) => typeof e == "object" && !!e && e._tag === "PrimaryKeyConflictError", nc = {
|
|
2362
2378
|
postgres: 5432,
|
|
2363
2379
|
mysql: 3306,
|
|
2364
2380
|
mariadb: 3306,
|
|
2365
2381
|
mssql: 1433
|
|
2366
|
-
},
|
|
2382
|
+
}, rc = /* @__PURE__ */ new Set([
|
|
2367
2383
|
"localhost",
|
|
2368
2384
|
"127.0.0.1",
|
|
2369
2385
|
"::1",
|
|
2370
2386
|
"[::1]",
|
|
2371
2387
|
"0.0.0.0"
|
|
2372
|
-
]),
|
|
2388
|
+
]), ic = (e) => {
|
|
2373
2389
|
let t = e.host, n = e.port, r = e.database;
|
|
2374
2390
|
if (e.url !== void 0 && e.url !== "") try {
|
|
2375
2391
|
let i = new URL(e.url);
|
|
@@ -2378,29 +2394,29 @@ $voltro_branch$`;
|
|
|
2378
2394
|
return;
|
|
2379
2395
|
}
|
|
2380
2396
|
if (t === void 0 || t === "" || r === void 0 || r === "") return;
|
|
2381
|
-
let i = n ??
|
|
2397
|
+
let i = n ?? nc[(e.dialect ?? "").toLowerCase()];
|
|
2382
2398
|
if (i === void 0 || !Number.isFinite(i)) return;
|
|
2383
2399
|
let a = t.toLowerCase();
|
|
2384
2400
|
return {
|
|
2385
|
-
host:
|
|
2401
|
+
host: rc.has(a) ? "localhost" : a,
|
|
2386
2402
|
port: i,
|
|
2387
2403
|
database: r.toLowerCase()
|
|
2388
2404
|
};
|
|
2389
|
-
},
|
|
2390
|
-
let t =
|
|
2391
|
-
if (t !== void 0) return
|
|
2392
|
-
},
|
|
2405
|
+
}, ac = (e) => {
|
|
2406
|
+
let t = ic(e);
|
|
2407
|
+
if (t !== void 0) return It("sha256").update(`${t.host}\u0000${t.port}\u0000${t.database}`).digest("hex").slice(0, 32);
|
|
2408
|
+
}, oc = (e, t) => e === void 0 || t === void 0 ? "unknown" : e === t ? "same" : "different", sc = {
|
|
2393
2409
|
app: 3,
|
|
2394
2410
|
plugin: 2,
|
|
2395
2411
|
framework: 1
|
|
2396
|
-
},
|
|
2412
|
+
}, cc = /* @__PURE__ */ new Map(), lc = [], uc = () => [...lc], dc = (e) => {
|
|
2397
2413
|
if (!e.table || !e.timeColumn) throw Error("registerRetention: table + timeColumn are required");
|
|
2398
2414
|
if (!Number.isFinite(e.ttlMs) || e.ttlMs <= 0) throw Error(`registerRetention(${e.table}): ttlMs must be a positive number`);
|
|
2399
|
-
let t = e.source ?? "app", n =
|
|
2415
|
+
let t = e.source ?? "app", n = cc.get(e.table);
|
|
2400
2416
|
if (n !== void 0) {
|
|
2401
2417
|
let r = n.source ?? "app", i = n.ttlMs !== e.ttlMs || n.where !== void 0 != (e.where !== void 0);
|
|
2402
|
-
if (
|
|
2403
|
-
i &&
|
|
2418
|
+
if (sc[t] < sc[r]) {
|
|
2419
|
+
i && lc.push({
|
|
2404
2420
|
table: e.table,
|
|
2405
2421
|
kept: {
|
|
2406
2422
|
source: r,
|
|
@@ -2413,7 +2429,7 @@ $voltro_branch$`;
|
|
|
2413
2429
|
});
|
|
2414
2430
|
return;
|
|
2415
2431
|
}
|
|
2416
|
-
i && r !== t &&
|
|
2432
|
+
i && r !== t && lc.push({
|
|
2417
2433
|
table: e.table,
|
|
2418
2434
|
kept: {
|
|
2419
2435
|
source: t,
|
|
@@ -2425,35 +2441,35 @@ $voltro_branch$`;
|
|
|
2425
2441
|
}
|
|
2426
2442
|
});
|
|
2427
2443
|
}
|
|
2428
|
-
|
|
2444
|
+
cc.set(e.table, {
|
|
2429
2445
|
...e,
|
|
2430
2446
|
source: t
|
|
2431
2447
|
});
|
|
2432
|
-
},
|
|
2433
|
-
|
|
2434
|
-
},
|
|
2448
|
+
}, fc = () => [...cc.values()], pc = () => {
|
|
2449
|
+
cc.clear(), lc.length = 0;
|
|
2450
|
+
}, mc = async (e, t, n, r = 2e4) => {
|
|
2435
2451
|
let i = new Date(t - e.ttlMs), a = 0;
|
|
2436
2452
|
for (;;) {
|
|
2437
2453
|
let t = await n(e.table, e.timeColumn, i, r, e.where);
|
|
2438
2454
|
if (a += t, t < r) break;
|
|
2439
2455
|
}
|
|
2440
2456
|
return a;
|
|
2441
|
-
},
|
|
2457
|
+
}, hc = (e, t) => {
|
|
2442
2458
|
let n = Number(e ?? String(t));
|
|
2443
2459
|
return (Number.isFinite(n) && n > 0 ? n : t) * 36e5;
|
|
2444
|
-
},
|
|
2445
|
-
id:
|
|
2446
|
-
replicaId:
|
|
2447
|
-
streamName:
|
|
2448
|
-
binlogFile:
|
|
2449
|
-
binlogPosition:
|
|
2450
|
-
updatedAt:
|
|
2451
|
-
}).index(["replicaId"]),
|
|
2452
|
-
id:
|
|
2453
|
-
replicaId:
|
|
2454
|
-
streamName:
|
|
2455
|
-
ctVersion:
|
|
2456
|
-
updatedAt:
|
|
2457
|
-
}).index(["replicaId"]),
|
|
2460
|
+
}, gc = "_voltro_cdc_offsets", _c = T(gc, {
|
|
2461
|
+
id: b({ prefix: "cdcoff" }),
|
|
2462
|
+
replicaId: v(),
|
|
2463
|
+
streamName: v(),
|
|
2464
|
+
binlogFile: v(),
|
|
2465
|
+
binlogPosition: v(),
|
|
2466
|
+
updatedAt: S()
|
|
2467
|
+
}).index(["replicaId"]), vc = T(gc, {
|
|
2468
|
+
id: b({ prefix: "cdcoff" }),
|
|
2469
|
+
replicaId: v(),
|
|
2470
|
+
streamName: v(),
|
|
2471
|
+
ctVersion: v(),
|
|
2472
|
+
updatedAt: S()
|
|
2473
|
+
}).index(["replicaId"]), yc = "postgres";
|
|
2458
2474
|
//#endregion
|
|
2459
|
-
export { e as AUTO_FILLED_COLUMNS,
|
|
2475
|
+
export { e as AUTO_FILLED_COLUMNS, Us as BULK_INSERT_LIMITS, q as BranchNameInvalid, qo as BranchTransitionInvalid, gc as CDC_OFFSETS_TABLE, Qs as CHANGE_LISTENER_CEILING, gt as ColumnBuilder, Ee as DEFAULT_ACQUIRE_TIMEOUT_MS, ho as DEFAULT_EAGER_FALLBACK_WARN_INTERVAL_MS, In as DEFAULT_SUBJECT_GRAPH_DEPTH, Ut as ENCRYPTED_PREFIX, yc as ENGINE, $n as EagerCardinalityError, Ge as FILE_MIGRATION_PATTERN, ut as FRAMEWORK_LIVE_TABLES, Wt as FieldDecryptionError, Oo as INITIAL_VERSION, ta as MATCHES_NO_ROWS, Le as MAX_IDENTIFIER_LENGTH, Ft as SqlClient, Fo as TableStreamError, pr as TenantNamespaceInvalid, fr as TenantNamespaceUnresolved, fs as TenantRegionUnavailable, ds as TenantResidencyUnresolved, ko as VOLTRO_EXPIRES_MIXIN_ID, jo as VOLTRO_LOCAL_FIRST_MIXIN_ID, wo as VersionConflict, Be as WARN_IDENTIFIER_LENGTH, zs as _voltroApiKeysTable, _c as _voltroCdcOffsetsTable, Ls as _voltroIdempotencyTable, Rs as _voltroKvTable, Is as _voltroMigrationPlansTable, Ps as _voltroMigrationsTable, vc as _voltroMssqlCdcOffsetsTable, Fs as _voltroSeedsTable, Vi as actorsTable, os as admitBranch, mt as allEnumRenames, Nn as allRegisteredRelations, h as allRegisteredTables, aa as and, _e as array, Qi as arrayContains, ea as arrayHas, $i as arrayOverlaps, tr as attachEagerLoads, Da as attributionFields, ja as attributionKey, Ts as auditAllTableIndexes, ws as auditTableIndexes, pt as avg, yt as avgOver, Ba as beginLocalWrite, u as bigint, bs as bindResidentStore, c as boolean, Wo as branchNamespaceName, Ws as bulkInsertLimitsFor, le as bytes, Sa as changeDelete, ba as changeInsert, Ca as changeSoftDelete, xa as changeUpdate, Ks as chunkRowsForInsert, Pa as claimPendingAttribution, on as classifyConstraintViolation, bt as clearEnumRenames, Ua as clearPendingAttribution, Fn as clearRelationsRegistry, gs as clearResidencyConfig, pc as clearRetentions, ve as clearTableRegistry, qa as clearWriteRecorders, xr as coercePredicateValue, Sr as coercePredicateValues, _a as collectSubqueries, Me as column, un as columnSchema, Gs as columnsPerRow, Ir as compileEagerJson, F as compilePredicate, Cr as compileRawFragment, Fr as compileSelect, Bo as computeEmbedding, ac as connectionIdentity, Xi as contains, _t as count, me as countDistinct, Ye as crdtManagedColumns, n as crdtText, Ea as currentWriteAttribution, a as databaseHandle, qe as date, ae as dbEnum, co as dbMetricTagCacheSizeForTests, fo as dbOperationsInFlight, se as decimal, Ne as declareEnumRename, Yt as decodeFieldValue, Xn as decodeRowsFromSchema, en as decryptFieldsOnRead, Ms as defineMigration, st as defineMixin, ct as defineSeed, ht as denseRank, Un as deriveSubjectGraph, rt as deriveTypeIdPrefix, ln as describeDriverError, Ht as deserializeArraysOnRead, it as drainIdentifierWarnings, de as dropped, qt as encodeFieldValue, Qn as encodeRowForSchema, $t as encryptFieldsForWrite, Qt as encryptedColumnsOf, Va as endLocalWrite, rs as enforceBranchLimits, Se as ensureTableRegistered, Hi as eq, wr as escapeLike, ga as evaluatePredicate, ge as except, fa as exists, Ao as expires, ya as externalChangeEvent, nn as extractDbCause, Pe as fireSchemaChangeSeeds, Xe as fireTenantCreateSeeds, As as formatIndexAuditIssue, Wn as formatSubjectGraph, Bs as frameworkTables, lt as generateId, Ze as generateSnowflake, f as geography, te as geometry, vt as getEnumRenames, Mn as getRelation, jn as getRelations, hs as getResidencyConfig, p as getTable, Wi as gt, Gi as gte, dr as hasEagerLoads, zo as hybridSearch, b as id, Ji as inSet, ua as inSubquery, N as inferForeignKey, Ys as insertRow, x as integer, l as intersect, St as interval, Go as isBranchNamespace, vn as isEmptyRelationsSpec, Kt as isEncrypted, Gt as isFieldDecryptionError, Ae as isFileMigration, sn as isForeignKeyViolation, et as isFrameworkOwnedLiveTable, No as isLocalFirst, Ns as isMigrationDefinition, we as isMysqlFamily, Zo as isNeonConnection, ra as isNotNull, na as isNull, tc as isPrimaryKeyConflictError, cn as isQueryTimeout, Xt as isRawEncoding, vs as isRegionServable, yn as isRelationsSpec, dt as isSeedDefinition, De as isSqliteFamily, ne as isTableReactive, Qe as isView, i as json, ca as jsonField, Re as jsonIndex, s as lag, ce as lead, uc as listRetentionConflicts, fc as listRetentions, Mo as localFirst, Po as localFirstTables, Ki as lt, qi as lte, vo as makeEagerFallbackReporter, us as makeNamespaceBranchExecutor, xn as many, Sn as manyToMany, xe as materializeFields, Je as max, nt as migration, t as min, Js as missingConflictColumns, qs as missingRequiredColumns, Ke as mixin, Ui as neq, Jo as nextBranchState, ic as normaliseTarget, sa as not, pa as notExists, Yi as notInSet, da as notInSubquery, g as numeric, mo as observeDbOp, bn as one, er as oneCardinalityMessage, oa as or, C as orderClausesOf, ie as paginateBy, oe as paginateById, Wa as pendingAttributionCount, ts as planBranch, Yo as planBranchProvision, Xo as planBranchTeardown, ns as planBranchTeardownFor, $o as planNeonBranchProvision, es as planNeonBranchTeardown, Eo as planVersionedUpdate, he as pluginRef, o as pluginRefSpecOf, ec as primaryKeyConflictError, is as provisionBranch, xs as provisionResidentTenant, vr as qualifyTable, ue as queryFor, Ue as queryForView, fe as quoteIdent, $s as raiseChangeListenerCeiling, d as rank, ye as raw, Ce as readableByColumns, m as real, po as recordDbOp, Ja as recordsTable, y as reference, Li as registerCoreTables, An as registerDiscoveredRelations, Na as registerPendingAttribution, kn as registerRelations, dc as registerRetention, at as registerTable, Ga as registerWriteRecorder, _n as relations, zi as requireActors, _ as requireTable, Bi as requireTenants, so as resetDbMetricTagCacheForTests, ft as resetSnowflake, ys as residentPlacement, Rt as resolveActorSnapshot, Qo as resolveBranchMechanism, _o as resolveEagerFallbackWarnIntervalMs, Ha as resolveEchoAttribution, ee as resolveFullTextIndex, Fe as resolveIdScheme, je as resolveMixinGraph, _s as resolveTenantHome, _r as resolveTenantNamespace, hc as retentionTtlMsFromEnv, re as rowNumber, dn as rowSchema, So as runRetryingTransaction, Co as runStoreTransaction, Ta as runWithWriteAttribution, Ya as runWriteRecorders, oc as sameDatabase, gr as sanitizeIdentifierFragment, Vt as serializeArraysForWrite, ot as serverOnlyColumns, ms as setResidencyConfig, Ie as setSchemaChangeSeedHook, ze as setTenantCreateSeedHook, yo as settleTransactionExit, Do as settleVersionedUpdate, ia as spatialPredicate, Vs as stampGeneratedId, Hs as stampGeneratedIds, Zi as startsWith, Ro as streamTable, pe as sum, xt as sumOver, mc as sweepRetention, T as table, as as teardownBranch, v as text, S as timestamp, Ct as timestampMs, wt as timestampMsOrNull, r as union, be as unionAll, Ka as unregisterWriteRecorder, Zs as updateManyRow, Xs as upsertRow, $e as validateColumnName, We as validateIndexName, Pn as validateRegisteredRelations, En as validateRelationConfig, He as validateTableName, ke as validateTypeIdPrefix, Te as vector, tt as vectorDistanceToOpclass, Vo as vectorEmbedding, To as versionColumnOf, Ve as view, Oe as viewSql, va as walkLeaves, Oa as withCapturedAttribution };
|