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