@voltro/database 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +52 -0
- package/LICENSE +57 -0
- package/README.md +26 -0
- package/SECURITY.md +56 -0
- package/THIRD-PARTY-NOTICES.md +319 -0
- package/dist/fileBased-cc9IzIjU.js +1083 -0
- package/dist/index.d.ts +5107 -0
- package/dist/index.js +1505 -0
- package/dist/sql.d.ts +2023 -0
- package/dist/sql.js +2569 -0
- package/package.json +51 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1505 @@
|
|
|
1
|
+
import { $ as e, A as t, B as n, C as r, Ct as i, D as a, Dt as o, E as s, Et as c, F as l, G as u, H as d, I as f, J as p, K as m, L as h, M as g, N as _, O as ee, Ot as v, P as y, Q as b, R as x, S as te, St as S, T as C, Tt as ne, U as re, V as ie, W as ae, X as oe, Y as se, Z as ce, _ as le, _t as ue, a as de, at as fe, b as pe, bt as me, c as he, ct as ge, d as _e, dt as ve, et as ye, f as be, ft as xe, g as Se, gt as Ce, h as we, ht as Te, i as w, it as Ee, j as De, k as Oe, kt as ke, l as Ae, lt as je, m as Me, mt as Ne, n as Pe, nt as Fe, o as Ie, ot as Le, p as Re, pt as ze, q as Be, r as Ve, rt as He, s as Ue, st as We, t as Ge, tt as Ke, u as qe, ut as Je, v as Ye, vt as T, w as Xe, wt as Ze, x as Qe, xt as E, y as $e, yt as D, z as et } from "./fileBased-cc9IzIjU.js";
|
|
2
|
+
import { Chunk as tt, Data as nt, Effect as rt, Option as O, Stream as it } from "effect";
|
|
3
|
+
import { ansi as k } from "@voltro/logger";
|
|
4
|
+
//#region src/arrayCodec.ts
|
|
5
|
+
var at = (e) => e === "mysql" || e === "mariadb" || e === "mssql" || e === "sqlite", ot = (e) => {
|
|
6
|
+
let t = [];
|
|
7
|
+
for (let [n, r] of Object.entries(e.fields)) r.type === "array" && t.push(n);
|
|
8
|
+
return t;
|
|
9
|
+
}, st = (e, t, n) => {
|
|
10
|
+
if (!at(n) || !t) return e;
|
|
11
|
+
let r = ot(t);
|
|
12
|
+
if (r.length === 0) return e;
|
|
13
|
+
let i = { ...e };
|
|
14
|
+
for (let e of r) {
|
|
15
|
+
let t = i[e];
|
|
16
|
+
Array.isArray(t) && (i[e] = JSON.stringify(t));
|
|
17
|
+
}
|
|
18
|
+
return i;
|
|
19
|
+
}, ct = (e, t, n) => {
|
|
20
|
+
if (!at(n) || !t) return e;
|
|
21
|
+
let r = ot(t);
|
|
22
|
+
return r.length === 0 ? e : e.map((e) => {
|
|
23
|
+
let t;
|
|
24
|
+
for (let n of r) {
|
|
25
|
+
let r = e[n];
|
|
26
|
+
if (r != null && !Array.isArray(r) && typeof r == "string") try {
|
|
27
|
+
let i = JSON.parse(r);
|
|
28
|
+
Array.isArray(i) && (t ||= { ...e }, t[n] = i);
|
|
29
|
+
} catch {}
|
|
30
|
+
}
|
|
31
|
+
return t ?? e;
|
|
32
|
+
});
|
|
33
|
+
}, lt = "enc:v1:", ut = (e) => typeof e == "string" && e.startsWith("enc:v1:"), dt = (e) => {
|
|
34
|
+
let t = [];
|
|
35
|
+
for (let [n, r] of Object.entries(e.fields)) r.encrypted === !0 && t.push(n);
|
|
36
|
+
return t;
|
|
37
|
+
}, ft = (e, t, n) => {
|
|
38
|
+
if (!t) return e;
|
|
39
|
+
let r = dt(t);
|
|
40
|
+
if (r.length === 0) return e;
|
|
41
|
+
let i = { ...e };
|
|
42
|
+
for (let e of r) {
|
|
43
|
+
let r = i[e];
|
|
44
|
+
if (!(r == null || ut(r))) {
|
|
45
|
+
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)`);
|
|
46
|
+
i[e] = n.encrypt(JSON.stringify(r));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return i;
|
|
50
|
+
}, pt = (e, t, n) => {
|
|
51
|
+
if (!t) return e;
|
|
52
|
+
let r = dt(t);
|
|
53
|
+
return r.length === 0 || !n ? e : e.map((e) => {
|
|
54
|
+
let t;
|
|
55
|
+
for (let i of r) {
|
|
56
|
+
let r = e[i];
|
|
57
|
+
ut(r) && (t ??= { ...e }, t[i] = JSON.parse(n.decrypt(r)));
|
|
58
|
+
}
|
|
59
|
+
return t ?? e;
|
|
60
|
+
});
|
|
61
|
+
}, mt = (e) => typeof e == "function" ? e : () => e, ht = (e, t) => ({
|
|
62
|
+
kind: "one",
|
|
63
|
+
target: mt(e),
|
|
64
|
+
...t?.foreignKey === void 0 ? {} : { foreignKey: t.foreignKey },
|
|
65
|
+
...t?.sourceKey === void 0 ? {} : { sourceKey: t.sourceKey }
|
|
66
|
+
}), gt = (e, t) => ({
|
|
67
|
+
kind: "many",
|
|
68
|
+
target: mt(e),
|
|
69
|
+
...t?.foreignKey === void 0 ? {} : { foreignKey: t.foreignKey },
|
|
70
|
+
...t?.sourceKey === void 0 ? {} : { sourceKey: t.sourceKey }
|
|
71
|
+
}), _t = (e, t) => ({
|
|
72
|
+
kind: "manyToMany",
|
|
73
|
+
target: mt(e),
|
|
74
|
+
through: mt(t.through),
|
|
75
|
+
sourceKey: t.sourceKey,
|
|
76
|
+
targetKey: t.targetKey
|
|
77
|
+
}), vt = {
|
|
78
|
+
one: ht,
|
|
79
|
+
many: gt,
|
|
80
|
+
manyToMany: _t
|
|
81
|
+
}, yt = (e, t) => {
|
|
82
|
+
let n = typeof e == "function" ? e : () => e;
|
|
83
|
+
return {
|
|
84
|
+
source: n,
|
|
85
|
+
relations: t(vt, n())
|
|
86
|
+
};
|
|
87
|
+
}, bt = (e) => {
|
|
88
|
+
if (typeof e != "object" || !e) return !1;
|
|
89
|
+
let t = e;
|
|
90
|
+
if (typeof t.source != "function" || typeof t.relations != "object" || t.relations === null) return !1;
|
|
91
|
+
let n = Object.values(t.relations);
|
|
92
|
+
return n.length !== 0 && n.every((e) => {
|
|
93
|
+
let t = e?.kind;
|
|
94
|
+
return t === "one" || t === "many" || t === "manyToMany";
|
|
95
|
+
});
|
|
96
|
+
}, xt = ht, St = gt, Ct = _t, A = (e, t, n) => {
|
|
97
|
+
let r = [];
|
|
98
|
+
for (let [n, i] of Object.entries(e.fields)) i.type === "reference" && i.references?.()?.tableName === t && r.push(n);
|
|
99
|
+
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>' }.`);
|
|
100
|
+
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.`);
|
|
101
|
+
return r[0];
|
|
102
|
+
}, wt = Symbol.for("@voltro/database/relationsRegistry"), Tt = globalThis, j = Tt[wt] ?? (Tt[wt] = /* @__PURE__ */ new Map()), Et = (e) => {
|
|
103
|
+
let t = e.source(), n = j.get(t.tableName);
|
|
104
|
+
if (n === void 0) {
|
|
105
|
+
j.set(t.tableName, new Map(Object.entries(e.relations)));
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
for (let [r, i] of Object.entries(e.relations)) {
|
|
109
|
+
if (n.has(r)) throw Error(`duplicate relation '${r}' on '${t.tableName}': two relations() blocks both declare it. Pick distinct names or merge them into one block.`);
|
|
110
|
+
n.set(r, i);
|
|
111
|
+
}
|
|
112
|
+
}, Dt = (e) => {
|
|
113
|
+
let t = 0;
|
|
114
|
+
for (let n of Object.values(e)) bt(n) && (Et(n), t += 1);
|
|
115
|
+
return t;
|
|
116
|
+
}, M = (e) => {
|
|
117
|
+
let t = j.get(e);
|
|
118
|
+
if (t !== void 0) return Object.fromEntries(t);
|
|
119
|
+
}, Ot = (e, t) => j.get(e)?.get(t), kt = () => {
|
|
120
|
+
let e = [];
|
|
121
|
+
for (let [t, n] of j) for (let [r, i] of n) e.push({
|
|
122
|
+
source: t,
|
|
123
|
+
name: r,
|
|
124
|
+
relation: i
|
|
125
|
+
});
|
|
126
|
+
return e;
|
|
127
|
+
}, At = () => {
|
|
128
|
+
j.clear();
|
|
129
|
+
}, jt = (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, Mt = (e) => {
|
|
130
|
+
if (e == null || typeof e != "string") return e;
|
|
131
|
+
let t = e.trim();
|
|
132
|
+
if (t.length === 0) return e;
|
|
133
|
+
let n = t.charAt(0);
|
|
134
|
+
if (n !== "{" && n !== "[" && n !== "\"" && n !== "t" && n !== "f" && n !== "n" && (n < "0" || n > "9") && n !== "-") return e;
|
|
135
|
+
try {
|
|
136
|
+
return JSON.parse(t);
|
|
137
|
+
} catch {
|
|
138
|
+
return e;
|
|
139
|
+
}
|
|
140
|
+
}, Nt = (e) => e == null || typeof e == "string" ? e : typeof e == "number" || typeof e == "bigint" ? String(e) : e, Pt = (e) => {
|
|
141
|
+
if (e == null || e instanceof Date) return e;
|
|
142
|
+
if (typeof e == "string") {
|
|
143
|
+
let t = new Date(e);
|
|
144
|
+
return isNaN(t.getTime()) ? e : t;
|
|
145
|
+
}
|
|
146
|
+
if (typeof e == "number") {
|
|
147
|
+
let t = new Date(e);
|
|
148
|
+
return isNaN(t.getTime()) ? e : t;
|
|
149
|
+
}
|
|
150
|
+
return e;
|
|
151
|
+
}, Ft = (e, t, n) => {
|
|
152
|
+
if (e == null) return e;
|
|
153
|
+
switch (t) {
|
|
154
|
+
case "boolean": return n === "postgres" ? e : jt(e);
|
|
155
|
+
case "json":
|
|
156
|
+
case "array": return n === "postgres" ? e : Mt(e);
|
|
157
|
+
case "timestamp":
|
|
158
|
+
case "date": return n === "sqlite" || n === "turso" ? Pt(e) : e;
|
|
159
|
+
case "decimal":
|
|
160
|
+
case "bigint": return Nt(e);
|
|
161
|
+
default: return e;
|
|
162
|
+
}
|
|
163
|
+
}, It = (e, t, n) => {
|
|
164
|
+
if (e.length === 0) return e;
|
|
165
|
+
let r = y(t);
|
|
166
|
+
if (r === void 0) return e;
|
|
167
|
+
let i = r.fields, a = [];
|
|
168
|
+
for (let [e, t] of Object.entries(i)) a.push({
|
|
169
|
+
key: e,
|
|
170
|
+
type: t.type
|
|
171
|
+
});
|
|
172
|
+
return e.map((e) => {
|
|
173
|
+
let t = { ...e };
|
|
174
|
+
for (let { key: e, type: r } of a) e in t && (t[e] = Ft(t[e], r, n));
|
|
175
|
+
return t;
|
|
176
|
+
});
|
|
177
|
+
}, Lt = (e, t) => {
|
|
178
|
+
if (e == null) return e;
|
|
179
|
+
switch (t) {
|
|
180
|
+
case "json":
|
|
181
|
+
case "array": return typeof e == "string" ? e : JSON.stringify(e);
|
|
182
|
+
default: return e;
|
|
183
|
+
}
|
|
184
|
+
}, Rt = (e, t, n = ["json", "array"]) => {
|
|
185
|
+
let r = y(t);
|
|
186
|
+
if (r === void 0) return e;
|
|
187
|
+
let i = r.fields, a = null;
|
|
188
|
+
for (let [t, r] of Object.entries(i)) if (n.includes(r.type) && t in e) {
|
|
189
|
+
let n = Lt(e[t], r.type);
|
|
190
|
+
n !== e[t] && (a === null && (a = { ...e }), a[t] = n);
|
|
191
|
+
}
|
|
192
|
+
return a ?? e;
|
|
193
|
+
}, zt = async (e, t, n, r) => {
|
|
194
|
+
if (e.length === 0) return e;
|
|
195
|
+
let i = M(n.tableName);
|
|
196
|
+
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?`);
|
|
197
|
+
let a = e.map((e) => ({ ...e }));
|
|
198
|
+
for (let [e, o] of Object.entries(t)) {
|
|
199
|
+
let t = i[e];
|
|
200
|
+
if (t === void 0) {
|
|
201
|
+
let t = Object.keys(i).join(", ") || "<none>";
|
|
202
|
+
throw Error(`unknown relation '${e}' on '${n.tableName}'. Registered relations: ${t}.`);
|
|
203
|
+
}
|
|
204
|
+
await Bt(a, e, t, o === !0 ? {} : o, n, r);
|
|
205
|
+
}
|
|
206
|
+
return a;
|
|
207
|
+
}, Bt = async (e, t, n, r, i, a) => {
|
|
208
|
+
switch (n.kind) {
|
|
209
|
+
case "one":
|
|
210
|
+
await Vt(e, t, n, r, i, a);
|
|
211
|
+
return;
|
|
212
|
+
case "many":
|
|
213
|
+
await Ht(e, t, n, r, i, a);
|
|
214
|
+
return;
|
|
215
|
+
case "manyToMany":
|
|
216
|
+
await Wt(e, t, n, r, i, a);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
}, Vt = async (e, t, n, r, i, a) => {
|
|
220
|
+
let o = n.target(), s = n.sourceKey ?? Gt(i, o.tableName);
|
|
221
|
+
if (s !== void 0) {
|
|
222
|
+
let n = N(e, s), i = n.length === 0 ? [] : await P(o, "id", n, r, a), c = new Map(i.map((e) => [e.id, e]));
|
|
223
|
+
for (let n of e) {
|
|
224
|
+
let e = n[s];
|
|
225
|
+
n[t] = e == null ? null : c.get(e) ?? null;
|
|
226
|
+
}
|
|
227
|
+
await F(i, r.with, o, a);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
let c = n.foreignKey ?? A(o, i.tableName, t), l = n.sourceKey ?? "id", u = N(e, l), d = u.length === 0 ? [] : await P(o, c, u, r, a), f = /* @__PURE__ */ new Map();
|
|
231
|
+
for (let e of d) f.set(e[c], e);
|
|
232
|
+
for (let n of e) {
|
|
233
|
+
let e = n[l];
|
|
234
|
+
n[t] = e == null ? null : f.get(e) ?? null;
|
|
235
|
+
}
|
|
236
|
+
await F(d, r.with, o, a);
|
|
237
|
+
}, Ht = async (e, t, n, r, i, a) => {
|
|
238
|
+
let o = n.target(), s = n.foreignKey ?? A(o, i.tableName, t), c = n.sourceKey ?? "id", l = N(e, c), u = Ut(r), d = l.length === 0 ? [] : await P(o, s, l, u, a), f = /* @__PURE__ */ new Map();
|
|
239
|
+
for (let e of d) {
|
|
240
|
+
let t = e[s], n = f.get(t);
|
|
241
|
+
n ? n.push(e) : f.set(t, [e]);
|
|
242
|
+
}
|
|
243
|
+
if (r.limit !== void 0 || r.offset !== void 0) {
|
|
244
|
+
let e = r.offset ?? 0, t = r.limit;
|
|
245
|
+
for (let [n, r] of f) {
|
|
246
|
+
let i = t === void 0 ? r.slice(e) : r.slice(e, e + t);
|
|
247
|
+
f.set(n, i);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
for (let n of e) {
|
|
251
|
+
let e = n[c];
|
|
252
|
+
n[t] = f.get(e) ?? [];
|
|
253
|
+
}
|
|
254
|
+
await F(d, r.with, o, a);
|
|
255
|
+
}, Ut = (e) => {
|
|
256
|
+
let { limit: t, offset: n, ...r } = e;
|
|
257
|
+
return r;
|
|
258
|
+
}, Wt = async (e, t, n, r, i, a) => {
|
|
259
|
+
let o = n.target(), s = n.through(), c = N(e, "id");
|
|
260
|
+
if (c.length === 0) {
|
|
261
|
+
for (let n of e) n[t] = [];
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
let l = {
|
|
265
|
+
column: n.sourceKey,
|
|
266
|
+
op: "in",
|
|
267
|
+
value: c
|
|
268
|
+
}, u = r.onJunction === void 0 ? l : { and: [l, r.onJunction] }, d = await a({
|
|
269
|
+
table: s.tableName,
|
|
270
|
+
predicate: u,
|
|
271
|
+
order: [],
|
|
272
|
+
take: void 0,
|
|
273
|
+
skip: void 0,
|
|
274
|
+
projection: void 0
|
|
275
|
+
});
|
|
276
|
+
if (d.length === 0) {
|
|
277
|
+
for (let n of e) n[t] = [];
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
let f = await P(o, "id", N(d, n.targetKey), r, a), p = new Map(f.map((e) => [e.id, e])), m = /* @__PURE__ */ new Map();
|
|
281
|
+
for (let e of d) {
|
|
282
|
+
let t = e[n.sourceKey], r = e[n.targetKey], i = p.get(r);
|
|
283
|
+
if (i === void 0) continue;
|
|
284
|
+
let a = m.get(t);
|
|
285
|
+
a ? a.push(i) : m.set(t, [i]);
|
|
286
|
+
}
|
|
287
|
+
for (let n of e) n[t] = m.get(n.id) ?? [];
|
|
288
|
+
await F(f, r.with, o, a);
|
|
289
|
+
}, Gt = (e, t) => {
|
|
290
|
+
for (let [n, r] of Object.entries(e.fields)) if (r.type === "reference" && r.references?.()?.tableName === t) return n;
|
|
291
|
+
}, N = (e, t) => {
|
|
292
|
+
let n = /* @__PURE__ */ new Set();
|
|
293
|
+
for (let r of e) {
|
|
294
|
+
let e = r[t];
|
|
295
|
+
e != null && n.add(e);
|
|
296
|
+
}
|
|
297
|
+
return [...n];
|
|
298
|
+
}, P = (e, t, n, r, i) => {
|
|
299
|
+
let a = {
|
|
300
|
+
column: t,
|
|
301
|
+
op: "in",
|
|
302
|
+
value: n
|
|
303
|
+
}, o = r.where === void 0 ? a : { and: [r.where, a] };
|
|
304
|
+
return i({
|
|
305
|
+
table: e.tableName,
|
|
306
|
+
predicate: o,
|
|
307
|
+
order: r.orderBy ?? [],
|
|
308
|
+
take: r.limit,
|
|
309
|
+
skip: r.offset,
|
|
310
|
+
projection: void 0
|
|
311
|
+
});
|
|
312
|
+
}, F = async (e, t, n, r) => {
|
|
313
|
+
t !== void 0 && e.length !== 0 && (await zt(e, t, n, r)).forEach((t, n) => {
|
|
314
|
+
let r = e[n];
|
|
315
|
+
for (let [e, n] of Object.entries(t)) e in r || (r[e] = n);
|
|
316
|
+
});
|
|
317
|
+
}, Kt = (e) => e.eager !== void 0 && Object.keys(e.eager).length > 0, qt = class extends Error {
|
|
318
|
+
_tag = "TenantNamespaceUnresolved";
|
|
319
|
+
constructor(e) {
|
|
320
|
+
super(e), this.name = "TenantNamespaceUnresolved";
|
|
321
|
+
}
|
|
322
|
+
}, Jt = class extends Error {
|
|
323
|
+
_tag = "TenantNamespaceInvalid";
|
|
324
|
+
constructor(e) {
|
|
325
|
+
super(e), this.name = "TenantNamespaceInvalid";
|
|
326
|
+
}
|
|
327
|
+
}, Yt = 63, Xt = "tenant_", Zt = (e) => e.toLowerCase().replace(/[^a-z0-9_]/g, "_"), Qt = (e) => {
|
|
328
|
+
if (e == null || e === "") throw new qt("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.");
|
|
329
|
+
let t = Zt(e);
|
|
330
|
+
if (t.replace(/_/g, "") === "") throw new Jt(`tenant id '${e}' has no usable identifier characters after sanitisation — cannot derive a distinct namespace.`);
|
|
331
|
+
let n = `${Xt}${t}`;
|
|
332
|
+
if (n.length > Yt) throw new Jt(`namespace '${n}' for tenant '${e}' exceeds the ${Yt}-byte identifier limit. Use a shorter tenant id.`);
|
|
333
|
+
return n;
|
|
334
|
+
}, I = (e, t) => e === null ? t : `${e}.${t}`, $t = (e, t) => {
|
|
335
|
+
let { strings: n, values: r } = e, i = t.unsafe(n[0] ?? "");
|
|
336
|
+
for (let e = 0; e < r.length; e++) i = t`${i}${r[e]}${t.unsafe(n[e + 1] ?? "")}`;
|
|
337
|
+
return t`${i}`;
|
|
338
|
+
}, L = (e) => e.replace(/[\\%_]/g, (e) => `\\${e}`), R = (e, t, n = null) => {
|
|
339
|
+
if ("not" in e) return t`NOT (${R(e.not, t, n)})`;
|
|
340
|
+
if ("and" in e) return e.and.length === 0 ? t`TRUE` : t.and(e.and.map((e) => R(e, t, n)));
|
|
341
|
+
if ("or" in e) return e.or.length === 0 ? t`FALSE` : t.or(e.or.map((e) => R(e, t, n)));
|
|
342
|
+
if (e.op === "subquery-in" || e.op === "subquery-not-in") {
|
|
343
|
+
let r = e, i = z(r.subquery, t, n);
|
|
344
|
+
return r.op === "subquery-in" ? t`${t(r.column)} IN (${i})` : t`${t(r.column)} NOT IN (${i})`;
|
|
345
|
+
}
|
|
346
|
+
if (e.op === "exists" || e.op === "not-exists") {
|
|
347
|
+
let r = e, i = z(r.subquery, t, n);
|
|
348
|
+
return r.op === "exists" ? t`EXISTS (${i})` : t`NOT EXISTS (${i})`;
|
|
349
|
+
}
|
|
350
|
+
let r = e, i = t(r.column), a = r.path, o = a !== void 0 && a.length > 0, s = () => t.onDialectOrElse({
|
|
351
|
+
mysql: () => t.unsafe(w(r.column, a, "mysql")),
|
|
352
|
+
mssql: () => t.unsafe(w(r.column, a, "mssql")),
|
|
353
|
+
sqlite: () => t.unsafe(w(r.column, a, "sqlite")),
|
|
354
|
+
orElse: () => t.unsafe(w(r.column, a, "postgres"))
|
|
355
|
+
}), c = () => t.onDialectOrElse({
|
|
356
|
+
mysql: () => t.unsafe(w(r.column, a, "mysql", { numeric: !0 })),
|
|
357
|
+
mssql: () => t.unsafe(w(r.column, a, "mssql", { numeric: !0 })),
|
|
358
|
+
sqlite: () => t.unsafe(w(r.column, a, "sqlite", { numeric: !0 })),
|
|
359
|
+
orElse: () => t.unsafe(w(r.column, a, "postgres", { numeric: !0 }))
|
|
360
|
+
}), l = o ? s() : i, u = o ? c() : i, d = (e) => o && e != null ? String(e) : e, f = (e) => o ? Number(e) : e;
|
|
361
|
+
switch (r.op) {
|
|
362
|
+
case "eq": return t`${l} = ${d(r.value)}`;
|
|
363
|
+
case "neq": return t`${l} <> ${d(r.value)}`;
|
|
364
|
+
case "gt": return t`${u} > ${f(r.value)}`;
|
|
365
|
+
case "gte": return t`${u} >= ${f(r.value)}`;
|
|
366
|
+
case "lt": return t`${u} < ${f(r.value)}`;
|
|
367
|
+
case "lte": return t`${u} <= ${f(r.value)}`;
|
|
368
|
+
case "in": {
|
|
369
|
+
let e = r.value ?? [];
|
|
370
|
+
return e.length === 0 ? t`FALSE` : t`${l} IN ${t.in(o ? e.map((e) => d(e)) : e)}`;
|
|
371
|
+
}
|
|
372
|
+
case "notIn": {
|
|
373
|
+
let e = r.value ?? [];
|
|
374
|
+
return e.length === 0 ? t`TRUE` : t`${l} NOT IN ${t.in(o ? e.map((e) => d(e)) : e)}`;
|
|
375
|
+
}
|
|
376
|
+
case "contains": {
|
|
377
|
+
let e = `%${L(typeof r.value == "string" ? r.value : String(r.value ?? ""))}%`;
|
|
378
|
+
return t.onDialectOrElse({
|
|
379
|
+
mysql: () => t`LOWER(${l}) LIKE LOWER(${e})`,
|
|
380
|
+
mssql: () => t`LOWER(${l}) LIKE LOWER(${e})`,
|
|
381
|
+
sqlite: () => t`LOWER(${l}) LIKE LOWER(${e})`,
|
|
382
|
+
orElse: () => t`${l} ILIKE ${e} ESCAPE '\\'`
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
case "arrayContains": {
|
|
386
|
+
let e = r.value ?? [];
|
|
387
|
+
return e.length === 0 ? t`TRUE` : t.onDialectOrElse({
|
|
388
|
+
mysql: () => t`JSON_CONTAINS(${l}, ${JSON.stringify(e)})`,
|
|
389
|
+
mssql: () => t`(SELECT COUNT(DISTINCT value) FROM OPENJSON(${l}) WHERE value IN ${t.in(e)}) = ${e.length}`,
|
|
390
|
+
sqlite: () => t`(SELECT COUNT(DISTINCT value) FROM json_each(${l}) WHERE value IN ${t.in(e)}) = ${e.length}`,
|
|
391
|
+
orElse: () => t`${l} @> ${e}`
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
case "arrayOverlaps": {
|
|
395
|
+
let e = r.value ?? [];
|
|
396
|
+
return e.length === 0 ? t`FALSE` : t.onDialectOrElse({
|
|
397
|
+
mysql: () => t.or(e.map((e) => t`JSON_CONTAINS(${l}, ${JSON.stringify(e)})`)),
|
|
398
|
+
mssql: () => t`EXISTS (SELECT 1 FROM OPENJSON(${l}) WHERE value IN ${t.in(e)})`,
|
|
399
|
+
sqlite: () => t`EXISTS (SELECT 1 FROM json_each(${l}) WHERE value IN ${t.in(e)})`,
|
|
400
|
+
orElse: () => t`${l} && ${e}`
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
case "arrayHas": return t.onDialectOrElse({
|
|
404
|
+
mysql: () => t`JSON_CONTAINS(${l}, ${JSON.stringify(r.value)})`,
|
|
405
|
+
mssql: () => t`${r.value} IN (SELECT value FROM OPENJSON(${l}))`,
|
|
406
|
+
sqlite: () => t`EXISTS (SELECT 1 FROM json_each(${l}) WHERE value = ${r.value})`,
|
|
407
|
+
orElse: () => t`${r.value} = ANY(${l})`
|
|
408
|
+
});
|
|
409
|
+
case "isNull": return t`${l} IS NULL`;
|
|
410
|
+
case "isNotNull": return t`${l} IS NOT NULL`;
|
|
411
|
+
case "spatial": {
|
|
412
|
+
let e = r.value, n = t.unsafe("::geometry"), a = e.bufferMeters === void 0 ? t`${e.geom}${n}` : t`ST_Buffer(${e.geom}::geography, ${e.bufferMeters})::geometry`;
|
|
413
|
+
return t.onDialectOrElse({
|
|
414
|
+
mysql: () => {
|
|
415
|
+
throw Error("@voltro/plugin-postgis: spatial predicates are postgres-only");
|
|
416
|
+
},
|
|
417
|
+
mssql: () => {
|
|
418
|
+
throw Error("@voltro/plugin-postgis: spatial predicates are postgres-only");
|
|
419
|
+
},
|
|
420
|
+
sqlite: () => {
|
|
421
|
+
throw Error("@voltro/plugin-postgis: spatial predicates are postgres-only");
|
|
422
|
+
},
|
|
423
|
+
orElse: () => e.fn === "ST_DWithin" ? t`ST_DWithin(${i}::geography, ${e.geom}::geography, ${e.distanceMeters})` : e.fn === "&&" ? t`${i}${n} && ${a}` : t`${t.unsafe(e.fn)}(${i}${n}, ${a})`
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}, en = (e, t) => {
|
|
428
|
+
let n = [];
|
|
429
|
+
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}`);
|
|
430
|
+
}, tn = (e, t) => t.csv(e.map((e) => {
|
|
431
|
+
let n = e.column === void 0 ? t.unsafe("*") : t`${t(e.column)}`;
|
|
432
|
+
if (e.op.startsWith("window-")) {
|
|
433
|
+
let r = en(e.window ?? {}, t);
|
|
434
|
+
switch (e.op) {
|
|
435
|
+
case "window-row-number": return t`ROW_NUMBER() OVER (${r}) AS ${t(e.alias)}`;
|
|
436
|
+
case "window-rank": return t`RANK() OVER (${r}) AS ${t(e.alias)}`;
|
|
437
|
+
case "window-dense-rank": return t`DENSE_RANK() OVER (${r}) AS ${t(e.alias)}`;
|
|
438
|
+
case "window-lag": return t`LAG(${n}, ${t.unsafe(String(e.offset ?? 1))}) OVER (${r}) AS ${t(e.alias)}`;
|
|
439
|
+
case "window-lead": return t`LEAD(${n}, ${t.unsafe(String(e.offset ?? 1))}) OVER (${r}) AS ${t(e.alias)}`;
|
|
440
|
+
case "window-sum-over": return t`SUM(${n}) OVER (${r}) AS ${t(e.alias)}`;
|
|
441
|
+
case "window-avg-over": return t`AVG(${n}) OVER (${r}) AS ${t(e.alias)}`;
|
|
442
|
+
default: throw Error(`compileAggregateProjection: unknown window op '${e.op}'`);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
switch (e.op) {
|
|
446
|
+
case "count": return t`COUNT(${n}) AS ${t(e.alias)}`;
|
|
447
|
+
case "count-distinct": return t`COUNT(DISTINCT ${n}) AS ${t(e.alias)}`;
|
|
448
|
+
case "sum": return t`SUM(${n}) AS ${t(e.alias)}`;
|
|
449
|
+
case "avg": return t`AVG(${n}) AS ${t(e.alias)}`;
|
|
450
|
+
case "min": return t`MIN(${n}) AS ${t(e.alias)}`;
|
|
451
|
+
case "max": return t`MAX(${n}) AS ${t(e.alias)}`;
|
|
452
|
+
case "column":
|
|
453
|
+
if (e.column === void 0) throw Error(`compileAggregateProjection: 'column' projection requires a column name (alias '${e.alias}')`);
|
|
454
|
+
return t`${t(e.column)} AS ${t(e.alias)}`;
|
|
455
|
+
default: throw Error(`compileAggregateProjection: unknown op '${e.op}'`);
|
|
456
|
+
}
|
|
457
|
+
})), nn = (e) => `[${e.join(",")}]`, rn = (e, t) => {
|
|
458
|
+
if (e.queryVector === void 0) return null;
|
|
459
|
+
let n = t(e.column), r = nn(e.queryVector), i = e.distance === "cosine" ? "<=>" : e.distance === "l2" ? "<->" : "<#>", a = e.distance === "cosine" ? "VEC_DISTANCE_COSINE" : "VEC_DISTANCE_EUCLIDEAN";
|
|
460
|
+
return t.onDialectOrElse({
|
|
461
|
+
mysql: () => t`${t.unsafe(a)}(${n}, ${r})`,
|
|
462
|
+
mssql: () => t`NULL`,
|
|
463
|
+
sqlite: () => t`NULL`,
|
|
464
|
+
orElse: () => t`${n} ${t.unsafe(i)} ${r}::vector`
|
|
465
|
+
});
|
|
466
|
+
}, an = (e, t) => {
|
|
467
|
+
let n = t.unsafe(e.useGeography ? "::geography" : "::geometry");
|
|
468
|
+
return t`ST_Distance(${t(e.column)}${n}, ${e.geom}${n})`;
|
|
469
|
+
}, on = (e, t) => t`${t(e.column)} <-> ${e.geom}::geometry`, sn = (e, t) => t`${t(`${e.indexName}_tsv`)}`, cn = (e, t) => `${t}_${e.indexName}_fts`, ln = (e, t, n) => {
|
|
470
|
+
let r = n.csv(e.columns.map((e) => n`${n(e)}`)), i = `%${L(e.query)}%`;
|
|
471
|
+
return n.onDialectOrElse({
|
|
472
|
+
mysql: () => n`MATCH(${r}) AGAINST(${e.query} IN NATURAL LANGUAGE MODE)`,
|
|
473
|
+
sqlite: () => {
|
|
474
|
+
let r = n(cn(e, t));
|
|
475
|
+
return n`${n("id")} IN (SELECT ${n("row_id")} FROM ${r} WHERE ${r} MATCH ${e.query})`;
|
|
476
|
+
},
|
|
477
|
+
mssql: () => n.or(e.columns.map((e) => n`LOWER(${n(e)}) LIKE LOWER(${i}) ESCAPE '\\'`)),
|
|
478
|
+
orElse: () => n`${sn(e, n)} @@ plainto_tsquery(${e.config ?? "english"}, ${e.query})`
|
|
479
|
+
});
|
|
480
|
+
}, un = (e, t, n) => {
|
|
481
|
+
let r = `%${L(e.query)}%`;
|
|
482
|
+
return n.onDialectOrElse({
|
|
483
|
+
mysql: () => n`MATCH(${n.csv(e.columns.map((e) => n`${n(e)}`))}) AGAINST(${e.query} IN NATURAL LANGUAGE MODE)`,
|
|
484
|
+
sqlite: () => {
|
|
485
|
+
let r = n(cn(e, t));
|
|
486
|
+
return n`COALESCE((SELECT -bm25(${r}) FROM ${r} WHERE ${r} MATCH ${e.query} AND ${r}.${n("row_id")} = ${n("id")}), 0)`;
|
|
487
|
+
},
|
|
488
|
+
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}`),
|
|
489
|
+
orElse: () => n`ts_rank(${sn(e, n)}, plainto_tsquery(${e.config ?? "english"}, ${e.query}))`
|
|
490
|
+
});
|
|
491
|
+
}, z = (e, t, n = null) => {
|
|
492
|
+
if (e.setOp && e.setOp.queries.length >= 2) {
|
|
493
|
+
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`(${z(e, t, n)})`).reduce((e, n, i) => i === 0 ? n : t`${e} ${t.unsafe(r)} ${n}`), a = [];
|
|
494
|
+
if (e.order.length > 0 && a.push(t` ORDER BY ${t.csv(e.order.map((e) => e.direction === "desc" ? t`${t(e.column)} DESC` : t`${t(e.column)} ASC`))}`), e.take !== void 0) {
|
|
495
|
+
if (!Number.isInteger(e.take) || e.take < 0) throw Error(`compileSelect: invalid take ${e.take}`);
|
|
496
|
+
a.push(t` LIMIT ${t.unsafe(String(e.take))}`);
|
|
497
|
+
}
|
|
498
|
+
if (e.skip !== void 0) {
|
|
499
|
+
if (!Number.isInteger(e.skip) || e.skip < 0) throw Error(`compileSelect: invalid skip ${e.skip}`);
|
|
500
|
+
a.push(t` OFFSET ${t.unsafe(String(e.skip))}`);
|
|
501
|
+
}
|
|
502
|
+
return t`${i}${a.reduce((e, n) => t`${e}${n}`, t``)}`;
|
|
503
|
+
}
|
|
504
|
+
let r = e.annClause === void 0 ? null : rn(e.annClause, t);
|
|
505
|
+
e.spatialClause !== void 0 && t.onDialectOrElse({
|
|
506
|
+
mysql: () => {
|
|
507
|
+
throw Error("@voltro/plugin-postgis: spatial distance / KNN ordering is postgres-only");
|
|
508
|
+
},
|
|
509
|
+
mssql: () => {
|
|
510
|
+
throw Error("@voltro/plugin-postgis: spatial distance / KNN ordering is postgres-only");
|
|
511
|
+
},
|
|
512
|
+
sqlite: () => {
|
|
513
|
+
throw Error("@voltro/plugin-postgis: spatial distance / KNN ordering is postgres-only");
|
|
514
|
+
},
|
|
515
|
+
orElse: () => t``
|
|
516
|
+
});
|
|
517
|
+
let i = e.spatialClause?.projectAs === void 0 ? null : an(e.spatialClause, t), a = e.aggregations && e.aggregations.length > 0 ? tn(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 ? un(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({
|
|
518
|
+
orElse: () => t`DISTINCT ON (${t.csv(e.distinctOn.map((e) => t`${t(e)}`))}) `,
|
|
519
|
+
mysql: () => t`DISTINCT `,
|
|
520
|
+
mssql: () => t`DISTINCT `,
|
|
521
|
+
sqlite: () => t`DISTINCT `
|
|
522
|
+
}) : e.distinct ? t`DISTINCT ` : t``, p = e.alias === void 0 ? t`${t(I(n, e.table))}` : t`${t(I(n, e.table))} AS ${t(e.alias)}`, m = (e.joins ?? []).map((e) => {
|
|
523
|
+
let r = e.kind === "inner" ? "INNER" : "LEFT";
|
|
524
|
+
return t` ${t.unsafe(r)} JOIN ${t(I(n, e.table))} AS ${t(e.alias)} ON ${R(e.on, t, n)}`;
|
|
525
|
+
}), h = m.length > 0 ? m.reduce((e, n) => t`${e}${n}`, p) : p, g = c === void 0 ? null : ln(c, e.table, t), _ = [e.predicate ? R(e.predicate, t, n) : null, g].filter((e) => e !== null), ee = _.length > 0 ? t` WHERE ${t.and(_)}` : t``, v = e.groupBy && e.groupBy.length > 0 ? t` GROUP BY ${t.csv(e.groupBy.map((e) => t`${t(e)}`))}` : t``, y = e.having ? t` HAVING ${R(e.having, t, n)}` : t``, b = e.skip !== void 0, x = [];
|
|
526
|
+
r !== null && x.push(t`${r} ASC`), e.spatialClause?.order !== void 0 && x.push(e.spatialClause.order === "desc" ? t`${on(e.spatialClause, t)} DESC` : t`${on(e.spatialClause, t)} ASC`), l !== null && c?.rank?.order === !0 && x.push(t`${l} DESC`);
|
|
527
|
+
for (let n of e.order) x.push(n.direction === "desc" ? t`${t(n.column)} DESC` : t`${t(n.column)} ASC`);
|
|
528
|
+
let te = x.length > 0 ? t` ORDER BY ${t.csv(x)}` : b ? t.onDialectOrElse({
|
|
529
|
+
mssql: () => t` ORDER BY (SELECT NULL)`,
|
|
530
|
+
orElse: () => t``
|
|
531
|
+
}) : t``, S = (e) => {
|
|
532
|
+
if (!Number.isFinite(e) || !Number.isInteger(e) || e < 0) throw Error(`compileSelect: expected non-negative integer take/skip, got ${e}`);
|
|
533
|
+
return t.unsafe(String(e));
|
|
534
|
+
}, C = (e.ctes ?? []).map((e) => t`${t(e.name)} AS (${z(e.descriptor, t, n)})`), ne = (e.ctes ?? []).some((e) => e.recursive === !0), re = C.length > 0 ? t`WITH ${t.unsafe(ne ? "RECURSIVE " : "")}${t.csv(C)} ` : t``;
|
|
535
|
+
return t.onDialectOrElse({
|
|
536
|
+
mssql: () => {
|
|
537
|
+
let n = e.take !== void 0 && !b ? t`TOP ${S(e.take)} ` : t``, r = b ? t` OFFSET ${S(e.skip)} ROWS FETCH NEXT ${S(e.take ?? 2 ** 53 - 1)} ROWS ONLY` : t``;
|
|
538
|
+
return t`${re}SELECT ${n}${f}${d} FROM ${h}${ee}${v}${y}${te}${r}`;
|
|
539
|
+
},
|
|
540
|
+
orElse: () => {
|
|
541
|
+
let n = e.take === void 0 ? t`` : t` LIMIT ${S(e.take)}`, r = b ? t` OFFSET ${S(e.skip)}` : t``;
|
|
542
|
+
return t`${re}SELECT ${f}${d} FROM ${h}${ee}${v}${y}${te}${n}${r}`;
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
}, dn = (e, t, n) => {
|
|
546
|
+
if (e.eager === void 0) return null;
|
|
547
|
+
let r = fn(e.table, e.eager, e.sourceTable);
|
|
548
|
+
if (r === null) return null;
|
|
549
|
+
let i = gn(n, e, r, hn(t, n));
|
|
550
|
+
return i === null ? null : {
|
|
551
|
+
fragment: i,
|
|
552
|
+
decode: _n(r)
|
|
553
|
+
};
|
|
554
|
+
}, fn = (e, t, n) => {
|
|
555
|
+
let r = n ?? f(e), i = M(e);
|
|
556
|
+
if (i === void 0) return null;
|
|
557
|
+
let a = [];
|
|
558
|
+
for (let [e, n] of Object.entries(t)) {
|
|
559
|
+
let t = i[e];
|
|
560
|
+
if (t === void 0) return null;
|
|
561
|
+
let o = pn(e, t, r, n === !0 ? {} : n);
|
|
562
|
+
if (o === null) return null;
|
|
563
|
+
a.push(o);
|
|
564
|
+
}
|
|
565
|
+
return {
|
|
566
|
+
table: r,
|
|
567
|
+
children: a
|
|
568
|
+
};
|
|
569
|
+
}, pn = (e, t, n, r) => {
|
|
570
|
+
let i = t.target(), a = (t.kind === "manyToMany" ? void 0 : t.sourceKey) ?? "id", o, s;
|
|
571
|
+
if (t.kind === "manyToMany") o = t.targetKey, s = "target";
|
|
572
|
+
else if (t.kind === "one") {
|
|
573
|
+
let r = mn(n, i.tableName);
|
|
574
|
+
if (r !== void 0) o = r, s = "source";
|
|
575
|
+
else try {
|
|
576
|
+
o = t.foreignKey ?? A(i, n.tableName, e), s = "target";
|
|
577
|
+
} catch {
|
|
578
|
+
return null;
|
|
579
|
+
}
|
|
580
|
+
} else try {
|
|
581
|
+
o = t.foreignKey ?? A(i, n.tableName, e), s = "target";
|
|
582
|
+
} catch {
|
|
583
|
+
return null;
|
|
584
|
+
}
|
|
585
|
+
let c = r.with === void 0 ? [] : (() => {
|
|
586
|
+
let e = M(i.tableName);
|
|
587
|
+
if (e === void 0 && Object.keys(r.with).length > 0) return null;
|
|
588
|
+
let t = [];
|
|
589
|
+
for (let [n, a] of Object.entries(r.with)) {
|
|
590
|
+
let r = e?.[n];
|
|
591
|
+
if (r === void 0) return null;
|
|
592
|
+
let o = pn(n, r, i, a === !0 ? {} : a);
|
|
593
|
+
if (o === null) return null;
|
|
594
|
+
t.push(o);
|
|
595
|
+
}
|
|
596
|
+
return t;
|
|
597
|
+
})();
|
|
598
|
+
return c === null ? null : {
|
|
599
|
+
relationName: e,
|
|
600
|
+
relation: t,
|
|
601
|
+
source: n,
|
|
602
|
+
target: i,
|
|
603
|
+
foreignKey: o,
|
|
604
|
+
sourceKey: a,
|
|
605
|
+
fkSide: s,
|
|
606
|
+
branch: r,
|
|
607
|
+
children: c
|
|
608
|
+
};
|
|
609
|
+
}, mn = (e, t) => {
|
|
610
|
+
for (let [n, r] of Object.entries(e.fields)) if (r.type === "reference" && r.references?.()?.tableName === t) return n;
|
|
611
|
+
}, hn = (e, t) => {
|
|
612
|
+
let n = 0;
|
|
613
|
+
return {
|
|
614
|
+
sql: e,
|
|
615
|
+
dialect: t,
|
|
616
|
+
nextAlias: () => `__a${n++}`
|
|
617
|
+
};
|
|
618
|
+
}, gn = (e, t, n, r) => {
|
|
619
|
+
switch (e) {
|
|
620
|
+
case "postgres": return yn(t, n, r);
|
|
621
|
+
case "sqlite":
|
|
622
|
+
case "turso": return Tn(t, n, r);
|
|
623
|
+
case "mysql":
|
|
624
|
+
case "mariadb": return jn(t, n, r);
|
|
625
|
+
case "mssql": return Rn(t, n, r);
|
|
626
|
+
}
|
|
627
|
+
}, _n = (e) => {
|
|
628
|
+
let t = vn(e.table, e.children);
|
|
629
|
+
return (e) => e.map((e) => {
|
|
630
|
+
let n = e.__row, r = typeof n == "string" ? JSON.parse(n) : n;
|
|
631
|
+
return t(r);
|
|
632
|
+
});
|
|
633
|
+
}, vn = (e, t) => {
|
|
634
|
+
let n = [];
|
|
635
|
+
for (let [t, r] of Object.entries(e.fields)) (r.type === "timestamp" || r.type === "date") && n.push(t);
|
|
636
|
+
let r = t.map((e) => {
|
|
637
|
+
let t = vn(e.target, e.children), n = e.relation.kind === "one";
|
|
638
|
+
return {
|
|
639
|
+
name: e.relationName,
|
|
640
|
+
decode: t,
|
|
641
|
+
isSingle: n
|
|
642
|
+
};
|
|
643
|
+
});
|
|
644
|
+
return (e) => {
|
|
645
|
+
let t = { ...e };
|
|
646
|
+
for (let e of n) {
|
|
647
|
+
let n = t[e];
|
|
648
|
+
typeof n == "string" && (t[e] = new Date(n));
|
|
649
|
+
}
|
|
650
|
+
for (let e of r) {
|
|
651
|
+
let n = t[e.name];
|
|
652
|
+
if (n == null) {
|
|
653
|
+
t[e.name] = e.isSingle ? null : [];
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
e.isSingle ? t[e.name] = e.decode(n) : t[e.name] = n.map((t) => e.decode(t));
|
|
657
|
+
}
|
|
658
|
+
return t;
|
|
659
|
+
};
|
|
660
|
+
}, yn = (e, t, n) => {
|
|
661
|
+
let r = n.sql, i = n.nextAlias(), a = r(t.table.tableName), o = r(i), s = B(i, t.children, n);
|
|
662
|
+
return s === null ? null : r`SELECT ${s} AS "__row" FROM ${a} AS ${o}${e.predicate ? r` WHERE ${K(e.predicate, i, r)}` : r``}${e.order.length > 0 ? r` ORDER BY ${r.csv(e.order.map((e) => e.direction === "desc" ? r`${U(i, e.column, r)} DESC` : r`${U(i, e.column, r)} ASC`))}` : r``}${e.take === void 0 ? r`` : r` LIMIT ${G(e.take, r)}`}${e.skip === void 0 ? r`` : r` OFFSET ${G(e.skip, r)}`}`;
|
|
663
|
+
}, B = (e, t, n) => {
|
|
664
|
+
let r = n.sql, i = r`to_jsonb(${r(e)}.*)`;
|
|
665
|
+
if (t.length === 0) return i;
|
|
666
|
+
let a = [];
|
|
667
|
+
for (let i of t) {
|
|
668
|
+
let t = bn(i, e, n);
|
|
669
|
+
if (t === null) return null;
|
|
670
|
+
a.push(r`${W(i.relationName, r)}, ${t}`);
|
|
671
|
+
}
|
|
672
|
+
return r`${i} || jsonb_build_object(${r.csv(a)})`;
|
|
673
|
+
}, bn = (e, t, n) => {
|
|
674
|
+
switch (n.sql, e.relation.kind) {
|
|
675
|
+
case "one": return xn(e, t, n);
|
|
676
|
+
case "many": return Sn(e, t, n);
|
|
677
|
+
case "manyToMany": return Cn(e, t, n);
|
|
678
|
+
}
|
|
679
|
+
}, xn = (e, t, n) => {
|
|
680
|
+
let r = n.sql, i = n.nextAlias(), a = B(i, e.children, n);
|
|
681
|
+
if (a === null) return null;
|
|
682
|
+
let o = e.fkSide === "source" ? r`${U(i, "id", r)} = ${U(t, e.foreignKey, r)}` : r`${U(i, e.foreignKey, r)} = ${U(t, e.sourceKey, r)}`, s = e.branch.where === void 0 ? r`` : r` AND ${K(e.branch.where, i, r)}`, c = V(i, e.branch.orderBy, r);
|
|
683
|
+
return r`(SELECT ${a} FROM ${r(e.target.tableName)} AS ${r(i)} WHERE ${o}${s}${c} LIMIT 1)`;
|
|
684
|
+
}, Sn = (e, t, n) => {
|
|
685
|
+
let r = n.sql, i = n.nextAlias(), a = B(i, e.children, n);
|
|
686
|
+
if (a === null) return null;
|
|
687
|
+
let o = r`${U(void 0, e.foreignKey, r)} = ${U(t, e.sourceKey, r)}`, s = e.branch.where === void 0 ? r`` : r` AND ${R(e.branch.where, r)}`, c = wn(e.branch.orderBy, r), l = e.branch.limit === void 0 ? r`` : r` LIMIT ${G(e.branch.limit, r)}`, u = e.branch.offset === void 0 ? r`` : r` OFFSET ${G(e.branch.offset, r)}`;
|
|
688
|
+
return r`COALESCE((SELECT jsonb_agg(${a}) FROM (SELECT * FROM ${r(e.target.tableName)} WHERE ${o}${s}${c}${l}${u}) AS ${r(i)}), '[]'::jsonb)`;
|
|
689
|
+
}, Cn = (e, t, n) => {
|
|
690
|
+
let r = n.sql, i = e.relation, a = i.through(), o = n.nextAlias(), s = n.nextAlias(), c = B(o, e.children, n);
|
|
691
|
+
if (c === null) return null;
|
|
692
|
+
let l = e.branch.where === void 0 ? r`` : r` AND ${R(e.branch.where, r)}`, u = q(e, s, r), d = V(o, e.branch.orderBy, r), f = e.branch.limit === void 0 ? r`` : r` LIMIT ${G(e.branch.limit, r)}`, p = e.branch.offset === void 0 ? r`` : r` OFFSET ${G(e.branch.offset, r)}`;
|
|
693
|
+
return r`COALESCE((SELECT jsonb_agg(${c}) FROM (SELECT ${r(o)}.* FROM ${r(a.tableName)} AS ${r(s)} INNER JOIN ${r(e.target.tableName)} AS ${r(o)} ON ${U(o, "id", r)} = ${U(s, i.targetKey, r)} WHERE ${U(s, i.sourceKey, r)} = ${U(t, "id", r)}${u}${l}${d}${f}${p}) AS ${r(o)}), '[]'::jsonb)`;
|
|
694
|
+
}, V = (e, t, n) => t === void 0 || t.length === 0 ? n`` : n` ORDER BY ${n.csv(t.map((t) => t.direction === "desc" ? n`${U(e, t.column, n)} DESC` : n`${U(e, t.column, n)} ASC`))}`, wn = (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`))}`, Tn = (e, t, n) => {
|
|
695
|
+
let r = n.sql, i = n.nextAlias(), a = En(i, t.table, t.children, n);
|
|
696
|
+
if (a === null) return null;
|
|
697
|
+
let o = e.predicate ? r` WHERE ${K(e.predicate, i, r)}` : r``, s = e.order.length > 0 ? r` ORDER BY ${r.csv(e.order.map((e) => e.direction === "desc" ? r`${U(i, e.column, r)} DESC` : r`${U(i, e.column, r)} ASC`))}` : r``, c = e.take === void 0 ? r`` : r` LIMIT ${G(e.take, r)}`, l = e.skip === void 0 ? r`` : r` OFFSET ${G(e.skip, r)}`;
|
|
698
|
+
return r`SELECT ${a} AS "__row" FROM ${r(t.table.tableName)} AS ${r(i)}${o}${s}${c}${l}`;
|
|
699
|
+
}, En = (e, t, n, r) => {
|
|
700
|
+
let i = r.sql, a = [];
|
|
701
|
+
for (let n of Object.keys(t.fields)) a.push(i`${W(n, i)}, ${U(e, n, i)}`);
|
|
702
|
+
for (let t of n) {
|
|
703
|
+
let n = Dn(t, e, r);
|
|
704
|
+
if (n === null) return null;
|
|
705
|
+
a.push(i`${W(t.relationName, i)}, ${n}`);
|
|
706
|
+
}
|
|
707
|
+
return i`json_object(${i.csv(a)})`;
|
|
708
|
+
}, Dn = (e, t, n) => {
|
|
709
|
+
switch (e.relation.kind) {
|
|
710
|
+
case "one": return On(e, t, n);
|
|
711
|
+
case "many": return kn(e, t, n);
|
|
712
|
+
case "manyToMany": return An(e, t, n);
|
|
713
|
+
}
|
|
714
|
+
}, On = (e, t, n) => {
|
|
715
|
+
let r = n.sql, i = n.nextAlias(), a = En(i, e.target, e.children, n);
|
|
716
|
+
if (a === null) return null;
|
|
717
|
+
let o = e.fkSide === "source" ? r`${U(i, "id", r)} = ${U(t, e.foreignKey, r)}` : r`${U(i, e.foreignKey, r)} = ${U(t, e.sourceKey, r)}`;
|
|
718
|
+
return r`(SELECT ${a} FROM ${r(e.target.tableName)} AS ${r(i)} WHERE ${o} LIMIT 1)`;
|
|
719
|
+
}, kn = (e, t, n) => {
|
|
720
|
+
let r = n.sql, i = n.nextAlias(), a = En(i, e.target, e.children, n);
|
|
721
|
+
if (a === null) return null;
|
|
722
|
+
let o = r`${U(void 0, e.foreignKey, r)} = ${U(t, e.sourceKey, r)}`, s = e.branch.where === void 0 ? r`` : r` AND ${R(J(e.branch.where), r)}`, c = wn(e.branch.orderBy, r), l = e.branch.limit === void 0 ? r`` : r` LIMIT ${G(e.branch.limit, r)}`, u = e.branch.offset === void 0 ? r`` : r` OFFSET ${G(e.branch.offset, r)}`;
|
|
723
|
+
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('[]'))`;
|
|
724
|
+
}, An = (e, t, n) => {
|
|
725
|
+
let r = n.sql, i = e.relation, a = i.through(), o = n.nextAlias(), s = n.nextAlias(), c = En(o, e.target, e.children, n);
|
|
726
|
+
if (c === null) return null;
|
|
727
|
+
let l = e.branch.where === void 0 ? r`` : r` AND ${R(J(e.branch.where), r)}`, u = q(e, s, r, !0), d = V(o, e.branch.orderBy, r), f = e.branch.limit === void 0 ? r`` : r` LIMIT ${G(e.branch.limit, r)}`, p = e.branch.offset === void 0 ? r`` : r` OFFSET ${G(e.branch.offset, r)}`;
|
|
728
|
+
return r`COALESCE((SELECT json_group_array(${c}) FROM (SELECT ${r(o)}.* FROM ${r(a.tableName)} AS ${r(s)} INNER JOIN ${r(e.target.tableName)} AS ${r(o)} ON ${U(o, "id", r)} = ${U(s, i.targetKey, r)} WHERE ${U(s, i.sourceKey, r)} = ${U(t, "id", r)}${u}${l}${d}${f}${p}) AS ${r(o)}), json('[]'))`;
|
|
729
|
+
}, jn = (e, t, n) => {
|
|
730
|
+
let r = n.sql, i = n.nextAlias(), a = H(i, t.table, t.children, n);
|
|
731
|
+
if (a === null) return null;
|
|
732
|
+
let o = e.predicate ? r` WHERE ${K(e.predicate, i, r)}` : r``, s = e.order.length > 0 ? r` ORDER BY ${r.csv(e.order.map((e) => e.direction === "desc" ? r`${U(i, e.column, r)} DESC` : r`${U(i, e.column, r)} ASC`))}` : r``, c = e.take === void 0 ? r`` : r` LIMIT ${G(e.take, r)}`, l = e.skip === void 0 ? r`` : r` OFFSET ${G(e.skip, r)}`;
|
|
733
|
+
return r`SELECT ${a} AS \`__row\` FROM ${r(t.table.tableName)} AS ${r(i)}${o}${s}${c}${l}`;
|
|
734
|
+
}, H = (e, t, n, r) => {
|
|
735
|
+
let i = r.sql, a = [];
|
|
736
|
+
for (let n of Object.keys(t.fields)) a.push(i`${W(n, i)}, ${U(e, n, i)}`);
|
|
737
|
+
for (let t of n) {
|
|
738
|
+
let n = Mn(t, e, r);
|
|
739
|
+
if (n === null) return null;
|
|
740
|
+
a.push(i`${W(t.relationName, i)}, ${n}`);
|
|
741
|
+
}
|
|
742
|
+
return i`JSON_OBJECT(${i.csv(a)})`;
|
|
743
|
+
}, Mn = (e, t, n) => {
|
|
744
|
+
switch (e.relation.kind) {
|
|
745
|
+
case "one": return Nn(e, t, n);
|
|
746
|
+
case "many": return n.dialect === "mariadb" ? In(e, t, n) : Pn(e, t, n);
|
|
747
|
+
case "manyToMany": return n.dialect === "mariadb" ? Ln(e, t, n) : Fn(e, t, n);
|
|
748
|
+
}
|
|
749
|
+
}, Nn = (e, t, n) => {
|
|
750
|
+
let r = n.sql, i = n.nextAlias(), a = H(i, e.target, e.children, n);
|
|
751
|
+
if (a === null) return null;
|
|
752
|
+
let o = e.fkSide === "source" ? r`${U(i, "id", r)} = ${U(t, e.foreignKey, r)}` : r`${U(i, e.foreignKey, r)} = ${U(t, e.sourceKey, r)}`;
|
|
753
|
+
return r`(SELECT ${a} FROM ${r(e.target.tableName)} AS ${r(i)} WHERE ${o} LIMIT 1)`;
|
|
754
|
+
}, Pn = (e, t, n) => {
|
|
755
|
+
let r = n.sql, i = n.nextAlias(), a = H(i, e.target, e.children, n);
|
|
756
|
+
if (a === null) return null;
|
|
757
|
+
let o = r`${U(void 0, e.foreignKey, r)} = ${U(t, e.sourceKey, r)}`, s = e.branch.where === void 0 ? r`` : r` AND ${R(J(e.branch.where), r)}`, c = wn(e.branch.orderBy, r), l = e.branch.limit === void 0 ? r`` : r` LIMIT ${G(e.branch.limit, r)}`, u = e.branch.offset === void 0 ? r`` : r` OFFSET ${G(e.branch.offset, r)}`;
|
|
758
|
+
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())`;
|
|
759
|
+
}, Fn = (e, t, n) => {
|
|
760
|
+
let r = n.sql, i = e.relation, a = i.through(), o = n.nextAlias(), s = n.nextAlias(), c = H(o, e.target, e.children, n);
|
|
761
|
+
if (c === null) return null;
|
|
762
|
+
let l = e.branch.where === void 0 ? r`` : r` AND ${R(J(e.branch.where), r)}`, u = q(e, s, r, !0), d = V(o, e.branch.orderBy, r), f = e.branch.limit === void 0 ? r`` : r` LIMIT ${G(e.branch.limit, r)}`, p = e.branch.offset === void 0 ? r`` : r` OFFSET ${G(e.branch.offset, r)}`;
|
|
763
|
+
return r`COALESCE((SELECT JSON_ARRAYAGG(${c}) FROM (SELECT ${r(o)}.* FROM ${r(a.tableName)} AS ${r(s)} INNER JOIN ${r(e.target.tableName)} AS ${r(o)} ON ${U(o, "id", r)} = ${U(s, i.targetKey, r)} WHERE ${U(s, i.sourceKey, r)} = ${U(t, "id", r)}${u}${l}${d}${f}${p}) AS ${r(o)}), JSON_ARRAY())`;
|
|
764
|
+
}, In = (e, t, n) => {
|
|
765
|
+
let r = n.sql, i = n.nextAlias(), a = H(i, e.target, e.children, n);
|
|
766
|
+
if (a === null) return null;
|
|
767
|
+
let o = r`${U(i, e.foreignKey, r)} = ${U(t, e.sourceKey, r)}`, s = e.branch.where === void 0 ? r`` : r` AND ${R(J(e.branch.where), r)}`, c = e.branch.orderBy ?? [];
|
|
768
|
+
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`${U(i, e.column, r)} DESC` : r`${U(i, e.column, r)} ASC`))}` : r``}) FROM ${r(e.target.tableName)} AS ${r(i)} WHERE ${o}${s}), JSON_ARRAY())`;
|
|
769
|
+
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 ${U(i, "rn", r)} > ${G(d, r)} AND ${U(i, "rn", r)} <= ${G(d + u, r)}` : u === void 0 ? r` AND ${U(i, "rn", r)} > ${G(d, r)}` : r` AND ${U(i, "rn", r)} <= ${G(u, r)}`;
|
|
770
|
+
return r`COALESCE((SELECT JSON_ARRAYAGG(${a} ORDER BY ${U(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())`;
|
|
771
|
+
}, Ln = (e, t, n) => {
|
|
772
|
+
let r = n.sql, i = e.relation, a = i.through(), o = n.nextAlias(), s = n.nextAlias(), c = H(o, e.target, e.children, n);
|
|
773
|
+
if (c === null) return null;
|
|
774
|
+
let l = r`${U(s, i.sourceKey, r)} = ${U(t, "id", r)}`, u = r`${U(o, "id", r)} = ${U(s, i.targetKey, r)}`, d = e.branch.where === void 0 ? r`` : r` AND ${R(J(e.branch.where), r)}`, f = q(e, s, r, !0), p = e.branch.orderBy ?? [];
|
|
775
|
+
if (!(e.branch.limit !== void 0 || e.branch.offset !== void 0)) return r`COALESCE((SELECT JSON_ARRAYAGG(${c}${p.length > 0 ? r` ORDER BY ${r.csv(p.map((e) => e.direction === "desc" ? r`${U(o, e.column, r)} DESC` : r`${U(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())`;
|
|
776
|
+
let m = p.length > 0 ? r` ORDER BY ${r.csv(p.map((e) => e.direction === "desc" ? r`${U(o, e.column, r)} DESC` : r`${U(o, e.column, r)} ASC`))}` : r` ORDER BY ${U(o, "id", r)}`, h = e.branch.limit, g = e.branch.offset, _ = n.nextAlias(), ee = h !== void 0 && g !== void 0 ? r` AND ${U(_, "rn", r)} > ${G(g, r)} AND ${U(_, "rn", r)} <= ${G(g + h, r)}` : h === void 0 ? r` AND ${U(_, "rn", r)} > ${G(g, r)}` : r` AND ${U(_, "rn", r)} <= ${G(h, r)}`, v = e.branch.onJunction === void 0 ? r`` : r` WHERE ${K(J(e.branch.onJunction), s, r)}`;
|
|
777
|
+
return r`COALESCE((SELECT JSON_ARRAYAGG(${c} ORDER BY ${U(_, "rn", r)}) FROM (SELECT ${r(o)}.*, ${U(s, i.sourceKey, r)} AS ${r(`__src_${i.sourceKey}`)}, ROW_NUMBER() OVER (PARTITION BY ${U(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}${v}) AS ${r(_)} WHERE ${U(_, `__src_${i.sourceKey}`, r)} = ${U(t, "id", r)}${d}${ee}), JSON_ARRAY())`;
|
|
778
|
+
}, Rn = (e, t, n) => {
|
|
779
|
+
let r = n.sql, i = n.nextAlias(), a = zn(i, t.table, t.children, n);
|
|
780
|
+
if (a === null) return null;
|
|
781
|
+
let o = e.predicate ? r` WHERE ${K(e.predicate, i, r)}` : r``, s = e.order.length > 0 ? r` ORDER BY ${r.csv(e.order.map((e) => e.direction === "desc" ? r`${U(i, e.column, r)} DESC` : r`${U(i, e.column, r)} ASC`))}` : r``, c = r``, l = r``;
|
|
782
|
+
if (e.skip !== void 0) {
|
|
783
|
+
let t = e.order.length > 0 ? s : r` ORDER BY (SELECT NULL)`;
|
|
784
|
+
l = e.take === void 0 ? r`${t} OFFSET ${G(e.skip, r)} ROWS` : r`${t} OFFSET ${G(e.skip, r)} ROWS FETCH NEXT ${G(e.take, r)} ROWS ONLY`;
|
|
785
|
+
} else e.take !== void 0 && (c = r`TOP ${G(e.take, r)} `);
|
|
786
|
+
let u = e.skip === void 0 ? s : r``;
|
|
787
|
+
return r`SELECT [__row] = (SELECT ${c}${a} FROM ${r(t.table.tableName)} AS ${r(i)}${o}${u}${l} FOR JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER)`;
|
|
788
|
+
}, zn = (e, t, n, r) => {
|
|
789
|
+
let i = r.sql, a = [];
|
|
790
|
+
for (let n of Object.keys(t.fields)) a.push(i`${U(e, n, i)} AS ${i(n)}`);
|
|
791
|
+
for (let t of n) {
|
|
792
|
+
let n = Bn(t, e, r);
|
|
793
|
+
if (n === null) return null;
|
|
794
|
+
a.push(n);
|
|
795
|
+
}
|
|
796
|
+
return i.csv(a);
|
|
797
|
+
}, Bn = (e, t, n) => {
|
|
798
|
+
let r = n.sql;
|
|
799
|
+
switch (e.relation.kind) {
|
|
800
|
+
case "one": {
|
|
801
|
+
let i = n.nextAlias(), a = zn(i, e.target, e.children, n);
|
|
802
|
+
if (a === null) return null;
|
|
803
|
+
let o = e.fkSide === "source" ? r`${U(i, "id", r)} = ${U(t, e.foreignKey, r)}` : r`${U(i, e.foreignKey, r)} = ${U(t, e.sourceKey, r)}`;
|
|
804
|
+
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)}`;
|
|
805
|
+
}
|
|
806
|
+
case "many": {
|
|
807
|
+
let i = n.nextAlias(), a = zn(i, e.target, e.children, n);
|
|
808
|
+
if (a === null) return null;
|
|
809
|
+
let o = e.branch.where === void 0 ? r`` : r` AND ${K(e.branch.where, i, r)}`, s = V(i, e.branch.orderBy, r), c = r``, l = r``;
|
|
810
|
+
if (e.branch.offset !== void 0) {
|
|
811
|
+
let t = e.branch.orderBy && e.branch.orderBy.length > 0 ? s : r` ORDER BY (SELECT NULL)`;
|
|
812
|
+
l = e.branch.limit === void 0 ? r`${t} OFFSET ${G(e.branch.offset, r)} ROWS` : r`${t} OFFSET ${G(e.branch.offset, r)} ROWS FETCH NEXT ${G(e.branch.limit, r)} ROWS ONLY`;
|
|
813
|
+
} else e.branch.limit !== void 0 && (c = r`TOP ${G(e.branch.limit, r)} `);
|
|
814
|
+
let u = e.branch.offset === void 0 ? s : r``;
|
|
815
|
+
return r`ISNULL(JSON_QUERY((SELECT ${c}${a} FROM ${r(e.target.tableName)} AS ${r(i)} WHERE ${U(i, e.foreignKey, r)} = ${U(t, e.sourceKey, r)}${o}${u}${l} FOR JSON PATH, INCLUDE_NULL_VALUES)), JSON_QUERY('[]')) AS ${r(e.relationName)}`;
|
|
816
|
+
}
|
|
817
|
+
case "manyToMany": {
|
|
818
|
+
let i = e.relation, a = i.through(), o = n.nextAlias(), s = n.nextAlias(), c = zn(o, e.target, e.children, n);
|
|
819
|
+
if (c === null) return null;
|
|
820
|
+
let l = e.branch.where === void 0 ? r`` : r` AND ${K(e.branch.where, o, r)}`, u = q(e, s, r), d = V(o, e.branch.orderBy, r), f = r``, p = r``;
|
|
821
|
+
if (e.branch.offset !== void 0) {
|
|
822
|
+
let t = e.branch.orderBy && e.branch.orderBy.length > 0 ? d : r` ORDER BY (SELECT NULL)`;
|
|
823
|
+
p = e.branch.limit === void 0 ? r`${t} OFFSET ${G(e.branch.offset, r)} ROWS` : r`${t} OFFSET ${G(e.branch.offset, r)} ROWS FETCH NEXT ${G(e.branch.limit, r)} ROWS ONLY`;
|
|
824
|
+
} else e.branch.limit !== void 0 && (f = r`TOP ${G(e.branch.limit, r)} `);
|
|
825
|
+
let m = e.branch.offset === void 0 ? d : r``;
|
|
826
|
+
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 ${U(o, "id", r)} = ${U(s, i.targetKey, r)} WHERE ${U(s, i.sourceKey, r)} = ${U(t, "id", r)}${u}${l}${m}${p} FOR JSON PATH, INCLUDE_NULL_VALUES)), JSON_QUERY('[]')) AS ${r(e.relationName)}`;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
}, U = (e, t, n) => e === void 0 ? n`${n(t)}` : n`${n(e)}.${n(t)}`, W = (e, t) => t.unsafe(`'${e.replace(/'/g, "''")}'`), G = (e, t) => {
|
|
830
|
+
if (!Number.isFinite(e) || !Number.isInteger(e) || e < 0) throw Error(`literalInt: expected non-negative integer, got ${e}`);
|
|
831
|
+
return t.unsafe(String(e));
|
|
832
|
+
}, K = (e, t, n) => R(Vn(e, t), n), q = (e, t, n, r = !1) => {
|
|
833
|
+
let i = e.branch.onJunction;
|
|
834
|
+
return i === void 0 ? n`` : n` AND ${K(r ? J(i) : i, t, n)}`;
|
|
835
|
+
}, Vn = (e, t) => {
|
|
836
|
+
if ("not" in e) return { not: Vn(e.not, t) };
|
|
837
|
+
if ("and" in e) return { and: e.and.map((e) => Vn(e, t)) };
|
|
838
|
+
if ("or" in e) return { or: e.or.map((e) => Vn(e, t)) };
|
|
839
|
+
if (e.op === "subquery-in" || e.op === "subquery-not-in") {
|
|
840
|
+
let n = e;
|
|
841
|
+
return {
|
|
842
|
+
...n,
|
|
843
|
+
column: `${t}.${n.column}`
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
if (e.op === "exists" || e.op === "not-exists") return e;
|
|
847
|
+
let n = e;
|
|
848
|
+
return {
|
|
849
|
+
...n,
|
|
850
|
+
column: `${t}.${n.column}`
|
|
851
|
+
};
|
|
852
|
+
}, J = (e) => {
|
|
853
|
+
if ("not" in e) return { not: J(e.not) };
|
|
854
|
+
if ("and" in e) return { and: e.and.map((e) => J(e)) };
|
|
855
|
+
if ("or" in e) return { or: e.or.map((e) => J(e)) };
|
|
856
|
+
if (e.op === "subquery-in" || e.op === "subquery-not-in" || e.op === "exists" || e.op === "not-exists") return e;
|
|
857
|
+
let t = e;
|
|
858
|
+
return {
|
|
859
|
+
...t,
|
|
860
|
+
value: Hn(t.value)
|
|
861
|
+
};
|
|
862
|
+
}, Hn = (e) => e == null ? e : e instanceof Date ? e.toISOString() : typeof e == "boolean" ? +!!e : Array.isArray(e) ? e.map((e) => Hn(e)) : e, Un, Wn, Gn = (e) => {
|
|
863
|
+
e.actors !== void 0 && (Un = e.actors), e.tenants !== void 0 && (Wn = e.tenants);
|
|
864
|
+
}, Kn = (e, t) => () => {
|
|
865
|
+
let n = e();
|
|
866
|
+
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`);
|
|
867
|
+
return n();
|
|
868
|
+
}, qn = () => Kn(() => Un, "actors"), Jn = () => Kn(() => Wn, "tenants"), Yn = n("actors", {
|
|
869
|
+
id: T(),
|
|
870
|
+
kind: o().oneOf([
|
|
871
|
+
"user",
|
|
872
|
+
"serviceAccount",
|
|
873
|
+
"apiKey",
|
|
874
|
+
"system"
|
|
875
|
+
]),
|
|
876
|
+
displayName: o().nullable(),
|
|
877
|
+
createdAt: v().default("now")
|
|
878
|
+
}), Xn = (e, t) => ({
|
|
879
|
+
column: e,
|
|
880
|
+
op: "eq",
|
|
881
|
+
value: t
|
|
882
|
+
}), Zn = (e, t) => ({
|
|
883
|
+
column: e,
|
|
884
|
+
op: "neq",
|
|
885
|
+
value: t
|
|
886
|
+
}), Qn = (e, t) => ({
|
|
887
|
+
column: e,
|
|
888
|
+
op: "gt",
|
|
889
|
+
value: t
|
|
890
|
+
}), $n = (e, t) => ({
|
|
891
|
+
column: e,
|
|
892
|
+
op: "gte",
|
|
893
|
+
value: t
|
|
894
|
+
}), er = (e, t) => ({
|
|
895
|
+
column: e,
|
|
896
|
+
op: "lt",
|
|
897
|
+
value: t
|
|
898
|
+
}), tr = (e, t) => ({
|
|
899
|
+
column: e,
|
|
900
|
+
op: "lte",
|
|
901
|
+
value: t
|
|
902
|
+
}), nr = (e, t) => ({
|
|
903
|
+
column: e,
|
|
904
|
+
op: "in",
|
|
905
|
+
value: t
|
|
906
|
+
}), rr = (e, t) => ({
|
|
907
|
+
column: e,
|
|
908
|
+
op: "notIn",
|
|
909
|
+
value: t
|
|
910
|
+
}), ir = (e, t) => ({
|
|
911
|
+
column: e,
|
|
912
|
+
op: "contains",
|
|
913
|
+
value: t
|
|
914
|
+
}), ar = (e, t) => ({
|
|
915
|
+
column: e,
|
|
916
|
+
op: "arrayContains",
|
|
917
|
+
value: t
|
|
918
|
+
}), or = (e, t) => ({
|
|
919
|
+
column: e,
|
|
920
|
+
op: "arrayOverlaps",
|
|
921
|
+
value: t
|
|
922
|
+
}), sr = (e, t) => ({
|
|
923
|
+
column: e,
|
|
924
|
+
op: "arrayHas",
|
|
925
|
+
value: t
|
|
926
|
+
}), cr = (e) => ({
|
|
927
|
+
column: e,
|
|
928
|
+
op: "isNull"
|
|
929
|
+
}), lr = (e) => ({
|
|
930
|
+
column: e,
|
|
931
|
+
op: "isNotNull"
|
|
932
|
+
}), ur = (e, t) => ({
|
|
933
|
+
column: e,
|
|
934
|
+
op: "spatial",
|
|
935
|
+
value: t
|
|
936
|
+
}), dr = (...e) => ({ and: e }), fr = (...e) => ({ or: e }), pr = (e) => ({ not: e }), mr = (e, ...t) => {
|
|
937
|
+
if (t.length === 0) throw Error(`jsonField("${e}"): at least one path segment is required`);
|
|
938
|
+
let n = (n, r) => ({
|
|
939
|
+
column: e,
|
|
940
|
+
path: t,
|
|
941
|
+
op: n,
|
|
942
|
+
value: r
|
|
943
|
+
});
|
|
944
|
+
return {
|
|
945
|
+
eq: (e) => n("eq", e),
|
|
946
|
+
neq: (e) => n("neq", e),
|
|
947
|
+
gt: (e) => n("gt", e),
|
|
948
|
+
gte: (e) => n("gte", e),
|
|
949
|
+
lt: (e) => n("lt", e),
|
|
950
|
+
lte: (e) => n("lte", e),
|
|
951
|
+
inSet: (e) => n("in", e),
|
|
952
|
+
notInSet: (e) => n("notIn", e),
|
|
953
|
+
contains: (e) => n("contains", e),
|
|
954
|
+
isNull: () => n("isNull"),
|
|
955
|
+
isNotNull: () => n("isNotNull")
|
|
956
|
+
};
|
|
957
|
+
}, hr = (e) => "descriptor" in e ? e.descriptor : e, gr = (e, t) => ({
|
|
958
|
+
column: e,
|
|
959
|
+
op: "subquery-in",
|
|
960
|
+
subquery: hr(t)
|
|
961
|
+
}), _r = (e, t) => ({
|
|
962
|
+
column: e,
|
|
963
|
+
op: "subquery-not-in",
|
|
964
|
+
subquery: hr(t)
|
|
965
|
+
}), vr = (e) => ({
|
|
966
|
+
op: "exists",
|
|
967
|
+
subquery: hr(e)
|
|
968
|
+
}), yr = (e) => ({
|
|
969
|
+
op: "not-exists",
|
|
970
|
+
subquery: hr(e)
|
|
971
|
+
}), br = (e, t) => e == null || t == null ? null : typeof e == typeof t ? e === t ? 0 : e > t ? 1 : -1 : null, xr = (e, t, n = null) => {
|
|
972
|
+
if (!t) return !1;
|
|
973
|
+
if ("not" in e) return !xr(e.not, t, n);
|
|
974
|
+
if ("and" in e) {
|
|
975
|
+
for (let r of e.and) if (!xr(r, t, n)) return !1;
|
|
976
|
+
return !0;
|
|
977
|
+
}
|
|
978
|
+
if ("or" in e) {
|
|
979
|
+
for (let r of e.or) if (xr(r, t, n)) return !0;
|
|
980
|
+
return !1;
|
|
981
|
+
}
|
|
982
|
+
if (e.op === "subquery-in" || e.op === "subquery-not-in") {
|
|
983
|
+
if (!n) return !1;
|
|
984
|
+
let r = n.get(e.subquery);
|
|
985
|
+
if (!(r instanceof Set)) return !1;
|
|
986
|
+
let i = t[e.column], a = r.has(i);
|
|
987
|
+
return e.op === "subquery-in" ? a : !a;
|
|
988
|
+
}
|
|
989
|
+
if (e.op === "exists" || e.op === "not-exists") {
|
|
990
|
+
if (!n) return !1;
|
|
991
|
+
let t = n.get(e.subquery), r = typeof t == "number" ? t > 0 : t instanceof Set && t.size > 0;
|
|
992
|
+
return e.op === "exists" ? r : !r;
|
|
993
|
+
}
|
|
994
|
+
let r = e, i = t[r.column];
|
|
995
|
+
switch (r.op) {
|
|
996
|
+
case "eq": return i === r.value;
|
|
997
|
+
case "neq": return i !== r.value;
|
|
998
|
+
case "gt": return (br(i, r.value) ?? -1) > 0;
|
|
999
|
+
case "gte": return (br(i, r.value) ?? -1) >= 0;
|
|
1000
|
+
case "lt": return (br(i, r.value) ?? 1) < 0;
|
|
1001
|
+
case "lte": return (br(i, r.value) ?? 1) <= 0;
|
|
1002
|
+
case "in": return Array.isArray(r.value) && r.value.includes(i);
|
|
1003
|
+
case "notIn": return Array.isArray(r.value) && !r.value.includes(i);
|
|
1004
|
+
case "contains": return typeof i == "string" && typeof r.value == "string" && i.toLowerCase().includes(r.value.toLowerCase());
|
|
1005
|
+
case "arrayContains": {
|
|
1006
|
+
let e = Array.isArray(i) ? i : [];
|
|
1007
|
+
return (Array.isArray(r.value) ? r.value : []).every((t) => e.includes(t));
|
|
1008
|
+
}
|
|
1009
|
+
case "arrayOverlaps": {
|
|
1010
|
+
let e = Array.isArray(i) ? i : [];
|
|
1011
|
+
return (Array.isArray(r.value) ? r.value : []).some((t) => e.includes(t));
|
|
1012
|
+
}
|
|
1013
|
+
case "arrayHas": return Array.isArray(i) && i.includes(r.value);
|
|
1014
|
+
case "isNull": return i == null;
|
|
1015
|
+
case "isNotNull": return i != null;
|
|
1016
|
+
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.");
|
|
1017
|
+
}
|
|
1018
|
+
}, Sr = function* (e) {
|
|
1019
|
+
if ("not" in e) {
|
|
1020
|
+
yield* Sr(e.not);
|
|
1021
|
+
return;
|
|
1022
|
+
}
|
|
1023
|
+
if ("and" in e) {
|
|
1024
|
+
for (let t of e.and) yield* Sr(t);
|
|
1025
|
+
return;
|
|
1026
|
+
}
|
|
1027
|
+
if ("or" in e) {
|
|
1028
|
+
for (let t of e.or) yield* Sr(t);
|
|
1029
|
+
return;
|
|
1030
|
+
}
|
|
1031
|
+
e.op === "subquery-in" || e.op === "subquery-not-in" ? yield {
|
|
1032
|
+
kind: "in",
|
|
1033
|
+
descriptor: e.subquery
|
|
1034
|
+
} : (e.op === "exists" || e.op === "not-exists") && (yield {
|
|
1035
|
+
kind: "exists",
|
|
1036
|
+
descriptor: e.subquery
|
|
1037
|
+
});
|
|
1038
|
+
}, Cr = function* (e, t = !0) {
|
|
1039
|
+
if ("not" in e) {
|
|
1040
|
+
yield* Cr(e.not, !1);
|
|
1041
|
+
return;
|
|
1042
|
+
}
|
|
1043
|
+
if ("and" in e) {
|
|
1044
|
+
for (let n of e.and) yield* Cr(n, t);
|
|
1045
|
+
return;
|
|
1046
|
+
}
|
|
1047
|
+
if ("or" in e) {
|
|
1048
|
+
for (let t of e.or) yield* Cr(t, !1);
|
|
1049
|
+
return;
|
|
1050
|
+
}
|
|
1051
|
+
e.op === "subquery-in" || e.op === "subquery-not-in" || e.op === "exists" || e.op === "not-exists" || (yield {
|
|
1052
|
+
...e,
|
|
1053
|
+
conjunctive: t
|
|
1054
|
+
});
|
|
1055
|
+
}, wr = class extends nt.TaggedError("TableStreamError") {}, Tr = 1e3, Er = (e, t, n, r) => {
|
|
1056
|
+
let i = O.match(r, {
|
|
1057
|
+
onNone: () => e.where,
|
|
1058
|
+
onSome: (n) => e.where ? dr(e.where, Qn(t, n)) : Qn(t, n)
|
|
1059
|
+
});
|
|
1060
|
+
return {
|
|
1061
|
+
table: e.table,
|
|
1062
|
+
predicate: i,
|
|
1063
|
+
order: [{
|
|
1064
|
+
column: t,
|
|
1065
|
+
direction: "asc"
|
|
1066
|
+
}],
|
|
1067
|
+
take: n,
|
|
1068
|
+
skip: void 0,
|
|
1069
|
+
projection: e.projection,
|
|
1070
|
+
...e.includeDeleted === void 0 ? {} : { includeDeleted: e.includeDeleted },
|
|
1071
|
+
...e.crossTenant === void 0 ? {} : { crossTenant: e.crossTenant }
|
|
1072
|
+
};
|
|
1073
|
+
}, Dr = (e, t) => {
|
|
1074
|
+
let n = t.pk ?? "id", r = t.chunkSize ?? Tr;
|
|
1075
|
+
return it.paginateChunkEffect(O.none(), (i) => {
|
|
1076
|
+
let a = rt.tryPromise({
|
|
1077
|
+
try: () => e.query(Er(t, n, r, i)),
|
|
1078
|
+
catch: (e) => new wr({
|
|
1079
|
+
table: t.table,
|
|
1080
|
+
cause: e
|
|
1081
|
+
})
|
|
1082
|
+
});
|
|
1083
|
+
return (t.retry ? a.pipe(rt.retry(t.retry)) : a).pipe(rt.map((e) => [tt.fromIterable(e), e.length === r && e.length > 0 ? O.some(O.some(e[e.length - 1][n])) : O.none()]));
|
|
1084
|
+
});
|
|
1085
|
+
}, Or = (e) => {
|
|
1086
|
+
let t = e.alpha ?? .5;
|
|
1087
|
+
if (t < 0 || t > 1) throw Error(`hybridSearch: alpha must be in [0, 1], got ${t}`);
|
|
1088
|
+
return (n) => {
|
|
1089
|
+
let r = Array.isArray(e.vector.query) ? {
|
|
1090
|
+
column: e.vector.col,
|
|
1091
|
+
queryVector: e.vector.query,
|
|
1092
|
+
distance: e.vector.distance ?? "cosine",
|
|
1093
|
+
alpha: t
|
|
1094
|
+
} : {
|
|
1095
|
+
column: e.vector.col,
|
|
1096
|
+
queryText: e.vector.query,
|
|
1097
|
+
distance: e.vector.distance ?? "cosine",
|
|
1098
|
+
alpha: t
|
|
1099
|
+
}, i = C(n.table, e.fts.indexName, n.fullTextIndexes);
|
|
1100
|
+
return {
|
|
1101
|
+
...n,
|
|
1102
|
+
annClause: r,
|
|
1103
|
+
fullTextSearch: {
|
|
1104
|
+
indexName: e.fts.indexName,
|
|
1105
|
+
query: e.fts.query,
|
|
1106
|
+
columns: i.columns,
|
|
1107
|
+
...i.config === void 0 ? {} : { config: i.config },
|
|
1108
|
+
...i.weights === void 0 ? {} : { weights: i.weights }
|
|
1109
|
+
}
|
|
1110
|
+
};
|
|
1111
|
+
};
|
|
1112
|
+
}, kr = async (e, t) => {
|
|
1113
|
+
let n = e.as ?? "embedding", r = t.row[e.from];
|
|
1114
|
+
if (typeof r != "string" || r.length === 0) return {};
|
|
1115
|
+
let i = await t.embed(r);
|
|
1116
|
+
return { [n]: i };
|
|
1117
|
+
}, Ar = (e) => {
|
|
1118
|
+
if (!Number.isInteger(e.dimensions) || e.dimensions <= 0) throw Error(`vectorEmbedding: dimensions must be a positive integer, got ${String(e.dimensions)}.`);
|
|
1119
|
+
let t = e.as ?? "embedding", n = (t) => kr(e, t);
|
|
1120
|
+
return d({
|
|
1121
|
+
id: "voltro/vectorEmbedding",
|
|
1122
|
+
fields: { [t]: ke(e.dimensions) },
|
|
1123
|
+
...e.index === !1 ? {} : { indexes: [{
|
|
1124
|
+
fields: [t],
|
|
1125
|
+
kind: "hnsw",
|
|
1126
|
+
kindOptions: { hnsw: {
|
|
1127
|
+
...e.index?.m === void 0 ? {} : { m: e.index.m },
|
|
1128
|
+
...e.index?.efConstruction === void 0 ? {} : { efConstruction: e.index.efConstruction },
|
|
1129
|
+
distance: e.distance ?? "cosine"
|
|
1130
|
+
} }
|
|
1131
|
+
}] },
|
|
1132
|
+
behaviors: {
|
|
1133
|
+
beforeInsert: n,
|
|
1134
|
+
beforeUpdate: n
|
|
1135
|
+
}
|
|
1136
|
+
});
|
|
1137
|
+
}, jr = 63, Mr = "br_pr", Y = class extends Error {
|
|
1138
|
+
_tag = "BranchNameInvalid";
|
|
1139
|
+
constructor(e) {
|
|
1140
|
+
super(e), this.name = "BranchNameInvalid";
|
|
1141
|
+
}
|
|
1142
|
+
}, Nr = (e, t) => {
|
|
1143
|
+
if (!Number.isInteger(t) || t < 0) throw new Y(`prNumber must be a non-negative integer, got ${String(t)}`);
|
|
1144
|
+
let n = Zt(e);
|
|
1145
|
+
if (n.replace(/_/g, "") === "") throw new Y(`app slug '${e}' has no usable identifier characters`);
|
|
1146
|
+
let r = `${Mr}${t}_${n}`;
|
|
1147
|
+
if (r.length > jr) throw new Y(`branch namespace '${r}' exceeds the ${jr}-byte identifier limit — use a shorter app slug`);
|
|
1148
|
+
return r;
|
|
1149
|
+
}, Pr = (e) => e.startsWith(Mr), Fr = {
|
|
1150
|
+
requested: {
|
|
1151
|
+
provision: "provisioning",
|
|
1152
|
+
destroy: "destroying"
|
|
1153
|
+
},
|
|
1154
|
+
provisioning: {
|
|
1155
|
+
provisioned: "ready",
|
|
1156
|
+
fail: "failed"
|
|
1157
|
+
},
|
|
1158
|
+
ready: { destroy: "destroying" },
|
|
1159
|
+
failed: {
|
|
1160
|
+
provision: "provisioning",
|
|
1161
|
+
destroy: "destroying"
|
|
1162
|
+
},
|
|
1163
|
+
destroying: {
|
|
1164
|
+
destroyed: "destroyed",
|
|
1165
|
+
fail: "failed"
|
|
1166
|
+
},
|
|
1167
|
+
destroyed: {}
|
|
1168
|
+
}, Ir = class extends Error {
|
|
1169
|
+
from;
|
|
1170
|
+
event;
|
|
1171
|
+
_tag = "BranchTransitionInvalid";
|
|
1172
|
+
constructor(e, t) {
|
|
1173
|
+
super(`invalid branch transition: ${e} --${t}-->`), this.from = e, this.event = t, this.name = "BranchTransitionInvalid";
|
|
1174
|
+
}
|
|
1175
|
+
}, Lr = (e, t) => {
|
|
1176
|
+
let n = Fr[e][t];
|
|
1177
|
+
if (n === void 0) throw new Ir(e, t);
|
|
1178
|
+
return n;
|
|
1179
|
+
}, Rr = (e, t, n, r) => {
|
|
1180
|
+
if (n === "copy" && (r === void 0 || r === "")) throw new Y("seed 'copy' requires a parentNamespace to snapshot from");
|
|
1181
|
+
let i = [{
|
|
1182
|
+
kind: "create-namespace",
|
|
1183
|
+
detail: e
|
|
1184
|
+
}];
|
|
1185
|
+
for (let n of t) i.push({
|
|
1186
|
+
kind: "create-table",
|
|
1187
|
+
table: n,
|
|
1188
|
+
detail: `${e}.${n}`
|
|
1189
|
+
});
|
|
1190
|
+
if (n === "copy") for (let n of t) i.push({
|
|
1191
|
+
kind: "copy-table",
|
|
1192
|
+
table: n,
|
|
1193
|
+
detail: `${r}.${n} → ${e}.${n}`
|
|
1194
|
+
});
|
|
1195
|
+
else n === "fresh" && i.push({
|
|
1196
|
+
kind: "seed",
|
|
1197
|
+
detail: `run boot seeds into ${e}`
|
|
1198
|
+
});
|
|
1199
|
+
return i;
|
|
1200
|
+
}, zr = (e) => {
|
|
1201
|
+
if (!Pr(e)) throw new Y(`refusing to plan teardown of '${e}' — not a branch namespace`);
|
|
1202
|
+
return [{
|
|
1203
|
+
kind: "drop-namespace",
|
|
1204
|
+
detail: e
|
|
1205
|
+
}];
|
|
1206
|
+
}, Br = (e) => {
|
|
1207
|
+
if (e === void 0 || e === "") return !1;
|
|
1208
|
+
try {
|
|
1209
|
+
let t = new URL(e).hostname.toLowerCase();
|
|
1210
|
+
return /(^|\.)neon\.(tech|build)$/.test(t);
|
|
1211
|
+
} catch {
|
|
1212
|
+
return /\bneon\.(tech|build)\b/i.test(e);
|
|
1213
|
+
}
|
|
1214
|
+
}, Vr = (e) => e.prefer === void 0 ? e.seed === "copy" && Br(e.dbUrl) ? "neon-cow" : "namespace" : e.prefer, Hr = (e, t = {}) => [{
|
|
1215
|
+
kind: "neon-branch-create",
|
|
1216
|
+
detail: `${t.parentBranch ?? "main"} → ${e} (copy-on-write)`
|
|
1217
|
+
}], Ur = (e) => {
|
|
1218
|
+
if (!Pr(e)) throw new Y(`refusing to plan teardown of '${e}' — not a branch namespace`);
|
|
1219
|
+
return [{
|
|
1220
|
+
kind: "neon-branch-delete",
|
|
1221
|
+
detail: e
|
|
1222
|
+
}];
|
|
1223
|
+
}, Wr = (e) => e.mechanism === "neon-cow" ? Hr(e.branchId, e.neonParentBranch === void 0 ? {} : { parentBranch: e.neonParentBranch }) : Rr(e.branchId, e.tableNames, e.seed, e.parentNamespace), Gr = (e, t) => e === "neon-cow" ? Ur(t) : zr(t), Kr = (e, t, n) => {
|
|
1224
|
+
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);
|
|
1225
|
+
return {
|
|
1226
|
+
evict: i,
|
|
1227
|
+
rejectNew: r.filter((e) => !a.has(e.namespace)).length >= t.maxBranches
|
|
1228
|
+
};
|
|
1229
|
+
}, qr = async (e, t) => {
|
|
1230
|
+
let n = Nr(e.appSlug, e.prNumber), r = Vr({
|
|
1231
|
+
seed: e.seed,
|
|
1232
|
+
...e.dbUrl === void 0 ? {} : { dbUrl: e.dbUrl },
|
|
1233
|
+
...e.prefer === void 0 ? {} : { prefer: e.prefer }
|
|
1234
|
+
}), i = Wr({
|
|
1235
|
+
mechanism: r,
|
|
1236
|
+
branchId: n,
|
|
1237
|
+
tableNames: e.tableNames,
|
|
1238
|
+
seed: e.seed,
|
|
1239
|
+
...e.parentNamespace === void 0 ? {} : { parentNamespace: e.parentNamespace },
|
|
1240
|
+
...e.neonParentBranch === void 0 ? {} : { neonParentBranch: e.neonParentBranch }
|
|
1241
|
+
});
|
|
1242
|
+
if (r === "neon-cow") return {
|
|
1243
|
+
branchId: n,
|
|
1244
|
+
mechanism: r,
|
|
1245
|
+
steps: i,
|
|
1246
|
+
connectionString: await t.neonBranchCreate(n, e.neonParentBranch ?? "main")
|
|
1247
|
+
};
|
|
1248
|
+
await t.createNamespace(n);
|
|
1249
|
+
for (let r of e.tableNames) await t.createTable(n, r);
|
|
1250
|
+
if (e.seed === "copy") {
|
|
1251
|
+
if (e.parentNamespace === void 0 || e.parentNamespace === "") throw Error("provisionBranch: seed 'copy' requires parentNamespace");
|
|
1252
|
+
for (let r of e.tableNames) await t.copyTable(e.parentNamespace, n, r);
|
|
1253
|
+
}
|
|
1254
|
+
return {
|
|
1255
|
+
branchId: n,
|
|
1256
|
+
mechanism: r,
|
|
1257
|
+
steps: i
|
|
1258
|
+
};
|
|
1259
|
+
}, Jr = async (e, t, n) => {
|
|
1260
|
+
let r = Gr(t, e);
|
|
1261
|
+
return t === "neon-cow" ? await n.neonBranchDelete(e) : await n.dropNamespace(e), r;
|
|
1262
|
+
}, Yr = (e, t, n) => Kr(e, t, n), X = (e) => `"${e.replace(/"/g, "\"\"")}"`, Xr = (e) => {
|
|
1263
|
+
let t = () => {
|
|
1264
|
+
throw Error("namespace branch executor: Neon ops require a neon-cow executor (use a Neon connection for copy-on-write branches)");
|
|
1265
|
+
};
|
|
1266
|
+
return {
|
|
1267
|
+
createNamespace: (t) => e.run(`CREATE SCHEMA IF NOT EXISTS ${X(t)}`),
|
|
1268
|
+
createTable: (t, n) => e.run(`CREATE TABLE IF NOT EXISTS ${X(t)}.${X(n)} (LIKE ${X(n)} INCLUDING ALL)`),
|
|
1269
|
+
copyTable: (t, n, r) => e.run(`INSERT INTO ${X(n)}.${X(r)} SELECT * FROM ${X(t)}.${X(r)}`),
|
|
1270
|
+
dropNamespace: (t) => e.run(`DROP SCHEMA IF EXISTS ${X(t)} CASCADE`),
|
|
1271
|
+
neonBranchCreate: t,
|
|
1272
|
+
neonBranchDelete: t
|
|
1273
|
+
};
|
|
1274
|
+
}, Zr = class extends Error {
|
|
1275
|
+
_tag = "TenantResidencyUnresolved";
|
|
1276
|
+
constructor(e) {
|
|
1277
|
+
super(e), this.name = "TenantResidencyUnresolved";
|
|
1278
|
+
}
|
|
1279
|
+
}, Qr = class extends Error {
|
|
1280
|
+
region;
|
|
1281
|
+
_tag = "TenantRegionUnavailable";
|
|
1282
|
+
constructor(e, t) {
|
|
1283
|
+
super(t), this.region = e, this.name = "TenantRegionUnavailable";
|
|
1284
|
+
}
|
|
1285
|
+
}, $r, ei = (e) => {
|
|
1286
|
+
if (e.homes === void 0) throw Error("setResidencyConfig: `homes` is required");
|
|
1287
|
+
if (e.servableRegions === void 0 || e.servableRegions.length === 0) throw Error("setResidencyConfig: `servableRegions` must be non-empty (which regions this deployment serves)");
|
|
1288
|
+
let t = /* @__PURE__ */ new Map();
|
|
1289
|
+
for (let n of e.homes) {
|
|
1290
|
+
let e = t.get(n.tenantId);
|
|
1291
|
+
if (e !== void 0 && e !== n.region) throw Error(`residency: tenant '${n.tenantId}' is mapped to two regions ('${e}' and '${n.region}')`);
|
|
1292
|
+
t.set(n.tenantId, n.region);
|
|
1293
|
+
}
|
|
1294
|
+
return $r = e, e;
|
|
1295
|
+
}, ti = () => $r, ni = () => {
|
|
1296
|
+
$r = void 0;
|
|
1297
|
+
}, ri = (e, t) => {
|
|
1298
|
+
if (e == null || e === "") throw new Zr("residency is on but the request has no resolvable tenant — refusing to bind a store");
|
|
1299
|
+
let n = t.homes.find((t) => t.tenantId === e);
|
|
1300
|
+
if (n === void 0) throw new Zr(`no residency home is mapped for tenant '${e}' — refusing to fall back to a default store (would be a residency violation)`);
|
|
1301
|
+
return n;
|
|
1302
|
+
}, ii = (e, t) => t.servableRegions.includes(e), ai = (e, t) => {
|
|
1303
|
+
let n = ri(e.tenantId, t), r = Qt(n.tenantId);
|
|
1304
|
+
return {
|
|
1305
|
+
region: n.region,
|
|
1306
|
+
namespace: r,
|
|
1307
|
+
...n.connectionKey === void 0 ? {} : { connectionKey: n.connectionKey }
|
|
1308
|
+
};
|
|
1309
|
+
}, oi = (e, t, n) => {
|
|
1310
|
+
let r = ai(e, t);
|
|
1311
|
+
if (!ii(r.region, t)) throw new Qr(r.region, `tenant homed in region '${r.region}', which this deployment does not serve — route to the home region`);
|
|
1312
|
+
let i = n.get(r.region);
|
|
1313
|
+
if (i === void 0) throw new Qr(r.region, `no store handle for home region '${r.region}' (declared servable but not wired) — failing closed`);
|
|
1314
|
+
return {
|
|
1315
|
+
store: i,
|
|
1316
|
+
placement: r
|
|
1317
|
+
};
|
|
1318
|
+
}, si = async (e, t, n, r) => {
|
|
1319
|
+
let { store: i, placement: a } = oi(e, t, n);
|
|
1320
|
+
return await r(i, a), a;
|
|
1321
|
+
}, Z = (e) => {
|
|
1322
|
+
if (typeof e == "string") return e;
|
|
1323
|
+
if ("jsonPath" in e) {
|
|
1324
|
+
let { column: t, path: n, numeric: r } = e.jsonPath;
|
|
1325
|
+
return `(json:${t}->${n.join(".")}${r ? "::num" : ""})`;
|
|
1326
|
+
}
|
|
1327
|
+
return `(${e.expr})`;
|
|
1328
|
+
}, Q = (e) => e.map(Z), ci = (e, t) => {
|
|
1329
|
+
if (e.length !== t.length) return !1;
|
|
1330
|
+
for (let n = 0; n < e.length; n++) if (Z(e[n]) !== Z(t[n])) return !1;
|
|
1331
|
+
return !0;
|
|
1332
|
+
}, li = (e, t) => {
|
|
1333
|
+
if (e.length >= t.length) return !1;
|
|
1334
|
+
for (let n = 0; n < e.length; n++) if (Z(e[n]) !== Z(t[n])) return !1;
|
|
1335
|
+
return !0;
|
|
1336
|
+
}, ui = (e) => {
|
|
1337
|
+
let t = [], n = e.appliedIndexes, r = e.tableName;
|
|
1338
|
+
for (let e = 0; e < n.length; e++) {
|
|
1339
|
+
let i = n[e];
|
|
1340
|
+
for (let a = e + 1; a < n.length; a++) {
|
|
1341
|
+
let e = n[a];
|
|
1342
|
+
if (ci(i.fields, e.fields)) {
|
|
1343
|
+
let [n, a] = i.name < e.name ? [i, e] : [e, i];
|
|
1344
|
+
t.push({
|
|
1345
|
+
kind: "duplicate-fields",
|
|
1346
|
+
table: r,
|
|
1347
|
+
redundant: n,
|
|
1348
|
+
coveredBy: a,
|
|
1349
|
+
message: `'${r}' has two indexes on the same fields [${Q(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).`
|
|
1350
|
+
});
|
|
1351
|
+
continue;
|
|
1352
|
+
}
|
|
1353
|
+
li(i.fields, e.fields) ? t.push({
|
|
1354
|
+
kind: "redundant-prefix",
|
|
1355
|
+
table: r,
|
|
1356
|
+
redundant: i,
|
|
1357
|
+
coveredBy: e,
|
|
1358
|
+
message: `'${r}' index '${i.name}' on [${Q(i.fields).join(", ")}] is a leading prefix of '${e.name}' on [${Q(e.fields).join(", ")}]. Every B-tree dialect we support (Postgres / MySQL / MariaDB / MSSQL / SQLite) can serve a [${Q(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).`
|
|
1359
|
+
}) : li(e.fields, i.fields) && t.push({
|
|
1360
|
+
kind: "redundant-prefix",
|
|
1361
|
+
table: r,
|
|
1362
|
+
redundant: e,
|
|
1363
|
+
coveredBy: i,
|
|
1364
|
+
message: `'${r}' index '${e.name}' on [${Q(e.fields).join(", ")}] is a leading prefix of '${i.name}' on [${Q(i.fields).join(", ")}]. Every B-tree dialect we support (Postgres / MySQL / MariaDB / MSSQL / SQLite) can serve a [${Q(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).`
|
|
1365
|
+
});
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
return t;
|
|
1369
|
+
}, di = (e) => {
|
|
1370
|
+
let t = [];
|
|
1371
|
+
for (let n of e) t.push(...ui(n));
|
|
1372
|
+
return t;
|
|
1373
|
+
}, fi = "\x1B[", pi = `${fi}38;2;245;158;11m`, mi = `${fi}38;2;16;185;129m`, hi = (e, t) => `${e}${t}${k.reset}`, $ = (e, t, n) => e ? typeof t == "function" ? t(n) : hi(t, n) : n, gi = (e, t = {}) => {
|
|
1374
|
+
let n = t.color ?? !0, r = t.indent ?? " ", i = (e) => $(n, k.dim, e.padEnd(11)), a = (e) => $(n, (e) => k.bold(k.brand(e)), e), o = (e) => $(n, k.brightRed24, e), s = (e) => $(n, mi, e), c = (e) => $(n, pi, e), l = (e) => $(n, k.dim, e), u = `${c("index audit")} ${l("·")} ${$(n, k.bold, e.kind)}`, d = e.redundant.fields.length, f = Q(e.coveredBy.fields.slice(d)), p = (e, t) => {
|
|
1375
|
+
let n = e.slice(0, t), r = e.slice(t), i = [];
|
|
1376
|
+
return n.length > 0 && i.push(s(`[${n.join(", ")}]`)), r.length > 0 && i.push(c(`+ [${r.join(", ")}]`)), i.join(" ");
|
|
1377
|
+
}, m = [];
|
|
1378
|
+
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(Q(e.redundant.fields), d)}`), m.push(""), m.push(`${i("covered by")}${s(e.coveredBy.name)}`), m.push(`${i("")}${p(Q(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 ${$(n, k.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");
|
|
1379
|
+
}, _i = (e, t) => ({
|
|
1380
|
+
name: e,
|
|
1381
|
+
body: t
|
|
1382
|
+
}), vi = (e) => {
|
|
1383
|
+
if (e.lifecycle === "cron" && !e.cron) throw Error(`seed "${e.id}": lifecycle 'cron' requires a 'cron' field`);
|
|
1384
|
+
if (e.lifecycle === "onSchemaChange" && (!e.watchedTables || e.watchedTables.length === 0)) throw Error(`seed "${e.id}": lifecycle 'onSchemaChange' requires at least one entry in 'watchedTables'`);
|
|
1385
|
+
let t = e.steps({ step: _i });
|
|
1386
|
+
if (t.length === 0) throw Error(`seed "${e.id}": at least one step is required`);
|
|
1387
|
+
return {
|
|
1388
|
+
__seed: !0,
|
|
1389
|
+
id: e.id,
|
|
1390
|
+
name: e.name,
|
|
1391
|
+
lifecycle: e.lifecycle,
|
|
1392
|
+
...e.cron === void 0 ? {} : { cron: e.cron },
|
|
1393
|
+
...e.watchedTables === void 0 ? {} : { watchedTables: e.watchedTables },
|
|
1394
|
+
...e.fingerprint === void 0 ? {} : { fingerprint: e.fingerprint },
|
|
1395
|
+
steps: t
|
|
1396
|
+
};
|
|
1397
|
+
}, yi = (e) => typeof e == "object" && !!e && "__seed" in e && e.__seed === !0, bi = (e, { apply: t, undo: n }) => ({
|
|
1398
|
+
name: e,
|
|
1399
|
+
apply: t,
|
|
1400
|
+
undo: n
|
|
1401
|
+
}), xi = (e) => {
|
|
1402
|
+
if (!/^\d{14}_/.test(e.id)) throw Error(`migration "${e.id}": id must start with a 14-digit timestamp followed by "_" (convention: <YYYYMMDDHHMMSS>_<slug>)`);
|
|
1403
|
+
let t = e.steps({ step: bi });
|
|
1404
|
+
if (t.length === 0) throw Error(`migration "${e.id}": at least one step is required`);
|
|
1405
|
+
return {
|
|
1406
|
+
__migration: !0,
|
|
1407
|
+
id: e.id,
|
|
1408
|
+
name: e.name,
|
|
1409
|
+
steps: t
|
|
1410
|
+
};
|
|
1411
|
+
}, Si = (e) => typeof e == "object" && !!e && "__migration" in e && e.__migration === !0, Ci = n("_voltro_migrations", {
|
|
1412
|
+
id: T({ prefix: "mig" }),
|
|
1413
|
+
name: o(),
|
|
1414
|
+
hash: o(),
|
|
1415
|
+
status: o(),
|
|
1416
|
+
appliedAt: v().nullable(),
|
|
1417
|
+
durationMs: D().nullable(),
|
|
1418
|
+
errorMessage: o().nullable(),
|
|
1419
|
+
errorStack: o().nullable(),
|
|
1420
|
+
workflowRunId: o().nullable(),
|
|
1421
|
+
createdAt: v().default("now"),
|
|
1422
|
+
updatedAt: v().default("now").onUpdate("now")
|
|
1423
|
+
}).index(["status"]).reactive(), wi = n("_voltro_seeds", {
|
|
1424
|
+
id: T({ prefix: "seed" }),
|
|
1425
|
+
name: o(),
|
|
1426
|
+
lifecycle: o(),
|
|
1427
|
+
fingerprint: o(),
|
|
1428
|
+
cronSchedule: o().nullable(),
|
|
1429
|
+
watchedTables: E().nullable(),
|
|
1430
|
+
nextRunAt: v().nullable(),
|
|
1431
|
+
lastRunStatus: o().nullable(),
|
|
1432
|
+
lastRunAt: v().nullable(),
|
|
1433
|
+
lastRunDurationMs: D().nullable(),
|
|
1434
|
+
lastRunRowsTouched: D().nullable(),
|
|
1435
|
+
lastError: o().nullable(),
|
|
1436
|
+
workflowRunId: o().nullable(),
|
|
1437
|
+
createdAt: v().default("now"),
|
|
1438
|
+
updatedAt: v().default("now").onUpdate("now")
|
|
1439
|
+
}).index(["lifecycle"]).index(["nextRunAt"]).reactive(), Ti = n("_voltro_migration_plans", {
|
|
1440
|
+
id: T({ prefix: "plan" }),
|
|
1441
|
+
fingerprint: o(),
|
|
1442
|
+
operations: E(),
|
|
1443
|
+
rollbackPlan: E().nullable(),
|
|
1444
|
+
appliedBy: o(),
|
|
1445
|
+
environment: o(),
|
|
1446
|
+
source: o(),
|
|
1447
|
+
onlineStrategy: o().nullable(),
|
|
1448
|
+
durationMs: D(),
|
|
1449
|
+
notes: o().nullable(),
|
|
1450
|
+
squashedAt: v().nullable(),
|
|
1451
|
+
appliedAt: v().default("now"),
|
|
1452
|
+
createdAt: v().default("now"),
|
|
1453
|
+
updatedAt: v().default("now").onUpdate("now")
|
|
1454
|
+
}).index(["fingerprint"]).index(["environment"]).reactive(), Ei = n("_voltro_idempotency", {
|
|
1455
|
+
id: T({ prefix: "idem" }),
|
|
1456
|
+
scope: o(),
|
|
1457
|
+
key: o(),
|
|
1458
|
+
status: o(),
|
|
1459
|
+
response: E().nullable(),
|
|
1460
|
+
createdAt: v().default("now")
|
|
1461
|
+
}).unique(["scope", "key"]).index(["createdAt"]).reactive(), Di = n("_voltro_kv", {
|
|
1462
|
+
id: T({ prefix: "kv" }),
|
|
1463
|
+
key: o().unique(),
|
|
1464
|
+
value: o().nullable(),
|
|
1465
|
+
expiresAt: v().nullable(),
|
|
1466
|
+
createdAt: v().default("now"),
|
|
1467
|
+
updatedAt: v().default("now").onUpdate("now")
|
|
1468
|
+
}).index(["expiresAt"]).reactive(), Oi = n("_voltro_api_keys", {
|
|
1469
|
+
id: T({ prefix: "apikey" }),
|
|
1470
|
+
tenantId: o().nullable(),
|
|
1471
|
+
name: o(),
|
|
1472
|
+
hashedKey: o().unique(),
|
|
1473
|
+
keyPrefix: o(),
|
|
1474
|
+
scopes: E().nullable(),
|
|
1475
|
+
lastUsedAt: v().nullable(),
|
|
1476
|
+
expiresAt: v().nullable(),
|
|
1477
|
+
revokedAt: v().nullable(),
|
|
1478
|
+
createdBy: o().nullable(),
|
|
1479
|
+
createdAt: v().default("now")
|
|
1480
|
+
}).index("byTenant", ["tenantId"]).reactive(), ki = [
|
|
1481
|
+
Ci,
|
|
1482
|
+
wi,
|
|
1483
|
+
Ti
|
|
1484
|
+
], Ai = (e, t, n) => ({
|
|
1485
|
+
_tag: "PrimaryKeyConflictError",
|
|
1486
|
+
table: e,
|
|
1487
|
+
key: t,
|
|
1488
|
+
cause: n
|
|
1489
|
+
}), ji = (e) => typeof e == "object" && !!e && e._tag === "PrimaryKeyConflictError", Mi = /* @__PURE__ */ new Map(), Ni = (e) => {
|
|
1490
|
+
if (!e.table || !e.timeColumn) throw Error("registerRetention: table + timeColumn are required");
|
|
1491
|
+
if (!Number.isFinite(e.ttlMs) || e.ttlMs <= 0) throw Error(`registerRetention(${e.table}): ttlMs must be a positive number`);
|
|
1492
|
+
Mi.set(e.table, e);
|
|
1493
|
+
}, Pi = () => [...Mi.values()], Fi = () => Mi.clear(), Ii = async (e, t, n, r = 2e4) => {
|
|
1494
|
+
let i = new Date(t - e.ttlMs), a = 0;
|
|
1495
|
+
for (;;) {
|
|
1496
|
+
let t = await n(e.table, e.timeColumn, i, r, e.where);
|
|
1497
|
+
if (a += t, t < r) break;
|
|
1498
|
+
}
|
|
1499
|
+
return a;
|
|
1500
|
+
}, Li = (e, t) => {
|
|
1501
|
+
let n = Number(e ?? String(t));
|
|
1502
|
+
return (Number.isFinite(n) && n > 0 ? n : t) * 36e5;
|
|
1503
|
+
}, Ri = "postgres";
|
|
1504
|
+
//#endregion
|
|
1505
|
+
export { Y as BranchNameInvalid, Ir as BranchTransitionInvalid, We as ColumnBuilder, lt as ENCRYPTED_PREFIX, Ri as ENGINE, Ge as FILE_MIGRATION_PATTERN, oe as MAX_IDENTIFIER_LENGTH, wr as TableStreamError, Jt as TenantNamespaceInvalid, qt as TenantNamespaceUnresolved, Qr as TenantRegionUnavailable, Zr as TenantResidencyUnresolved, ce as WARN_IDENTIFIER_LENGTH, Oi as _voltroApiKeysTable, Ei as _voltroIdempotencyTable, Di as _voltroKvTable, Ti as _voltroMigrationPlansTable, Ci as _voltroMigrationsTable, wi as _voltroSeedsTable, Yn as actorsTable, Yr as admitBranch, He as allEnumRenames, kt as allRegisteredRelations, De as allRegisteredTables, dr as and, ge as array, ar as arrayContains, sr as arrayHas, or as arrayOverlaps, zt as attachEagerLoads, di as auditAllTableIndexes, ui as auditTableIndexes, Ae as avg, qe as avgOver, je as bigint, oi as bindResidentStore, Je as boolean, Nr as branchNamespaceName, ve as bytes, Ee as clearEnumRenames, At as clearRelationsRegistry, ni as clearResidencyConfig, Fi as clearRetentions, g as clearTableRegistry, Sr as collectSubqueries, _e as column, dn as compileEagerJson, R as compilePredicate, $t as compileRawFragment, z as compileSelect, kr as computeEmbedding, ir as contains, be as count, Re as countDistinct, Me as databaseHandle, xe as date, ze as dbEnum, Ne as decimal, fe as declareEnumRename, It as decodeRowsFromSchema, pt as decryptFieldsOnRead, xi as defineMigration, d as defineMixin, vi as defineSeed, we as denseRank, u as deriveTypeIdPrefix, ct as deserializeArraysOnRead, b as drainIdentifierWarnings, Te as dropped, Rt as encodeRowForSchema, ft as encryptFieldsForWrite, dt as encryptedColumnsOf, Kr as enforceBranchLimits, _ as ensureTableRegistered, Xn as eq, L as escapeLike, xr as evaluatePredicate, Se as except, vr as exists, gi as formatIndexAuditIssue, ki as frameworkTables, m as generateId, p as generateSnowflake, Ce as geography, ue as geometry, Le as getEnumRenames, Ot as getRelation, M as getRelations, ti as getResidencyConfig, y as getTable, Qn as gt, $n as gte, Kt as hasEagerLoads, Or as hybridSearch, T as id, nr as inSet, gr as inSubquery, A as inferForeignKey, D as integer, le as intersect, me as interval, Pr as isBranchNamespace, ut as isEncrypted, Pe as isFileMigration, Si as isMigrationDefinition, Br as isNeonConnection, lr as isNotNull, cr as isNull, ji as isPrimaryKeyConflictError, ii as isRegionServable, bt as isRelationsSpec, yi as isSeedDefinition, h as isSqliteFamily, de as isView, E as json, mr as jsonField, et as jsonIndex, Ye as lag, $e as lead, Pi as listRetentions, er as lt, tr as lte, Xr as makeNamespaceBranchExecutor, St as many, Ct as manyToMany, S as materializeFields, pe as max, Ve as migration, Qe as min, re as mixin, Zn as neq, Lr as nextBranchState, pr as not, yr as notExists, rr as notInSet, _r as notInSubquery, i as numeric, xt as one, fr as or, te as paginateById, Wr as planBranch, Rr as planBranchProvision, zr as planBranchTeardown, Gr as planBranchTeardownFor, Hr as planNeonBranchProvision, Ur as planNeonBranchTeardown, Ai as primaryKeyConflictError, qr as provisionBranch, si as provisionResidentTenant, I as qualifyTable, r as queryFor, Ie as queryForView, x as quoteIdent, Xe as rank, Ze as raw, ne as real, c as reference, Gn as registerCoreTables, Dt as registerDiscoveredRelations, Et as registerRelations, Ni as registerRetention, l as registerTable, yt as relations, qn as requireActors, f as requireTable, Jn as requireTenants, se as resetSnowflake, ai as residentPlacement, Vr as resolveBranchMechanism, C as resolveFullTextIndex, Be as resolveIdScheme, ae as resolveMixinGraph, ri as resolveTenantHome, Qt as resolveTenantNamespace, Li as retentionTtlMsFromEnv, s as rowNumber, Zt as sanitizeIdentifierFragment, st as serializeArraysForWrite, ei as setResidencyConfig, ur as spatialPredicate, Dr as streamTable, a as sum, ee as sumOver, Ii as sweepRetention, n as table, Jr as teardownBranch, o as text, v as timestamp, Oe as union, t as unionAll, e as validateColumnName, ye as validateIndexName, Ke as validateTableName, Fe as validateTypeIdPrefix, ke as vector, ie as vectorDistanceToOpclass, Ar as vectorEmbedding, Ue as view, he as viewSql, Cr as walkLeaves };
|