@kapishdima/ai-ledger 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +137 -0
- package/dist/cli.js +4461 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3998 -0
- package/dist/lib/db.d.ts +352 -0
- package/dist/lib/db.d.ts.map +1 -0
- package/dist/lib/format.d.ts +15 -0
- package/dist/lib/format.d.ts.map +1 -0
- package/dist/lib/providers.d.ts +29 -0
- package/dist/lib/providers.d.ts.map +1 -0
- package/dist/plugin.js +3784 -0
- package/package.json +57 -0
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,3784 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// lib/db.ts
|
|
3
|
+
import { Database as Database2 } from "bun:sqlite";
|
|
4
|
+
|
|
5
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/bun-sqlite/driver.js
|
|
6
|
+
import { Database } from "bun:sqlite";
|
|
7
|
+
|
|
8
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/entity.js
|
|
9
|
+
var entityKind = Symbol.for("drizzle:entityKind");
|
|
10
|
+
var hasOwnEntityKind = Symbol.for("drizzle:hasOwnEntityKind");
|
|
11
|
+
function is(value, type) {
|
|
12
|
+
if (!value || typeof value !== "object") {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
if (value instanceof type) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
if (!Object.prototype.hasOwnProperty.call(type, entityKind)) {
|
|
19
|
+
throw new Error(`Class "${type.name ?? "<unknown>"}" doesn't look like a Drizzle entity. If this is incorrect and the class is provided by Drizzle, please report this as a bug.`);
|
|
20
|
+
}
|
|
21
|
+
let cls = Object.getPrototypeOf(value).constructor;
|
|
22
|
+
if (cls) {
|
|
23
|
+
while (cls) {
|
|
24
|
+
if (entityKind in cls && cls[entityKind] === type[entityKind]) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
cls = Object.getPrototypeOf(cls);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/logger.js
|
|
34
|
+
class ConsoleLogWriter {
|
|
35
|
+
static [entityKind] = "ConsoleLogWriter";
|
|
36
|
+
write(message) {
|
|
37
|
+
console.log(message);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
class DefaultLogger {
|
|
42
|
+
static [entityKind] = "DefaultLogger";
|
|
43
|
+
writer;
|
|
44
|
+
constructor(config) {
|
|
45
|
+
this.writer = config?.writer ?? new ConsoleLogWriter;
|
|
46
|
+
}
|
|
47
|
+
logQuery(query, params) {
|
|
48
|
+
const stringifiedParams = params.map((p) => {
|
|
49
|
+
try {
|
|
50
|
+
return JSON.stringify(p);
|
|
51
|
+
} catch {
|
|
52
|
+
return String(p);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
const paramsStr = stringifiedParams.length ? ` -- params: [${stringifiedParams.join(", ")}]` : "";
|
|
56
|
+
this.writer.write(`Query: ${query}${paramsStr}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
class NoopLogger {
|
|
61
|
+
static [entityKind] = "NoopLogger";
|
|
62
|
+
logQuery() {}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/table.utils.js
|
|
66
|
+
var TableName = Symbol.for("drizzle:Name");
|
|
67
|
+
|
|
68
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/table.js
|
|
69
|
+
var Schema = Symbol.for("drizzle:Schema");
|
|
70
|
+
var Columns = Symbol.for("drizzle:Columns");
|
|
71
|
+
var ExtraConfigColumns = Symbol.for("drizzle:ExtraConfigColumns");
|
|
72
|
+
var OriginalName = Symbol.for("drizzle:OriginalName");
|
|
73
|
+
var BaseName = Symbol.for("drizzle:BaseName");
|
|
74
|
+
var IsAlias = Symbol.for("drizzle:IsAlias");
|
|
75
|
+
var ExtraConfigBuilder = Symbol.for("drizzle:ExtraConfigBuilder");
|
|
76
|
+
var IsDrizzleTable = Symbol.for("drizzle:IsDrizzleTable");
|
|
77
|
+
|
|
78
|
+
class Table {
|
|
79
|
+
static [entityKind] = "Table";
|
|
80
|
+
static Symbol = {
|
|
81
|
+
Name: TableName,
|
|
82
|
+
Schema,
|
|
83
|
+
OriginalName,
|
|
84
|
+
Columns,
|
|
85
|
+
ExtraConfigColumns,
|
|
86
|
+
BaseName,
|
|
87
|
+
IsAlias,
|
|
88
|
+
ExtraConfigBuilder
|
|
89
|
+
};
|
|
90
|
+
[TableName];
|
|
91
|
+
[OriginalName];
|
|
92
|
+
[Schema];
|
|
93
|
+
[Columns];
|
|
94
|
+
[ExtraConfigColumns];
|
|
95
|
+
[BaseName];
|
|
96
|
+
[IsAlias] = false;
|
|
97
|
+
[IsDrizzleTable] = true;
|
|
98
|
+
[ExtraConfigBuilder] = undefined;
|
|
99
|
+
constructor(name, schema, baseName) {
|
|
100
|
+
this[TableName] = this[OriginalName] = name;
|
|
101
|
+
this[Schema] = schema;
|
|
102
|
+
this[BaseName] = baseName;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function getTableName(table) {
|
|
106
|
+
return table[TableName];
|
|
107
|
+
}
|
|
108
|
+
function getTableUniqueName(table) {
|
|
109
|
+
return `${table[Schema] ?? "public"}.${table[TableName]}`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/column.js
|
|
113
|
+
class Column {
|
|
114
|
+
constructor(table, config) {
|
|
115
|
+
this.table = table;
|
|
116
|
+
this.config = config;
|
|
117
|
+
this.name = config.name;
|
|
118
|
+
this.keyAsName = config.keyAsName;
|
|
119
|
+
this.notNull = config.notNull;
|
|
120
|
+
this.default = config.default;
|
|
121
|
+
this.defaultFn = config.defaultFn;
|
|
122
|
+
this.onUpdateFn = config.onUpdateFn;
|
|
123
|
+
this.hasDefault = config.hasDefault;
|
|
124
|
+
this.primary = config.primaryKey;
|
|
125
|
+
this.isUnique = config.isUnique;
|
|
126
|
+
this.uniqueName = config.uniqueName;
|
|
127
|
+
this.uniqueType = config.uniqueType;
|
|
128
|
+
this.dataType = config.dataType;
|
|
129
|
+
this.columnType = config.columnType;
|
|
130
|
+
this.generated = config.generated;
|
|
131
|
+
this.generatedIdentity = config.generatedIdentity;
|
|
132
|
+
}
|
|
133
|
+
static [entityKind] = "Column";
|
|
134
|
+
name;
|
|
135
|
+
keyAsName;
|
|
136
|
+
primary;
|
|
137
|
+
notNull;
|
|
138
|
+
default;
|
|
139
|
+
defaultFn;
|
|
140
|
+
onUpdateFn;
|
|
141
|
+
hasDefault;
|
|
142
|
+
isUnique;
|
|
143
|
+
uniqueName;
|
|
144
|
+
uniqueType;
|
|
145
|
+
dataType;
|
|
146
|
+
columnType;
|
|
147
|
+
enumValues = undefined;
|
|
148
|
+
generated = undefined;
|
|
149
|
+
generatedIdentity = undefined;
|
|
150
|
+
config;
|
|
151
|
+
mapFromDriverValue(value) {
|
|
152
|
+
return value;
|
|
153
|
+
}
|
|
154
|
+
mapToDriverValue(value) {
|
|
155
|
+
return value;
|
|
156
|
+
}
|
|
157
|
+
shouldDisableInsert() {
|
|
158
|
+
return this.config.generated !== undefined && this.config.generated.type !== "byDefault";
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/column-builder.js
|
|
163
|
+
class ColumnBuilder {
|
|
164
|
+
static [entityKind] = "ColumnBuilder";
|
|
165
|
+
config;
|
|
166
|
+
constructor(name, dataType, columnType) {
|
|
167
|
+
this.config = {
|
|
168
|
+
name,
|
|
169
|
+
keyAsName: name === "",
|
|
170
|
+
notNull: false,
|
|
171
|
+
default: undefined,
|
|
172
|
+
hasDefault: false,
|
|
173
|
+
primaryKey: false,
|
|
174
|
+
isUnique: false,
|
|
175
|
+
uniqueName: undefined,
|
|
176
|
+
uniqueType: undefined,
|
|
177
|
+
dataType,
|
|
178
|
+
columnType,
|
|
179
|
+
generated: undefined
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
$type() {
|
|
183
|
+
return this;
|
|
184
|
+
}
|
|
185
|
+
notNull() {
|
|
186
|
+
this.config.notNull = true;
|
|
187
|
+
return this;
|
|
188
|
+
}
|
|
189
|
+
default(value) {
|
|
190
|
+
this.config.default = value;
|
|
191
|
+
this.config.hasDefault = true;
|
|
192
|
+
return this;
|
|
193
|
+
}
|
|
194
|
+
$defaultFn(fn) {
|
|
195
|
+
this.config.defaultFn = fn;
|
|
196
|
+
this.config.hasDefault = true;
|
|
197
|
+
return this;
|
|
198
|
+
}
|
|
199
|
+
$default = this.$defaultFn;
|
|
200
|
+
$onUpdateFn(fn) {
|
|
201
|
+
this.config.onUpdateFn = fn;
|
|
202
|
+
this.config.hasDefault = true;
|
|
203
|
+
return this;
|
|
204
|
+
}
|
|
205
|
+
$onUpdate = this.$onUpdateFn;
|
|
206
|
+
primaryKey() {
|
|
207
|
+
this.config.primaryKey = true;
|
|
208
|
+
this.config.notNull = true;
|
|
209
|
+
return this;
|
|
210
|
+
}
|
|
211
|
+
setName(name) {
|
|
212
|
+
if (this.config.name !== "")
|
|
213
|
+
return;
|
|
214
|
+
this.config.name = name;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/tracing-utils.js
|
|
219
|
+
function iife(fn, ...args) {
|
|
220
|
+
return fn(...args);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/pg-core/unique-constraint.js
|
|
224
|
+
function uniqueKeyName(table, columns) {
|
|
225
|
+
return `${table[TableName]}_${columns.join("_")}_unique`;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/pg-core/columns/common.js
|
|
229
|
+
class PgColumn extends Column {
|
|
230
|
+
constructor(table, config) {
|
|
231
|
+
if (!config.uniqueName) {
|
|
232
|
+
config.uniqueName = uniqueKeyName(table, [config.name]);
|
|
233
|
+
}
|
|
234
|
+
super(table, config);
|
|
235
|
+
this.table = table;
|
|
236
|
+
}
|
|
237
|
+
static [entityKind] = "PgColumn";
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
class ExtraConfigColumn extends PgColumn {
|
|
241
|
+
static [entityKind] = "ExtraConfigColumn";
|
|
242
|
+
getSQLType() {
|
|
243
|
+
return this.getSQLType();
|
|
244
|
+
}
|
|
245
|
+
indexConfig = {
|
|
246
|
+
order: this.config.order ?? "asc",
|
|
247
|
+
nulls: this.config.nulls ?? "last",
|
|
248
|
+
opClass: this.config.opClass
|
|
249
|
+
};
|
|
250
|
+
defaultConfig = {
|
|
251
|
+
order: "asc",
|
|
252
|
+
nulls: "last",
|
|
253
|
+
opClass: undefined
|
|
254
|
+
};
|
|
255
|
+
asc() {
|
|
256
|
+
this.indexConfig.order = "asc";
|
|
257
|
+
return this;
|
|
258
|
+
}
|
|
259
|
+
desc() {
|
|
260
|
+
this.indexConfig.order = "desc";
|
|
261
|
+
return this;
|
|
262
|
+
}
|
|
263
|
+
nullsFirst() {
|
|
264
|
+
this.indexConfig.nulls = "first";
|
|
265
|
+
return this;
|
|
266
|
+
}
|
|
267
|
+
nullsLast() {
|
|
268
|
+
this.indexConfig.nulls = "last";
|
|
269
|
+
return this;
|
|
270
|
+
}
|
|
271
|
+
op(opClass) {
|
|
272
|
+
this.indexConfig.opClass = opClass;
|
|
273
|
+
return this;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/pg-core/columns/enum.js
|
|
278
|
+
var isPgEnumSym = Symbol.for("drizzle:isPgEnum");
|
|
279
|
+
function isPgEnum(obj) {
|
|
280
|
+
return !!obj && typeof obj === "function" && isPgEnumSym in obj && obj[isPgEnumSym] === true;
|
|
281
|
+
}
|
|
282
|
+
class PgEnumColumn extends PgColumn {
|
|
283
|
+
static [entityKind] = "PgEnumColumn";
|
|
284
|
+
enum = this.config.enum;
|
|
285
|
+
enumValues = this.config.enum.enumValues;
|
|
286
|
+
constructor(table, config) {
|
|
287
|
+
super(table, config);
|
|
288
|
+
this.enum = config.enum;
|
|
289
|
+
}
|
|
290
|
+
getSQLType() {
|
|
291
|
+
return this.enum.enumName;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/subquery.js
|
|
296
|
+
class Subquery {
|
|
297
|
+
static [entityKind] = "Subquery";
|
|
298
|
+
constructor(sql, selection, alias, isWith = false) {
|
|
299
|
+
this._ = {
|
|
300
|
+
brand: "Subquery",
|
|
301
|
+
sql,
|
|
302
|
+
selectedFields: selection,
|
|
303
|
+
alias,
|
|
304
|
+
isWith
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
class WithSubquery extends Subquery {
|
|
310
|
+
static [entityKind] = "WithSubquery";
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/version.js
|
|
314
|
+
var version = "0.38.4";
|
|
315
|
+
|
|
316
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/tracing.js
|
|
317
|
+
var otel;
|
|
318
|
+
var rawTracer;
|
|
319
|
+
var tracer = {
|
|
320
|
+
startActiveSpan(name, fn) {
|
|
321
|
+
if (!otel) {
|
|
322
|
+
return fn();
|
|
323
|
+
}
|
|
324
|
+
if (!rawTracer) {
|
|
325
|
+
rawTracer = otel.trace.getTracer("drizzle-orm", version);
|
|
326
|
+
}
|
|
327
|
+
return iife((otel2, rawTracer2) => rawTracer2.startActiveSpan(name, (span) => {
|
|
328
|
+
try {
|
|
329
|
+
return fn(span);
|
|
330
|
+
} catch (e) {
|
|
331
|
+
span.setStatus({
|
|
332
|
+
code: otel2.SpanStatusCode.ERROR,
|
|
333
|
+
message: e instanceof Error ? e.message : "Unknown error"
|
|
334
|
+
});
|
|
335
|
+
throw e;
|
|
336
|
+
} finally {
|
|
337
|
+
span.end();
|
|
338
|
+
}
|
|
339
|
+
}), otel, rawTracer);
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/view-common.js
|
|
344
|
+
var ViewBaseConfig = Symbol.for("drizzle:ViewBaseConfig");
|
|
345
|
+
|
|
346
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sql/sql.js
|
|
347
|
+
function isSQLWrapper(value) {
|
|
348
|
+
return value !== null && value !== undefined && typeof value.getSQL === "function";
|
|
349
|
+
}
|
|
350
|
+
function mergeQueries(queries) {
|
|
351
|
+
const result = { sql: "", params: [] };
|
|
352
|
+
for (const query of queries) {
|
|
353
|
+
result.sql += query.sql;
|
|
354
|
+
result.params.push(...query.params);
|
|
355
|
+
if (query.typings?.length) {
|
|
356
|
+
if (!result.typings) {
|
|
357
|
+
result.typings = [];
|
|
358
|
+
}
|
|
359
|
+
result.typings.push(...query.typings);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return result;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
class StringChunk {
|
|
366
|
+
static [entityKind] = "StringChunk";
|
|
367
|
+
value;
|
|
368
|
+
constructor(value) {
|
|
369
|
+
this.value = Array.isArray(value) ? value : [value];
|
|
370
|
+
}
|
|
371
|
+
getSQL() {
|
|
372
|
+
return new SQL([this]);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
class SQL {
|
|
377
|
+
constructor(queryChunks) {
|
|
378
|
+
this.queryChunks = queryChunks;
|
|
379
|
+
}
|
|
380
|
+
static [entityKind] = "SQL";
|
|
381
|
+
decoder = noopDecoder;
|
|
382
|
+
shouldInlineParams = false;
|
|
383
|
+
append(query) {
|
|
384
|
+
this.queryChunks.push(...query.queryChunks);
|
|
385
|
+
return this;
|
|
386
|
+
}
|
|
387
|
+
toQuery(config) {
|
|
388
|
+
return tracer.startActiveSpan("drizzle.buildSQL", (span) => {
|
|
389
|
+
const query = this.buildQueryFromSourceParams(this.queryChunks, config);
|
|
390
|
+
span?.setAttributes({
|
|
391
|
+
"drizzle.query.text": query.sql,
|
|
392
|
+
"drizzle.query.params": JSON.stringify(query.params)
|
|
393
|
+
});
|
|
394
|
+
return query;
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
buildQueryFromSourceParams(chunks, _config) {
|
|
398
|
+
const config = Object.assign({}, _config, {
|
|
399
|
+
inlineParams: _config.inlineParams || this.shouldInlineParams,
|
|
400
|
+
paramStartIndex: _config.paramStartIndex || { value: 0 }
|
|
401
|
+
});
|
|
402
|
+
const {
|
|
403
|
+
casing,
|
|
404
|
+
escapeName,
|
|
405
|
+
escapeParam,
|
|
406
|
+
prepareTyping,
|
|
407
|
+
inlineParams,
|
|
408
|
+
paramStartIndex
|
|
409
|
+
} = config;
|
|
410
|
+
return mergeQueries(chunks.map((chunk) => {
|
|
411
|
+
if (is(chunk, StringChunk)) {
|
|
412
|
+
return { sql: chunk.value.join(""), params: [] };
|
|
413
|
+
}
|
|
414
|
+
if (is(chunk, Name)) {
|
|
415
|
+
return { sql: escapeName(chunk.value), params: [] };
|
|
416
|
+
}
|
|
417
|
+
if (chunk === undefined) {
|
|
418
|
+
return { sql: "", params: [] };
|
|
419
|
+
}
|
|
420
|
+
if (Array.isArray(chunk)) {
|
|
421
|
+
const result = [new StringChunk("(")];
|
|
422
|
+
for (const [i, p] of chunk.entries()) {
|
|
423
|
+
result.push(p);
|
|
424
|
+
if (i < chunk.length - 1) {
|
|
425
|
+
result.push(new StringChunk(", "));
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
result.push(new StringChunk(")"));
|
|
429
|
+
return this.buildQueryFromSourceParams(result, config);
|
|
430
|
+
}
|
|
431
|
+
if (is(chunk, SQL)) {
|
|
432
|
+
return this.buildQueryFromSourceParams(chunk.queryChunks, {
|
|
433
|
+
...config,
|
|
434
|
+
inlineParams: inlineParams || chunk.shouldInlineParams
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
if (is(chunk, Table)) {
|
|
438
|
+
const schemaName = chunk[Table.Symbol.Schema];
|
|
439
|
+
const tableName = chunk[Table.Symbol.Name];
|
|
440
|
+
return {
|
|
441
|
+
sql: schemaName === undefined ? escapeName(tableName) : escapeName(schemaName) + "." + escapeName(tableName),
|
|
442
|
+
params: []
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
if (is(chunk, Column)) {
|
|
446
|
+
const columnName = casing.getColumnCasing(chunk);
|
|
447
|
+
if (_config.invokeSource === "indexes") {
|
|
448
|
+
return { sql: escapeName(columnName), params: [] };
|
|
449
|
+
}
|
|
450
|
+
const schemaName = chunk.table[Table.Symbol.Schema];
|
|
451
|
+
return {
|
|
452
|
+
sql: chunk.table[IsAlias] || schemaName === undefined ? escapeName(chunk.table[Table.Symbol.Name]) + "." + escapeName(columnName) : escapeName(schemaName) + "." + escapeName(chunk.table[Table.Symbol.Name]) + "." + escapeName(columnName),
|
|
453
|
+
params: []
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
if (is(chunk, View)) {
|
|
457
|
+
const schemaName = chunk[ViewBaseConfig].schema;
|
|
458
|
+
const viewName = chunk[ViewBaseConfig].name;
|
|
459
|
+
return {
|
|
460
|
+
sql: schemaName === undefined ? escapeName(viewName) : escapeName(schemaName) + "." + escapeName(viewName),
|
|
461
|
+
params: []
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
if (is(chunk, Param)) {
|
|
465
|
+
if (is(chunk.value, Placeholder)) {
|
|
466
|
+
return { sql: escapeParam(paramStartIndex.value++, chunk), params: [chunk], typings: ["none"] };
|
|
467
|
+
}
|
|
468
|
+
const mappedValue = chunk.value === null ? null : chunk.encoder.mapToDriverValue(chunk.value);
|
|
469
|
+
if (is(mappedValue, SQL)) {
|
|
470
|
+
return this.buildQueryFromSourceParams([mappedValue], config);
|
|
471
|
+
}
|
|
472
|
+
if (inlineParams) {
|
|
473
|
+
return { sql: this.mapInlineParam(mappedValue, config), params: [] };
|
|
474
|
+
}
|
|
475
|
+
let typings = ["none"];
|
|
476
|
+
if (prepareTyping) {
|
|
477
|
+
typings = [prepareTyping(chunk.encoder)];
|
|
478
|
+
}
|
|
479
|
+
return { sql: escapeParam(paramStartIndex.value++, mappedValue), params: [mappedValue], typings };
|
|
480
|
+
}
|
|
481
|
+
if (is(chunk, Placeholder)) {
|
|
482
|
+
return { sql: escapeParam(paramStartIndex.value++, chunk), params: [chunk], typings: ["none"] };
|
|
483
|
+
}
|
|
484
|
+
if (is(chunk, SQL.Aliased) && chunk.fieldAlias !== undefined) {
|
|
485
|
+
return { sql: escapeName(chunk.fieldAlias), params: [] };
|
|
486
|
+
}
|
|
487
|
+
if (is(chunk, Subquery)) {
|
|
488
|
+
if (chunk._.isWith) {
|
|
489
|
+
return { sql: escapeName(chunk._.alias), params: [] };
|
|
490
|
+
}
|
|
491
|
+
return this.buildQueryFromSourceParams([
|
|
492
|
+
new StringChunk("("),
|
|
493
|
+
chunk._.sql,
|
|
494
|
+
new StringChunk(") "),
|
|
495
|
+
new Name(chunk._.alias)
|
|
496
|
+
], config);
|
|
497
|
+
}
|
|
498
|
+
if (isPgEnum(chunk)) {
|
|
499
|
+
if (chunk.schema) {
|
|
500
|
+
return { sql: escapeName(chunk.schema) + "." + escapeName(chunk.enumName), params: [] };
|
|
501
|
+
}
|
|
502
|
+
return { sql: escapeName(chunk.enumName), params: [] };
|
|
503
|
+
}
|
|
504
|
+
if (isSQLWrapper(chunk)) {
|
|
505
|
+
if (chunk.shouldOmitSQLParens?.()) {
|
|
506
|
+
return this.buildQueryFromSourceParams([chunk.getSQL()], config);
|
|
507
|
+
}
|
|
508
|
+
return this.buildQueryFromSourceParams([
|
|
509
|
+
new StringChunk("("),
|
|
510
|
+
chunk.getSQL(),
|
|
511
|
+
new StringChunk(")")
|
|
512
|
+
], config);
|
|
513
|
+
}
|
|
514
|
+
if (inlineParams) {
|
|
515
|
+
return { sql: this.mapInlineParam(chunk, config), params: [] };
|
|
516
|
+
}
|
|
517
|
+
return { sql: escapeParam(paramStartIndex.value++, chunk), params: [chunk], typings: ["none"] };
|
|
518
|
+
}));
|
|
519
|
+
}
|
|
520
|
+
mapInlineParam(chunk, { escapeString }) {
|
|
521
|
+
if (chunk === null) {
|
|
522
|
+
return "null";
|
|
523
|
+
}
|
|
524
|
+
if (typeof chunk === "number" || typeof chunk === "boolean") {
|
|
525
|
+
return chunk.toString();
|
|
526
|
+
}
|
|
527
|
+
if (typeof chunk === "string") {
|
|
528
|
+
return escapeString(chunk);
|
|
529
|
+
}
|
|
530
|
+
if (typeof chunk === "object") {
|
|
531
|
+
const mappedValueAsString = chunk.toString();
|
|
532
|
+
if (mappedValueAsString === "[object Object]") {
|
|
533
|
+
return escapeString(JSON.stringify(chunk));
|
|
534
|
+
}
|
|
535
|
+
return escapeString(mappedValueAsString);
|
|
536
|
+
}
|
|
537
|
+
throw new Error("Unexpected param value: " + chunk);
|
|
538
|
+
}
|
|
539
|
+
getSQL() {
|
|
540
|
+
return this;
|
|
541
|
+
}
|
|
542
|
+
as(alias) {
|
|
543
|
+
if (alias === undefined) {
|
|
544
|
+
return this;
|
|
545
|
+
}
|
|
546
|
+
return new SQL.Aliased(this, alias);
|
|
547
|
+
}
|
|
548
|
+
mapWith(decoder) {
|
|
549
|
+
this.decoder = typeof decoder === "function" ? { mapFromDriverValue: decoder } : decoder;
|
|
550
|
+
return this;
|
|
551
|
+
}
|
|
552
|
+
inlineParams() {
|
|
553
|
+
this.shouldInlineParams = true;
|
|
554
|
+
return this;
|
|
555
|
+
}
|
|
556
|
+
if(condition) {
|
|
557
|
+
return condition ? this : undefined;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
class Name {
|
|
562
|
+
constructor(value) {
|
|
563
|
+
this.value = value;
|
|
564
|
+
}
|
|
565
|
+
static [entityKind] = "Name";
|
|
566
|
+
brand;
|
|
567
|
+
getSQL() {
|
|
568
|
+
return new SQL([this]);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
function isDriverValueEncoder(value) {
|
|
572
|
+
return typeof value === "object" && value !== null && "mapToDriverValue" in value && typeof value.mapToDriverValue === "function";
|
|
573
|
+
}
|
|
574
|
+
var noopDecoder = {
|
|
575
|
+
mapFromDriverValue: (value) => value
|
|
576
|
+
};
|
|
577
|
+
var noopEncoder = {
|
|
578
|
+
mapToDriverValue: (value) => value
|
|
579
|
+
};
|
|
580
|
+
var noopMapper = {
|
|
581
|
+
...noopDecoder,
|
|
582
|
+
...noopEncoder
|
|
583
|
+
};
|
|
584
|
+
|
|
585
|
+
class Param {
|
|
586
|
+
constructor(value, encoder = noopEncoder) {
|
|
587
|
+
this.value = value;
|
|
588
|
+
this.encoder = encoder;
|
|
589
|
+
}
|
|
590
|
+
static [entityKind] = "Param";
|
|
591
|
+
brand;
|
|
592
|
+
getSQL() {
|
|
593
|
+
return new SQL([this]);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
function sql(strings, ...params) {
|
|
597
|
+
const queryChunks = [];
|
|
598
|
+
if (params.length > 0 || strings.length > 0 && strings[0] !== "") {
|
|
599
|
+
queryChunks.push(new StringChunk(strings[0]));
|
|
600
|
+
}
|
|
601
|
+
for (const [paramIndex, param2] of params.entries()) {
|
|
602
|
+
queryChunks.push(param2, new StringChunk(strings[paramIndex + 1]));
|
|
603
|
+
}
|
|
604
|
+
return new SQL(queryChunks);
|
|
605
|
+
}
|
|
606
|
+
((sql2) => {
|
|
607
|
+
function empty() {
|
|
608
|
+
return new SQL([]);
|
|
609
|
+
}
|
|
610
|
+
sql2.empty = empty;
|
|
611
|
+
function fromList(list) {
|
|
612
|
+
return new SQL(list);
|
|
613
|
+
}
|
|
614
|
+
sql2.fromList = fromList;
|
|
615
|
+
function raw(str) {
|
|
616
|
+
return new SQL([new StringChunk(str)]);
|
|
617
|
+
}
|
|
618
|
+
sql2.raw = raw;
|
|
619
|
+
function join(chunks, separator) {
|
|
620
|
+
const result = [];
|
|
621
|
+
for (const [i, chunk] of chunks.entries()) {
|
|
622
|
+
if (i > 0 && separator !== undefined) {
|
|
623
|
+
result.push(separator);
|
|
624
|
+
}
|
|
625
|
+
result.push(chunk);
|
|
626
|
+
}
|
|
627
|
+
return new SQL(result);
|
|
628
|
+
}
|
|
629
|
+
sql2.join = join;
|
|
630
|
+
function identifier(value) {
|
|
631
|
+
return new Name(value);
|
|
632
|
+
}
|
|
633
|
+
sql2.identifier = identifier;
|
|
634
|
+
function placeholder2(name2) {
|
|
635
|
+
return new Placeholder(name2);
|
|
636
|
+
}
|
|
637
|
+
sql2.placeholder = placeholder2;
|
|
638
|
+
function param2(value, encoder) {
|
|
639
|
+
return new Param(value, encoder);
|
|
640
|
+
}
|
|
641
|
+
sql2.param = param2;
|
|
642
|
+
})(sql || (sql = {}));
|
|
643
|
+
((SQL2) => {
|
|
644
|
+
|
|
645
|
+
class Aliased {
|
|
646
|
+
constructor(sql2, fieldAlias) {
|
|
647
|
+
this.sql = sql2;
|
|
648
|
+
this.fieldAlias = fieldAlias;
|
|
649
|
+
}
|
|
650
|
+
static [entityKind] = "SQL.Aliased";
|
|
651
|
+
isSelectionField = false;
|
|
652
|
+
getSQL() {
|
|
653
|
+
return this.sql;
|
|
654
|
+
}
|
|
655
|
+
clone() {
|
|
656
|
+
return new Aliased(this.sql, this.fieldAlias);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
SQL2.Aliased = Aliased;
|
|
660
|
+
})(SQL || (SQL = {}));
|
|
661
|
+
|
|
662
|
+
class Placeholder {
|
|
663
|
+
constructor(name2) {
|
|
664
|
+
this.name = name2;
|
|
665
|
+
}
|
|
666
|
+
static [entityKind] = "Placeholder";
|
|
667
|
+
getSQL() {
|
|
668
|
+
return new SQL([this]);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
function fillPlaceholders(params, values) {
|
|
672
|
+
return params.map((p) => {
|
|
673
|
+
if (is(p, Placeholder)) {
|
|
674
|
+
if (!(p.name in values)) {
|
|
675
|
+
throw new Error(`No value for placeholder "${p.name}" was provided`);
|
|
676
|
+
}
|
|
677
|
+
return values[p.name];
|
|
678
|
+
}
|
|
679
|
+
if (is(p, Param) && is(p.value, Placeholder)) {
|
|
680
|
+
if (!(p.value.name in values)) {
|
|
681
|
+
throw new Error(`No value for placeholder "${p.value.name}" was provided`);
|
|
682
|
+
}
|
|
683
|
+
return p.encoder.mapToDriverValue(values[p.value.name]);
|
|
684
|
+
}
|
|
685
|
+
return p;
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
var IsDrizzleView = Symbol.for("drizzle:IsDrizzleView");
|
|
689
|
+
|
|
690
|
+
class View {
|
|
691
|
+
static [entityKind] = "View";
|
|
692
|
+
[ViewBaseConfig];
|
|
693
|
+
[IsDrizzleView] = true;
|
|
694
|
+
constructor({ name: name2, schema, selectedFields, query }) {
|
|
695
|
+
this[ViewBaseConfig] = {
|
|
696
|
+
name: name2,
|
|
697
|
+
originalName: name2,
|
|
698
|
+
schema,
|
|
699
|
+
selectedFields,
|
|
700
|
+
query,
|
|
701
|
+
isExisting: !query,
|
|
702
|
+
isAlias: false
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
getSQL() {
|
|
706
|
+
return new SQL([this]);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
Column.prototype.getSQL = function() {
|
|
710
|
+
return new SQL([this]);
|
|
711
|
+
};
|
|
712
|
+
Table.prototype.getSQL = function() {
|
|
713
|
+
return new SQL([this]);
|
|
714
|
+
};
|
|
715
|
+
Subquery.prototype.getSQL = function() {
|
|
716
|
+
return new SQL([this]);
|
|
717
|
+
};
|
|
718
|
+
|
|
719
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/utils.js
|
|
720
|
+
function mapResultRow(columns, row, joinsNotNullableMap) {
|
|
721
|
+
const nullifyMap = {};
|
|
722
|
+
const result = columns.reduce((result2, { path, field }, columnIndex) => {
|
|
723
|
+
let decoder;
|
|
724
|
+
if (is(field, Column)) {
|
|
725
|
+
decoder = field;
|
|
726
|
+
} else if (is(field, SQL)) {
|
|
727
|
+
decoder = field.decoder;
|
|
728
|
+
} else {
|
|
729
|
+
decoder = field.sql.decoder;
|
|
730
|
+
}
|
|
731
|
+
let node = result2;
|
|
732
|
+
for (const [pathChunkIndex, pathChunk] of path.entries()) {
|
|
733
|
+
if (pathChunkIndex < path.length - 1) {
|
|
734
|
+
if (!(pathChunk in node)) {
|
|
735
|
+
node[pathChunk] = {};
|
|
736
|
+
}
|
|
737
|
+
node = node[pathChunk];
|
|
738
|
+
} else {
|
|
739
|
+
const rawValue = row[columnIndex];
|
|
740
|
+
const value = node[pathChunk] = rawValue === null ? null : decoder.mapFromDriverValue(rawValue);
|
|
741
|
+
if (joinsNotNullableMap && is(field, Column) && path.length === 2) {
|
|
742
|
+
const objectName = path[0];
|
|
743
|
+
if (!(objectName in nullifyMap)) {
|
|
744
|
+
nullifyMap[objectName] = value === null ? getTableName(field.table) : false;
|
|
745
|
+
} else if (typeof nullifyMap[objectName] === "string" && nullifyMap[objectName] !== getTableName(field.table)) {
|
|
746
|
+
nullifyMap[objectName] = false;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
return result2;
|
|
752
|
+
}, {});
|
|
753
|
+
if (joinsNotNullableMap && Object.keys(nullifyMap).length > 0) {
|
|
754
|
+
for (const [objectName, tableName] of Object.entries(nullifyMap)) {
|
|
755
|
+
if (typeof tableName === "string" && !joinsNotNullableMap[tableName]) {
|
|
756
|
+
result[objectName] = null;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
return result;
|
|
761
|
+
}
|
|
762
|
+
function orderSelectedFields(fields, pathPrefix) {
|
|
763
|
+
return Object.entries(fields).reduce((result, [name, field]) => {
|
|
764
|
+
if (typeof name !== "string") {
|
|
765
|
+
return result;
|
|
766
|
+
}
|
|
767
|
+
const newPath = pathPrefix ? [...pathPrefix, name] : [name];
|
|
768
|
+
if (is(field, Column) || is(field, SQL) || is(field, SQL.Aliased)) {
|
|
769
|
+
result.push({ path: newPath, field });
|
|
770
|
+
} else if (is(field, Table)) {
|
|
771
|
+
result.push(...orderSelectedFields(field[Table.Symbol.Columns], newPath));
|
|
772
|
+
} else {
|
|
773
|
+
result.push(...orderSelectedFields(field, newPath));
|
|
774
|
+
}
|
|
775
|
+
return result;
|
|
776
|
+
}, []);
|
|
777
|
+
}
|
|
778
|
+
function haveSameKeys(left, right) {
|
|
779
|
+
const leftKeys = Object.keys(left);
|
|
780
|
+
const rightKeys = Object.keys(right);
|
|
781
|
+
if (leftKeys.length !== rightKeys.length) {
|
|
782
|
+
return false;
|
|
783
|
+
}
|
|
784
|
+
for (const [index, key] of leftKeys.entries()) {
|
|
785
|
+
if (key !== rightKeys[index]) {
|
|
786
|
+
return false;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
return true;
|
|
790
|
+
}
|
|
791
|
+
function mapUpdateSet(table, values) {
|
|
792
|
+
const entries = Object.entries(values).filter(([, value]) => value !== undefined).map(([key, value]) => {
|
|
793
|
+
if (is(value, SQL) || is(value, Column)) {
|
|
794
|
+
return [key, value];
|
|
795
|
+
} else {
|
|
796
|
+
return [key, new Param(value, table[Table.Symbol.Columns][key])];
|
|
797
|
+
}
|
|
798
|
+
});
|
|
799
|
+
if (entries.length === 0) {
|
|
800
|
+
throw new Error("No values to set");
|
|
801
|
+
}
|
|
802
|
+
return Object.fromEntries(entries);
|
|
803
|
+
}
|
|
804
|
+
function applyMixins(baseClass, extendedClasses) {
|
|
805
|
+
for (const extendedClass of extendedClasses) {
|
|
806
|
+
for (const name of Object.getOwnPropertyNames(extendedClass.prototype)) {
|
|
807
|
+
if (name === "constructor")
|
|
808
|
+
continue;
|
|
809
|
+
Object.defineProperty(baseClass.prototype, name, Object.getOwnPropertyDescriptor(extendedClass.prototype, name) || /* @__PURE__ */ Object.create(null));
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
function getTableColumns(table) {
|
|
814
|
+
return table[Table.Symbol.Columns];
|
|
815
|
+
}
|
|
816
|
+
function getTableLikeName(table) {
|
|
817
|
+
return is(table, Subquery) ? table._.alias : is(table, View) ? table[ViewBaseConfig].name : is(table, SQL) ? undefined : table[Table.Symbol.IsAlias] ? table[Table.Symbol.Name] : table[Table.Symbol.BaseName];
|
|
818
|
+
}
|
|
819
|
+
function getColumnNameAndConfig(a, b) {
|
|
820
|
+
return {
|
|
821
|
+
name: typeof a === "string" && a.length > 0 ? a : "",
|
|
822
|
+
config: typeof a === "object" ? a : b
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
function isConfig(data) {
|
|
826
|
+
if (typeof data !== "object" || data === null)
|
|
827
|
+
return false;
|
|
828
|
+
if (data.constructor.name !== "Object")
|
|
829
|
+
return false;
|
|
830
|
+
if ("logger" in data) {
|
|
831
|
+
const type = typeof data["logger"];
|
|
832
|
+
if (type !== "boolean" && (type !== "object" || typeof data["logger"]["logQuery"] !== "function") && type !== "undefined")
|
|
833
|
+
return false;
|
|
834
|
+
return true;
|
|
835
|
+
}
|
|
836
|
+
if ("schema" in data) {
|
|
837
|
+
const type = typeof data["logger"];
|
|
838
|
+
if (type !== "object" && type !== "undefined")
|
|
839
|
+
return false;
|
|
840
|
+
return true;
|
|
841
|
+
}
|
|
842
|
+
if ("casing" in data) {
|
|
843
|
+
const type = typeof data["logger"];
|
|
844
|
+
if (type !== "string" && type !== "undefined")
|
|
845
|
+
return false;
|
|
846
|
+
return true;
|
|
847
|
+
}
|
|
848
|
+
if ("mode" in data) {
|
|
849
|
+
if (data["mode"] !== "default" || data["mode"] !== "planetscale" || data["mode"] !== undefined)
|
|
850
|
+
return false;
|
|
851
|
+
return true;
|
|
852
|
+
}
|
|
853
|
+
if ("connection" in data) {
|
|
854
|
+
const type = typeof data["connection"];
|
|
855
|
+
if (type !== "string" && type !== "object" && type !== "undefined")
|
|
856
|
+
return false;
|
|
857
|
+
return true;
|
|
858
|
+
}
|
|
859
|
+
if ("client" in data) {
|
|
860
|
+
const type = typeof data["client"];
|
|
861
|
+
if (type !== "object" && type !== "function" && type !== "undefined")
|
|
862
|
+
return false;
|
|
863
|
+
return true;
|
|
864
|
+
}
|
|
865
|
+
if (Object.keys(data).length === 0)
|
|
866
|
+
return true;
|
|
867
|
+
return false;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/pg-core/table.js
|
|
871
|
+
var InlineForeignKeys = Symbol.for("drizzle:PgInlineForeignKeys");
|
|
872
|
+
var EnableRLS = Symbol.for("drizzle:EnableRLS");
|
|
873
|
+
|
|
874
|
+
class PgTable extends Table {
|
|
875
|
+
static [entityKind] = "PgTable";
|
|
876
|
+
static Symbol = Object.assign({}, Table.Symbol, {
|
|
877
|
+
InlineForeignKeys,
|
|
878
|
+
EnableRLS
|
|
879
|
+
});
|
|
880
|
+
[InlineForeignKeys] = [];
|
|
881
|
+
[EnableRLS] = false;
|
|
882
|
+
[Table.Symbol.ExtraConfigBuilder] = undefined;
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/pg-core/primary-keys.js
|
|
886
|
+
class PrimaryKeyBuilder {
|
|
887
|
+
static [entityKind] = "PgPrimaryKeyBuilder";
|
|
888
|
+
columns;
|
|
889
|
+
name;
|
|
890
|
+
constructor(columns, name) {
|
|
891
|
+
this.columns = columns;
|
|
892
|
+
this.name = name;
|
|
893
|
+
}
|
|
894
|
+
build(table) {
|
|
895
|
+
return new PrimaryKey(table, this.columns, this.name);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
class PrimaryKey {
|
|
900
|
+
constructor(table, columns, name) {
|
|
901
|
+
this.table = table;
|
|
902
|
+
this.columns = columns;
|
|
903
|
+
this.name = name;
|
|
904
|
+
}
|
|
905
|
+
static [entityKind] = "PgPrimaryKey";
|
|
906
|
+
columns;
|
|
907
|
+
name;
|
|
908
|
+
getName() {
|
|
909
|
+
return this.name ?? `${this.table[PgTable.Symbol.Name]}_${this.columns.map((column) => column.name).join("_")}_pk`;
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sql/expressions/conditions.js
|
|
914
|
+
function bindIfParam(value, column) {
|
|
915
|
+
if (isDriverValueEncoder(column) && !isSQLWrapper(value) && !is(value, Param) && !is(value, Placeholder) && !is(value, Column) && !is(value, Table) && !is(value, View)) {
|
|
916
|
+
return new Param(value, column);
|
|
917
|
+
}
|
|
918
|
+
return value;
|
|
919
|
+
}
|
|
920
|
+
var eq = (left, right) => {
|
|
921
|
+
return sql`${left} = ${bindIfParam(right, left)}`;
|
|
922
|
+
};
|
|
923
|
+
var ne = (left, right) => {
|
|
924
|
+
return sql`${left} <> ${bindIfParam(right, left)}`;
|
|
925
|
+
};
|
|
926
|
+
function and(...unfilteredConditions) {
|
|
927
|
+
const conditions = unfilteredConditions.filter((c) => c !== undefined);
|
|
928
|
+
if (conditions.length === 0) {
|
|
929
|
+
return;
|
|
930
|
+
}
|
|
931
|
+
if (conditions.length === 1) {
|
|
932
|
+
return new SQL(conditions);
|
|
933
|
+
}
|
|
934
|
+
return new SQL([
|
|
935
|
+
new StringChunk("("),
|
|
936
|
+
sql.join(conditions, new StringChunk(" and ")),
|
|
937
|
+
new StringChunk(")")
|
|
938
|
+
]);
|
|
939
|
+
}
|
|
940
|
+
function or(...unfilteredConditions) {
|
|
941
|
+
const conditions = unfilteredConditions.filter((c) => c !== undefined);
|
|
942
|
+
if (conditions.length === 0) {
|
|
943
|
+
return;
|
|
944
|
+
}
|
|
945
|
+
if (conditions.length === 1) {
|
|
946
|
+
return new SQL(conditions);
|
|
947
|
+
}
|
|
948
|
+
return new SQL([
|
|
949
|
+
new StringChunk("("),
|
|
950
|
+
sql.join(conditions, new StringChunk(" or ")),
|
|
951
|
+
new StringChunk(")")
|
|
952
|
+
]);
|
|
953
|
+
}
|
|
954
|
+
function not(condition) {
|
|
955
|
+
return sql`not ${condition}`;
|
|
956
|
+
}
|
|
957
|
+
var gt = (left, right) => {
|
|
958
|
+
return sql`${left} > ${bindIfParam(right, left)}`;
|
|
959
|
+
};
|
|
960
|
+
var gte = (left, right) => {
|
|
961
|
+
return sql`${left} >= ${bindIfParam(right, left)}`;
|
|
962
|
+
};
|
|
963
|
+
var lt = (left, right) => {
|
|
964
|
+
return sql`${left} < ${bindIfParam(right, left)}`;
|
|
965
|
+
};
|
|
966
|
+
var lte = (left, right) => {
|
|
967
|
+
return sql`${left} <= ${bindIfParam(right, left)}`;
|
|
968
|
+
};
|
|
969
|
+
function inArray(column, values) {
|
|
970
|
+
if (Array.isArray(values)) {
|
|
971
|
+
if (values.length === 0) {
|
|
972
|
+
return sql`false`;
|
|
973
|
+
}
|
|
974
|
+
return sql`${column} in ${values.map((v) => bindIfParam(v, column))}`;
|
|
975
|
+
}
|
|
976
|
+
return sql`${column} in ${bindIfParam(values, column)}`;
|
|
977
|
+
}
|
|
978
|
+
function notInArray(column, values) {
|
|
979
|
+
if (Array.isArray(values)) {
|
|
980
|
+
if (values.length === 0) {
|
|
981
|
+
return sql`true`;
|
|
982
|
+
}
|
|
983
|
+
return sql`${column} not in ${values.map((v) => bindIfParam(v, column))}`;
|
|
984
|
+
}
|
|
985
|
+
return sql`${column} not in ${bindIfParam(values, column)}`;
|
|
986
|
+
}
|
|
987
|
+
function isNull(value) {
|
|
988
|
+
return sql`${value} is null`;
|
|
989
|
+
}
|
|
990
|
+
function isNotNull(value) {
|
|
991
|
+
return sql`${value} is not null`;
|
|
992
|
+
}
|
|
993
|
+
function exists(subquery) {
|
|
994
|
+
return sql`exists ${subquery}`;
|
|
995
|
+
}
|
|
996
|
+
function notExists(subquery) {
|
|
997
|
+
return sql`not exists ${subquery}`;
|
|
998
|
+
}
|
|
999
|
+
function between(column, min, max) {
|
|
1000
|
+
return sql`${column} between ${bindIfParam(min, column)} and ${bindIfParam(max, column)}`;
|
|
1001
|
+
}
|
|
1002
|
+
function notBetween(column, min, max) {
|
|
1003
|
+
return sql`${column} not between ${bindIfParam(min, column)} and ${bindIfParam(max, column)}`;
|
|
1004
|
+
}
|
|
1005
|
+
function like(column, value) {
|
|
1006
|
+
return sql`${column} like ${value}`;
|
|
1007
|
+
}
|
|
1008
|
+
function notLike(column, value) {
|
|
1009
|
+
return sql`${column} not like ${value}`;
|
|
1010
|
+
}
|
|
1011
|
+
function ilike(column, value) {
|
|
1012
|
+
return sql`${column} ilike ${value}`;
|
|
1013
|
+
}
|
|
1014
|
+
function notIlike(column, value) {
|
|
1015
|
+
return sql`${column} not ilike ${value}`;
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sql/expressions/select.js
|
|
1019
|
+
function asc(column) {
|
|
1020
|
+
return sql`${column} asc`;
|
|
1021
|
+
}
|
|
1022
|
+
function desc(column) {
|
|
1023
|
+
return sql`${column} desc`;
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/relations.js
|
|
1027
|
+
class Relation {
|
|
1028
|
+
constructor(sourceTable, referencedTable, relationName) {
|
|
1029
|
+
this.sourceTable = sourceTable;
|
|
1030
|
+
this.referencedTable = referencedTable;
|
|
1031
|
+
this.relationName = relationName;
|
|
1032
|
+
this.referencedTableName = referencedTable[Table.Symbol.Name];
|
|
1033
|
+
}
|
|
1034
|
+
static [entityKind] = "Relation";
|
|
1035
|
+
referencedTableName;
|
|
1036
|
+
fieldName;
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
class Relations {
|
|
1040
|
+
constructor(table, config) {
|
|
1041
|
+
this.table = table;
|
|
1042
|
+
this.config = config;
|
|
1043
|
+
}
|
|
1044
|
+
static [entityKind] = "Relations";
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
class One extends Relation {
|
|
1048
|
+
constructor(sourceTable, referencedTable, config, isNullable) {
|
|
1049
|
+
super(sourceTable, referencedTable, config?.relationName);
|
|
1050
|
+
this.config = config;
|
|
1051
|
+
this.isNullable = isNullable;
|
|
1052
|
+
}
|
|
1053
|
+
static [entityKind] = "One";
|
|
1054
|
+
withFieldName(fieldName) {
|
|
1055
|
+
const relation = new One(this.sourceTable, this.referencedTable, this.config, this.isNullable);
|
|
1056
|
+
relation.fieldName = fieldName;
|
|
1057
|
+
return relation;
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
class Many extends Relation {
|
|
1062
|
+
constructor(sourceTable, referencedTable, config) {
|
|
1063
|
+
super(sourceTable, referencedTable, config?.relationName);
|
|
1064
|
+
this.config = config;
|
|
1065
|
+
}
|
|
1066
|
+
static [entityKind] = "Many";
|
|
1067
|
+
withFieldName(fieldName) {
|
|
1068
|
+
const relation = new Many(this.sourceTable, this.referencedTable, this.config);
|
|
1069
|
+
relation.fieldName = fieldName;
|
|
1070
|
+
return relation;
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
function getOperators() {
|
|
1074
|
+
return {
|
|
1075
|
+
and,
|
|
1076
|
+
between,
|
|
1077
|
+
eq,
|
|
1078
|
+
exists,
|
|
1079
|
+
gt,
|
|
1080
|
+
gte,
|
|
1081
|
+
ilike,
|
|
1082
|
+
inArray,
|
|
1083
|
+
isNull,
|
|
1084
|
+
isNotNull,
|
|
1085
|
+
like,
|
|
1086
|
+
lt,
|
|
1087
|
+
lte,
|
|
1088
|
+
ne,
|
|
1089
|
+
not,
|
|
1090
|
+
notBetween,
|
|
1091
|
+
notExists,
|
|
1092
|
+
notLike,
|
|
1093
|
+
notIlike,
|
|
1094
|
+
notInArray,
|
|
1095
|
+
or,
|
|
1096
|
+
sql
|
|
1097
|
+
};
|
|
1098
|
+
}
|
|
1099
|
+
function getOrderByOperators() {
|
|
1100
|
+
return {
|
|
1101
|
+
sql,
|
|
1102
|
+
asc,
|
|
1103
|
+
desc
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
1106
|
+
function extractTablesRelationalConfig(schema, configHelpers) {
|
|
1107
|
+
if (Object.keys(schema).length === 1 && "default" in schema && !is(schema["default"], Table)) {
|
|
1108
|
+
schema = schema["default"];
|
|
1109
|
+
}
|
|
1110
|
+
const tableNamesMap = {};
|
|
1111
|
+
const relationsBuffer = {};
|
|
1112
|
+
const tablesConfig = {};
|
|
1113
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
1114
|
+
if (is(value, Table)) {
|
|
1115
|
+
const dbName = getTableUniqueName(value);
|
|
1116
|
+
const bufferedRelations = relationsBuffer[dbName];
|
|
1117
|
+
tableNamesMap[dbName] = key;
|
|
1118
|
+
tablesConfig[key] = {
|
|
1119
|
+
tsName: key,
|
|
1120
|
+
dbName: value[Table.Symbol.Name],
|
|
1121
|
+
schema: value[Table.Symbol.Schema],
|
|
1122
|
+
columns: value[Table.Symbol.Columns],
|
|
1123
|
+
relations: bufferedRelations?.relations ?? {},
|
|
1124
|
+
primaryKey: bufferedRelations?.primaryKey ?? []
|
|
1125
|
+
};
|
|
1126
|
+
for (const column of Object.values(value[Table.Symbol.Columns])) {
|
|
1127
|
+
if (column.primary) {
|
|
1128
|
+
tablesConfig[key].primaryKey.push(column);
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
const extraConfig = value[Table.Symbol.ExtraConfigBuilder]?.(value[Table.Symbol.ExtraConfigColumns]);
|
|
1132
|
+
if (extraConfig) {
|
|
1133
|
+
for (const configEntry of Object.values(extraConfig)) {
|
|
1134
|
+
if (is(configEntry, PrimaryKeyBuilder)) {
|
|
1135
|
+
tablesConfig[key].primaryKey.push(...configEntry.columns);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
} else if (is(value, Relations)) {
|
|
1140
|
+
const dbName = getTableUniqueName(value.table);
|
|
1141
|
+
const tableName = tableNamesMap[dbName];
|
|
1142
|
+
const relations2 = value.config(configHelpers(value.table));
|
|
1143
|
+
let primaryKey;
|
|
1144
|
+
for (const [relationName, relation] of Object.entries(relations2)) {
|
|
1145
|
+
if (tableName) {
|
|
1146
|
+
const tableConfig = tablesConfig[tableName];
|
|
1147
|
+
tableConfig.relations[relationName] = relation;
|
|
1148
|
+
if (primaryKey) {
|
|
1149
|
+
tableConfig.primaryKey.push(...primaryKey);
|
|
1150
|
+
}
|
|
1151
|
+
} else {
|
|
1152
|
+
if (!(dbName in relationsBuffer)) {
|
|
1153
|
+
relationsBuffer[dbName] = {
|
|
1154
|
+
relations: {},
|
|
1155
|
+
primaryKey
|
|
1156
|
+
};
|
|
1157
|
+
}
|
|
1158
|
+
relationsBuffer[dbName].relations[relationName] = relation;
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
return { tables: tablesConfig, tableNamesMap };
|
|
1164
|
+
}
|
|
1165
|
+
function createOne(sourceTable) {
|
|
1166
|
+
return function one(table, config) {
|
|
1167
|
+
return new One(sourceTable, table, config, config?.fields.reduce((res, f) => res && f.notNull, true) ?? false);
|
|
1168
|
+
};
|
|
1169
|
+
}
|
|
1170
|
+
function createMany(sourceTable) {
|
|
1171
|
+
return function many(referencedTable, config) {
|
|
1172
|
+
return new Many(sourceTable, referencedTable, config);
|
|
1173
|
+
};
|
|
1174
|
+
}
|
|
1175
|
+
function normalizeRelation(schema, tableNamesMap, relation) {
|
|
1176
|
+
if (is(relation, One) && relation.config) {
|
|
1177
|
+
return {
|
|
1178
|
+
fields: relation.config.fields,
|
|
1179
|
+
references: relation.config.references
|
|
1180
|
+
};
|
|
1181
|
+
}
|
|
1182
|
+
const referencedTableTsName = tableNamesMap[getTableUniqueName(relation.referencedTable)];
|
|
1183
|
+
if (!referencedTableTsName) {
|
|
1184
|
+
throw new Error(`Table "${relation.referencedTable[Table.Symbol.Name]}" not found in schema`);
|
|
1185
|
+
}
|
|
1186
|
+
const referencedTableConfig = schema[referencedTableTsName];
|
|
1187
|
+
if (!referencedTableConfig) {
|
|
1188
|
+
throw new Error(`Table "${referencedTableTsName}" not found in schema`);
|
|
1189
|
+
}
|
|
1190
|
+
const sourceTable = relation.sourceTable;
|
|
1191
|
+
const sourceTableTsName = tableNamesMap[getTableUniqueName(sourceTable)];
|
|
1192
|
+
if (!sourceTableTsName) {
|
|
1193
|
+
throw new Error(`Table "${sourceTable[Table.Symbol.Name]}" not found in schema`);
|
|
1194
|
+
}
|
|
1195
|
+
const reverseRelations = [];
|
|
1196
|
+
for (const referencedTableRelation of Object.values(referencedTableConfig.relations)) {
|
|
1197
|
+
if (relation.relationName && relation !== referencedTableRelation && referencedTableRelation.relationName === relation.relationName || !relation.relationName && referencedTableRelation.referencedTable === relation.sourceTable) {
|
|
1198
|
+
reverseRelations.push(referencedTableRelation);
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
if (reverseRelations.length > 1) {
|
|
1202
|
+
throw relation.relationName ? new Error(`There are multiple relations with name "${relation.relationName}" in table "${referencedTableTsName}"`) : new Error(`There are multiple relations between "${referencedTableTsName}" and "${relation.sourceTable[Table.Symbol.Name]}". Please specify relation name`);
|
|
1203
|
+
}
|
|
1204
|
+
if (reverseRelations[0] && is(reverseRelations[0], One) && reverseRelations[0].config) {
|
|
1205
|
+
return {
|
|
1206
|
+
fields: reverseRelations[0].config.references,
|
|
1207
|
+
references: reverseRelations[0].config.fields
|
|
1208
|
+
};
|
|
1209
|
+
}
|
|
1210
|
+
throw new Error(`There is not enough information to infer relation "${sourceTableTsName}.${relation.fieldName}"`);
|
|
1211
|
+
}
|
|
1212
|
+
function createTableRelationsHelpers(sourceTable) {
|
|
1213
|
+
return {
|
|
1214
|
+
one: createOne(sourceTable),
|
|
1215
|
+
many: createMany(sourceTable)
|
|
1216
|
+
};
|
|
1217
|
+
}
|
|
1218
|
+
function mapRelationalRow(tablesConfig, tableConfig, row, buildQueryResultSelection, mapColumnValue = (value) => value) {
|
|
1219
|
+
const result = {};
|
|
1220
|
+
for (const [
|
|
1221
|
+
selectionItemIndex,
|
|
1222
|
+
selectionItem
|
|
1223
|
+
] of buildQueryResultSelection.entries()) {
|
|
1224
|
+
if (selectionItem.isJson) {
|
|
1225
|
+
const relation = tableConfig.relations[selectionItem.tsKey];
|
|
1226
|
+
const rawSubRows = row[selectionItemIndex];
|
|
1227
|
+
const subRows = typeof rawSubRows === "string" ? JSON.parse(rawSubRows) : rawSubRows;
|
|
1228
|
+
result[selectionItem.tsKey] = is(relation, One) ? subRows && mapRelationalRow(tablesConfig, tablesConfig[selectionItem.relationTableTsKey], subRows, selectionItem.selection, mapColumnValue) : subRows.map((subRow) => mapRelationalRow(tablesConfig, tablesConfig[selectionItem.relationTableTsKey], subRow, selectionItem.selection, mapColumnValue));
|
|
1229
|
+
} else {
|
|
1230
|
+
const value = mapColumnValue(row[selectionItemIndex]);
|
|
1231
|
+
const field = selectionItem.field;
|
|
1232
|
+
let decoder;
|
|
1233
|
+
if (is(field, Column)) {
|
|
1234
|
+
decoder = field;
|
|
1235
|
+
} else if (is(field, SQL)) {
|
|
1236
|
+
decoder = field.decoder;
|
|
1237
|
+
} else {
|
|
1238
|
+
decoder = field.sql.decoder;
|
|
1239
|
+
}
|
|
1240
|
+
result[selectionItem.tsKey] = value === null ? null : decoder.mapFromDriverValue(value);
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
return result;
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/alias.js
|
|
1247
|
+
class ColumnAliasProxyHandler {
|
|
1248
|
+
constructor(table) {
|
|
1249
|
+
this.table = table;
|
|
1250
|
+
}
|
|
1251
|
+
static [entityKind] = "ColumnAliasProxyHandler";
|
|
1252
|
+
get(columnObj, prop) {
|
|
1253
|
+
if (prop === "table") {
|
|
1254
|
+
return this.table;
|
|
1255
|
+
}
|
|
1256
|
+
return columnObj[prop];
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
class TableAliasProxyHandler {
|
|
1261
|
+
constructor(alias, replaceOriginalName) {
|
|
1262
|
+
this.alias = alias;
|
|
1263
|
+
this.replaceOriginalName = replaceOriginalName;
|
|
1264
|
+
}
|
|
1265
|
+
static [entityKind] = "TableAliasProxyHandler";
|
|
1266
|
+
get(target, prop) {
|
|
1267
|
+
if (prop === Table.Symbol.IsAlias) {
|
|
1268
|
+
return true;
|
|
1269
|
+
}
|
|
1270
|
+
if (prop === Table.Symbol.Name) {
|
|
1271
|
+
return this.alias;
|
|
1272
|
+
}
|
|
1273
|
+
if (this.replaceOriginalName && prop === Table.Symbol.OriginalName) {
|
|
1274
|
+
return this.alias;
|
|
1275
|
+
}
|
|
1276
|
+
if (prop === ViewBaseConfig) {
|
|
1277
|
+
return {
|
|
1278
|
+
...target[ViewBaseConfig],
|
|
1279
|
+
name: this.alias,
|
|
1280
|
+
isAlias: true
|
|
1281
|
+
};
|
|
1282
|
+
}
|
|
1283
|
+
if (prop === Table.Symbol.Columns) {
|
|
1284
|
+
const columns = target[Table.Symbol.Columns];
|
|
1285
|
+
if (!columns) {
|
|
1286
|
+
return columns;
|
|
1287
|
+
}
|
|
1288
|
+
const proxiedColumns = {};
|
|
1289
|
+
Object.keys(columns).map((key) => {
|
|
1290
|
+
proxiedColumns[key] = new Proxy(columns[key], new ColumnAliasProxyHandler(new Proxy(target, this)));
|
|
1291
|
+
});
|
|
1292
|
+
return proxiedColumns;
|
|
1293
|
+
}
|
|
1294
|
+
const value = target[prop];
|
|
1295
|
+
if (is(value, Column)) {
|
|
1296
|
+
return new Proxy(value, new ColumnAliasProxyHandler(new Proxy(target, this)));
|
|
1297
|
+
}
|
|
1298
|
+
return value;
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
function aliasedTable(table, tableAlias) {
|
|
1302
|
+
return new Proxy(table, new TableAliasProxyHandler(tableAlias, false));
|
|
1303
|
+
}
|
|
1304
|
+
function aliasedTableColumn(column, tableAlias) {
|
|
1305
|
+
return new Proxy(column, new ColumnAliasProxyHandler(new Proxy(column.table, new TableAliasProxyHandler(tableAlias, false))));
|
|
1306
|
+
}
|
|
1307
|
+
function mapColumnsInAliasedSQLToAlias(query, alias) {
|
|
1308
|
+
return new SQL.Aliased(mapColumnsInSQLToAlias(query.sql, alias), query.fieldAlias);
|
|
1309
|
+
}
|
|
1310
|
+
function mapColumnsInSQLToAlias(query, alias) {
|
|
1311
|
+
return sql.join(query.queryChunks.map((c) => {
|
|
1312
|
+
if (is(c, Column)) {
|
|
1313
|
+
return aliasedTableColumn(c, alias);
|
|
1314
|
+
}
|
|
1315
|
+
if (is(c, SQL)) {
|
|
1316
|
+
return mapColumnsInSQLToAlias(c, alias);
|
|
1317
|
+
}
|
|
1318
|
+
if (is(c, SQL.Aliased)) {
|
|
1319
|
+
return mapColumnsInAliasedSQLToAlias(c, alias);
|
|
1320
|
+
}
|
|
1321
|
+
return c;
|
|
1322
|
+
}));
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/selection-proxy.js
|
|
1326
|
+
class SelectionProxyHandler {
|
|
1327
|
+
static [entityKind] = "SelectionProxyHandler";
|
|
1328
|
+
config;
|
|
1329
|
+
constructor(config) {
|
|
1330
|
+
this.config = { ...config };
|
|
1331
|
+
}
|
|
1332
|
+
get(subquery, prop) {
|
|
1333
|
+
if (prop === "_") {
|
|
1334
|
+
return {
|
|
1335
|
+
...subquery["_"],
|
|
1336
|
+
selectedFields: new Proxy(subquery._.selectedFields, this)
|
|
1337
|
+
};
|
|
1338
|
+
}
|
|
1339
|
+
if (prop === ViewBaseConfig) {
|
|
1340
|
+
return {
|
|
1341
|
+
...subquery[ViewBaseConfig],
|
|
1342
|
+
selectedFields: new Proxy(subquery[ViewBaseConfig].selectedFields, this)
|
|
1343
|
+
};
|
|
1344
|
+
}
|
|
1345
|
+
if (typeof prop === "symbol") {
|
|
1346
|
+
return subquery[prop];
|
|
1347
|
+
}
|
|
1348
|
+
const columns = is(subquery, Subquery) ? subquery._.selectedFields : is(subquery, View) ? subquery[ViewBaseConfig].selectedFields : subquery;
|
|
1349
|
+
const value = columns[prop];
|
|
1350
|
+
if (is(value, SQL.Aliased)) {
|
|
1351
|
+
if (this.config.sqlAliasedBehavior === "sql" && !value.isSelectionField) {
|
|
1352
|
+
return value.sql;
|
|
1353
|
+
}
|
|
1354
|
+
const newValue = value.clone();
|
|
1355
|
+
newValue.isSelectionField = true;
|
|
1356
|
+
return newValue;
|
|
1357
|
+
}
|
|
1358
|
+
if (is(value, SQL)) {
|
|
1359
|
+
if (this.config.sqlBehavior === "sql") {
|
|
1360
|
+
return value;
|
|
1361
|
+
}
|
|
1362
|
+
throw new Error(`You tried to reference "${prop}" 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.`);
|
|
1363
|
+
}
|
|
1364
|
+
if (is(value, Column)) {
|
|
1365
|
+
if (this.config.alias) {
|
|
1366
|
+
return new Proxy(value, new ColumnAliasProxyHandler(new Proxy(value.table, new TableAliasProxyHandler(this.config.alias, this.config.replaceOriginalName ?? false))));
|
|
1367
|
+
}
|
|
1368
|
+
return value;
|
|
1369
|
+
}
|
|
1370
|
+
if (typeof value !== "object" || value === null) {
|
|
1371
|
+
return value;
|
|
1372
|
+
}
|
|
1373
|
+
return new Proxy(value, new SelectionProxyHandler(this.config));
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/query-promise.js
|
|
1378
|
+
class QueryPromise {
|
|
1379
|
+
static [entityKind] = "QueryPromise";
|
|
1380
|
+
[Symbol.toStringTag] = "QueryPromise";
|
|
1381
|
+
catch(onRejected) {
|
|
1382
|
+
return this.then(undefined, onRejected);
|
|
1383
|
+
}
|
|
1384
|
+
finally(onFinally) {
|
|
1385
|
+
return this.then((value) => {
|
|
1386
|
+
onFinally?.();
|
|
1387
|
+
return value;
|
|
1388
|
+
}, (reason) => {
|
|
1389
|
+
onFinally?.();
|
|
1390
|
+
throw reason;
|
|
1391
|
+
});
|
|
1392
|
+
}
|
|
1393
|
+
then(onFulfilled, onRejected) {
|
|
1394
|
+
return this.execute().then(onFulfilled, onRejected);
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/foreign-keys.js
|
|
1399
|
+
class ForeignKeyBuilder {
|
|
1400
|
+
static [entityKind] = "SQLiteForeignKeyBuilder";
|
|
1401
|
+
reference;
|
|
1402
|
+
_onUpdate;
|
|
1403
|
+
_onDelete;
|
|
1404
|
+
constructor(config, actions) {
|
|
1405
|
+
this.reference = () => {
|
|
1406
|
+
const { name, columns, foreignColumns } = config();
|
|
1407
|
+
return { name, columns, foreignTable: foreignColumns[0].table, foreignColumns };
|
|
1408
|
+
};
|
|
1409
|
+
if (actions) {
|
|
1410
|
+
this._onUpdate = actions.onUpdate;
|
|
1411
|
+
this._onDelete = actions.onDelete;
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
onUpdate(action) {
|
|
1415
|
+
this._onUpdate = action;
|
|
1416
|
+
return this;
|
|
1417
|
+
}
|
|
1418
|
+
onDelete(action) {
|
|
1419
|
+
this._onDelete = action;
|
|
1420
|
+
return this;
|
|
1421
|
+
}
|
|
1422
|
+
build(table) {
|
|
1423
|
+
return new ForeignKey(table, this);
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
class ForeignKey {
|
|
1428
|
+
constructor(table, builder) {
|
|
1429
|
+
this.table = table;
|
|
1430
|
+
this.reference = builder.reference;
|
|
1431
|
+
this.onUpdate = builder._onUpdate;
|
|
1432
|
+
this.onDelete = builder._onDelete;
|
|
1433
|
+
}
|
|
1434
|
+
static [entityKind] = "SQLiteForeignKey";
|
|
1435
|
+
reference;
|
|
1436
|
+
onUpdate;
|
|
1437
|
+
onDelete;
|
|
1438
|
+
getName() {
|
|
1439
|
+
const { name, columns, foreignColumns } = this.reference();
|
|
1440
|
+
const columnNames = columns.map((column) => column.name);
|
|
1441
|
+
const foreignColumnNames = foreignColumns.map((column) => column.name);
|
|
1442
|
+
const chunks = [
|
|
1443
|
+
this.table[TableName],
|
|
1444
|
+
...columnNames,
|
|
1445
|
+
foreignColumns[0].table[TableName],
|
|
1446
|
+
...foreignColumnNames
|
|
1447
|
+
];
|
|
1448
|
+
return name ?? `${chunks.join("_")}_fk`;
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/unique-constraint.js
|
|
1453
|
+
function uniqueKeyName2(table, columns) {
|
|
1454
|
+
return `${table[TableName]}_${columns.join("_")}_unique`;
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/columns/common.js
|
|
1458
|
+
class SQLiteColumnBuilder extends ColumnBuilder {
|
|
1459
|
+
static [entityKind] = "SQLiteColumnBuilder";
|
|
1460
|
+
foreignKeyConfigs = [];
|
|
1461
|
+
references(ref, actions = {}) {
|
|
1462
|
+
this.foreignKeyConfigs.push({ ref, actions });
|
|
1463
|
+
return this;
|
|
1464
|
+
}
|
|
1465
|
+
unique(name) {
|
|
1466
|
+
this.config.isUnique = true;
|
|
1467
|
+
this.config.uniqueName = name;
|
|
1468
|
+
return this;
|
|
1469
|
+
}
|
|
1470
|
+
generatedAlwaysAs(as, config) {
|
|
1471
|
+
this.config.generated = {
|
|
1472
|
+
as,
|
|
1473
|
+
type: "always",
|
|
1474
|
+
mode: config?.mode ?? "virtual"
|
|
1475
|
+
};
|
|
1476
|
+
return this;
|
|
1477
|
+
}
|
|
1478
|
+
buildForeignKeys(column, table) {
|
|
1479
|
+
return this.foreignKeyConfigs.map(({ ref, actions }) => {
|
|
1480
|
+
return ((ref2, actions2) => {
|
|
1481
|
+
const builder = new ForeignKeyBuilder(() => {
|
|
1482
|
+
const foreignColumn = ref2();
|
|
1483
|
+
return { columns: [column], foreignColumns: [foreignColumn] };
|
|
1484
|
+
});
|
|
1485
|
+
if (actions2.onUpdate) {
|
|
1486
|
+
builder.onUpdate(actions2.onUpdate);
|
|
1487
|
+
}
|
|
1488
|
+
if (actions2.onDelete) {
|
|
1489
|
+
builder.onDelete(actions2.onDelete);
|
|
1490
|
+
}
|
|
1491
|
+
return builder.build(table);
|
|
1492
|
+
})(ref, actions);
|
|
1493
|
+
});
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
class SQLiteColumn extends Column {
|
|
1498
|
+
constructor(table, config) {
|
|
1499
|
+
if (!config.uniqueName) {
|
|
1500
|
+
config.uniqueName = uniqueKeyName2(table, [config.name]);
|
|
1501
|
+
}
|
|
1502
|
+
super(table, config);
|
|
1503
|
+
this.table = table;
|
|
1504
|
+
}
|
|
1505
|
+
static [entityKind] = "SQLiteColumn";
|
|
1506
|
+
}
|
|
1507
|
+
|
|
1508
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/columns/blob.js
|
|
1509
|
+
class SQLiteBigIntBuilder extends SQLiteColumnBuilder {
|
|
1510
|
+
static [entityKind] = "SQLiteBigIntBuilder";
|
|
1511
|
+
constructor(name) {
|
|
1512
|
+
super(name, "bigint", "SQLiteBigInt");
|
|
1513
|
+
}
|
|
1514
|
+
build(table) {
|
|
1515
|
+
return new SQLiteBigInt(table, this.config);
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
class SQLiteBigInt extends SQLiteColumn {
|
|
1520
|
+
static [entityKind] = "SQLiteBigInt";
|
|
1521
|
+
getSQLType() {
|
|
1522
|
+
return "blob";
|
|
1523
|
+
}
|
|
1524
|
+
mapFromDriverValue(value) {
|
|
1525
|
+
if (Buffer.isBuffer(value)) {
|
|
1526
|
+
return BigInt(value.toString());
|
|
1527
|
+
}
|
|
1528
|
+
if (value instanceof ArrayBuffer) {
|
|
1529
|
+
const decoder = new TextDecoder;
|
|
1530
|
+
return BigInt(decoder.decode(value));
|
|
1531
|
+
}
|
|
1532
|
+
return BigInt(String.fromCodePoint(...value));
|
|
1533
|
+
}
|
|
1534
|
+
mapToDriverValue(value) {
|
|
1535
|
+
return Buffer.from(value.toString());
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
class SQLiteBlobJsonBuilder extends SQLiteColumnBuilder {
|
|
1540
|
+
static [entityKind] = "SQLiteBlobJsonBuilder";
|
|
1541
|
+
constructor(name) {
|
|
1542
|
+
super(name, "json", "SQLiteBlobJson");
|
|
1543
|
+
}
|
|
1544
|
+
build(table) {
|
|
1545
|
+
return new SQLiteBlobJson(table, this.config);
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
class SQLiteBlobJson extends SQLiteColumn {
|
|
1550
|
+
static [entityKind] = "SQLiteBlobJson";
|
|
1551
|
+
getSQLType() {
|
|
1552
|
+
return "blob";
|
|
1553
|
+
}
|
|
1554
|
+
mapFromDriverValue(value) {
|
|
1555
|
+
if (Buffer.isBuffer(value)) {
|
|
1556
|
+
return JSON.parse(value.toString());
|
|
1557
|
+
}
|
|
1558
|
+
if (value instanceof ArrayBuffer) {
|
|
1559
|
+
const decoder = new TextDecoder;
|
|
1560
|
+
return JSON.parse(decoder.decode(value));
|
|
1561
|
+
}
|
|
1562
|
+
return JSON.parse(String.fromCodePoint(...value));
|
|
1563
|
+
}
|
|
1564
|
+
mapToDriverValue(value) {
|
|
1565
|
+
return Buffer.from(JSON.stringify(value));
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
class SQLiteBlobBufferBuilder extends SQLiteColumnBuilder {
|
|
1570
|
+
static [entityKind] = "SQLiteBlobBufferBuilder";
|
|
1571
|
+
constructor(name) {
|
|
1572
|
+
super(name, "buffer", "SQLiteBlobBuffer");
|
|
1573
|
+
}
|
|
1574
|
+
build(table) {
|
|
1575
|
+
return new SQLiteBlobBuffer(table, this.config);
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
class SQLiteBlobBuffer extends SQLiteColumn {
|
|
1580
|
+
static [entityKind] = "SQLiteBlobBuffer";
|
|
1581
|
+
getSQLType() {
|
|
1582
|
+
return "blob";
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
function blob(a, b) {
|
|
1586
|
+
const { name, config } = getColumnNameAndConfig(a, b);
|
|
1587
|
+
if (config?.mode === "json") {
|
|
1588
|
+
return new SQLiteBlobJsonBuilder(name);
|
|
1589
|
+
}
|
|
1590
|
+
if (config?.mode === "bigint") {
|
|
1591
|
+
return new SQLiteBigIntBuilder(name);
|
|
1592
|
+
}
|
|
1593
|
+
return new SQLiteBlobBufferBuilder(name);
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/columns/custom.js
|
|
1597
|
+
class SQLiteCustomColumnBuilder extends SQLiteColumnBuilder {
|
|
1598
|
+
static [entityKind] = "SQLiteCustomColumnBuilder";
|
|
1599
|
+
constructor(name, fieldConfig, customTypeParams) {
|
|
1600
|
+
super(name, "custom", "SQLiteCustomColumn");
|
|
1601
|
+
this.config.fieldConfig = fieldConfig;
|
|
1602
|
+
this.config.customTypeParams = customTypeParams;
|
|
1603
|
+
}
|
|
1604
|
+
build(table) {
|
|
1605
|
+
return new SQLiteCustomColumn(table, this.config);
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
class SQLiteCustomColumn extends SQLiteColumn {
|
|
1610
|
+
static [entityKind] = "SQLiteCustomColumn";
|
|
1611
|
+
sqlName;
|
|
1612
|
+
mapTo;
|
|
1613
|
+
mapFrom;
|
|
1614
|
+
constructor(table, config) {
|
|
1615
|
+
super(table, config);
|
|
1616
|
+
this.sqlName = config.customTypeParams.dataType(config.fieldConfig);
|
|
1617
|
+
this.mapTo = config.customTypeParams.toDriver;
|
|
1618
|
+
this.mapFrom = config.customTypeParams.fromDriver;
|
|
1619
|
+
}
|
|
1620
|
+
getSQLType() {
|
|
1621
|
+
return this.sqlName;
|
|
1622
|
+
}
|
|
1623
|
+
mapFromDriverValue(value) {
|
|
1624
|
+
return typeof this.mapFrom === "function" ? this.mapFrom(value) : value;
|
|
1625
|
+
}
|
|
1626
|
+
mapToDriverValue(value) {
|
|
1627
|
+
return typeof this.mapTo === "function" ? this.mapTo(value) : value;
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
function customType(customTypeParams) {
|
|
1631
|
+
return (a, b) => {
|
|
1632
|
+
const { name, config } = getColumnNameAndConfig(a, b);
|
|
1633
|
+
return new SQLiteCustomColumnBuilder(name, config, customTypeParams);
|
|
1634
|
+
};
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/columns/integer.js
|
|
1638
|
+
class SQLiteBaseIntegerBuilder extends SQLiteColumnBuilder {
|
|
1639
|
+
static [entityKind] = "SQLiteBaseIntegerBuilder";
|
|
1640
|
+
constructor(name, dataType, columnType) {
|
|
1641
|
+
super(name, dataType, columnType);
|
|
1642
|
+
this.config.autoIncrement = false;
|
|
1643
|
+
}
|
|
1644
|
+
primaryKey(config) {
|
|
1645
|
+
if (config?.autoIncrement) {
|
|
1646
|
+
this.config.autoIncrement = true;
|
|
1647
|
+
}
|
|
1648
|
+
this.config.hasDefault = true;
|
|
1649
|
+
return super.primaryKey();
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
|
|
1653
|
+
class SQLiteBaseInteger extends SQLiteColumn {
|
|
1654
|
+
static [entityKind] = "SQLiteBaseInteger";
|
|
1655
|
+
autoIncrement = this.config.autoIncrement;
|
|
1656
|
+
getSQLType() {
|
|
1657
|
+
return "integer";
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
class SQLiteIntegerBuilder extends SQLiteBaseIntegerBuilder {
|
|
1662
|
+
static [entityKind] = "SQLiteIntegerBuilder";
|
|
1663
|
+
constructor(name) {
|
|
1664
|
+
super(name, "number", "SQLiteInteger");
|
|
1665
|
+
}
|
|
1666
|
+
build(table) {
|
|
1667
|
+
return new SQLiteInteger(table, this.config);
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
|
|
1671
|
+
class SQLiteInteger extends SQLiteBaseInteger {
|
|
1672
|
+
static [entityKind] = "SQLiteInteger";
|
|
1673
|
+
}
|
|
1674
|
+
|
|
1675
|
+
class SQLiteTimestampBuilder extends SQLiteBaseIntegerBuilder {
|
|
1676
|
+
static [entityKind] = "SQLiteTimestampBuilder";
|
|
1677
|
+
constructor(name, mode) {
|
|
1678
|
+
super(name, "date", "SQLiteTimestamp");
|
|
1679
|
+
this.config.mode = mode;
|
|
1680
|
+
}
|
|
1681
|
+
defaultNow() {
|
|
1682
|
+
return this.default(sql`(cast((julianday('now') - 2440587.5)*86400000 as integer))`);
|
|
1683
|
+
}
|
|
1684
|
+
build(table) {
|
|
1685
|
+
return new SQLiteTimestamp(table, this.config);
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
class SQLiteTimestamp extends SQLiteBaseInteger {
|
|
1690
|
+
static [entityKind] = "SQLiteTimestamp";
|
|
1691
|
+
mode = this.config.mode;
|
|
1692
|
+
mapFromDriverValue(value) {
|
|
1693
|
+
if (this.config.mode === "timestamp") {
|
|
1694
|
+
return new Date(value * 1000);
|
|
1695
|
+
}
|
|
1696
|
+
return new Date(value);
|
|
1697
|
+
}
|
|
1698
|
+
mapToDriverValue(value) {
|
|
1699
|
+
const unix = value.getTime();
|
|
1700
|
+
if (this.config.mode === "timestamp") {
|
|
1701
|
+
return Math.floor(unix / 1000);
|
|
1702
|
+
}
|
|
1703
|
+
return unix;
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
class SQLiteBooleanBuilder extends SQLiteBaseIntegerBuilder {
|
|
1708
|
+
static [entityKind] = "SQLiteBooleanBuilder";
|
|
1709
|
+
constructor(name, mode) {
|
|
1710
|
+
super(name, "boolean", "SQLiteBoolean");
|
|
1711
|
+
this.config.mode = mode;
|
|
1712
|
+
}
|
|
1713
|
+
build(table) {
|
|
1714
|
+
return new SQLiteBoolean(table, this.config);
|
|
1715
|
+
}
|
|
1716
|
+
}
|
|
1717
|
+
|
|
1718
|
+
class SQLiteBoolean extends SQLiteBaseInteger {
|
|
1719
|
+
static [entityKind] = "SQLiteBoolean";
|
|
1720
|
+
mode = this.config.mode;
|
|
1721
|
+
mapFromDriverValue(value) {
|
|
1722
|
+
return Number(value) === 1;
|
|
1723
|
+
}
|
|
1724
|
+
mapToDriverValue(value) {
|
|
1725
|
+
return value ? 1 : 0;
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1728
|
+
function integer(a, b) {
|
|
1729
|
+
const { name, config } = getColumnNameAndConfig(a, b);
|
|
1730
|
+
if (config?.mode === "timestamp" || config?.mode === "timestamp_ms") {
|
|
1731
|
+
return new SQLiteTimestampBuilder(name, config.mode);
|
|
1732
|
+
}
|
|
1733
|
+
if (config?.mode === "boolean") {
|
|
1734
|
+
return new SQLiteBooleanBuilder(name, config.mode);
|
|
1735
|
+
}
|
|
1736
|
+
return new SQLiteIntegerBuilder(name);
|
|
1737
|
+
}
|
|
1738
|
+
|
|
1739
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/columns/numeric.js
|
|
1740
|
+
class SQLiteNumericBuilder extends SQLiteColumnBuilder {
|
|
1741
|
+
static [entityKind] = "SQLiteNumericBuilder";
|
|
1742
|
+
constructor(name) {
|
|
1743
|
+
super(name, "string", "SQLiteNumeric");
|
|
1744
|
+
}
|
|
1745
|
+
build(table) {
|
|
1746
|
+
return new SQLiteNumeric(table, this.config);
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
class SQLiteNumeric extends SQLiteColumn {
|
|
1751
|
+
static [entityKind] = "SQLiteNumeric";
|
|
1752
|
+
getSQLType() {
|
|
1753
|
+
return "numeric";
|
|
1754
|
+
}
|
|
1755
|
+
}
|
|
1756
|
+
function numeric(name) {
|
|
1757
|
+
return new SQLiteNumericBuilder(name ?? "");
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/columns/real.js
|
|
1761
|
+
class SQLiteRealBuilder extends SQLiteColumnBuilder {
|
|
1762
|
+
static [entityKind] = "SQLiteRealBuilder";
|
|
1763
|
+
constructor(name) {
|
|
1764
|
+
super(name, "number", "SQLiteReal");
|
|
1765
|
+
}
|
|
1766
|
+
build(table) {
|
|
1767
|
+
return new SQLiteReal(table, this.config);
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
|
|
1771
|
+
class SQLiteReal extends SQLiteColumn {
|
|
1772
|
+
static [entityKind] = "SQLiteReal";
|
|
1773
|
+
getSQLType() {
|
|
1774
|
+
return "real";
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
function real(name) {
|
|
1778
|
+
return new SQLiteRealBuilder(name ?? "");
|
|
1779
|
+
}
|
|
1780
|
+
|
|
1781
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/columns/text.js
|
|
1782
|
+
class SQLiteTextBuilder extends SQLiteColumnBuilder {
|
|
1783
|
+
static [entityKind] = "SQLiteTextBuilder";
|
|
1784
|
+
constructor(name, config) {
|
|
1785
|
+
super(name, "string", "SQLiteText");
|
|
1786
|
+
this.config.enumValues = config.enum;
|
|
1787
|
+
this.config.length = config.length;
|
|
1788
|
+
}
|
|
1789
|
+
build(table) {
|
|
1790
|
+
return new SQLiteText(table, this.config);
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
class SQLiteText extends SQLiteColumn {
|
|
1795
|
+
static [entityKind] = "SQLiteText";
|
|
1796
|
+
enumValues = this.config.enumValues;
|
|
1797
|
+
length = this.config.length;
|
|
1798
|
+
constructor(table, config) {
|
|
1799
|
+
super(table, config);
|
|
1800
|
+
}
|
|
1801
|
+
getSQLType() {
|
|
1802
|
+
return `text${this.config.length ? `(${this.config.length})` : ""}`;
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1806
|
+
class SQLiteTextJsonBuilder extends SQLiteColumnBuilder {
|
|
1807
|
+
static [entityKind] = "SQLiteTextJsonBuilder";
|
|
1808
|
+
constructor(name) {
|
|
1809
|
+
super(name, "json", "SQLiteTextJson");
|
|
1810
|
+
}
|
|
1811
|
+
build(table) {
|
|
1812
|
+
return new SQLiteTextJson(table, this.config);
|
|
1813
|
+
}
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
class SQLiteTextJson extends SQLiteColumn {
|
|
1817
|
+
static [entityKind] = "SQLiteTextJson";
|
|
1818
|
+
getSQLType() {
|
|
1819
|
+
return "text";
|
|
1820
|
+
}
|
|
1821
|
+
mapFromDriverValue(value) {
|
|
1822
|
+
return JSON.parse(value);
|
|
1823
|
+
}
|
|
1824
|
+
mapToDriverValue(value) {
|
|
1825
|
+
return JSON.stringify(value);
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
function text(a, b = {}) {
|
|
1829
|
+
const { name, config } = getColumnNameAndConfig(a, b);
|
|
1830
|
+
if (config.mode === "json") {
|
|
1831
|
+
return new SQLiteTextJsonBuilder(name);
|
|
1832
|
+
}
|
|
1833
|
+
return new SQLiteTextBuilder(name, config);
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/columns/all.js
|
|
1837
|
+
function getSQLiteColumnBuilders() {
|
|
1838
|
+
return {
|
|
1839
|
+
blob,
|
|
1840
|
+
customType,
|
|
1841
|
+
integer,
|
|
1842
|
+
numeric,
|
|
1843
|
+
real,
|
|
1844
|
+
text
|
|
1845
|
+
};
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1848
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/table.js
|
|
1849
|
+
var InlineForeignKeys2 = Symbol.for("drizzle:SQLiteInlineForeignKeys");
|
|
1850
|
+
|
|
1851
|
+
class SQLiteTable extends Table {
|
|
1852
|
+
static [entityKind] = "SQLiteTable";
|
|
1853
|
+
static Symbol = Object.assign({}, Table.Symbol, {
|
|
1854
|
+
InlineForeignKeys: InlineForeignKeys2
|
|
1855
|
+
});
|
|
1856
|
+
[Table.Symbol.Columns];
|
|
1857
|
+
[InlineForeignKeys2] = [];
|
|
1858
|
+
[Table.Symbol.ExtraConfigBuilder] = undefined;
|
|
1859
|
+
}
|
|
1860
|
+
function sqliteTableBase(name, columns, extraConfig, schema, baseName = name) {
|
|
1861
|
+
const rawTable = new SQLiteTable(name, schema, baseName);
|
|
1862
|
+
const parsedColumns = typeof columns === "function" ? columns(getSQLiteColumnBuilders()) : columns;
|
|
1863
|
+
const builtColumns = Object.fromEntries(Object.entries(parsedColumns).map(([name2, colBuilderBase]) => {
|
|
1864
|
+
const colBuilder = colBuilderBase;
|
|
1865
|
+
colBuilder.setName(name2);
|
|
1866
|
+
const column = colBuilder.build(rawTable);
|
|
1867
|
+
rawTable[InlineForeignKeys2].push(...colBuilder.buildForeignKeys(column, rawTable));
|
|
1868
|
+
return [name2, column];
|
|
1869
|
+
}));
|
|
1870
|
+
const table = Object.assign(rawTable, builtColumns);
|
|
1871
|
+
table[Table.Symbol.Columns] = builtColumns;
|
|
1872
|
+
table[Table.Symbol.ExtraConfigColumns] = builtColumns;
|
|
1873
|
+
if (extraConfig) {
|
|
1874
|
+
table[SQLiteTable.Symbol.ExtraConfigBuilder] = extraConfig;
|
|
1875
|
+
}
|
|
1876
|
+
return table;
|
|
1877
|
+
}
|
|
1878
|
+
var sqliteTable = (name, columns, extraConfig) => {
|
|
1879
|
+
return sqliteTableBase(name, columns, extraConfig);
|
|
1880
|
+
};
|
|
1881
|
+
|
|
1882
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/query-builders/delete.js
|
|
1883
|
+
class SQLiteDeleteBase extends QueryPromise {
|
|
1884
|
+
constructor(table, session, dialect, withList) {
|
|
1885
|
+
super();
|
|
1886
|
+
this.table = table;
|
|
1887
|
+
this.session = session;
|
|
1888
|
+
this.dialect = dialect;
|
|
1889
|
+
this.config = { table, withList };
|
|
1890
|
+
}
|
|
1891
|
+
static [entityKind] = "SQLiteDelete";
|
|
1892
|
+
config;
|
|
1893
|
+
where(where) {
|
|
1894
|
+
this.config.where = where;
|
|
1895
|
+
return this;
|
|
1896
|
+
}
|
|
1897
|
+
orderBy(...columns) {
|
|
1898
|
+
if (typeof columns[0] === "function") {
|
|
1899
|
+
const orderBy = columns[0](new Proxy(this.config.table[Table.Symbol.Columns], new SelectionProxyHandler({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" })));
|
|
1900
|
+
const orderByArray = Array.isArray(orderBy) ? orderBy : [orderBy];
|
|
1901
|
+
this.config.orderBy = orderByArray;
|
|
1902
|
+
} else {
|
|
1903
|
+
const orderByArray = columns;
|
|
1904
|
+
this.config.orderBy = orderByArray;
|
|
1905
|
+
}
|
|
1906
|
+
return this;
|
|
1907
|
+
}
|
|
1908
|
+
limit(limit) {
|
|
1909
|
+
this.config.limit = limit;
|
|
1910
|
+
return this;
|
|
1911
|
+
}
|
|
1912
|
+
returning(fields = this.table[SQLiteTable.Symbol.Columns]) {
|
|
1913
|
+
this.config.returning = orderSelectedFields(fields);
|
|
1914
|
+
return this;
|
|
1915
|
+
}
|
|
1916
|
+
getSQL() {
|
|
1917
|
+
return this.dialect.buildDeleteQuery(this.config);
|
|
1918
|
+
}
|
|
1919
|
+
toSQL() {
|
|
1920
|
+
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
|
|
1921
|
+
return rest;
|
|
1922
|
+
}
|
|
1923
|
+
_prepare(isOneTimeQuery = true) {
|
|
1924
|
+
return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](this.dialect.sqlToQuery(this.getSQL()), this.config.returning, this.config.returning ? "all" : "run", true);
|
|
1925
|
+
}
|
|
1926
|
+
prepare() {
|
|
1927
|
+
return this._prepare(false);
|
|
1928
|
+
}
|
|
1929
|
+
run = (placeholderValues) => {
|
|
1930
|
+
return this._prepare().run(placeholderValues);
|
|
1931
|
+
};
|
|
1932
|
+
all = (placeholderValues) => {
|
|
1933
|
+
return this._prepare().all(placeholderValues);
|
|
1934
|
+
};
|
|
1935
|
+
get = (placeholderValues) => {
|
|
1936
|
+
return this._prepare().get(placeholderValues);
|
|
1937
|
+
};
|
|
1938
|
+
values = (placeholderValues) => {
|
|
1939
|
+
return this._prepare().values(placeholderValues);
|
|
1940
|
+
};
|
|
1941
|
+
async execute(placeholderValues) {
|
|
1942
|
+
return this._prepare().execute(placeholderValues);
|
|
1943
|
+
}
|
|
1944
|
+
$dynamic() {
|
|
1945
|
+
return this;
|
|
1946
|
+
}
|
|
1947
|
+
}
|
|
1948
|
+
|
|
1949
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/casing.js
|
|
1950
|
+
function toSnakeCase(input) {
|
|
1951
|
+
const words = input.replace(/['\u2019]/g, "").match(/[\da-z]+|[A-Z]+(?![a-z])|[A-Z][\da-z]+/g) ?? [];
|
|
1952
|
+
return words.map((word) => word.toLowerCase()).join("_");
|
|
1953
|
+
}
|
|
1954
|
+
function toCamelCase(input) {
|
|
1955
|
+
const words = input.replace(/['\u2019]/g, "").match(/[\da-z]+|[A-Z]+(?![a-z])|[A-Z][\da-z]+/g) ?? [];
|
|
1956
|
+
return words.reduce((acc, word, i) => {
|
|
1957
|
+
const formattedWord = i === 0 ? word.toLowerCase() : `${word[0].toUpperCase()}${word.slice(1)}`;
|
|
1958
|
+
return acc + formattedWord;
|
|
1959
|
+
}, "");
|
|
1960
|
+
}
|
|
1961
|
+
function noopCase(input) {
|
|
1962
|
+
return input;
|
|
1963
|
+
}
|
|
1964
|
+
|
|
1965
|
+
class CasingCache {
|
|
1966
|
+
static [entityKind] = "CasingCache";
|
|
1967
|
+
cache = {};
|
|
1968
|
+
cachedTables = {};
|
|
1969
|
+
convert;
|
|
1970
|
+
constructor(casing) {
|
|
1971
|
+
this.convert = casing === "snake_case" ? toSnakeCase : casing === "camelCase" ? toCamelCase : noopCase;
|
|
1972
|
+
}
|
|
1973
|
+
getColumnCasing(column) {
|
|
1974
|
+
if (!column.keyAsName)
|
|
1975
|
+
return column.name;
|
|
1976
|
+
const schema = column.table[Table.Symbol.Schema] ?? "public";
|
|
1977
|
+
const tableName = column.table[Table.Symbol.OriginalName];
|
|
1978
|
+
const key = `${schema}.${tableName}.${column.name}`;
|
|
1979
|
+
if (!this.cache[key]) {
|
|
1980
|
+
this.cacheTable(column.table);
|
|
1981
|
+
}
|
|
1982
|
+
return this.cache[key];
|
|
1983
|
+
}
|
|
1984
|
+
cacheTable(table) {
|
|
1985
|
+
const schema = table[Table.Symbol.Schema] ?? "public";
|
|
1986
|
+
const tableName = table[Table.Symbol.OriginalName];
|
|
1987
|
+
const tableKey = `${schema}.${tableName}`;
|
|
1988
|
+
if (!this.cachedTables[tableKey]) {
|
|
1989
|
+
for (const column of Object.values(table[Table.Symbol.Columns])) {
|
|
1990
|
+
const columnKey = `${tableKey}.${column.name}`;
|
|
1991
|
+
this.cache[columnKey] = this.convert(column.name);
|
|
1992
|
+
}
|
|
1993
|
+
this.cachedTables[tableKey] = true;
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
clearCache() {
|
|
1997
|
+
this.cache = {};
|
|
1998
|
+
this.cachedTables = {};
|
|
1999
|
+
}
|
|
2000
|
+
}
|
|
2001
|
+
|
|
2002
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/errors.js
|
|
2003
|
+
class DrizzleError extends Error {
|
|
2004
|
+
static [entityKind] = "DrizzleError";
|
|
2005
|
+
constructor({ message, cause }) {
|
|
2006
|
+
super(message);
|
|
2007
|
+
this.name = "DrizzleError";
|
|
2008
|
+
this.cause = cause;
|
|
2009
|
+
}
|
|
2010
|
+
}
|
|
2011
|
+
|
|
2012
|
+
class TransactionRollbackError extends DrizzleError {
|
|
2013
|
+
static [entityKind] = "TransactionRollbackError";
|
|
2014
|
+
constructor() {
|
|
2015
|
+
super({ message: "Rollback" });
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
|
|
2019
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/view-base.js
|
|
2020
|
+
class SQLiteViewBase extends View {
|
|
2021
|
+
static [entityKind] = "SQLiteViewBase";
|
|
2022
|
+
}
|
|
2023
|
+
|
|
2024
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/dialect.js
|
|
2025
|
+
class SQLiteDialect {
|
|
2026
|
+
static [entityKind] = "SQLiteDialect";
|
|
2027
|
+
casing;
|
|
2028
|
+
constructor(config) {
|
|
2029
|
+
this.casing = new CasingCache(config?.casing);
|
|
2030
|
+
}
|
|
2031
|
+
escapeName(name) {
|
|
2032
|
+
return `"${name}"`;
|
|
2033
|
+
}
|
|
2034
|
+
escapeParam(_num) {
|
|
2035
|
+
return "?";
|
|
2036
|
+
}
|
|
2037
|
+
escapeString(str) {
|
|
2038
|
+
return `'${str.replace(/'/g, "''")}'`;
|
|
2039
|
+
}
|
|
2040
|
+
buildWithCTE(queries) {
|
|
2041
|
+
if (!queries?.length)
|
|
2042
|
+
return;
|
|
2043
|
+
const withSqlChunks = [sql`with `];
|
|
2044
|
+
for (const [i, w] of queries.entries()) {
|
|
2045
|
+
withSqlChunks.push(sql`${sql.identifier(w._.alias)} as (${w._.sql})`);
|
|
2046
|
+
if (i < queries.length - 1) {
|
|
2047
|
+
withSqlChunks.push(sql`, `);
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
withSqlChunks.push(sql` `);
|
|
2051
|
+
return sql.join(withSqlChunks);
|
|
2052
|
+
}
|
|
2053
|
+
buildDeleteQuery({ table, where, returning, withList, limit, orderBy }) {
|
|
2054
|
+
const withSql = this.buildWithCTE(withList);
|
|
2055
|
+
const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : undefined;
|
|
2056
|
+
const whereSql = where ? sql` where ${where}` : undefined;
|
|
2057
|
+
const orderBySql = this.buildOrderBy(orderBy);
|
|
2058
|
+
const limitSql = this.buildLimit(limit);
|
|
2059
|
+
return sql`${withSql}delete from ${table}${whereSql}${returningSql}${orderBySql}${limitSql}`;
|
|
2060
|
+
}
|
|
2061
|
+
buildUpdateSet(table, set) {
|
|
2062
|
+
const tableColumns = table[Table.Symbol.Columns];
|
|
2063
|
+
const columnNames = Object.keys(tableColumns).filter((colName) => set[colName] !== undefined || tableColumns[colName]?.onUpdateFn !== undefined);
|
|
2064
|
+
const setSize = columnNames.length;
|
|
2065
|
+
return sql.join(columnNames.flatMap((colName, i) => {
|
|
2066
|
+
const col = tableColumns[colName];
|
|
2067
|
+
const value = set[colName] ?? sql.param(col.onUpdateFn(), col);
|
|
2068
|
+
const res = sql`${sql.identifier(this.casing.getColumnCasing(col))} = ${value}`;
|
|
2069
|
+
if (i < setSize - 1) {
|
|
2070
|
+
return [res, sql.raw(", ")];
|
|
2071
|
+
}
|
|
2072
|
+
return [res];
|
|
2073
|
+
}));
|
|
2074
|
+
}
|
|
2075
|
+
buildUpdateQuery({ table, set, where, returning, withList, joins, from, limit, orderBy }) {
|
|
2076
|
+
const withSql = this.buildWithCTE(withList);
|
|
2077
|
+
const setSql = this.buildUpdateSet(table, set);
|
|
2078
|
+
const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
|
|
2079
|
+
const joinsSql = this.buildJoins(joins);
|
|
2080
|
+
const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : undefined;
|
|
2081
|
+
const whereSql = where ? sql` where ${where}` : undefined;
|
|
2082
|
+
const orderBySql = this.buildOrderBy(orderBy);
|
|
2083
|
+
const limitSql = this.buildLimit(limit);
|
|
2084
|
+
return sql`${withSql}update ${table} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
|
|
2085
|
+
}
|
|
2086
|
+
buildSelection(fields, { isSingleTable = false } = {}) {
|
|
2087
|
+
const columnsLen = fields.length;
|
|
2088
|
+
const chunks = fields.flatMap(({ field }, i) => {
|
|
2089
|
+
const chunk = [];
|
|
2090
|
+
if (is(field, SQL.Aliased) && field.isSelectionField) {
|
|
2091
|
+
chunk.push(sql.identifier(field.fieldAlias));
|
|
2092
|
+
} else if (is(field, SQL.Aliased) || is(field, SQL)) {
|
|
2093
|
+
const query = is(field, SQL.Aliased) ? field.sql : field;
|
|
2094
|
+
if (isSingleTable) {
|
|
2095
|
+
chunk.push(new SQL(query.queryChunks.map((c) => {
|
|
2096
|
+
if (is(c, Column)) {
|
|
2097
|
+
return sql.identifier(this.casing.getColumnCasing(c));
|
|
2098
|
+
}
|
|
2099
|
+
return c;
|
|
2100
|
+
})));
|
|
2101
|
+
} else {
|
|
2102
|
+
chunk.push(query);
|
|
2103
|
+
}
|
|
2104
|
+
if (is(field, SQL.Aliased)) {
|
|
2105
|
+
chunk.push(sql` as ${sql.identifier(field.fieldAlias)}`);
|
|
2106
|
+
}
|
|
2107
|
+
} else if (is(field, Column)) {
|
|
2108
|
+
const tableName = field.table[Table.Symbol.Name];
|
|
2109
|
+
if (isSingleTable) {
|
|
2110
|
+
chunk.push(sql.identifier(this.casing.getColumnCasing(field)));
|
|
2111
|
+
} else {
|
|
2112
|
+
chunk.push(sql`${sql.identifier(tableName)}.${sql.identifier(this.casing.getColumnCasing(field))}`);
|
|
2113
|
+
}
|
|
2114
|
+
}
|
|
2115
|
+
if (i < columnsLen - 1) {
|
|
2116
|
+
chunk.push(sql`, `);
|
|
2117
|
+
}
|
|
2118
|
+
return chunk;
|
|
2119
|
+
});
|
|
2120
|
+
return sql.join(chunks);
|
|
2121
|
+
}
|
|
2122
|
+
buildJoins(joins) {
|
|
2123
|
+
if (!joins || joins.length === 0) {
|
|
2124
|
+
return;
|
|
2125
|
+
}
|
|
2126
|
+
const joinsArray = [];
|
|
2127
|
+
if (joins) {
|
|
2128
|
+
for (const [index, joinMeta] of joins.entries()) {
|
|
2129
|
+
if (index === 0) {
|
|
2130
|
+
joinsArray.push(sql` `);
|
|
2131
|
+
}
|
|
2132
|
+
const table = joinMeta.table;
|
|
2133
|
+
if (is(table, SQLiteTable)) {
|
|
2134
|
+
const tableName = table[SQLiteTable.Symbol.Name];
|
|
2135
|
+
const tableSchema = table[SQLiteTable.Symbol.Schema];
|
|
2136
|
+
const origTableName = table[SQLiteTable.Symbol.OriginalName];
|
|
2137
|
+
const alias = tableName === origTableName ? undefined : joinMeta.alias;
|
|
2138
|
+
joinsArray.push(sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : undefined}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`);
|
|
2139
|
+
} else {
|
|
2140
|
+
joinsArray.push(sql`${sql.raw(joinMeta.joinType)} join ${table} on ${joinMeta.on}`);
|
|
2141
|
+
}
|
|
2142
|
+
if (index < joins.length - 1) {
|
|
2143
|
+
joinsArray.push(sql` `);
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
}
|
|
2147
|
+
return sql.join(joinsArray);
|
|
2148
|
+
}
|
|
2149
|
+
buildLimit(limit) {
|
|
2150
|
+
return typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : undefined;
|
|
2151
|
+
}
|
|
2152
|
+
buildOrderBy(orderBy) {
|
|
2153
|
+
const orderByList = [];
|
|
2154
|
+
if (orderBy) {
|
|
2155
|
+
for (const [index, orderByValue] of orderBy.entries()) {
|
|
2156
|
+
orderByList.push(orderByValue);
|
|
2157
|
+
if (index < orderBy.length - 1) {
|
|
2158
|
+
orderByList.push(sql`, `);
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
}
|
|
2162
|
+
return orderByList.length > 0 ? sql` order by ${sql.join(orderByList)}` : undefined;
|
|
2163
|
+
}
|
|
2164
|
+
buildFromTable(table) {
|
|
2165
|
+
if (is(table, Table) && table[Table.Symbol.OriginalName] !== table[Table.Symbol.Name]) {
|
|
2166
|
+
return sql`${sql.identifier(table[Table.Symbol.OriginalName])} ${sql.identifier(table[Table.Symbol.Name])}`;
|
|
2167
|
+
}
|
|
2168
|
+
return table;
|
|
2169
|
+
}
|
|
2170
|
+
buildSelectQuery({
|
|
2171
|
+
withList,
|
|
2172
|
+
fields,
|
|
2173
|
+
fieldsFlat,
|
|
2174
|
+
where,
|
|
2175
|
+
having,
|
|
2176
|
+
table,
|
|
2177
|
+
joins,
|
|
2178
|
+
orderBy,
|
|
2179
|
+
groupBy,
|
|
2180
|
+
limit,
|
|
2181
|
+
offset,
|
|
2182
|
+
distinct,
|
|
2183
|
+
setOperators
|
|
2184
|
+
}) {
|
|
2185
|
+
const fieldsList = fieldsFlat ?? orderSelectedFields(fields);
|
|
2186
|
+
for (const f of fieldsList) {
|
|
2187
|
+
if (is(f.field, Column) && getTableName(f.field.table) !== (is(table, Subquery) ? table._.alias : is(table, SQLiteViewBase) ? table[ViewBaseConfig].name : is(table, SQL) ? undefined : getTableName(table)) && !((table2) => joins?.some(({ alias }) => alias === (table2[Table.Symbol.IsAlias] ? getTableName(table2) : table2[Table.Symbol.BaseName])))(f.field.table)) {
|
|
2188
|
+
const tableName = getTableName(f.field.table);
|
|
2189
|
+
throw new Error(`Your "${f.path.join("->")}" field references a column "${tableName}"."${f.field.name}", but the table "${tableName}" is not part of the query! Did you forget to join it?`);
|
|
2190
|
+
}
|
|
2191
|
+
}
|
|
2192
|
+
const isSingleTable = !joins || joins.length === 0;
|
|
2193
|
+
const withSql = this.buildWithCTE(withList);
|
|
2194
|
+
const distinctSql = distinct ? sql` distinct` : undefined;
|
|
2195
|
+
const selection = this.buildSelection(fieldsList, { isSingleTable });
|
|
2196
|
+
const tableSql = this.buildFromTable(table);
|
|
2197
|
+
const joinsSql = this.buildJoins(joins);
|
|
2198
|
+
const whereSql = where ? sql` where ${where}` : undefined;
|
|
2199
|
+
const havingSql = having ? sql` having ${having}` : undefined;
|
|
2200
|
+
const groupByList = [];
|
|
2201
|
+
if (groupBy) {
|
|
2202
|
+
for (const [index, groupByValue] of groupBy.entries()) {
|
|
2203
|
+
groupByList.push(groupByValue);
|
|
2204
|
+
if (index < groupBy.length - 1) {
|
|
2205
|
+
groupByList.push(sql`, `);
|
|
2206
|
+
}
|
|
2207
|
+
}
|
|
2208
|
+
}
|
|
2209
|
+
const groupBySql = groupByList.length > 0 ? sql` group by ${sql.join(groupByList)}` : undefined;
|
|
2210
|
+
const orderBySql = this.buildOrderBy(orderBy);
|
|
2211
|
+
const limitSql = this.buildLimit(limit);
|
|
2212
|
+
const offsetSql = offset ? sql` offset ${offset}` : undefined;
|
|
2213
|
+
const finalQuery = sql`${withSql}select${distinctSql} ${selection} from ${tableSql}${joinsSql}${whereSql}${groupBySql}${havingSql}${orderBySql}${limitSql}${offsetSql}`;
|
|
2214
|
+
if (setOperators.length > 0) {
|
|
2215
|
+
return this.buildSetOperations(finalQuery, setOperators);
|
|
2216
|
+
}
|
|
2217
|
+
return finalQuery;
|
|
2218
|
+
}
|
|
2219
|
+
buildSetOperations(leftSelect, setOperators) {
|
|
2220
|
+
const [setOperator, ...rest] = setOperators;
|
|
2221
|
+
if (!setOperator) {
|
|
2222
|
+
throw new Error("Cannot pass undefined values to any set operator");
|
|
2223
|
+
}
|
|
2224
|
+
if (rest.length === 0) {
|
|
2225
|
+
return this.buildSetOperationQuery({ leftSelect, setOperator });
|
|
2226
|
+
}
|
|
2227
|
+
return this.buildSetOperations(this.buildSetOperationQuery({ leftSelect, setOperator }), rest);
|
|
2228
|
+
}
|
|
2229
|
+
buildSetOperationQuery({
|
|
2230
|
+
leftSelect,
|
|
2231
|
+
setOperator: { type, isAll, rightSelect, limit, orderBy, offset }
|
|
2232
|
+
}) {
|
|
2233
|
+
const leftChunk = sql`${leftSelect.getSQL()} `;
|
|
2234
|
+
const rightChunk = sql`${rightSelect.getSQL()}`;
|
|
2235
|
+
let orderBySql;
|
|
2236
|
+
if (orderBy && orderBy.length > 0) {
|
|
2237
|
+
const orderByValues = [];
|
|
2238
|
+
for (const singleOrderBy of orderBy) {
|
|
2239
|
+
if (is(singleOrderBy, SQLiteColumn)) {
|
|
2240
|
+
orderByValues.push(sql.identifier(singleOrderBy.name));
|
|
2241
|
+
} else if (is(singleOrderBy, SQL)) {
|
|
2242
|
+
for (let i = 0;i < singleOrderBy.queryChunks.length; i++) {
|
|
2243
|
+
const chunk = singleOrderBy.queryChunks[i];
|
|
2244
|
+
if (is(chunk, SQLiteColumn)) {
|
|
2245
|
+
singleOrderBy.queryChunks[i] = sql.identifier(this.casing.getColumnCasing(chunk));
|
|
2246
|
+
}
|
|
2247
|
+
}
|
|
2248
|
+
orderByValues.push(sql`${singleOrderBy}`);
|
|
2249
|
+
} else {
|
|
2250
|
+
orderByValues.push(sql`${singleOrderBy}`);
|
|
2251
|
+
}
|
|
2252
|
+
}
|
|
2253
|
+
orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)}`;
|
|
2254
|
+
}
|
|
2255
|
+
const limitSql = typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : undefined;
|
|
2256
|
+
const operatorChunk = sql.raw(`${type} ${isAll ? "all " : ""}`);
|
|
2257
|
+
const offsetSql = offset ? sql` offset ${offset}` : undefined;
|
|
2258
|
+
return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
|
|
2259
|
+
}
|
|
2260
|
+
buildInsertQuery({ table, values: valuesOrSelect, onConflict, returning, withList, select }) {
|
|
2261
|
+
const valuesSqlList = [];
|
|
2262
|
+
const columns = table[Table.Symbol.Columns];
|
|
2263
|
+
const colEntries = Object.entries(columns).filter(([_, col]) => !col.shouldDisableInsert());
|
|
2264
|
+
const insertOrder = colEntries.map(([, column]) => sql.identifier(this.casing.getColumnCasing(column)));
|
|
2265
|
+
if (select) {
|
|
2266
|
+
const select2 = valuesOrSelect;
|
|
2267
|
+
if (is(select2, SQL)) {
|
|
2268
|
+
valuesSqlList.push(select2);
|
|
2269
|
+
} else {
|
|
2270
|
+
valuesSqlList.push(select2.getSQL());
|
|
2271
|
+
}
|
|
2272
|
+
} else {
|
|
2273
|
+
const values = valuesOrSelect;
|
|
2274
|
+
valuesSqlList.push(sql.raw("values "));
|
|
2275
|
+
for (const [valueIndex, value] of values.entries()) {
|
|
2276
|
+
const valueList = [];
|
|
2277
|
+
for (const [fieldName, col] of colEntries) {
|
|
2278
|
+
const colValue = value[fieldName];
|
|
2279
|
+
if (colValue === undefined || is(colValue, Param) && colValue.value === undefined) {
|
|
2280
|
+
let defaultValue;
|
|
2281
|
+
if (col.default !== null && col.default !== undefined) {
|
|
2282
|
+
defaultValue = is(col.default, SQL) ? col.default : sql.param(col.default, col);
|
|
2283
|
+
} else if (col.defaultFn !== undefined) {
|
|
2284
|
+
const defaultFnResult = col.defaultFn();
|
|
2285
|
+
defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
|
|
2286
|
+
} else if (!col.default && col.onUpdateFn !== undefined) {
|
|
2287
|
+
const onUpdateFnResult = col.onUpdateFn();
|
|
2288
|
+
defaultValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
|
|
2289
|
+
} else {
|
|
2290
|
+
defaultValue = sql`null`;
|
|
2291
|
+
}
|
|
2292
|
+
valueList.push(defaultValue);
|
|
2293
|
+
} else {
|
|
2294
|
+
valueList.push(colValue);
|
|
2295
|
+
}
|
|
2296
|
+
}
|
|
2297
|
+
valuesSqlList.push(valueList);
|
|
2298
|
+
if (valueIndex < values.length - 1) {
|
|
2299
|
+
valuesSqlList.push(sql`, `);
|
|
2300
|
+
}
|
|
2301
|
+
}
|
|
2302
|
+
}
|
|
2303
|
+
const withSql = this.buildWithCTE(withList);
|
|
2304
|
+
const valuesSql = sql.join(valuesSqlList);
|
|
2305
|
+
const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : undefined;
|
|
2306
|
+
const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : undefined;
|
|
2307
|
+
return sql`${withSql}insert into ${table} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
|
|
2308
|
+
}
|
|
2309
|
+
sqlToQuery(sql2, invokeSource) {
|
|
2310
|
+
return sql2.toQuery({
|
|
2311
|
+
casing: this.casing,
|
|
2312
|
+
escapeName: this.escapeName,
|
|
2313
|
+
escapeParam: this.escapeParam,
|
|
2314
|
+
escapeString: this.escapeString,
|
|
2315
|
+
invokeSource
|
|
2316
|
+
});
|
|
2317
|
+
}
|
|
2318
|
+
buildRelationalQuery({
|
|
2319
|
+
fullSchema,
|
|
2320
|
+
schema,
|
|
2321
|
+
tableNamesMap,
|
|
2322
|
+
table,
|
|
2323
|
+
tableConfig,
|
|
2324
|
+
queryConfig: config,
|
|
2325
|
+
tableAlias,
|
|
2326
|
+
nestedQueryRelation,
|
|
2327
|
+
joinOn
|
|
2328
|
+
}) {
|
|
2329
|
+
let selection = [];
|
|
2330
|
+
let limit, offset, orderBy = [], where;
|
|
2331
|
+
const joins = [];
|
|
2332
|
+
if (config === true) {
|
|
2333
|
+
const selectionEntries = Object.entries(tableConfig.columns);
|
|
2334
|
+
selection = selectionEntries.map(([key, value]) => ({
|
|
2335
|
+
dbKey: value.name,
|
|
2336
|
+
tsKey: key,
|
|
2337
|
+
field: aliasedTableColumn(value, tableAlias),
|
|
2338
|
+
relationTableTsKey: undefined,
|
|
2339
|
+
isJson: false,
|
|
2340
|
+
selection: []
|
|
2341
|
+
}));
|
|
2342
|
+
} else {
|
|
2343
|
+
const aliasedColumns = Object.fromEntries(Object.entries(tableConfig.columns).map(([key, value]) => [key, aliasedTableColumn(value, tableAlias)]));
|
|
2344
|
+
if (config.where) {
|
|
2345
|
+
const whereSql = typeof config.where === "function" ? config.where(aliasedColumns, getOperators()) : config.where;
|
|
2346
|
+
where = whereSql && mapColumnsInSQLToAlias(whereSql, tableAlias);
|
|
2347
|
+
}
|
|
2348
|
+
const fieldsSelection = [];
|
|
2349
|
+
let selectedColumns = [];
|
|
2350
|
+
if (config.columns) {
|
|
2351
|
+
let isIncludeMode = false;
|
|
2352
|
+
for (const [field, value] of Object.entries(config.columns)) {
|
|
2353
|
+
if (value === undefined) {
|
|
2354
|
+
continue;
|
|
2355
|
+
}
|
|
2356
|
+
if (field in tableConfig.columns) {
|
|
2357
|
+
if (!isIncludeMode && value === true) {
|
|
2358
|
+
isIncludeMode = true;
|
|
2359
|
+
}
|
|
2360
|
+
selectedColumns.push(field);
|
|
2361
|
+
}
|
|
2362
|
+
}
|
|
2363
|
+
if (selectedColumns.length > 0) {
|
|
2364
|
+
selectedColumns = isIncludeMode ? selectedColumns.filter((c) => config.columns?.[c] === true) : Object.keys(tableConfig.columns).filter((key) => !selectedColumns.includes(key));
|
|
2365
|
+
}
|
|
2366
|
+
} else {
|
|
2367
|
+
selectedColumns = Object.keys(tableConfig.columns);
|
|
2368
|
+
}
|
|
2369
|
+
for (const field of selectedColumns) {
|
|
2370
|
+
const column = tableConfig.columns[field];
|
|
2371
|
+
fieldsSelection.push({ tsKey: field, value: column });
|
|
2372
|
+
}
|
|
2373
|
+
let selectedRelations = [];
|
|
2374
|
+
if (config.with) {
|
|
2375
|
+
selectedRelations = Object.entries(config.with).filter((entry) => !!entry[1]).map(([tsKey, queryConfig]) => ({ tsKey, queryConfig, relation: tableConfig.relations[tsKey] }));
|
|
2376
|
+
}
|
|
2377
|
+
let extras;
|
|
2378
|
+
if (config.extras) {
|
|
2379
|
+
extras = typeof config.extras === "function" ? config.extras(aliasedColumns, { sql }) : config.extras;
|
|
2380
|
+
for (const [tsKey, value] of Object.entries(extras)) {
|
|
2381
|
+
fieldsSelection.push({
|
|
2382
|
+
tsKey,
|
|
2383
|
+
value: mapColumnsInAliasedSQLToAlias(value, tableAlias)
|
|
2384
|
+
});
|
|
2385
|
+
}
|
|
2386
|
+
}
|
|
2387
|
+
for (const { tsKey, value } of fieldsSelection) {
|
|
2388
|
+
selection.push({
|
|
2389
|
+
dbKey: is(value, SQL.Aliased) ? value.fieldAlias : tableConfig.columns[tsKey].name,
|
|
2390
|
+
tsKey,
|
|
2391
|
+
field: is(value, Column) ? aliasedTableColumn(value, tableAlias) : value,
|
|
2392
|
+
relationTableTsKey: undefined,
|
|
2393
|
+
isJson: false,
|
|
2394
|
+
selection: []
|
|
2395
|
+
});
|
|
2396
|
+
}
|
|
2397
|
+
let orderByOrig = typeof config.orderBy === "function" ? config.orderBy(aliasedColumns, getOrderByOperators()) : config.orderBy ?? [];
|
|
2398
|
+
if (!Array.isArray(orderByOrig)) {
|
|
2399
|
+
orderByOrig = [orderByOrig];
|
|
2400
|
+
}
|
|
2401
|
+
orderBy = orderByOrig.map((orderByValue) => {
|
|
2402
|
+
if (is(orderByValue, Column)) {
|
|
2403
|
+
return aliasedTableColumn(orderByValue, tableAlias);
|
|
2404
|
+
}
|
|
2405
|
+
return mapColumnsInSQLToAlias(orderByValue, tableAlias);
|
|
2406
|
+
});
|
|
2407
|
+
limit = config.limit;
|
|
2408
|
+
offset = config.offset;
|
|
2409
|
+
for (const {
|
|
2410
|
+
tsKey: selectedRelationTsKey,
|
|
2411
|
+
queryConfig: selectedRelationConfigValue,
|
|
2412
|
+
relation
|
|
2413
|
+
} of selectedRelations) {
|
|
2414
|
+
const normalizedRelation = normalizeRelation(schema, tableNamesMap, relation);
|
|
2415
|
+
const relationTableName = getTableUniqueName(relation.referencedTable);
|
|
2416
|
+
const relationTableTsName = tableNamesMap[relationTableName];
|
|
2417
|
+
const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
|
|
2418
|
+
const joinOn2 = and(...normalizedRelation.fields.map((field2, i) => eq(aliasedTableColumn(normalizedRelation.references[i], relationTableAlias), aliasedTableColumn(field2, tableAlias))));
|
|
2419
|
+
const builtRelation = this.buildRelationalQuery({
|
|
2420
|
+
fullSchema,
|
|
2421
|
+
schema,
|
|
2422
|
+
tableNamesMap,
|
|
2423
|
+
table: fullSchema[relationTableTsName],
|
|
2424
|
+
tableConfig: schema[relationTableTsName],
|
|
2425
|
+
queryConfig: is(relation, One) ? selectedRelationConfigValue === true ? { limit: 1 } : { ...selectedRelationConfigValue, limit: 1 } : selectedRelationConfigValue,
|
|
2426
|
+
tableAlias: relationTableAlias,
|
|
2427
|
+
joinOn: joinOn2,
|
|
2428
|
+
nestedQueryRelation: relation
|
|
2429
|
+
});
|
|
2430
|
+
const field = sql`(${builtRelation.sql})`.as(selectedRelationTsKey);
|
|
2431
|
+
selection.push({
|
|
2432
|
+
dbKey: selectedRelationTsKey,
|
|
2433
|
+
tsKey: selectedRelationTsKey,
|
|
2434
|
+
field,
|
|
2435
|
+
relationTableTsKey: relationTableTsName,
|
|
2436
|
+
isJson: true,
|
|
2437
|
+
selection: builtRelation.selection
|
|
2438
|
+
});
|
|
2439
|
+
}
|
|
2440
|
+
}
|
|
2441
|
+
if (selection.length === 0) {
|
|
2442
|
+
throw new DrizzleError({
|
|
2443
|
+
message: `No fields selected for table "${tableConfig.tsName}" ("${tableAlias}"). 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.`
|
|
2444
|
+
});
|
|
2445
|
+
}
|
|
2446
|
+
let result;
|
|
2447
|
+
where = and(joinOn, where);
|
|
2448
|
+
if (nestedQueryRelation) {
|
|
2449
|
+
let field = sql`json_array(${sql.join(selection.map(({ field: field2 }) => is(field2, SQLiteColumn) ? sql.identifier(this.casing.getColumnCasing(field2)) : is(field2, SQL.Aliased) ? field2.sql : field2), sql`, `)})`;
|
|
2450
|
+
if (is(nestedQueryRelation, Many)) {
|
|
2451
|
+
field = sql`coalesce(json_group_array(${field}), json_array())`;
|
|
2452
|
+
}
|
|
2453
|
+
const nestedSelection = [{
|
|
2454
|
+
dbKey: "data",
|
|
2455
|
+
tsKey: "data",
|
|
2456
|
+
field: field.as("data"),
|
|
2457
|
+
isJson: true,
|
|
2458
|
+
relationTableTsKey: tableConfig.tsName,
|
|
2459
|
+
selection
|
|
2460
|
+
}];
|
|
2461
|
+
const needsSubquery = limit !== undefined || offset !== undefined || orderBy.length > 0;
|
|
2462
|
+
if (needsSubquery) {
|
|
2463
|
+
result = this.buildSelectQuery({
|
|
2464
|
+
table: aliasedTable(table, tableAlias),
|
|
2465
|
+
fields: {},
|
|
2466
|
+
fieldsFlat: [
|
|
2467
|
+
{
|
|
2468
|
+
path: [],
|
|
2469
|
+
field: sql.raw("*")
|
|
2470
|
+
}
|
|
2471
|
+
],
|
|
2472
|
+
where,
|
|
2473
|
+
limit,
|
|
2474
|
+
offset,
|
|
2475
|
+
orderBy,
|
|
2476
|
+
setOperators: []
|
|
2477
|
+
});
|
|
2478
|
+
where = undefined;
|
|
2479
|
+
limit = undefined;
|
|
2480
|
+
offset = undefined;
|
|
2481
|
+
orderBy = undefined;
|
|
2482
|
+
} else {
|
|
2483
|
+
result = aliasedTable(table, tableAlias);
|
|
2484
|
+
}
|
|
2485
|
+
result = this.buildSelectQuery({
|
|
2486
|
+
table: is(result, SQLiteTable) ? result : new Subquery(result, {}, tableAlias),
|
|
2487
|
+
fields: {},
|
|
2488
|
+
fieldsFlat: nestedSelection.map(({ field: field2 }) => ({
|
|
2489
|
+
path: [],
|
|
2490
|
+
field: is(field2, Column) ? aliasedTableColumn(field2, tableAlias) : field2
|
|
2491
|
+
})),
|
|
2492
|
+
joins,
|
|
2493
|
+
where,
|
|
2494
|
+
limit,
|
|
2495
|
+
offset,
|
|
2496
|
+
orderBy,
|
|
2497
|
+
setOperators: []
|
|
2498
|
+
});
|
|
2499
|
+
} else {
|
|
2500
|
+
result = this.buildSelectQuery({
|
|
2501
|
+
table: aliasedTable(table, tableAlias),
|
|
2502
|
+
fields: {},
|
|
2503
|
+
fieldsFlat: selection.map(({ field }) => ({
|
|
2504
|
+
path: [],
|
|
2505
|
+
field: is(field, Column) ? aliasedTableColumn(field, tableAlias) : field
|
|
2506
|
+
})),
|
|
2507
|
+
joins,
|
|
2508
|
+
where,
|
|
2509
|
+
limit,
|
|
2510
|
+
offset,
|
|
2511
|
+
orderBy,
|
|
2512
|
+
setOperators: []
|
|
2513
|
+
});
|
|
2514
|
+
}
|
|
2515
|
+
return {
|
|
2516
|
+
tableTsKey: tableConfig.tsName,
|
|
2517
|
+
sql: result,
|
|
2518
|
+
selection
|
|
2519
|
+
};
|
|
2520
|
+
}
|
|
2521
|
+
}
|
|
2522
|
+
|
|
2523
|
+
class SQLiteSyncDialect extends SQLiteDialect {
|
|
2524
|
+
static [entityKind] = "SQLiteSyncDialect";
|
|
2525
|
+
migrate(migrations, session, config) {
|
|
2526
|
+
const migrationsTable = config === undefined ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
|
|
2527
|
+
const migrationTableCreate = sql`
|
|
2528
|
+
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
|
|
2529
|
+
id SERIAL PRIMARY KEY,
|
|
2530
|
+
hash text NOT NULL,
|
|
2531
|
+
created_at numeric
|
|
2532
|
+
)
|
|
2533
|
+
`;
|
|
2534
|
+
session.run(migrationTableCreate);
|
|
2535
|
+
const dbMigrations = session.values(sql`SELECT id, hash, created_at FROM ${sql.identifier(migrationsTable)} ORDER BY created_at DESC LIMIT 1`);
|
|
2536
|
+
const lastDbMigration = dbMigrations[0] ?? undefined;
|
|
2537
|
+
session.run(sql`BEGIN`);
|
|
2538
|
+
try {
|
|
2539
|
+
for (const migration of migrations) {
|
|
2540
|
+
if (!lastDbMigration || Number(lastDbMigration[2]) < migration.folderMillis) {
|
|
2541
|
+
for (const stmt of migration.sql) {
|
|
2542
|
+
session.run(sql.raw(stmt));
|
|
2543
|
+
}
|
|
2544
|
+
session.run(sql`INSERT INTO ${sql.identifier(migrationsTable)} ("hash", "created_at") VALUES(${migration.hash}, ${migration.folderMillis})`);
|
|
2545
|
+
}
|
|
2546
|
+
}
|
|
2547
|
+
session.run(sql`COMMIT`);
|
|
2548
|
+
} catch (e) {
|
|
2549
|
+
session.run(sql`ROLLBACK`);
|
|
2550
|
+
throw e;
|
|
2551
|
+
}
|
|
2552
|
+
}
|
|
2553
|
+
}
|
|
2554
|
+
|
|
2555
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/query-builders/query-builder.js
|
|
2556
|
+
class TypedQueryBuilder {
|
|
2557
|
+
static [entityKind] = "TypedQueryBuilder";
|
|
2558
|
+
getSelectedFields() {
|
|
2559
|
+
return this._.selectedFields;
|
|
2560
|
+
}
|
|
2561
|
+
}
|
|
2562
|
+
|
|
2563
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/query-builders/select.js
|
|
2564
|
+
class SQLiteSelectBuilder {
|
|
2565
|
+
static [entityKind] = "SQLiteSelectBuilder";
|
|
2566
|
+
fields;
|
|
2567
|
+
session;
|
|
2568
|
+
dialect;
|
|
2569
|
+
withList;
|
|
2570
|
+
distinct;
|
|
2571
|
+
constructor(config) {
|
|
2572
|
+
this.fields = config.fields;
|
|
2573
|
+
this.session = config.session;
|
|
2574
|
+
this.dialect = config.dialect;
|
|
2575
|
+
this.withList = config.withList;
|
|
2576
|
+
this.distinct = config.distinct;
|
|
2577
|
+
}
|
|
2578
|
+
from(source) {
|
|
2579
|
+
const isPartialSelect = !!this.fields;
|
|
2580
|
+
let fields;
|
|
2581
|
+
if (this.fields) {
|
|
2582
|
+
fields = this.fields;
|
|
2583
|
+
} else if (is(source, Subquery)) {
|
|
2584
|
+
fields = Object.fromEntries(Object.keys(source._.selectedFields).map((key) => [key, source[key]]));
|
|
2585
|
+
} else if (is(source, SQLiteViewBase)) {
|
|
2586
|
+
fields = source[ViewBaseConfig].selectedFields;
|
|
2587
|
+
} else if (is(source, SQL)) {
|
|
2588
|
+
fields = {};
|
|
2589
|
+
} else {
|
|
2590
|
+
fields = getTableColumns(source);
|
|
2591
|
+
}
|
|
2592
|
+
return new SQLiteSelectBase({
|
|
2593
|
+
table: source,
|
|
2594
|
+
fields,
|
|
2595
|
+
isPartialSelect,
|
|
2596
|
+
session: this.session,
|
|
2597
|
+
dialect: this.dialect,
|
|
2598
|
+
withList: this.withList,
|
|
2599
|
+
distinct: this.distinct
|
|
2600
|
+
});
|
|
2601
|
+
}
|
|
2602
|
+
}
|
|
2603
|
+
|
|
2604
|
+
class SQLiteSelectQueryBuilderBase extends TypedQueryBuilder {
|
|
2605
|
+
static [entityKind] = "SQLiteSelectQueryBuilder";
|
|
2606
|
+
_;
|
|
2607
|
+
config;
|
|
2608
|
+
joinsNotNullableMap;
|
|
2609
|
+
tableName;
|
|
2610
|
+
isPartialSelect;
|
|
2611
|
+
session;
|
|
2612
|
+
dialect;
|
|
2613
|
+
constructor({ table, fields, isPartialSelect, session, dialect, withList, distinct }) {
|
|
2614
|
+
super();
|
|
2615
|
+
this.config = {
|
|
2616
|
+
withList,
|
|
2617
|
+
table,
|
|
2618
|
+
fields: { ...fields },
|
|
2619
|
+
distinct,
|
|
2620
|
+
setOperators: []
|
|
2621
|
+
};
|
|
2622
|
+
this.isPartialSelect = isPartialSelect;
|
|
2623
|
+
this.session = session;
|
|
2624
|
+
this.dialect = dialect;
|
|
2625
|
+
this._ = {
|
|
2626
|
+
selectedFields: fields
|
|
2627
|
+
};
|
|
2628
|
+
this.tableName = getTableLikeName(table);
|
|
2629
|
+
this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
|
|
2630
|
+
}
|
|
2631
|
+
createJoin(joinType) {
|
|
2632
|
+
return (table, on) => {
|
|
2633
|
+
const baseTableName = this.tableName;
|
|
2634
|
+
const tableName = getTableLikeName(table);
|
|
2635
|
+
if (typeof tableName === "string" && this.config.joins?.some((join) => join.alias === tableName)) {
|
|
2636
|
+
throw new Error(`Alias "${tableName}" is already used in this query`);
|
|
2637
|
+
}
|
|
2638
|
+
if (!this.isPartialSelect) {
|
|
2639
|
+
if (Object.keys(this.joinsNotNullableMap).length === 1 && typeof baseTableName === "string") {
|
|
2640
|
+
this.config.fields = {
|
|
2641
|
+
[baseTableName]: this.config.fields
|
|
2642
|
+
};
|
|
2643
|
+
}
|
|
2644
|
+
if (typeof tableName === "string" && !is(table, SQL)) {
|
|
2645
|
+
const selection = is(table, Subquery) ? table._.selectedFields : is(table, View) ? table[ViewBaseConfig].selectedFields : table[Table.Symbol.Columns];
|
|
2646
|
+
this.config.fields[tableName] = selection;
|
|
2647
|
+
}
|
|
2648
|
+
}
|
|
2649
|
+
if (typeof on === "function") {
|
|
2650
|
+
on = on(new Proxy(this.config.fields, new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })));
|
|
2651
|
+
}
|
|
2652
|
+
if (!this.config.joins) {
|
|
2653
|
+
this.config.joins = [];
|
|
2654
|
+
}
|
|
2655
|
+
this.config.joins.push({ on, table, joinType, alias: tableName });
|
|
2656
|
+
if (typeof tableName === "string") {
|
|
2657
|
+
switch (joinType) {
|
|
2658
|
+
case "left": {
|
|
2659
|
+
this.joinsNotNullableMap[tableName] = false;
|
|
2660
|
+
break;
|
|
2661
|
+
}
|
|
2662
|
+
case "right": {
|
|
2663
|
+
this.joinsNotNullableMap = Object.fromEntries(Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false]));
|
|
2664
|
+
this.joinsNotNullableMap[tableName] = true;
|
|
2665
|
+
break;
|
|
2666
|
+
}
|
|
2667
|
+
case "inner": {
|
|
2668
|
+
this.joinsNotNullableMap[tableName] = true;
|
|
2669
|
+
break;
|
|
2670
|
+
}
|
|
2671
|
+
case "full": {
|
|
2672
|
+
this.joinsNotNullableMap = Object.fromEntries(Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false]));
|
|
2673
|
+
this.joinsNotNullableMap[tableName] = false;
|
|
2674
|
+
break;
|
|
2675
|
+
}
|
|
2676
|
+
}
|
|
2677
|
+
}
|
|
2678
|
+
return this;
|
|
2679
|
+
};
|
|
2680
|
+
}
|
|
2681
|
+
leftJoin = this.createJoin("left");
|
|
2682
|
+
rightJoin = this.createJoin("right");
|
|
2683
|
+
innerJoin = this.createJoin("inner");
|
|
2684
|
+
fullJoin = this.createJoin("full");
|
|
2685
|
+
createSetOperator(type, isAll) {
|
|
2686
|
+
return (rightSelection) => {
|
|
2687
|
+
const rightSelect = typeof rightSelection === "function" ? rightSelection(getSQLiteSetOperators()) : rightSelection;
|
|
2688
|
+
if (!haveSameKeys(this.getSelectedFields(), rightSelect.getSelectedFields())) {
|
|
2689
|
+
throw new Error("Set operator error (union / intersect / except): selected fields are not the same or are in a different order");
|
|
2690
|
+
}
|
|
2691
|
+
this.config.setOperators.push({ type, isAll, rightSelect });
|
|
2692
|
+
return this;
|
|
2693
|
+
};
|
|
2694
|
+
}
|
|
2695
|
+
union = this.createSetOperator("union", false);
|
|
2696
|
+
unionAll = this.createSetOperator("union", true);
|
|
2697
|
+
intersect = this.createSetOperator("intersect", false);
|
|
2698
|
+
except = this.createSetOperator("except", false);
|
|
2699
|
+
addSetOperators(setOperators) {
|
|
2700
|
+
this.config.setOperators.push(...setOperators);
|
|
2701
|
+
return this;
|
|
2702
|
+
}
|
|
2703
|
+
where(where) {
|
|
2704
|
+
if (typeof where === "function") {
|
|
2705
|
+
where = where(new Proxy(this.config.fields, new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })));
|
|
2706
|
+
}
|
|
2707
|
+
this.config.where = where;
|
|
2708
|
+
return this;
|
|
2709
|
+
}
|
|
2710
|
+
having(having) {
|
|
2711
|
+
if (typeof having === "function") {
|
|
2712
|
+
having = having(new Proxy(this.config.fields, new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })));
|
|
2713
|
+
}
|
|
2714
|
+
this.config.having = having;
|
|
2715
|
+
return this;
|
|
2716
|
+
}
|
|
2717
|
+
groupBy(...columns) {
|
|
2718
|
+
if (typeof columns[0] === "function") {
|
|
2719
|
+
const groupBy = columns[0](new Proxy(this.config.fields, new SelectionProxyHandler({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" })));
|
|
2720
|
+
this.config.groupBy = Array.isArray(groupBy) ? groupBy : [groupBy];
|
|
2721
|
+
} else {
|
|
2722
|
+
this.config.groupBy = columns;
|
|
2723
|
+
}
|
|
2724
|
+
return this;
|
|
2725
|
+
}
|
|
2726
|
+
orderBy(...columns) {
|
|
2727
|
+
if (typeof columns[0] === "function") {
|
|
2728
|
+
const orderBy = columns[0](new Proxy(this.config.fields, new SelectionProxyHandler({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" })));
|
|
2729
|
+
const orderByArray = Array.isArray(orderBy) ? orderBy : [orderBy];
|
|
2730
|
+
if (this.config.setOperators.length > 0) {
|
|
2731
|
+
this.config.setOperators.at(-1).orderBy = orderByArray;
|
|
2732
|
+
} else {
|
|
2733
|
+
this.config.orderBy = orderByArray;
|
|
2734
|
+
}
|
|
2735
|
+
} else {
|
|
2736
|
+
const orderByArray = columns;
|
|
2737
|
+
if (this.config.setOperators.length > 0) {
|
|
2738
|
+
this.config.setOperators.at(-1).orderBy = orderByArray;
|
|
2739
|
+
} else {
|
|
2740
|
+
this.config.orderBy = orderByArray;
|
|
2741
|
+
}
|
|
2742
|
+
}
|
|
2743
|
+
return this;
|
|
2744
|
+
}
|
|
2745
|
+
limit(limit) {
|
|
2746
|
+
if (this.config.setOperators.length > 0) {
|
|
2747
|
+
this.config.setOperators.at(-1).limit = limit;
|
|
2748
|
+
} else {
|
|
2749
|
+
this.config.limit = limit;
|
|
2750
|
+
}
|
|
2751
|
+
return this;
|
|
2752
|
+
}
|
|
2753
|
+
offset(offset) {
|
|
2754
|
+
if (this.config.setOperators.length > 0) {
|
|
2755
|
+
this.config.setOperators.at(-1).offset = offset;
|
|
2756
|
+
} else {
|
|
2757
|
+
this.config.offset = offset;
|
|
2758
|
+
}
|
|
2759
|
+
return this;
|
|
2760
|
+
}
|
|
2761
|
+
getSQL() {
|
|
2762
|
+
return this.dialect.buildSelectQuery(this.config);
|
|
2763
|
+
}
|
|
2764
|
+
toSQL() {
|
|
2765
|
+
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
|
|
2766
|
+
return rest;
|
|
2767
|
+
}
|
|
2768
|
+
as(alias) {
|
|
2769
|
+
return new Proxy(new Subquery(this.getSQL(), this.config.fields, alias), new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" }));
|
|
2770
|
+
}
|
|
2771
|
+
getSelectedFields() {
|
|
2772
|
+
return new Proxy(this.config.fields, new SelectionProxyHandler({ alias: this.tableName, sqlAliasedBehavior: "alias", sqlBehavior: "error" }));
|
|
2773
|
+
}
|
|
2774
|
+
$dynamic() {
|
|
2775
|
+
return this;
|
|
2776
|
+
}
|
|
2777
|
+
}
|
|
2778
|
+
|
|
2779
|
+
class SQLiteSelectBase extends SQLiteSelectQueryBuilderBase {
|
|
2780
|
+
static [entityKind] = "SQLiteSelect";
|
|
2781
|
+
_prepare(isOneTimeQuery = true) {
|
|
2782
|
+
if (!this.session) {
|
|
2783
|
+
throw new Error("Cannot execute a query on a query builder. Please use a database instance instead.");
|
|
2784
|
+
}
|
|
2785
|
+
const fieldsList = orderSelectedFields(this.config.fields);
|
|
2786
|
+
const query = this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](this.dialect.sqlToQuery(this.getSQL()), fieldsList, "all", true);
|
|
2787
|
+
query.joinsNotNullableMap = this.joinsNotNullableMap;
|
|
2788
|
+
return query;
|
|
2789
|
+
}
|
|
2790
|
+
prepare() {
|
|
2791
|
+
return this._prepare(false);
|
|
2792
|
+
}
|
|
2793
|
+
run = (placeholderValues) => {
|
|
2794
|
+
return this._prepare().run(placeholderValues);
|
|
2795
|
+
};
|
|
2796
|
+
all = (placeholderValues) => {
|
|
2797
|
+
return this._prepare().all(placeholderValues);
|
|
2798
|
+
};
|
|
2799
|
+
get = (placeholderValues) => {
|
|
2800
|
+
return this._prepare().get(placeholderValues);
|
|
2801
|
+
};
|
|
2802
|
+
values = (placeholderValues) => {
|
|
2803
|
+
return this._prepare().values(placeholderValues);
|
|
2804
|
+
};
|
|
2805
|
+
async execute() {
|
|
2806
|
+
return this.all();
|
|
2807
|
+
}
|
|
2808
|
+
}
|
|
2809
|
+
applyMixins(SQLiteSelectBase, [QueryPromise]);
|
|
2810
|
+
function createSetOperator(type, isAll) {
|
|
2811
|
+
return (leftSelect, rightSelect, ...restSelects) => {
|
|
2812
|
+
const setOperators = [rightSelect, ...restSelects].map((select) => ({
|
|
2813
|
+
type,
|
|
2814
|
+
isAll,
|
|
2815
|
+
rightSelect: select
|
|
2816
|
+
}));
|
|
2817
|
+
for (const setOperator of setOperators) {
|
|
2818
|
+
if (!haveSameKeys(leftSelect.getSelectedFields(), setOperator.rightSelect.getSelectedFields())) {
|
|
2819
|
+
throw new Error("Set operator error (union / intersect / except): selected fields are not the same or are in a different order");
|
|
2820
|
+
}
|
|
2821
|
+
}
|
|
2822
|
+
return leftSelect.addSetOperators(setOperators);
|
|
2823
|
+
};
|
|
2824
|
+
}
|
|
2825
|
+
var getSQLiteSetOperators = () => ({
|
|
2826
|
+
union,
|
|
2827
|
+
unionAll,
|
|
2828
|
+
intersect,
|
|
2829
|
+
except
|
|
2830
|
+
});
|
|
2831
|
+
var union = createSetOperator("union", false);
|
|
2832
|
+
var unionAll = createSetOperator("union", true);
|
|
2833
|
+
var intersect = createSetOperator("intersect", false);
|
|
2834
|
+
var except = createSetOperator("except", false);
|
|
2835
|
+
|
|
2836
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/query-builders/query-builder.js
|
|
2837
|
+
class QueryBuilder {
|
|
2838
|
+
static [entityKind] = "SQLiteQueryBuilder";
|
|
2839
|
+
dialect;
|
|
2840
|
+
dialectConfig;
|
|
2841
|
+
constructor(dialect) {
|
|
2842
|
+
this.dialect = is(dialect, SQLiteDialect) ? dialect : undefined;
|
|
2843
|
+
this.dialectConfig = is(dialect, SQLiteDialect) ? undefined : dialect;
|
|
2844
|
+
}
|
|
2845
|
+
$with(alias) {
|
|
2846
|
+
const queryBuilder = this;
|
|
2847
|
+
return {
|
|
2848
|
+
as(qb) {
|
|
2849
|
+
if (typeof qb === "function") {
|
|
2850
|
+
qb = qb(queryBuilder);
|
|
2851
|
+
}
|
|
2852
|
+
return new Proxy(new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true), new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" }));
|
|
2853
|
+
}
|
|
2854
|
+
};
|
|
2855
|
+
}
|
|
2856
|
+
with(...queries) {
|
|
2857
|
+
const self = this;
|
|
2858
|
+
function select(fields) {
|
|
2859
|
+
return new SQLiteSelectBuilder({
|
|
2860
|
+
fields: fields ?? undefined,
|
|
2861
|
+
session: undefined,
|
|
2862
|
+
dialect: self.getDialect(),
|
|
2863
|
+
withList: queries
|
|
2864
|
+
});
|
|
2865
|
+
}
|
|
2866
|
+
function selectDistinct(fields) {
|
|
2867
|
+
return new SQLiteSelectBuilder({
|
|
2868
|
+
fields: fields ?? undefined,
|
|
2869
|
+
session: undefined,
|
|
2870
|
+
dialect: self.getDialect(),
|
|
2871
|
+
withList: queries,
|
|
2872
|
+
distinct: true
|
|
2873
|
+
});
|
|
2874
|
+
}
|
|
2875
|
+
return { select, selectDistinct };
|
|
2876
|
+
}
|
|
2877
|
+
select(fields) {
|
|
2878
|
+
return new SQLiteSelectBuilder({ fields: fields ?? undefined, session: undefined, dialect: this.getDialect() });
|
|
2879
|
+
}
|
|
2880
|
+
selectDistinct(fields) {
|
|
2881
|
+
return new SQLiteSelectBuilder({
|
|
2882
|
+
fields: fields ?? undefined,
|
|
2883
|
+
session: undefined,
|
|
2884
|
+
dialect: this.getDialect(),
|
|
2885
|
+
distinct: true
|
|
2886
|
+
});
|
|
2887
|
+
}
|
|
2888
|
+
getDialect() {
|
|
2889
|
+
if (!this.dialect) {
|
|
2890
|
+
this.dialect = new SQLiteSyncDialect(this.dialectConfig);
|
|
2891
|
+
}
|
|
2892
|
+
return this.dialect;
|
|
2893
|
+
}
|
|
2894
|
+
}
|
|
2895
|
+
|
|
2896
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/query-builders/insert.js
|
|
2897
|
+
class SQLiteInsertBuilder {
|
|
2898
|
+
constructor(table, session, dialect, withList) {
|
|
2899
|
+
this.table = table;
|
|
2900
|
+
this.session = session;
|
|
2901
|
+
this.dialect = dialect;
|
|
2902
|
+
this.withList = withList;
|
|
2903
|
+
}
|
|
2904
|
+
static [entityKind] = "SQLiteInsertBuilder";
|
|
2905
|
+
values(values) {
|
|
2906
|
+
values = Array.isArray(values) ? values : [values];
|
|
2907
|
+
if (values.length === 0) {
|
|
2908
|
+
throw new Error("values() must be called with at least one value");
|
|
2909
|
+
}
|
|
2910
|
+
const mappedValues = values.map((entry) => {
|
|
2911
|
+
const result = {};
|
|
2912
|
+
const cols = this.table[Table.Symbol.Columns];
|
|
2913
|
+
for (const colKey of Object.keys(entry)) {
|
|
2914
|
+
const colValue = entry[colKey];
|
|
2915
|
+
result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
|
|
2916
|
+
}
|
|
2917
|
+
return result;
|
|
2918
|
+
});
|
|
2919
|
+
return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
|
|
2920
|
+
}
|
|
2921
|
+
select(selectQuery) {
|
|
2922
|
+
const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder) : selectQuery;
|
|
2923
|
+
if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
|
|
2924
|
+
throw new Error("Insert select error: selected fields are not the same or are in a different order compared to the table definition");
|
|
2925
|
+
}
|
|
2926
|
+
return new SQLiteInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
|
|
2927
|
+
}
|
|
2928
|
+
}
|
|
2929
|
+
|
|
2930
|
+
class SQLiteInsertBase extends QueryPromise {
|
|
2931
|
+
constructor(table, values, session, dialect, withList, select) {
|
|
2932
|
+
super();
|
|
2933
|
+
this.session = session;
|
|
2934
|
+
this.dialect = dialect;
|
|
2935
|
+
this.config = { table, values, withList, select };
|
|
2936
|
+
}
|
|
2937
|
+
static [entityKind] = "SQLiteInsert";
|
|
2938
|
+
config;
|
|
2939
|
+
returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
|
|
2940
|
+
this.config.returning = orderSelectedFields(fields);
|
|
2941
|
+
return this;
|
|
2942
|
+
}
|
|
2943
|
+
onConflictDoNothing(config = {}) {
|
|
2944
|
+
if (config.target === undefined) {
|
|
2945
|
+
this.config.onConflict = sql`do nothing`;
|
|
2946
|
+
} else {
|
|
2947
|
+
const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
|
|
2948
|
+
const whereSql = config.where ? sql` where ${config.where}` : sql``;
|
|
2949
|
+
this.config.onConflict = sql`${targetSql} do nothing${whereSql}`;
|
|
2950
|
+
}
|
|
2951
|
+
return this;
|
|
2952
|
+
}
|
|
2953
|
+
onConflictDoUpdate(config) {
|
|
2954
|
+
if (config.where && (config.targetWhere || config.setWhere)) {
|
|
2955
|
+
throw new Error('You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.');
|
|
2956
|
+
}
|
|
2957
|
+
const whereSql = config.where ? sql` where ${config.where}` : undefined;
|
|
2958
|
+
const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : undefined;
|
|
2959
|
+
const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : undefined;
|
|
2960
|
+
const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
|
|
2961
|
+
const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
|
|
2962
|
+
this.config.onConflict = sql`${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
|
|
2963
|
+
return this;
|
|
2964
|
+
}
|
|
2965
|
+
getSQL() {
|
|
2966
|
+
return this.dialect.buildInsertQuery(this.config);
|
|
2967
|
+
}
|
|
2968
|
+
toSQL() {
|
|
2969
|
+
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
|
|
2970
|
+
return rest;
|
|
2971
|
+
}
|
|
2972
|
+
_prepare(isOneTimeQuery = true) {
|
|
2973
|
+
return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](this.dialect.sqlToQuery(this.getSQL()), this.config.returning, this.config.returning ? "all" : "run", true);
|
|
2974
|
+
}
|
|
2975
|
+
prepare() {
|
|
2976
|
+
return this._prepare(false);
|
|
2977
|
+
}
|
|
2978
|
+
run = (placeholderValues) => {
|
|
2979
|
+
return this._prepare().run(placeholderValues);
|
|
2980
|
+
};
|
|
2981
|
+
all = (placeholderValues) => {
|
|
2982
|
+
return this._prepare().all(placeholderValues);
|
|
2983
|
+
};
|
|
2984
|
+
get = (placeholderValues) => {
|
|
2985
|
+
return this._prepare().get(placeholderValues);
|
|
2986
|
+
};
|
|
2987
|
+
values = (placeholderValues) => {
|
|
2988
|
+
return this._prepare().values(placeholderValues);
|
|
2989
|
+
};
|
|
2990
|
+
async execute() {
|
|
2991
|
+
return this.config.returning ? this.all() : this.run();
|
|
2992
|
+
}
|
|
2993
|
+
$dynamic() {
|
|
2994
|
+
return this;
|
|
2995
|
+
}
|
|
2996
|
+
}
|
|
2997
|
+
|
|
2998
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/query-builders/update.js
|
|
2999
|
+
class SQLiteUpdateBuilder {
|
|
3000
|
+
constructor(table, session, dialect, withList) {
|
|
3001
|
+
this.table = table;
|
|
3002
|
+
this.session = session;
|
|
3003
|
+
this.dialect = dialect;
|
|
3004
|
+
this.withList = withList;
|
|
3005
|
+
}
|
|
3006
|
+
static [entityKind] = "SQLiteUpdateBuilder";
|
|
3007
|
+
set(values) {
|
|
3008
|
+
return new SQLiteUpdateBase(this.table, mapUpdateSet(this.table, values), this.session, this.dialect, this.withList);
|
|
3009
|
+
}
|
|
3010
|
+
}
|
|
3011
|
+
|
|
3012
|
+
class SQLiteUpdateBase extends QueryPromise {
|
|
3013
|
+
constructor(table, set, session, dialect, withList) {
|
|
3014
|
+
super();
|
|
3015
|
+
this.session = session;
|
|
3016
|
+
this.dialect = dialect;
|
|
3017
|
+
this.config = { set, table, withList, joins: [] };
|
|
3018
|
+
}
|
|
3019
|
+
static [entityKind] = "SQLiteUpdate";
|
|
3020
|
+
config;
|
|
3021
|
+
from(source) {
|
|
3022
|
+
this.config.from = source;
|
|
3023
|
+
return this;
|
|
3024
|
+
}
|
|
3025
|
+
createJoin(joinType) {
|
|
3026
|
+
return (table, on) => {
|
|
3027
|
+
const tableName = getTableLikeName(table);
|
|
3028
|
+
if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
|
|
3029
|
+
throw new Error(`Alias "${tableName}" is already used in this query`);
|
|
3030
|
+
}
|
|
3031
|
+
if (typeof on === "function") {
|
|
3032
|
+
const from = this.config.from ? is(table, SQLiteTable) ? table[Table.Symbol.Columns] : is(table, Subquery) ? table._.selectedFields : is(table, SQLiteViewBase) ? table[ViewBaseConfig].selectedFields : undefined : undefined;
|
|
3033
|
+
on = on(new Proxy(this.config.table[Table.Symbol.Columns], new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })), from && new Proxy(from, new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })));
|
|
3034
|
+
}
|
|
3035
|
+
this.config.joins.push({ on, table, joinType, alias: tableName });
|
|
3036
|
+
return this;
|
|
3037
|
+
};
|
|
3038
|
+
}
|
|
3039
|
+
leftJoin = this.createJoin("left");
|
|
3040
|
+
rightJoin = this.createJoin("right");
|
|
3041
|
+
innerJoin = this.createJoin("inner");
|
|
3042
|
+
fullJoin = this.createJoin("full");
|
|
3043
|
+
where(where) {
|
|
3044
|
+
this.config.where = where;
|
|
3045
|
+
return this;
|
|
3046
|
+
}
|
|
3047
|
+
orderBy(...columns) {
|
|
3048
|
+
if (typeof columns[0] === "function") {
|
|
3049
|
+
const orderBy = columns[0](new Proxy(this.config.table[Table.Symbol.Columns], new SelectionProxyHandler({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" })));
|
|
3050
|
+
const orderByArray = Array.isArray(orderBy) ? orderBy : [orderBy];
|
|
3051
|
+
this.config.orderBy = orderByArray;
|
|
3052
|
+
} else {
|
|
3053
|
+
const orderByArray = columns;
|
|
3054
|
+
this.config.orderBy = orderByArray;
|
|
3055
|
+
}
|
|
3056
|
+
return this;
|
|
3057
|
+
}
|
|
3058
|
+
limit(limit) {
|
|
3059
|
+
this.config.limit = limit;
|
|
3060
|
+
return this;
|
|
3061
|
+
}
|
|
3062
|
+
returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
|
|
3063
|
+
this.config.returning = orderSelectedFields(fields);
|
|
3064
|
+
return this;
|
|
3065
|
+
}
|
|
3066
|
+
getSQL() {
|
|
3067
|
+
return this.dialect.buildUpdateQuery(this.config);
|
|
3068
|
+
}
|
|
3069
|
+
toSQL() {
|
|
3070
|
+
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
|
|
3071
|
+
return rest;
|
|
3072
|
+
}
|
|
3073
|
+
_prepare(isOneTimeQuery = true) {
|
|
3074
|
+
return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](this.dialect.sqlToQuery(this.getSQL()), this.config.returning, this.config.returning ? "all" : "run", true);
|
|
3075
|
+
}
|
|
3076
|
+
prepare() {
|
|
3077
|
+
return this._prepare(false);
|
|
3078
|
+
}
|
|
3079
|
+
run = (placeholderValues) => {
|
|
3080
|
+
return this._prepare().run(placeholderValues);
|
|
3081
|
+
};
|
|
3082
|
+
all = (placeholderValues) => {
|
|
3083
|
+
return this._prepare().all(placeholderValues);
|
|
3084
|
+
};
|
|
3085
|
+
get = (placeholderValues) => {
|
|
3086
|
+
return this._prepare().get(placeholderValues);
|
|
3087
|
+
};
|
|
3088
|
+
values = (placeholderValues) => {
|
|
3089
|
+
return this._prepare().values(placeholderValues);
|
|
3090
|
+
};
|
|
3091
|
+
async execute() {
|
|
3092
|
+
return this.config.returning ? this.all() : this.run();
|
|
3093
|
+
}
|
|
3094
|
+
$dynamic() {
|
|
3095
|
+
return this;
|
|
3096
|
+
}
|
|
3097
|
+
}
|
|
3098
|
+
|
|
3099
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/query-builders/count.js
|
|
3100
|
+
class SQLiteCountBuilder extends SQL {
|
|
3101
|
+
constructor(params) {
|
|
3102
|
+
super(SQLiteCountBuilder.buildEmbeddedCount(params.source, params.filters).queryChunks);
|
|
3103
|
+
this.params = params;
|
|
3104
|
+
this.session = params.session;
|
|
3105
|
+
this.sql = SQLiteCountBuilder.buildCount(params.source, params.filters);
|
|
3106
|
+
}
|
|
3107
|
+
sql;
|
|
3108
|
+
static [entityKind] = "SQLiteCountBuilderAsync";
|
|
3109
|
+
[Symbol.toStringTag] = "SQLiteCountBuilderAsync";
|
|
3110
|
+
session;
|
|
3111
|
+
static buildEmbeddedCount(source, filters) {
|
|
3112
|
+
return sql`(select count(*) from ${source}${sql.raw(" where ").if(filters)}${filters})`;
|
|
3113
|
+
}
|
|
3114
|
+
static buildCount(source, filters) {
|
|
3115
|
+
return sql`select count(*) from ${source}${sql.raw(" where ").if(filters)}${filters}`;
|
|
3116
|
+
}
|
|
3117
|
+
then(onfulfilled, onrejected) {
|
|
3118
|
+
return Promise.resolve(this.session.count(this.sql)).then(onfulfilled, onrejected);
|
|
3119
|
+
}
|
|
3120
|
+
catch(onRejected) {
|
|
3121
|
+
return this.then(undefined, onRejected);
|
|
3122
|
+
}
|
|
3123
|
+
finally(onFinally) {
|
|
3124
|
+
return this.then((value) => {
|
|
3125
|
+
onFinally?.();
|
|
3126
|
+
return value;
|
|
3127
|
+
}, (reason) => {
|
|
3128
|
+
onFinally?.();
|
|
3129
|
+
throw reason;
|
|
3130
|
+
});
|
|
3131
|
+
}
|
|
3132
|
+
}
|
|
3133
|
+
|
|
3134
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/query-builders/query.js
|
|
3135
|
+
class RelationalQueryBuilder {
|
|
3136
|
+
constructor(mode, fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session) {
|
|
3137
|
+
this.mode = mode;
|
|
3138
|
+
this.fullSchema = fullSchema;
|
|
3139
|
+
this.schema = schema;
|
|
3140
|
+
this.tableNamesMap = tableNamesMap;
|
|
3141
|
+
this.table = table;
|
|
3142
|
+
this.tableConfig = tableConfig;
|
|
3143
|
+
this.dialect = dialect;
|
|
3144
|
+
this.session = session;
|
|
3145
|
+
}
|
|
3146
|
+
static [entityKind] = "SQLiteAsyncRelationalQueryBuilder";
|
|
3147
|
+
findMany(config) {
|
|
3148
|
+
return this.mode === "sync" ? new SQLiteSyncRelationalQuery(this.fullSchema, this.schema, this.tableNamesMap, this.table, this.tableConfig, this.dialect, this.session, config ? config : {}, "many") : new SQLiteRelationalQuery(this.fullSchema, this.schema, this.tableNamesMap, this.table, this.tableConfig, this.dialect, this.session, config ? config : {}, "many");
|
|
3149
|
+
}
|
|
3150
|
+
findFirst(config) {
|
|
3151
|
+
return this.mode === "sync" ? new SQLiteSyncRelationalQuery(this.fullSchema, this.schema, this.tableNamesMap, this.table, this.tableConfig, this.dialect, this.session, config ? { ...config, limit: 1 } : { limit: 1 }, "first") : new SQLiteRelationalQuery(this.fullSchema, this.schema, this.tableNamesMap, this.table, this.tableConfig, this.dialect, this.session, config ? { ...config, limit: 1 } : { limit: 1 }, "first");
|
|
3152
|
+
}
|
|
3153
|
+
}
|
|
3154
|
+
|
|
3155
|
+
class SQLiteRelationalQuery extends QueryPromise {
|
|
3156
|
+
constructor(fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session, config, mode) {
|
|
3157
|
+
super();
|
|
3158
|
+
this.fullSchema = fullSchema;
|
|
3159
|
+
this.schema = schema;
|
|
3160
|
+
this.tableNamesMap = tableNamesMap;
|
|
3161
|
+
this.table = table;
|
|
3162
|
+
this.tableConfig = tableConfig;
|
|
3163
|
+
this.dialect = dialect;
|
|
3164
|
+
this.session = session;
|
|
3165
|
+
this.config = config;
|
|
3166
|
+
this.mode = mode;
|
|
3167
|
+
}
|
|
3168
|
+
static [entityKind] = "SQLiteAsyncRelationalQuery";
|
|
3169
|
+
mode;
|
|
3170
|
+
getSQL() {
|
|
3171
|
+
return this.dialect.buildRelationalQuery({
|
|
3172
|
+
fullSchema: this.fullSchema,
|
|
3173
|
+
schema: this.schema,
|
|
3174
|
+
tableNamesMap: this.tableNamesMap,
|
|
3175
|
+
table: this.table,
|
|
3176
|
+
tableConfig: this.tableConfig,
|
|
3177
|
+
queryConfig: this.config,
|
|
3178
|
+
tableAlias: this.tableConfig.tsName
|
|
3179
|
+
}).sql;
|
|
3180
|
+
}
|
|
3181
|
+
_prepare(isOneTimeQuery = false) {
|
|
3182
|
+
const { query, builtQuery } = this._toSQL();
|
|
3183
|
+
return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](builtQuery, undefined, this.mode === "first" ? "get" : "all", true, (rawRows, mapColumnValue) => {
|
|
3184
|
+
const rows = rawRows.map((row) => mapRelationalRow(this.schema, this.tableConfig, row, query.selection, mapColumnValue));
|
|
3185
|
+
if (this.mode === "first") {
|
|
3186
|
+
return rows[0];
|
|
3187
|
+
}
|
|
3188
|
+
return rows;
|
|
3189
|
+
});
|
|
3190
|
+
}
|
|
3191
|
+
prepare() {
|
|
3192
|
+
return this._prepare(false);
|
|
3193
|
+
}
|
|
3194
|
+
_toSQL() {
|
|
3195
|
+
const query = this.dialect.buildRelationalQuery({
|
|
3196
|
+
fullSchema: this.fullSchema,
|
|
3197
|
+
schema: this.schema,
|
|
3198
|
+
tableNamesMap: this.tableNamesMap,
|
|
3199
|
+
table: this.table,
|
|
3200
|
+
tableConfig: this.tableConfig,
|
|
3201
|
+
queryConfig: this.config,
|
|
3202
|
+
tableAlias: this.tableConfig.tsName
|
|
3203
|
+
});
|
|
3204
|
+
const builtQuery = this.dialect.sqlToQuery(query.sql);
|
|
3205
|
+
return { query, builtQuery };
|
|
3206
|
+
}
|
|
3207
|
+
toSQL() {
|
|
3208
|
+
return this._toSQL().builtQuery;
|
|
3209
|
+
}
|
|
3210
|
+
executeRaw() {
|
|
3211
|
+
if (this.mode === "first") {
|
|
3212
|
+
return this._prepare(false).get();
|
|
3213
|
+
}
|
|
3214
|
+
return this._prepare(false).all();
|
|
3215
|
+
}
|
|
3216
|
+
async execute() {
|
|
3217
|
+
return this.executeRaw();
|
|
3218
|
+
}
|
|
3219
|
+
}
|
|
3220
|
+
|
|
3221
|
+
class SQLiteSyncRelationalQuery extends SQLiteRelationalQuery {
|
|
3222
|
+
static [entityKind] = "SQLiteSyncRelationalQuery";
|
|
3223
|
+
sync() {
|
|
3224
|
+
return this.executeRaw();
|
|
3225
|
+
}
|
|
3226
|
+
}
|
|
3227
|
+
|
|
3228
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/query-builders/raw.js
|
|
3229
|
+
class SQLiteRaw extends QueryPromise {
|
|
3230
|
+
constructor(execute, getSQL, action, dialect, mapBatchResult) {
|
|
3231
|
+
super();
|
|
3232
|
+
this.execute = execute;
|
|
3233
|
+
this.getSQL = getSQL;
|
|
3234
|
+
this.dialect = dialect;
|
|
3235
|
+
this.mapBatchResult = mapBatchResult;
|
|
3236
|
+
this.config = { action };
|
|
3237
|
+
}
|
|
3238
|
+
static [entityKind] = "SQLiteRaw";
|
|
3239
|
+
config;
|
|
3240
|
+
getQuery() {
|
|
3241
|
+
return { ...this.dialect.sqlToQuery(this.getSQL()), method: this.config.action };
|
|
3242
|
+
}
|
|
3243
|
+
mapResult(result, isFromBatch) {
|
|
3244
|
+
return isFromBatch ? this.mapBatchResult(result) : result;
|
|
3245
|
+
}
|
|
3246
|
+
_prepare() {
|
|
3247
|
+
return this;
|
|
3248
|
+
}
|
|
3249
|
+
isResponseInArrayMode() {
|
|
3250
|
+
return false;
|
|
3251
|
+
}
|
|
3252
|
+
}
|
|
3253
|
+
|
|
3254
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/db.js
|
|
3255
|
+
class BaseSQLiteDatabase {
|
|
3256
|
+
constructor(resultKind, dialect, session, schema) {
|
|
3257
|
+
this.resultKind = resultKind;
|
|
3258
|
+
this.dialect = dialect;
|
|
3259
|
+
this.session = session;
|
|
3260
|
+
this._ = schema ? {
|
|
3261
|
+
schema: schema.schema,
|
|
3262
|
+
fullSchema: schema.fullSchema,
|
|
3263
|
+
tableNamesMap: schema.tableNamesMap
|
|
3264
|
+
} : {
|
|
3265
|
+
schema: undefined,
|
|
3266
|
+
fullSchema: {},
|
|
3267
|
+
tableNamesMap: {}
|
|
3268
|
+
};
|
|
3269
|
+
this.query = {};
|
|
3270
|
+
const query = this.query;
|
|
3271
|
+
if (this._.schema) {
|
|
3272
|
+
for (const [tableName, columns] of Object.entries(this._.schema)) {
|
|
3273
|
+
query[tableName] = new RelationalQueryBuilder(resultKind, schema.fullSchema, this._.schema, this._.tableNamesMap, schema.fullSchema[tableName], columns, dialect, session);
|
|
3274
|
+
}
|
|
3275
|
+
}
|
|
3276
|
+
}
|
|
3277
|
+
static [entityKind] = "BaseSQLiteDatabase";
|
|
3278
|
+
query;
|
|
3279
|
+
$with(alias) {
|
|
3280
|
+
const self = this;
|
|
3281
|
+
return {
|
|
3282
|
+
as(qb) {
|
|
3283
|
+
if (typeof qb === "function") {
|
|
3284
|
+
qb = qb(new QueryBuilder(self.dialect));
|
|
3285
|
+
}
|
|
3286
|
+
return new Proxy(new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true), new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" }));
|
|
3287
|
+
}
|
|
3288
|
+
};
|
|
3289
|
+
}
|
|
3290
|
+
$count(source, filters) {
|
|
3291
|
+
return new SQLiteCountBuilder({ source, filters, session: this.session });
|
|
3292
|
+
}
|
|
3293
|
+
with(...queries) {
|
|
3294
|
+
const self = this;
|
|
3295
|
+
function select(fields) {
|
|
3296
|
+
return new SQLiteSelectBuilder({
|
|
3297
|
+
fields: fields ?? undefined,
|
|
3298
|
+
session: self.session,
|
|
3299
|
+
dialect: self.dialect,
|
|
3300
|
+
withList: queries
|
|
3301
|
+
});
|
|
3302
|
+
}
|
|
3303
|
+
function selectDistinct(fields) {
|
|
3304
|
+
return new SQLiteSelectBuilder({
|
|
3305
|
+
fields: fields ?? undefined,
|
|
3306
|
+
session: self.session,
|
|
3307
|
+
dialect: self.dialect,
|
|
3308
|
+
withList: queries,
|
|
3309
|
+
distinct: true
|
|
3310
|
+
});
|
|
3311
|
+
}
|
|
3312
|
+
function update(table) {
|
|
3313
|
+
return new SQLiteUpdateBuilder(table, self.session, self.dialect, queries);
|
|
3314
|
+
}
|
|
3315
|
+
function insert(into) {
|
|
3316
|
+
return new SQLiteInsertBuilder(into, self.session, self.dialect, queries);
|
|
3317
|
+
}
|
|
3318
|
+
function delete_(from) {
|
|
3319
|
+
return new SQLiteDeleteBase(from, self.session, self.dialect, queries);
|
|
3320
|
+
}
|
|
3321
|
+
return { select, selectDistinct, update, insert, delete: delete_ };
|
|
3322
|
+
}
|
|
3323
|
+
select(fields) {
|
|
3324
|
+
return new SQLiteSelectBuilder({ fields: fields ?? undefined, session: this.session, dialect: this.dialect });
|
|
3325
|
+
}
|
|
3326
|
+
selectDistinct(fields) {
|
|
3327
|
+
return new SQLiteSelectBuilder({
|
|
3328
|
+
fields: fields ?? undefined,
|
|
3329
|
+
session: this.session,
|
|
3330
|
+
dialect: this.dialect,
|
|
3331
|
+
distinct: true
|
|
3332
|
+
});
|
|
3333
|
+
}
|
|
3334
|
+
update(table) {
|
|
3335
|
+
return new SQLiteUpdateBuilder(table, this.session, this.dialect);
|
|
3336
|
+
}
|
|
3337
|
+
insert(into) {
|
|
3338
|
+
return new SQLiteInsertBuilder(into, this.session, this.dialect);
|
|
3339
|
+
}
|
|
3340
|
+
delete(from) {
|
|
3341
|
+
return new SQLiteDeleteBase(from, this.session, this.dialect);
|
|
3342
|
+
}
|
|
3343
|
+
run(query) {
|
|
3344
|
+
const sequel = typeof query === "string" ? sql.raw(query) : query.getSQL();
|
|
3345
|
+
if (this.resultKind === "async") {
|
|
3346
|
+
return new SQLiteRaw(async () => this.session.run(sequel), () => sequel, "run", this.dialect, this.session.extractRawRunValueFromBatchResult.bind(this.session));
|
|
3347
|
+
}
|
|
3348
|
+
return this.session.run(sequel);
|
|
3349
|
+
}
|
|
3350
|
+
all(query) {
|
|
3351
|
+
const sequel = typeof query === "string" ? sql.raw(query) : query.getSQL();
|
|
3352
|
+
if (this.resultKind === "async") {
|
|
3353
|
+
return new SQLiteRaw(async () => this.session.all(sequel), () => sequel, "all", this.dialect, this.session.extractRawAllValueFromBatchResult.bind(this.session));
|
|
3354
|
+
}
|
|
3355
|
+
return this.session.all(sequel);
|
|
3356
|
+
}
|
|
3357
|
+
get(query) {
|
|
3358
|
+
const sequel = typeof query === "string" ? sql.raw(query) : query.getSQL();
|
|
3359
|
+
if (this.resultKind === "async") {
|
|
3360
|
+
return new SQLiteRaw(async () => this.session.get(sequel), () => sequel, "get", this.dialect, this.session.extractRawGetValueFromBatchResult.bind(this.session));
|
|
3361
|
+
}
|
|
3362
|
+
return this.session.get(sequel);
|
|
3363
|
+
}
|
|
3364
|
+
values(query) {
|
|
3365
|
+
const sequel = typeof query === "string" ? sql.raw(query) : query.getSQL();
|
|
3366
|
+
if (this.resultKind === "async") {
|
|
3367
|
+
return new SQLiteRaw(async () => this.session.values(sequel), () => sequel, "values", this.dialect, this.session.extractRawValuesValueFromBatchResult.bind(this.session));
|
|
3368
|
+
}
|
|
3369
|
+
return this.session.values(sequel);
|
|
3370
|
+
}
|
|
3371
|
+
transaction(transaction, config) {
|
|
3372
|
+
return this.session.transaction(transaction, config);
|
|
3373
|
+
}
|
|
3374
|
+
}
|
|
3375
|
+
|
|
3376
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/sqlite-core/session.js
|
|
3377
|
+
class ExecuteResultSync extends QueryPromise {
|
|
3378
|
+
constructor(resultCb) {
|
|
3379
|
+
super();
|
|
3380
|
+
this.resultCb = resultCb;
|
|
3381
|
+
}
|
|
3382
|
+
static [entityKind] = "ExecuteResultSync";
|
|
3383
|
+
async execute() {
|
|
3384
|
+
return this.resultCb();
|
|
3385
|
+
}
|
|
3386
|
+
sync() {
|
|
3387
|
+
return this.resultCb();
|
|
3388
|
+
}
|
|
3389
|
+
}
|
|
3390
|
+
|
|
3391
|
+
class SQLitePreparedQuery {
|
|
3392
|
+
constructor(mode, executeMethod, query) {
|
|
3393
|
+
this.mode = mode;
|
|
3394
|
+
this.executeMethod = executeMethod;
|
|
3395
|
+
this.query = query;
|
|
3396
|
+
}
|
|
3397
|
+
static [entityKind] = "PreparedQuery";
|
|
3398
|
+
joinsNotNullableMap;
|
|
3399
|
+
getQuery() {
|
|
3400
|
+
return this.query;
|
|
3401
|
+
}
|
|
3402
|
+
mapRunResult(result, _isFromBatch) {
|
|
3403
|
+
return result;
|
|
3404
|
+
}
|
|
3405
|
+
mapAllResult(_result, _isFromBatch) {
|
|
3406
|
+
throw new Error("Not implemented");
|
|
3407
|
+
}
|
|
3408
|
+
mapGetResult(_result, _isFromBatch) {
|
|
3409
|
+
throw new Error("Not implemented");
|
|
3410
|
+
}
|
|
3411
|
+
execute(placeholderValues) {
|
|
3412
|
+
if (this.mode === "async") {
|
|
3413
|
+
return this[this.executeMethod](placeholderValues);
|
|
3414
|
+
}
|
|
3415
|
+
return new ExecuteResultSync(() => this[this.executeMethod](placeholderValues));
|
|
3416
|
+
}
|
|
3417
|
+
mapResult(response, isFromBatch) {
|
|
3418
|
+
switch (this.executeMethod) {
|
|
3419
|
+
case "run": {
|
|
3420
|
+
return this.mapRunResult(response, isFromBatch);
|
|
3421
|
+
}
|
|
3422
|
+
case "all": {
|
|
3423
|
+
return this.mapAllResult(response, isFromBatch);
|
|
3424
|
+
}
|
|
3425
|
+
case "get": {
|
|
3426
|
+
return this.mapGetResult(response, isFromBatch);
|
|
3427
|
+
}
|
|
3428
|
+
}
|
|
3429
|
+
}
|
|
3430
|
+
}
|
|
3431
|
+
|
|
3432
|
+
class SQLiteSession {
|
|
3433
|
+
constructor(dialect) {
|
|
3434
|
+
this.dialect = dialect;
|
|
3435
|
+
}
|
|
3436
|
+
static [entityKind] = "SQLiteSession";
|
|
3437
|
+
prepareOneTimeQuery(query, fields, executeMethod, isResponseInArrayMode) {
|
|
3438
|
+
return this.prepareQuery(query, fields, executeMethod, isResponseInArrayMode);
|
|
3439
|
+
}
|
|
3440
|
+
run(query) {
|
|
3441
|
+
const staticQuery = this.dialect.sqlToQuery(query);
|
|
3442
|
+
try {
|
|
3443
|
+
return this.prepareOneTimeQuery(staticQuery, undefined, "run", false).run();
|
|
3444
|
+
} catch (err) {
|
|
3445
|
+
throw new DrizzleError({ cause: err, message: `Failed to run the query '${staticQuery.sql}'` });
|
|
3446
|
+
}
|
|
3447
|
+
}
|
|
3448
|
+
extractRawRunValueFromBatchResult(result) {
|
|
3449
|
+
return result;
|
|
3450
|
+
}
|
|
3451
|
+
all(query) {
|
|
3452
|
+
return this.prepareOneTimeQuery(this.dialect.sqlToQuery(query), undefined, "run", false).all();
|
|
3453
|
+
}
|
|
3454
|
+
extractRawAllValueFromBatchResult(_result) {
|
|
3455
|
+
throw new Error("Not implemented");
|
|
3456
|
+
}
|
|
3457
|
+
get(query) {
|
|
3458
|
+
return this.prepareOneTimeQuery(this.dialect.sqlToQuery(query), undefined, "run", false).get();
|
|
3459
|
+
}
|
|
3460
|
+
extractRawGetValueFromBatchResult(_result) {
|
|
3461
|
+
throw new Error("Not implemented");
|
|
3462
|
+
}
|
|
3463
|
+
values(query) {
|
|
3464
|
+
return this.prepareOneTimeQuery(this.dialect.sqlToQuery(query), undefined, "run", false).values();
|
|
3465
|
+
}
|
|
3466
|
+
async count(sql2) {
|
|
3467
|
+
const result = await this.values(sql2);
|
|
3468
|
+
return result[0][0];
|
|
3469
|
+
}
|
|
3470
|
+
extractRawValuesValueFromBatchResult(_result) {
|
|
3471
|
+
throw new Error("Not implemented");
|
|
3472
|
+
}
|
|
3473
|
+
}
|
|
3474
|
+
|
|
3475
|
+
class SQLiteTransaction extends BaseSQLiteDatabase {
|
|
3476
|
+
constructor(resultType, dialect, session, schema, nestedIndex = 0) {
|
|
3477
|
+
super(resultType, dialect, session, schema);
|
|
3478
|
+
this.schema = schema;
|
|
3479
|
+
this.nestedIndex = nestedIndex;
|
|
3480
|
+
}
|
|
3481
|
+
static [entityKind] = "SQLiteTransaction";
|
|
3482
|
+
rollback() {
|
|
3483
|
+
throw new TransactionRollbackError;
|
|
3484
|
+
}
|
|
3485
|
+
}
|
|
3486
|
+
|
|
3487
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/bun-sqlite/session.js
|
|
3488
|
+
class SQLiteBunSession extends SQLiteSession {
|
|
3489
|
+
constructor(client, dialect, schema, options = {}) {
|
|
3490
|
+
super(dialect);
|
|
3491
|
+
this.client = client;
|
|
3492
|
+
this.schema = schema;
|
|
3493
|
+
this.logger = options.logger ?? new NoopLogger;
|
|
3494
|
+
}
|
|
3495
|
+
static [entityKind] = "SQLiteBunSession";
|
|
3496
|
+
logger;
|
|
3497
|
+
exec(query) {
|
|
3498
|
+
this.client.exec(query);
|
|
3499
|
+
}
|
|
3500
|
+
prepareQuery(query, fields, executeMethod, isResponseInArrayMode, customResultMapper) {
|
|
3501
|
+
const stmt = this.client.prepare(query.sql);
|
|
3502
|
+
return new PreparedQuery(stmt, query, this.logger, fields, executeMethod, isResponseInArrayMode, customResultMapper);
|
|
3503
|
+
}
|
|
3504
|
+
transaction(transaction, config = {}) {
|
|
3505
|
+
const tx = new SQLiteBunTransaction("sync", this.dialect, this, this.schema);
|
|
3506
|
+
let result;
|
|
3507
|
+
const nativeTx = this.client.transaction(() => {
|
|
3508
|
+
result = transaction(tx);
|
|
3509
|
+
});
|
|
3510
|
+
nativeTx[config.behavior ?? "deferred"]();
|
|
3511
|
+
return result;
|
|
3512
|
+
}
|
|
3513
|
+
}
|
|
3514
|
+
|
|
3515
|
+
class SQLiteBunTransaction extends SQLiteTransaction {
|
|
3516
|
+
static [entityKind] = "SQLiteBunTransaction";
|
|
3517
|
+
transaction(transaction) {
|
|
3518
|
+
const savepointName = `sp${this.nestedIndex}`;
|
|
3519
|
+
const tx = new SQLiteBunTransaction("sync", this.dialect, this.session, this.schema, this.nestedIndex + 1);
|
|
3520
|
+
this.session.run(sql.raw(`savepoint ${savepointName}`));
|
|
3521
|
+
try {
|
|
3522
|
+
const result = transaction(tx);
|
|
3523
|
+
this.session.run(sql.raw(`release savepoint ${savepointName}`));
|
|
3524
|
+
return result;
|
|
3525
|
+
} catch (err) {
|
|
3526
|
+
this.session.run(sql.raw(`rollback to savepoint ${savepointName}`));
|
|
3527
|
+
throw err;
|
|
3528
|
+
}
|
|
3529
|
+
}
|
|
3530
|
+
}
|
|
3531
|
+
|
|
3532
|
+
class PreparedQuery extends SQLitePreparedQuery {
|
|
3533
|
+
constructor(stmt, query, logger, fields, executeMethod, _isResponseInArrayMode, customResultMapper) {
|
|
3534
|
+
super("sync", executeMethod, query);
|
|
3535
|
+
this.stmt = stmt;
|
|
3536
|
+
this.logger = logger;
|
|
3537
|
+
this.fields = fields;
|
|
3538
|
+
this._isResponseInArrayMode = _isResponseInArrayMode;
|
|
3539
|
+
this.customResultMapper = customResultMapper;
|
|
3540
|
+
}
|
|
3541
|
+
static [entityKind] = "SQLiteBunPreparedQuery";
|
|
3542
|
+
run(placeholderValues) {
|
|
3543
|
+
const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
|
|
3544
|
+
this.logger.logQuery(this.query.sql, params);
|
|
3545
|
+
return this.stmt.run(...params);
|
|
3546
|
+
}
|
|
3547
|
+
all(placeholderValues) {
|
|
3548
|
+
const { fields, query, logger, joinsNotNullableMap, stmt, customResultMapper } = this;
|
|
3549
|
+
if (!fields && !customResultMapper) {
|
|
3550
|
+
const params = fillPlaceholders(query.params, placeholderValues ?? {});
|
|
3551
|
+
logger.logQuery(query.sql, params);
|
|
3552
|
+
return stmt.all(...params);
|
|
3553
|
+
}
|
|
3554
|
+
const rows = this.values(placeholderValues);
|
|
3555
|
+
if (customResultMapper) {
|
|
3556
|
+
return customResultMapper(rows);
|
|
3557
|
+
}
|
|
3558
|
+
return rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
|
|
3559
|
+
}
|
|
3560
|
+
get(placeholderValues) {
|
|
3561
|
+
const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
|
|
3562
|
+
this.logger.logQuery(this.query.sql, params);
|
|
3563
|
+
const row = this.stmt.values(...params)[0];
|
|
3564
|
+
if (!row) {
|
|
3565
|
+
return;
|
|
3566
|
+
}
|
|
3567
|
+
const { fields, joinsNotNullableMap, customResultMapper } = this;
|
|
3568
|
+
if (!fields && !customResultMapper) {
|
|
3569
|
+
return row;
|
|
3570
|
+
}
|
|
3571
|
+
if (customResultMapper) {
|
|
3572
|
+
return customResultMapper([row]);
|
|
3573
|
+
}
|
|
3574
|
+
return mapResultRow(fields, row, joinsNotNullableMap);
|
|
3575
|
+
}
|
|
3576
|
+
values(placeholderValues) {
|
|
3577
|
+
const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
|
|
3578
|
+
this.logger.logQuery(this.query.sql, params);
|
|
3579
|
+
return this.stmt.values(...params);
|
|
3580
|
+
}
|
|
3581
|
+
isResponseInArrayMode() {
|
|
3582
|
+
return this._isResponseInArrayMode;
|
|
3583
|
+
}
|
|
3584
|
+
}
|
|
3585
|
+
|
|
3586
|
+
// ../../node_modules/.bun/drizzle-orm@0.38.4+11aa72c9bfcf30b8/node_modules/drizzle-orm/bun-sqlite/driver.js
|
|
3587
|
+
class BunSQLiteDatabase extends BaseSQLiteDatabase {
|
|
3588
|
+
static [entityKind] = "BunSQLiteDatabase";
|
|
3589
|
+
}
|
|
3590
|
+
function construct(client, config = {}) {
|
|
3591
|
+
const dialect = new SQLiteSyncDialect({ casing: config.casing });
|
|
3592
|
+
let logger;
|
|
3593
|
+
if (config.logger === true) {
|
|
3594
|
+
logger = new DefaultLogger;
|
|
3595
|
+
} else if (config.logger !== false) {
|
|
3596
|
+
logger = config.logger;
|
|
3597
|
+
}
|
|
3598
|
+
let schema;
|
|
3599
|
+
if (config.schema) {
|
|
3600
|
+
const tablesConfig = extractTablesRelationalConfig(config.schema, createTableRelationsHelpers);
|
|
3601
|
+
schema = {
|
|
3602
|
+
fullSchema: config.schema,
|
|
3603
|
+
schema: tablesConfig.tables,
|
|
3604
|
+
tableNamesMap: tablesConfig.tableNamesMap
|
|
3605
|
+
};
|
|
3606
|
+
}
|
|
3607
|
+
const session = new SQLiteBunSession(client, dialect, schema, { logger });
|
|
3608
|
+
const db = new BunSQLiteDatabase("sync", dialect, session, schema);
|
|
3609
|
+
db.$client = client;
|
|
3610
|
+
return db;
|
|
3611
|
+
}
|
|
3612
|
+
function drizzle(...params) {
|
|
3613
|
+
if (params[0] === undefined || typeof params[0] === "string") {
|
|
3614
|
+
const instance = params[0] === undefined ? new Database : new Database(params[0]);
|
|
3615
|
+
return construct(instance, params[1]);
|
|
3616
|
+
}
|
|
3617
|
+
if (isConfig(params[0])) {
|
|
3618
|
+
const { connection, client, ...drizzleConfig } = params[0];
|
|
3619
|
+
if (client)
|
|
3620
|
+
return construct(client, drizzleConfig);
|
|
3621
|
+
if (typeof connection === "object") {
|
|
3622
|
+
const { source, ...opts } = connection;
|
|
3623
|
+
const options = Object.values(opts).filter((v) => v !== undefined).length ? opts : undefined;
|
|
3624
|
+
const instance2 = new Database(source, options);
|
|
3625
|
+
return construct(instance2, drizzleConfig);
|
|
3626
|
+
}
|
|
3627
|
+
const instance = new Database(connection);
|
|
3628
|
+
return construct(instance, drizzleConfig);
|
|
3629
|
+
}
|
|
3630
|
+
return construct(params[0], params[1]);
|
|
3631
|
+
}
|
|
3632
|
+
((drizzle2) => {
|
|
3633
|
+
function mock(config) {
|
|
3634
|
+
return construct({}, config);
|
|
3635
|
+
}
|
|
3636
|
+
drizzle2.mock = mock;
|
|
3637
|
+
})(drizzle || (drizzle = {}));
|
|
3638
|
+
|
|
3639
|
+
// lib/db.ts
|
|
3640
|
+
import path from "path";
|
|
3641
|
+
import os from "os";
|
|
3642
|
+
import fs from "fs";
|
|
3643
|
+
var wallets = sqliteTable("wallets", {
|
|
3644
|
+
id: text("id").primaryKey(),
|
|
3645
|
+
name: text("name").notNull(),
|
|
3646
|
+
balance: real("balance").notNull().default(0),
|
|
3647
|
+
initial_balance: real("initial_balance").notNull().default(0),
|
|
3648
|
+
currency: text("currency").notNull().default("USD"),
|
|
3649
|
+
updated_at: text("updated_at").notNull(),
|
|
3650
|
+
created_at: text("created_at").notNull()
|
|
3651
|
+
});
|
|
3652
|
+
var transactions = sqliteTable("transactions", {
|
|
3653
|
+
id: text("id").primaryKey(),
|
|
3654
|
+
provider: text("provider").notNull(),
|
|
3655
|
+
model: text("model").notNull(),
|
|
3656
|
+
cost: real("cost").notNull(),
|
|
3657
|
+
input_tokens: integer("input_tokens"),
|
|
3658
|
+
output_tokens: integer("output_tokens"),
|
|
3659
|
+
session_id: text("session_id"),
|
|
3660
|
+
created_at: text("created_at").notNull(),
|
|
3661
|
+
synced: integer("synced").default(0)
|
|
3662
|
+
});
|
|
3663
|
+
var dbPath = path.join(os.homedir(), ".config", "opencode", "ai-ledger", "wallet.db");
|
|
3664
|
+
var dbInstance = null;
|
|
3665
|
+
function getDb() {
|
|
3666
|
+
if (!dbInstance) {
|
|
3667
|
+
const dbDir = path.dirname(dbPath);
|
|
3668
|
+
if (!fs.existsSync(dbDir)) {
|
|
3669
|
+
fs.mkdirSync(dbDir, { recursive: true });
|
|
3670
|
+
}
|
|
3671
|
+
const sqliteDb = new Database2(dbPath);
|
|
3672
|
+
dbInstance = drizzle(sqliteDb);
|
|
3673
|
+
}
|
|
3674
|
+
return dbInstance;
|
|
3675
|
+
}
|
|
3676
|
+
async function initializeDatabase() {
|
|
3677
|
+
const db = getDb();
|
|
3678
|
+
db.run(sql`
|
|
3679
|
+
CREATE TABLE IF NOT EXISTS wallets (
|
|
3680
|
+
id TEXT PRIMARY KEY,
|
|
3681
|
+
name TEXT NOT NULL,
|
|
3682
|
+
balance REAL NOT NULL DEFAULT 0,
|
|
3683
|
+
initial_balance REAL NOT NULL DEFAULT 0,
|
|
3684
|
+
currency TEXT NOT NULL DEFAULT 'USD',
|
|
3685
|
+
updated_at TEXT NOT NULL,
|
|
3686
|
+
created_at TEXT NOT NULL
|
|
3687
|
+
)
|
|
3688
|
+
`);
|
|
3689
|
+
db.run(sql`
|
|
3690
|
+
CREATE TABLE IF NOT EXISTS transactions (
|
|
3691
|
+
id TEXT PRIMARY KEY,
|
|
3692
|
+
provider TEXT NOT NULL,
|
|
3693
|
+
model TEXT NOT NULL,
|
|
3694
|
+
cost REAL NOT NULL,
|
|
3695
|
+
input_tokens INTEGER,
|
|
3696
|
+
output_tokens INTEGER,
|
|
3697
|
+
session_id TEXT,
|
|
3698
|
+
created_at TEXT NOT NULL,
|
|
3699
|
+
synced INTEGER DEFAULT 0
|
|
3700
|
+
)
|
|
3701
|
+
`);
|
|
3702
|
+
}
|
|
3703
|
+
async function updateWallet(data) {
|
|
3704
|
+
const db = getDb();
|
|
3705
|
+
const now = new Date().toISOString();
|
|
3706
|
+
await db.update(wallets).set({
|
|
3707
|
+
balance: data.balance,
|
|
3708
|
+
updated_at: now
|
|
3709
|
+
}).where(eq(wallets.id, data.id));
|
|
3710
|
+
}
|
|
3711
|
+
async function getWallet(provider) {
|
|
3712
|
+
const db = getDb();
|
|
3713
|
+
const result = await db.select().from(wallets).where(eq(wallets.id, provider)).limit(1);
|
|
3714
|
+
return result[0] || null;
|
|
3715
|
+
}
|
|
3716
|
+
async function insertTransaction(data) {
|
|
3717
|
+
const db = getDb();
|
|
3718
|
+
await db.insert(transactions).values({
|
|
3719
|
+
id: data.id,
|
|
3720
|
+
provider: data.provider,
|
|
3721
|
+
model: data.model,
|
|
3722
|
+
cost: data.cost,
|
|
3723
|
+
input_tokens: data.input_tokens,
|
|
3724
|
+
output_tokens: data.output_tokens,
|
|
3725
|
+
session_id: data.session_id,
|
|
3726
|
+
created_at: data.created_at,
|
|
3727
|
+
synced: data.synced
|
|
3728
|
+
});
|
|
3729
|
+
}
|
|
3730
|
+
async function deductBalance(provider, cost) {
|
|
3731
|
+
const db = getDb();
|
|
3732
|
+
const wallet = await getWallet(provider);
|
|
3733
|
+
if (!wallet) {
|
|
3734
|
+
throw new Error(`Wallet for provider '${provider}' not found`);
|
|
3735
|
+
}
|
|
3736
|
+
const newBalance = wallet.balance - cost;
|
|
3737
|
+
await updateWallet({ id: provider, balance: newBalance });
|
|
3738
|
+
return newBalance;
|
|
3739
|
+
}
|
|
3740
|
+
|
|
3741
|
+
// plugin.ts
|
|
3742
|
+
var plugin_default = async (input) => {
|
|
3743
|
+
await initializeDatabase();
|
|
3744
|
+
return {
|
|
3745
|
+
event: async ({ event }) => {
|
|
3746
|
+
if (event.type !== "message.updated")
|
|
3747
|
+
return;
|
|
3748
|
+
const msg = event.properties?.info;
|
|
3749
|
+
if (!msg)
|
|
3750
|
+
return;
|
|
3751
|
+
if (msg.role !== "assistant")
|
|
3752
|
+
return;
|
|
3753
|
+
if (!msg.finish)
|
|
3754
|
+
return;
|
|
3755
|
+
if (!msg.cost || msg.cost === 0)
|
|
3756
|
+
return;
|
|
3757
|
+
const wallet = await getWallet(msg.providerID);
|
|
3758
|
+
if (!wallet)
|
|
3759
|
+
return;
|
|
3760
|
+
const newBalance = await deductBalance(msg.providerID, msg.cost);
|
|
3761
|
+
await insertTransaction({
|
|
3762
|
+
id: crypto.randomUUID(),
|
|
3763
|
+
provider: msg.providerID,
|
|
3764
|
+
model: msg.modelID,
|
|
3765
|
+
cost: msg.cost,
|
|
3766
|
+
input_tokens: msg.tokens?.input,
|
|
3767
|
+
output_tokens: msg.tokens?.output,
|
|
3768
|
+
session_id: msg.sessionID,
|
|
3769
|
+
created_at: new Date().toISOString(),
|
|
3770
|
+
synced: 0
|
|
3771
|
+
});
|
|
3772
|
+
input.client.app.log({
|
|
3773
|
+
body: {
|
|
3774
|
+
service: "ai-ledger",
|
|
3775
|
+
level: "info",
|
|
3776
|
+
message: `${msg.providerID} (${msg.modelID}): -$${msg.cost.toFixed(4)} | balance: $${newBalance.toFixed(2)}`
|
|
3777
|
+
}
|
|
3778
|
+
});
|
|
3779
|
+
}
|
|
3780
|
+
};
|
|
3781
|
+
};
|
|
3782
|
+
export {
|
|
3783
|
+
plugin_default as default
|
|
3784
|
+
};
|