@voltro/database 0.83.1 → 0.85.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 +1191 -0
- package/THIRD-PARTY-NOTICES.md +1 -57
- package/dist/index.d.ts +171 -90
- package/dist/index.js +816 -710
- package/dist/replaceInProgress-DC88Pdiy.js +3735 -0
- package/dist/sql.d.ts +226 -22
- package/dist/sql.js +3050 -1722
- package/dist/{wire-B28cPjEp.js → wire-CG2j7J3f.js} +1 -1
- package/dist/wire.d.ts +9 -10
- package/dist/wire.js +1 -1
- package/package.json +3 -5
- package/dist/replaceInProgress-Z1aAw3GD.js +0 -2365
|
@@ -1,2365 +0,0 @@
|
|
|
1
|
-
import { Cause as e, Effect as t, Runtime as n, Schema as r } from "effect";
|
|
2
|
-
import { typeid as i } from "typeid-js";
|
|
3
|
-
import { ulid as a } from "ulidx";
|
|
4
|
-
//#region src/columns.ts
|
|
5
|
-
var o = (e) => {
|
|
6
|
-
let t = [];
|
|
7
|
-
for (let [n, r] of Object.entries(e.fields)) r.serverOnly === !0 && t.push(n);
|
|
8
|
-
return t;
|
|
9
|
-
}, s = (e) => {
|
|
10
|
-
let t = [];
|
|
11
|
-
for (let [n, r] of Object.entries(e.fields)) r.crdtManaged === !0 && t.push(n);
|
|
12
|
-
return t;
|
|
13
|
-
}, c = (e) => {
|
|
14
|
-
let t = [];
|
|
15
|
-
for (let [n, r] of Object.entries(e.fields)) {
|
|
16
|
-
let e = r.readableBy;
|
|
17
|
-
e !== void 0 && e.length > 0 && t.push({
|
|
18
|
-
column: n,
|
|
19
|
-
scopes: e
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
return t;
|
|
23
|
-
}, l = (e) => {
|
|
24
|
-
let t = [];
|
|
25
|
-
for (let [n, r] of Object.entries(e.fields)) r.crdtManaged === !0 && t.push(n);
|
|
26
|
-
return t;
|
|
27
|
-
}, u = class e {
|
|
28
|
-
definition;
|
|
29
|
-
constructor(e) {
|
|
30
|
-
this.definition = e;
|
|
31
|
-
}
|
|
32
|
-
rejectIfRaw(e) {
|
|
33
|
-
if (this.definition.type === "raw") throw Error(`raw(...) column rejects .${e}() — the DDL fragment is the single source of truth for type/nullability/default. Encode ${e === "nullable" ? "NULL" : e === "unique" ? "UNIQUE" : "DEFAULT"} directly in the raw fragment instead.`);
|
|
34
|
-
}
|
|
35
|
-
nullable() {
|
|
36
|
-
return this.rejectIfRaw("nullable"), new e({
|
|
37
|
-
...this.definition,
|
|
38
|
-
nullable: !0
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
externalId() {
|
|
42
|
-
if (this.rejectIfRaw("externalId"), this.definition.type === "reference" || this.definition.type === "id") throw Error(`.externalId() requires a scalar non-primary-key column (got ${this.definition.type}) — \`reference()\` is local by definition and \`id()\` identifies this row.`);
|
|
43
|
-
return new e({
|
|
44
|
-
...this.definition,
|
|
45
|
-
externalId: !0
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
encrypted() {
|
|
49
|
-
return this.rejectIfRaw("encrypted"), new e({
|
|
50
|
-
...this.definition,
|
|
51
|
-
encrypted: !0
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
serverOnly() {
|
|
55
|
-
if (this.rejectIfRaw("serverOnly"), this.definition.crdtManaged === !0) throw Error("serverOnly() on a CRDT column (crdtText()/crdtDoc()) is a contradiction: clients write CRDT updates and must read the merged state back. Remove serverOnly(), or use a plain bytes() column if only the server touches it.");
|
|
56
|
-
return new e({
|
|
57
|
-
...this.definition,
|
|
58
|
-
serverOnly: !0
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
readableBy(...t) {
|
|
62
|
-
if (this.rejectIfRaw("readableBy"), t.length === 0) throw Error(".readableBy() needs at least one scope — a column readable by nobody is .serverOnly(). List the scopes that may read it, e.g. .readableBy('billing:read').");
|
|
63
|
-
if (t.find((e) => typeof e != "string" || e.trim() === "") !== void 0) throw Error(`.readableBy(...) rejects a blank scope — every scope must be a non-empty string (a blank scope can never be held, so it would hide the column from everyone). Got: ${JSON.stringify(t)}.`);
|
|
64
|
-
let n = [...new Set(t)];
|
|
65
|
-
return new e({
|
|
66
|
-
...this.definition,
|
|
67
|
-
readableBy: n
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
version() {
|
|
71
|
-
if (this.rejectIfRaw("version"), this.definition.type !== "integer") throw Error(`.version() requires an integer() column (got ${this.definition.type}). The store increments it, so it has to be a number — and a timestamp version reintroduces exactly the clock problem optimistic locking exists to avoid: two writes in the same millisecond are indistinguishable, and replica clocks disagree.`);
|
|
72
|
-
return new e({
|
|
73
|
-
...this.definition,
|
|
74
|
-
versionColumn: !0,
|
|
75
|
-
hasDefault: !0,
|
|
76
|
-
defaultValue: 1
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
sensitive(t) {
|
|
80
|
-
return new e({
|
|
81
|
-
...this.definition,
|
|
82
|
-
sensitive: { class: t }
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
safe() {
|
|
86
|
-
return new e({
|
|
87
|
-
...this.definition,
|
|
88
|
-
safe: !0
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
unique(t) {
|
|
92
|
-
return this.rejectIfRaw("unique"), t?.dedup === void 0 ? new e({
|
|
93
|
-
...this.definition,
|
|
94
|
-
unique: !0
|
|
95
|
-
}) : new e({
|
|
96
|
-
...this.definition,
|
|
97
|
-
unique: !0,
|
|
98
|
-
uniqueSpec: { dedup: t.dedup }
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
default(t) {
|
|
102
|
-
return this.rejectIfRaw("default"), typeof t == "function" ? new e({
|
|
103
|
-
...this.definition,
|
|
104
|
-
hasDefault: !0,
|
|
105
|
-
defaultFactory: t
|
|
106
|
-
}) : new e({
|
|
107
|
-
...this.definition,
|
|
108
|
-
hasDefault: !0,
|
|
109
|
-
defaultValue: t
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
computed(t) {
|
|
113
|
-
return new e({
|
|
114
|
-
...this.definition,
|
|
115
|
-
computed: t
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
onUpdate(t) {
|
|
119
|
-
return new e({
|
|
120
|
-
...this.definition,
|
|
121
|
-
onUpdate: t
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
generatedAs(t, n) {
|
|
125
|
-
return new e({
|
|
126
|
-
...this.definition,
|
|
127
|
-
generatedAs: {
|
|
128
|
-
expr: t,
|
|
129
|
-
stored: n?.stored ?? !1
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
backfill(t, n) {
|
|
134
|
-
let r = typeof t == "function" ? {
|
|
135
|
-
kind: "js",
|
|
136
|
-
fn: t,
|
|
137
|
-
...n?.batchSize === void 0 ? {} : { batchSize: n.batchSize },
|
|
138
|
-
...n?.sleepMs === void 0 ? {} : { sleepMs: n.sleepMs }
|
|
139
|
-
} : {
|
|
140
|
-
kind: "sql",
|
|
141
|
-
sql: t,
|
|
142
|
-
...n?.batchSize === void 0 ? {} : { batchSize: n.batchSize },
|
|
143
|
-
...n?.sleepMs === void 0 ? {} : { sleepMs: n.sleepMs }
|
|
144
|
-
};
|
|
145
|
-
return new e({
|
|
146
|
-
...this.definition,
|
|
147
|
-
backfill: r
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
renamedFrom(t) {
|
|
151
|
-
return new e({
|
|
152
|
-
...this.definition,
|
|
153
|
-
renamedFrom: t
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
narrowedFrom(t, n) {
|
|
157
|
-
let r = n?.using === void 0 ? { from: t } : {
|
|
158
|
-
from: t,
|
|
159
|
-
using: n.using
|
|
160
|
-
};
|
|
161
|
-
return new e({
|
|
162
|
-
...this.definition,
|
|
163
|
-
narrowedFrom: r
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
oneOf(t) {
|
|
167
|
-
return new e({
|
|
168
|
-
...this.definition,
|
|
169
|
-
oneOf: t
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
maxLength(t) {
|
|
173
|
-
return new e({
|
|
174
|
-
...this.definition,
|
|
175
|
-
maxLength: t
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
check(t) {
|
|
179
|
-
let n = this.definition.checks ? [...this.definition.checks, t] : [t];
|
|
180
|
-
return new e({
|
|
181
|
-
...this.definition,
|
|
182
|
-
checks: n
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
__definition() {
|
|
186
|
-
return this.definition;
|
|
187
|
-
}
|
|
188
|
-
}, d = (e) => new u({
|
|
189
|
-
type: e,
|
|
190
|
-
nullable: !1,
|
|
191
|
-
unique: !1,
|
|
192
|
-
hasDefault: !1
|
|
193
|
-
});
|
|
194
|
-
function f(e) {
|
|
195
|
-
return new u({
|
|
196
|
-
type: "id",
|
|
197
|
-
nullable: !1,
|
|
198
|
-
unique: !0,
|
|
199
|
-
hasDefault: !1,
|
|
200
|
-
idSchemeInput: e
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
var p = () => d("text"), m = () => d("integer"), h = () => d("real"), g = (e, t = 0) => {
|
|
204
|
-
if (!Number.isInteger(e) || e <= 0) throw Error(`decimal(${String(e)}, ${String(t)}): precision must be a positive integer (the total number of significant digits, e.g. decimal(12, 2)).`);
|
|
205
|
-
if (!Number.isInteger(t) || t < 0 || t > e) throw Error(`decimal(${String(e)}, ${String(t)}): scale must be an integer between 0 and precision (${e}).`);
|
|
206
|
-
return new u({
|
|
207
|
-
type: "decimal",
|
|
208
|
-
nullable: !1,
|
|
209
|
-
unique: !1,
|
|
210
|
-
hasDefault: !1,
|
|
211
|
-
precision: e,
|
|
212
|
-
scale: t
|
|
213
|
-
});
|
|
214
|
-
}, ee = g, te = () => d("bigint"), ne = (e, t) => {
|
|
215
|
-
if (t.length === 0) throw Error(`dbEnum('${e}', []): enum must have at least one value`);
|
|
216
|
-
return {
|
|
217
|
-
name: e,
|
|
218
|
-
values: t,
|
|
219
|
-
column: () => new u({
|
|
220
|
-
type: "enum",
|
|
221
|
-
nullable: !1,
|
|
222
|
-
unique: !1,
|
|
223
|
-
hasDefault: !1,
|
|
224
|
-
enumName: e,
|
|
225
|
-
enumValues: t
|
|
226
|
-
})
|
|
227
|
-
};
|
|
228
|
-
}, _ = () => d("boolean"), v = () => d("timestamp"), re = () => d("date"), y = () => d("json"), b = () => d("bytes"), x = () => new u({
|
|
229
|
-
type: "bytes",
|
|
230
|
-
nullable: !0,
|
|
231
|
-
unique: !1,
|
|
232
|
-
hasDefault: !1,
|
|
233
|
-
crdtManaged: !0
|
|
234
|
-
}), ie = () => new u({
|
|
235
|
-
type: "bytes",
|
|
236
|
-
nullable: !0,
|
|
237
|
-
unique: !1,
|
|
238
|
-
hasDefault: !1,
|
|
239
|
-
crdtManaged: !0
|
|
240
|
-
}), S = () => {
|
|
241
|
-
throw Error("selfReference(): the target is resolved by table() — a column built with selfReference() must be declared inside a table({ … }) literal, not read before the table exists");
|
|
242
|
-
};
|
|
243
|
-
function ae(e) {
|
|
244
|
-
return oe(S, e);
|
|
245
|
-
}
|
|
246
|
-
function oe(e, t) {
|
|
247
|
-
return new u({
|
|
248
|
-
type: "reference",
|
|
249
|
-
nullable: !1,
|
|
250
|
-
unique: !1,
|
|
251
|
-
hasDefault: !1,
|
|
252
|
-
references: e,
|
|
253
|
-
referenceStorage: t?.storage ?? "text",
|
|
254
|
-
referenceColumn: t?.column ?? "id",
|
|
255
|
-
onDelete: t?.onDelete ?? "restrict",
|
|
256
|
-
refOnUpdate: t?.onUpdate ?? "noAction",
|
|
257
|
-
...t?.onSoftDelete === void 0 ? {} : { refOnSoftDelete: t.onSoftDelete },
|
|
258
|
-
fkAutoIndex: t?.index ?? !0,
|
|
259
|
-
...t?.orphanPolicy ? { orphanPolicy: t.orphanPolicy } : {}
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
var se = () => new u({
|
|
263
|
-
type: "text",
|
|
264
|
-
nullable: !0,
|
|
265
|
-
unique: !1,
|
|
266
|
-
hasDefault: !1,
|
|
267
|
-
dropped: !0
|
|
268
|
-
}), ce = (e) => Object.keys(e.fields).filter((t) => e.fields[t]?.dropped !== !0), le = (e, t) => {
|
|
269
|
-
if (!Number.isInteger(e) || e <= 0) throw Error(`vector(${String(e)}): dimension must be a positive integer (the embedding length, e.g. vector(1536)).`);
|
|
270
|
-
return new u({
|
|
271
|
-
type: "vector",
|
|
272
|
-
nullable: !1,
|
|
273
|
-
unique: !1,
|
|
274
|
-
hasDefault: !1,
|
|
275
|
-
vectorDim: e,
|
|
276
|
-
vectorPrecision: t?.precision ?? "float32"
|
|
277
|
-
});
|
|
278
|
-
}, ue = (e) => new u({
|
|
279
|
-
type: "array",
|
|
280
|
-
nullable: !1,
|
|
281
|
-
unique: !1,
|
|
282
|
-
hasDefault: !1,
|
|
283
|
-
arrayElement: e.__definition().type
|
|
284
|
-
}), de = () => d("interval"), fe = (e, t = 4326) => new u({
|
|
285
|
-
type: "text",
|
|
286
|
-
nullable: !1,
|
|
287
|
-
unique: !1,
|
|
288
|
-
hasDefault: !1,
|
|
289
|
-
spatial: {
|
|
290
|
-
kind: "geography",
|
|
291
|
-
geomKind: e,
|
|
292
|
-
srid: t
|
|
293
|
-
}
|
|
294
|
-
}), pe = (e, t = 4326) => new u({
|
|
295
|
-
type: "text",
|
|
296
|
-
nullable: !1,
|
|
297
|
-
unique: !1,
|
|
298
|
-
hasDefault: !1,
|
|
299
|
-
spatial: {
|
|
300
|
-
kind: "geometry",
|
|
301
|
-
geomKind: e,
|
|
302
|
-
srid: t
|
|
303
|
-
}
|
|
304
|
-
}), me = (e) => new u({
|
|
305
|
-
type: "raw",
|
|
306
|
-
nullable: !1,
|
|
307
|
-
unique: !1,
|
|
308
|
-
hasDefault: !1,
|
|
309
|
-
rawDdl: e
|
|
310
|
-
}), C = (e) => {
|
|
311
|
-
let t = {};
|
|
312
|
-
for (let [n, r] of Object.entries(e)) t[n] = r.__definition();
|
|
313
|
-
return t;
|
|
314
|
-
}, he = /* @__PURE__ */ new WeakMap(), ge = (e) => typeof e == "object" && e ? he.get(e) : void 0, _e = (e, t = {}) => {
|
|
315
|
-
let n = p();
|
|
316
|
-
return he.set(n, {
|
|
317
|
-
target: e,
|
|
318
|
-
orphanPolicy: t.orphanPolicy ?? "keep",
|
|
319
|
-
onSoftDelete: t.onSoftDelete ?? !1
|
|
320
|
-
}), n;
|
|
321
|
-
}, w = /* @__PURE__ */ new Map(), ve = (e, t) => {
|
|
322
|
-
if (t.from === t.to) throw Error(`declareEnumRename('${e}'): \`from\` and \`to\` are both '${t.from}' — a rename must change the value.`);
|
|
323
|
-
let n = w.get(e) ?? [];
|
|
324
|
-
for (let r of n) {
|
|
325
|
-
if (r.from === t.from && r.to === t.to) return;
|
|
326
|
-
if (r.from === t.from && r.to !== t.to) throw Error(`declareEnumRename('${e}'): value '${t.from}' is already declared as renamed to '${r.to}', cannot also rename it to '${t.to}'.`);
|
|
327
|
-
}
|
|
328
|
-
n.push(t), w.set(e, n);
|
|
329
|
-
}, ye = (e) => w.get(e) ?? [], be = () => w, xe = () => {
|
|
330
|
-
w.clear();
|
|
331
|
-
}, Se = (t) => {
|
|
332
|
-
let r = t;
|
|
333
|
-
for (let t = 0; t < 4 && n.isFiberFailure(r); t++) r = e.squash(r[n.FiberFailureCauseId]);
|
|
334
|
-
return r;
|
|
335
|
-
}, T = (t, r = 0) => {
|
|
336
|
-
if (r >= 4 || !n.isFiberFailure(t)) return [t];
|
|
337
|
-
let i = t[n.FiberFailureCauseId], a = [...e.failures(i), ...e.defects(i)];
|
|
338
|
-
if (a.length === 0) return [e.squash(i)];
|
|
339
|
-
let o = [];
|
|
340
|
-
for (let e of a.slice(0, 8)) o.push(...T(e, r + 1));
|
|
341
|
-
return o;
|
|
342
|
-
}, Ce = (e) => e.dbCode !== void 0 || e.dbErrno !== void 0 || e.dbNumber !== void 0 || e.dbSqlState !== void 0 || e.dbConstraint !== void 0, we = (e) => {
|
|
343
|
-
let t = {}, n = [], r = Se(e);
|
|
344
|
-
for (let e = 0; e < 8 && typeof r == "object" && r; e++) {
|
|
345
|
-
let e = r;
|
|
346
|
-
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.dbBindings === void 0 && typeof e.voltroBindings == "string" && (t.dbBindings = e.voltroBindings), t.dbStatement === void 0 && typeof e.sql == "string" && e.sql.includes("?") && (t.dbStatement = e.sql, t.dbPlaceholders = (e.sql.match(/\?/g) ?? []).length), 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);
|
|
347
|
-
let i = e.sqlMessage ?? e.detail;
|
|
348
|
-
t.dbMessage === void 0 && typeof i == "string" && (t.dbMessage = i);
|
|
349
|
-
let a = typeof e.constructor == "function" ? e.constructor.name : void 0, o = typeof e._tag == "string" && e._tag || typeof e.name == "string" && e.name || (typeof a == "string" && a !== "Object" ? a : void 0), s = typeof e.message == "string" ? e.message : void 0;
|
|
350
|
-
(o || s) && n.push(`${o || "Error"}${s ? `: ${s.slice(0, 200)}` : ""}`), r = Se(e.cause);
|
|
351
|
-
}
|
|
352
|
-
return n.length > 0 && (t.dbCauseChain = n.join(" ← "), t.dbMessage === void 0 && (t.dbMessage = n[n.length - 1])), t;
|
|
353
|
-
}, Te = (e) => {
|
|
354
|
-
let t = T(e).map(we);
|
|
355
|
-
if (t.length <= 1) return t[0] ?? {};
|
|
356
|
-
let n = t.find(Ce) ?? t[0], r = t.filter((e) => e !== n).map((e) => e.dbCauseChain).filter((e) => typeof e == "string");
|
|
357
|
-
if (r.length === 0) return n;
|
|
358
|
-
let i = typeof n.dbCauseChain == "string" ? n.dbCauseChain : void 0;
|
|
359
|
-
return {
|
|
360
|
-
...n,
|
|
361
|
-
dbCauseChain: [i, ...r].filter(Boolean).join(" ‖ ")
|
|
362
|
-
};
|
|
363
|
-
}, Ee = (e, t) => {
|
|
364
|
-
let n = Oe(e);
|
|
365
|
-
if (n !== void 0) return n;
|
|
366
|
-
let r = Te(e), i = Pe(r, t);
|
|
367
|
-
if (i !== void 0) {
|
|
368
|
-
let e = i.constraint ?? i.column;
|
|
369
|
-
return e === void 0 ? i.kind : `${i.kind}: ${e}`;
|
|
370
|
-
}
|
|
371
|
-
let a = r.dbCode ?? r.dbSqlState ?? r.dbErrno ?? r.dbNumber, o = je(e);
|
|
372
|
-
return a !== void 0 && ke.test(String(a)) ? `${o} (${String(a)})` : o;
|
|
373
|
-
}, De = Symbol.for("@voltro/database/authoredMessage"), E = (e, t) => {
|
|
374
|
-
let n = t === void 0 ? Error(e) : Error(e, t);
|
|
375
|
-
return Object.defineProperty(n, De, {
|
|
376
|
-
value: !0,
|
|
377
|
-
enumerable: !1
|
|
378
|
-
}), n;
|
|
379
|
-
}, Oe = (e) => {
|
|
380
|
-
for (let t of T(e)) {
|
|
381
|
-
let e = t;
|
|
382
|
-
for (let t = 0; typeof e == "object" && e && t < 8; t += 1) {
|
|
383
|
-
if (e[De] === !0) {
|
|
384
|
-
let t = e.message;
|
|
385
|
-
if (typeof t == "string" && t.length > 0) return t;
|
|
386
|
-
}
|
|
387
|
-
e = e.cause;
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
}, ke = /^[A-Za-z0-9_]{1,32}$/, Ae = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/, je = (e) => {
|
|
391
|
-
let t = e?._tag, n = e?.name, r = (typeof t == "string" ? t : typeof n == "string" ? n : "").split(/[^A-Za-z0-9_]+/).filter(Boolean).pop();
|
|
392
|
-
return r !== void 0 && Ae.test(r) ? r : "unknown";
|
|
393
|
-
}, Me = /^[A-Za-z_][A-Za-z0-9_$]*$/, D = (e) => e !== void 0 && e.length <= 128 && Me.test(e) ? e : void 0, Ne = (e) => e === void 0 ? void 0 : e.split(".").pop() ?? e, O = (e, ...t) => {
|
|
394
|
-
for (let n of t) {
|
|
395
|
-
let t = n.exec(e);
|
|
396
|
-
if (t?.[1] !== void 0) return t[1];
|
|
397
|
-
}
|
|
398
|
-
}, Pe = (e, t) => {
|
|
399
|
-
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}`;
|
|
400
|
-
if (n === "23503") {
|
|
401
|
-
let t = D(e.dbConstraint);
|
|
402
|
-
return {
|
|
403
|
-
kind: /is still referenced from table/i.test(o) ? "foreignKeyInUse" : "foreignKey",
|
|
404
|
-
...t === void 0 ? {} : { constraint: t }
|
|
405
|
-
};
|
|
406
|
-
}
|
|
407
|
-
if (n === "23505") {
|
|
408
|
-
let t = D(e.dbConstraint);
|
|
409
|
-
return {
|
|
410
|
-
kind: "unique",
|
|
411
|
-
...t === void 0 ? {} : { constraint: t }
|
|
412
|
-
};
|
|
413
|
-
}
|
|
414
|
-
if (n === "23502") {
|
|
415
|
-
let t = D(e.dbColumn);
|
|
416
|
-
return {
|
|
417
|
-
kind: "notNull",
|
|
418
|
-
...t === void 0 ? {} : { column: t }
|
|
419
|
-
};
|
|
420
|
-
}
|
|
421
|
-
if (n === "23514") {
|
|
422
|
-
let t = D(e.dbConstraint);
|
|
423
|
-
return {
|
|
424
|
-
kind: "check",
|
|
425
|
-
...t === void 0 ? {} : { constraint: t }
|
|
426
|
-
};
|
|
427
|
-
}
|
|
428
|
-
if (r === 1452 || r === 1451) {
|
|
429
|
-
let e = D(O(o, /CONSTRAINT `([^`]+)`/));
|
|
430
|
-
return {
|
|
431
|
-
kind: r === 1451 ? "foreignKeyInUse" : "foreignKey",
|
|
432
|
-
...e === void 0 ? {} : { constraint: e }
|
|
433
|
-
};
|
|
434
|
-
}
|
|
435
|
-
if (r === 1062) {
|
|
436
|
-
let e = D(Ne(O(o, /for key '([^']+)'/)));
|
|
437
|
-
return {
|
|
438
|
-
kind: "unique",
|
|
439
|
-
...e === void 0 ? {} : { constraint: e }
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
|
-
if (r === 1048 || r === 1364) {
|
|
443
|
-
let e = D(O(o, /Column '([^']+)' cannot be null/, /Field '([^']+)' doesn't have a default value/));
|
|
444
|
-
return {
|
|
445
|
-
kind: "notNull",
|
|
446
|
-
...e === void 0 ? {} : { column: e }
|
|
447
|
-
};
|
|
448
|
-
}
|
|
449
|
-
if (r === 3819 || r === 4025) {
|
|
450
|
-
let e = D(O(o, /Check constraint '([^']+)'/, /CONSTRAINT `([^`]+)` failed/));
|
|
451
|
-
return {
|
|
452
|
-
kind: "check",
|
|
453
|
-
...e === void 0 ? {} : { constraint: e }
|
|
454
|
-
};
|
|
455
|
-
}
|
|
456
|
-
if (i === 547) {
|
|
457
|
-
let e = O(o, /(?:FOREIGN KEY|REFERENCE) constraint "([^"]+)"/), t = O(o, /CHECK constraint "([^"]+)"/);
|
|
458
|
-
if (t !== void 0 && e === void 0) {
|
|
459
|
-
let e = D(t);
|
|
460
|
-
return {
|
|
461
|
-
kind: "check",
|
|
462
|
-
...e === void 0 ? {} : { constraint: e }
|
|
463
|
-
};
|
|
464
|
-
}
|
|
465
|
-
let n = /REFERENCE constraint/.test(o), r = D(e);
|
|
466
|
-
return {
|
|
467
|
-
kind: n ? "foreignKeyInUse" : "foreignKey",
|
|
468
|
-
...r === void 0 ? {} : { constraint: r }
|
|
469
|
-
};
|
|
470
|
-
}
|
|
471
|
-
if (i === 2627 || i === 2601) {
|
|
472
|
-
let e = D(O(o, /(?:UNIQUE KEY|PRIMARY KEY) constraint '([^']+)'/, /index '([^']+)'/));
|
|
473
|
-
return {
|
|
474
|
-
kind: "unique",
|
|
475
|
-
...e === void 0 ? {} : { constraint: e }
|
|
476
|
-
};
|
|
477
|
-
}
|
|
478
|
-
if (i === 515) {
|
|
479
|
-
let e = D(O(o, /Cannot insert the value NULL into column '([^']+)'/));
|
|
480
|
-
return {
|
|
481
|
-
kind: "notNull",
|
|
482
|
-
...e === void 0 ? {} : { column: e }
|
|
483
|
-
};
|
|
484
|
-
}
|
|
485
|
-
if (n === "SQLITE_CONSTRAINT_FOREIGNKEY") return { kind: t === "delete" || t === "deleteMany" || t === "hardDelete" ? "foreignKeyInUse" : "foreignKey" };
|
|
486
|
-
if (n === "SQLITE_CONSTRAINT_UNIQUE" || n === "SQLITE_CONSTRAINT_PRIMARYKEY") {
|
|
487
|
-
let e = D(Ne(O(o, /UNIQUE constraint failed: ([^,\s]+)/)));
|
|
488
|
-
return {
|
|
489
|
-
kind: "unique",
|
|
490
|
-
...e === void 0 ? {} : { column: e }
|
|
491
|
-
};
|
|
492
|
-
}
|
|
493
|
-
if (n === "SQLITE_CONSTRAINT_NOTNULL") {
|
|
494
|
-
let e = D(Ne(O(o, /NOT NULL constraint failed: ([^,\s]+)/)));
|
|
495
|
-
return {
|
|
496
|
-
kind: "notNull",
|
|
497
|
-
...e === void 0 ? {} : { column: e }
|
|
498
|
-
};
|
|
499
|
-
}
|
|
500
|
-
if (n === "SQLITE_CONSTRAINT_CHECK") {
|
|
501
|
-
let e = D(O(a, /CHECK constraint failed: (.+)$/));
|
|
502
|
-
return {
|
|
503
|
-
kind: "check",
|
|
504
|
-
...e === void 0 ? {} : { constraint: e }
|
|
505
|
-
};
|
|
506
|
-
}
|
|
507
|
-
}, Fe = (e) => {
|
|
508
|
-
let t = Pe(e)?.kind;
|
|
509
|
-
return t === "foreignKey" || t === "foreignKeyInUse";
|
|
510
|
-
}, Ie = (e) => {
|
|
511
|
-
let t = e.dbCode, n = e.dbErrno;
|
|
512
|
-
return t === "57014" || n === 3024 || n === 1969 || t === "ETIMEOUT" || t === "SQLITE_INTERRUPT";
|
|
513
|
-
}, Le = (e) => {
|
|
514
|
-
if (e.dbErrno !== 1366) return;
|
|
515
|
-
let t = `${typeof e.dbCauseChain == "string" ? e.dbCauseChain : ""} ${typeof e.dbMessage == "string" ? e.dbMessage : ""}`;
|
|
516
|
-
if (/Incorrect string value: '\\x(F[0-4])/i.exec(t) === null) return;
|
|
517
|
-
let n = O(t, /for column [`']?(?:[^`'.]+[`']?\.[`']?)*([^`'.]+)[`']? at row/);
|
|
518
|
-
return `${n === void 0 ? "the column" : `column '${n}'`} is stored in a character set that cannot hold a 4-byte character (utf8mb3). Declare the store's character set in app.config.ts — store: { dialect: 'mariadb' | 'mysql', charset: 'utf8mb4' } — and run \`voltro db plan\`: every declared table is created with it and converted to it.`;
|
|
519
|
-
}, Re = (e) => {
|
|
520
|
-
let t = Te(e), n = [
|
|
521
|
-
t.dbCode === void 0 ? void 0 : `code=${String(t.dbCode)}`,
|
|
522
|
-
t.dbErrno === void 0 ? void 0 : `errno=${String(t.dbErrno)}`,
|
|
523
|
-
t.dbNumber === void 0 ? void 0 : `number=${String(t.dbNumber)}`,
|
|
524
|
-
t.dbSqlState === void 0 ? void 0 : `sqlState=${String(t.dbSqlState)}`,
|
|
525
|
-
t.dbConstraint === void 0 ? void 0 : `constraint=${String(t.dbConstraint)}`,
|
|
526
|
-
t.dbPlaceholders === void 0 ? void 0 : `placeholders=${String(t.dbPlaceholders)}`
|
|
527
|
-
].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, o = typeof t.dbBindings == "string" ? `\n bindings: ${t.dbBindings}` : "", s = typeof t.dbStatement == "string" ? `\n statement: ${t.dbStatement.length <= 400 ? t.dbStatement : `${t.dbStatement.slice(0, 400)}…`}` : "";
|
|
528
|
-
if (a === void 0 && n.length === 0) return;
|
|
529
|
-
let c = Le(t), l = c === void 0 ? "" : `\n hint: ${c}`;
|
|
530
|
-
return [a, n.length > 0 ? `[${n.join(" ")}]` : void 0].filter((e) => e !== void 0 && e !== "").join(" ") + l + s + o;
|
|
531
|
-
}, ze = (e) => Object.entries(e).map(([e, t]) => `${e}:${Be(t)}`).join(" "), Be = (e) => {
|
|
532
|
-
if (e === null) return "null";
|
|
533
|
-
if (e === void 0) return "undefined";
|
|
534
|
-
if (typeof e == "bigint") return "bigint";
|
|
535
|
-
if (typeof e != "object") return typeof e;
|
|
536
|
-
if (e instanceof Date) return Number.isNaN(e.getTime()) ? "Date(Invalid)" : "Date";
|
|
537
|
-
if (Array.isArray(e)) return "Array";
|
|
538
|
-
if (globalThis.Buffer !== void 0 && globalThis.Buffer.isBuffer(e)) return "Buffer";
|
|
539
|
-
let t = e.constructor?.name;
|
|
540
|
-
return typeof t == "string" && t !== "" ? t : "Object";
|
|
541
|
-
}, Ve = 63, He = 60, k = [], A = (e) => {
|
|
542
|
-
k.includes(e) || k.push(e);
|
|
543
|
-
}, Ue = () => {
|
|
544
|
-
let e = k.slice();
|
|
545
|
-
return k.length = 0, e;
|
|
546
|
-
}, We = /^[a-zA-Z_][a-zA-Z0-9_]*$/, Ge = (e, t) => {
|
|
547
|
-
if (t.length === 0) throw Error(`${e} cannot be empty.`);
|
|
548
|
-
if (!We.test(t)) throw Error(`${e} '${t}' contains characters outside [A-Za-z0-9_] or starts with a digit. Dialect-portable identifiers must match /^[a-zA-Z_][a-zA-Z0-9_]*$/.`);
|
|
549
|
-
}, j = (e) => {
|
|
550
|
-
if (Ge("table name", e), e.length > 63) throw Error(`table name '${e}' is ${e.length} chars — exceeds the 63-byte cross-dialect limit (Postgres floor). Shorten the name; long table names also cascade into auto-named indexes and FK constraints that would themselves exceed the limit.`);
|
|
551
|
-
e.length > 60 && A(`table name '${e}' is ${e.length} chars — ${63 - e.length} from the Postgres 63-byte limit. Auto-named indexes on this table will run out of room fast.`);
|
|
552
|
-
}, M = (e, t) => {
|
|
553
|
-
if (Ge("column name", t), t.length > 63) throw Error(`column '${t}' on table '${e}' is ${t.length} chars — exceeds the 63-byte cross-dialect limit (Postgres floor). Rename it.`);
|
|
554
|
-
t.length > 60 && A(`column '${t}' on table '${e}' is ${t.length} chars — close to the 63-byte limit.`);
|
|
555
|
-
}, N = (e, t, n, r) => {
|
|
556
|
-
if (Ge("index name", t), t.length > 63) throw Error(n ? `auto-named index '${t}' on table '${e}' is ${t.length} chars — exceeds the 63-byte cross-dialect limit (Postgres floor). Supply an explicit short name: .index('<shortName>', ${JSON.stringify([...r])}).` : `index name '${t}' on table '${e}' is ${t.length} chars — exceeds the 63-byte cross-dialect limit (Postgres floor). Shorten it.`);
|
|
557
|
-
t.length > 60 && A(`index '${t}' on table '${e}' is ${t.length} chars — ${63 - t.length} from the 63-byte limit. ${n ? `Consider .index('<shortName>', ${JSON.stringify([...r])}) before it grows.` : ""}`);
|
|
558
|
-
}, P = (e, t) => {
|
|
559
|
-
if (e.length === 0) throw Error(`typeid prefix on table '${t.tableName}' cannot be empty.`);
|
|
560
|
-
if (!/^[a-z][a-z0-9_]*$/.test(e)) {
|
|
561
|
-
let n = t.kind === "derived" ? "the derived prefix doesn't match typeid's required shape — supply an explicit one via id({ prefix: '...' })." : "the prefix must match typeid's required shape /^[a-z][a-z0-9_]*$/.";
|
|
562
|
-
throw Error(`typeid prefix '${e}' on table '${t.tableName}' is invalid — ${n}`);
|
|
563
|
-
}
|
|
564
|
-
if (e.length > 63) {
|
|
565
|
-
let n = t.kind === "derived" ? "shorten the table name, or supply an explicit short prefix via id({ prefix: '...' })." : "pick a shorter prefix.";
|
|
566
|
-
throw Error(`typeid prefix '${e}' on table '${t.tableName}' is ${e.length} chars — exceeds the 63-byte limit; ${n}`);
|
|
567
|
-
}
|
|
568
|
-
e.length > 60 && A(`typeid prefix '${e}' on table '${t.tableName}' is ${e.length} chars — close to the 63-byte limit. Generated ids will be even longer (prefix + '_' + 26).`);
|
|
569
|
-
}, Ke = 1704067200000n, qe = 10n, Je = 12n, Ye = (1n << qe) - 1n, Xe = (1n << Je) - 1n, Ze = Je, Qe = 22n, $e = () => {
|
|
570
|
-
let e = process.env.SNOWFLAKE_MACHINE_ID;
|
|
571
|
-
if (e === void 0 || e === "") return 0n;
|
|
572
|
-
let t = BigInt(e);
|
|
573
|
-
if (t < 0n || t > Ye) throw Error(`SNOWFLAKE_MACHINE_ID out of range: ${e}. Must be 0..${Ye}.`);
|
|
574
|
-
return t;
|
|
575
|
-
}, et = () => ({
|
|
576
|
-
lastMs: 0n,
|
|
577
|
-
sequence: 0n,
|
|
578
|
-
machineId: $e()
|
|
579
|
-
}), F, tt = () => (F === void 0 && (F = et()), F), nt = () => {
|
|
580
|
-
F = et();
|
|
581
|
-
}, rt = () => BigInt(Date.now()), it = (e) => {
|
|
582
|
-
let t = rt();
|
|
583
|
-
for (; t <= e;) t = rt();
|
|
584
|
-
return t;
|
|
585
|
-
}, at = () => {
|
|
586
|
-
let e = tt(), t = rt();
|
|
587
|
-
t === e.lastMs ? (e.sequence = e.sequence + 1n & Xe, e.sequence === 0n && (t = it(e.lastMs), e.lastMs = t)) : (e.sequence = 0n, e.lastMs = t);
|
|
588
|
-
let n = t - Ke;
|
|
589
|
-
if (n < 0n) throw Error("Snowflake timestamp before configured epoch (2024-01-01). System clock is wrong.");
|
|
590
|
-
return (n << Qe | e.machineId << Ze | e.sequence).toString();
|
|
591
|
-
}, ot = (e) => {
|
|
592
|
-
let t = e.toLowerCase();
|
|
593
|
-
if (t.startsWith("_")) throw Error(`cannot derive a typeid prefix for table '${e}' (table names starting with '_' are not valid typeid prefixes). Pick an explicit prefix via id({ prefix: '...' }) or use a different scheme like id({ scheme: 'numeric' }) for internal tables.`);
|
|
594
|
-
if (!/^[a-z][a-z0-9_]*$/.test(t)) throw Error(`cannot derive a typeid prefix from table name '${e}' (must match [a-z][a-z0-9_]*). Pick an explicit prefix via id({ prefix: '...' }).`);
|
|
595
|
-
let n = t.endsWith("ies") && t.length > 3 ? t.slice(0, -3) + "y" : t.endsWith("ses") || t.endsWith("xes") || t.endsWith("zes") || t.endsWith("shes") || t.endsWith("ches") ? t.slice(0, -2) : t.endsWith("s") && t.length > 1 ? t.slice(0, -1) : t;
|
|
596
|
-
return P(n, {
|
|
597
|
-
kind: "derived",
|
|
598
|
-
tableName: e
|
|
599
|
-
}), n;
|
|
600
|
-
}, st = (e, t) => {
|
|
601
|
-
if (e === void 0) return {
|
|
602
|
-
kind: "typeid",
|
|
603
|
-
prefix: ot(t)
|
|
604
|
-
};
|
|
605
|
-
if ("prefix" in e && !("scheme" in e)) return P(e.prefix, {
|
|
606
|
-
kind: "explicit",
|
|
607
|
-
tableName: t
|
|
608
|
-
}), {
|
|
609
|
-
kind: "typeid",
|
|
610
|
-
prefix: e.prefix
|
|
611
|
-
};
|
|
612
|
-
if (!("scheme" in e)) throw Error("invalid id() options: no scheme or prefix");
|
|
613
|
-
switch (e.scheme) {
|
|
614
|
-
case "typeid": {
|
|
615
|
-
let n = e.prefix ?? ot(t);
|
|
616
|
-
return e.prefix !== void 0 && P(e.prefix, {
|
|
617
|
-
kind: "explicit",
|
|
618
|
-
tableName: t
|
|
619
|
-
}), {
|
|
620
|
-
kind: "typeid",
|
|
621
|
-
prefix: n
|
|
622
|
-
};
|
|
623
|
-
}
|
|
624
|
-
case "ulid": return { kind: "ulid" };
|
|
625
|
-
case "numeric": return { kind: "numeric" };
|
|
626
|
-
case "snowflake": return { kind: "snowflake" };
|
|
627
|
-
case "custom": return {
|
|
628
|
-
kind: "custom",
|
|
629
|
-
generate: e.generate
|
|
630
|
-
};
|
|
631
|
-
}
|
|
632
|
-
}, ct = (e, t) => {
|
|
633
|
-
switch (e.kind) {
|
|
634
|
-
case "typeid": return i(e.prefix).toString();
|
|
635
|
-
case "ulid": return a();
|
|
636
|
-
case "numeric": return null;
|
|
637
|
-
case "snowflake": return at();
|
|
638
|
-
case "custom": return e.generate(t);
|
|
639
|
-
}
|
|
640
|
-
}, lt = (e) => ({
|
|
641
|
-
fields: C(e.fields),
|
|
642
|
-
...e.id === void 0 ? {} : { id: e.id },
|
|
643
|
-
...e.requires === void 0 ? {} : { requires: e.requires },
|
|
644
|
-
...e.indexes === void 0 ? {} : { indexes: e.indexes },
|
|
645
|
-
...e.behaviors === void 0 ? {} : { behaviors: e.behaviors },
|
|
646
|
-
...e.policies === void 0 ? {} : { policies: e.policies }
|
|
647
|
-
}), ut = lt, dt = (e) => {
|
|
648
|
-
let t = /* @__PURE__ */ new Set(), n = [], r = (e) => {
|
|
649
|
-
let i = e.id ?? e;
|
|
650
|
-
if (!t.has(i)) {
|
|
651
|
-
if (t.add(i), e.requires !== void 0) for (let t of e.requires) r(t);
|
|
652
|
-
n.push(e);
|
|
653
|
-
}
|
|
654
|
-
};
|
|
655
|
-
for (let t of e) r(t);
|
|
656
|
-
return n;
|
|
657
|
-
}, ft = (e, ...t) => {
|
|
658
|
-
if (t.length === 0) throw Error(`jsonIndex("${e}"): at least one path segment is required`);
|
|
659
|
-
let n = (r) => ({
|
|
660
|
-
jsonPath: {
|
|
661
|
-
column: e,
|
|
662
|
-
path: t,
|
|
663
|
-
numeric: r
|
|
664
|
-
},
|
|
665
|
-
numeric: () => n(!0)
|
|
666
|
-
});
|
|
667
|
-
return n(!1);
|
|
668
|
-
}, pt = (e = "cosine", t = "float32") => {
|
|
669
|
-
let n = t === "half" ? "halfvec" : "vector";
|
|
670
|
-
switch (e) {
|
|
671
|
-
case "cosine": return `${n}_cosine_ops`;
|
|
672
|
-
case "l2": return `${n}_l2_ops`;
|
|
673
|
-
case "inner": return `${n}_ip_ops`;
|
|
674
|
-
}
|
|
675
|
-
}, mt = [
|
|
676
|
-
"id",
|
|
677
|
-
"tenantId",
|
|
678
|
-
"createdAt",
|
|
679
|
-
"updatedAt",
|
|
680
|
-
"createdBy",
|
|
681
|
-
"updatedBy",
|
|
682
|
-
"deletedAt",
|
|
683
|
-
"deletedBy"
|
|
684
|
-
], ht = (e) => e.id ?? `<anonymous mixin: ${Object.keys(e.fields).join(", ")}>`, gt = (e, t) => `${e}_${t.join("_")}_idx`, _t = (e, t) => `${e}_${t.join("_")}_uq`, vt = (e, t, n, r, i) => {
|
|
685
|
-
if (n !== "hnsw") return;
|
|
686
|
-
if (r.length !== 1) throw Error(`hnsw index '${t}' on table '${e}' must cover exactly one vector column — got ${r.length} fields. pgvector's HNSW access method indexes a single vector column.`);
|
|
687
|
-
let a = r[0];
|
|
688
|
-
if (typeof a != "string") throw Error(`hnsw index '${t}' on table '${e}' must reference a vector COLUMN, not an expression. pgvector's HNSW access method indexes a column directly.`);
|
|
689
|
-
let o = i[a];
|
|
690
|
-
if (o === void 0) throw Error(`hnsw index '${t}' on table '${e}' references unknown column '${a}'.`);
|
|
691
|
-
if (o.type !== "vector") throw Error(`hnsw index '${t}' on table '${e}' requires a vector() column — column '${a}' is '${o.type}'. HNSW is the pgvector approximate-nearest-neighbour method and only applies to vectors.`);
|
|
692
|
-
}, yt = (e, t) => {
|
|
693
|
-
let n;
|
|
694
|
-
for (let [r, i] of Object.entries(e)) i.type === "id" && i.idScheme === void 0 && (n === void 0 && (n = { ...e }), n[r] = {
|
|
695
|
-
...i,
|
|
696
|
-
idScheme: st(i.idSchemeInput, t)
|
|
697
|
-
});
|
|
698
|
-
return n ?? e;
|
|
699
|
-
}, I = (e) => {
|
|
700
|
-
let t = e.appliedPrimaryKey ?? [];
|
|
701
|
-
return {
|
|
702
|
-
tableName: e.tableName,
|
|
703
|
-
fields: e.fields,
|
|
704
|
-
isReactive: e.isReactive,
|
|
705
|
-
appliedMixins: e.appliedMixins,
|
|
706
|
-
appliedIndexes: e.appliedIndexes,
|
|
707
|
-
appliedUniques: e.appliedUniques,
|
|
708
|
-
appliedFullText: e.appliedFullText,
|
|
709
|
-
appliedChecks: e.appliedChecks,
|
|
710
|
-
appliedRules: e.appliedRules ?? [],
|
|
711
|
-
appliedPluginRefs: e.appliedPluginRefs ?? [],
|
|
712
|
-
appliedPrimaryKey: t,
|
|
713
|
-
...e.previousTableName === void 0 ? {} : { previousTableName: e.previousTableName },
|
|
714
|
-
...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
|
|
715
|
-
...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema },
|
|
716
|
-
fullTextIndex: ((t, n, r) => {
|
|
717
|
-
if (n.length === 0) throw Error(`fullTextIndex '${t}' on '${e.tableName}' has no columns.`);
|
|
718
|
-
if (N(e.tableName, t, !1, n), e.appliedFullText.some((e) => e.name === t)) throw Error(`duplicate full-text index '${t}' on table '${e.tableName}'.`);
|
|
719
|
-
return I({
|
|
720
|
-
...e,
|
|
721
|
-
tableName: e.tableName,
|
|
722
|
-
fields: e.fields,
|
|
723
|
-
isReactive: e.isReactive,
|
|
724
|
-
appliedMixins: e.appliedMixins,
|
|
725
|
-
appliedIndexes: e.appliedIndexes,
|
|
726
|
-
appliedUniques: e.appliedUniques,
|
|
727
|
-
appliedChecks: e.appliedChecks,
|
|
728
|
-
appliedPrimaryKey: e.appliedPrimaryKey,
|
|
729
|
-
appliedFullText: [...e.appliedFullText, {
|
|
730
|
-
name: t,
|
|
731
|
-
columns: [...n],
|
|
732
|
-
...r?.config === void 0 ? {} : { config: r.config },
|
|
733
|
-
...r?.weights === void 0 ? {} : { weights: r.weights }
|
|
734
|
-
}],
|
|
735
|
-
...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
|
|
736
|
-
...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
|
|
737
|
-
});
|
|
738
|
-
}),
|
|
739
|
-
with: ((...t) => {
|
|
740
|
-
let n = dt(t), r = { ...e.fields }, i = /* @__PURE__ */ new Map();
|
|
741
|
-
for (let e of Object.keys(r)) i.set(e, {
|
|
742
|
-
id: "<table>",
|
|
743
|
-
fields: {}
|
|
744
|
-
});
|
|
745
|
-
for (let t of n) for (let [n, a] of Object.entries(t.fields)) {
|
|
746
|
-
let o = i.get(n);
|
|
747
|
-
if (o !== void 0) throw Error(`mixin collision on column '${n}' in table '${e.tableName}': '${ht(o)}' and '${ht(t)}' both define it. If they're the same mixin reached via different paths, set a shared 'id' on both. If they're genuinely different concerns, rename one of the columns.`);
|
|
748
|
-
M(e.tableName, n), r[n] = a, i.set(n, t);
|
|
749
|
-
}
|
|
750
|
-
let a = [...e.appliedIndexes];
|
|
751
|
-
for (let t of n) for (let n of t.indexes ?? []) {
|
|
752
|
-
let t = n.name ?? gt(e.tableName, n.fields);
|
|
753
|
-
a.some((e) => e.name === t) || (N(e.tableName, t, n.name === void 0, n.fields), vt(e.tableName, t, n.kind, n.fields, r), a.push({
|
|
754
|
-
name: t,
|
|
755
|
-
fields: n.fields,
|
|
756
|
-
...n.kind === void 0 ? {} : { kind: n.kind },
|
|
757
|
-
...n.kindOptions === void 0 ? {} : { kindOptions: n.kindOptions }
|
|
758
|
-
}));
|
|
759
|
-
}
|
|
760
|
-
return I({
|
|
761
|
-
...e,
|
|
762
|
-
tableName: e.tableName,
|
|
763
|
-
fields: r,
|
|
764
|
-
isReactive: e.isReactive,
|
|
765
|
-
appliedMixins: [...e.appliedMixins, ...n],
|
|
766
|
-
appliedIndexes: a,
|
|
767
|
-
appliedUniques: e.appliedUniques,
|
|
768
|
-
appliedFullText: e.appliedFullText,
|
|
769
|
-
appliedChecks: e.appliedChecks,
|
|
770
|
-
appliedPrimaryKey: e.appliedPrimaryKey,
|
|
771
|
-
...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
|
|
772
|
-
...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
|
|
773
|
-
});
|
|
774
|
-
}),
|
|
775
|
-
index: ((...t) => {
|
|
776
|
-
let n, r, i, a = Array.isArray(t[0]);
|
|
777
|
-
if (a ? (r = t[0], i = t[1], n = gt(e.tableName, r)) : (n = t[0], r = t[1], i = t[2]), r.length === 0) throw Error(`index '${n}' on table '${e.tableName}' has no fields — an index must cover at least one column.`);
|
|
778
|
-
let o = r.filter((e) => typeof e == "string");
|
|
779
|
-
if (N(e.tableName, n, a, o), vt(e.tableName, n, i?.kind, r, e.fields), e.appliedIndexes.some((e) => e.name === n)) throw t.length === 1 ? Error(`duplicate auto-named index '${n}' on table '${e.tableName}' — you've called .index(${JSON.stringify(r)}) twice. Pass an explicit name to the second one if both are intended (rare).`) : Error(`duplicate index '${n}' on table '${e.tableName}': each index name must be unique within a table.`);
|
|
780
|
-
return I({
|
|
781
|
-
...e,
|
|
782
|
-
tableName: e.tableName,
|
|
783
|
-
fields: e.fields,
|
|
784
|
-
isReactive: e.isReactive,
|
|
785
|
-
appliedMixins: e.appliedMixins,
|
|
786
|
-
appliedIndexes: [...e.appliedIndexes, {
|
|
787
|
-
name: n,
|
|
788
|
-
fields: [...r],
|
|
789
|
-
...i?.where === void 0 ? {} : { where: i.where },
|
|
790
|
-
...i?.kind === void 0 ? {} : { kind: i.kind },
|
|
791
|
-
...i?.kindOptions === void 0 ? {} : { kindOptions: i.kindOptions }
|
|
792
|
-
}],
|
|
793
|
-
appliedUniques: e.appliedUniques,
|
|
794
|
-
appliedFullText: e.appliedFullText,
|
|
795
|
-
appliedChecks: e.appliedChecks,
|
|
796
|
-
appliedPrimaryKey: e.appliedPrimaryKey,
|
|
797
|
-
...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
|
|
798
|
-
...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
|
|
799
|
-
});
|
|
800
|
-
}),
|
|
801
|
-
expressionIndex: ((t, n, r) => {
|
|
802
|
-
if (n.length === 0) throw Error(`expressionIndex '${t}' on table '${e.tableName}' has no fields — an index must cover at least one column or expression.`);
|
|
803
|
-
let i = n.filter((e) => typeof e == "string");
|
|
804
|
-
if (N(e.tableName, t, !1, i), vt(e.tableName, t, r?.kind, n, e.fields), e.appliedIndexes.some((e) => e.name === t)) throw Error(`duplicate index '${t}' on table '${e.tableName}': each index name must be unique within a table.`);
|
|
805
|
-
return I({
|
|
806
|
-
...e,
|
|
807
|
-
tableName: e.tableName,
|
|
808
|
-
fields: e.fields,
|
|
809
|
-
isReactive: e.isReactive,
|
|
810
|
-
appliedMixins: e.appliedMixins,
|
|
811
|
-
appliedIndexes: [...e.appliedIndexes, {
|
|
812
|
-
name: t,
|
|
813
|
-
fields: [...n],
|
|
814
|
-
...r?.where === void 0 ? {} : { where: r.where },
|
|
815
|
-
...r?.kind === void 0 ? {} : { kind: r.kind },
|
|
816
|
-
...r?.kindOptions === void 0 ? {} : { kindOptions: r.kindOptions }
|
|
817
|
-
}],
|
|
818
|
-
appliedUniques: e.appliedUniques,
|
|
819
|
-
appliedFullText: e.appliedFullText,
|
|
820
|
-
appliedChecks: e.appliedChecks,
|
|
821
|
-
appliedPrimaryKey: e.appliedPrimaryKey,
|
|
822
|
-
...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
|
|
823
|
-
...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
|
|
824
|
-
});
|
|
825
|
-
}),
|
|
826
|
-
unique: ((...t) => {
|
|
827
|
-
let n, r, i, a = Array.isArray(t[0]);
|
|
828
|
-
if (a ? (r = t[0], i = t[1], n = _t(e.tableName, r)) : (n = t[0], r = t[1], i = t[2]), r.length === 0) throw Error(`unique '${n}' on table '${e.tableName}' has no fields — a UNIQUE constraint must cover at least one column.`);
|
|
829
|
-
if (N(e.tableName, n, a, r), e.appliedUniques.some((e) => e.name === n)) throw Error(a ? `duplicate auto-named UNIQUE '${n}' on table '${e.tableName}' — you've called .unique(${JSON.stringify(r)}) twice. Pass an explicit name to the second one if both are intended (very rare).` : `duplicate UNIQUE '${n}' on table '${e.tableName}': each constraint name must be unique within a table.`);
|
|
830
|
-
return I({
|
|
831
|
-
...e,
|
|
832
|
-
tableName: e.tableName,
|
|
833
|
-
fields: e.fields,
|
|
834
|
-
isReactive: e.isReactive,
|
|
835
|
-
appliedMixins: e.appliedMixins,
|
|
836
|
-
appliedIndexes: e.appliedIndexes,
|
|
837
|
-
appliedUniques: [...e.appliedUniques, {
|
|
838
|
-
name: n,
|
|
839
|
-
fields: [...r],
|
|
840
|
-
...i?.dedup === void 0 ? {} : { dedup: i.dedup }
|
|
841
|
-
}],
|
|
842
|
-
appliedFullText: e.appliedFullText,
|
|
843
|
-
appliedChecks: e.appliedChecks,
|
|
844
|
-
appliedPrimaryKey: e.appliedPrimaryKey,
|
|
845
|
-
...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
|
|
846
|
-
...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
|
|
847
|
-
});
|
|
848
|
-
}),
|
|
849
|
-
uniqueActive: ((...t) => {
|
|
850
|
-
let n, r, i, a = Array.isArray(t[0]);
|
|
851
|
-
if (a ? (r = t[0], i = t[1], n = gt(e.tableName, r)) : (n = t[0], r = t[1], i = t[2]), r.length === 0) throw Error(`uniqueActive '${n}' on table '${e.tableName}' has no fields — a partial UNIQUE must cover at least one column.`);
|
|
852
|
-
if (N(e.tableName, n, a, r), e.appliedIndexes.some((e) => e.name === n)) throw Error(`duplicate index '${n}' on table '${e.tableName}': each index name must be unique within a table (uniqueActive shares the index namespace).`);
|
|
853
|
-
let o = i?.where ?? "\"deletedAt\" IS NULL";
|
|
854
|
-
return I({
|
|
855
|
-
...e,
|
|
856
|
-
tableName: e.tableName,
|
|
857
|
-
fields: e.fields,
|
|
858
|
-
isReactive: e.isReactive,
|
|
859
|
-
appliedMixins: e.appliedMixins,
|
|
860
|
-
appliedIndexes: [...e.appliedIndexes, {
|
|
861
|
-
name: n,
|
|
862
|
-
fields: [...r],
|
|
863
|
-
where: o,
|
|
864
|
-
unique: !0
|
|
865
|
-
}],
|
|
866
|
-
appliedUniques: e.appliedUniques,
|
|
867
|
-
appliedFullText: e.appliedFullText,
|
|
868
|
-
appliedChecks: e.appliedChecks,
|
|
869
|
-
appliedPrimaryKey: e.appliedPrimaryKey,
|
|
870
|
-
...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
|
|
871
|
-
...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
|
|
872
|
-
});
|
|
873
|
-
}),
|
|
874
|
-
check: ((t, n) => {
|
|
875
|
-
if (n.trim().length === 0) throw Error(`CHECK '${t}' on table '${e.tableName}' has an empty expression.`);
|
|
876
|
-
if (N(e.tableName, t, !1, []), e.appliedChecks.some((e) => e.name === t)) throw Error(`duplicate CHECK '${t}' on table '${e.tableName}': each constraint name must be unique within a table.`);
|
|
877
|
-
return I({
|
|
878
|
-
...e,
|
|
879
|
-
tableName: e.tableName,
|
|
880
|
-
fields: e.fields,
|
|
881
|
-
isReactive: e.isReactive,
|
|
882
|
-
appliedMixins: e.appliedMixins,
|
|
883
|
-
appliedIndexes: e.appliedIndexes,
|
|
884
|
-
appliedUniques: e.appliedUniques,
|
|
885
|
-
appliedFullText: e.appliedFullText,
|
|
886
|
-
appliedChecks: [...e.appliedChecks, {
|
|
887
|
-
name: t,
|
|
888
|
-
expr: n
|
|
889
|
-
}],
|
|
890
|
-
appliedPrimaryKey: e.appliedPrimaryKey,
|
|
891
|
-
...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
|
|
892
|
-
...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
|
|
893
|
-
});
|
|
894
|
-
}),
|
|
895
|
-
rule: ((t, n, r) => {
|
|
896
|
-
if (t.trim().length === 0) throw Error(`rule on table '${e.tableName}' has an empty name.`);
|
|
897
|
-
let i = e.appliedRules ?? [];
|
|
898
|
-
if (i.some((e) => e.name === t)) throw Error(`duplicate rule '${t}' on table '${e.tableName}': each rule name must be unique within a table.`);
|
|
899
|
-
return I({
|
|
900
|
-
...e,
|
|
901
|
-
appliedRules: [...i, {
|
|
902
|
-
name: t,
|
|
903
|
-
predicate: n,
|
|
904
|
-
severity: r?.severity ?? "error"
|
|
905
|
-
}]
|
|
906
|
-
});
|
|
907
|
-
}),
|
|
908
|
-
renamedFrom: ((t) => {
|
|
909
|
-
if (t === e.tableName) throw Error(`renamedFrom('${t}') on table '${e.tableName}' names the table itself — drop the marker, there is nothing to rename.`);
|
|
910
|
-
return j(t), I({
|
|
911
|
-
...e,
|
|
912
|
-
previousTableName: t
|
|
913
|
-
});
|
|
914
|
-
}),
|
|
915
|
-
primaryKey: ((n) => {
|
|
916
|
-
if (n.length === 0) throw Error(`primaryKey([...]) on table '${e.tableName}' needs at least one column.`);
|
|
917
|
-
let r = /* @__PURE__ */ new Set();
|
|
918
|
-
for (let t of n) {
|
|
919
|
-
if (!(t in e.fields)) throw Error(`primaryKey([...]) on table '${e.tableName}' references unknown column '${t}'.`);
|
|
920
|
-
if (r.has(t)) throw Error(`primaryKey([...]) on table '${e.tableName}' lists column '${t}' twice.`);
|
|
921
|
-
r.add(t);
|
|
922
|
-
}
|
|
923
|
-
if (t.length > 0) throw Error(`primaryKey([...]) declared twice on table '${e.tableName}'.`);
|
|
924
|
-
for (let [t, r] of Object.entries(e.fields)) if (r.type === "id" && !n.includes(t)) throw Error(`primaryKey([...]) on table '${e.tableName}' conflicts with the id() column '${t}'. The explicit primary key IS the key — drop the id() column, or include '${t}' in the composite key.`);
|
|
925
|
-
return I({
|
|
926
|
-
...e,
|
|
927
|
-
tableName: e.tableName,
|
|
928
|
-
fields: e.fields,
|
|
929
|
-
isReactive: e.isReactive,
|
|
930
|
-
appliedMixins: e.appliedMixins,
|
|
931
|
-
appliedIndexes: e.appliedIndexes,
|
|
932
|
-
appliedUniques: e.appliedUniques,
|
|
933
|
-
appliedFullText: e.appliedFullText,
|
|
934
|
-
appliedChecks: e.appliedChecks,
|
|
935
|
-
appliedPrimaryKey: [...n],
|
|
936
|
-
...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
|
|
937
|
-
...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
|
|
938
|
-
});
|
|
939
|
-
}),
|
|
940
|
-
nonReactive: (() => I({
|
|
941
|
-
...e,
|
|
942
|
-
tableName: e.tableName,
|
|
943
|
-
fields: e.fields,
|
|
944
|
-
isReactive: !1,
|
|
945
|
-
appliedMixins: e.appliedMixins,
|
|
946
|
-
appliedIndexes: e.appliedIndexes,
|
|
947
|
-
appliedUniques: e.appliedUniques,
|
|
948
|
-
appliedFullText: e.appliedFullText,
|
|
949
|
-
appliedChecks: e.appliedChecks,
|
|
950
|
-
appliedPrimaryKey: e.appliedPrimaryKey,
|
|
951
|
-
...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
|
|
952
|
-
...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
|
|
953
|
-
})),
|
|
954
|
-
validate: ((t) => I({
|
|
955
|
-
...e,
|
|
956
|
-
tableName: e.tableName,
|
|
957
|
-
fields: e.fields,
|
|
958
|
-
isReactive: e.isReactive,
|
|
959
|
-
appliedMixins: e.appliedMixins,
|
|
960
|
-
appliedIndexes: e.appliedIndexes,
|
|
961
|
-
appliedUniques: e.appliedUniques,
|
|
962
|
-
appliedFullText: e.appliedFullText,
|
|
963
|
-
appliedChecks: e.appliedChecks,
|
|
964
|
-
appliedPrimaryKey: e.appliedPrimaryKey,
|
|
965
|
-
insertSchema: t,
|
|
966
|
-
...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
|
|
967
|
-
})),
|
|
968
|
-
validatePatch: ((t) => I({
|
|
969
|
-
...e,
|
|
970
|
-
tableName: e.tableName,
|
|
971
|
-
fields: e.fields,
|
|
972
|
-
isReactive: e.isReactive,
|
|
973
|
-
appliedMixins: e.appliedMixins,
|
|
974
|
-
appliedIndexes: e.appliedIndexes,
|
|
975
|
-
appliedUniques: e.appliedUniques,
|
|
976
|
-
appliedFullText: e.appliedFullText,
|
|
977
|
-
appliedChecks: e.appliedChecks,
|
|
978
|
-
appliedPrimaryKey: e.appliedPrimaryKey,
|
|
979
|
-
...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
|
|
980
|
-
validatePatchSchema: r.partial(t)
|
|
981
|
-
}))
|
|
982
|
-
};
|
|
983
|
-
}, L = (e, t) => {
|
|
984
|
-
j(e);
|
|
985
|
-
let n = Object.entries(t).flatMap(([e, t]) => {
|
|
986
|
-
let n = ge(t);
|
|
987
|
-
return n === void 0 ? [] : [{
|
|
988
|
-
column: e,
|
|
989
|
-
...n
|
|
990
|
-
}];
|
|
991
|
-
}), r = yt(C(t), e);
|
|
992
|
-
for (let t of Object.keys(r)) M(e, t);
|
|
993
|
-
let i;
|
|
994
|
-
for (let [t, n] of Object.entries(r)) n.references === S && (n.references = () => {
|
|
995
|
-
if (i === void 0) throw Error(`selfReference() on '${e}.${t}' read before table() returned`);
|
|
996
|
-
return i;
|
|
997
|
-
});
|
|
998
|
-
return i = I({
|
|
999
|
-
appliedPluginRefs: n,
|
|
1000
|
-
tableName: e,
|
|
1001
|
-
fields: r,
|
|
1002
|
-
isReactive: !0,
|
|
1003
|
-
appliedMixins: [],
|
|
1004
|
-
appliedIndexes: [],
|
|
1005
|
-
appliedUniques: [],
|
|
1006
|
-
appliedFullText: [],
|
|
1007
|
-
appliedChecks: [],
|
|
1008
|
-
appliedRules: []
|
|
1009
|
-
}), i;
|
|
1010
|
-
}, bt = (e) => e === "sqlite" || e === "turso", xt = (e) => e === "mysql" || e === "mariadb", R = (e, t) => {
|
|
1011
|
-
switch (t) {
|
|
1012
|
-
case "mysql":
|
|
1013
|
-
case "mariadb": return `\`${e.replace(/`/g, "``")}\``;
|
|
1014
|
-
case "mssql": return `[${e.replace(/\]/g, "]]")}]`;
|
|
1015
|
-
case "postgres":
|
|
1016
|
-
case "sqlite":
|
|
1017
|
-
case "turso": return `"${e.replace(/"/g, "\"\"")}"`;
|
|
1018
|
-
}
|
|
1019
|
-
}, St = 1e4, Ct = Symbol.for("@voltro/database/tableRegistry"), wt = globalThis, z = wt[Ct] ?? (wt[Ct] = /* @__PURE__ */ new Map()), Tt = (e) => {
|
|
1020
|
-
let t = z.get(e.tableName);
|
|
1021
|
-
if (t === void 0) {
|
|
1022
|
-
z.set(e.tableName, e);
|
|
1023
|
-
return;
|
|
1024
|
-
}
|
|
1025
|
-
if (t !== e) throw Error(`duplicate table '${e.tableName}' registration: two different table descriptors claim the same name. Rename one or import the shared module that defines it.`);
|
|
1026
|
-
}, Et = (e) => {
|
|
1027
|
-
z.get(e.tableName) === void 0 && z.set(e.tableName, e);
|
|
1028
|
-
}, B = (e) => z.get(e), Dt = (e) => {
|
|
1029
|
-
let t = z.get(e);
|
|
1030
|
-
if (t === void 0) throw Error(`table '${e}' is not registered. Register it by wrapping it in \`databaseHandle({ ... })\` (the usual path) or call \`registerTable(t)\` explicitly before this runs. Note a \`queryFor(t)\` query carries its own source table, so eager-loads don't hit this — this error means a reconstructed descriptor or by-name tooling reached an unregistered table.`);
|
|
1031
|
-
return t;
|
|
1032
|
-
}, Ot = () => [...z.values()], kt = (e) => z.delete(e), At = () => {
|
|
1033
|
-
z.clear();
|
|
1034
|
-
}, jt = (e) => z.get(e)?.isReactive !== !1, V = (e) => e.order ?? [], Mt = (e) => ({
|
|
1035
|
-
op: "count",
|
|
1036
|
-
alias: e ?? "count"
|
|
1037
|
-
}), Nt = (e, t) => ({
|
|
1038
|
-
op: "count-distinct",
|
|
1039
|
-
column: e,
|
|
1040
|
-
alias: t ?? `count_distinct_${e}`
|
|
1041
|
-
}), Pt = (e, t) => ({
|
|
1042
|
-
op: "sum",
|
|
1043
|
-
column: e,
|
|
1044
|
-
alias: t ?? `sum_${e}`
|
|
1045
|
-
}), Ft = (e, t) => ({
|
|
1046
|
-
op: "avg",
|
|
1047
|
-
column: e,
|
|
1048
|
-
alias: t ?? `avg_${e}`
|
|
1049
|
-
}), It = (e, t) => ({
|
|
1050
|
-
op: "min",
|
|
1051
|
-
column: e,
|
|
1052
|
-
alias: t ?? `min_${e}`
|
|
1053
|
-
}), Lt = (e, t) => ({
|
|
1054
|
-
op: "max",
|
|
1055
|
-
column: e,
|
|
1056
|
-
alias: t ?? `max_${e}`
|
|
1057
|
-
}), Rt = (e, t) => ({
|
|
1058
|
-
op: "column",
|
|
1059
|
-
column: e,
|
|
1060
|
-
alias: t ?? e
|
|
1061
|
-
}), H = (e) => (...t) => {
|
|
1062
|
-
if (t.length < 2) throw Error(`${e}: requires at least two queries`);
|
|
1063
|
-
return W({
|
|
1064
|
-
table: t[0].descriptor.table,
|
|
1065
|
-
predicate: void 0,
|
|
1066
|
-
order: [],
|
|
1067
|
-
take: void 0,
|
|
1068
|
-
skip: void 0,
|
|
1069
|
-
projection: void 0,
|
|
1070
|
-
setOp: {
|
|
1071
|
-
kind: e,
|
|
1072
|
-
queries: t.map((e) => e.descriptor)
|
|
1073
|
-
}
|
|
1074
|
-
});
|
|
1075
|
-
}, zt = H("union"), Bt = H("union-all"), Vt = H("intersect"), Ht = H("except"), U = (e) => ({ over: (t) => ({
|
|
1076
|
-
...e,
|
|
1077
|
-
window: t
|
|
1078
|
-
}) }), Ut = (e = "rowNumber") => U({
|
|
1079
|
-
op: "window-row-number",
|
|
1080
|
-
alias: e
|
|
1081
|
-
}), Wt = (e = "rank") => U({
|
|
1082
|
-
op: "window-rank",
|
|
1083
|
-
alias: e
|
|
1084
|
-
}), Gt = (e = "denseRank") => U({
|
|
1085
|
-
op: "window-dense-rank",
|
|
1086
|
-
alias: e
|
|
1087
|
-
}), Kt = (e, t = 1, n) => U({
|
|
1088
|
-
op: "window-lag",
|
|
1089
|
-
column: e,
|
|
1090
|
-
alias: n ?? `lag_${e}`,
|
|
1091
|
-
offset: t
|
|
1092
|
-
}), qt = (e, t = 1, n) => U({
|
|
1093
|
-
op: "window-lead",
|
|
1094
|
-
column: e,
|
|
1095
|
-
alias: n ?? `lead_${e}`,
|
|
1096
|
-
offset: t
|
|
1097
|
-
}), Jt = (e, t) => U({
|
|
1098
|
-
op: "window-sum-over",
|
|
1099
|
-
column: e,
|
|
1100
|
-
alias: t ?? `sum_${e}_over`
|
|
1101
|
-
}), Yt = (e, t) => U({
|
|
1102
|
-
op: "window-avg-over",
|
|
1103
|
-
column: e,
|
|
1104
|
-
alias: t ?? `avg_${e}_over`
|
|
1105
|
-
}), Xt = (e, t, n) => {
|
|
1106
|
-
let r = n?.find((e) => e.name === t) ?? B(e)?.appliedFullText?.find((e) => e.name === t);
|
|
1107
|
-
if (r === void 0) throw Error(`.matching('${t}', ...): no full-text index named '${t}' is declared on table '${e}'. Declare it with \`table(...).fullTextIndex('${t}', [columns])\` first.`);
|
|
1108
|
-
return {
|
|
1109
|
-
columns: r.columns,
|
|
1110
|
-
...r.config === void 0 ? {} : { config: r.config },
|
|
1111
|
-
...r.weights === void 0 ? {} : { weights: r.weights }
|
|
1112
|
-
};
|
|
1113
|
-
}, W = (e) => ({
|
|
1114
|
-
descriptor: e,
|
|
1115
|
-
where(t) {
|
|
1116
|
-
let n = e.predicate ? { and: [e.predicate, t] } : t;
|
|
1117
|
-
return W({
|
|
1118
|
-
...e,
|
|
1119
|
-
predicate: n
|
|
1120
|
-
});
|
|
1121
|
-
},
|
|
1122
|
-
orderBy(t, n = "asc") {
|
|
1123
|
-
return W({
|
|
1124
|
-
...e,
|
|
1125
|
-
order: [...e.order, {
|
|
1126
|
-
column: t,
|
|
1127
|
-
direction: n
|
|
1128
|
-
}]
|
|
1129
|
-
});
|
|
1130
|
-
},
|
|
1131
|
-
limit(t) {
|
|
1132
|
-
return W({
|
|
1133
|
-
...e,
|
|
1134
|
-
take: t
|
|
1135
|
-
});
|
|
1136
|
-
},
|
|
1137
|
-
offset(t) {
|
|
1138
|
-
return W({
|
|
1139
|
-
...e,
|
|
1140
|
-
skip: t
|
|
1141
|
-
});
|
|
1142
|
-
},
|
|
1143
|
-
select(...t) {
|
|
1144
|
-
return W({
|
|
1145
|
-
...e,
|
|
1146
|
-
projection: t
|
|
1147
|
-
});
|
|
1148
|
-
},
|
|
1149
|
-
using(t) {
|
|
1150
|
-
return W({
|
|
1151
|
-
...e,
|
|
1152
|
-
usingIndex: t
|
|
1153
|
-
});
|
|
1154
|
-
},
|
|
1155
|
-
withDeleted() {
|
|
1156
|
-
return W({
|
|
1157
|
-
...e,
|
|
1158
|
-
includeDeleted: !0
|
|
1159
|
-
});
|
|
1160
|
-
},
|
|
1161
|
-
forUpdate() {
|
|
1162
|
-
return W({
|
|
1163
|
-
...e,
|
|
1164
|
-
lock: "update"
|
|
1165
|
-
});
|
|
1166
|
-
},
|
|
1167
|
-
unscoped() {
|
|
1168
|
-
return W({
|
|
1169
|
-
...e,
|
|
1170
|
-
crossTenant: !0
|
|
1171
|
-
});
|
|
1172
|
-
},
|
|
1173
|
-
with(t) {
|
|
1174
|
-
let n = {
|
|
1175
|
-
...e.eager ?? {},
|
|
1176
|
-
...t
|
|
1177
|
-
};
|
|
1178
|
-
return W({
|
|
1179
|
-
...e,
|
|
1180
|
-
eager: n
|
|
1181
|
-
});
|
|
1182
|
-
},
|
|
1183
|
-
count() {
|
|
1184
|
-
return W({
|
|
1185
|
-
...e,
|
|
1186
|
-
projection: void 0,
|
|
1187
|
-
aggregations: [{
|
|
1188
|
-
op: "count",
|
|
1189
|
-
alias: "count"
|
|
1190
|
-
}]
|
|
1191
|
-
});
|
|
1192
|
-
},
|
|
1193
|
-
aggregate(t) {
|
|
1194
|
-
let n = Object.entries(t).map(([e, t]) => ({
|
|
1195
|
-
...t,
|
|
1196
|
-
alias: e
|
|
1197
|
-
}));
|
|
1198
|
-
return W({
|
|
1199
|
-
...e,
|
|
1200
|
-
projection: void 0,
|
|
1201
|
-
aggregations: n
|
|
1202
|
-
});
|
|
1203
|
-
},
|
|
1204
|
-
matching(t, n, r) {
|
|
1205
|
-
let i = Xt(e.table, t, e.fullTextIndexes);
|
|
1206
|
-
return W({
|
|
1207
|
-
...e,
|
|
1208
|
-
fullTextSearch: {
|
|
1209
|
-
indexName: t,
|
|
1210
|
-
query: n,
|
|
1211
|
-
columns: i.columns,
|
|
1212
|
-
...r?.prefix === !0 ? { prefix: !0 } : {},
|
|
1213
|
-
...i.config === void 0 ? {} : { config: i.config },
|
|
1214
|
-
...i.weights === void 0 ? {} : { weights: i.weights }
|
|
1215
|
-
}
|
|
1216
|
-
});
|
|
1217
|
-
},
|
|
1218
|
-
rankBy(t) {
|
|
1219
|
-
let n = e.fullTextSearch;
|
|
1220
|
-
if (n === void 0) throw Error(".rankBy() requires a preceding .matching(...) clause — there is no full-text search to rank on this query.");
|
|
1221
|
-
return W({
|
|
1222
|
-
...e,
|
|
1223
|
-
fullTextSearch: {
|
|
1224
|
-
...n,
|
|
1225
|
-
rank: {
|
|
1226
|
-
alias: t?.alias ?? "rank",
|
|
1227
|
-
order: t?.order ?? !0
|
|
1228
|
-
}
|
|
1229
|
-
}
|
|
1230
|
-
});
|
|
1231
|
-
},
|
|
1232
|
-
nearestNeighbours(t, n, r) {
|
|
1233
|
-
if (typeof t == "string" && Array.isArray(n)) {
|
|
1234
|
-
let i = {
|
|
1235
|
-
column: t,
|
|
1236
|
-
queryVector: n,
|
|
1237
|
-
distance: r?.distance ?? "cosine",
|
|
1238
|
-
...e.annClause?.ef === void 0 ? {} : { ef: e.annClause.ef }
|
|
1239
|
-
};
|
|
1240
|
-
return W({
|
|
1241
|
-
...e,
|
|
1242
|
-
annClause: i
|
|
1243
|
-
});
|
|
1244
|
-
}
|
|
1245
|
-
if (typeof t == "string" && typeof n == "number") {
|
|
1246
|
-
let r = {
|
|
1247
|
-
column: "",
|
|
1248
|
-
queryText: t,
|
|
1249
|
-
distance: "cosine",
|
|
1250
|
-
...e.annClause?.ef === void 0 ? {} : { ef: e.annClause.ef }
|
|
1251
|
-
};
|
|
1252
|
-
return W({
|
|
1253
|
-
...e,
|
|
1254
|
-
annClause: r,
|
|
1255
|
-
take: n
|
|
1256
|
-
});
|
|
1257
|
-
}
|
|
1258
|
-
throw Error("nearestNeighbours: call as (column, queryVector, options?) or (queryString, k).");
|
|
1259
|
-
},
|
|
1260
|
-
efSearch(t) {
|
|
1261
|
-
let n = e.annClause ?? {
|
|
1262
|
-
column: "",
|
|
1263
|
-
distance: "cosine"
|
|
1264
|
-
};
|
|
1265
|
-
return W({
|
|
1266
|
-
...e,
|
|
1267
|
-
annClause: {
|
|
1268
|
-
...n,
|
|
1269
|
-
ef: t
|
|
1270
|
-
}
|
|
1271
|
-
});
|
|
1272
|
-
},
|
|
1273
|
-
use(t) {
|
|
1274
|
-
return W({ ...t(e) });
|
|
1275
|
-
},
|
|
1276
|
-
distinct() {
|
|
1277
|
-
return W({
|
|
1278
|
-
...e,
|
|
1279
|
-
distinct: !0
|
|
1280
|
-
});
|
|
1281
|
-
},
|
|
1282
|
-
distinctOn(t) {
|
|
1283
|
-
return W({
|
|
1284
|
-
...e,
|
|
1285
|
-
distinctOn: [...t]
|
|
1286
|
-
});
|
|
1287
|
-
},
|
|
1288
|
-
as(t) {
|
|
1289
|
-
return W({
|
|
1290
|
-
...e,
|
|
1291
|
-
alias: t
|
|
1292
|
-
});
|
|
1293
|
-
},
|
|
1294
|
-
innerJoin(t, n, r) {
|
|
1295
|
-
let i = typeof t == "string" ? t : t.tableName, a = [...e.joins ?? [], {
|
|
1296
|
-
kind: "inner",
|
|
1297
|
-
table: i,
|
|
1298
|
-
alias: n,
|
|
1299
|
-
on: r,
|
|
1300
|
-
...typeof t == "string" ? {} : { sourceTable: t }
|
|
1301
|
-
}];
|
|
1302
|
-
return W({
|
|
1303
|
-
...e,
|
|
1304
|
-
joins: a
|
|
1305
|
-
});
|
|
1306
|
-
},
|
|
1307
|
-
leftJoin(t, n, r) {
|
|
1308
|
-
let i = typeof t == "string" ? t : t.tableName, a = [...e.joins ?? [], {
|
|
1309
|
-
kind: "left",
|
|
1310
|
-
table: i,
|
|
1311
|
-
alias: n,
|
|
1312
|
-
on: r,
|
|
1313
|
-
...typeof t == "string" ? {} : { sourceTable: t }
|
|
1314
|
-
}];
|
|
1315
|
-
return W({
|
|
1316
|
-
...e,
|
|
1317
|
-
joins: a
|
|
1318
|
-
});
|
|
1319
|
-
},
|
|
1320
|
-
selectJoined(t) {
|
|
1321
|
-
let n = Object.entries(t).map(([e, t]) => ({
|
|
1322
|
-
outputKey: e,
|
|
1323
|
-
source: t
|
|
1324
|
-
}));
|
|
1325
|
-
return W({
|
|
1326
|
-
...e,
|
|
1327
|
-
projection: void 0,
|
|
1328
|
-
joinedProjection: n
|
|
1329
|
-
});
|
|
1330
|
-
},
|
|
1331
|
-
withCte(t, n) {
|
|
1332
|
-
let r = e.ctes ?? [];
|
|
1333
|
-
if (r.some((e) => e.name === t)) throw Error(`Query.withCte: duplicate CTE name '${t}'`);
|
|
1334
|
-
return W({
|
|
1335
|
-
...e,
|
|
1336
|
-
ctes: [...r, {
|
|
1337
|
-
name: t,
|
|
1338
|
-
descriptor: n
|
|
1339
|
-
}]
|
|
1340
|
-
});
|
|
1341
|
-
},
|
|
1342
|
-
recursiveCte(t, n) {
|
|
1343
|
-
let r = e.ctes ?? [];
|
|
1344
|
-
if (r.some((e) => e.name === t)) throw Error(`Query.recursiveCte: duplicate CTE name '${t}'`);
|
|
1345
|
-
return W({
|
|
1346
|
-
...e,
|
|
1347
|
-
ctes: [...r, {
|
|
1348
|
-
name: t,
|
|
1349
|
-
descriptor: n,
|
|
1350
|
-
recursive: !0
|
|
1351
|
-
}]
|
|
1352
|
-
});
|
|
1353
|
-
},
|
|
1354
|
-
groupBy(t) {
|
|
1355
|
-
let n = e.groupBy ? [...e.groupBy, ...t] : [...t];
|
|
1356
|
-
return W({
|
|
1357
|
-
...e,
|
|
1358
|
-
groupBy: n
|
|
1359
|
-
});
|
|
1360
|
-
},
|
|
1361
|
-
having(t) {
|
|
1362
|
-
let n = e.having ? { and: [e.having, t] } : t;
|
|
1363
|
-
return W({
|
|
1364
|
-
...e,
|
|
1365
|
-
having: n
|
|
1366
|
-
});
|
|
1367
|
-
},
|
|
1368
|
-
exists() {
|
|
1369
|
-
return W({
|
|
1370
|
-
...e,
|
|
1371
|
-
projection: void 0,
|
|
1372
|
-
aggregations: [{
|
|
1373
|
-
op: "count",
|
|
1374
|
-
alias: "__exists__"
|
|
1375
|
-
}],
|
|
1376
|
-
take: 1
|
|
1377
|
-
});
|
|
1378
|
-
}
|
|
1379
|
-
});
|
|
1380
|
-
function Zt(e) {
|
|
1381
|
-
return W({
|
|
1382
|
-
table: e.tableName,
|
|
1383
|
-
predicate: void 0,
|
|
1384
|
-
order: [],
|
|
1385
|
-
take: void 0,
|
|
1386
|
-
skip: void 0,
|
|
1387
|
-
projection: void 0,
|
|
1388
|
-
fullTextIndexes: e.appliedFullText,
|
|
1389
|
-
sourceTable: e
|
|
1390
|
-
});
|
|
1391
|
-
}
|
|
1392
|
-
var Qt = (e, t, n) => $t(e, "id", t, n), $t = (e, t, n, r, i = "asc") => {
|
|
1393
|
-
let a = [...e.order.filter((e) => e.column !== t), {
|
|
1394
|
-
column: t,
|
|
1395
|
-
direction: i
|
|
1396
|
-
}], o = n === void 0 ? void 0 : {
|
|
1397
|
-
column: t,
|
|
1398
|
-
op: i === "desc" ? "lt" : "gt",
|
|
1399
|
-
value: n
|
|
1400
|
-
}, s = o === void 0 ? e.predicate : e.predicate === void 0 ? o : { and: [e.predicate, o] };
|
|
1401
|
-
return {
|
|
1402
|
-
...e,
|
|
1403
|
-
predicate: s,
|
|
1404
|
-
order: a,
|
|
1405
|
-
take: r
|
|
1406
|
-
};
|
|
1407
|
-
}, en = (e) => {
|
|
1408
|
-
let t = {};
|
|
1409
|
-
for (let [n, r] of Object.entries(e)) Tt(r), t[n] = Zt(r);
|
|
1410
|
-
return t;
|
|
1411
|
-
}, tn = (e) => e.isView === !0, nn = (e, t, n) => {
|
|
1412
|
-
if (j(e), n.trim().length === 0) throw Error(`view('${e}', …): the SELECT body is empty — a view must define a query.`);
|
|
1413
|
-
let r = C(t);
|
|
1414
|
-
for (let t of Object.keys(r)) M(e, t);
|
|
1415
|
-
return {
|
|
1416
|
-
tableName: e,
|
|
1417
|
-
fields: r,
|
|
1418
|
-
isView: !0,
|
|
1419
|
-
viewSelect: n
|
|
1420
|
-
};
|
|
1421
|
-
}, rn = (e) => Zt({
|
|
1422
|
-
...e,
|
|
1423
|
-
isReactive: !1,
|
|
1424
|
-
appliedMixins: [],
|
|
1425
|
-
appliedIndexes: [],
|
|
1426
|
-
appliedUniques: [],
|
|
1427
|
-
appliedFullText: [],
|
|
1428
|
-
appliedChecks: []
|
|
1429
|
-
}), an = (e, t, n = null) => {
|
|
1430
|
-
let r = n === null ? R(e.tableName, t) : `${R(n, t)}.${R(e.tableName, t)}`, i = e.viewSelect.trim();
|
|
1431
|
-
switch (t) {
|
|
1432
|
-
case "postgres":
|
|
1433
|
-
case "mysql":
|
|
1434
|
-
case "mariadb": return `CREATE OR REPLACE VIEW ${r} AS ${i};`;
|
|
1435
|
-
case "mssql": return `CREATE OR ALTER VIEW ${r} AS ${i};`;
|
|
1436
|
-
case "sqlite":
|
|
1437
|
-
case "turso": return `DROP VIEW IF EXISTS ${r};\nCREATE VIEW ${r} AS ${i};`;
|
|
1438
|
-
}
|
|
1439
|
-
}, on = (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, sn = (e) => {
|
|
1440
|
-
if (e == null || typeof e != "string") return e;
|
|
1441
|
-
let t = e.trim();
|
|
1442
|
-
if (t.length === 0) return e;
|
|
1443
|
-
let n = t.charAt(0);
|
|
1444
|
-
if (n !== "{" && n !== "[" && n !== "\"" && n !== "t" && n !== "f" && n !== "n" && (n < "0" || n > "9") && n !== "-") return e;
|
|
1445
|
-
try {
|
|
1446
|
-
return JSON.parse(t);
|
|
1447
|
-
} catch {
|
|
1448
|
-
return e;
|
|
1449
|
-
}
|
|
1450
|
-
}, cn = (e) => e == null || typeof e == "string" ? e : typeof e == "number" || typeof e == "bigint" ? String(e) : e, ln = (e) => {
|
|
1451
|
-
if (e == null || e instanceof Date) return e;
|
|
1452
|
-
if (typeof e == "string") {
|
|
1453
|
-
let t = new Date(e);
|
|
1454
|
-
return isNaN(t.getTime()) ? e : t;
|
|
1455
|
-
}
|
|
1456
|
-
if (typeof e == "number") {
|
|
1457
|
-
let t = new Date(e);
|
|
1458
|
-
return isNaN(t.getTime()) ? e : t;
|
|
1459
|
-
}
|
|
1460
|
-
return e;
|
|
1461
|
-
}, un = (e, t, n) => {
|
|
1462
|
-
if (e == null) return e;
|
|
1463
|
-
switch (t) {
|
|
1464
|
-
case "boolean": return n === "postgres" ? e : on(e);
|
|
1465
|
-
case "vector": return n === "postgres" ? sn(e) : e;
|
|
1466
|
-
case "json": return n === "postgres" || n === "mysql" || n === "mariadb" ? e : sn(e);
|
|
1467
|
-
case "array": return n === "postgres" ? e : sn(e);
|
|
1468
|
-
case "timestamp":
|
|
1469
|
-
case "date": return n === "sqlite" || n === "turso" ? ln(e) : e;
|
|
1470
|
-
case "decimal":
|
|
1471
|
-
case "bigint": return cn(e);
|
|
1472
|
-
default: return e;
|
|
1473
|
-
}
|
|
1474
|
-
}, dn = (e, t, n) => {
|
|
1475
|
-
if (e.length === 0) return e;
|
|
1476
|
-
let r = B(t);
|
|
1477
|
-
return r === void 0 ? e : fn(e, r, n);
|
|
1478
|
-
}, fn = (e, t, n) => {
|
|
1479
|
-
if (e.length === 0) return e;
|
|
1480
|
-
let r = t.fields, i = [];
|
|
1481
|
-
for (let [e, t] of Object.entries(r)) i.push({
|
|
1482
|
-
key: e,
|
|
1483
|
-
type: t.type
|
|
1484
|
-
});
|
|
1485
|
-
return e.map((e) => {
|
|
1486
|
-
let t = { ...e };
|
|
1487
|
-
for (let { key: e, type: r } of i) e in t && (t[e] = un(t[e], r, n));
|
|
1488
|
-
return t;
|
|
1489
|
-
});
|
|
1490
|
-
}, pn = (e, t) => {
|
|
1491
|
-
if (e == null) return e;
|
|
1492
|
-
switch (t) {
|
|
1493
|
-
case "json": return JSON.stringify(e);
|
|
1494
|
-
case "array":
|
|
1495
|
-
case "vector": return typeof e == "string" ? e : JSON.stringify(e);
|
|
1496
|
-
default: return e;
|
|
1497
|
-
}
|
|
1498
|
-
}, mn = (e, t, n = ["json", "array"]) => {
|
|
1499
|
-
let r = B(t);
|
|
1500
|
-
if (r === void 0) return e;
|
|
1501
|
-
let i = r.fields, a = null;
|
|
1502
|
-
for (let [t, r] of Object.entries(i)) if (n.includes(r.type) && t in e) {
|
|
1503
|
-
let n = pn(e[t], r.type);
|
|
1504
|
-
n !== e[t] && (a === null && (a = { ...e }), a[t] = n);
|
|
1505
|
-
}
|
|
1506
|
-
return a ?? e;
|
|
1507
|
-
}, hn = (e) => {
|
|
1508
|
-
if (e.length === 0) throw RangeError("Column reference must not be empty");
|
|
1509
|
-
return {
|
|
1510
|
-
_tag: "ColumnReference",
|
|
1511
|
-
column: e
|
|
1512
|
-
};
|
|
1513
|
-
}, G = (e) => typeof e == "object" && !!e && "_tag" in e && e._tag === "ColumnReference" && "column" in e && typeof e.column == "string", gn = [
|
|
1514
|
-
"eq",
|
|
1515
|
-
"neq",
|
|
1516
|
-
"gt",
|
|
1517
|
-
"gte",
|
|
1518
|
-
"lt",
|
|
1519
|
-
"lte"
|
|
1520
|
-
], _n = (e) => e == null ? e : e instanceof Date ? e.toISOString() : typeof e == "boolean" ? +!!e : Array.isArray(e) ? e.map((e) => _n(e)) : e, vn = /^[A-Za-z_][A-Za-z0-9_]*$/, yn = (e) => "$" + e.map((e) => typeof e == "number" ? `[${e}]` : vn.test(e) ? `.${e}` : `."${e.replace(/"/g, "\\\"")}"`).join(""), bn = (e) => "{" + e.map((e) => {
|
|
1521
|
-
let t = String(e);
|
|
1522
|
-
return /^[A-Za-z0-9_]+$/.test(t) ? t : `"${t.replace(/(["\\])/g, "\\$1")}"`;
|
|
1523
|
-
}).join(",") + "}", K = (e, t, n, r = {}) => {
|
|
1524
|
-
let i = R(e, n), a = r.numeric ?? !1;
|
|
1525
|
-
if (n === "postgres") {
|
|
1526
|
-
let e = `(${i} #>> '${bn(t)}'::text[])`;
|
|
1527
|
-
return a ? `${e}::numeric` : e;
|
|
1528
|
-
}
|
|
1529
|
-
let o = yn(t);
|
|
1530
|
-
switch (n) {
|
|
1531
|
-
case "mysql":
|
|
1532
|
-
case "mariadb": {
|
|
1533
|
-
let e = `JSON_UNQUOTE(JSON_EXTRACT(${i}, '${o}'))`;
|
|
1534
|
-
return a ? `CAST(${e} AS DECIMAL(38,10))` : e;
|
|
1535
|
-
}
|
|
1536
|
-
case "mssql": {
|
|
1537
|
-
let e = `JSON_VALUE(${i}, '${o}')`;
|
|
1538
|
-
return a ? `CAST(${e} AS FLOAT)` : e;
|
|
1539
|
-
}
|
|
1540
|
-
case "sqlite":
|
|
1541
|
-
case "turso": {
|
|
1542
|
-
let e = `json_extract(${i}, '${o}')`;
|
|
1543
|
-
return a ? `CAST(${e} AS REAL)` : e;
|
|
1544
|
-
}
|
|
1545
|
-
}
|
|
1546
|
-
}, xn = class extends Error {
|
|
1547
|
-
_tag = "TenantNamespaceUnresolved";
|
|
1548
|
-
constructor(e) {
|
|
1549
|
-
super(e), this.name = "TenantNamespaceUnresolved";
|
|
1550
|
-
}
|
|
1551
|
-
}, Sn = class extends Error {
|
|
1552
|
-
_tag = "TenantNamespaceInvalid";
|
|
1553
|
-
constructor(e) {
|
|
1554
|
-
super(e), this.name = "TenantNamespaceInvalid";
|
|
1555
|
-
}
|
|
1556
|
-
}, Cn = 63, wn = "tenant_", Tn = (e) => e.toLowerCase().replace(/[^a-z0-9_]/g, "_"), En = (e) => {
|
|
1557
|
-
if (e == null || e === "") throw new xn("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.");
|
|
1558
|
-
let t = Tn(e);
|
|
1559
|
-
if (t.replace(/_/g, "") === "") throw new Sn(`tenant id '${e}' has no usable identifier characters after sanitisation — cannot derive a distinct namespace.`);
|
|
1560
|
-
let n = `${wn}${t}`;
|
|
1561
|
-
if (n.length > Cn) throw new Sn(`namespace '${n}' for tenant '${e}' exceeds the ${Cn}-byte identifier limit. Use a shorter tenant id.`);
|
|
1562
|
-
return n;
|
|
1563
|
-
}, q = (e, t) => e === null ? t : `${e}.${t}`, Dn = /* @__PURE__ */ new Set(["timestamp", "date"]), On = /^\d{4}-\d{2}-\d{2}([T ]\d{2}:\d{2})?/, kn = (e, t) => {
|
|
1564
|
-
if (e === void 0 || t == null || !Dn.has(e) || t instanceof Date) return t;
|
|
1565
|
-
if (typeof t == "number" && Number.isFinite(t)) return new Date(t);
|
|
1566
|
-
if (typeof t == "string" && On.test(t)) {
|
|
1567
|
-
let e = new Date(t);
|
|
1568
|
-
return Number.isNaN(e.getTime()) ? t : e;
|
|
1569
|
-
}
|
|
1570
|
-
return t;
|
|
1571
|
-
}, An = (e, t) => e === void 0 || !Dn.has(e) ? t : t.map((t) => kn(e, t)), jn = (e, t) => {
|
|
1572
|
-
let n = e.lastIndexOf("."), r = n < 0 ? e : e.slice(n + 1), i = n < 0 ? void 0 : e.slice(0, n), a = i === void 0 || i === t.alias || i === t.table, o = a ? void 0 : t.joins?.find((e) => e.alias === i), s = a ? t.table : o?.table, c = a ? t.sourceTable : o?.sourceTable;
|
|
1573
|
-
return (c?.tableName === s ? c : s === void 0 ? void 0 : B(s))?.fields[r];
|
|
1574
|
-
}, Mn = (e, t) => {
|
|
1575
|
-
let { strings: n, values: r } = e, i = t.unsafe(n[0] ?? "");
|
|
1576
|
-
for (let e = 0; e < r.length; e++) i = t`${i}${r[e]}${t.unsafe(n[e + 1] ?? "")}`;
|
|
1577
|
-
return t`${i}`;
|
|
1578
|
-
}, J = (e) => e.replace(/[\\%_]/g, (e) => `\\${e}`), Y = (e, t, n = null, r) => {
|
|
1579
|
-
if ("not" in e) return t`NOT (${Y(e.not, t, n, r)})`;
|
|
1580
|
-
if ("and" in e) return e.and.length === 0 ? t`TRUE` : t.and(e.and.map((e) => Y(e, t, n, r)));
|
|
1581
|
-
if ("or" in e) return e.or.length === 0 ? t`FALSE` : t.or(e.or.map((e) => Y(e, t, n, r)));
|
|
1582
|
-
if (e.op === "subquery-in" || e.op === "subquery-not-in") {
|
|
1583
|
-
let r = e, i = Z(r.subquery, t, n);
|
|
1584
|
-
return r.op === "subquery-in" ? t`${t(r.column)} IN (${i})` : t`${t(r.column)} NOT IN (${i})`;
|
|
1585
|
-
}
|
|
1586
|
-
if (e.op === "exists" || e.op === "not-exists") {
|
|
1587
|
-
let r = e, i = Z(r.subquery, t, n);
|
|
1588
|
-
return r.op === "exists" ? t`EXISTS (${i})` : t`NOT EXISTS (${i})`;
|
|
1589
|
-
}
|
|
1590
|
-
let i = e;
|
|
1591
|
-
if (G(i.value)) {
|
|
1592
|
-
if (!gn.some((e) => e === i.op) || i.path !== void 0) throw E("Column references require a scalar comparison without a JSON path");
|
|
1593
|
-
let e = t(i.value.column), n = t(i.column);
|
|
1594
|
-
switch (i.op) {
|
|
1595
|
-
case "eq": return t`${n} = ${e}`;
|
|
1596
|
-
case "neq": return t`${n} <> ${e}`;
|
|
1597
|
-
case "gt": return t`${n} > ${e}`;
|
|
1598
|
-
case "gte": return t`${n} >= ${e}`;
|
|
1599
|
-
case "lt": return t`${n} < ${e}`;
|
|
1600
|
-
case "lte": return t`${n} <= ${e}`;
|
|
1601
|
-
}
|
|
1602
|
-
}
|
|
1603
|
-
let a = t(i.column), o = i.path, s = o !== void 0 && o.length > 0, c = () => t.onDialectOrElse({
|
|
1604
|
-
mysql: () => t.unsafe(K(i.column, o, "mysql")),
|
|
1605
|
-
mssql: () => t.unsafe(K(i.column, o, "mssql")),
|
|
1606
|
-
sqlite: () => t.unsafe(K(i.column, o, "sqlite")),
|
|
1607
|
-
orElse: () => t.unsafe(K(i.column, o, "postgres"))
|
|
1608
|
-
}), l = () => t.onDialectOrElse({
|
|
1609
|
-
mysql: () => t.unsafe(K(i.column, o, "mysql", { numeric: !0 })),
|
|
1610
|
-
mssql: () => t.unsafe(K(i.column, o, "mssql", { numeric: !0 })),
|
|
1611
|
-
sqlite: () => t.unsafe(K(i.column, o, "sqlite", { numeric: !0 })),
|
|
1612
|
-
orElse: () => t.unsafe(K(i.column, o, "postgres", { numeric: !0 }))
|
|
1613
|
-
}), u = s ? c() : a, d = s ? l() : a, f = (e) => s && e != null ? String(e) : e, p = (e) => s ? Number(e) : e, m = s || r === void 0 ? void 0 : B(r)?.fields?.[i.column]?.type, h = (e) => {
|
|
1614
|
-
let n = kn(m, e);
|
|
1615
|
-
return n instanceof Date ? t.onDialectOrElse({
|
|
1616
|
-
sqlite: () => _n(n),
|
|
1617
|
-
orElse: () => n
|
|
1618
|
-
}) : n;
|
|
1619
|
-
};
|
|
1620
|
-
switch (i.op) {
|
|
1621
|
-
case "eq": return t`${u} = ${f(h(i.value))}`;
|
|
1622
|
-
case "neq": return t`${u} <> ${f(h(i.value))}`;
|
|
1623
|
-
case "gt": return t`${d} > ${p(h(i.value))}`;
|
|
1624
|
-
case "gte": return t`${d} >= ${p(h(i.value))}`;
|
|
1625
|
-
case "lt": return t`${d} < ${p(h(i.value))}`;
|
|
1626
|
-
case "lte": return t`${d} <= ${p(h(i.value))}`;
|
|
1627
|
-
case "in": {
|
|
1628
|
-
let e = i.value ?? [];
|
|
1629
|
-
return e.length === 0 ? t`FALSE` : t`${u} IN ${t.in(s ? e.map((e) => f(e)) : e.map(h))}`;
|
|
1630
|
-
}
|
|
1631
|
-
case "notIn": {
|
|
1632
|
-
let e = i.value ?? [];
|
|
1633
|
-
return e.length === 0 ? t`TRUE` : t`${u} NOT IN ${t.in(s ? e.map((e) => f(e)) : e.map(h))}`;
|
|
1634
|
-
}
|
|
1635
|
-
case "contains": {
|
|
1636
|
-
let e = `%${J(typeof i.value == "string" ? i.value : String(i.value ?? ""))}%`;
|
|
1637
|
-
return t.onDialectOrElse({
|
|
1638
|
-
mysql: () => t`LOWER(${u}) LIKE LOWER(${e})`,
|
|
1639
|
-
mssql: () => t`LOWER(${u}) LIKE LOWER(${e})`,
|
|
1640
|
-
sqlite: () => t`LOWER(${u}) LIKE LOWER(${e})`,
|
|
1641
|
-
orElse: () => t`${u} ILIKE ${e} ESCAPE '\\'`
|
|
1642
|
-
});
|
|
1643
|
-
}
|
|
1644
|
-
case "startsWith": {
|
|
1645
|
-
let e = `${J(typeof i.value == "string" ? i.value : String(i.value ?? ""))}%`;
|
|
1646
|
-
return t.onDialectOrElse({
|
|
1647
|
-
mysql: () => t`${u} LIKE ${e}`,
|
|
1648
|
-
orElse: () => t`${u} LIKE ${e} ESCAPE '\\'`
|
|
1649
|
-
});
|
|
1650
|
-
}
|
|
1651
|
-
case "arrayContains": {
|
|
1652
|
-
let e = i.value ?? [];
|
|
1653
|
-
return e.length === 0 ? t`TRUE` : t.onDialectOrElse({
|
|
1654
|
-
mysql: () => t`JSON_CONTAINS(${u}, ${JSON.stringify(e)})`,
|
|
1655
|
-
mssql: () => t`(SELECT COUNT(DISTINCT value) FROM OPENJSON(${u}) WHERE value IN ${t.in(e)}) = ${e.length}`,
|
|
1656
|
-
sqlite: () => t`(SELECT COUNT(DISTINCT value) FROM json_each(${u}) WHERE value IN ${t.in(e)}) = ${e.length}`,
|
|
1657
|
-
orElse: () => t`${u} @> ${e}`
|
|
1658
|
-
});
|
|
1659
|
-
}
|
|
1660
|
-
case "arrayOverlaps": {
|
|
1661
|
-
let e = i.value ?? [];
|
|
1662
|
-
return e.length === 0 ? t`FALSE` : t.onDialectOrElse({
|
|
1663
|
-
mysql: () => t.or(e.map((e) => t`JSON_CONTAINS(${u}, ${JSON.stringify(e)})`)),
|
|
1664
|
-
mssql: () => t`EXISTS (SELECT 1 FROM OPENJSON(${u}) WHERE value IN ${t.in(e)})`,
|
|
1665
|
-
sqlite: () => t`EXISTS (SELECT 1 FROM json_each(${u}) WHERE value IN ${t.in(e)})`,
|
|
1666
|
-
orElse: () => t`${u} && ${e}`
|
|
1667
|
-
});
|
|
1668
|
-
}
|
|
1669
|
-
case "arrayHas": return t.onDialectOrElse({
|
|
1670
|
-
mysql: () => t`JSON_CONTAINS(${u}, ${JSON.stringify(i.value)})`,
|
|
1671
|
-
mssql: () => t`${i.value} IN (SELECT value FROM OPENJSON(${u}))`,
|
|
1672
|
-
sqlite: () => t`EXISTS (SELECT 1 FROM json_each(${u}) WHERE value = ${i.value})`,
|
|
1673
|
-
orElse: () => t`${i.value} = ANY(${u})`
|
|
1674
|
-
});
|
|
1675
|
-
case "isNull": return t`${u} IS NULL`;
|
|
1676
|
-
case "isNotNull": return t`${u} IS NOT NULL`;
|
|
1677
|
-
case "spatial": {
|
|
1678
|
-
let e = i.value, n = t.unsafe("::geometry"), r = e.bufferMeters === void 0 ? t`${e.geom}${n}` : t`ST_Buffer(${e.geom}::geography, ${e.bufferMeters})::geometry`;
|
|
1679
|
-
return t.onDialectOrElse({
|
|
1680
|
-
mysql: () => {
|
|
1681
|
-
throw E("@voltro/plugin-postgis: spatial predicates are postgres-only");
|
|
1682
|
-
},
|
|
1683
|
-
mssql: () => {
|
|
1684
|
-
throw E("@voltro/plugin-postgis: spatial predicates are postgres-only");
|
|
1685
|
-
},
|
|
1686
|
-
sqlite: () => {
|
|
1687
|
-
throw E("@voltro/plugin-postgis: spatial predicates are postgres-only");
|
|
1688
|
-
},
|
|
1689
|
-
orElse: () => e.fn === "ST_DWithin" ? t`ST_DWithin(${a}::geography, ${e.geom}::geography, ${e.distanceMeters})` : e.fn === "&&" ? t`${a}${n} && ${r}` : t`${t.unsafe(e.fn)}(${a}${n}, ${r})`
|
|
1690
|
-
});
|
|
1691
|
-
}
|
|
1692
|
-
}
|
|
1693
|
-
}, Nn = (e, t) => {
|
|
1694
|
-
let n = [];
|
|
1695
|
-
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}`);
|
|
1696
|
-
}, Pn = (e, t) => t.csv(e.map((e) => {
|
|
1697
|
-
let n = e.column === void 0 ? t.unsafe("*") : t`${t(e.column)}`;
|
|
1698
|
-
if (e.op.startsWith("window-")) {
|
|
1699
|
-
let r = Nn(e.window ?? {}, t);
|
|
1700
|
-
switch (e.op) {
|
|
1701
|
-
case "window-row-number": return t`ROW_NUMBER() OVER (${r}) AS ${t(e.alias)}`;
|
|
1702
|
-
case "window-rank": return t`RANK() OVER (${r}) AS ${t(e.alias)}`;
|
|
1703
|
-
case "window-dense-rank": return t`DENSE_RANK() OVER (${r}) AS ${t(e.alias)}`;
|
|
1704
|
-
case "window-lag": return t`LAG(${n}, ${t.unsafe(String(e.offset ?? 1))}) OVER (${r}) AS ${t(e.alias)}`;
|
|
1705
|
-
case "window-lead": return t`LEAD(${n}, ${t.unsafe(String(e.offset ?? 1))}) OVER (${r}) AS ${t(e.alias)}`;
|
|
1706
|
-
case "window-sum-over": return t`SUM(${n}) OVER (${r}) AS ${t(e.alias)}`;
|
|
1707
|
-
case "window-avg-over": return t`AVG(${n}) OVER (${r}) AS ${t(e.alias)}`;
|
|
1708
|
-
default: throw E(`compileAggregateProjection: unknown window op '${e.op}'`);
|
|
1709
|
-
}
|
|
1710
|
-
}
|
|
1711
|
-
switch (e.op) {
|
|
1712
|
-
case "count": return t`COUNT(${n}) AS ${t(e.alias)}`;
|
|
1713
|
-
case "count-distinct": return t`COUNT(DISTINCT ${n}) AS ${t(e.alias)}`;
|
|
1714
|
-
case "sum": return t`SUM(${n}) AS ${t(e.alias)}`;
|
|
1715
|
-
case "avg": return t`AVG(${n}) AS ${t(e.alias)}`;
|
|
1716
|
-
case "min": return t`MIN(${n}) AS ${t(e.alias)}`;
|
|
1717
|
-
case "max": return t`MAX(${n}) AS ${t(e.alias)}`;
|
|
1718
|
-
case "column":
|
|
1719
|
-
if (e.column === void 0) throw E(`compileAggregateProjection: 'column' projection requires a column name (alias '${e.alias}')`);
|
|
1720
|
-
return t`${t(e.column)} AS ${t(e.alias)}`;
|
|
1721
|
-
default: throw E(`compileAggregateProjection: unknown op '${e.op}'`);
|
|
1722
|
-
}
|
|
1723
|
-
})), Fn = (e) => `[${e.join(",")}]`, In = (e, t) => {
|
|
1724
|
-
if (e.queryVector === void 0) return null;
|
|
1725
|
-
let n = t(e.column), r = Fn(e.queryVector), i = e.distance === "cosine" ? "<=>" : e.distance === "l2" ? "<->" : "<#>", a = e.distance === "cosine" ? "VEC_DISTANCE_COSINE" : "VEC_DISTANCE_EUCLIDEAN";
|
|
1726
|
-
return t.onDialectOrElse({
|
|
1727
|
-
mysql: () => t`${t.unsafe(a)}(${n}, ${r})`,
|
|
1728
|
-
mssql: () => t`NULL`,
|
|
1729
|
-
sqlite: () => t`NULL`,
|
|
1730
|
-
orElse: () => t`${n} ${t.unsafe(i)} ${r}::vector`
|
|
1731
|
-
});
|
|
1732
|
-
}, Ln = (e, t) => {
|
|
1733
|
-
let n = t.unsafe(e.useGeography ? "::geography" : "::geometry");
|
|
1734
|
-
return t`ST_Distance(${t(e.column)}${n}, ${e.geom}${n})`;
|
|
1735
|
-
}, Rn = (e, t) => t`${t(e.column)} <-> ${e.geom}::geometry`, X = (e, t) => t`${t(`${e.indexName}_tsv`)}`, zn = (e, t) => `${t}_${e.indexName}_fts`, Bn = (e) => e.toLowerCase().split(/[^\p{L}\p{N}]+/u).filter((e) => e !== ""), Vn = (e) => {
|
|
1736
|
-
if (e.prefix !== !0) return;
|
|
1737
|
-
let t = Bn(e.query);
|
|
1738
|
-
if (t.length !== 0) return {
|
|
1739
|
-
pg: t.map((e) => `${e}:*`).join(" & "),
|
|
1740
|
-
mysql: t.map((e) => `+${e}*`).join(" "),
|
|
1741
|
-
sqlite: t.map((e) => `"${e}"*`).join(" ")
|
|
1742
|
-
};
|
|
1743
|
-
}, Hn = (e, t, n) => {
|
|
1744
|
-
let r = n.csv(e.columns.map((e) => n`${n(e)}`)), i = `%${J(e.query)}%`, a = Vn(e), o = () => n.or(e.columns.map((e) => n`LOWER(${n(e)}) LIKE LOWER(${i}) ESCAPE '\\'`));
|
|
1745
|
-
return e.fallback === "like" ? o() : n.onDialectOrElse({
|
|
1746
|
-
mysql: () => a === void 0 ? n`MATCH(${r}) AGAINST(${e.query} IN NATURAL LANGUAGE MODE)` : n`MATCH(${r}) AGAINST(${a.mysql} IN BOOLEAN MODE)`,
|
|
1747
|
-
sqlite: () => {
|
|
1748
|
-
let r = n(zn(e, t));
|
|
1749
|
-
return n`${n("id")} IN (SELECT ${n("row_id")} FROM ${r} WHERE ${r} MATCH ${a?.sqlite ?? e.query})`;
|
|
1750
|
-
},
|
|
1751
|
-
mssql: o,
|
|
1752
|
-
orElse: () => a === void 0 ? n`${X(e, n)} @@ plainto_tsquery(${e.config ?? "english"}, ${e.query})` : n`${X(e, n)} @@ to_tsquery(${e.config ?? "english"}, ${a.pg})`
|
|
1753
|
-
});
|
|
1754
|
-
}, Un = (e, t, n) => {
|
|
1755
|
-
let r = `%${J(e.query)}%`, i = Vn(e), a = () => 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}`);
|
|
1756
|
-
return e.fallback === "like" ? a() : n.onDialectOrElse({
|
|
1757
|
-
mysql: () => i === void 0 ? n`MATCH(${n.csv(e.columns.map((e) => n`${n(e)}`))}) AGAINST(${e.query} IN NATURAL LANGUAGE MODE)` : n`MATCH(${n.csv(e.columns.map((e) => n`${n(e)}`))}) AGAINST(${i.mysql} IN BOOLEAN MODE)`,
|
|
1758
|
-
sqlite: () => {
|
|
1759
|
-
let r = n(zn(e, t));
|
|
1760
|
-
return n`COALESCE((SELECT -bm25(${r}) FROM ${r} WHERE ${r} MATCH ${i?.sqlite ?? e.query} AND ${r}.${n("row_id")} = ${n("id")}), 0)`;
|
|
1761
|
-
},
|
|
1762
|
-
mssql: a,
|
|
1763
|
-
orElse: () => i === void 0 ? n`ts_rank(${X(e, n)}, plainto_tsquery(${e.config ?? "english"}, ${e.query}))` : n`ts_rank(${X(e, n)}, to_tsquery(${e.config ?? "english"}, ${i.pg}))`
|
|
1764
|
-
});
|
|
1765
|
-
}, Wn = (e, t, n, r) => {
|
|
1766
|
-
let i = (e) => t(r && !e.includes(".") ? `${r}.${e}` : e), a = () => n?.length ? t.csv(n.map((e) => t`${i(e)}`)) : r ? t`${t(r)}.*` : t`*`;
|
|
1767
|
-
return !e || !Object.values(e).some((e) => e.type === "bigint") ? a() : t.onDialectOrElse({
|
|
1768
|
-
sqlite: () => t.csv((n?.length ? n : Object.keys(e)).map((n) => e[n]?.type === "bigint" ? t`CAST(${i(n)} AS TEXT) AS ${t(n)}` : t`${i(n)}`)),
|
|
1769
|
-
orElse: a
|
|
1770
|
-
});
|
|
1771
|
-
}, Gn = (e, t, n) => Wn(B(e)?.fields, t, n), Z = (e, t, n = null) => {
|
|
1772
|
-
if (e.setOp && e.setOp.queries.length >= 2) {
|
|
1773
|
-
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 = [];
|
|
1774
|
-
if (V(e).length > 0 && a.push(t` ORDER BY ${t.csv(V(e).map((e) => e.direction === "desc" ? t`${t(e.column)} DESC` : t`${t(e.column)} ASC`))}`), e.take !== void 0) {
|
|
1775
|
-
if (!Number.isInteger(e.take) || e.take < 0) throw E(`compileSelect: invalid take ${e.take}`);
|
|
1776
|
-
a.push(t` LIMIT ${t.unsafe(String(e.take))}`);
|
|
1777
|
-
}
|
|
1778
|
-
if (e.skip !== void 0) {
|
|
1779
|
-
if (!Number.isInteger(e.skip) || e.skip < 0) throw E(`compileSelect: invalid skip ${e.skip}`);
|
|
1780
|
-
a.push(t` OFFSET ${t.unsafe(String(e.skip))}`);
|
|
1781
|
-
}
|
|
1782
|
-
return t`${i}${a.reduce((e, n) => t`${e}${n}`, t``)}`;
|
|
1783
|
-
}
|
|
1784
|
-
let r = e.annClause === void 0 ? null : In(e.annClause, t);
|
|
1785
|
-
e.spatialClause !== void 0 && t.onDialectOrElse({
|
|
1786
|
-
mysql: () => {
|
|
1787
|
-
throw E("@voltro/plugin-postgis: spatial distance / KNN ordering is postgres-only");
|
|
1788
|
-
},
|
|
1789
|
-
mssql: () => {
|
|
1790
|
-
throw E("@voltro/plugin-postgis: spatial distance / KNN ordering is postgres-only");
|
|
1791
|
-
},
|
|
1792
|
-
sqlite: () => {
|
|
1793
|
-
throw E("@voltro/plugin-postgis: spatial distance / KNN ordering is postgres-only");
|
|
1794
|
-
},
|
|
1795
|
-
orElse: () => t``
|
|
1796
|
-
});
|
|
1797
|
-
let i = e.spatialClause?.projectAs === void 0 ? null : Ln(e.spatialClause, t), a = e.aggregations && e.aggregations.length > 0 ? Pn(e.aggregations, t) : e.joinedProjection && e.joinedProjection.length > 0 ? t.csv(e.joinedProjection.map((n) => {
|
|
1798
|
-
let r = t`${t(n.source)}`;
|
|
1799
|
-
return t`${jn(n.source, e)?.type === "bigint" ? t.onDialectOrElse({
|
|
1800
|
-
sqlite: () => t`CAST(${r} AS TEXT)`,
|
|
1801
|
-
orElse: () => r
|
|
1802
|
-
}) : r} AS ${t(n.outputKey)}`;
|
|
1803
|
-
})) : Wn((e.sourceTable?.tableName === e.table ? e.sourceTable : B(e.table))?.fields, t, e.projection, (e.joins?.length ?? 0) > 0 ? e.alias ?? e.table : void 0), 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({
|
|
1804
|
-
orElse: () => t`DISTINCT ON (${t.csv(e.distinctOn.map((e) => t`${t(e)}`))}) `,
|
|
1805
|
-
mysql: () => t`DISTINCT `,
|
|
1806
|
-
mssql: () => t`DISTINCT `,
|
|
1807
|
-
sqlite: () => t`DISTINCT `
|
|
1808
|
-
}) : e.distinct ? t`DISTINCT ` : t``, p = e.lock === "update", m = p ? t.onDialectOrElse({
|
|
1809
|
-
mssql: () => t` WITH (UPDLOCK, ROWLOCK)`,
|
|
1810
|
-
orElse: () => t``
|
|
1811
|
-
}) : t``, h = p ? t.onDialectOrElse({
|
|
1812
|
-
mssql: () => t``,
|
|
1813
|
-
sqlite: () => t``,
|
|
1814
|
-
orElse: () => t` FOR UPDATE`
|
|
1815
|
-
}) : t``, g = e.alias === void 0 ? t`${t(q(n, e.table))}${m}` : t`${t(q(n, e.table))} AS ${t(e.alias)}${m}`, ee = (e.joins ?? []).map((e) => {
|
|
1816
|
-
let r = e.kind === "inner" ? "INNER" : "LEFT";
|
|
1817
|
-
return t` ${t.unsafe(r)} JOIN ${t(q(n, e.table))} AS ${t(e.alias)} ON ${Y(e.on, t, n)}`;
|
|
1818
|
-
}), te = ee.length > 0 ? ee.reduce((e, n) => t`${e}${n}`, g) : g, ne = c === void 0 ? null : Hn(c, e.table, t), _ = [e.predicate ? Y(e.predicate, t, n, e.table) : null, ne].filter((e) => e !== null), v = _.length > 0 ? t` WHERE ${t.and(_)}` : t``, re = e.groupBy && e.groupBy.length > 0 ? t` GROUP BY ${t.csv(e.groupBy.map((e) => t`${t(e)}`))}` : t``, y = e.having ? t` HAVING ${Y(e.having, t, n, e.table)}` : t``, b = e.skip !== void 0, x = [];
|
|
1819
|
-
r !== null && x.push(t`${r} ASC`), e.spatialClause?.order !== void 0 && x.push(e.spatialClause.order === "desc" ? t`${Rn(e.spatialClause, t)} DESC` : t`${Rn(e.spatialClause, t)} ASC`), l !== null && c?.rank?.order === !0 && x.push(t`${l} DESC`);
|
|
1820
|
-
for (let n of V(e)) {
|
|
1821
|
-
let r = e.joinedProjection?.find((e) => e.outputKey === n.column)?.source ?? n.column, i = !o && jn(r, e)?.type === "bigint" ? t.onDialectOrElse({
|
|
1822
|
-
sqlite: () => t`${t(r.includes(".") ? r : `${e.alias ?? e.table}.${r}`)}`,
|
|
1823
|
-
orElse: () => t`${t(n.column)}`
|
|
1824
|
-
}) : t`${t(n.column)}`;
|
|
1825
|
-
x.push(n.direction === "desc" ? t`${i} DESC` : t`${i} ASC`);
|
|
1826
|
-
}
|
|
1827
|
-
let ie = x.length > 0 ? t` ORDER BY ${t.csv(x)}` : b ? t.onDialectOrElse({
|
|
1828
|
-
mssql: () => t` ORDER BY (SELECT NULL)`,
|
|
1829
|
-
orElse: () => t``
|
|
1830
|
-
}) : t``, S = (e) => {
|
|
1831
|
-
if (!Number.isFinite(e) || !Number.isInteger(e) || e < 0) throw E(`compileSelect: expected non-negative integer take/skip, got ${e}`);
|
|
1832
|
-
return t.unsafe(String(e));
|
|
1833
|
-
}, ae = (e.ctes ?? []).map((e) => t`${t(e.name)} AS (${Z(e.descriptor, t, n)})`), oe = (e.ctes ?? []).some((e) => e.recursive === !0), se = ae.length > 0 ? t`WITH ${t.unsafe(oe ? "RECURSIVE " : "")}${t.csv(ae)} ` : t``;
|
|
1834
|
-
return t.onDialectOrElse({
|
|
1835
|
-
mssql: () => {
|
|
1836
|
-
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``;
|
|
1837
|
-
return t`${se}SELECT ${n}${f}${d} FROM ${te}${v}${re}${y}${ie}${r}`;
|
|
1838
|
-
},
|
|
1839
|
-
orElse: () => {
|
|
1840
|
-
let n = e.take === void 0 ? b ? t.onDialectOrElse({
|
|
1841
|
-
sqlite: () => t` LIMIT -1`,
|
|
1842
|
-
mysql: () => t` LIMIT 18446744073709551615`,
|
|
1843
|
-
orElse: () => t``
|
|
1844
|
-
}) : t`` : t` LIMIT ${S(e.take)}`, r = b ? t` OFFSET ${S(e.skip)}` : t``;
|
|
1845
|
-
return t`${se}SELECT ${f}${d} FROM ${te}${v}${re}${y}${ie}${n}${r}${h}`;
|
|
1846
|
-
}
|
|
1847
|
-
});
|
|
1848
|
-
}, Q = (e, t) => {
|
|
1849
|
-
let n = (e) => {
|
|
1850
|
-
if (typeof e != "string" || !/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/.test(e)) return null;
|
|
1851
|
-
let [t = "", n = ""] = e.replace(/^[+-]/, "").split("."), r = t.replace(/^0+/, "") || "0";
|
|
1852
|
-
return {
|
|
1853
|
-
negative: e.startsWith("-") && /[1-9]/.test(r + n),
|
|
1854
|
-
whole: r,
|
|
1855
|
-
fraction: n
|
|
1856
|
-
};
|
|
1857
|
-
}, r = n(e), i = n(t);
|
|
1858
|
-
if (!r || !i) return null;
|
|
1859
|
-
if (r.negative !== i.negative) return r.negative ? -1 : 1;
|
|
1860
|
-
let a = r.whole.length - i.whole.length;
|
|
1861
|
-
if (a === 0 && (a = r.whole === i.whole ? 0 : r.whole > i.whole ? 1 : -1), a === 0) {
|
|
1862
|
-
let e = Math.max(r.fraction.length, i.fraction.length), t = r.fraction.padEnd(e, "0"), n = i.fraction.padEnd(e, "0");
|
|
1863
|
-
a = t === n ? 0 : t > n ? 1 : -1;
|
|
1864
|
-
}
|
|
1865
|
-
return a === 0 ? 0 : (a > 0 ? 1 : -1) * (r.negative ? -1 : 1);
|
|
1866
|
-
}, Kn = (e, t) => ({
|
|
1867
|
-
column: e,
|
|
1868
|
-
op: "eq",
|
|
1869
|
-
value: t
|
|
1870
|
-
}), qn = (e, t) => ({
|
|
1871
|
-
column: e,
|
|
1872
|
-
op: "neq",
|
|
1873
|
-
value: t
|
|
1874
|
-
}), Jn = (e, t) => ({
|
|
1875
|
-
column: e,
|
|
1876
|
-
op: "gt",
|
|
1877
|
-
value: t
|
|
1878
|
-
}), Yn = (e, t) => ({
|
|
1879
|
-
column: e,
|
|
1880
|
-
op: "gte",
|
|
1881
|
-
value: t
|
|
1882
|
-
}), Xn = (e, t) => ({
|
|
1883
|
-
column: e,
|
|
1884
|
-
op: "lt",
|
|
1885
|
-
value: t
|
|
1886
|
-
}), Zn = (e, t) => ({
|
|
1887
|
-
column: e,
|
|
1888
|
-
op: "lte",
|
|
1889
|
-
value: t
|
|
1890
|
-
}), Qn = (e, t) => ({
|
|
1891
|
-
column: e,
|
|
1892
|
-
op: "in",
|
|
1893
|
-
value: t
|
|
1894
|
-
}), $n = (e, t) => ({
|
|
1895
|
-
column: e,
|
|
1896
|
-
op: "notIn",
|
|
1897
|
-
value: t
|
|
1898
|
-
}), er = (e, t) => ({
|
|
1899
|
-
column: e,
|
|
1900
|
-
op: "contains",
|
|
1901
|
-
value: t
|
|
1902
|
-
}), tr = (e, t) => ({
|
|
1903
|
-
column: e,
|
|
1904
|
-
op: "startsWith",
|
|
1905
|
-
value: t
|
|
1906
|
-
}), nr = (e, t) => ({
|
|
1907
|
-
column: e,
|
|
1908
|
-
op: "arrayContains",
|
|
1909
|
-
value: t
|
|
1910
|
-
}), rr = (e, t) => ({
|
|
1911
|
-
column: e,
|
|
1912
|
-
op: "arrayOverlaps",
|
|
1913
|
-
value: t
|
|
1914
|
-
}), ir = (e, t) => ({
|
|
1915
|
-
column: e,
|
|
1916
|
-
op: "arrayHas",
|
|
1917
|
-
value: t
|
|
1918
|
-
}), ar = {
|
|
1919
|
-
column: "id",
|
|
1920
|
-
op: "in",
|
|
1921
|
-
value: []
|
|
1922
|
-
}, or = (e) => ({
|
|
1923
|
-
column: e,
|
|
1924
|
-
op: "isNull"
|
|
1925
|
-
}), sr = (e) => ({
|
|
1926
|
-
column: e,
|
|
1927
|
-
op: "isNotNull"
|
|
1928
|
-
}), cr = (e, t) => ({
|
|
1929
|
-
column: e,
|
|
1930
|
-
op: "spatial",
|
|
1931
|
-
value: t
|
|
1932
|
-
}), lr = (...e) => ({ and: e }), ur = (...e) => ({ or: e }), dr = (e) => ({ not: e }), fr = (e, ...t) => {
|
|
1933
|
-
if (t.length === 0) throw E(`jsonField("${e}"): at least one path segment is required`);
|
|
1934
|
-
let n = (n, r) => ({
|
|
1935
|
-
column: e,
|
|
1936
|
-
path: t,
|
|
1937
|
-
op: n,
|
|
1938
|
-
value: r
|
|
1939
|
-
});
|
|
1940
|
-
return {
|
|
1941
|
-
eq: (e) => n("eq", e),
|
|
1942
|
-
neq: (e) => n("neq", e),
|
|
1943
|
-
gt: (e) => n("gt", e),
|
|
1944
|
-
gte: (e) => n("gte", e),
|
|
1945
|
-
lt: (e) => n("lt", e),
|
|
1946
|
-
lte: (e) => n("lte", e),
|
|
1947
|
-
inSet: (e) => n("in", e),
|
|
1948
|
-
notInSet: (e) => n("notIn", e),
|
|
1949
|
-
contains: (e) => n("contains", e),
|
|
1950
|
-
startsWith: (e) => n("startsWith", e),
|
|
1951
|
-
isNull: () => n("isNull"),
|
|
1952
|
-
isNotNull: () => n("isNotNull")
|
|
1953
|
-
};
|
|
1954
|
-
}, $ = (e) => "descriptor" in e ? e.descriptor : e, pr = (e, t) => ({
|
|
1955
|
-
column: e,
|
|
1956
|
-
op: "subquery-in",
|
|
1957
|
-
subquery: $(t)
|
|
1958
|
-
}), mr = (e, t) => ({
|
|
1959
|
-
column: e,
|
|
1960
|
-
op: "subquery-not-in",
|
|
1961
|
-
subquery: $(t)
|
|
1962
|
-
}), hr = (e) => ({
|
|
1963
|
-
op: "exists",
|
|
1964
|
-
subquery: $(e)
|
|
1965
|
-
}), gr = (e) => ({
|
|
1966
|
-
op: "not-exists",
|
|
1967
|
-
subquery: $(e)
|
|
1968
|
-
}), _r = /^\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?)?$/, vr = (e) => {
|
|
1969
|
-
if (e instanceof Date) return e.getTime();
|
|
1970
|
-
if (typeof e == "string" && _r.test(e)) {
|
|
1971
|
-
let t = Date.parse(e);
|
|
1972
|
-
return Number.isNaN(t) ? void 0 : t;
|
|
1973
|
-
}
|
|
1974
|
-
}, yr = (e, t) => {
|
|
1975
|
-
if (!(e instanceof Date) && !(t instanceof Date)) return;
|
|
1976
|
-
let n = vr(e), r = vr(t);
|
|
1977
|
-
return n === void 0 || r === void 0 ? void 0 : [n, r];
|
|
1978
|
-
}, br = (e, t) => {
|
|
1979
|
-
if (e === t) return !0;
|
|
1980
|
-
let n = yr(e, t);
|
|
1981
|
-
return n !== void 0 && n[0] === n[1];
|
|
1982
|
-
}, xr = (e, t) => {
|
|
1983
|
-
if (e == null || t == null) return null;
|
|
1984
|
-
let n = yr(e, t);
|
|
1985
|
-
if (n !== void 0) return n[0] === n[1] ? 0 : n[0] > n[1] ? 1 : -1;
|
|
1986
|
-
let r = e instanceof Date ? e.getTime() : e, i = t instanceof Date ? t.getTime() : t;
|
|
1987
|
-
return typeof r == typeof i ? r === i ? 0 : r > i ? 1 : -1 : null;
|
|
1988
|
-
}, Sr = (e, t, n = null, r = {}) => Cr(e, t, n, r) === !0, Cr = (e, t, n = null, r = {}) => {
|
|
1989
|
-
if (!t) return !1;
|
|
1990
|
-
if ("not" in e) {
|
|
1991
|
-
let i = Cr(e.not, t, n, r);
|
|
1992
|
-
return i === null ? null : !i;
|
|
1993
|
-
}
|
|
1994
|
-
if ("and" in e) {
|
|
1995
|
-
let i = !1;
|
|
1996
|
-
for (let a of e.and) {
|
|
1997
|
-
let e = Cr(a, t, n, r);
|
|
1998
|
-
if (e === !1) return !1;
|
|
1999
|
-
e === null && (i = !0);
|
|
2000
|
-
}
|
|
2001
|
-
return !i || null;
|
|
2002
|
-
}
|
|
2003
|
-
if ("or" in e) {
|
|
2004
|
-
let i = !1;
|
|
2005
|
-
for (let a of e.or) {
|
|
2006
|
-
let e = Cr(a, t, n, r);
|
|
2007
|
-
if (e === !0) return !0;
|
|
2008
|
-
e === null && (i = !0);
|
|
2009
|
-
}
|
|
2010
|
-
return i ? null : !1;
|
|
2011
|
-
}
|
|
2012
|
-
if (e.op === "subquery-in" || e.op === "subquery-not-in") {
|
|
2013
|
-
if (!n) return !1;
|
|
2014
|
-
let i = n.get(e.subquery);
|
|
2015
|
-
if (!(i instanceof Set)) return !1;
|
|
2016
|
-
let a = t[e.column], o = r[e.column]?.type === "decimal" ? [...i].some((e) => Q(a, e) === 0) : i.has(a);
|
|
2017
|
-
return e.op === "subquery-in" ? o : !o;
|
|
2018
|
-
}
|
|
2019
|
-
if (e.op === "exists" || e.op === "not-exists") {
|
|
2020
|
-
if (!n) return !1;
|
|
2021
|
-
let t = n.get(e.subquery), r = typeof t == "number" ? t > 0 : t instanceof Set && t.size > 0;
|
|
2022
|
-
return e.op === "exists" ? r : !r;
|
|
2023
|
-
}
|
|
2024
|
-
let i = e, a = t[i.column], o = G(i.value) ? i.value : void 0;
|
|
2025
|
-
if (o !== void 0 && (!gn.some((e) => e === i.op) || i.path !== void 0)) throw E("Column references require a scalar comparison without a JSON path");
|
|
2026
|
-
let s = o === void 0 ? i.value : t[o.column];
|
|
2027
|
-
if (o !== void 0 && (a == null || s == null)) return null;
|
|
2028
|
-
let c = r[i.column]?.type === "decimal", l = (e, t) => c && e != null && t != null ? Q(e, t) === 0 : br(e, t), u = c ? Q : xr;
|
|
2029
|
-
switch (i.op) {
|
|
2030
|
-
case "eq": return l(a, s);
|
|
2031
|
-
case "neq": return !l(a, s);
|
|
2032
|
-
case "gt": return (u(a, s) ?? -1) > 0;
|
|
2033
|
-
case "gte": return (u(a, s) ?? -1) >= 0;
|
|
2034
|
-
case "lt": return (u(a, s) ?? 1) < 0;
|
|
2035
|
-
case "lte": return (u(a, s) ?? 1) <= 0;
|
|
2036
|
-
case "in": return Array.isArray(s) && s.some((e) => l(a, e));
|
|
2037
|
-
case "notIn": return Array.isArray(s) && !s.some((e) => l(a, e));
|
|
2038
|
-
case "contains": return typeof a == "string" && typeof s == "string" && a.toLowerCase().includes(s.toLowerCase());
|
|
2039
|
-
case "startsWith": return typeof a == "string" && typeof s == "string" && a.startsWith(s);
|
|
2040
|
-
case "arrayContains": {
|
|
2041
|
-
let e = Array.isArray(a) ? a : [];
|
|
2042
|
-
return (Array.isArray(s) ? s : []).every((t) => e.includes(t));
|
|
2043
|
-
}
|
|
2044
|
-
case "arrayOverlaps": {
|
|
2045
|
-
let e = Array.isArray(a) ? a : [];
|
|
2046
|
-
return (Array.isArray(s) ? s : []).some((t) => e.includes(t));
|
|
2047
|
-
}
|
|
2048
|
-
case "arrayHas": return Array.isArray(a) && a.includes(s);
|
|
2049
|
-
case "isNull": return a == null;
|
|
2050
|
-
case "isNotNull": return a != null;
|
|
2051
|
-
case "spatial": throw E("@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.");
|
|
2052
|
-
}
|
|
2053
|
-
}, wr = function* (e) {
|
|
2054
|
-
if ("not" in e) {
|
|
2055
|
-
yield* wr(e.not);
|
|
2056
|
-
return;
|
|
2057
|
-
}
|
|
2058
|
-
if ("and" in e) {
|
|
2059
|
-
for (let t of e.and) yield* wr(t);
|
|
2060
|
-
return;
|
|
2061
|
-
}
|
|
2062
|
-
if ("or" in e) {
|
|
2063
|
-
for (let t of e.or) yield* wr(t);
|
|
2064
|
-
return;
|
|
2065
|
-
}
|
|
2066
|
-
e.op === "subquery-in" || e.op === "subquery-not-in" ? yield {
|
|
2067
|
-
kind: "in",
|
|
2068
|
-
descriptor: e.subquery
|
|
2069
|
-
} : (e.op === "exists" || e.op === "not-exists") && (yield {
|
|
2070
|
-
kind: "exists",
|
|
2071
|
-
descriptor: e.subquery
|
|
2072
|
-
});
|
|
2073
|
-
}, Tr = function* (e, t = !0) {
|
|
2074
|
-
if ("not" in e) {
|
|
2075
|
-
yield* Tr(e.not, !1);
|
|
2076
|
-
return;
|
|
2077
|
-
}
|
|
2078
|
-
if ("and" in e) {
|
|
2079
|
-
for (let n of e.and) yield* Tr(n, t);
|
|
2080
|
-
return;
|
|
2081
|
-
}
|
|
2082
|
-
if ("or" in e) {
|
|
2083
|
-
for (let t of e.or) yield* Tr(t, !1);
|
|
2084
|
-
return;
|
|
2085
|
-
}
|
|
2086
|
-
e.op !== "subquery-in" && e.op !== "subquery-not-in" && e.op !== "exists" && e.op !== "not-exists" && (yield {
|
|
2087
|
-
...e,
|
|
2088
|
-
conjunctive: t && !G(e.value)
|
|
2089
|
-
});
|
|
2090
|
-
}, Er = (e, t) => ({
|
|
2091
|
-
name: e,
|
|
2092
|
-
body: t
|
|
2093
|
-
}), Dr = (e) => {
|
|
2094
|
-
if (e.lifecycle === "cron" && !e.cron) throw Error(`seed "${e.id}": lifecycle 'cron' requires a 'cron' field`);
|
|
2095
|
-
if (e.lifecycle === "onSchemaChange" && (!e.watchedTables || e.watchedTables.length === 0)) throw Error(`seed "${e.id}": lifecycle 'onSchemaChange' requires at least one entry in 'watchedTables'`);
|
|
2096
|
-
let t = e.steps({ step: Er });
|
|
2097
|
-
if (t.length === 0) throw Error(`seed "${e.id}": at least one step is required`);
|
|
2098
|
-
return {
|
|
2099
|
-
__seed: !0,
|
|
2100
|
-
id: e.id,
|
|
2101
|
-
name: e.name,
|
|
2102
|
-
lifecycle: e.lifecycle,
|
|
2103
|
-
...e.cron === void 0 ? {} : { cron: e.cron },
|
|
2104
|
-
...e.timezone === void 0 ? {} : { timezone: e.timezone },
|
|
2105
|
-
...e.watchedTables === void 0 ? {} : { watchedTables: e.watchedTables },
|
|
2106
|
-
...e.fingerprint === void 0 ? {} : { fingerprint: e.fingerprint },
|
|
2107
|
-
steps: t
|
|
2108
|
-
};
|
|
2109
|
-
}, Or = (e) => typeof e == "object" && !!e && "__seed" in e && e.__seed === !0, kr = null, Ar = null, jr = (e) => {
|
|
2110
|
-
kr = e;
|
|
2111
|
-
}, Mr = (e) => {
|
|
2112
|
-
Ar = e;
|
|
2113
|
-
}, Nr = async (e) => {
|
|
2114
|
-
let t = kr;
|
|
2115
|
-
t !== null && await t(e);
|
|
2116
|
-
}, Pr = async (e) => {
|
|
2117
|
-
let t = Ar;
|
|
2118
|
-
t !== null && e.changedTables.length !== 0 && await t(e);
|
|
2119
|
-
}, Fr = ["before-schema", "after-schema"], Ir = (e) => {
|
|
2120
|
-
if (!e.id || e.id.length === 0) throw Error("migration: `id` is required + must be non-empty");
|
|
2121
|
-
if (!e.up || !e.down) throw Error(`migration ${e.id}: both \`up\` and \`down\` must be functions`);
|
|
2122
|
-
if (!Fr.includes(e.phase)) throw Error(`migration ${e.id}: \`phase\` is required and must be one of ${Fr.map((e) => `'${e}'`).join(" | ")} — 'before-schema' runs before the declarative apply (a shape change the planner must not compute against), 'after-schema' runs after it (a body that needs the new shape, such as a back-fill into a table this deploy creates)`);
|
|
2123
|
-
return e;
|
|
2124
|
-
}, Lr = Symbol.for("voltro.migration.irreversible"), Rr = class extends Error {
|
|
2125
|
-
migrationId;
|
|
2126
|
-
reason;
|
|
2127
|
-
_tag = "IrreversibleMigrationError";
|
|
2128
|
-
constructor(e, t) {
|
|
2129
|
-
super(`migration ${e} cannot be rolled back: ${t}`), this.migrationId = e, this.reason = t, this.name = "IrreversibleMigrationError";
|
|
2130
|
-
}
|
|
2131
|
-
}, zr = (e) => {
|
|
2132
|
-
if (!e || e.trim().length === 0) throw Error("irreversible(): a reason is required");
|
|
2133
|
-
return Object.assign(() => t.fail(new Rr("<unknown>", e)), { [Lr]: e });
|
|
2134
|
-
}, Br = (e) => {
|
|
2135
|
-
let t = e[Lr];
|
|
2136
|
-
return typeof t == "string" ? t : void 0;
|
|
2137
|
-
}, Vr = class {
|
|
2138
|
-
reason;
|
|
2139
|
-
_tag = "MigrationSkipped";
|
|
2140
|
-
constructor(e) {
|
|
2141
|
-
this.reason = e;
|
|
2142
|
-
}
|
|
2143
|
-
}, Hr = class extends Error {
|
|
2144
|
-
migrationId;
|
|
2145
|
-
failed;
|
|
2146
|
-
_tag = "MigrationExpectationFailed";
|
|
2147
|
-
constructor(e, t) {
|
|
2148
|
-
super(`migration ${e}: ${t.length} post-condition(s) did not hold after up():\n` + t.map((e) => ` - ${e}`).join("\n") + "\n The migration is NOT recorded; the next run applies it again."), this.migrationId = e, this.failed = t, this.name = "MigrationExpectationFailed";
|
|
2149
|
-
}
|
|
2150
|
-
}, Ur = class extends Error {
|
|
2151
|
-
migrationId;
|
|
2152
|
-
dialect;
|
|
2153
|
-
_tag = "TransactionBrokenByBodyError";
|
|
2154
|
-
constructor(e, t) {
|
|
2155
|
-
super(`migration ${e} is transactional, and its body COMMITTED the transaction from inside — on ${t} a DDL statement commits everything before it. The work is in the database and the migration is recorded; it will not run again. What did not hold is the all-or-nothing guarantee. Either mark it \`transactional: false\` (per-statement, resumable through your own idempotency) or move the schema change into \`ctx.schema\` in a migration of its own and keep this one to data.`), this.migrationId = e, this.dialect = t, this.name = "TransactionBrokenByBodyError";
|
|
2156
|
-
}
|
|
2157
|
-
}, Wr = class extends Error {
|
|
2158
|
-
migrationId;
|
|
2159
|
-
operation;
|
|
2160
|
-
_tag = "MigrationNeedsSqlClient";
|
|
2161
|
-
constructor(e, t, n) {
|
|
2162
|
-
super(`migration ${e}: ${t} — ${n}`), this.migrationId = e, this.operation = t, this.name = "MigrationNeedsSqlClient";
|
|
2163
|
-
}
|
|
2164
|
-
}, Gr = class extends Error {
|
|
2165
|
-
table;
|
|
2166
|
-
report;
|
|
2167
|
-
_tag = "RewriteUnstableError";
|
|
2168
|
-
constructor(e, t) {
|
|
2169
|
-
super(`rewrite of ${e} did not stabilise after ${t.passes} pass(es): ${t.rewritten} rewritten, ${t.contended} contended on the last pass. Check for concurrent writes and guard values that cannot round-trip exactly before raising maxPasses.`), this.table = e, this.report = t, this.name = "RewriteUnstableError";
|
|
2170
|
-
}
|
|
2171
|
-
}, Kr = /^\d{8}_\d{6}_[a-z0-9_]+\.ts$/, qr = (e) => typeof e == "object" && !!e && "id" in e && "up" in e && "down" in e && typeof e.id == "string" && typeof e.up == "function" && typeof e.down == "function", Jr = /* @__PURE__ */ new Set(["voltro_isr_cache"]), Yr = (e) => Jr.has(e) || e.startsWith("_voltro_") || e.startsWith("_cloud_") || e.startsWith("cluster_") || e.startsWith("__turso_internal_"), Xr = "_voltro_staging_", Zr = (e) => e.startsWith(Xr), Qr = /__dropped_\d{14}$/, $r = (e) => Qr.test(e), ei = 7, ti = (e) => {
|
|
2172
|
-
let t = /__dropped_(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/.exec(e);
|
|
2173
|
-
if (t === null) return;
|
|
2174
|
-
let [, n, r, i, a, o, s] = t;
|
|
2175
|
-
return new Date(Date.UTC(Number(n), Number(r) - 1, Number(i), Number(a), Number(o), Number(s)));
|
|
2176
|
-
}, ni = (e, t = {}) => {
|
|
2177
|
-
let n = t.now ?? /* @__PURE__ */ new Date(), r = t.retentionDays ?? 7, i = [], a = (e, t) => {
|
|
2178
|
-
let a = ti(e);
|
|
2179
|
-
if (a === void 0) return;
|
|
2180
|
-
let o = Math.floor((n.getTime() - a.getTime()) / 864e5);
|
|
2181
|
-
o >= r && i.push({
|
|
2182
|
-
name: t,
|
|
2183
|
-
droppedAt: a,
|
|
2184
|
-
ageDays: o
|
|
2185
|
-
});
|
|
2186
|
-
};
|
|
2187
|
-
for (let t of e.tables) if (a(t.name, t.name), !$r(t.name)) for (let e of t.columns ?? []) a(e.name, `${t.name}.${e.name}`);
|
|
2188
|
-
return i;
|
|
2189
|
-
}, ri = (e, t) => {
|
|
2190
|
-
let n = e.reduce((e, t) => e.ageDays >= t.ageDays ? e : t), r = (/* @__PURE__ */ new Date(Date.now() - t * 864e5)).toISOString().slice(0, 10);
|
|
2191
|
-
return `${e.length} soft-drop snapshot(s) older than ${t} day(s) still hold the dropped data (oldest: ${n.name}, ${n.ageDays}d) — ${e.map((e) => e.name).join(", ")}. Restore with \`voltro db restore-snapshot\` or reclaim with \`voltro db gc-snapshots --before ${r}\`; the window is \`database.softDropRetentionDays\` in app.config.`;
|
|
2192
|
-
}, ii = (e, t) => e.filter((e) => !Yr(e.name) || t.has(e.name)), ai = L("_voltro_migrations", {
|
|
2193
|
-
id: f({ prefix: "mig" }),
|
|
2194
|
-
name: p(),
|
|
2195
|
-
hash: p(),
|
|
2196
|
-
status: p(),
|
|
2197
|
-
appliedAt: v().nullable(),
|
|
2198
|
-
durationMs: m().nullable(),
|
|
2199
|
-
errorMessage: p().nullable(),
|
|
2200
|
-
errorStack: p().nullable(),
|
|
2201
|
-
workflowRunId: p().nullable(),
|
|
2202
|
-
createdAt: v().default("now"),
|
|
2203
|
-
updatedAt: v().default("now").onUpdate("now")
|
|
2204
|
-
}).index(["status"]), oi = L("_voltro_seeds", {
|
|
2205
|
-
id: f({ prefix: "seed" }),
|
|
2206
|
-
name: p(),
|
|
2207
|
-
lifecycle: p(),
|
|
2208
|
-
fingerprint: p(),
|
|
2209
|
-
cronSchedule: p().nullable(),
|
|
2210
|
-
watchedTables: y().nullable(),
|
|
2211
|
-
nextRunAt: v().nullable(),
|
|
2212
|
-
lastRunStatus: p().nullable(),
|
|
2213
|
-
lastRunAt: v().nullable(),
|
|
2214
|
-
lastRunDurationMs: m().nullable(),
|
|
2215
|
-
lastRunRowsTouched: m().nullable(),
|
|
2216
|
-
lastError: p().nullable(),
|
|
2217
|
-
workflowRunId: p().nullable(),
|
|
2218
|
-
createdAt: v().default("now"),
|
|
2219
|
-
updatedAt: v().default("now").onUpdate("now")
|
|
2220
|
-
}).index(["lifecycle"]).index(["nextRunAt"]), si = L("_voltro_schema_owner", {
|
|
2221
|
-
id: f({ prefix: "own" }),
|
|
2222
|
-
appName: p(),
|
|
2223
|
-
projectName: p().nullable(),
|
|
2224
|
-
frameworkVersion: p().nullable(),
|
|
2225
|
-
claimedAt: v().default("now")
|
|
2226
|
-
}), ci = L("_voltro_plugin_migrations", {
|
|
2227
|
-
id: f({ prefix: "plm" }),
|
|
2228
|
-
plugin_name: p(),
|
|
2229
|
-
migration_id: p(),
|
|
2230
|
-
description: p().nullable(),
|
|
2231
|
-
applied_at: p()
|
|
2232
|
-
}), li = L("_voltro_migration_plans", {
|
|
2233
|
-
id: f({ prefix: "plan" }),
|
|
2234
|
-
fingerprint: p(),
|
|
2235
|
-
liveFingerprint: p().nullable(),
|
|
2236
|
-
operations: y(),
|
|
2237
|
-
rollbackPlan: y().nullable(),
|
|
2238
|
-
appliedBy: p(),
|
|
2239
|
-
environment: p(),
|
|
2240
|
-
source: p(),
|
|
2241
|
-
onlineStrategy: p().nullable(),
|
|
2242
|
-
durationMs: m(),
|
|
2243
|
-
notes: p().nullable(),
|
|
2244
|
-
squashedAt: v().nullable(),
|
|
2245
|
-
appliedAt: v().default("now"),
|
|
2246
|
-
createdAt: v().default("now"),
|
|
2247
|
-
updatedAt: v().default("now").onUpdate("now")
|
|
2248
|
-
}).index(["fingerprint"]).index(["environment"]), ui = L("_voltro_idempotency", {
|
|
2249
|
-
id: f({ prefix: "idem" }),
|
|
2250
|
-
scope: p(),
|
|
2251
|
-
key: p(),
|
|
2252
|
-
status: p(),
|
|
2253
|
-
response: y().nullable(),
|
|
2254
|
-
createdAt: v().default("now")
|
|
2255
|
-
}).unique(["scope", "key"]).index(["createdAt"]), di = L("_voltro_devices", {
|
|
2256
|
-
id: f({ prefix: "device" }),
|
|
2257
|
-
tenantId: p(),
|
|
2258
|
-
name: p(),
|
|
2259
|
-
scopes: y().nullable(),
|
|
2260
|
-
pairingHash: p().maxLength(64).nullable(),
|
|
2261
|
-
pairingExpiresAt: v().nullable(),
|
|
2262
|
-
accessHash: p().maxLength(64).nullable(),
|
|
2263
|
-
accessExpiresAt: v().nullable(),
|
|
2264
|
-
refreshHash: p().maxLength(64).nullable(),
|
|
2265
|
-
refreshExpiresAt: v().nullable(),
|
|
2266
|
-
previousRefreshHash: p().maxLength(64).nullable(),
|
|
2267
|
-
generation: m().default(0),
|
|
2268
|
-
pairedAt: v().nullable(),
|
|
2269
|
-
lastSeenAt: v().nullable(),
|
|
2270
|
-
revokedAt: v().nullable(),
|
|
2271
|
-
revokedReason: p().nullable(),
|
|
2272
|
-
createdBy: p().nullable(),
|
|
2273
|
-
metadata: y().nullable(),
|
|
2274
|
-
createdAt: v()
|
|
2275
|
-
}).index(["tenantId"]).index(["pairingHash"]).index(["accessHash"]).index(["refreshHash"]).index(["previousRefreshHash"]), fi = L("_voltro_tenant_selections", {
|
|
2276
|
-
id: f({ prefix: "tsel" }),
|
|
2277
|
-
subjectType: p().maxLength(32),
|
|
2278
|
-
subjectId: p(),
|
|
2279
|
-
tenantId: p(),
|
|
2280
|
-
selectedAt: v()
|
|
2281
|
-
}).primaryKey(["id"]).nonReactive(), pi = L("_voltro_kv", {
|
|
2282
|
-
id: f({ prefix: "kv" }),
|
|
2283
|
-
key: p().unique(),
|
|
2284
|
-
value: p().nullable(),
|
|
2285
|
-
expiresAt: v().nullable(),
|
|
2286
|
-
createdAt: v().default("now"),
|
|
2287
|
-
updatedAt: v().default("now").onUpdate("now")
|
|
2288
|
-
}).index(["expiresAt"]), mi = L("_voltro_api_keys", {
|
|
2289
|
-
id: f({ prefix: "apikey" }),
|
|
2290
|
-
tenantId: p().nullable(),
|
|
2291
|
-
name: p(),
|
|
2292
|
-
hashedKey: p().maxLength(64).unique(),
|
|
2293
|
-
keyPrefix: p(),
|
|
2294
|
-
scopes: y().nullable(),
|
|
2295
|
-
lastUsedAt: v().nullable(),
|
|
2296
|
-
requestCount: m().default(0),
|
|
2297
|
-
expiresAt: v().nullable(),
|
|
2298
|
-
revokedAt: v().nullable(),
|
|
2299
|
-
createdBy: p().nullable(),
|
|
2300
|
-
onBehalfOf: p().nullable(),
|
|
2301
|
-
metadata: y().nullable(),
|
|
2302
|
-
createdAt: v().default("now")
|
|
2303
|
-
}).index("byApiKeyTenant", ["tenantId"]), hi = L("_voltro_replica_observations", {
|
|
2304
|
-
id: f({ prefix: "robs" }),
|
|
2305
|
-
replicaId: p().maxLength(191),
|
|
2306
|
-
startedAt: v(),
|
|
2307
|
-
version: p().maxLength(64).nullable(),
|
|
2308
|
-
kind: p().maxLength(64),
|
|
2309
|
-
payload: p().nullable(),
|
|
2310
|
-
reachableAt: p().maxLength(255).nullable(),
|
|
2311
|
-
writtenAt: v().default("now").onUpdate("now")
|
|
2312
|
-
}).unique(["replicaId", "kind"]).index(["kind"]), gi = [
|
|
2313
|
-
ai,
|
|
2314
|
-
oi,
|
|
2315
|
-
li
|
|
2316
|
-
], _i = "_voltro_replace_in_progress", vi = L(_i, {
|
|
2317
|
-
id: f({ prefix: "repl" }),
|
|
2318
|
-
startedAt: v().default("now"),
|
|
2319
|
-
kind: p().default("replace"),
|
|
2320
|
-
tables: p().nullable(),
|
|
2321
|
-
capture: p().nullable(),
|
|
2322
|
-
via: p(),
|
|
2323
|
-
staging: p().nullable(),
|
|
2324
|
-
heartbeatAt: v().nullable(),
|
|
2325
|
-
resumable: _().nullable()
|
|
2326
|
-
}).nonReactive(), yi = 30, bi = (e, t) => {
|
|
2327
|
-
let n = typeof t == "number" && Number.isFinite(t) && t > 0 ? t : 30, r = Number(e.VOLTRO_STAGING_STALE_MINUTES);
|
|
2328
|
-
return (Number.isFinite(r) && r > 0 ? r : n) * 6e4;
|
|
2329
|
-
}, xi = (e) => e == null || String(e).length === 0 ? [] : String(e).split(",").map((e) => e.trim()).filter((e) => e.length > 0), Si = (e) => {
|
|
2330
|
-
let t = e.filter((e) => e.kind !== "staging");
|
|
2331
|
-
if (t.length !== 0) return `refusing to start: a destructive data operation did not finish.
|
|
2332
|
-
${t.map((e) => {
|
|
2333
|
-
let t = Number.isFinite(e.startedAt.getTime()) ? `${Math.max(0, Math.round((Date.now() - e.startedAt.getTime()) / 6e4))} minute(s) ago` : "at an unknown time";
|
|
2334
|
-
if (e.kind === "restore") return ` - a native \`restore\` over ${e.via} began loading an artefact over this database ${t}\n and never reported finishing.\n` + (e.capture === null ? " The artefact it was loading was not recorded. Re-run the restore from your backup\n directory, or restore from another backup." : ` The artefact it was loading:\n ${e.capture}\n Re-run it to completion with: voltro data restore ${e.capture} --allow-live`);
|
|
2335
|
-
let n = e.tables === null ? "this database" : `${e.tables} table(s)`;
|
|
2336
|
-
return ` - a \`replace\` over ${e.via} began emptying ${n} ${t} and never reported finishing.\n` + (e.capture === null ? " NO capture was taken for that run, so the previous rows are not recoverable from here.\n Restore from your own backup." : ` The target's previous rows were captured first:\n ${e.capture}\n Restore them with: voltro data import ${e.capture} --mode replace`);
|
|
2337
|
-
}).join("\n")}\n
|
|
2338
|
-
A half-replaced database is indistinguishable from an empty one from the inside — every table
|
|
2339
|
-
exists, every constraint holds, and every query returns nothing without erroring. That is why
|
|
2340
|
-
this is a refusal and not a warning.
|
|
2341
|
-
|
|
2342
|
-
When the data is back, or you have decided the current state is correct, clear the marker:
|
|
2343
|
-
voltro data clear-replace-marker --yes`;
|
|
2344
|
-
}, Ci = (e, t, n) => {
|
|
2345
|
-
let r = e.filter((e) => e.kind === "staging");
|
|
2346
|
-
return {
|
|
2347
|
-
all: r,
|
|
2348
|
-
droppable: r.filter((e) => {
|
|
2349
|
-
if (e.resumable) return !1;
|
|
2350
|
-
let r = (e.heartbeatAt ?? e.startedAt).getTime();
|
|
2351
|
-
return Number.isFinite(r) ? t - r >= n : !1;
|
|
2352
|
-
})
|
|
2353
|
-
};
|
|
2354
|
-
}, wi = (e, t) => {
|
|
2355
|
-
if (e.all.length === 0) return;
|
|
2356
|
-
let n = new Set(e.droppable.map((e) => e.id));
|
|
2357
|
-
return `staged data-import leftovers:
|
|
2358
|
-
${e.all.map((e) => {
|
|
2359
|
-
let r = e.heartbeatAt ?? e.startedAt, i = Number.isFinite(r.getTime()) ? `${Math.max(0, Math.round((t - r.getTime()) / 6e4))} minute(s) ago` : "at an unknown time", a = `${e.staging.length} staging table(s) from a \`replace\` over ${e.via}, last active ${i}`;
|
|
2360
|
-
return n.has(e.id) ? ` - ${a} — DROPPED: the run is not resumable and has been silent long enough that nothing is loading into them.` : e.resumable ? ` - ${a} — KEPT: the run was started \`--no-atomic\`, so these ARE its resume point. Re-run the same\n bundle to continue from them, or drop them with: voltro data clear-staging --yes` : ` - ${a} — KEPT: still beating, so an import is loading into them right now.`;
|
|
2361
|
-
}).join("\n")}\n The target of a staged \`replace\` is untouched until one short swap at the end, so none of this is
|
|
2362
|
-
a reason to refuse the boot — it is a reason to know the disk is holding a copy of a bundle.`;
|
|
2363
|
-
};
|
|
2364
|
-
//#endregion
|
|
2365
|
-
export { rr as $, Le as $n, en as $t, ti as A, mt as An, de as Ar, An as At, Br as B, at as Bn, o as Br, un as Bt, Xr as C, Tt as Cn, ne as Cr, Q as Ct, $r as D, xt as Dn, pe as Dr, Z as Dt, Yr as E, St as En, fe as Er, Gn as Et, Wr as F, lt as Fn, me as Fr, Tn as Ft, Nr as G, M as Gn, rn as Gt, Ir as H, Ve as Hn, v as Hr, dn as Ht, Vr as I, dt as In, c as Ir, K as It, jr as J, P as Jn, Ft as Jt, Or as K, N as Kn, nn as Kt, Gr as L, ot as Ln, h as Lr, _n as Lt, Rr as M, L as Mn, C as Mr, xn as Mt, Fr as N, pt as Nn, ee as Nr, q as Nt, Zr as O, bt as On, f as Or, J as Ot, Hr as P, ut as Pn, ce as Pr, En as Pt, ir as Q, Pe as Qn, Nt as Qt, Ur as R, ct as Rn, oe as Rr, hn as Rt, Jr as S, jt as Sn, re as Sr, Tr as St, ri as T, kt as Tn, se as Tr, Mn as Tt, Dr as U, He as Un, le as Ur, mn as Ut, qr as V, nt as Vn, p as Vr, fn as Vt, Pr as W, Ue as Wn, tn as Wt, lr as X, Oe as Xn, Rt as Xt, ar as Y, E as Yn, Yt, nr as Z, T as Zn, Mt as Zt, si as _, Bt as _n, b as _r, $n as _t, wi as a, Lt as an, Ee as ar, Jn as at, gi as b, Et as bn, s as br, cr as bt, bi as c, $t as cn, ve as cr, pr as ct, ui as d, Wt as dn, ge as dr, fr as dt, Gt as en, Re as er, wr as et, pi as f, Xt as fn, u as fr, Xn as ft, hi as g, zt as gn, _ as gr, gr as gt, ci as h, Jt as hn, te as hr, dr as ht, Si as i, qt as in, Ie as ir, hr as it, Kr as j, ft as jn, y as jr, Sn as jt, ii as k, R as kn, m as kr, kn as kt, mi as l, Qt as ln, ye as lr, sr as lt, ai as m, Pt as mn, ue as mr, qn as mt, yi as n, Vt as nn, Te as nr, Kn as nt, Ci as o, It as on, be as or, Yn as ot, li as p, Ut as pn, S as pr, Zn as pt, Mr as q, j as qn, an as qt, vi as r, Kt as rn, Fe as rr, Sr as rt, xi as s, V as sn, xe as sr, Qn as st, _i as t, Ht as tn, ze as tr, er as tt, di as u, Zt as un, _e as ur, or as ut, oi as v, Ot as vn, ie as vr, mr as vt, ni as w, Dt as wn, g as wr, Y as wt, ei as x, B as xn, x as xr, tr as xt, fi as y, At as yn, l as yr, ur as yt, zr as z, st as zn, ae as zr, G as zt };
|