@redocly/replay 0.27.0-next.4 → 0.27.0-next.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/replay-AiChatProvider-D7E0a83D.js +1006 -0
- package/dist/replay-AiChatProvider-p9SioFWy.js +6 -0
- package/dist/replay-AiChatSqliteDBStorage-CKQJ5VZi.js +88 -0
- package/dist/replay-AiChatSqliteDBStorage-DgGB1XKW.js +1 -0
- package/dist/replay-AiPanel-BLkqnlmb.js +849 -0
- package/dist/replay-AiPanel-DvOGYsWl.js +2918 -0
- package/dist/replay-HistorySqliteDBStorage-BrILW8Fu.js +1 -0
- package/dist/replay-HistorySqliteDBStorage-CtF6-vY8.js +113 -0
- package/dist/replay-SqliteDBStorage-CNJbZXTw.js +14 -0
- package/dist/replay-SqliteDBStorage-D_31Bd3Z.js +2857 -0
- package/dist/replay-StepContextSqliteDBStorage-BSepLyBI.js +1 -0
- package/dist/replay-StepContextSqliteDBStorage-BfAAKDwA.js +83 -0
- package/dist/replay-agent-loop-DklepPbJ.js +1533 -0
- package/dist/replay-agent-loop-fKCHgyOD.js +1 -0
- package/dist/replay-ai-chat-mappers-D-vUBq2s.js +1 -0
- package/dist/replay-ai-chat-mappers-IWtvE0Nl.js +99 -0
- package/dist/replay-ai-message-parts.schema-BBdjwldJ.js +1 -0
- package/dist/replay-ai-message-parts.schema-E9u9-nqr.js +60 -0
- package/dist/replay-create-model-CfrWgdF7.js +38224 -0
- package/dist/replay-create-model-xGRAIhcu.js +104 -0
- package/dist/replay-db-BtdwaWh3.js +306 -0
- package/dist/replay-db-XlueEDU-.js +64 -0
- package/dist/replay-defaults-CzR6jw9a.js +32 -0
- package/dist/replay-defaults-DVvOnvnR.js +27 -0
- package/dist/replay-external-resolver-D6VSVDwt.js +34 -0
- package/dist/replay-external-resolver-Skh0OGde.js +2 -0
- package/dist/replay-history.schema-5ce6X0R1.js +28 -0
- package/dist/replay-history.schema-miM3Etuf.js +1 -0
- package/dist/replay-index-BEzVYZeb.js +504 -0
- package/dist/{replay-index-DCclzbQl.js → replay-index-BOB__Jm6.js} +1 -1
- package/dist/replay-index-C6SJPGDO.js +23 -0
- package/dist/{replay-index-D6LeDb4M.js → replay-index-CCvitTKt.js} +6 -6
- package/dist/replay-index-CUs62_1f.js +1 -0
- package/dist/replay-index-DXnDNeqk.js +93088 -0
- package/dist/replay-index-S_wAPmTA.js +6877 -0
- package/dist/replay-index-fVC8e5y9.js +1 -0
- package/dist/replay-indexes-CfsvVMi_.js +1242 -0
- package/dist/replay-indexes-Dqjl8m6C.js +1 -0
- package/dist/replay-lint-BCVeL_wb.js +184 -0
- package/dist/replay-lint-DjMIgYfF.js +33416 -0
- package/dist/{replay-lint-graphql-Cdmm_aJs.js → replay-lint-graphql-Bltv-T6_.js} +2 -2
- package/dist/{replay-lint-graphql-8tMA-ttz.js → replay-lint-graphql-bkrdHRy7.js} +5 -5
- package/dist/replay-resolve-BhcSceq-.js +3694 -0
- package/dist/replay-resolve-CQL6WBG1.js +47 -0
- package/dist/{replay-respect-run-DrWWHTWp.js → replay-respect-run-CecN1j9r.js} +1608 -1558
- package/dist/replay-respect-run-rJm4W93K.js +78 -0
- package/dist/replay-step-context.schema-BuuXkAck.js +1 -0
- package/dist/replay-step-context.schema-DBk4jufr.js +21 -0
- package/dist/{replay-tauri-path-BIIo3xjJ.js → replay-tauri-path--8KtMkGg.js} +1 -1
- package/dist/{replay-tauri-path-BQmCYwRI.js → replay-tauri-path--BfusnkB.js} +4 -4
- package/dist/replay-useAiChatQueries-B1-NwsZz.js +213 -0
- package/dist/replay-useAiChatQueries-Btd-PmIH.js +3532 -0
- package/dist/replay-validate-key-Bk0SFd3-.js +1 -0
- package/dist/replay-validate-key-CYsfegSv.js +28 -0
- package/dist/replay.cjs +1 -1
- package/dist/replay.d.ts +37 -5
- package/dist/replay.js +18 -18
- package/package.json +4 -4
- package/dist/replay-index-BFggGnsX.js +0 -181252
- package/dist/replay-index-BWX4r5us.js +0 -8354
- package/dist/replay-index-DuAFN7X8.js +0 -21
- package/dist/replay-index-tDvNtfHp.js +0 -1
- package/dist/replay-respect-run-DGe96SG3.js +0 -78
|
@@ -0,0 +1,2857 @@
|
|
|
1
|
+
import { F as Qe } from "./replay-index-DXnDNeqk.js";
|
|
2
|
+
import { h as Ve } from "./replay-history.schema-5ce6X0R1.js";
|
|
3
|
+
import { s as We } from "./replay-step-context.schema-DBk4jufr.js";
|
|
4
|
+
import { b as J, c as X, a as ee } from "./replay-ai-message-parts.schema-E9u9-nqr.js";
|
|
5
|
+
import { c as f, T as m, i as h, g as V, s as n, o as Ue, n as He, f as Ye, h as Ge, j as Ze, k as Xe, l as et, m as tt, p as st, q as it, r as rt, t as nt, u as at, v as ot, w as lt, x as ct, y as ht, z as ut, e as _e, A as dt, b as he, d as ft, a as mt, C as R, S as b, V as A, B as P, D as ye, E as O, F as W, G as Y, H as le, P as Oe, I as yt, J as ue, K as pe, L as pt, W as Ae, M as gt, N as Me, O as G, Q as $e } from "./replay-indexes-CfsvVMi_.js";
|
|
6
|
+
class bt {
|
|
7
|
+
static [f] = "ConsoleLogWriter";
|
|
8
|
+
write(e) {
|
|
9
|
+
console.log(e);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
class wt {
|
|
13
|
+
static [f] = "DefaultLogger";
|
|
14
|
+
writer;
|
|
15
|
+
constructor(e) {
|
|
16
|
+
this.writer = e?.writer ?? new bt();
|
|
17
|
+
}
|
|
18
|
+
logQuery(e, t) {
|
|
19
|
+
const s = t.map((r) => {
|
|
20
|
+
try {
|
|
21
|
+
return JSON.stringify(r);
|
|
22
|
+
} catch {
|
|
23
|
+
return String(r);
|
|
24
|
+
}
|
|
25
|
+
}), i = s.length ? ` -- params: [${s.join(", ")}]` : "";
|
|
26
|
+
this.writer.write(`Query: ${e}${i}`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
class St {
|
|
30
|
+
static [f] = "NoopLogger";
|
|
31
|
+
logQuery() {
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const Ce = /* @__PURE__ */ Symbol.for("drizzle:PgInlineForeignKeys"), Ne = /* @__PURE__ */ Symbol.for("drizzle:EnableRLS");
|
|
35
|
+
class vt extends m {
|
|
36
|
+
static [f] = "PgTable";
|
|
37
|
+
/** @internal */
|
|
38
|
+
static Symbol = Object.assign({}, m.Symbol, {
|
|
39
|
+
InlineForeignKeys: Ce,
|
|
40
|
+
EnableRLS: Ne
|
|
41
|
+
});
|
|
42
|
+
/**@internal */
|
|
43
|
+
[Ce] = [];
|
|
44
|
+
/** @internal */
|
|
45
|
+
[Ne] = !1;
|
|
46
|
+
/** @internal */
|
|
47
|
+
[m.Symbol.ExtraConfigBuilder] = void 0;
|
|
48
|
+
/** @internal */
|
|
49
|
+
[m.Symbol.ExtraConfigColumns] = {};
|
|
50
|
+
}
|
|
51
|
+
class Qt {
|
|
52
|
+
static [f] = "PgPrimaryKeyBuilder";
|
|
53
|
+
/** @internal */
|
|
54
|
+
columns;
|
|
55
|
+
/** @internal */
|
|
56
|
+
name;
|
|
57
|
+
constructor(e, t) {
|
|
58
|
+
this.columns = e, this.name = t;
|
|
59
|
+
}
|
|
60
|
+
/** @internal */
|
|
61
|
+
build(e) {
|
|
62
|
+
return new $t(e, this.columns, this.name);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
class $t {
|
|
66
|
+
constructor(e, t, s) {
|
|
67
|
+
this.table = e, this.columns = t, this.name = s;
|
|
68
|
+
}
|
|
69
|
+
static [f] = "PgPrimaryKey";
|
|
70
|
+
columns;
|
|
71
|
+
name;
|
|
72
|
+
getName() {
|
|
73
|
+
return this.name ?? `${this.table[vt.Symbol.Name]}_${this.columns.map((e) => e.name).join("_")}_pk`;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
class je {
|
|
77
|
+
constructor(e, t, s) {
|
|
78
|
+
this.sourceTable = e, this.referencedTable = t, this.relationName = s, this.referencedTableName = t[m.Symbol.Name];
|
|
79
|
+
}
|
|
80
|
+
static [f] = "Relation";
|
|
81
|
+
referencedTableName;
|
|
82
|
+
fieldName;
|
|
83
|
+
}
|
|
84
|
+
class Ee {
|
|
85
|
+
constructor(e, t) {
|
|
86
|
+
this.table = e, this.config = t;
|
|
87
|
+
}
|
|
88
|
+
static [f] = "Relations";
|
|
89
|
+
}
|
|
90
|
+
class k extends je {
|
|
91
|
+
constructor(e, t, s, i) {
|
|
92
|
+
super(e, t, s?.relationName), this.config = s, this.isNullable = i;
|
|
93
|
+
}
|
|
94
|
+
static [f] = "One";
|
|
95
|
+
withFieldName(e) {
|
|
96
|
+
const t = new k(
|
|
97
|
+
this.sourceTable,
|
|
98
|
+
this.referencedTable,
|
|
99
|
+
this.config,
|
|
100
|
+
this.isNullable
|
|
101
|
+
);
|
|
102
|
+
return t.fieldName = e, t;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
class ne extends je {
|
|
106
|
+
constructor(e, t, s) {
|
|
107
|
+
super(e, t, s?.relationName), this.config = s;
|
|
108
|
+
}
|
|
109
|
+
static [f] = "Many";
|
|
110
|
+
withFieldName(e) {
|
|
111
|
+
const t = new ne(
|
|
112
|
+
this.sourceTable,
|
|
113
|
+
this.referencedTable,
|
|
114
|
+
this.config
|
|
115
|
+
);
|
|
116
|
+
return t.fieldName = e, t;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function Ct() {
|
|
120
|
+
return {
|
|
121
|
+
and: he,
|
|
122
|
+
between: dt,
|
|
123
|
+
eq: _e,
|
|
124
|
+
exists: ut,
|
|
125
|
+
gt: ht,
|
|
126
|
+
gte: ct,
|
|
127
|
+
ilike: lt,
|
|
128
|
+
inArray: ot,
|
|
129
|
+
isNull: at,
|
|
130
|
+
isNotNull: nt,
|
|
131
|
+
like: rt,
|
|
132
|
+
lt: it,
|
|
133
|
+
lte: st,
|
|
134
|
+
ne: tt,
|
|
135
|
+
not: et,
|
|
136
|
+
notBetween: Xe,
|
|
137
|
+
notExists: Ze,
|
|
138
|
+
notLike: Ge,
|
|
139
|
+
notIlike: Ye,
|
|
140
|
+
notInArray: He,
|
|
141
|
+
or: Ue,
|
|
142
|
+
sql: n
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function Nt() {
|
|
146
|
+
return {
|
|
147
|
+
sql: n,
|
|
148
|
+
asc: mt,
|
|
149
|
+
desc: ft
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
function qt(l, e) {
|
|
153
|
+
Object.keys(l).length === 1 && "default" in l && !h(l.default, m) && (l = l.default);
|
|
154
|
+
const t = {}, s = {}, i = {};
|
|
155
|
+
for (const [r, a] of Object.entries(l))
|
|
156
|
+
if (h(a, m)) {
|
|
157
|
+
const o = V(a), c = s[o];
|
|
158
|
+
t[o] = r, i[r] = {
|
|
159
|
+
tsName: r,
|
|
160
|
+
dbName: a[m.Symbol.Name],
|
|
161
|
+
schema: a[m.Symbol.Schema],
|
|
162
|
+
columns: a[m.Symbol.Columns],
|
|
163
|
+
relations: c?.relations ?? {},
|
|
164
|
+
primaryKey: c?.primaryKey ?? []
|
|
165
|
+
};
|
|
166
|
+
for (const d of Object.values(
|
|
167
|
+
a[m.Symbol.Columns]
|
|
168
|
+
))
|
|
169
|
+
d.primary && i[r].primaryKey.push(d);
|
|
170
|
+
const u = a[m.Symbol.ExtraConfigBuilder]?.(a[m.Symbol.ExtraConfigColumns]);
|
|
171
|
+
if (u)
|
|
172
|
+
for (const d of Object.values(u))
|
|
173
|
+
h(d, Qt) && i[r].primaryKey.push(...d.columns);
|
|
174
|
+
} else if (h(a, Ee)) {
|
|
175
|
+
const o = V(a.table), c = t[o], u = a.config(
|
|
176
|
+
e(a.table)
|
|
177
|
+
);
|
|
178
|
+
let d;
|
|
179
|
+
for (const [p, C] of Object.entries(u))
|
|
180
|
+
if (c) {
|
|
181
|
+
const N = i[c];
|
|
182
|
+
N.relations[p] = C;
|
|
183
|
+
} else
|
|
184
|
+
o in s || (s[o] = {
|
|
185
|
+
relations: {},
|
|
186
|
+
primaryKey: d
|
|
187
|
+
}), s[o].relations[p] = C;
|
|
188
|
+
}
|
|
189
|
+
return { tables: i, tableNamesMap: t };
|
|
190
|
+
}
|
|
191
|
+
function ge(l, e) {
|
|
192
|
+
return new Ee(
|
|
193
|
+
l,
|
|
194
|
+
(t) => Object.fromEntries(
|
|
195
|
+
Object.entries(e(t)).map(([s, i]) => [
|
|
196
|
+
s,
|
|
197
|
+
i.withFieldName(s)
|
|
198
|
+
])
|
|
199
|
+
)
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
function Tt(l) {
|
|
203
|
+
return function(t, s) {
|
|
204
|
+
return new k(
|
|
205
|
+
l,
|
|
206
|
+
t,
|
|
207
|
+
s,
|
|
208
|
+
s?.fields.reduce((i, r) => i && r.notNull, !0) ?? !1
|
|
209
|
+
);
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
function Lt(l) {
|
|
213
|
+
return function(t, s) {
|
|
214
|
+
return new ne(l, t, s);
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
function xt(l, e, t) {
|
|
218
|
+
if (h(t, k) && t.config)
|
|
219
|
+
return {
|
|
220
|
+
fields: t.config.fields,
|
|
221
|
+
references: t.config.references
|
|
222
|
+
};
|
|
223
|
+
const s = e[V(t.referencedTable)];
|
|
224
|
+
if (!s)
|
|
225
|
+
throw new Error(
|
|
226
|
+
`Table "${t.referencedTable[m.Symbol.Name]}" not found in schema`
|
|
227
|
+
);
|
|
228
|
+
const i = l[s];
|
|
229
|
+
if (!i)
|
|
230
|
+
throw new Error(`Table "${s}" not found in schema`);
|
|
231
|
+
const r = t.sourceTable, a = e[V(r)];
|
|
232
|
+
if (!a)
|
|
233
|
+
throw new Error(
|
|
234
|
+
`Table "${r[m.Symbol.Name]}" not found in schema`
|
|
235
|
+
);
|
|
236
|
+
const o = [];
|
|
237
|
+
for (const c of Object.values(
|
|
238
|
+
i.relations
|
|
239
|
+
))
|
|
240
|
+
(t.relationName && t !== c && c.relationName === t.relationName || !t.relationName && c.referencedTable === t.sourceTable) && o.push(c);
|
|
241
|
+
if (o.length > 1)
|
|
242
|
+
throw t.relationName ? new Error(
|
|
243
|
+
`There are multiple relations with name "${t.relationName}" in table "${s}"`
|
|
244
|
+
) : new Error(
|
|
245
|
+
`There are multiple relations between "${s}" and "${t.sourceTable[m.Symbol.Name]}". Please specify relation name`
|
|
246
|
+
);
|
|
247
|
+
if (o[0] && h(o[0], k) && o[0].config)
|
|
248
|
+
return {
|
|
249
|
+
fields: o[0].config.references,
|
|
250
|
+
references: o[0].config.fields
|
|
251
|
+
};
|
|
252
|
+
throw new Error(
|
|
253
|
+
`There is not enough information to infer relation "${a}.${t.fieldName}"`
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
function Rt(l) {
|
|
257
|
+
return {
|
|
258
|
+
one: Tt(l),
|
|
259
|
+
many: Lt(l)
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
function de(l, e, t, s, i = (r) => r) {
|
|
263
|
+
const r = {};
|
|
264
|
+
for (const [
|
|
265
|
+
a,
|
|
266
|
+
o
|
|
267
|
+
] of s.entries())
|
|
268
|
+
if (o.isJson) {
|
|
269
|
+
const c = e.relations[o.tsKey], u = t[a], d = typeof u == "string" ? JSON.parse(u) : u;
|
|
270
|
+
r[o.tsKey] = h(c, k) ? d && de(
|
|
271
|
+
l,
|
|
272
|
+
l[o.relationTableTsKey],
|
|
273
|
+
d,
|
|
274
|
+
o.selection,
|
|
275
|
+
i
|
|
276
|
+
) : d.map(
|
|
277
|
+
(p) => de(
|
|
278
|
+
l,
|
|
279
|
+
l[o.relationTableTsKey],
|
|
280
|
+
p,
|
|
281
|
+
o.selection,
|
|
282
|
+
i
|
|
283
|
+
)
|
|
284
|
+
);
|
|
285
|
+
} else {
|
|
286
|
+
const c = i(t[a]), u = o.field;
|
|
287
|
+
let d;
|
|
288
|
+
h(u, R) ? d = u : h(u, b) ? d = u.decoder : d = u.sql.decoder, r[o.tsKey] = c === null ? null : d.mapFromDriverValue(c);
|
|
289
|
+
}
|
|
290
|
+
return r;
|
|
291
|
+
}
|
|
292
|
+
class te {
|
|
293
|
+
constructor(e) {
|
|
294
|
+
this.table = e;
|
|
295
|
+
}
|
|
296
|
+
static [f] = "ColumnAliasProxyHandler";
|
|
297
|
+
get(e, t) {
|
|
298
|
+
return t === "table" ? this.table : e[t];
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
class be {
|
|
302
|
+
constructor(e, t) {
|
|
303
|
+
this.alias = e, this.replaceOriginalName = t;
|
|
304
|
+
}
|
|
305
|
+
static [f] = "TableAliasProxyHandler";
|
|
306
|
+
get(e, t) {
|
|
307
|
+
if (t === m.Symbol.IsAlias)
|
|
308
|
+
return !0;
|
|
309
|
+
if (t === m.Symbol.Name)
|
|
310
|
+
return this.alias;
|
|
311
|
+
if (this.replaceOriginalName && t === m.Symbol.OriginalName)
|
|
312
|
+
return this.alias;
|
|
313
|
+
if (t === A)
|
|
314
|
+
return {
|
|
315
|
+
...e[A],
|
|
316
|
+
name: this.alias,
|
|
317
|
+
isAlias: !0
|
|
318
|
+
};
|
|
319
|
+
if (t === m.Symbol.Columns) {
|
|
320
|
+
const i = e[m.Symbol.Columns];
|
|
321
|
+
if (!i)
|
|
322
|
+
return i;
|
|
323
|
+
const r = {};
|
|
324
|
+
return Object.keys(i).map((a) => {
|
|
325
|
+
r[a] = new Proxy(
|
|
326
|
+
i[a],
|
|
327
|
+
new te(new Proxy(e, this))
|
|
328
|
+
);
|
|
329
|
+
}), r;
|
|
330
|
+
}
|
|
331
|
+
const s = e[t];
|
|
332
|
+
return h(s, R) ? new Proxy(s, new te(new Proxy(e, this))) : s;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function ce(l, e) {
|
|
336
|
+
return new Proxy(l, new be(e, !1));
|
|
337
|
+
}
|
|
338
|
+
function E(l, e) {
|
|
339
|
+
return new Proxy(
|
|
340
|
+
l,
|
|
341
|
+
new te(new Proxy(l.table, new be(e, !1)))
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
function Pe(l, e) {
|
|
345
|
+
return new b.Aliased(se(l.sql, e), l.fieldAlias);
|
|
346
|
+
}
|
|
347
|
+
function se(l, e) {
|
|
348
|
+
return n.join(l.queryChunks.map((t) => h(t, R) ? E(t, e) : h(t, b) ? se(t, e) : h(t, b.Aliased) ? Pe(t, e) : t));
|
|
349
|
+
}
|
|
350
|
+
class T {
|
|
351
|
+
static [f] = "SelectionProxyHandler";
|
|
352
|
+
config;
|
|
353
|
+
constructor(e) {
|
|
354
|
+
this.config = { ...e };
|
|
355
|
+
}
|
|
356
|
+
get(e, t) {
|
|
357
|
+
if (t === "_")
|
|
358
|
+
return {
|
|
359
|
+
...e._,
|
|
360
|
+
selectedFields: new Proxy(
|
|
361
|
+
e._.selectedFields,
|
|
362
|
+
this
|
|
363
|
+
)
|
|
364
|
+
};
|
|
365
|
+
if (t === A)
|
|
366
|
+
return {
|
|
367
|
+
...e[A],
|
|
368
|
+
selectedFields: new Proxy(
|
|
369
|
+
e[A].selectedFields,
|
|
370
|
+
this
|
|
371
|
+
)
|
|
372
|
+
};
|
|
373
|
+
if (typeof t == "symbol")
|
|
374
|
+
return e[t];
|
|
375
|
+
const i = (h(e, P) ? e._.selectedFields : h(e, ye) ? e[A].selectedFields : e)[t];
|
|
376
|
+
if (h(i, b.Aliased)) {
|
|
377
|
+
if (this.config.sqlAliasedBehavior === "sql" && !i.isSelectionField)
|
|
378
|
+
return i.sql;
|
|
379
|
+
const r = i.clone();
|
|
380
|
+
return r.isSelectionField = !0, r;
|
|
381
|
+
}
|
|
382
|
+
if (h(i, b)) {
|
|
383
|
+
if (this.config.sqlBehavior === "sql")
|
|
384
|
+
return i;
|
|
385
|
+
throw new Error(
|
|
386
|
+
`You tried to reference "${t}" field from a subquery, which is a raw SQL field, but it doesn't have an alias declared. Please add an alias to the field using ".as('alias')" method.`
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
return h(i, R) ? this.config.alias ? new Proxy(
|
|
390
|
+
i,
|
|
391
|
+
new te(
|
|
392
|
+
new Proxy(
|
|
393
|
+
i.table,
|
|
394
|
+
new be(this.config.alias, this.config.replaceOriginalName ?? !1)
|
|
395
|
+
)
|
|
396
|
+
)
|
|
397
|
+
) : i : typeof i != "object" || i === null ? i : new Proxy(i, new T(this.config));
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
class D {
|
|
401
|
+
static [f] = "QueryPromise";
|
|
402
|
+
[Symbol.toStringTag] = "QueryPromise";
|
|
403
|
+
catch(e) {
|
|
404
|
+
return this.then(void 0, e);
|
|
405
|
+
}
|
|
406
|
+
finally(e) {
|
|
407
|
+
return this.then(
|
|
408
|
+
(t) => (e?.(), t),
|
|
409
|
+
(t) => {
|
|
410
|
+
throw e?.(), t;
|
|
411
|
+
}
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
then(e, t) {
|
|
415
|
+
return this.execute().then(e, t);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
function K(l) {
|
|
419
|
+
return h(l, O) ? [`${l[m.Symbol.BaseName]}`] : h(l, P) ? l._.usedTables ?? [] : h(l, b) ? l.usedTables ?? [] : [];
|
|
420
|
+
}
|
|
421
|
+
class qe extends D {
|
|
422
|
+
constructor(e, t, s, i) {
|
|
423
|
+
super(), this.table = e, this.session = t, this.dialect = s, this.config = { table: e, withList: i };
|
|
424
|
+
}
|
|
425
|
+
static [f] = "SQLiteDelete";
|
|
426
|
+
/** @internal */
|
|
427
|
+
config;
|
|
428
|
+
/**
|
|
429
|
+
* Adds a `where` clause to the query.
|
|
430
|
+
*
|
|
431
|
+
* Calling this method will delete only those rows that fulfill a specified condition.
|
|
432
|
+
*
|
|
433
|
+
* See docs: {@link https://orm.drizzle.team/docs/delete}
|
|
434
|
+
*
|
|
435
|
+
* @param where the `where` clause.
|
|
436
|
+
*
|
|
437
|
+
* @example
|
|
438
|
+
* You can use conditional operators and `sql function` to filter the rows to be deleted.
|
|
439
|
+
*
|
|
440
|
+
* ```ts
|
|
441
|
+
* // Delete all cars with green color
|
|
442
|
+
* db.delete(cars).where(eq(cars.color, 'green'));
|
|
443
|
+
* // or
|
|
444
|
+
* db.delete(cars).where(sql`${cars.color} = 'green'`)
|
|
445
|
+
* ```
|
|
446
|
+
*
|
|
447
|
+
* You can logically combine conditional operators with `and()` and `or()` operators:
|
|
448
|
+
*
|
|
449
|
+
* ```ts
|
|
450
|
+
* // Delete all BMW cars with a green color
|
|
451
|
+
* db.delete(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
|
|
452
|
+
*
|
|
453
|
+
* // Delete all cars with the green or blue color
|
|
454
|
+
* db.delete(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
|
|
455
|
+
* ```
|
|
456
|
+
*/
|
|
457
|
+
where(e) {
|
|
458
|
+
return this.config.where = e, this;
|
|
459
|
+
}
|
|
460
|
+
orderBy(...e) {
|
|
461
|
+
if (typeof e[0] == "function") {
|
|
462
|
+
const t = e[0](
|
|
463
|
+
new Proxy(
|
|
464
|
+
this.config.table[m.Symbol.Columns],
|
|
465
|
+
new T({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" })
|
|
466
|
+
)
|
|
467
|
+
), s = Array.isArray(t) ? t : [t];
|
|
468
|
+
this.config.orderBy = s;
|
|
469
|
+
} else {
|
|
470
|
+
const t = e;
|
|
471
|
+
this.config.orderBy = t;
|
|
472
|
+
}
|
|
473
|
+
return this;
|
|
474
|
+
}
|
|
475
|
+
limit(e) {
|
|
476
|
+
return this.config.limit = e, this;
|
|
477
|
+
}
|
|
478
|
+
returning(e = this.table[O.Symbol.Columns]) {
|
|
479
|
+
return this.config.returning = W(e), this;
|
|
480
|
+
}
|
|
481
|
+
/** @internal */
|
|
482
|
+
getSQL() {
|
|
483
|
+
return this.dialect.buildDeleteQuery(this.config);
|
|
484
|
+
}
|
|
485
|
+
toSQL() {
|
|
486
|
+
const { typings: e, ...t } = this.dialect.sqlToQuery(this.getSQL());
|
|
487
|
+
return t;
|
|
488
|
+
}
|
|
489
|
+
/** @internal */
|
|
490
|
+
_prepare(e = !0) {
|
|
491
|
+
return this.session[e ? "prepareOneTimeQuery" : "prepareQuery"](
|
|
492
|
+
this.dialect.sqlToQuery(this.getSQL()),
|
|
493
|
+
this.config.returning,
|
|
494
|
+
this.config.returning ? "all" : "run",
|
|
495
|
+
!0,
|
|
496
|
+
void 0,
|
|
497
|
+
{
|
|
498
|
+
type: "delete",
|
|
499
|
+
tables: K(this.config.table)
|
|
500
|
+
}
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
prepare() {
|
|
504
|
+
return this._prepare(!1);
|
|
505
|
+
}
|
|
506
|
+
run = (e) => this._prepare().run(e);
|
|
507
|
+
all = (e) => this._prepare().all(e);
|
|
508
|
+
get = (e) => this._prepare().get(e);
|
|
509
|
+
values = (e) => this._prepare().values(e);
|
|
510
|
+
async execute(e) {
|
|
511
|
+
return this._prepare().execute(e);
|
|
512
|
+
}
|
|
513
|
+
$dynamic() {
|
|
514
|
+
return this;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
function Bt(l) {
|
|
518
|
+
return (l.replace(/['\u2019]/g, "").match(/[\da-z]+|[A-Z]+(?![a-z])|[A-Z][\da-z]+/g) ?? []).map((t) => t.toLowerCase()).join("_");
|
|
519
|
+
}
|
|
520
|
+
function _t(l) {
|
|
521
|
+
return (l.replace(/['\u2019]/g, "").match(/[\da-z]+|[A-Z]+(?![a-z])|[A-Z][\da-z]+/g) ?? []).reduce((t, s, i) => {
|
|
522
|
+
const r = i === 0 ? s.toLowerCase() : `${s[0].toUpperCase()}${s.slice(1)}`;
|
|
523
|
+
return t + r;
|
|
524
|
+
}, "");
|
|
525
|
+
}
|
|
526
|
+
function Ot(l) {
|
|
527
|
+
return l;
|
|
528
|
+
}
|
|
529
|
+
class At {
|
|
530
|
+
static [f] = "CasingCache";
|
|
531
|
+
/** @internal */
|
|
532
|
+
cache = {};
|
|
533
|
+
cachedTables = {};
|
|
534
|
+
convert;
|
|
535
|
+
constructor(e) {
|
|
536
|
+
this.convert = e === "snake_case" ? Bt : e === "camelCase" ? _t : Ot;
|
|
537
|
+
}
|
|
538
|
+
getColumnCasing(e) {
|
|
539
|
+
if (!e.keyAsName) return e.name;
|
|
540
|
+
const t = e.table[m.Symbol.Schema] ?? "public", s = e.table[m.Symbol.OriginalName], i = `${t}.${s}.${e.name}`;
|
|
541
|
+
return this.cache[i] || this.cacheTable(e.table), this.cache[i];
|
|
542
|
+
}
|
|
543
|
+
cacheTable(e) {
|
|
544
|
+
const t = e[m.Symbol.Schema] ?? "public", s = e[m.Symbol.OriginalName], i = `${t}.${s}`;
|
|
545
|
+
if (!this.cachedTables[i]) {
|
|
546
|
+
for (const r of Object.values(e[m.Symbol.Columns])) {
|
|
547
|
+
const a = `${i}.${r.name}`;
|
|
548
|
+
this.cache[a] = this.convert(r.name);
|
|
549
|
+
}
|
|
550
|
+
this.cachedTables[i] = !0;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
clearCache() {
|
|
554
|
+
this.cache = {}, this.cachedTables = {};
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
class we extends Error {
|
|
558
|
+
static [f] = "DrizzleError";
|
|
559
|
+
constructor({ message: e, cause: t }) {
|
|
560
|
+
super(e), this.name = "DrizzleError", this.cause = t;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
class F extends Error {
|
|
564
|
+
constructor(e, t, s) {
|
|
565
|
+
super(`Failed query: ${e}
|
|
566
|
+
params: ${t}`), this.query = e, this.params = t, this.cause = s, Error.captureStackTrace(this, F), s && (this.cause = s);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
class Mt extends we {
|
|
570
|
+
static [f] = "TransactionRollbackError";
|
|
571
|
+
constructor() {
|
|
572
|
+
super({ message: "Rollback" });
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
class Se extends ye {
|
|
576
|
+
static [f] = "SQLiteViewBase";
|
|
577
|
+
}
|
|
578
|
+
class ie {
|
|
579
|
+
static [f] = "SQLiteDialect";
|
|
580
|
+
/** @internal */
|
|
581
|
+
casing;
|
|
582
|
+
constructor(e) {
|
|
583
|
+
this.casing = new At(e?.casing);
|
|
584
|
+
}
|
|
585
|
+
escapeName(e) {
|
|
586
|
+
return `"${e.replace(/"/g, '""')}"`;
|
|
587
|
+
}
|
|
588
|
+
escapeParam(e) {
|
|
589
|
+
return "?";
|
|
590
|
+
}
|
|
591
|
+
escapeString(e) {
|
|
592
|
+
return `'${e.replace(/'/g, "''")}'`;
|
|
593
|
+
}
|
|
594
|
+
buildWithCTE(e) {
|
|
595
|
+
if (!e?.length) return;
|
|
596
|
+
const t = [n`with `];
|
|
597
|
+
for (const [s, i] of e.entries())
|
|
598
|
+
t.push(n`${n.identifier(i._.alias)} as (${i._.sql})`), s < e.length - 1 && t.push(n`, `);
|
|
599
|
+
return t.push(n` `), n.join(t);
|
|
600
|
+
}
|
|
601
|
+
buildDeleteQuery({
|
|
602
|
+
table: e,
|
|
603
|
+
where: t,
|
|
604
|
+
returning: s,
|
|
605
|
+
withList: i,
|
|
606
|
+
limit: r,
|
|
607
|
+
orderBy: a
|
|
608
|
+
}) {
|
|
609
|
+
const o = this.buildWithCTE(i), c = s ? n` returning ${this.buildSelection(s, { isSingleTable: !0 })}` : void 0, u = t ? n` where ${t}` : void 0, d = this.buildOrderBy(a), p = this.buildLimit(r);
|
|
610
|
+
return n`${o}delete from ${e}${u}${c}${d}${p}`;
|
|
611
|
+
}
|
|
612
|
+
buildUpdateSet(e, t) {
|
|
613
|
+
const s = e[m.Symbol.Columns], i = Object.keys(s).filter(
|
|
614
|
+
(a) => t[a] !== void 0 || s[a]?.onUpdateFn !== void 0
|
|
615
|
+
), r = i.length;
|
|
616
|
+
return n.join(
|
|
617
|
+
i.flatMap((a, o) => {
|
|
618
|
+
const c = s[a], u = c.onUpdateFn?.(), d = t[a] ?? (h(u, b) ? u : n.param(u, c)), p = n`${n.identifier(this.casing.getColumnCasing(c))} = ${d}`;
|
|
619
|
+
return o < r - 1 ? [p, n.raw(", ")] : [p];
|
|
620
|
+
})
|
|
621
|
+
);
|
|
622
|
+
}
|
|
623
|
+
buildUpdateQuery({
|
|
624
|
+
table: e,
|
|
625
|
+
set: t,
|
|
626
|
+
where: s,
|
|
627
|
+
returning: i,
|
|
628
|
+
withList: r,
|
|
629
|
+
joins: a,
|
|
630
|
+
from: o,
|
|
631
|
+
limit: c,
|
|
632
|
+
orderBy: u
|
|
633
|
+
}) {
|
|
634
|
+
const d = this.buildWithCTE(r), p = this.buildUpdateSet(e, t), C = o && n.join([n.raw(" from "), this.buildFromTable(o)]), N = this.buildJoins(a), $ = i ? n` returning ${this.buildSelection(i, { isSingleTable: !0 })}` : void 0, S = s ? n` where ${s}` : void 0, v = this.buildOrderBy(u), Q = this.buildLimit(c);
|
|
635
|
+
return n`${d}update ${e} set ${p}${C}${N}${S}${$}${v}${Q}`;
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Builds selection SQL with provided fields/expressions
|
|
639
|
+
*
|
|
640
|
+
* Examples:
|
|
641
|
+
*
|
|
642
|
+
* `select <selection> from`
|
|
643
|
+
*
|
|
644
|
+
* `insert ... returning <selection>`
|
|
645
|
+
*
|
|
646
|
+
* If `isSingleTable` is true, then columns won't be prefixed with table name
|
|
647
|
+
*/
|
|
648
|
+
buildSelection(e, { isSingleTable: t = !1 } = {}) {
|
|
649
|
+
const s = e.length, i = e.flatMap(({ field: r }, a) => {
|
|
650
|
+
const o = [];
|
|
651
|
+
if (h(r, b.Aliased) && r.isSelectionField)
|
|
652
|
+
o.push(n.identifier(r.fieldAlias));
|
|
653
|
+
else if (h(r, b.Aliased) || h(r, b)) {
|
|
654
|
+
const c = h(r, b.Aliased) ? r.sql : r;
|
|
655
|
+
t ? o.push(
|
|
656
|
+
new b(
|
|
657
|
+
c.queryChunks.map((u) => h(u, R) ? n.identifier(this.casing.getColumnCasing(u)) : u)
|
|
658
|
+
)
|
|
659
|
+
) : o.push(c), h(r, b.Aliased) && o.push(n` as ${n.identifier(r.fieldAlias)}`);
|
|
660
|
+
} else if (h(r, R)) {
|
|
661
|
+
const c = r.table[m.Symbol.Name];
|
|
662
|
+
r.columnType === "SQLiteNumericBigInt" ? t ? o.push(
|
|
663
|
+
n`cast(${n.identifier(this.casing.getColumnCasing(r))} as text)`
|
|
664
|
+
) : o.push(
|
|
665
|
+
n`cast(${n.identifier(c)}.${n.identifier(this.casing.getColumnCasing(r))} as text)`
|
|
666
|
+
) : t ? o.push(n.identifier(this.casing.getColumnCasing(r))) : o.push(
|
|
667
|
+
n`${n.identifier(c)}.${n.identifier(this.casing.getColumnCasing(r))}`
|
|
668
|
+
);
|
|
669
|
+
} else if (h(r, P)) {
|
|
670
|
+
const c = Object.entries(r._.selectedFields);
|
|
671
|
+
if (c.length === 1) {
|
|
672
|
+
const u = c[0][1], d = h(u, b) ? u.decoder : h(u, R) ? { mapFromDriverValue: (p) => u.mapFromDriverValue(p) } : u.sql.decoder;
|
|
673
|
+
d && (r._.sql.decoder = d);
|
|
674
|
+
}
|
|
675
|
+
o.push(r);
|
|
676
|
+
}
|
|
677
|
+
return a < s - 1 && o.push(n`, `), o;
|
|
678
|
+
});
|
|
679
|
+
return n.join(i);
|
|
680
|
+
}
|
|
681
|
+
buildJoins(e) {
|
|
682
|
+
if (!e || e.length === 0)
|
|
683
|
+
return;
|
|
684
|
+
const t = [];
|
|
685
|
+
if (e)
|
|
686
|
+
for (const [s, i] of e.entries()) {
|
|
687
|
+
s === 0 && t.push(n` `);
|
|
688
|
+
const r = i.table, a = i.on ? n` on ${i.on}` : void 0;
|
|
689
|
+
if (h(r, O)) {
|
|
690
|
+
const o = r[O.Symbol.Name], c = r[O.Symbol.Schema], u = r[O.Symbol.OriginalName], d = o === u ? void 0 : i.alias;
|
|
691
|
+
t.push(
|
|
692
|
+
n`${n.raw(i.joinType)} join ${c ? n`${n.identifier(c)}.` : void 0}${n.identifier(
|
|
693
|
+
u
|
|
694
|
+
)}${d && n` ${n.identifier(d)}`}${a}`
|
|
695
|
+
);
|
|
696
|
+
} else
|
|
697
|
+
t.push(
|
|
698
|
+
n`${n.raw(i.joinType)} join ${r}${a}`
|
|
699
|
+
);
|
|
700
|
+
s < e.length - 1 && t.push(n` `);
|
|
701
|
+
}
|
|
702
|
+
return n.join(t);
|
|
703
|
+
}
|
|
704
|
+
buildLimit(e) {
|
|
705
|
+
return typeof e == "object" || typeof e == "number" && e >= 0 ? n` limit ${e}` : void 0;
|
|
706
|
+
}
|
|
707
|
+
buildOrderBy(e) {
|
|
708
|
+
const t = [];
|
|
709
|
+
if (e)
|
|
710
|
+
for (const [s, i] of e.entries())
|
|
711
|
+
t.push(i), s < e.length - 1 && t.push(n`, `);
|
|
712
|
+
return t.length > 0 ? n` order by ${n.join(t)}` : void 0;
|
|
713
|
+
}
|
|
714
|
+
buildFromTable(e) {
|
|
715
|
+
return h(e, m) && e[m.Symbol.IsAlias] ? n`${n`${n.identifier(e[m.Symbol.Schema] ?? "")}.`.if(e[m.Symbol.Schema])}${n.identifier(
|
|
716
|
+
e[m.Symbol.OriginalName]
|
|
717
|
+
)} ${n.identifier(e[m.Symbol.Name])}` : e;
|
|
718
|
+
}
|
|
719
|
+
buildSelectQuery({
|
|
720
|
+
withList: e,
|
|
721
|
+
fields: t,
|
|
722
|
+
fieldsFlat: s,
|
|
723
|
+
where: i,
|
|
724
|
+
having: r,
|
|
725
|
+
table: a,
|
|
726
|
+
joins: o,
|
|
727
|
+
orderBy: c,
|
|
728
|
+
groupBy: u,
|
|
729
|
+
limit: d,
|
|
730
|
+
offset: p,
|
|
731
|
+
distinct: C,
|
|
732
|
+
setOperators: N
|
|
733
|
+
}) {
|
|
734
|
+
const $ = s ?? W(t);
|
|
735
|
+
for (const B of $)
|
|
736
|
+
if (h(B.field, R) && Y(B.field.table) !== (h(a, P) ? a._.alias : h(a, Se) ? a[A].name : h(a, b) ? void 0 : Y(a)) && !((_) => o?.some(
|
|
737
|
+
({ alias: H }) => H === (_[m.Symbol.IsAlias] ? Y(_) : _[m.Symbol.BaseName])
|
|
738
|
+
))(B.field.table)) {
|
|
739
|
+
const _ = Y(B.field.table);
|
|
740
|
+
throw new Error(
|
|
741
|
+
`Your "${B.path.join(
|
|
742
|
+
"->"
|
|
743
|
+
)}" field references a column "${_}"."${B.field.name}", but the table "${_}" is not part of the query! Did you forget to join it?`
|
|
744
|
+
);
|
|
745
|
+
}
|
|
746
|
+
const S = !o || o.length === 0, v = this.buildWithCTE(e), Q = C ? n` distinct` : void 0, L = this.buildSelection($, { isSingleTable: S }), q = this.buildFromTable(a), g = this.buildJoins(o), M = i ? n` where ${i}` : void 0, x = r ? n` having ${r}` : void 0, y = [];
|
|
747
|
+
if (u)
|
|
748
|
+
for (const [B, _] of u.entries())
|
|
749
|
+
y.push(_), B < u.length - 1 && y.push(n`, `);
|
|
750
|
+
const w = y.length > 0 ? n` group by ${n.join(y)}` : void 0, j = this.buildOrderBy(c), U = this.buildLimit(d), oe = p ? n` offset ${p}` : void 0, z = n`${v}select${Q} ${L} from ${q}${g}${M}${w}${x}${j}${U}${oe}`;
|
|
751
|
+
return N.length > 0 ? this.buildSetOperations(z, N) : z;
|
|
752
|
+
}
|
|
753
|
+
buildSetOperations(e, t) {
|
|
754
|
+
const [s, ...i] = t;
|
|
755
|
+
if (!s)
|
|
756
|
+
throw new Error("Cannot pass undefined values to any set operator");
|
|
757
|
+
return i.length === 0 ? this.buildSetOperationQuery({ leftSelect: e, setOperator: s }) : this.buildSetOperations(
|
|
758
|
+
this.buildSetOperationQuery({ leftSelect: e, setOperator: s }),
|
|
759
|
+
i
|
|
760
|
+
);
|
|
761
|
+
}
|
|
762
|
+
buildSetOperationQuery({
|
|
763
|
+
leftSelect: e,
|
|
764
|
+
setOperator: { type: t, isAll: s, rightSelect: i, limit: r, orderBy: a, offset: o }
|
|
765
|
+
}) {
|
|
766
|
+
const c = n`${e.getSQL()} `, u = n`${i.getSQL()}`;
|
|
767
|
+
let d;
|
|
768
|
+
if (a && a.length > 0) {
|
|
769
|
+
const $ = [];
|
|
770
|
+
for (const S of a)
|
|
771
|
+
if (h(S, le))
|
|
772
|
+
$.push(n.identifier(S.name));
|
|
773
|
+
else if (h(S, b)) {
|
|
774
|
+
for (let v = 0; v < S.queryChunks.length; v++) {
|
|
775
|
+
const Q = S.queryChunks[v];
|
|
776
|
+
h(Q, le) && (S.queryChunks[v] = n.identifier(
|
|
777
|
+
this.casing.getColumnCasing(Q)
|
|
778
|
+
));
|
|
779
|
+
}
|
|
780
|
+
$.push(n`${S}`);
|
|
781
|
+
} else
|
|
782
|
+
$.push(n`${S}`);
|
|
783
|
+
d = n` order by ${n.join($, n`, `)}`;
|
|
784
|
+
}
|
|
785
|
+
const p = typeof r == "object" || typeof r == "number" && r >= 0 ? n` limit ${r}` : void 0, C = n.raw(`${t} ${s ? "all " : ""}`), N = o ? n` offset ${o}` : void 0;
|
|
786
|
+
return n`${c}${C}${u}${d}${p}${N}`;
|
|
787
|
+
}
|
|
788
|
+
buildInsertQuery({
|
|
789
|
+
table: e,
|
|
790
|
+
values: t,
|
|
791
|
+
onConflict: s,
|
|
792
|
+
returning: i,
|
|
793
|
+
withList: r,
|
|
794
|
+
select: a
|
|
795
|
+
}) {
|
|
796
|
+
const o = [], c = e[m.Symbol.Columns], u = Object.entries(c).filter(
|
|
797
|
+
([S, v]) => !v.shouldDisableInsert()
|
|
798
|
+
), d = u.map(([, S]) => n.identifier(this.casing.getColumnCasing(S)));
|
|
799
|
+
if (a) {
|
|
800
|
+
const S = t;
|
|
801
|
+
h(S, b) ? o.push(S) : o.push(S.getSQL());
|
|
802
|
+
} else {
|
|
803
|
+
const S = t;
|
|
804
|
+
o.push(n.raw("values "));
|
|
805
|
+
for (const [v, Q] of S.entries()) {
|
|
806
|
+
const L = [];
|
|
807
|
+
for (const [q, g] of u) {
|
|
808
|
+
const M = Q[q];
|
|
809
|
+
if (M === void 0 || h(M, Oe) && M.value === void 0) {
|
|
810
|
+
let x;
|
|
811
|
+
if (g.default !== null && g.default !== void 0)
|
|
812
|
+
x = h(g.default, b) ? g.default : n.param(g.default, g);
|
|
813
|
+
else if (g.defaultFn !== void 0) {
|
|
814
|
+
const y = g.defaultFn();
|
|
815
|
+
x = h(y, b) ? y : n.param(y, g);
|
|
816
|
+
} else if (!g.default && g.onUpdateFn !== void 0) {
|
|
817
|
+
const y = g.onUpdateFn();
|
|
818
|
+
x = h(y, b) ? y : n.param(y, g);
|
|
819
|
+
} else
|
|
820
|
+
x = n`null`;
|
|
821
|
+
L.push(x);
|
|
822
|
+
} else
|
|
823
|
+
L.push(M);
|
|
824
|
+
}
|
|
825
|
+
o.push(L), v < S.length - 1 && o.push(n`, `);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
const p = this.buildWithCTE(r), C = n.join(o), N = i ? n` returning ${this.buildSelection(i, { isSingleTable: !0 })}` : void 0, $ = s?.length ? n.join(s) : void 0;
|
|
829
|
+
return n`${p}insert into ${e} ${d} ${C}${$}${N}`;
|
|
830
|
+
}
|
|
831
|
+
sqlToQuery(e, t) {
|
|
832
|
+
return e.toQuery({
|
|
833
|
+
casing: this.casing,
|
|
834
|
+
escapeName: this.escapeName,
|
|
835
|
+
escapeParam: this.escapeParam,
|
|
836
|
+
escapeString: this.escapeString,
|
|
837
|
+
invokeSource: t
|
|
838
|
+
});
|
|
839
|
+
}
|
|
840
|
+
buildRelationalQuery({
|
|
841
|
+
fullSchema: e,
|
|
842
|
+
schema: t,
|
|
843
|
+
tableNamesMap: s,
|
|
844
|
+
table: i,
|
|
845
|
+
tableConfig: r,
|
|
846
|
+
queryConfig: a,
|
|
847
|
+
tableAlias: o,
|
|
848
|
+
nestedQueryRelation: c,
|
|
849
|
+
joinOn: u
|
|
850
|
+
}) {
|
|
851
|
+
let d = [], p, C, N = [], $;
|
|
852
|
+
const S = [];
|
|
853
|
+
if (a === !0)
|
|
854
|
+
d = Object.entries(r.columns).map(([L, q]) => ({
|
|
855
|
+
dbKey: q.name,
|
|
856
|
+
tsKey: L,
|
|
857
|
+
field: E(q, o),
|
|
858
|
+
relationTableTsKey: void 0,
|
|
859
|
+
isJson: !1,
|
|
860
|
+
selection: []
|
|
861
|
+
}));
|
|
862
|
+
else {
|
|
863
|
+
const Q = Object.fromEntries(
|
|
864
|
+
Object.entries(r.columns).map(([y, w]) => [
|
|
865
|
+
y,
|
|
866
|
+
E(w, o)
|
|
867
|
+
])
|
|
868
|
+
);
|
|
869
|
+
if (a.where) {
|
|
870
|
+
const y = typeof a.where == "function" ? a.where(Q, Ct()) : a.where;
|
|
871
|
+
$ = y && se(y, o);
|
|
872
|
+
}
|
|
873
|
+
const L = [];
|
|
874
|
+
let q = [];
|
|
875
|
+
if (a.columns) {
|
|
876
|
+
let y = !1;
|
|
877
|
+
for (const [w, j] of Object.entries(a.columns))
|
|
878
|
+
j !== void 0 && w in r.columns && (!y && j === !0 && (y = !0), q.push(w));
|
|
879
|
+
q.length > 0 && (q = y ? q.filter((w) => a.columns?.[w] === !0) : Object.keys(r.columns).filter(
|
|
880
|
+
(w) => !q.includes(w)
|
|
881
|
+
));
|
|
882
|
+
} else
|
|
883
|
+
q = Object.keys(r.columns);
|
|
884
|
+
for (const y of q) {
|
|
885
|
+
const w = r.columns[y];
|
|
886
|
+
L.push({ tsKey: y, value: w });
|
|
887
|
+
}
|
|
888
|
+
let g = [];
|
|
889
|
+
a.with && (g = Object.entries(a.with).filter(
|
|
890
|
+
(y) => !!y[1]
|
|
891
|
+
).map(([y, w]) => ({
|
|
892
|
+
tsKey: y,
|
|
893
|
+
queryConfig: w,
|
|
894
|
+
relation: r.relations[y]
|
|
895
|
+
})));
|
|
896
|
+
let M;
|
|
897
|
+
if (a.extras) {
|
|
898
|
+
M = typeof a.extras == "function" ? a.extras(Q, { sql: n }) : a.extras;
|
|
899
|
+
for (const [y, w] of Object.entries(M))
|
|
900
|
+
L.push({
|
|
901
|
+
tsKey: y,
|
|
902
|
+
value: Pe(w, o)
|
|
903
|
+
});
|
|
904
|
+
}
|
|
905
|
+
for (const { tsKey: y, value: w } of L)
|
|
906
|
+
d.push({
|
|
907
|
+
dbKey: h(w, b.Aliased) ? w.fieldAlias : r.columns[y].name,
|
|
908
|
+
tsKey: y,
|
|
909
|
+
field: h(w, R) ? E(w, o) : w,
|
|
910
|
+
relationTableTsKey: void 0,
|
|
911
|
+
isJson: !1,
|
|
912
|
+
selection: []
|
|
913
|
+
});
|
|
914
|
+
let x = typeof a.orderBy == "function" ? a.orderBy(Q, Nt()) : a.orderBy ?? [];
|
|
915
|
+
Array.isArray(x) || (x = [x]), N = x.map((y) => h(y, R) ? E(y, o) : se(y, o)), p = a.limit, C = a.offset;
|
|
916
|
+
for (const {
|
|
917
|
+
tsKey: y,
|
|
918
|
+
queryConfig: w,
|
|
919
|
+
relation: j
|
|
920
|
+
} of g) {
|
|
921
|
+
const U = xt(
|
|
922
|
+
t,
|
|
923
|
+
s,
|
|
924
|
+
j
|
|
925
|
+
), oe = V(j.referencedTable), z = s[oe], B = `${o}_${y}`, _ = he(
|
|
926
|
+
...U.fields.map(
|
|
927
|
+
(ze, Je) => _e(
|
|
928
|
+
E(
|
|
929
|
+
U.references[Je],
|
|
930
|
+
B
|
|
931
|
+
),
|
|
932
|
+
E(ze, o)
|
|
933
|
+
)
|
|
934
|
+
)
|
|
935
|
+
), H = this.buildRelationalQuery({
|
|
936
|
+
fullSchema: e,
|
|
937
|
+
schema: t,
|
|
938
|
+
tableNamesMap: s,
|
|
939
|
+
table: e[z],
|
|
940
|
+
tableConfig: t[z],
|
|
941
|
+
queryConfig: h(j, k) ? w === !0 ? { limit: 1 } : { ...w, limit: 1 } : w,
|
|
942
|
+
tableAlias: B,
|
|
943
|
+
joinOn: _,
|
|
944
|
+
nestedQueryRelation: j
|
|
945
|
+
}), De = n`(${H.sql})`.as(y);
|
|
946
|
+
d.push({
|
|
947
|
+
dbKey: y,
|
|
948
|
+
tsKey: y,
|
|
949
|
+
field: De,
|
|
950
|
+
relationTableTsKey: z,
|
|
951
|
+
isJson: !0,
|
|
952
|
+
selection: H.selection
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
if (d.length === 0)
|
|
957
|
+
throw new we({
|
|
958
|
+
message: `No fields selected for table "${r.tsName}" ("${o}"). You need to have at least one item in "columns", "with" or "extras". If you need to select all columns, omit the "columns" key or set it to undefined.`
|
|
959
|
+
});
|
|
960
|
+
let v;
|
|
961
|
+
if ($ = he(u, $), c) {
|
|
962
|
+
let Q = n`json_array(${n.join(
|
|
963
|
+
d.map(
|
|
964
|
+
({ field: g }) => h(g, le) ? n.identifier(this.casing.getColumnCasing(g)) : h(g, b.Aliased) ? g.sql : g
|
|
965
|
+
),
|
|
966
|
+
n`, `
|
|
967
|
+
)})`;
|
|
968
|
+
h(c, ne) && (Q = n`coalesce(json_group_array(${Q}), json_array())`);
|
|
969
|
+
const L = [
|
|
970
|
+
{
|
|
971
|
+
dbKey: "data",
|
|
972
|
+
tsKey: "data",
|
|
973
|
+
field: Q.as("data"),
|
|
974
|
+
isJson: !0,
|
|
975
|
+
relationTableTsKey: r.tsName,
|
|
976
|
+
selection: d
|
|
977
|
+
}
|
|
978
|
+
];
|
|
979
|
+
p !== void 0 || C !== void 0 || N.length > 0 ? (v = this.buildSelectQuery({
|
|
980
|
+
table: ce(i, o),
|
|
981
|
+
fields: {},
|
|
982
|
+
fieldsFlat: [
|
|
983
|
+
{
|
|
984
|
+
path: [],
|
|
985
|
+
field: n.raw("*")
|
|
986
|
+
}
|
|
987
|
+
],
|
|
988
|
+
where: $,
|
|
989
|
+
limit: p,
|
|
990
|
+
offset: C,
|
|
991
|
+
orderBy: N,
|
|
992
|
+
setOperators: []
|
|
993
|
+
}), $ = void 0, p = void 0, C = void 0, N = void 0) : v = ce(i, o), v = this.buildSelectQuery({
|
|
994
|
+
table: h(v, O) ? v : new P(v, {}, o),
|
|
995
|
+
fields: {},
|
|
996
|
+
fieldsFlat: L.map(({ field: g }) => ({
|
|
997
|
+
path: [],
|
|
998
|
+
field: h(g, R) ? E(g, o) : g
|
|
999
|
+
})),
|
|
1000
|
+
joins: S,
|
|
1001
|
+
where: $,
|
|
1002
|
+
limit: p,
|
|
1003
|
+
offset: C,
|
|
1004
|
+
orderBy: N,
|
|
1005
|
+
setOperators: []
|
|
1006
|
+
});
|
|
1007
|
+
} else
|
|
1008
|
+
v = this.buildSelectQuery({
|
|
1009
|
+
table: ce(i, o),
|
|
1010
|
+
fields: {},
|
|
1011
|
+
fieldsFlat: d.map(({ field: Q }) => ({
|
|
1012
|
+
path: [],
|
|
1013
|
+
field: h(Q, R) ? E(Q, o) : Q
|
|
1014
|
+
})),
|
|
1015
|
+
joins: S,
|
|
1016
|
+
where: $,
|
|
1017
|
+
limit: p,
|
|
1018
|
+
offset: C,
|
|
1019
|
+
orderBy: N,
|
|
1020
|
+
setOperators: []
|
|
1021
|
+
});
|
|
1022
|
+
return {
|
|
1023
|
+
tableTsKey: r.tsName,
|
|
1024
|
+
sql: v,
|
|
1025
|
+
selection: d
|
|
1026
|
+
};
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
class jt extends ie {
|
|
1030
|
+
static [f] = "SQLiteSyncDialect";
|
|
1031
|
+
migrate(e, t, s) {
|
|
1032
|
+
const i = s === void 0 || typeof s == "string" ? "__drizzle_migrations" : s.migrationsTable ?? "__drizzle_migrations", r = n`
|
|
1033
|
+
CREATE TABLE IF NOT EXISTS ${n.identifier(i)} (
|
|
1034
|
+
id SERIAL PRIMARY KEY,
|
|
1035
|
+
hash text NOT NULL,
|
|
1036
|
+
created_at numeric
|
|
1037
|
+
)
|
|
1038
|
+
`;
|
|
1039
|
+
t.run(r);
|
|
1040
|
+
const o = t.values(
|
|
1041
|
+
n`SELECT id, hash, created_at FROM ${n.identifier(i)} ORDER BY created_at DESC LIMIT 1`
|
|
1042
|
+
)[0] ?? void 0;
|
|
1043
|
+
t.run(n`BEGIN`);
|
|
1044
|
+
try {
|
|
1045
|
+
for (const c of e)
|
|
1046
|
+
if (!o || Number(o[2]) < c.folderMillis) {
|
|
1047
|
+
for (const u of c.sql)
|
|
1048
|
+
t.run(n.raw(u));
|
|
1049
|
+
t.run(
|
|
1050
|
+
n`INSERT INTO ${n.identifier(
|
|
1051
|
+
i
|
|
1052
|
+
)} ("hash", "created_at") VALUES(${c.hash}, ${c.folderMillis})`
|
|
1053
|
+
);
|
|
1054
|
+
}
|
|
1055
|
+
t.run(n`COMMIT`);
|
|
1056
|
+
} catch (c) {
|
|
1057
|
+
throw t.run(n`ROLLBACK`), c;
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
class Et extends ie {
|
|
1062
|
+
static [f] = "SQLiteAsyncDialect";
|
|
1063
|
+
async migrate(e, t, s) {
|
|
1064
|
+
const i = s === void 0 || typeof s == "string" ? "__drizzle_migrations" : s.migrationsTable ?? "__drizzle_migrations", r = n`
|
|
1065
|
+
CREATE TABLE IF NOT EXISTS ${n.identifier(i)} (
|
|
1066
|
+
id SERIAL PRIMARY KEY,
|
|
1067
|
+
hash text NOT NULL,
|
|
1068
|
+
created_at numeric
|
|
1069
|
+
)
|
|
1070
|
+
`;
|
|
1071
|
+
await t.run(r);
|
|
1072
|
+
const o = (await t.values(
|
|
1073
|
+
n`SELECT id, hash, created_at FROM ${n.identifier(i)} ORDER BY created_at DESC LIMIT 1`
|
|
1074
|
+
))[0] ?? void 0;
|
|
1075
|
+
await t.transaction(async (c) => {
|
|
1076
|
+
for (const u of e)
|
|
1077
|
+
if (!o || Number(o[2]) < u.folderMillis) {
|
|
1078
|
+
for (const d of u.sql)
|
|
1079
|
+
await c.run(n.raw(d));
|
|
1080
|
+
await c.run(
|
|
1081
|
+
n`INSERT INTO ${n.identifier(
|
|
1082
|
+
i
|
|
1083
|
+
)} ("hash", "created_at") VALUES(${u.hash}, ${u.folderMillis})`
|
|
1084
|
+
);
|
|
1085
|
+
}
|
|
1086
|
+
});
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
class Pt {
|
|
1090
|
+
static [f] = "TypedQueryBuilder";
|
|
1091
|
+
/** @internal */
|
|
1092
|
+
getSelectedFields() {
|
|
1093
|
+
return this._.selectedFields;
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
class I {
|
|
1097
|
+
static [f] = "SQLiteSelectBuilder";
|
|
1098
|
+
fields;
|
|
1099
|
+
session;
|
|
1100
|
+
dialect;
|
|
1101
|
+
withList;
|
|
1102
|
+
distinct;
|
|
1103
|
+
constructor(e) {
|
|
1104
|
+
this.fields = e.fields, this.session = e.session, this.dialect = e.dialect, this.withList = e.withList, this.distinct = e.distinct;
|
|
1105
|
+
}
|
|
1106
|
+
from(e) {
|
|
1107
|
+
const t = !!this.fields;
|
|
1108
|
+
let s;
|
|
1109
|
+
return this.fields ? s = this.fields : h(e, P) ? s = Object.fromEntries(
|
|
1110
|
+
Object.keys(e._.selectedFields).map((i) => [i, e[i]])
|
|
1111
|
+
) : h(e, Se) ? s = e[A].selectedFields : h(e, b) ? s = {} : s = yt(e), new Fe({
|
|
1112
|
+
table: e,
|
|
1113
|
+
fields: s,
|
|
1114
|
+
isPartialSelect: t,
|
|
1115
|
+
session: this.session,
|
|
1116
|
+
dialect: this.dialect,
|
|
1117
|
+
withList: this.withList,
|
|
1118
|
+
distinct: this.distinct
|
|
1119
|
+
});
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
class Ft extends Pt {
|
|
1123
|
+
static [f] = "SQLiteSelectQueryBuilder";
|
|
1124
|
+
_;
|
|
1125
|
+
/** @internal */
|
|
1126
|
+
config;
|
|
1127
|
+
joinsNotNullableMap;
|
|
1128
|
+
tableName;
|
|
1129
|
+
isPartialSelect;
|
|
1130
|
+
session;
|
|
1131
|
+
dialect;
|
|
1132
|
+
cacheConfig = void 0;
|
|
1133
|
+
usedTables = /* @__PURE__ */ new Set();
|
|
1134
|
+
constructor({ table: e, fields: t, isPartialSelect: s, session: i, dialect: r, withList: a, distinct: o }) {
|
|
1135
|
+
super(), this.config = {
|
|
1136
|
+
withList: a,
|
|
1137
|
+
table: e,
|
|
1138
|
+
fields: { ...t },
|
|
1139
|
+
distinct: o,
|
|
1140
|
+
setOperators: []
|
|
1141
|
+
}, this.isPartialSelect = s, this.session = i, this.dialect = r, this._ = {
|
|
1142
|
+
selectedFields: t,
|
|
1143
|
+
config: this.config
|
|
1144
|
+
}, this.tableName = ue(e), this.joinsNotNullableMap = typeof this.tableName == "string" ? { [this.tableName]: !0 } : {};
|
|
1145
|
+
for (const c of K(e)) this.usedTables.add(c);
|
|
1146
|
+
}
|
|
1147
|
+
/** @internal */
|
|
1148
|
+
getUsedTables() {
|
|
1149
|
+
return [...this.usedTables];
|
|
1150
|
+
}
|
|
1151
|
+
createJoin(e) {
|
|
1152
|
+
return (t, s) => {
|
|
1153
|
+
const i = this.tableName, r = ue(t);
|
|
1154
|
+
for (const a of K(t)) this.usedTables.add(a);
|
|
1155
|
+
if (typeof r == "string" && this.config.joins?.some((a) => a.alias === r))
|
|
1156
|
+
throw new Error(`Alias "${r}" is already used in this query`);
|
|
1157
|
+
if (!this.isPartialSelect && (Object.keys(this.joinsNotNullableMap).length === 1 && typeof i == "string" && (this.config.fields = {
|
|
1158
|
+
[i]: this.config.fields
|
|
1159
|
+
}), typeof r == "string" && !h(t, b))) {
|
|
1160
|
+
const a = h(t, P) ? t._.selectedFields : h(t, ye) ? t[A].selectedFields : t[m.Symbol.Columns];
|
|
1161
|
+
this.config.fields[r] = a;
|
|
1162
|
+
}
|
|
1163
|
+
if (typeof s == "function" && (s = s(
|
|
1164
|
+
new Proxy(
|
|
1165
|
+
this.config.fields,
|
|
1166
|
+
new T({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
|
|
1167
|
+
)
|
|
1168
|
+
)), this.config.joins || (this.config.joins = []), this.config.joins.push({ on: s, table: t, joinType: e, alias: r }), typeof r == "string")
|
|
1169
|
+
switch (e) {
|
|
1170
|
+
case "left": {
|
|
1171
|
+
this.joinsNotNullableMap[r] = !1;
|
|
1172
|
+
break;
|
|
1173
|
+
}
|
|
1174
|
+
case "right": {
|
|
1175
|
+
this.joinsNotNullableMap = Object.fromEntries(
|
|
1176
|
+
Object.entries(this.joinsNotNullableMap).map(([a]) => [a, !1])
|
|
1177
|
+
), this.joinsNotNullableMap[r] = !0;
|
|
1178
|
+
break;
|
|
1179
|
+
}
|
|
1180
|
+
case "cross":
|
|
1181
|
+
case "inner": {
|
|
1182
|
+
this.joinsNotNullableMap[r] = !0;
|
|
1183
|
+
break;
|
|
1184
|
+
}
|
|
1185
|
+
case "full": {
|
|
1186
|
+
this.joinsNotNullableMap = Object.fromEntries(
|
|
1187
|
+
Object.entries(this.joinsNotNullableMap).map(([a]) => [a, !1])
|
|
1188
|
+
), this.joinsNotNullableMap[r] = !1;
|
|
1189
|
+
break;
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
return this;
|
|
1193
|
+
};
|
|
1194
|
+
}
|
|
1195
|
+
/**
|
|
1196
|
+
* Executes a `left join` operation by adding another table to the current query.
|
|
1197
|
+
*
|
|
1198
|
+
* Calling this method associates each row of the table with the corresponding row from the joined table, if a match is found. If no matching row exists, it sets all columns of the joined table to null.
|
|
1199
|
+
*
|
|
1200
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#left-join}
|
|
1201
|
+
*
|
|
1202
|
+
* @param table the table to join.
|
|
1203
|
+
* @param on the `on` clause.
|
|
1204
|
+
*
|
|
1205
|
+
* @example
|
|
1206
|
+
*
|
|
1207
|
+
* ```ts
|
|
1208
|
+
* // Select all users and their pets
|
|
1209
|
+
* const usersWithPets: { user: User; pets: Pet | null; }[] = await db.select()
|
|
1210
|
+
* .from(users)
|
|
1211
|
+
* .leftJoin(pets, eq(users.id, pets.ownerId))
|
|
1212
|
+
*
|
|
1213
|
+
* // Select userId and petId
|
|
1214
|
+
* const usersIdsAndPetIds: { userId: number; petId: number | null; }[] = await db.select({
|
|
1215
|
+
* userId: users.id,
|
|
1216
|
+
* petId: pets.id,
|
|
1217
|
+
* })
|
|
1218
|
+
* .from(users)
|
|
1219
|
+
* .leftJoin(pets, eq(users.id, pets.ownerId))
|
|
1220
|
+
* ```
|
|
1221
|
+
*/
|
|
1222
|
+
leftJoin = this.createJoin("left");
|
|
1223
|
+
/**
|
|
1224
|
+
* Executes a `right join` operation by adding another table to the current query.
|
|
1225
|
+
*
|
|
1226
|
+
* Calling this method associates each row of the joined table with the corresponding row from the main table, if a match is found. If no matching row exists, it sets all columns of the main table to null.
|
|
1227
|
+
*
|
|
1228
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#right-join}
|
|
1229
|
+
*
|
|
1230
|
+
* @param table the table to join.
|
|
1231
|
+
* @param on the `on` clause.
|
|
1232
|
+
*
|
|
1233
|
+
* @example
|
|
1234
|
+
*
|
|
1235
|
+
* ```ts
|
|
1236
|
+
* // Select all users and their pets
|
|
1237
|
+
* const usersWithPets: { user: User | null; pets: Pet; }[] = await db.select()
|
|
1238
|
+
* .from(users)
|
|
1239
|
+
* .rightJoin(pets, eq(users.id, pets.ownerId))
|
|
1240
|
+
*
|
|
1241
|
+
* // Select userId and petId
|
|
1242
|
+
* const usersIdsAndPetIds: { userId: number | null; petId: number; }[] = await db.select({
|
|
1243
|
+
* userId: users.id,
|
|
1244
|
+
* petId: pets.id,
|
|
1245
|
+
* })
|
|
1246
|
+
* .from(users)
|
|
1247
|
+
* .rightJoin(pets, eq(users.id, pets.ownerId))
|
|
1248
|
+
* ```
|
|
1249
|
+
*/
|
|
1250
|
+
rightJoin = this.createJoin("right");
|
|
1251
|
+
/**
|
|
1252
|
+
* Executes an `inner join` operation, creating a new table by combining rows from two tables that have matching values.
|
|
1253
|
+
*
|
|
1254
|
+
* Calling this method retrieves rows that have corresponding entries in both joined tables. Rows without matching entries in either table are excluded, resulting in a table that includes only matching pairs.
|
|
1255
|
+
*
|
|
1256
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#inner-join}
|
|
1257
|
+
*
|
|
1258
|
+
* @param table the table to join.
|
|
1259
|
+
* @param on the `on` clause.
|
|
1260
|
+
*
|
|
1261
|
+
* @example
|
|
1262
|
+
*
|
|
1263
|
+
* ```ts
|
|
1264
|
+
* // Select all users and their pets
|
|
1265
|
+
* const usersWithPets: { user: User; pets: Pet; }[] = await db.select()
|
|
1266
|
+
* .from(users)
|
|
1267
|
+
* .innerJoin(pets, eq(users.id, pets.ownerId))
|
|
1268
|
+
*
|
|
1269
|
+
* // Select userId and petId
|
|
1270
|
+
* const usersIdsAndPetIds: { userId: number; petId: number; }[] = await db.select({
|
|
1271
|
+
* userId: users.id,
|
|
1272
|
+
* petId: pets.id,
|
|
1273
|
+
* })
|
|
1274
|
+
* .from(users)
|
|
1275
|
+
* .innerJoin(pets, eq(users.id, pets.ownerId))
|
|
1276
|
+
* ```
|
|
1277
|
+
*/
|
|
1278
|
+
innerJoin = this.createJoin("inner");
|
|
1279
|
+
/**
|
|
1280
|
+
* Executes a `full join` operation by combining rows from two tables into a new table.
|
|
1281
|
+
*
|
|
1282
|
+
* Calling this method retrieves all rows from both main and joined tables, merging rows with matching values and filling in `null` for non-matching columns.
|
|
1283
|
+
*
|
|
1284
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#full-join}
|
|
1285
|
+
*
|
|
1286
|
+
* @param table the table to join.
|
|
1287
|
+
* @param on the `on` clause.
|
|
1288
|
+
*
|
|
1289
|
+
* @example
|
|
1290
|
+
*
|
|
1291
|
+
* ```ts
|
|
1292
|
+
* // Select all users and their pets
|
|
1293
|
+
* const usersWithPets: { user: User | null; pets: Pet | null; }[] = await db.select()
|
|
1294
|
+
* .from(users)
|
|
1295
|
+
* .fullJoin(pets, eq(users.id, pets.ownerId))
|
|
1296
|
+
*
|
|
1297
|
+
* // Select userId and petId
|
|
1298
|
+
* const usersIdsAndPetIds: { userId: number | null; petId: number | null; }[] = await db.select({
|
|
1299
|
+
* userId: users.id,
|
|
1300
|
+
* petId: pets.id,
|
|
1301
|
+
* })
|
|
1302
|
+
* .from(users)
|
|
1303
|
+
* .fullJoin(pets, eq(users.id, pets.ownerId))
|
|
1304
|
+
* ```
|
|
1305
|
+
*/
|
|
1306
|
+
fullJoin = this.createJoin("full");
|
|
1307
|
+
/**
|
|
1308
|
+
* Executes a `cross join` operation by combining rows from two tables into a new table.
|
|
1309
|
+
*
|
|
1310
|
+
* Calling this method retrieves all rows from both main and joined tables, merging all rows from each table.
|
|
1311
|
+
*
|
|
1312
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#cross-join}
|
|
1313
|
+
*
|
|
1314
|
+
* @param table the table to join.
|
|
1315
|
+
*
|
|
1316
|
+
* @example
|
|
1317
|
+
*
|
|
1318
|
+
* ```ts
|
|
1319
|
+
* // Select all users, each user with every pet
|
|
1320
|
+
* const usersWithPets: { user: User; pets: Pet; }[] = await db.select()
|
|
1321
|
+
* .from(users)
|
|
1322
|
+
* .crossJoin(pets)
|
|
1323
|
+
*
|
|
1324
|
+
* // Select userId and petId
|
|
1325
|
+
* const usersIdsAndPetIds: { userId: number; petId: number; }[] = await db.select({
|
|
1326
|
+
* userId: users.id,
|
|
1327
|
+
* petId: pets.id,
|
|
1328
|
+
* })
|
|
1329
|
+
* .from(users)
|
|
1330
|
+
* .crossJoin(pets)
|
|
1331
|
+
* ```
|
|
1332
|
+
*/
|
|
1333
|
+
crossJoin = this.createJoin("cross");
|
|
1334
|
+
createSetOperator(e, t) {
|
|
1335
|
+
return (s) => {
|
|
1336
|
+
const i = typeof s == "function" ? s(It()) : s;
|
|
1337
|
+
if (!pe(this.getSelectedFields(), i.getSelectedFields()))
|
|
1338
|
+
throw new Error(
|
|
1339
|
+
"Set operator error (union / intersect / except): selected fields are not the same or are in a different order"
|
|
1340
|
+
);
|
|
1341
|
+
return this.config.setOperators.push({ type: e, isAll: t, rightSelect: i }), this;
|
|
1342
|
+
};
|
|
1343
|
+
}
|
|
1344
|
+
/**
|
|
1345
|
+
* Adds `union` set operator to the query.
|
|
1346
|
+
*
|
|
1347
|
+
* Calling this method will combine the result sets of the `select` statements and remove any duplicate rows that appear across them.
|
|
1348
|
+
*
|
|
1349
|
+
* See docs: {@link https://orm.drizzle.team/docs/set-operations#union}
|
|
1350
|
+
*
|
|
1351
|
+
* @example
|
|
1352
|
+
*
|
|
1353
|
+
* ```ts
|
|
1354
|
+
* // Select all unique names from customers and users tables
|
|
1355
|
+
* await db.select({ name: users.name })
|
|
1356
|
+
* .from(users)
|
|
1357
|
+
* .union(
|
|
1358
|
+
* db.select({ name: customers.name }).from(customers)
|
|
1359
|
+
* );
|
|
1360
|
+
* // or
|
|
1361
|
+
* import { union } from 'drizzle-orm/sqlite-core'
|
|
1362
|
+
*
|
|
1363
|
+
* await union(
|
|
1364
|
+
* db.select({ name: users.name }).from(users),
|
|
1365
|
+
* db.select({ name: customers.name }).from(customers)
|
|
1366
|
+
* );
|
|
1367
|
+
* ```
|
|
1368
|
+
*/
|
|
1369
|
+
union = this.createSetOperator("union", !1);
|
|
1370
|
+
/**
|
|
1371
|
+
* Adds `union all` set operator to the query.
|
|
1372
|
+
*
|
|
1373
|
+
* Calling this method will combine the result-set of the `select` statements and keep all duplicate rows that appear across them.
|
|
1374
|
+
*
|
|
1375
|
+
* See docs: {@link https://orm.drizzle.team/docs/set-operations#union-all}
|
|
1376
|
+
*
|
|
1377
|
+
* @example
|
|
1378
|
+
*
|
|
1379
|
+
* ```ts
|
|
1380
|
+
* // Select all transaction ids from both online and in-store sales
|
|
1381
|
+
* await db.select({ transaction: onlineSales.transactionId })
|
|
1382
|
+
* .from(onlineSales)
|
|
1383
|
+
* .unionAll(
|
|
1384
|
+
* db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
|
|
1385
|
+
* );
|
|
1386
|
+
* // or
|
|
1387
|
+
* import { unionAll } from 'drizzle-orm/sqlite-core'
|
|
1388
|
+
*
|
|
1389
|
+
* await unionAll(
|
|
1390
|
+
* db.select({ transaction: onlineSales.transactionId }).from(onlineSales),
|
|
1391
|
+
* db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
|
|
1392
|
+
* );
|
|
1393
|
+
* ```
|
|
1394
|
+
*/
|
|
1395
|
+
unionAll = this.createSetOperator("union", !0);
|
|
1396
|
+
/**
|
|
1397
|
+
* Adds `intersect` set operator to the query.
|
|
1398
|
+
*
|
|
1399
|
+
* Calling this method will retain only the rows that are present in both result sets and eliminate duplicates.
|
|
1400
|
+
*
|
|
1401
|
+
* See docs: {@link https://orm.drizzle.team/docs/set-operations#intersect}
|
|
1402
|
+
*
|
|
1403
|
+
* @example
|
|
1404
|
+
*
|
|
1405
|
+
* ```ts
|
|
1406
|
+
* // Select course names that are offered in both departments A and B
|
|
1407
|
+
* await db.select({ courseName: depA.courseName })
|
|
1408
|
+
* .from(depA)
|
|
1409
|
+
* .intersect(
|
|
1410
|
+
* db.select({ courseName: depB.courseName }).from(depB)
|
|
1411
|
+
* );
|
|
1412
|
+
* // or
|
|
1413
|
+
* import { intersect } from 'drizzle-orm/sqlite-core'
|
|
1414
|
+
*
|
|
1415
|
+
* await intersect(
|
|
1416
|
+
* db.select({ courseName: depA.courseName }).from(depA),
|
|
1417
|
+
* db.select({ courseName: depB.courseName }).from(depB)
|
|
1418
|
+
* );
|
|
1419
|
+
* ```
|
|
1420
|
+
*/
|
|
1421
|
+
intersect = this.createSetOperator("intersect", !1);
|
|
1422
|
+
/**
|
|
1423
|
+
* Adds `except` set operator to the query.
|
|
1424
|
+
*
|
|
1425
|
+
* Calling this method will retrieve all unique rows from the left query, except for the rows that are present in the result set of the right query.
|
|
1426
|
+
*
|
|
1427
|
+
* See docs: {@link https://orm.drizzle.team/docs/set-operations#except}
|
|
1428
|
+
*
|
|
1429
|
+
* @example
|
|
1430
|
+
*
|
|
1431
|
+
* ```ts
|
|
1432
|
+
* // Select all courses offered in department A but not in department B
|
|
1433
|
+
* await db.select({ courseName: depA.courseName })
|
|
1434
|
+
* .from(depA)
|
|
1435
|
+
* .except(
|
|
1436
|
+
* db.select({ courseName: depB.courseName }).from(depB)
|
|
1437
|
+
* );
|
|
1438
|
+
* // or
|
|
1439
|
+
* import { except } from 'drizzle-orm/sqlite-core'
|
|
1440
|
+
*
|
|
1441
|
+
* await except(
|
|
1442
|
+
* db.select({ courseName: depA.courseName }).from(depA),
|
|
1443
|
+
* db.select({ courseName: depB.courseName }).from(depB)
|
|
1444
|
+
* );
|
|
1445
|
+
* ```
|
|
1446
|
+
*/
|
|
1447
|
+
except = this.createSetOperator("except", !1);
|
|
1448
|
+
/** @internal */
|
|
1449
|
+
addSetOperators(e) {
|
|
1450
|
+
return this.config.setOperators.push(...e), this;
|
|
1451
|
+
}
|
|
1452
|
+
/**
|
|
1453
|
+
* Adds a `where` clause to the query.
|
|
1454
|
+
*
|
|
1455
|
+
* Calling this method will select only those rows that fulfill a specified condition.
|
|
1456
|
+
*
|
|
1457
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#filtering}
|
|
1458
|
+
*
|
|
1459
|
+
* @param where the `where` clause.
|
|
1460
|
+
*
|
|
1461
|
+
* @example
|
|
1462
|
+
* You can use conditional operators and `sql function` to filter the rows to be selected.
|
|
1463
|
+
*
|
|
1464
|
+
* ```ts
|
|
1465
|
+
* // Select all cars with green color
|
|
1466
|
+
* await db.select().from(cars).where(eq(cars.color, 'green'));
|
|
1467
|
+
* // or
|
|
1468
|
+
* await db.select().from(cars).where(sql`${cars.color} = 'green'`)
|
|
1469
|
+
* ```
|
|
1470
|
+
*
|
|
1471
|
+
* You can logically combine conditional operators with `and()` and `or()` operators:
|
|
1472
|
+
*
|
|
1473
|
+
* ```ts
|
|
1474
|
+
* // Select all BMW cars with a green color
|
|
1475
|
+
* await db.select().from(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
|
|
1476
|
+
*
|
|
1477
|
+
* // Select all cars with the green or blue color
|
|
1478
|
+
* await db.select().from(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
|
|
1479
|
+
* ```
|
|
1480
|
+
*/
|
|
1481
|
+
where(e) {
|
|
1482
|
+
return typeof e == "function" && (e = e(
|
|
1483
|
+
new Proxy(
|
|
1484
|
+
this.config.fields,
|
|
1485
|
+
new T({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
|
|
1486
|
+
)
|
|
1487
|
+
)), this.config.where = e, this;
|
|
1488
|
+
}
|
|
1489
|
+
/**
|
|
1490
|
+
* Adds a `having` clause to the query.
|
|
1491
|
+
*
|
|
1492
|
+
* Calling this method will select only those rows that fulfill a specified condition. It is typically used with aggregate functions to filter the aggregated data based on a specified condition.
|
|
1493
|
+
*
|
|
1494
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#aggregations}
|
|
1495
|
+
*
|
|
1496
|
+
* @param having the `having` clause.
|
|
1497
|
+
*
|
|
1498
|
+
* @example
|
|
1499
|
+
*
|
|
1500
|
+
* ```ts
|
|
1501
|
+
* // Select all brands with more than one car
|
|
1502
|
+
* await db.select({
|
|
1503
|
+
* brand: cars.brand,
|
|
1504
|
+
* count: sql<number>`cast(count(${cars.id}) as int)`,
|
|
1505
|
+
* })
|
|
1506
|
+
* .from(cars)
|
|
1507
|
+
* .groupBy(cars.brand)
|
|
1508
|
+
* .having(({ count }) => gt(count, 1));
|
|
1509
|
+
* ```
|
|
1510
|
+
*/
|
|
1511
|
+
having(e) {
|
|
1512
|
+
return typeof e == "function" && (e = e(
|
|
1513
|
+
new Proxy(
|
|
1514
|
+
this.config.fields,
|
|
1515
|
+
new T({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
|
|
1516
|
+
)
|
|
1517
|
+
)), this.config.having = e, this;
|
|
1518
|
+
}
|
|
1519
|
+
groupBy(...e) {
|
|
1520
|
+
if (typeof e[0] == "function") {
|
|
1521
|
+
const t = e[0](
|
|
1522
|
+
new Proxy(
|
|
1523
|
+
this.config.fields,
|
|
1524
|
+
new T({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" })
|
|
1525
|
+
)
|
|
1526
|
+
);
|
|
1527
|
+
this.config.groupBy = Array.isArray(t) ? t : [t];
|
|
1528
|
+
} else
|
|
1529
|
+
this.config.groupBy = e;
|
|
1530
|
+
return this;
|
|
1531
|
+
}
|
|
1532
|
+
orderBy(...e) {
|
|
1533
|
+
if (typeof e[0] == "function") {
|
|
1534
|
+
const t = e[0](
|
|
1535
|
+
new Proxy(
|
|
1536
|
+
this.config.fields,
|
|
1537
|
+
new T({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" })
|
|
1538
|
+
)
|
|
1539
|
+
), s = Array.isArray(t) ? t : [t];
|
|
1540
|
+
this.config.setOperators.length > 0 ? this.config.setOperators.at(-1).orderBy = s : this.config.orderBy = s;
|
|
1541
|
+
} else {
|
|
1542
|
+
const t = e;
|
|
1543
|
+
this.config.setOperators.length > 0 ? this.config.setOperators.at(-1).orderBy = t : this.config.orderBy = t;
|
|
1544
|
+
}
|
|
1545
|
+
return this;
|
|
1546
|
+
}
|
|
1547
|
+
/**
|
|
1548
|
+
* Adds a `limit` clause to the query.
|
|
1549
|
+
*
|
|
1550
|
+
* Calling this method will set the maximum number of rows that will be returned by this query.
|
|
1551
|
+
*
|
|
1552
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#limit--offset}
|
|
1553
|
+
*
|
|
1554
|
+
* @param limit the `limit` clause.
|
|
1555
|
+
*
|
|
1556
|
+
* @example
|
|
1557
|
+
*
|
|
1558
|
+
* ```ts
|
|
1559
|
+
* // Get the first 10 people from this query.
|
|
1560
|
+
* await db.select().from(people).limit(10);
|
|
1561
|
+
* ```
|
|
1562
|
+
*/
|
|
1563
|
+
limit(e) {
|
|
1564
|
+
return this.config.setOperators.length > 0 ? this.config.setOperators.at(-1).limit = e : this.config.limit = e, this;
|
|
1565
|
+
}
|
|
1566
|
+
/**
|
|
1567
|
+
* Adds an `offset` clause to the query.
|
|
1568
|
+
*
|
|
1569
|
+
* Calling this method will skip a number of rows when returning results from this query.
|
|
1570
|
+
*
|
|
1571
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#limit--offset}
|
|
1572
|
+
*
|
|
1573
|
+
* @param offset the `offset` clause.
|
|
1574
|
+
*
|
|
1575
|
+
* @example
|
|
1576
|
+
*
|
|
1577
|
+
* ```ts
|
|
1578
|
+
* // Get the 10th-20th people from this query.
|
|
1579
|
+
* await db.select().from(people).offset(10).limit(10);
|
|
1580
|
+
* ```
|
|
1581
|
+
*/
|
|
1582
|
+
offset(e) {
|
|
1583
|
+
return this.config.setOperators.length > 0 ? this.config.setOperators.at(-1).offset = e : this.config.offset = e, this;
|
|
1584
|
+
}
|
|
1585
|
+
/** @internal */
|
|
1586
|
+
getSQL() {
|
|
1587
|
+
return this.dialect.buildSelectQuery(this.config);
|
|
1588
|
+
}
|
|
1589
|
+
toSQL() {
|
|
1590
|
+
const { typings: e, ...t } = this.dialect.sqlToQuery(this.getSQL());
|
|
1591
|
+
return t;
|
|
1592
|
+
}
|
|
1593
|
+
as(e) {
|
|
1594
|
+
const t = [];
|
|
1595
|
+
if (t.push(...K(this.config.table)), this.config.joins)
|
|
1596
|
+
for (const s of this.config.joins) t.push(...K(s.table));
|
|
1597
|
+
return new Proxy(
|
|
1598
|
+
new P(this.getSQL(), this.config.fields, e, !1, [...new Set(t)]),
|
|
1599
|
+
new T({ alias: e, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
|
|
1600
|
+
);
|
|
1601
|
+
}
|
|
1602
|
+
/** @internal */
|
|
1603
|
+
getSelectedFields() {
|
|
1604
|
+
return new Proxy(
|
|
1605
|
+
this.config.fields,
|
|
1606
|
+
new T({ alias: this.tableName, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
|
|
1607
|
+
);
|
|
1608
|
+
}
|
|
1609
|
+
$dynamic() {
|
|
1610
|
+
return this;
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
class Fe extends Ft {
|
|
1614
|
+
static [f] = "SQLiteSelect";
|
|
1615
|
+
/** @internal */
|
|
1616
|
+
_prepare(e = !0) {
|
|
1617
|
+
if (!this.session)
|
|
1618
|
+
throw new Error("Cannot execute a query on a query builder. Please use a database instance instead.");
|
|
1619
|
+
const t = W(this.config.fields), s = this.session[e ? "prepareOneTimeQuery" : "prepareQuery"](
|
|
1620
|
+
this.dialect.sqlToQuery(this.getSQL()),
|
|
1621
|
+
t,
|
|
1622
|
+
"all",
|
|
1623
|
+
!0,
|
|
1624
|
+
void 0,
|
|
1625
|
+
{
|
|
1626
|
+
type: "select",
|
|
1627
|
+
tables: [...this.usedTables]
|
|
1628
|
+
},
|
|
1629
|
+
this.cacheConfig
|
|
1630
|
+
);
|
|
1631
|
+
return s.joinsNotNullableMap = this.joinsNotNullableMap, s;
|
|
1632
|
+
}
|
|
1633
|
+
$withCache(e) {
|
|
1634
|
+
return this.cacheConfig = e === void 0 ? { config: {}, enable: !0, autoInvalidate: !0 } : e === !1 ? { enable: !1 } : { enable: !0, autoInvalidate: !0, ...e }, this;
|
|
1635
|
+
}
|
|
1636
|
+
prepare() {
|
|
1637
|
+
return this._prepare(!1);
|
|
1638
|
+
}
|
|
1639
|
+
run = (e) => this._prepare().run(e);
|
|
1640
|
+
all = (e) => this._prepare().all(e);
|
|
1641
|
+
get = (e) => this._prepare().get(e);
|
|
1642
|
+
values = (e) => this._prepare().values(e);
|
|
1643
|
+
async execute() {
|
|
1644
|
+
return this.all();
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
pt(Fe, [D]);
|
|
1648
|
+
function ae(l, e) {
|
|
1649
|
+
return (t, s, ...i) => {
|
|
1650
|
+
const r = [s, ...i].map((a) => ({
|
|
1651
|
+
type: l,
|
|
1652
|
+
isAll: e,
|
|
1653
|
+
rightSelect: a
|
|
1654
|
+
}));
|
|
1655
|
+
for (const a of r)
|
|
1656
|
+
if (!pe(t.getSelectedFields(), a.rightSelect.getSelectedFields()))
|
|
1657
|
+
throw new Error(
|
|
1658
|
+
"Set operator error (union / intersect / except): selected fields are not the same or are in a different order"
|
|
1659
|
+
);
|
|
1660
|
+
return t.addSetOperators(r);
|
|
1661
|
+
};
|
|
1662
|
+
}
|
|
1663
|
+
const It = () => ({
|
|
1664
|
+
union: Kt,
|
|
1665
|
+
unionAll: kt,
|
|
1666
|
+
intersect: Dt,
|
|
1667
|
+
except: zt
|
|
1668
|
+
}), Kt = ae("union", !1), kt = ae("union", !0), Dt = ae("intersect", !1), zt = ae("except", !1);
|
|
1669
|
+
class Ie {
|
|
1670
|
+
static [f] = "SQLiteQueryBuilder";
|
|
1671
|
+
dialect;
|
|
1672
|
+
dialectConfig;
|
|
1673
|
+
constructor(e) {
|
|
1674
|
+
this.dialect = h(e, ie) ? e : void 0, this.dialectConfig = h(e, ie) ? void 0 : e;
|
|
1675
|
+
}
|
|
1676
|
+
$with = (e, t) => {
|
|
1677
|
+
const s = this;
|
|
1678
|
+
return { as: (r) => (typeof r == "function" && (r = r(s)), new Proxy(
|
|
1679
|
+
new Ae(
|
|
1680
|
+
r.getSQL(),
|
|
1681
|
+
t ?? ("getSelectedFields" in r ? r.getSelectedFields() ?? {} : {}),
|
|
1682
|
+
e,
|
|
1683
|
+
!0
|
|
1684
|
+
),
|
|
1685
|
+
new T({ alias: e, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
|
|
1686
|
+
)) };
|
|
1687
|
+
};
|
|
1688
|
+
with(...e) {
|
|
1689
|
+
const t = this;
|
|
1690
|
+
function s(r) {
|
|
1691
|
+
return new I({
|
|
1692
|
+
fields: r ?? void 0,
|
|
1693
|
+
session: void 0,
|
|
1694
|
+
dialect: t.getDialect(),
|
|
1695
|
+
withList: e
|
|
1696
|
+
});
|
|
1697
|
+
}
|
|
1698
|
+
function i(r) {
|
|
1699
|
+
return new I({
|
|
1700
|
+
fields: r ?? void 0,
|
|
1701
|
+
session: void 0,
|
|
1702
|
+
dialect: t.getDialect(),
|
|
1703
|
+
withList: e,
|
|
1704
|
+
distinct: !0
|
|
1705
|
+
});
|
|
1706
|
+
}
|
|
1707
|
+
return { select: s, selectDistinct: i };
|
|
1708
|
+
}
|
|
1709
|
+
select(e) {
|
|
1710
|
+
return new I({ fields: e ?? void 0, session: void 0, dialect: this.getDialect() });
|
|
1711
|
+
}
|
|
1712
|
+
selectDistinct(e) {
|
|
1713
|
+
return new I({
|
|
1714
|
+
fields: e ?? void 0,
|
|
1715
|
+
session: void 0,
|
|
1716
|
+
dialect: this.getDialect(),
|
|
1717
|
+
distinct: !0
|
|
1718
|
+
});
|
|
1719
|
+
}
|
|
1720
|
+
// Lazy load dialect to avoid circular dependency
|
|
1721
|
+
getDialect() {
|
|
1722
|
+
return this.dialect || (this.dialect = new jt(this.dialectConfig)), this.dialect;
|
|
1723
|
+
}
|
|
1724
|
+
}
|
|
1725
|
+
class Te {
|
|
1726
|
+
constructor(e, t, s, i) {
|
|
1727
|
+
this.table = e, this.session = t, this.dialect = s, this.withList = i;
|
|
1728
|
+
}
|
|
1729
|
+
static [f] = "SQLiteInsertBuilder";
|
|
1730
|
+
values(e) {
|
|
1731
|
+
if (e = Array.isArray(e) ? e : [e], e.length === 0)
|
|
1732
|
+
throw new Error("values() must be called with at least one value");
|
|
1733
|
+
const t = e.map((s) => {
|
|
1734
|
+
const i = {}, r = this.table[m.Symbol.Columns];
|
|
1735
|
+
for (const a of Object.keys(s)) {
|
|
1736
|
+
const o = s[a];
|
|
1737
|
+
i[a] = h(o, b) ? o : new Oe(o, r[a]);
|
|
1738
|
+
}
|
|
1739
|
+
return i;
|
|
1740
|
+
});
|
|
1741
|
+
return new Le(this.table, t, this.session, this.dialect, this.withList);
|
|
1742
|
+
}
|
|
1743
|
+
select(e) {
|
|
1744
|
+
const t = typeof e == "function" ? e(new Ie()) : e;
|
|
1745
|
+
if (!h(t, b) && !pe(this.table[gt], t._.selectedFields))
|
|
1746
|
+
throw new Error(
|
|
1747
|
+
"Insert select error: selected fields are not the same or are in a different order compared to the table definition"
|
|
1748
|
+
);
|
|
1749
|
+
return new Le(this.table, t, this.session, this.dialect, this.withList, !0);
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
class Le extends D {
|
|
1753
|
+
constructor(e, t, s, i, r, a) {
|
|
1754
|
+
super(), this.session = s, this.dialect = i, this.config = { table: e, values: t, withList: r, select: a };
|
|
1755
|
+
}
|
|
1756
|
+
static [f] = "SQLiteInsert";
|
|
1757
|
+
/** @internal */
|
|
1758
|
+
config;
|
|
1759
|
+
returning(e = this.config.table[O.Symbol.Columns]) {
|
|
1760
|
+
return this.config.returning = W(e), this;
|
|
1761
|
+
}
|
|
1762
|
+
/**
|
|
1763
|
+
* Adds an `on conflict do nothing` clause to the query.
|
|
1764
|
+
*
|
|
1765
|
+
* Calling this method simply avoids inserting a row as its alternative action.
|
|
1766
|
+
*
|
|
1767
|
+
* See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
|
|
1768
|
+
*
|
|
1769
|
+
* @param config The `target` and `where` clauses.
|
|
1770
|
+
*
|
|
1771
|
+
* @example
|
|
1772
|
+
* ```ts
|
|
1773
|
+
* // Insert one row and cancel the insert if there's a conflict
|
|
1774
|
+
* await db.insert(cars)
|
|
1775
|
+
* .values({ id: 1, brand: 'BMW' })
|
|
1776
|
+
* .onConflictDoNothing();
|
|
1777
|
+
*
|
|
1778
|
+
* // Explicitly specify conflict target
|
|
1779
|
+
* await db.insert(cars)
|
|
1780
|
+
* .values({ id: 1, brand: 'BMW' })
|
|
1781
|
+
* .onConflictDoNothing({ target: cars.id });
|
|
1782
|
+
* ```
|
|
1783
|
+
*/
|
|
1784
|
+
onConflictDoNothing(e = {}) {
|
|
1785
|
+
if (this.config.onConflict || (this.config.onConflict = []), e.target === void 0)
|
|
1786
|
+
this.config.onConflict.push(n` on conflict do nothing`);
|
|
1787
|
+
else {
|
|
1788
|
+
const t = Array.isArray(e.target) ? n`${e.target}` : n`${[e.target]}`, s = e.where ? n` where ${e.where}` : n``;
|
|
1789
|
+
this.config.onConflict.push(n` on conflict ${t} do nothing${s}`);
|
|
1790
|
+
}
|
|
1791
|
+
return this;
|
|
1792
|
+
}
|
|
1793
|
+
/**
|
|
1794
|
+
* Adds an `on conflict do update` clause to the query.
|
|
1795
|
+
*
|
|
1796
|
+
* Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
|
|
1797
|
+
*
|
|
1798
|
+
* See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
|
|
1799
|
+
*
|
|
1800
|
+
* @param config The `target`, `set` and `where` clauses.
|
|
1801
|
+
*
|
|
1802
|
+
* @example
|
|
1803
|
+
* ```ts
|
|
1804
|
+
* // Update the row if there's a conflict
|
|
1805
|
+
* await db.insert(cars)
|
|
1806
|
+
* .values({ id: 1, brand: 'BMW' })
|
|
1807
|
+
* .onConflictDoUpdate({
|
|
1808
|
+
* target: cars.id,
|
|
1809
|
+
* set: { brand: 'Porsche' }
|
|
1810
|
+
* });
|
|
1811
|
+
*
|
|
1812
|
+
* // Upsert with 'where' clause
|
|
1813
|
+
* await db.insert(cars)
|
|
1814
|
+
* .values({ id: 1, brand: 'BMW' })
|
|
1815
|
+
* .onConflictDoUpdate({
|
|
1816
|
+
* target: cars.id,
|
|
1817
|
+
* set: { brand: 'newBMW' },
|
|
1818
|
+
* where: sql`${cars.createdAt} > '2023-01-01'::date`,
|
|
1819
|
+
* });
|
|
1820
|
+
* ```
|
|
1821
|
+
*/
|
|
1822
|
+
onConflictDoUpdate(e) {
|
|
1823
|
+
if (e.where && (e.targetWhere || e.setWhere))
|
|
1824
|
+
throw new Error(
|
|
1825
|
+
'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
|
|
1826
|
+
);
|
|
1827
|
+
this.config.onConflict || (this.config.onConflict = []);
|
|
1828
|
+
const t = e.where ? n` where ${e.where}` : void 0, s = e.targetWhere ? n` where ${e.targetWhere}` : void 0, i = e.setWhere ? n` where ${e.setWhere}` : void 0, r = Array.isArray(e.target) ? n`${e.target}` : n`${[e.target]}`, a = this.dialect.buildUpdateSet(this.config.table, Me(this.config.table, e.set));
|
|
1829
|
+
return this.config.onConflict.push(
|
|
1830
|
+
n` on conflict ${r}${s} do update set ${a}${t}${i}`
|
|
1831
|
+
), this;
|
|
1832
|
+
}
|
|
1833
|
+
/** @internal */
|
|
1834
|
+
getSQL() {
|
|
1835
|
+
return this.dialect.buildInsertQuery(this.config);
|
|
1836
|
+
}
|
|
1837
|
+
toSQL() {
|
|
1838
|
+
const { typings: e, ...t } = this.dialect.sqlToQuery(this.getSQL());
|
|
1839
|
+
return t;
|
|
1840
|
+
}
|
|
1841
|
+
/** @internal */
|
|
1842
|
+
_prepare(e = !0) {
|
|
1843
|
+
return this.session[e ? "prepareOneTimeQuery" : "prepareQuery"](
|
|
1844
|
+
this.dialect.sqlToQuery(this.getSQL()),
|
|
1845
|
+
this.config.returning,
|
|
1846
|
+
this.config.returning ? "all" : "run",
|
|
1847
|
+
!0,
|
|
1848
|
+
void 0,
|
|
1849
|
+
{
|
|
1850
|
+
type: "insert",
|
|
1851
|
+
tables: K(this.config.table)
|
|
1852
|
+
}
|
|
1853
|
+
);
|
|
1854
|
+
}
|
|
1855
|
+
prepare() {
|
|
1856
|
+
return this._prepare(!1);
|
|
1857
|
+
}
|
|
1858
|
+
run = (e) => this._prepare().run(e);
|
|
1859
|
+
all = (e) => this._prepare().all(e);
|
|
1860
|
+
get = (e) => this._prepare().get(e);
|
|
1861
|
+
values = (e) => this._prepare().values(e);
|
|
1862
|
+
async execute() {
|
|
1863
|
+
return this.config.returning ? this.all() : this.run();
|
|
1864
|
+
}
|
|
1865
|
+
$dynamic() {
|
|
1866
|
+
return this;
|
|
1867
|
+
}
|
|
1868
|
+
}
|
|
1869
|
+
class xe {
|
|
1870
|
+
constructor(e, t, s, i) {
|
|
1871
|
+
this.table = e, this.session = t, this.dialect = s, this.withList = i;
|
|
1872
|
+
}
|
|
1873
|
+
static [f] = "SQLiteUpdateBuilder";
|
|
1874
|
+
set(e) {
|
|
1875
|
+
return new Jt(
|
|
1876
|
+
this.table,
|
|
1877
|
+
Me(this.table, e),
|
|
1878
|
+
this.session,
|
|
1879
|
+
this.dialect,
|
|
1880
|
+
this.withList
|
|
1881
|
+
);
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
class Jt extends D {
|
|
1885
|
+
constructor(e, t, s, i, r) {
|
|
1886
|
+
super(), this.session = s, this.dialect = i, this.config = { set: t, table: e, withList: r, joins: [] };
|
|
1887
|
+
}
|
|
1888
|
+
static [f] = "SQLiteUpdate";
|
|
1889
|
+
/** @internal */
|
|
1890
|
+
config;
|
|
1891
|
+
from(e) {
|
|
1892
|
+
return this.config.from = e, this;
|
|
1893
|
+
}
|
|
1894
|
+
createJoin(e) {
|
|
1895
|
+
return (t, s) => {
|
|
1896
|
+
const i = ue(t);
|
|
1897
|
+
if (typeof i == "string" && this.config.joins.some((r) => r.alias === i))
|
|
1898
|
+
throw new Error(`Alias "${i}" is already used in this query`);
|
|
1899
|
+
if (typeof s == "function") {
|
|
1900
|
+
const r = this.config.from ? h(t, O) ? t[m.Symbol.Columns] : h(t, P) ? t._.selectedFields : h(t, Se) ? t[A].selectedFields : void 0 : void 0;
|
|
1901
|
+
s = s(
|
|
1902
|
+
new Proxy(
|
|
1903
|
+
this.config.table[m.Symbol.Columns],
|
|
1904
|
+
new T({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
|
|
1905
|
+
),
|
|
1906
|
+
r && new Proxy(
|
|
1907
|
+
r,
|
|
1908
|
+
new T({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
|
|
1909
|
+
)
|
|
1910
|
+
);
|
|
1911
|
+
}
|
|
1912
|
+
return this.config.joins.push({ on: s, table: t, joinType: e, alias: i }), this;
|
|
1913
|
+
};
|
|
1914
|
+
}
|
|
1915
|
+
leftJoin = this.createJoin("left");
|
|
1916
|
+
rightJoin = this.createJoin("right");
|
|
1917
|
+
innerJoin = this.createJoin("inner");
|
|
1918
|
+
fullJoin = this.createJoin("full");
|
|
1919
|
+
/**
|
|
1920
|
+
* Adds a 'where' clause to the query.
|
|
1921
|
+
*
|
|
1922
|
+
* Calling this method will update only those rows that fulfill a specified condition.
|
|
1923
|
+
*
|
|
1924
|
+
* See docs: {@link https://orm.drizzle.team/docs/update}
|
|
1925
|
+
*
|
|
1926
|
+
* @param where the 'where' clause.
|
|
1927
|
+
*
|
|
1928
|
+
* @example
|
|
1929
|
+
* You can use conditional operators and `sql function` to filter the rows to be updated.
|
|
1930
|
+
*
|
|
1931
|
+
* ```ts
|
|
1932
|
+
* // Update all cars with green color
|
|
1933
|
+
* db.update(cars).set({ color: 'red' })
|
|
1934
|
+
* .where(eq(cars.color, 'green'));
|
|
1935
|
+
* // or
|
|
1936
|
+
* db.update(cars).set({ color: 'red' })
|
|
1937
|
+
* .where(sql`${cars.color} = 'green'`)
|
|
1938
|
+
* ```
|
|
1939
|
+
*
|
|
1940
|
+
* You can logically combine conditional operators with `and()` and `or()` operators:
|
|
1941
|
+
*
|
|
1942
|
+
* ```ts
|
|
1943
|
+
* // Update all BMW cars with a green color
|
|
1944
|
+
* db.update(cars).set({ color: 'red' })
|
|
1945
|
+
* .where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
|
|
1946
|
+
*
|
|
1947
|
+
* // Update all cars with the green or blue color
|
|
1948
|
+
* db.update(cars).set({ color: 'red' })
|
|
1949
|
+
* .where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
|
|
1950
|
+
* ```
|
|
1951
|
+
*/
|
|
1952
|
+
where(e) {
|
|
1953
|
+
return this.config.where = e, this;
|
|
1954
|
+
}
|
|
1955
|
+
orderBy(...e) {
|
|
1956
|
+
if (typeof e[0] == "function") {
|
|
1957
|
+
const t = e[0](
|
|
1958
|
+
new Proxy(
|
|
1959
|
+
this.config.table[m.Symbol.Columns],
|
|
1960
|
+
new T({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" })
|
|
1961
|
+
)
|
|
1962
|
+
), s = Array.isArray(t) ? t : [t];
|
|
1963
|
+
this.config.orderBy = s;
|
|
1964
|
+
} else {
|
|
1965
|
+
const t = e;
|
|
1966
|
+
this.config.orderBy = t;
|
|
1967
|
+
}
|
|
1968
|
+
return this;
|
|
1969
|
+
}
|
|
1970
|
+
limit(e) {
|
|
1971
|
+
return this.config.limit = e, this;
|
|
1972
|
+
}
|
|
1973
|
+
returning(e = this.config.table[O.Symbol.Columns]) {
|
|
1974
|
+
return this.config.returning = W(e), this;
|
|
1975
|
+
}
|
|
1976
|
+
/** @internal */
|
|
1977
|
+
getSQL() {
|
|
1978
|
+
return this.dialect.buildUpdateQuery(this.config);
|
|
1979
|
+
}
|
|
1980
|
+
toSQL() {
|
|
1981
|
+
const { typings: e, ...t } = this.dialect.sqlToQuery(this.getSQL());
|
|
1982
|
+
return t;
|
|
1983
|
+
}
|
|
1984
|
+
/** @internal */
|
|
1985
|
+
_prepare(e = !0) {
|
|
1986
|
+
return this.session[e ? "prepareOneTimeQuery" : "prepareQuery"](
|
|
1987
|
+
this.dialect.sqlToQuery(this.getSQL()),
|
|
1988
|
+
this.config.returning,
|
|
1989
|
+
this.config.returning ? "all" : "run",
|
|
1990
|
+
!0,
|
|
1991
|
+
void 0,
|
|
1992
|
+
{
|
|
1993
|
+
type: "insert",
|
|
1994
|
+
tables: K(this.config.table)
|
|
1995
|
+
}
|
|
1996
|
+
);
|
|
1997
|
+
}
|
|
1998
|
+
prepare() {
|
|
1999
|
+
return this._prepare(!1);
|
|
2000
|
+
}
|
|
2001
|
+
run = (e) => this._prepare().run(e);
|
|
2002
|
+
all = (e) => this._prepare().all(e);
|
|
2003
|
+
get = (e) => this._prepare().get(e);
|
|
2004
|
+
values = (e) => this._prepare().values(e);
|
|
2005
|
+
async execute() {
|
|
2006
|
+
return this.config.returning ? this.all() : this.run();
|
|
2007
|
+
}
|
|
2008
|
+
$dynamic() {
|
|
2009
|
+
return this;
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
class re extends b {
|
|
2013
|
+
constructor(e) {
|
|
2014
|
+
super(re.buildEmbeddedCount(e.source, e.filters).queryChunks), this.params = e, this.session = e.session, this.sql = re.buildCount(
|
|
2015
|
+
e.source,
|
|
2016
|
+
e.filters
|
|
2017
|
+
);
|
|
2018
|
+
}
|
|
2019
|
+
sql;
|
|
2020
|
+
static [f] = "SQLiteCountBuilderAsync";
|
|
2021
|
+
[Symbol.toStringTag] = "SQLiteCountBuilderAsync";
|
|
2022
|
+
session;
|
|
2023
|
+
static buildEmbeddedCount(e, t) {
|
|
2024
|
+
return n`(select count(*) from ${e}${n.raw(" where ").if(t)}${t})`;
|
|
2025
|
+
}
|
|
2026
|
+
static buildCount(e, t) {
|
|
2027
|
+
return n`select count(*) from ${e}${n.raw(" where ").if(t)}${t}`;
|
|
2028
|
+
}
|
|
2029
|
+
then(e, t) {
|
|
2030
|
+
return Promise.resolve(this.session.count(this.sql)).then(
|
|
2031
|
+
e,
|
|
2032
|
+
t
|
|
2033
|
+
);
|
|
2034
|
+
}
|
|
2035
|
+
catch(e) {
|
|
2036
|
+
return this.then(void 0, e);
|
|
2037
|
+
}
|
|
2038
|
+
finally(e) {
|
|
2039
|
+
return this.then(
|
|
2040
|
+
(t) => (e?.(), t),
|
|
2041
|
+
(t) => {
|
|
2042
|
+
throw e?.(), t;
|
|
2043
|
+
}
|
|
2044
|
+
);
|
|
2045
|
+
}
|
|
2046
|
+
}
|
|
2047
|
+
class Vt {
|
|
2048
|
+
constructor(e, t, s, i, r, a, o, c) {
|
|
2049
|
+
this.mode = e, this.fullSchema = t, this.schema = s, this.tableNamesMap = i, this.table = r, this.tableConfig = a, this.dialect = o, this.session = c;
|
|
2050
|
+
}
|
|
2051
|
+
static [f] = "SQLiteAsyncRelationalQueryBuilder";
|
|
2052
|
+
findMany(e) {
|
|
2053
|
+
return this.mode === "sync" ? new Re(
|
|
2054
|
+
this.fullSchema,
|
|
2055
|
+
this.schema,
|
|
2056
|
+
this.tableNamesMap,
|
|
2057
|
+
this.table,
|
|
2058
|
+
this.tableConfig,
|
|
2059
|
+
this.dialect,
|
|
2060
|
+
this.session,
|
|
2061
|
+
e || {},
|
|
2062
|
+
"many"
|
|
2063
|
+
) : new fe(
|
|
2064
|
+
this.fullSchema,
|
|
2065
|
+
this.schema,
|
|
2066
|
+
this.tableNamesMap,
|
|
2067
|
+
this.table,
|
|
2068
|
+
this.tableConfig,
|
|
2069
|
+
this.dialect,
|
|
2070
|
+
this.session,
|
|
2071
|
+
e || {},
|
|
2072
|
+
"many"
|
|
2073
|
+
);
|
|
2074
|
+
}
|
|
2075
|
+
findFirst(e) {
|
|
2076
|
+
return this.mode === "sync" ? new Re(
|
|
2077
|
+
this.fullSchema,
|
|
2078
|
+
this.schema,
|
|
2079
|
+
this.tableNamesMap,
|
|
2080
|
+
this.table,
|
|
2081
|
+
this.tableConfig,
|
|
2082
|
+
this.dialect,
|
|
2083
|
+
this.session,
|
|
2084
|
+
e ? { ...e, limit: 1 } : { limit: 1 },
|
|
2085
|
+
"first"
|
|
2086
|
+
) : new fe(
|
|
2087
|
+
this.fullSchema,
|
|
2088
|
+
this.schema,
|
|
2089
|
+
this.tableNamesMap,
|
|
2090
|
+
this.table,
|
|
2091
|
+
this.tableConfig,
|
|
2092
|
+
this.dialect,
|
|
2093
|
+
this.session,
|
|
2094
|
+
e ? { ...e, limit: 1 } : { limit: 1 },
|
|
2095
|
+
"first"
|
|
2096
|
+
);
|
|
2097
|
+
}
|
|
2098
|
+
}
|
|
2099
|
+
class fe extends D {
|
|
2100
|
+
constructor(e, t, s, i, r, a, o, c, u) {
|
|
2101
|
+
super(), this.fullSchema = e, this.schema = t, this.tableNamesMap = s, this.table = i, this.tableConfig = r, this.dialect = a, this.session = o, this.config = c, this.mode = u;
|
|
2102
|
+
}
|
|
2103
|
+
static [f] = "SQLiteAsyncRelationalQuery";
|
|
2104
|
+
/** @internal */
|
|
2105
|
+
mode;
|
|
2106
|
+
/** @internal */
|
|
2107
|
+
getSQL() {
|
|
2108
|
+
return this.dialect.buildRelationalQuery({
|
|
2109
|
+
fullSchema: this.fullSchema,
|
|
2110
|
+
schema: this.schema,
|
|
2111
|
+
tableNamesMap: this.tableNamesMap,
|
|
2112
|
+
table: this.table,
|
|
2113
|
+
tableConfig: this.tableConfig,
|
|
2114
|
+
queryConfig: this.config,
|
|
2115
|
+
tableAlias: this.tableConfig.tsName
|
|
2116
|
+
}).sql;
|
|
2117
|
+
}
|
|
2118
|
+
/** @internal */
|
|
2119
|
+
_prepare(e = !1) {
|
|
2120
|
+
const { query: t, builtQuery: s } = this._toSQL();
|
|
2121
|
+
return this.session[e ? "prepareOneTimeQuery" : "prepareQuery"](
|
|
2122
|
+
s,
|
|
2123
|
+
void 0,
|
|
2124
|
+
this.mode === "first" ? "get" : "all",
|
|
2125
|
+
!0,
|
|
2126
|
+
(i, r) => {
|
|
2127
|
+
const a = i.map(
|
|
2128
|
+
(o) => de(this.schema, this.tableConfig, o, t.selection, r)
|
|
2129
|
+
);
|
|
2130
|
+
return this.mode === "first" ? a[0] : a;
|
|
2131
|
+
}
|
|
2132
|
+
);
|
|
2133
|
+
}
|
|
2134
|
+
prepare() {
|
|
2135
|
+
return this._prepare(!1);
|
|
2136
|
+
}
|
|
2137
|
+
_toSQL() {
|
|
2138
|
+
const e = this.dialect.buildRelationalQuery({
|
|
2139
|
+
fullSchema: this.fullSchema,
|
|
2140
|
+
schema: this.schema,
|
|
2141
|
+
tableNamesMap: this.tableNamesMap,
|
|
2142
|
+
table: this.table,
|
|
2143
|
+
tableConfig: this.tableConfig,
|
|
2144
|
+
queryConfig: this.config,
|
|
2145
|
+
tableAlias: this.tableConfig.tsName
|
|
2146
|
+
}), t = this.dialect.sqlToQuery(e.sql);
|
|
2147
|
+
return { query: e, builtQuery: t };
|
|
2148
|
+
}
|
|
2149
|
+
toSQL() {
|
|
2150
|
+
return this._toSQL().builtQuery;
|
|
2151
|
+
}
|
|
2152
|
+
/** @internal */
|
|
2153
|
+
executeRaw() {
|
|
2154
|
+
return this.mode === "first" ? this._prepare(!1).get() : this._prepare(!1).all();
|
|
2155
|
+
}
|
|
2156
|
+
async execute() {
|
|
2157
|
+
return this.executeRaw();
|
|
2158
|
+
}
|
|
2159
|
+
}
|
|
2160
|
+
class Re extends fe {
|
|
2161
|
+
static [f] = "SQLiteSyncRelationalQuery";
|
|
2162
|
+
sync() {
|
|
2163
|
+
return this.executeRaw();
|
|
2164
|
+
}
|
|
2165
|
+
}
|
|
2166
|
+
class Z extends D {
|
|
2167
|
+
constructor(e, t, s, i, r) {
|
|
2168
|
+
super(), this.execute = e, this.getSQL = t, this.dialect = i, this.mapBatchResult = r, this.config = { action: s };
|
|
2169
|
+
}
|
|
2170
|
+
static [f] = "SQLiteRaw";
|
|
2171
|
+
/** @internal */
|
|
2172
|
+
config;
|
|
2173
|
+
getQuery() {
|
|
2174
|
+
return { ...this.dialect.sqlToQuery(this.getSQL()), method: this.config.action };
|
|
2175
|
+
}
|
|
2176
|
+
mapResult(e, t) {
|
|
2177
|
+
return t ? this.mapBatchResult(e) : e;
|
|
2178
|
+
}
|
|
2179
|
+
_prepare() {
|
|
2180
|
+
return this;
|
|
2181
|
+
}
|
|
2182
|
+
/** @internal */
|
|
2183
|
+
isResponseInArrayMode() {
|
|
2184
|
+
return !1;
|
|
2185
|
+
}
|
|
2186
|
+
}
|
|
2187
|
+
class Ke {
|
|
2188
|
+
constructor(e, t, s, i) {
|
|
2189
|
+
this.resultKind = e, this.dialect = t, this.session = s, this._ = i ? {
|
|
2190
|
+
schema: i.schema,
|
|
2191
|
+
fullSchema: i.fullSchema,
|
|
2192
|
+
tableNamesMap: i.tableNamesMap
|
|
2193
|
+
} : {
|
|
2194
|
+
schema: void 0,
|
|
2195
|
+
fullSchema: {},
|
|
2196
|
+
tableNamesMap: {}
|
|
2197
|
+
}, this.query = {};
|
|
2198
|
+
const r = this.query;
|
|
2199
|
+
if (this._.schema)
|
|
2200
|
+
for (const [a, o] of Object.entries(this._.schema))
|
|
2201
|
+
r[a] = new Vt(
|
|
2202
|
+
e,
|
|
2203
|
+
i.fullSchema,
|
|
2204
|
+
this._.schema,
|
|
2205
|
+
this._.tableNamesMap,
|
|
2206
|
+
i.fullSchema[a],
|
|
2207
|
+
o,
|
|
2208
|
+
t,
|
|
2209
|
+
s
|
|
2210
|
+
);
|
|
2211
|
+
this.$cache = { invalidate: async (a) => {
|
|
2212
|
+
} };
|
|
2213
|
+
}
|
|
2214
|
+
static [f] = "BaseSQLiteDatabase";
|
|
2215
|
+
query;
|
|
2216
|
+
/**
|
|
2217
|
+
* Creates a subquery that defines a temporary named result set as a CTE.
|
|
2218
|
+
*
|
|
2219
|
+
* It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
|
|
2220
|
+
*
|
|
2221
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
|
|
2222
|
+
*
|
|
2223
|
+
* @param alias The alias for the subquery.
|
|
2224
|
+
*
|
|
2225
|
+
* Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
|
|
2226
|
+
*
|
|
2227
|
+
* @example
|
|
2228
|
+
*
|
|
2229
|
+
* ```ts
|
|
2230
|
+
* // Create a subquery with alias 'sq' and use it in the select query
|
|
2231
|
+
* const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
|
|
2232
|
+
*
|
|
2233
|
+
* const result = await db.with(sq).select().from(sq);
|
|
2234
|
+
* ```
|
|
2235
|
+
*
|
|
2236
|
+
* To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
|
|
2237
|
+
*
|
|
2238
|
+
* ```ts
|
|
2239
|
+
* // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
|
|
2240
|
+
* const sq = db.$with('sq').as(db.select({
|
|
2241
|
+
* name: sql<string>`upper(${users.name})`.as('name'),
|
|
2242
|
+
* })
|
|
2243
|
+
* .from(users));
|
|
2244
|
+
*
|
|
2245
|
+
* const result = await db.with(sq).select({ name: sq.name }).from(sq);
|
|
2246
|
+
* ```
|
|
2247
|
+
*/
|
|
2248
|
+
$with = (e, t) => {
|
|
2249
|
+
const s = this;
|
|
2250
|
+
return { as: (r) => (typeof r == "function" && (r = r(new Ie(s.dialect))), new Proxy(
|
|
2251
|
+
new Ae(
|
|
2252
|
+
r.getSQL(),
|
|
2253
|
+
t ?? ("getSelectedFields" in r ? r.getSelectedFields() ?? {} : {}),
|
|
2254
|
+
e,
|
|
2255
|
+
!0
|
|
2256
|
+
),
|
|
2257
|
+
new T({ alias: e, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
|
|
2258
|
+
)) };
|
|
2259
|
+
};
|
|
2260
|
+
$count(e, t) {
|
|
2261
|
+
return new re({ source: e, filters: t, session: this.session });
|
|
2262
|
+
}
|
|
2263
|
+
/**
|
|
2264
|
+
* Incorporates a previously defined CTE (using `$with`) into the main query.
|
|
2265
|
+
*
|
|
2266
|
+
* This method allows the main query to reference a temporary named result set.
|
|
2267
|
+
*
|
|
2268
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
|
|
2269
|
+
*
|
|
2270
|
+
* @param queries The CTEs to incorporate into the main query.
|
|
2271
|
+
*
|
|
2272
|
+
* @example
|
|
2273
|
+
*
|
|
2274
|
+
* ```ts
|
|
2275
|
+
* // Define a subquery 'sq' as a CTE using $with
|
|
2276
|
+
* const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
|
|
2277
|
+
*
|
|
2278
|
+
* // Incorporate the CTE 'sq' into the main query and select from it
|
|
2279
|
+
* const result = await db.with(sq).select().from(sq);
|
|
2280
|
+
* ```
|
|
2281
|
+
*/
|
|
2282
|
+
with(...e) {
|
|
2283
|
+
const t = this;
|
|
2284
|
+
function s(c) {
|
|
2285
|
+
return new I({
|
|
2286
|
+
fields: c ?? void 0,
|
|
2287
|
+
session: t.session,
|
|
2288
|
+
dialect: t.dialect,
|
|
2289
|
+
withList: e
|
|
2290
|
+
});
|
|
2291
|
+
}
|
|
2292
|
+
function i(c) {
|
|
2293
|
+
return new I({
|
|
2294
|
+
fields: c ?? void 0,
|
|
2295
|
+
session: t.session,
|
|
2296
|
+
dialect: t.dialect,
|
|
2297
|
+
withList: e,
|
|
2298
|
+
distinct: !0
|
|
2299
|
+
});
|
|
2300
|
+
}
|
|
2301
|
+
function r(c) {
|
|
2302
|
+
return new xe(c, t.session, t.dialect, e);
|
|
2303
|
+
}
|
|
2304
|
+
function a(c) {
|
|
2305
|
+
return new Te(c, t.session, t.dialect, e);
|
|
2306
|
+
}
|
|
2307
|
+
function o(c) {
|
|
2308
|
+
return new qe(c, t.session, t.dialect, e);
|
|
2309
|
+
}
|
|
2310
|
+
return { select: s, selectDistinct: i, update: r, insert: a, delete: o };
|
|
2311
|
+
}
|
|
2312
|
+
select(e) {
|
|
2313
|
+
return new I({ fields: e ?? void 0, session: this.session, dialect: this.dialect });
|
|
2314
|
+
}
|
|
2315
|
+
selectDistinct(e) {
|
|
2316
|
+
return new I({
|
|
2317
|
+
fields: e ?? void 0,
|
|
2318
|
+
session: this.session,
|
|
2319
|
+
dialect: this.dialect,
|
|
2320
|
+
distinct: !0
|
|
2321
|
+
});
|
|
2322
|
+
}
|
|
2323
|
+
/**
|
|
2324
|
+
* Creates an update query.
|
|
2325
|
+
*
|
|
2326
|
+
* Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.
|
|
2327
|
+
*
|
|
2328
|
+
* Use `.set()` method to specify which values to update.
|
|
2329
|
+
*
|
|
2330
|
+
* See docs: {@link https://orm.drizzle.team/docs/update}
|
|
2331
|
+
*
|
|
2332
|
+
* @param table The table to update.
|
|
2333
|
+
*
|
|
2334
|
+
* @example
|
|
2335
|
+
*
|
|
2336
|
+
* ```ts
|
|
2337
|
+
* // Update all rows in the 'cars' table
|
|
2338
|
+
* await db.update(cars).set({ color: 'red' });
|
|
2339
|
+
*
|
|
2340
|
+
* // Update rows with filters and conditions
|
|
2341
|
+
* await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));
|
|
2342
|
+
*
|
|
2343
|
+
* // Update with returning clause
|
|
2344
|
+
* const updatedCar: Car[] = await db.update(cars)
|
|
2345
|
+
* .set({ color: 'red' })
|
|
2346
|
+
* .where(eq(cars.id, 1))
|
|
2347
|
+
* .returning();
|
|
2348
|
+
* ```
|
|
2349
|
+
*/
|
|
2350
|
+
update(e) {
|
|
2351
|
+
return new xe(e, this.session, this.dialect);
|
|
2352
|
+
}
|
|
2353
|
+
$cache;
|
|
2354
|
+
/**
|
|
2355
|
+
* Creates an insert query.
|
|
2356
|
+
*
|
|
2357
|
+
* Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.
|
|
2358
|
+
*
|
|
2359
|
+
* See docs: {@link https://orm.drizzle.team/docs/insert}
|
|
2360
|
+
*
|
|
2361
|
+
* @param table The table to insert into.
|
|
2362
|
+
*
|
|
2363
|
+
* @example
|
|
2364
|
+
*
|
|
2365
|
+
* ```ts
|
|
2366
|
+
* // Insert one row
|
|
2367
|
+
* await db.insert(cars).values({ brand: 'BMW' });
|
|
2368
|
+
*
|
|
2369
|
+
* // Insert multiple rows
|
|
2370
|
+
* await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
|
|
2371
|
+
*
|
|
2372
|
+
* // Insert with returning clause
|
|
2373
|
+
* const insertedCar: Car[] = await db.insert(cars)
|
|
2374
|
+
* .values({ brand: 'BMW' })
|
|
2375
|
+
* .returning();
|
|
2376
|
+
* ```
|
|
2377
|
+
*/
|
|
2378
|
+
insert(e) {
|
|
2379
|
+
return new Te(e, this.session, this.dialect);
|
|
2380
|
+
}
|
|
2381
|
+
/**
|
|
2382
|
+
* Creates a delete query.
|
|
2383
|
+
*
|
|
2384
|
+
* Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.
|
|
2385
|
+
*
|
|
2386
|
+
* See docs: {@link https://orm.drizzle.team/docs/delete}
|
|
2387
|
+
*
|
|
2388
|
+
* @param table The table to delete from.
|
|
2389
|
+
*
|
|
2390
|
+
* @example
|
|
2391
|
+
*
|
|
2392
|
+
* ```ts
|
|
2393
|
+
* // Delete all rows in the 'cars' table
|
|
2394
|
+
* await db.delete(cars);
|
|
2395
|
+
*
|
|
2396
|
+
* // Delete rows with filters and conditions
|
|
2397
|
+
* await db.delete(cars).where(eq(cars.color, 'green'));
|
|
2398
|
+
*
|
|
2399
|
+
* // Delete with returning clause
|
|
2400
|
+
* const deletedCar: Car[] = await db.delete(cars)
|
|
2401
|
+
* .where(eq(cars.id, 1))
|
|
2402
|
+
* .returning();
|
|
2403
|
+
* ```
|
|
2404
|
+
*/
|
|
2405
|
+
delete(e) {
|
|
2406
|
+
return new qe(e, this.session, this.dialect);
|
|
2407
|
+
}
|
|
2408
|
+
run(e) {
|
|
2409
|
+
const t = typeof e == "string" ? n.raw(e) : e.getSQL();
|
|
2410
|
+
return this.resultKind === "async" ? new Z(
|
|
2411
|
+
async () => this.session.run(t),
|
|
2412
|
+
() => t,
|
|
2413
|
+
"run",
|
|
2414
|
+
this.dialect,
|
|
2415
|
+
this.session.extractRawRunValueFromBatchResult.bind(this.session)
|
|
2416
|
+
) : this.session.run(t);
|
|
2417
|
+
}
|
|
2418
|
+
all(e) {
|
|
2419
|
+
const t = typeof e == "string" ? n.raw(e) : e.getSQL();
|
|
2420
|
+
return this.resultKind === "async" ? new Z(
|
|
2421
|
+
async () => this.session.all(t),
|
|
2422
|
+
() => t,
|
|
2423
|
+
"all",
|
|
2424
|
+
this.dialect,
|
|
2425
|
+
this.session.extractRawAllValueFromBatchResult.bind(this.session)
|
|
2426
|
+
) : this.session.all(t);
|
|
2427
|
+
}
|
|
2428
|
+
get(e) {
|
|
2429
|
+
const t = typeof e == "string" ? n.raw(e) : e.getSQL();
|
|
2430
|
+
return this.resultKind === "async" ? new Z(
|
|
2431
|
+
async () => this.session.get(t),
|
|
2432
|
+
() => t,
|
|
2433
|
+
"get",
|
|
2434
|
+
this.dialect,
|
|
2435
|
+
this.session.extractRawGetValueFromBatchResult.bind(this.session)
|
|
2436
|
+
) : this.session.get(t);
|
|
2437
|
+
}
|
|
2438
|
+
values(e) {
|
|
2439
|
+
const t = typeof e == "string" ? n.raw(e) : e.getSQL();
|
|
2440
|
+
return this.resultKind === "async" ? new Z(
|
|
2441
|
+
async () => this.session.values(t),
|
|
2442
|
+
() => t,
|
|
2443
|
+
"values",
|
|
2444
|
+
this.dialect,
|
|
2445
|
+
this.session.extractRawValuesValueFromBatchResult.bind(this.session)
|
|
2446
|
+
) : this.session.values(t);
|
|
2447
|
+
}
|
|
2448
|
+
transaction(e, t) {
|
|
2449
|
+
return this.session.transaction(e, t);
|
|
2450
|
+
}
|
|
2451
|
+
}
|
|
2452
|
+
class Wt {
|
|
2453
|
+
static [f] = "Cache";
|
|
2454
|
+
}
|
|
2455
|
+
class ke extends Wt {
|
|
2456
|
+
strategy() {
|
|
2457
|
+
return "all";
|
|
2458
|
+
}
|
|
2459
|
+
static [f] = "NoopCache";
|
|
2460
|
+
async get(e) {
|
|
2461
|
+
}
|
|
2462
|
+
async put(e, t, s, i) {
|
|
2463
|
+
}
|
|
2464
|
+
async onMutate(e) {
|
|
2465
|
+
}
|
|
2466
|
+
}
|
|
2467
|
+
async function Be(l, e) {
|
|
2468
|
+
const t = `${l}-${JSON.stringify(e)}`, i = new TextEncoder().encode(t), r = await crypto.subtle.digest("SHA-256", i);
|
|
2469
|
+
return [...new Uint8Array(r)].map((c) => c.toString(16).padStart(2, "0")).join("");
|
|
2470
|
+
}
|
|
2471
|
+
class Ut extends D {
|
|
2472
|
+
constructor(e) {
|
|
2473
|
+
super(), this.resultCb = e;
|
|
2474
|
+
}
|
|
2475
|
+
static [f] = "ExecuteResultSync";
|
|
2476
|
+
async execute() {
|
|
2477
|
+
return this.resultCb();
|
|
2478
|
+
}
|
|
2479
|
+
sync() {
|
|
2480
|
+
return this.resultCb();
|
|
2481
|
+
}
|
|
2482
|
+
}
|
|
2483
|
+
class Ht {
|
|
2484
|
+
constructor(e, t, s, i, r, a) {
|
|
2485
|
+
this.mode = e, this.executeMethod = t, this.query = s, this.cache = i, this.queryMetadata = r, this.cacheConfig = a, i && i.strategy() === "all" && a === void 0 && (this.cacheConfig = { enable: !0, autoInvalidate: !0 }), this.cacheConfig?.enable || (this.cacheConfig = void 0);
|
|
2486
|
+
}
|
|
2487
|
+
static [f] = "PreparedQuery";
|
|
2488
|
+
/** @internal */
|
|
2489
|
+
joinsNotNullableMap;
|
|
2490
|
+
/** @internal */
|
|
2491
|
+
async queryWithCache(e, t, s) {
|
|
2492
|
+
if (this.cache === void 0 || h(this.cache, ke) || this.queryMetadata === void 0)
|
|
2493
|
+
try {
|
|
2494
|
+
return await s();
|
|
2495
|
+
} catch (i) {
|
|
2496
|
+
throw new F(e, t, i);
|
|
2497
|
+
}
|
|
2498
|
+
if (this.cacheConfig && !this.cacheConfig.enable)
|
|
2499
|
+
try {
|
|
2500
|
+
return await s();
|
|
2501
|
+
} catch (i) {
|
|
2502
|
+
throw new F(e, t, i);
|
|
2503
|
+
}
|
|
2504
|
+
if ((this.queryMetadata.type === "insert" || this.queryMetadata.type === "update" || this.queryMetadata.type === "delete") && this.queryMetadata.tables.length > 0)
|
|
2505
|
+
try {
|
|
2506
|
+
const [i] = await Promise.all([
|
|
2507
|
+
s(),
|
|
2508
|
+
this.cache.onMutate({ tables: this.queryMetadata.tables })
|
|
2509
|
+
]);
|
|
2510
|
+
return i;
|
|
2511
|
+
} catch (i) {
|
|
2512
|
+
throw new F(e, t, i);
|
|
2513
|
+
}
|
|
2514
|
+
if (!this.cacheConfig)
|
|
2515
|
+
try {
|
|
2516
|
+
return await s();
|
|
2517
|
+
} catch (i) {
|
|
2518
|
+
throw new F(e, t, i);
|
|
2519
|
+
}
|
|
2520
|
+
if (this.queryMetadata.type === "select") {
|
|
2521
|
+
const i = await this.cache.get(
|
|
2522
|
+
this.cacheConfig.tag ?? await Be(e, t),
|
|
2523
|
+
this.queryMetadata.tables,
|
|
2524
|
+
this.cacheConfig.tag !== void 0,
|
|
2525
|
+
this.cacheConfig.autoInvalidate
|
|
2526
|
+
);
|
|
2527
|
+
if (i === void 0) {
|
|
2528
|
+
let r;
|
|
2529
|
+
try {
|
|
2530
|
+
r = await s();
|
|
2531
|
+
} catch (a) {
|
|
2532
|
+
throw new F(e, t, a);
|
|
2533
|
+
}
|
|
2534
|
+
return await this.cache.put(
|
|
2535
|
+
this.cacheConfig.tag ?? await Be(e, t),
|
|
2536
|
+
r,
|
|
2537
|
+
// make sure we send tables that were used in a query only if user wants to invalidate it on each write
|
|
2538
|
+
this.cacheConfig.autoInvalidate ? this.queryMetadata.tables : [],
|
|
2539
|
+
this.cacheConfig.tag !== void 0,
|
|
2540
|
+
this.cacheConfig.config
|
|
2541
|
+
), r;
|
|
2542
|
+
}
|
|
2543
|
+
return i;
|
|
2544
|
+
}
|
|
2545
|
+
try {
|
|
2546
|
+
return await s();
|
|
2547
|
+
} catch (i) {
|
|
2548
|
+
throw new F(e, t, i);
|
|
2549
|
+
}
|
|
2550
|
+
}
|
|
2551
|
+
getQuery() {
|
|
2552
|
+
return this.query;
|
|
2553
|
+
}
|
|
2554
|
+
mapRunResult(e, t) {
|
|
2555
|
+
return e;
|
|
2556
|
+
}
|
|
2557
|
+
mapAllResult(e, t) {
|
|
2558
|
+
throw new Error("Not implemented");
|
|
2559
|
+
}
|
|
2560
|
+
mapGetResult(e, t) {
|
|
2561
|
+
throw new Error("Not implemented");
|
|
2562
|
+
}
|
|
2563
|
+
execute(e) {
|
|
2564
|
+
return this.mode === "async" ? this[this.executeMethod](e) : new Ut(() => this[this.executeMethod](e));
|
|
2565
|
+
}
|
|
2566
|
+
mapResult(e, t) {
|
|
2567
|
+
switch (this.executeMethod) {
|
|
2568
|
+
case "run":
|
|
2569
|
+
return this.mapRunResult(e, t);
|
|
2570
|
+
case "all":
|
|
2571
|
+
return this.mapAllResult(e, t);
|
|
2572
|
+
case "get":
|
|
2573
|
+
return this.mapGetResult(e, t);
|
|
2574
|
+
}
|
|
2575
|
+
}
|
|
2576
|
+
}
|
|
2577
|
+
class Yt {
|
|
2578
|
+
constructor(e) {
|
|
2579
|
+
this.dialect = e;
|
|
2580
|
+
}
|
|
2581
|
+
static [f] = "SQLiteSession";
|
|
2582
|
+
prepareOneTimeQuery(e, t, s, i, r, a, o) {
|
|
2583
|
+
return this.prepareQuery(
|
|
2584
|
+
e,
|
|
2585
|
+
t,
|
|
2586
|
+
s,
|
|
2587
|
+
i,
|
|
2588
|
+
r,
|
|
2589
|
+
a,
|
|
2590
|
+
o
|
|
2591
|
+
);
|
|
2592
|
+
}
|
|
2593
|
+
run(e) {
|
|
2594
|
+
const t = this.dialect.sqlToQuery(e);
|
|
2595
|
+
try {
|
|
2596
|
+
return this.prepareOneTimeQuery(t, void 0, "run", !1).run();
|
|
2597
|
+
} catch (s) {
|
|
2598
|
+
throw new we({ cause: s, message: `Failed to run the query '${t.sql}'` });
|
|
2599
|
+
}
|
|
2600
|
+
}
|
|
2601
|
+
/** @internal */
|
|
2602
|
+
extractRawRunValueFromBatchResult(e) {
|
|
2603
|
+
return e;
|
|
2604
|
+
}
|
|
2605
|
+
all(e) {
|
|
2606
|
+
return this.prepareOneTimeQuery(this.dialect.sqlToQuery(e), void 0, "run", !1).all();
|
|
2607
|
+
}
|
|
2608
|
+
/** @internal */
|
|
2609
|
+
extractRawAllValueFromBatchResult(e) {
|
|
2610
|
+
throw new Error("Not implemented");
|
|
2611
|
+
}
|
|
2612
|
+
get(e) {
|
|
2613
|
+
return this.prepareOneTimeQuery(this.dialect.sqlToQuery(e), void 0, "run", !1).get();
|
|
2614
|
+
}
|
|
2615
|
+
/** @internal */
|
|
2616
|
+
extractRawGetValueFromBatchResult(e) {
|
|
2617
|
+
throw new Error("Not implemented");
|
|
2618
|
+
}
|
|
2619
|
+
values(e) {
|
|
2620
|
+
return this.prepareOneTimeQuery(this.dialect.sqlToQuery(e), void 0, "run", !1).values();
|
|
2621
|
+
}
|
|
2622
|
+
async count(e) {
|
|
2623
|
+
return (await this.values(e))[0][0];
|
|
2624
|
+
}
|
|
2625
|
+
/** @internal */
|
|
2626
|
+
extractRawValuesValueFromBatchResult(e) {
|
|
2627
|
+
throw new Error("Not implemented");
|
|
2628
|
+
}
|
|
2629
|
+
}
|
|
2630
|
+
class Gt extends Ke {
|
|
2631
|
+
constructor(e, t, s, i, r = 0) {
|
|
2632
|
+
super(e, t, s, i), this.schema = i, this.nestedIndex = r;
|
|
2633
|
+
}
|
|
2634
|
+
static [f] = "SQLiteTransaction";
|
|
2635
|
+
rollback() {
|
|
2636
|
+
throw new Mt();
|
|
2637
|
+
}
|
|
2638
|
+
}
|
|
2639
|
+
class Zt extends Yt {
|
|
2640
|
+
constructor(e, t, s, i, r = {}) {
|
|
2641
|
+
super(t), this.client = e, this.schema = s, this.batchCLient = i, this.logger = r.logger ?? new St(), this.cache = r.cache ?? new ke();
|
|
2642
|
+
}
|
|
2643
|
+
static [f] = "SQLiteRemoteSession";
|
|
2644
|
+
logger;
|
|
2645
|
+
cache;
|
|
2646
|
+
prepareQuery(e, t, s, i, r, a, o) {
|
|
2647
|
+
return new Xt(
|
|
2648
|
+
this.client,
|
|
2649
|
+
e,
|
|
2650
|
+
this.logger,
|
|
2651
|
+
this.cache,
|
|
2652
|
+
a,
|
|
2653
|
+
o,
|
|
2654
|
+
t,
|
|
2655
|
+
s,
|
|
2656
|
+
i,
|
|
2657
|
+
r
|
|
2658
|
+
);
|
|
2659
|
+
}
|
|
2660
|
+
async batch(e) {
|
|
2661
|
+
const t = [], s = [];
|
|
2662
|
+
for (const r of e) {
|
|
2663
|
+
const a = r._prepare(), o = a.getQuery();
|
|
2664
|
+
t.push(a), s.push({ sql: o.sql, params: o.params, method: o.method });
|
|
2665
|
+
}
|
|
2666
|
+
return (await this.batchCLient(s)).map((r, a) => t[a].mapResult(r, !0));
|
|
2667
|
+
}
|
|
2668
|
+
async transaction(e, t) {
|
|
2669
|
+
const s = new ve("async", this.dialect, this, this.schema);
|
|
2670
|
+
await this.run(n.raw(`begin${t?.behavior ? " " + t.behavior : ""}`));
|
|
2671
|
+
try {
|
|
2672
|
+
const i = await e(s);
|
|
2673
|
+
return await this.run(n`commit`), i;
|
|
2674
|
+
} catch (i) {
|
|
2675
|
+
throw await this.run(n`rollback`), i;
|
|
2676
|
+
}
|
|
2677
|
+
}
|
|
2678
|
+
extractRawAllValueFromBatchResult(e) {
|
|
2679
|
+
return e.rows;
|
|
2680
|
+
}
|
|
2681
|
+
extractRawGetValueFromBatchResult(e) {
|
|
2682
|
+
return e.rows[0];
|
|
2683
|
+
}
|
|
2684
|
+
extractRawValuesValueFromBatchResult(e) {
|
|
2685
|
+
return e.rows;
|
|
2686
|
+
}
|
|
2687
|
+
}
|
|
2688
|
+
class ve extends Gt {
|
|
2689
|
+
static [f] = "SQLiteProxyTransaction";
|
|
2690
|
+
async transaction(e) {
|
|
2691
|
+
const t = `sp${this.nestedIndex}`, s = new ve("async", this.dialect, this.session, this.schema, this.nestedIndex + 1);
|
|
2692
|
+
await this.session.run(n.raw(`savepoint ${t}`));
|
|
2693
|
+
try {
|
|
2694
|
+
const i = await e(s);
|
|
2695
|
+
return await this.session.run(n.raw(`release savepoint ${t}`)), i;
|
|
2696
|
+
} catch (i) {
|
|
2697
|
+
throw await this.session.run(n.raw(`rollback to savepoint ${t}`)), i;
|
|
2698
|
+
}
|
|
2699
|
+
}
|
|
2700
|
+
}
|
|
2701
|
+
class Xt extends Ht {
|
|
2702
|
+
constructor(e, t, s, i, r, a, o, c, u, d) {
|
|
2703
|
+
super("async", c, t, i, r, a), this.client = e, this.logger = s, this.fields = o, this._isResponseInArrayMode = u, this.customResultMapper = d, this.customResultMapper = d, this.method = c;
|
|
2704
|
+
}
|
|
2705
|
+
static [f] = "SQLiteProxyPreparedQuery";
|
|
2706
|
+
method;
|
|
2707
|
+
getQuery() {
|
|
2708
|
+
return { ...this.query, method: this.method };
|
|
2709
|
+
}
|
|
2710
|
+
async run(e) {
|
|
2711
|
+
const t = G(this.query.params, e ?? {});
|
|
2712
|
+
return this.logger.logQuery(this.query.sql, t), await this.queryWithCache(this.query.sql, t, async () => await this.client(this.query.sql, t, "run"));
|
|
2713
|
+
}
|
|
2714
|
+
mapAllResult(e, t) {
|
|
2715
|
+
return t && (e = e.rows), !this.fields && !this.customResultMapper ? e : this.customResultMapper ? this.customResultMapper(e) : e.map((s) => $e(
|
|
2716
|
+
this.fields,
|
|
2717
|
+
s,
|
|
2718
|
+
this.joinsNotNullableMap
|
|
2719
|
+
));
|
|
2720
|
+
}
|
|
2721
|
+
async all(e) {
|
|
2722
|
+
const { query: t, logger: s, client: i } = this, r = G(t.params, e ?? {});
|
|
2723
|
+
s.logQuery(t.sql, r);
|
|
2724
|
+
const { rows: a } = await this.queryWithCache(t.sql, r, async () => await i(t.sql, r, "all"));
|
|
2725
|
+
return this.mapAllResult(a);
|
|
2726
|
+
}
|
|
2727
|
+
async get(e) {
|
|
2728
|
+
const { query: t, logger: s, client: i } = this, r = G(t.params, e ?? {});
|
|
2729
|
+
s.logQuery(t.sql, r);
|
|
2730
|
+
const a = await this.queryWithCache(t.sql, r, async () => await i(t.sql, r, "get"));
|
|
2731
|
+
return this.mapGetResult(a.rows);
|
|
2732
|
+
}
|
|
2733
|
+
mapGetResult(e, t) {
|
|
2734
|
+
t && (e = e.rows);
|
|
2735
|
+
const s = e;
|
|
2736
|
+
if (!this.fields && !this.customResultMapper)
|
|
2737
|
+
return s;
|
|
2738
|
+
if (s)
|
|
2739
|
+
return this.customResultMapper ? this.customResultMapper([e]) : $e(
|
|
2740
|
+
this.fields,
|
|
2741
|
+
s,
|
|
2742
|
+
this.joinsNotNullableMap
|
|
2743
|
+
);
|
|
2744
|
+
}
|
|
2745
|
+
async values(e) {
|
|
2746
|
+
const t = G(this.query.params, e ?? {});
|
|
2747
|
+
return this.logger.logQuery(this.query.sql, t), (await this.queryWithCache(this.query.sql, t, async () => await this.client(this.query.sql, t, "values"))).rows;
|
|
2748
|
+
}
|
|
2749
|
+
/** @internal */
|
|
2750
|
+
isResponseInArrayMode() {
|
|
2751
|
+
return this._isResponseInArrayMode;
|
|
2752
|
+
}
|
|
2753
|
+
}
|
|
2754
|
+
class es extends Ke {
|
|
2755
|
+
static [f] = "SqliteRemoteDatabase";
|
|
2756
|
+
async batch(e) {
|
|
2757
|
+
return this.session.batch(e);
|
|
2758
|
+
}
|
|
2759
|
+
}
|
|
2760
|
+
function ts(l, e, t) {
|
|
2761
|
+
const s = new Et({ casing: t?.casing });
|
|
2762
|
+
let i, r, a, o = {};
|
|
2763
|
+
e && (typeof e == "function" ? (a = e, o = {}) : (a = void 0, o = e), o.logger === !0 ? i = new wt() : o.logger !== !1 && (i = o.logger, r = o.cache));
|
|
2764
|
+
let c;
|
|
2765
|
+
if (o.schema) {
|
|
2766
|
+
const p = qt(
|
|
2767
|
+
o.schema,
|
|
2768
|
+
Rt
|
|
2769
|
+
);
|
|
2770
|
+
c = {
|
|
2771
|
+
fullSchema: o.schema,
|
|
2772
|
+
schema: p.tables,
|
|
2773
|
+
tableNamesMap: p.tableNamesMap
|
|
2774
|
+
};
|
|
2775
|
+
}
|
|
2776
|
+
const u = new Zt(l, s, c, a, { logger: i, cache: r }), d = new es("async", s, u, c);
|
|
2777
|
+
return d.$cache = r, d.$cache && (d.$cache.invalidate = r?.onMutate), d;
|
|
2778
|
+
}
|
|
2779
|
+
const ss = ge(X, ({ many: l }) => ({
|
|
2780
|
+
messages: l(J)
|
|
2781
|
+
})), is = ge(J, ({ one: l, many: e }) => ({
|
|
2782
|
+
chat: l(X, {
|
|
2783
|
+
fields: [J.aiChatId],
|
|
2784
|
+
references: [X.id]
|
|
2785
|
+
}),
|
|
2786
|
+
parts: e(ee)
|
|
2787
|
+
})), rs = ge(ee, ({ one: l }) => ({
|
|
2788
|
+
message: l(J, {
|
|
2789
|
+
fields: [ee.aiMessageId],
|
|
2790
|
+
references: [J.id]
|
|
2791
|
+
})
|
|
2792
|
+
})), ns = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
2793
|
+
__proto__: null,
|
|
2794
|
+
aiChats: X,
|
|
2795
|
+
aiChatsRelations: ss,
|
|
2796
|
+
aiMessageParts: ee,
|
|
2797
|
+
aiMessagePartsRelations: rs,
|
|
2798
|
+
aiMessages: J,
|
|
2799
|
+
aiMessagesRelations: is,
|
|
2800
|
+
history: Ve,
|
|
2801
|
+
stepContext: We
|
|
2802
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
2803
|
+
function as(l) {
|
|
2804
|
+
return l.map((e) => e === "" ? null : e);
|
|
2805
|
+
}
|
|
2806
|
+
class me {
|
|
2807
|
+
constructor(e) {
|
|
2808
|
+
this.dbPath = e, this.db = ts(
|
|
2809
|
+
async (t, s, i) => {
|
|
2810
|
+
try {
|
|
2811
|
+
const a = (await Qe("db_query", {
|
|
2812
|
+
params: { sql: t, params: s || [] }
|
|
2813
|
+
})).rows || [];
|
|
2814
|
+
if (a.length === 0 && i === "get")
|
|
2815
|
+
return {};
|
|
2816
|
+
const o = a.map(as);
|
|
2817
|
+
return { rows: i === "get" ? o[0] : o };
|
|
2818
|
+
} catch (r) {
|
|
2819
|
+
throw console.error("SQLite proxy error:", r), r;
|
|
2820
|
+
}
|
|
2821
|
+
},
|
|
2822
|
+
{ schema: ns }
|
|
2823
|
+
);
|
|
2824
|
+
}
|
|
2825
|
+
dbPath;
|
|
2826
|
+
static instances = /* @__PURE__ */ new Map();
|
|
2827
|
+
initPromise = null;
|
|
2828
|
+
db;
|
|
2829
|
+
/**
|
|
2830
|
+
* Get or create singleton instance for a database path
|
|
2831
|
+
* All entities with the same dbPath share this instance
|
|
2832
|
+
*/
|
|
2833
|
+
static getInstance(e) {
|
|
2834
|
+
let t = this.instances.get(e);
|
|
2835
|
+
return t || (t = new me(e), this.instances.set(e, t)), t;
|
|
2836
|
+
}
|
|
2837
|
+
/**
|
|
2838
|
+
* Initialize database connection
|
|
2839
|
+
* Handles concurrent initialization - only runs once even if called multiple times
|
|
2840
|
+
*/
|
|
2841
|
+
async initialize() {
|
|
2842
|
+
if (this.initPromise)
|
|
2843
|
+
return this.initPromise;
|
|
2844
|
+
this.initPromise = Qe("db_init", { dbPath: this.dbPath });
|
|
2845
|
+
try {
|
|
2846
|
+
await this.initPromise;
|
|
2847
|
+
} catch (e) {
|
|
2848
|
+
throw this.initPromise = null, e;
|
|
2849
|
+
}
|
|
2850
|
+
}
|
|
2851
|
+
async close() {
|
|
2852
|
+
me.instances.delete(this.dbPath);
|
|
2853
|
+
}
|
|
2854
|
+
}
|
|
2855
|
+
export {
|
|
2856
|
+
me as SqliteDBStorage
|
|
2857
|
+
};
|