@type32/tauri-sqlite-orm 0.2.13 → 0.2.15
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/README.md +14 -8
- package/dist/index.d.mts +219 -177
- package/dist/index.d.ts +219 -177
- package/dist/index.js +702 -847
- package/dist/index.mjs +706 -845
- package/package.json +6 -6
package/dist/index.mjs
CHANGED
|
@@ -1,174 +1,81 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
1
|
+
// src/orm.ts
|
|
2
|
+
import { Kysely as Kysely4, sql as kyselySql } from "kysely";
|
|
3
|
+
|
|
4
|
+
// src/dialect.ts
|
|
5
|
+
import {
|
|
6
|
+
CompiledQuery,
|
|
7
|
+
SqliteAdapter,
|
|
8
|
+
SqliteIntrospector,
|
|
9
|
+
SqliteQueryCompiler
|
|
10
|
+
} from "kysely";
|
|
11
|
+
var TauriConnection = class {
|
|
3
12
|
constructor(db) {
|
|
4
13
|
this.db = db;
|
|
5
14
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
15
|
+
async executeQuery(compiledQuery) {
|
|
16
|
+
const { sql: sql6, parameters } = compiledQuery;
|
|
17
|
+
const params = parameters;
|
|
18
|
+
const trimmed = sql6.trimStart();
|
|
19
|
+
const isSelect = /^\s*(SELECT|WITH|PRAGMA)/i.test(trimmed);
|
|
20
|
+
const hasReturning = /\bRETURNING\b/i.test(sql6);
|
|
21
|
+
if (isSelect || hasReturning) {
|
|
22
|
+
const rows = await this.db.select(sql6, params);
|
|
23
|
+
return { rows };
|
|
24
|
+
}
|
|
25
|
+
const result = await this.db.execute(sql6, params);
|
|
26
|
+
return {
|
|
27
|
+
rows: [],
|
|
28
|
+
insertId: BigInt(Math.round(result.lastInsertId ?? 0)),
|
|
29
|
+
numAffectedRows: BigInt(result.rowsAffected ?? 0)
|
|
30
|
+
};
|
|
12
31
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
this.query += ` ORDER BY ${column.sql} ${direction}`;
|
|
16
|
-
this.params.push(...column.params);
|
|
17
|
-
} else {
|
|
18
|
-
this.query += ` ORDER BY ${column._.name} ${direction}`;
|
|
19
|
-
}
|
|
20
|
-
return this;
|
|
32
|
+
async *streamQuery(_compiledQuery) {
|
|
33
|
+
throw new Error("Streaming queries are not supported by @tauri-apps/plugin-sql");
|
|
21
34
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
35
|
+
};
|
|
36
|
+
var TauriDriver = class {
|
|
37
|
+
constructor(db) {
|
|
38
|
+
this.db = db;
|
|
25
39
|
}
|
|
26
|
-
|
|
27
|
-
this.query += ` OFFSET ${count2}`;
|
|
28
|
-
return this;
|
|
40
|
+
async init() {
|
|
29
41
|
}
|
|
30
|
-
|
|
31
|
-
return
|
|
32
|
-
sql: this.query,
|
|
33
|
-
params: this.params
|
|
34
|
-
};
|
|
42
|
+
async acquireConnection() {
|
|
43
|
+
return new TauriConnection(this.db);
|
|
35
44
|
}
|
|
36
|
-
|
|
37
|
-
|
|
45
|
+
async beginTransaction(conn, _settings) {
|
|
46
|
+
await conn.executeQuery(CompiledQuery.raw("BEGIN"));
|
|
38
47
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
};
|
|
49
|
-
var ne = (column, value, tableAlias) => {
|
|
50
|
-
const columnName = tableAlias ? `${tableAlias}.${column._.name}` : column._.name;
|
|
51
|
-
return {
|
|
52
|
-
sql: `${columnName} != ?`,
|
|
53
|
-
params: [value]
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
var and = (...conditions) => ({
|
|
57
|
-
sql: conditions.map((c) => `(${c.sql})`).join(" AND "),
|
|
58
|
-
params: conditions.flatMap((c) => c.params)
|
|
59
|
-
});
|
|
60
|
-
var or = (...conditions) => ({
|
|
61
|
-
sql: conditions.map((c) => `(${c.sql})`).join(" OR "),
|
|
62
|
-
params: conditions.flatMap((c) => c.params)
|
|
63
|
-
});
|
|
64
|
-
var not = (condition) => ({
|
|
65
|
-
sql: `NOT (${condition.sql})`,
|
|
66
|
-
params: condition.params
|
|
67
|
-
});
|
|
68
|
-
var gt = (column, value) => ({
|
|
69
|
-
sql: `${column._.name} > ?`,
|
|
70
|
-
params: [value]
|
|
71
|
-
});
|
|
72
|
-
var gte = (column, value) => ({
|
|
73
|
-
sql: `${column._.name} >= ?`,
|
|
74
|
-
params: [value]
|
|
75
|
-
});
|
|
76
|
-
var lt = (column, value) => ({
|
|
77
|
-
sql: `${column._.name} < ?`,
|
|
78
|
-
params: [value]
|
|
79
|
-
});
|
|
80
|
-
var lte = (column, value) => ({
|
|
81
|
-
sql: `${column._.name} <= ?`,
|
|
82
|
-
params: [value]
|
|
83
|
-
});
|
|
84
|
-
var like = (column, pattern) => ({
|
|
85
|
-
sql: `${column._.name} LIKE ?`,
|
|
86
|
-
params: [pattern]
|
|
87
|
-
});
|
|
88
|
-
var ilike = (column, pattern) => ({
|
|
89
|
-
sql: `${column._.name} LIKE ? COLLATE NOCASE`,
|
|
90
|
-
params: [pattern]
|
|
91
|
-
});
|
|
92
|
-
var startsWith = (column, value) => ({
|
|
93
|
-
sql: `${column._.name} LIKE ?`,
|
|
94
|
-
params: [`${value}%`]
|
|
95
|
-
});
|
|
96
|
-
var endsWith = (column, value) => ({
|
|
97
|
-
sql: `${column._.name} LIKE ?`,
|
|
98
|
-
params: [`%${value}`]
|
|
99
|
-
});
|
|
100
|
-
var contains = (column, value) => ({
|
|
101
|
-
sql: `${column._.name} LIKE ?`,
|
|
102
|
-
params: [`%${value}%`]
|
|
103
|
-
});
|
|
104
|
-
var isNull = (column) => ({
|
|
105
|
-
sql: `${column._.name} IS NULL`,
|
|
106
|
-
params: []
|
|
107
|
-
});
|
|
108
|
-
var isNotNull = (column) => ({
|
|
109
|
-
sql: `${column._.name} IS NOT NULL`,
|
|
110
|
-
params: []
|
|
111
|
-
});
|
|
112
|
-
var exists = (subquery2) => ({
|
|
113
|
-
sql: `EXISTS (${subquery2.sql})`,
|
|
114
|
-
params: subquery2.params
|
|
115
|
-
});
|
|
116
|
-
var notExists = (subquery2) => ({
|
|
117
|
-
sql: `NOT EXISTS (${subquery2.sql})`,
|
|
118
|
-
params: subquery2.params
|
|
119
|
-
});
|
|
120
|
-
var eqSubquery = (column, subquery2) => ({
|
|
121
|
-
sql: `${column._.name} = ${subquery2.sql}`,
|
|
122
|
-
params: subquery2.params
|
|
123
|
-
});
|
|
124
|
-
var neSubquery = (column, subquery2) => ({
|
|
125
|
-
sql: `${column._.name} != ${subquery2.sql}`,
|
|
126
|
-
params: subquery2.params
|
|
127
|
-
});
|
|
128
|
-
var gtSubquery = (column, subquery2) => ({
|
|
129
|
-
sql: `${column._.name} > ${subquery2.sql}`,
|
|
130
|
-
params: subquery2.params
|
|
131
|
-
});
|
|
132
|
-
var gteSubquery = (column, subquery2) => ({
|
|
133
|
-
sql: `${column._.name} >= ${subquery2.sql}`,
|
|
134
|
-
params: subquery2.params
|
|
135
|
-
});
|
|
136
|
-
var ltSubquery = (column, subquery2) => ({
|
|
137
|
-
sql: `${column._.name} < ${subquery2.sql}`,
|
|
138
|
-
params: subquery2.params
|
|
139
|
-
});
|
|
140
|
-
var lteSubquery = (column, subquery2) => ({
|
|
141
|
-
sql: `${column._.name} <= ${subquery2.sql}`,
|
|
142
|
-
params: subquery2.params
|
|
143
|
-
});
|
|
144
|
-
var inArray = (column, values) => {
|
|
145
|
-
if ("_isSubquery" in values && values._isSubquery) {
|
|
146
|
-
return {
|
|
147
|
-
sql: `${column._.name} IN ${values.sql}`,
|
|
148
|
-
params: values.params
|
|
149
|
-
};
|
|
48
|
+
async commitTransaction(conn) {
|
|
49
|
+
await conn.executeQuery(CompiledQuery.raw("COMMIT"));
|
|
50
|
+
}
|
|
51
|
+
async rollbackTransaction(conn) {
|
|
52
|
+
await conn.executeQuery(CompiledQuery.raw("ROLLBACK"));
|
|
53
|
+
}
|
|
54
|
+
async releaseConnection(_conn) {
|
|
55
|
+
}
|
|
56
|
+
async destroy() {
|
|
150
57
|
}
|
|
151
|
-
return {
|
|
152
|
-
sql: `${column._.name} IN (${values.map(() => "?").join(",")})`,
|
|
153
|
-
params: values
|
|
154
|
-
};
|
|
155
58
|
};
|
|
156
|
-
var
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
59
|
+
var TauriDialect = class {
|
|
60
|
+
constructor(db) {
|
|
61
|
+
this.db = db;
|
|
62
|
+
}
|
|
63
|
+
createAdapter() {
|
|
64
|
+
return new SqliteAdapter();
|
|
65
|
+
}
|
|
66
|
+
createDriver() {
|
|
67
|
+
return new TauriDriver(this.db);
|
|
68
|
+
}
|
|
69
|
+
createIntrospector(db) {
|
|
70
|
+
return new SqliteIntrospector(db);
|
|
71
|
+
}
|
|
72
|
+
createQueryCompiler() {
|
|
73
|
+
return new SqliteQueryCompiler();
|
|
162
74
|
}
|
|
163
|
-
return {
|
|
164
|
-
sql: `${column._.name} NOT IN (${values.map(() => "?").join(",")})`,
|
|
165
|
-
params: values
|
|
166
|
-
};
|
|
167
75
|
};
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
});
|
|
76
|
+
|
|
77
|
+
// src/builders/select.ts
|
|
78
|
+
import { sql } from "kysely";
|
|
172
79
|
|
|
173
80
|
// src/serialization.ts
|
|
174
81
|
function serializeValue(value, column) {
|
|
@@ -272,71 +179,116 @@ function deserializeValue(value, column) {
|
|
|
272
179
|
}
|
|
273
180
|
|
|
274
181
|
// src/builders/select.ts
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
182
|
+
function getDbNameToTsName(table) {
|
|
183
|
+
const map = {};
|
|
184
|
+
for (const [tsName, col] of Object.entries(table._.columns)) {
|
|
185
|
+
map[col._.name] = tsName;
|
|
186
|
+
}
|
|
187
|
+
return map;
|
|
188
|
+
}
|
|
189
|
+
function normalizeRowKey(key) {
|
|
190
|
+
if (key.startsWith('"') && key.endsWith('"')) {
|
|
191
|
+
return key.slice(1, -1);
|
|
192
|
+
}
|
|
193
|
+
return key;
|
|
194
|
+
}
|
|
195
|
+
function resolveRelationColumns(table, include) {
|
|
196
|
+
const allEntries = Object.entries(table._.columns);
|
|
197
|
+
if (include === true || typeof include !== "object") {
|
|
198
|
+
return allEntries.map(([, col]) => col);
|
|
199
|
+
}
|
|
200
|
+
const cols = include.columns;
|
|
201
|
+
if (!cols) {
|
|
202
|
+
return allEntries.map(([, col]) => col);
|
|
203
|
+
}
|
|
204
|
+
const names = Array.isArray(cols) ? cols : Object.entries(cols).filter(([, v]) => v).map(([k]) => k);
|
|
205
|
+
const pkNames = allEntries.filter(([, c]) => c.options.primaryKey).map(([k]) => k);
|
|
206
|
+
const combined = /* @__PURE__ */ new Set([...names, ...pkNames]);
|
|
207
|
+
return allEntries.filter(([tsName]) => combined.has(tsName)).map(([, col]) => col);
|
|
208
|
+
}
|
|
209
|
+
var SelectQueryBuilder = class {
|
|
210
|
+
constructor(kysely, table, columns) {
|
|
211
|
+
this.kysely = kysely;
|
|
212
|
+
this._table = table;
|
|
213
|
+
this._columns = columns;
|
|
214
|
+
const selected = columns ? columns.map((c) => table._.columns[c]) : Object.values(table._.columns);
|
|
215
|
+
const colSelections = selected.map(
|
|
216
|
+
(col) => `${table._.name}.${col._.name} as "${table._.name}.${col._.name}"`
|
|
284
217
|
);
|
|
285
|
-
this.
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
218
|
+
this._includedColumnAliases = colSelections;
|
|
219
|
+
this._builder = kysely.selectFrom(table._.name).select(colSelections);
|
|
220
|
+
}
|
|
221
|
+
_builder;
|
|
222
|
+
_table;
|
|
223
|
+
_columns;
|
|
224
|
+
_includeRelations = {};
|
|
225
|
+
_manualJoins = [];
|
|
226
|
+
_isDistinct = false;
|
|
227
|
+
_includedColumnAliases = [];
|
|
294
228
|
distinct() {
|
|
295
|
-
this.
|
|
229
|
+
this._isDistinct = true;
|
|
230
|
+
this._builder = this._builder.distinct();
|
|
231
|
+
return this;
|
|
232
|
+
}
|
|
233
|
+
where(condition) {
|
|
234
|
+
this._builder = this._builder.where(condition);
|
|
235
|
+
return this;
|
|
236
|
+
}
|
|
237
|
+
orderBy(column, direction = "asc") {
|
|
238
|
+
if ("toOperationNode" in column) {
|
|
239
|
+
this._builder = this._builder.orderBy(column, direction);
|
|
240
|
+
} else {
|
|
241
|
+
this._builder = this._builder.orderBy(
|
|
242
|
+
sql.ref(column._.name),
|
|
243
|
+
direction
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
return this;
|
|
247
|
+
}
|
|
248
|
+
limit(count2) {
|
|
249
|
+
this._builder = this._builder.limit(count2);
|
|
250
|
+
return this;
|
|
251
|
+
}
|
|
252
|
+
offset(count2) {
|
|
253
|
+
this._builder = this._builder.offset(count2);
|
|
296
254
|
return this;
|
|
297
255
|
}
|
|
298
256
|
groupBy(...columns) {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
257
|
+
for (const col of columns) {
|
|
258
|
+
this._builder = this._builder.groupBy(
|
|
259
|
+
sql`${sql.ref(this._table._.name)}.${sql.ref(col._.name)}`
|
|
260
|
+
);
|
|
261
|
+
}
|
|
302
262
|
return this;
|
|
303
263
|
}
|
|
304
264
|
having(condition) {
|
|
305
|
-
this.
|
|
306
|
-
this.query += ` HAVING ${condition.sql}`;
|
|
307
|
-
this.params.push(...condition.params);
|
|
265
|
+
this._builder = this._builder.having(condition);
|
|
308
266
|
return this;
|
|
309
267
|
}
|
|
310
268
|
leftJoin(table, condition, alias2) {
|
|
311
|
-
this.
|
|
312
|
-
const
|
|
313
|
-
(col) => `${alias2}.${col._.name}
|
|
269
|
+
this._manualJoins.push({ type: "LEFT", table, condition, alias: alias2 });
|
|
270
|
+
const aliasedCols = Object.values(table._.columns).map(
|
|
271
|
+
(col) => `${alias2}.${col._.name} as "${alias2}.${col._.name}"`
|
|
314
272
|
);
|
|
315
|
-
this.
|
|
273
|
+
this._builder = this._builder.leftJoin(`${table._.name} as ${alias2}`, (join) => join.on(condition)).select(aliasedCols);
|
|
316
274
|
return this;
|
|
317
275
|
}
|
|
318
276
|
innerJoin(table, condition, alias2) {
|
|
319
|
-
this.
|
|
320
|
-
const
|
|
321
|
-
(col) => `${alias2}.${col._.name}
|
|
277
|
+
this._manualJoins.push({ type: "INNER", table, condition, alias: alias2 });
|
|
278
|
+
const aliasedCols = Object.values(table._.columns).map(
|
|
279
|
+
(col) => `${alias2}.${col._.name} as "${alias2}.${col._.name}"`
|
|
322
280
|
);
|
|
323
|
-
this.
|
|
281
|
+
this._builder = this._builder.innerJoin(`${table._.name} as ${alias2}`, (join) => join.on(condition)).select(aliasedCols);
|
|
324
282
|
return this;
|
|
325
283
|
}
|
|
326
284
|
include(relations2) {
|
|
327
|
-
this.
|
|
285
|
+
this._includeRelations = { ...this._includeRelations, ...relations2 };
|
|
328
286
|
return this;
|
|
329
287
|
}
|
|
330
|
-
|
|
331
|
-
let sql2 = "";
|
|
332
|
-
const params = [];
|
|
333
|
-
for (const join of this.joins) {
|
|
334
|
-
sql2 += ` ${join.type} JOIN ${join.table._.name} ${join.alias} ON ${join.condition.sql}`;
|
|
335
|
-
params.push(...join.condition.params);
|
|
336
|
-
}
|
|
288
|
+
applyIncludes() {
|
|
337
289
|
const processRelations = (parentTable, parentAlias, relations2, depth = 0) => {
|
|
338
290
|
if (depth > 10) {
|
|
339
|
-
console.warn("[Tauri-ORM] Maximum relation depth (10) exceeded.
|
|
291
|
+
console.warn("[Tauri-ORM] Maximum relation depth (10) exceeded.");
|
|
340
292
|
return;
|
|
341
293
|
}
|
|
342
294
|
for (const [relationName, include] of Object.entries(relations2)) {
|
|
@@ -344,78 +296,43 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
344
296
|
const relation = parentTable.relations[relationName];
|
|
345
297
|
if (!relation) {
|
|
346
298
|
console.warn(
|
|
347
|
-
`[Tauri-ORM] Relation "${relationName}" not found on table "${parentTable._.name}". Skipping
|
|
299
|
+
`[Tauri-ORM] Relation "${relationName}" not found on table "${parentTable._.name}". Skipping.`
|
|
348
300
|
);
|
|
349
301
|
continue;
|
|
350
302
|
}
|
|
351
303
|
const foreignTable = relation.foreignTable;
|
|
352
304
|
const foreignAlias = `${parentAlias}_${relationName}`;
|
|
353
|
-
const
|
|
354
|
-
|
|
305
|
+
const selectedCols = resolveRelationColumns(foreignTable, include);
|
|
306
|
+
const aliasedCols = selectedCols.map(
|
|
307
|
+
(col) => `${foreignAlias}.${col._.name} as "${foreignAlias}.${col._.name}"`
|
|
355
308
|
);
|
|
356
|
-
this.selectedColumns.push(...aliasedColumns);
|
|
357
309
|
if (relation.type === "one" && relation.fields && relation.references) {
|
|
358
|
-
const
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
params.push(...condition.params);
|
|
310
|
+
const onCondition = sql`${sql.join(
|
|
311
|
+
relation.fields.map(
|
|
312
|
+
(field, i) => sql`${sql.ref(`${parentAlias}.${field._.name}`)} = ${sql.ref(`${foreignAlias}.${relation.references[i]._.name}`)}`
|
|
313
|
+
),
|
|
314
|
+
sql` AND `
|
|
315
|
+
)}`;
|
|
316
|
+
this._builder = this._builder.leftJoin(
|
|
317
|
+
`${foreignTable._.name} as ${foreignAlias}`,
|
|
318
|
+
(join) => join.on(onCondition)
|
|
319
|
+
).select(aliasedCols);
|
|
369
320
|
} else if (relation.type === "many") {
|
|
370
321
|
const refRelation = Object.entries(foreignTable.relations).find(
|
|
371
|
-
([
|
|
322
|
+
([, r]) => r.foreignTable === parentTable
|
|
372
323
|
);
|
|
373
324
|
if (refRelation && refRelation[1].fields && refRelation[1].references) {
|
|
374
|
-
const [
|
|
375
|
-
const
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
params.push(...condition.params);
|
|
386
|
-
}
|
|
387
|
-
} else if (relation.type === "manyToMany" && relation.junctionTable && relation.junctionFields && relation.junctionReferences) {
|
|
388
|
-
const junctionTable = relation.junctionTable;
|
|
389
|
-
const junctionAlias = `${foreignAlias}_junction`;
|
|
390
|
-
const parentTablePks = Object.values(parentTable._.columns).filter((c) => c.options.primaryKey).map((c) => c._.name);
|
|
391
|
-
if (parentTablePks.length > 0 && relation.junctionFields.length > 0) {
|
|
392
|
-
const junctionConditions = relation.junctionFields.map((field, i) => {
|
|
393
|
-
const parentPk = parentTablePks[i] || parentTablePks[0];
|
|
394
|
-
const localColumn = `${parentAlias}.${parentPk}`;
|
|
395
|
-
const junctionColumn = `${junctionAlias}.${field._.name}`;
|
|
396
|
-
return {
|
|
397
|
-
sql: `${localColumn} = ${junctionColumn}`,
|
|
398
|
-
params: []
|
|
399
|
-
};
|
|
400
|
-
});
|
|
401
|
-
const junctionCondition = junctionConditions.length > 1 ? and(...junctionConditions) : junctionConditions[0];
|
|
402
|
-
sql2 += ` LEFT JOIN ${junctionTable._.name} ${junctionAlias} ON ${junctionCondition.sql}`;
|
|
403
|
-
params.push(...junctionCondition.params);
|
|
404
|
-
const foreignTablePks = Object.values(foreignTable._.columns).filter((c) => c.options.primaryKey).map((c) => c._.name);
|
|
405
|
-
if (foreignTablePks.length > 0 && relation.junctionReferences.length > 0) {
|
|
406
|
-
const foreignConditions = relation.junctionReferences.map((field, i) => {
|
|
407
|
-
const foreignPk = foreignTablePks[i] || foreignTablePks[0];
|
|
408
|
-
const junctionColumn = `${junctionAlias}.${field._.name}`;
|
|
409
|
-
const foreignColumn = `${foreignAlias}.${foreignPk}`;
|
|
410
|
-
return {
|
|
411
|
-
sql: `${junctionColumn} = ${foreignColumn}`,
|
|
412
|
-
params: []
|
|
413
|
-
};
|
|
414
|
-
});
|
|
415
|
-
const foreignCondition = foreignConditions.length > 1 ? and(...foreignConditions) : foreignConditions[0];
|
|
416
|
-
sql2 += ` LEFT JOIN ${foreignTable._.name} ${foreignAlias} ON ${foreignCondition.sql}`;
|
|
417
|
-
params.push(...foreignCondition.params);
|
|
418
|
-
}
|
|
325
|
+
const [, relationConfig] = refRelation;
|
|
326
|
+
const onCondition = sql`${sql.join(
|
|
327
|
+
relationConfig.fields.map(
|
|
328
|
+
(field, i) => sql`${sql.ref(`${foreignAlias}.${field._.name}`)} = ${sql.ref(`${parentAlias}.${relationConfig.references[i]._.name}`)}`
|
|
329
|
+
),
|
|
330
|
+
sql` AND `
|
|
331
|
+
)}`;
|
|
332
|
+
this._builder = this._builder.leftJoin(
|
|
333
|
+
`${foreignTable._.name} as ${foreignAlias}`,
|
|
334
|
+
(join) => join.on(onCondition)
|
|
335
|
+
).select(aliasedCols);
|
|
419
336
|
}
|
|
420
337
|
}
|
|
421
338
|
if (typeof include === "object" && include.with) {
|
|
@@ -423,184 +340,159 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
423
340
|
}
|
|
424
341
|
}
|
|
425
342
|
};
|
|
426
|
-
processRelations(this.
|
|
427
|
-
return { sql: sql2, params };
|
|
343
|
+
processRelations(this._table, this._table._.name, this._includeRelations, 0);
|
|
428
344
|
}
|
|
429
|
-
// Enhanced execute method that handles relation data mapping
|
|
430
345
|
async execute() {
|
|
431
|
-
|
|
432
|
-
const
|
|
433
|
-
const
|
|
434
|
-
let fromPart = this.query;
|
|
435
|
-
let wherePart = "";
|
|
436
|
-
if (whereIndex !== -1) {
|
|
437
|
-
fromPart = this.query.substring(0, whereIndex);
|
|
438
|
-
wherePart = this.query.substring(whereIndex);
|
|
439
|
-
}
|
|
440
|
-
this.query = `SELECT ${distinct}${this.selectedColumns.join(", ")} ${fromPart}${joinSql}${wherePart}`;
|
|
441
|
-
this.params = [...joinParams, ...this.params];
|
|
442
|
-
const { sql: sql2, params } = this.build();
|
|
443
|
-
const rawResults = await this.db.select(sql2, params);
|
|
444
|
-
const hasIncludes = Object.values(this.includeRelations).some((i) => i);
|
|
346
|
+
this.applyIncludes();
|
|
347
|
+
const rawResults = await this._builder.execute();
|
|
348
|
+
const hasIncludes = Object.values(this._includeRelations).some((i) => i);
|
|
445
349
|
if (hasIncludes) {
|
|
446
350
|
return this.processRelationResults(rawResults);
|
|
447
351
|
}
|
|
448
|
-
const
|
|
449
|
-
if (
|
|
352
|
+
const hasManualJoins = this._manualJoins.length > 0;
|
|
353
|
+
if (hasManualJoins) {
|
|
450
354
|
return rawResults;
|
|
451
355
|
}
|
|
452
|
-
const prefix = `${this.
|
|
356
|
+
const prefix = `${this._table._.name}.`;
|
|
357
|
+
const dbNameToTs = getDbNameToTsName(this._table);
|
|
453
358
|
return rawResults.map((row) => {
|
|
454
|
-
const
|
|
359
|
+
const out = {};
|
|
455
360
|
for (const key in row) {
|
|
456
|
-
const
|
|
457
|
-
const
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
newRow[columnName] = row[key];
|
|
462
|
-
}
|
|
361
|
+
const normKey = normalizeRowKey(key);
|
|
362
|
+
const dbColName = normKey.startsWith(prefix) ? normKey.slice(prefix.length) : normKey;
|
|
363
|
+
const tsName = dbNameToTs[dbColName] ?? dbColName;
|
|
364
|
+
const column = this._table._.columns[tsName];
|
|
365
|
+
out[tsName] = column ? deserializeValue(row[key], column) : row[key];
|
|
463
366
|
}
|
|
464
|
-
return
|
|
367
|
+
return out;
|
|
465
368
|
});
|
|
466
369
|
}
|
|
467
370
|
processRelationResults(rawResults) {
|
|
468
371
|
if (!rawResults.length) return [];
|
|
469
|
-
const mainTablePks = Object.values(this.
|
|
470
|
-
if (mainTablePks.length === 0)
|
|
471
|
-
return rawResults;
|
|
472
|
-
}
|
|
372
|
+
const mainTablePks = Object.values(this._table._.columns).filter((c) => c.options.primaryKey).map((c) => c._.name);
|
|
373
|
+
if (mainTablePks.length === 0) return rawResults;
|
|
473
374
|
const groupedResults = /* @__PURE__ */ new Map();
|
|
474
375
|
const parseRelationPath = (tableAlias, baseAlias) => {
|
|
475
|
-
if (!tableAlias.startsWith(baseAlias + "_"))
|
|
476
|
-
|
|
477
|
-
}
|
|
478
|
-
const path = tableAlias.substring(baseAlias.length + 1);
|
|
479
|
-
return path.split("_");
|
|
376
|
+
if (!tableAlias.startsWith(baseAlias + "_")) return [];
|
|
377
|
+
return tableAlias.slice(baseAlias.length + 1).split("_");
|
|
480
378
|
};
|
|
481
|
-
const setNestedValue = (obj, path,
|
|
482
|
-
let
|
|
379
|
+
const setNestedValue = (obj, path, columnName, value) => {
|
|
380
|
+
let cur = obj;
|
|
483
381
|
for (let i = 0; i < path.length; i++) {
|
|
484
382
|
const key = path[i];
|
|
485
383
|
if (i === path.length - 1) {
|
|
486
|
-
if (!
|
|
487
|
-
|
|
384
|
+
if (!cur[key]) cur[key] = {};
|
|
385
|
+
cur[key][columnName] = value;
|
|
488
386
|
} else {
|
|
489
|
-
if (!
|
|
490
|
-
|
|
387
|
+
if (!cur[key]) cur[key] = {};
|
|
388
|
+
cur = cur[key];
|
|
491
389
|
}
|
|
492
390
|
}
|
|
493
391
|
};
|
|
494
392
|
const getNestedRelation = (table, path) => {
|
|
495
|
-
let
|
|
496
|
-
let
|
|
497
|
-
for (const
|
|
498
|
-
|
|
499
|
-
if (!
|
|
500
|
-
|
|
393
|
+
let current = table;
|
|
394
|
+
let relation = null;
|
|
395
|
+
for (const name of path) {
|
|
396
|
+
relation = current.relations[name];
|
|
397
|
+
if (!relation) return null;
|
|
398
|
+
current = relation.foreignTable;
|
|
501
399
|
}
|
|
502
|
-
return
|
|
400
|
+
return relation;
|
|
503
401
|
};
|
|
504
402
|
for (const row of rawResults) {
|
|
505
|
-
const
|
|
403
|
+
const getVal = (logicalKey) => {
|
|
404
|
+
const quoted = `"${logicalKey}"`;
|
|
405
|
+
return row[quoted] ?? row[logicalKey];
|
|
406
|
+
};
|
|
407
|
+
const mainTableKey = mainTablePks.map((pk) => getVal(`${this._table._.name}.${pk}`) ?? getVal(pk)).join("_");
|
|
506
408
|
if (!groupedResults.has(mainTableKey)) {
|
|
507
409
|
groupedResults.set(mainTableKey, {});
|
|
508
410
|
}
|
|
509
411
|
const result = groupedResults.get(mainTableKey);
|
|
510
412
|
const relations2 = {};
|
|
511
413
|
for (const [key, value] of Object.entries(row)) {
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
414
|
+
const normKey = normalizeRowKey(key);
|
|
415
|
+
if (!normKey.includes(".")) {
|
|
416
|
+
const mainDbToTs = getDbNameToTsName(this._table);
|
|
417
|
+
const tsName = mainDbToTs[normKey] ?? normKey;
|
|
418
|
+
const column = this._table._.columns[tsName];
|
|
419
|
+
result[tsName] = column ? deserializeValue(value, column) : value;
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
const dotIndex = normKey.indexOf(".");
|
|
423
|
+
const tableAlias = normKey.slice(0, dotIndex);
|
|
424
|
+
const columnName = normKey.slice(dotIndex + 1);
|
|
425
|
+
if (tableAlias === this._table._.name) {
|
|
426
|
+
const mainDbToTs = getDbNameToTsName(this._table);
|
|
427
|
+
const tsName = mainDbToTs[columnName] ?? columnName;
|
|
428
|
+
const column = this._table._.columns[tsName];
|
|
429
|
+
result[tsName] = column ? deserializeValue(value, column) : value;
|
|
430
|
+
} else {
|
|
431
|
+
const path = parseRelationPath(tableAlias, this._table._.name);
|
|
432
|
+
if (path.length > 0) {
|
|
433
|
+
const relationConfig = getNestedRelation(this._table, path);
|
|
434
|
+
const foreignTable = relationConfig?.foreignTable;
|
|
435
|
+
const foreignDbToTs = foreignTable ? getDbNameToTsName(foreignTable) : {};
|
|
436
|
+
const tsName = foreignDbToTs[columnName] ?? columnName;
|
|
437
|
+
const col = foreignTable?._.columns?.[tsName];
|
|
438
|
+
setNestedValue(relations2, path, tsName, col ? deserializeValue(value, col) : value);
|
|
517
439
|
} else {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
const relationConfig = getNestedRelation(this.table, relationPath);
|
|
521
|
-
const column = relationConfig?.foreignTable?._.columns?.[columnName];
|
|
522
|
-
const deserializedValue = column ? deserializeValue(value, column) : value;
|
|
523
|
-
setNestedValue(relations2, relationPath, deserializedValue, columnName);
|
|
524
|
-
} else {
|
|
525
|
-
if (!result[tableAlias]) result[tableAlias] = {};
|
|
526
|
-
result[tableAlias][columnName] = value;
|
|
527
|
-
}
|
|
440
|
+
if (!result[tableAlias]) result[tableAlias] = {};
|
|
441
|
+
result[tableAlias][columnName] = value;
|
|
528
442
|
}
|
|
529
|
-
} else {
|
|
530
|
-
const column = this.table._.columns[key];
|
|
531
|
-
result[key] = column ? deserializeValue(value, column) : value;
|
|
532
443
|
}
|
|
533
444
|
}
|
|
534
|
-
const attachRelations = (target,
|
|
535
|
-
for (const [relName,
|
|
536
|
-
const
|
|
537
|
-
const relationConfig = getNestedRelation(table, currentPath);
|
|
445
|
+
const attachRelations = (target, relData, table) => {
|
|
446
|
+
for (const [relName, data] of Object.entries(relData)) {
|
|
447
|
+
const relationConfig = table.relations[relName];
|
|
538
448
|
if (!relationConfig) continue;
|
|
539
|
-
const
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
}
|
|
548
|
-
if (relationConfig.type === "many" || relationConfig.type === "manyToMany") {
|
|
549
|
-
if (!target[relName]) target[relName] = [];
|
|
550
|
-
const directData = {};
|
|
551
|
-
const nestedData = {};
|
|
552
|
-
if (typeof relData === "object" && relData !== null) {
|
|
553
|
-
for (const [k, v] of Object.entries(relData)) {
|
|
554
|
-
if (typeof v === "object" && v !== null) {
|
|
555
|
-
nestedData[k] = v;
|
|
556
|
-
} else {
|
|
557
|
-
directData[k] = v;
|
|
558
|
-
}
|
|
449
|
+
const directData = {};
|
|
450
|
+
const nestedData = {};
|
|
451
|
+
if (typeof data === "object" && data !== null) {
|
|
452
|
+
for (const [k, v] of Object.entries(data)) {
|
|
453
|
+
if (typeof v === "object" && v !== null) {
|
|
454
|
+
nestedData[k] = v;
|
|
455
|
+
} else {
|
|
456
|
+
directData[k] = v;
|
|
559
457
|
}
|
|
560
458
|
}
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
)
|
|
459
|
+
}
|
|
460
|
+
const hasData = Object.values(directData).some(
|
|
461
|
+
(v) => v !== null && v !== void 0 && v !== ""
|
|
462
|
+
);
|
|
463
|
+
if (relationConfig.type === "many") {
|
|
464
|
+
if (!target[relName]) target[relName] = [];
|
|
564
465
|
if (hasData) {
|
|
565
|
-
const
|
|
566
|
-
const
|
|
567
|
-
if (
|
|
466
|
+
const relPks = Object.values(relationConfig.foreignTable._.columns).filter((c) => c.options.primaryKey).map((c) => c._.name);
|
|
467
|
+
const key = relPks.map((pk) => directData[pk]).join("_");
|
|
468
|
+
if (relPks.length === 0 || !target[relName].some(
|
|
469
|
+
(r) => relPks.map((pk) => r[pk]).join("_") === key
|
|
470
|
+
)) {
|
|
568
471
|
const newItem = { ...directData };
|
|
569
472
|
if (Object.keys(nestedData).length > 0) {
|
|
570
|
-
attachRelations(newItem, nestedData, relationConfig.foreignTable
|
|
473
|
+
attachRelations(newItem, nestedData, relationConfig.foreignTable);
|
|
571
474
|
}
|
|
572
475
|
target[relName].push(newItem);
|
|
573
476
|
}
|
|
574
477
|
}
|
|
575
478
|
} else {
|
|
576
|
-
const directData = {};
|
|
577
|
-
const nestedData = {};
|
|
578
|
-
if (typeof relData === "object" && relData !== null) {
|
|
579
|
-
for (const [k, v] of Object.entries(relData)) {
|
|
580
|
-
if (typeof v === "object" && v !== null) {
|
|
581
|
-
nestedData[k] = v;
|
|
582
|
-
} else {
|
|
583
|
-
directData[k] = v;
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
const hasData = Object.values(directData).some(
|
|
588
|
-
(v) => v !== null && v !== void 0 && v !== ""
|
|
589
|
-
);
|
|
590
479
|
if (hasData || Object.keys(nestedData).length > 0) {
|
|
591
480
|
target[relName] = { ...directData };
|
|
592
481
|
if (Object.keys(nestedData).length > 0) {
|
|
593
|
-
attachRelations(
|
|
482
|
+
attachRelations(
|
|
483
|
+
target[relName],
|
|
484
|
+
nestedData,
|
|
485
|
+
relationConfig.foreignTable
|
|
486
|
+
);
|
|
594
487
|
}
|
|
595
488
|
}
|
|
596
489
|
}
|
|
597
490
|
}
|
|
598
491
|
};
|
|
599
|
-
attachRelations(result, relations2, this.
|
|
492
|
+
attachRelations(result, relations2, this._table);
|
|
600
493
|
}
|
|
601
494
|
return Array.from(groupedResults.values());
|
|
602
495
|
}
|
|
603
|
-
// Update the return type signatures
|
|
604
496
|
async all() {
|
|
605
497
|
return this.execute();
|
|
606
498
|
}
|
|
@@ -609,61 +501,32 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
609
501
|
const result = await this.execute();
|
|
610
502
|
return result[0];
|
|
611
503
|
}
|
|
504
|
+
async first() {
|
|
505
|
+
return this.get();
|
|
506
|
+
}
|
|
612
507
|
async exists() {
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
const whereIndex = this.query.indexOf(" WHERE ");
|
|
617
|
-
let fromPart = this.query;
|
|
618
|
-
let wherePart = "";
|
|
619
|
-
if (whereIndex !== -1) {
|
|
620
|
-
fromPart = this.query.substring(0, whereIndex);
|
|
621
|
-
wherePart = this.query.substring(whereIndex);
|
|
622
|
-
}
|
|
623
|
-
const query = `SELECT 1 ${fromPart}${joinSql}${wherePart} LIMIT 1`;
|
|
624
|
-
const params = [...joinParams, ...this.params];
|
|
625
|
-
this.selectedColumns = originalColumns;
|
|
626
|
-
const result = await this.db.select(query, params);
|
|
627
|
-
return result.length > 0;
|
|
508
|
+
this.applyIncludes();
|
|
509
|
+
const compiledResult = await this._builder.clearSelect().select(sql.raw("1").as("__exists__")).limit(1).execute();
|
|
510
|
+
return compiledResult.length > 0;
|
|
628
511
|
}
|
|
629
512
|
async count() {
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
const
|
|
633
|
-
const
|
|
634
|
-
|
|
635
|
-
let wherePart = "";
|
|
636
|
-
if (whereIndex !== -1) {
|
|
637
|
-
fromPart = this.query.substring(0, whereIndex);
|
|
638
|
-
wherePart = this.query.substring(whereIndex);
|
|
639
|
-
}
|
|
640
|
-
const query = `SELECT COUNT(*) as count ${fromPart}${joinSql}${wherePart}`;
|
|
641
|
-
const params = [...joinParams, ...this.params];
|
|
642
|
-
this.selectedColumns = originalColumns;
|
|
643
|
-
const result = await this.db.select(query, params);
|
|
644
|
-
return result[0]?.count || 0;
|
|
645
|
-
}
|
|
646
|
-
async first() {
|
|
647
|
-
return this.get();
|
|
513
|
+
this.applyIncludes();
|
|
514
|
+
const result = await this._builder.clearSelect().select(sql`COUNT(*)`.as("count")).execute();
|
|
515
|
+
const row = result[0];
|
|
516
|
+
const val = row ? row['"count"'] ?? row.count : void 0;
|
|
517
|
+
return Number(val ?? 0);
|
|
648
518
|
}
|
|
649
519
|
async pluck(column) {
|
|
650
|
-
|
|
651
|
-
const
|
|
652
|
-
|
|
653
|
-
const
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
}
|
|
661
|
-
const query = `SELECT ${this.selectedColumns.join(", ")} ${fromPart}${joinSql}${wherePart}`;
|
|
662
|
-
const params = [...joinParams, ...this.params];
|
|
663
|
-
this.selectedColumns = originalColumns;
|
|
664
|
-
const results = await this.db.select(query, params);
|
|
665
|
-
const col = this.table._.columns[column];
|
|
666
|
-
return results.map((row) => col ? deserializeValue(row[columnName], col) : row[columnName]);
|
|
520
|
+
this.applyIncludes();
|
|
521
|
+
const col = this._table._.columns[column];
|
|
522
|
+
const alias2 = col._.name;
|
|
523
|
+
const results = await this._builder.clearSelect().select(
|
|
524
|
+
sql.raw(`${this._table._.name}.${alias2}`).as(alias2)
|
|
525
|
+
).execute();
|
|
526
|
+
return results.map((row) => {
|
|
527
|
+
const val = row['"' + alias2 + '"'] ?? row[alias2];
|
|
528
|
+
return col ? deserializeValue(val, col) : val;
|
|
529
|
+
});
|
|
667
530
|
}
|
|
668
531
|
async paginate(page = 1, pageSize = 10) {
|
|
669
532
|
if (page < 1) page = 1;
|
|
@@ -683,16 +546,19 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
683
546
|
};
|
|
684
547
|
}
|
|
685
548
|
toSQL() {
|
|
686
|
-
|
|
687
|
-
const
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
549
|
+
this.applyIncludes();
|
|
550
|
+
const compiled = this._builder.compile();
|
|
551
|
+
return { sql: compiled.sql, params: [...compiled.parameters] };
|
|
552
|
+
}
|
|
553
|
+
toKyselyExpression() {
|
|
554
|
+
this.applyIncludes();
|
|
555
|
+
return this._builder;
|
|
693
556
|
}
|
|
694
557
|
};
|
|
695
558
|
|
|
559
|
+
// src/builders/update.ts
|
|
560
|
+
import { sql as sql2 } from "kysely";
|
|
561
|
+
|
|
696
562
|
// src/errors.ts
|
|
697
563
|
var TauriORMError = class extends Error {
|
|
698
564
|
constructor(message) {
|
|
@@ -761,414 +627,384 @@ var TableNotFoundError = class extends TauriORMError {
|
|
|
761
627
|
};
|
|
762
628
|
|
|
763
629
|
// src/builders/update.ts
|
|
764
|
-
var UpdateQueryBuilder = class
|
|
765
|
-
constructor(
|
|
766
|
-
|
|
767
|
-
this.
|
|
768
|
-
this.
|
|
769
|
-
}
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
630
|
+
var UpdateQueryBuilder = class {
|
|
631
|
+
constructor(kysely, table) {
|
|
632
|
+
this.kysely = kysely;
|
|
633
|
+
this._table = table;
|
|
634
|
+
this._builder = kysely.updateTable(table._.name);
|
|
635
|
+
}
|
|
636
|
+
_builder;
|
|
637
|
+
_table;
|
|
638
|
+
_updateData = {};
|
|
639
|
+
_returningColumns = [];
|
|
640
|
+
_hasWhereClause = false;
|
|
641
|
+
_allowGlobal = false;
|
|
642
|
+
_incrementDecrementOps = [];
|
|
775
643
|
set(data) {
|
|
776
|
-
this.
|
|
644
|
+
this._updateData = { ...this._updateData, ...data };
|
|
777
645
|
return this;
|
|
778
646
|
}
|
|
779
647
|
where(condition) {
|
|
780
|
-
this.
|
|
781
|
-
|
|
648
|
+
this._hasWhereClause = true;
|
|
649
|
+
this._builder = this._builder.where(condition);
|
|
650
|
+
return this;
|
|
782
651
|
}
|
|
783
652
|
increment(column, value = 1) {
|
|
784
|
-
const col = this.
|
|
785
|
-
if (!col)
|
|
786
|
-
|
|
787
|
-
}
|
|
788
|
-
this.incrementDecrementOps.push({ column: col._.name, op: "increment", value });
|
|
653
|
+
const col = this._table._.columns[column];
|
|
654
|
+
if (!col) throw new ColumnNotFoundError(String(column), this._table._.name);
|
|
655
|
+
this._incrementDecrementOps.push({ column: col._.name, op: "increment", value });
|
|
789
656
|
return this;
|
|
790
657
|
}
|
|
791
658
|
decrement(column, value = 1) {
|
|
792
|
-
const col = this.
|
|
793
|
-
if (!col)
|
|
794
|
-
|
|
795
|
-
}
|
|
796
|
-
this.incrementDecrementOps.push({ column: col._.name, op: "decrement", value });
|
|
659
|
+
const col = this._table._.columns[column];
|
|
660
|
+
if (!col) throw new ColumnNotFoundError(String(column), this._table._.name);
|
|
661
|
+
this._incrementDecrementOps.push({ column: col._.name, op: "decrement", value });
|
|
797
662
|
return this;
|
|
798
663
|
}
|
|
799
664
|
allowGlobalOperation() {
|
|
800
|
-
this.
|
|
665
|
+
this._allowGlobal = true;
|
|
801
666
|
return this;
|
|
802
667
|
}
|
|
803
668
|
returning(...columns) {
|
|
804
|
-
this.
|
|
669
|
+
this._returningColumns.push(...columns);
|
|
805
670
|
return this;
|
|
806
671
|
}
|
|
807
|
-
|
|
808
|
-
const
|
|
809
|
-
for (const [
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
672
|
+
mapReturningRows(rows) {
|
|
673
|
+
const dbNameToTs = {};
|
|
674
|
+
for (const [tsName, col] of Object.entries(this._table._.columns)) {
|
|
675
|
+
dbNameToTs[col._.name] = tsName;
|
|
676
|
+
}
|
|
677
|
+
const norm = (k) => k.startsWith('"') && k.endsWith('"') ? k.slice(1, -1) : k;
|
|
678
|
+
return rows.map((row) => {
|
|
679
|
+
const out = {};
|
|
680
|
+
for (const [dbKey, value] of Object.entries(row)) {
|
|
681
|
+
const logicalKey = norm(dbKey);
|
|
682
|
+
const tsName = dbNameToTs[logicalKey] ?? logicalKey;
|
|
683
|
+
const column = this._table._.columns[tsName];
|
|
684
|
+
out[tsName] = column ? deserializeValue(value, column) : value;
|
|
685
|
+
}
|
|
686
|
+
return out;
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
buildSetClause() {
|
|
690
|
+
const finalData = { ...this._updateData };
|
|
691
|
+
for (const [key, column] of Object.entries(this._table._.columns)) {
|
|
692
|
+
if (finalData[key] === void 0 && column.options.$onUpdateFn) {
|
|
693
|
+
;
|
|
694
|
+
finalData[key] = column.options.$onUpdateFn();
|
|
813
695
|
}
|
|
814
696
|
}
|
|
815
|
-
const
|
|
816
|
-
const whereParams = this.params;
|
|
817
|
-
let tablePart = baseQuery;
|
|
818
|
-
let whereClause = "";
|
|
819
|
-
const whereIndex = baseQuery.indexOf(" WHERE ");
|
|
820
|
-
if (whereIndex !== -1) {
|
|
821
|
-
tablePart = baseQuery.substring(0, whereIndex);
|
|
822
|
-
whereClause = baseQuery.substring(whereIndex);
|
|
823
|
-
}
|
|
824
|
-
const entries = Object.entries(finalUpdateData);
|
|
697
|
+
const entries = Object.entries(finalData);
|
|
825
698
|
const hasSetData = entries.length > 0;
|
|
826
|
-
const
|
|
827
|
-
if (!hasSetData && !
|
|
828
|
-
throw new UpdateValidationError(
|
|
699
|
+
const hasOps = this._incrementDecrementOps.length > 0;
|
|
700
|
+
if (!hasSetData && !hasOps) {
|
|
701
|
+
throw new UpdateValidationError(
|
|
702
|
+
"Cannot execute an update query without a .set(), .increment(), or .decrement() call."
|
|
703
|
+
);
|
|
829
704
|
}
|
|
830
|
-
const
|
|
831
|
-
const
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
if (!column) {
|
|
836
|
-
throw new ColumnNotFoundError(key, this.table._.name);
|
|
837
|
-
}
|
|
838
|
-
setClauses.push(`${column._.name} = ?`);
|
|
839
|
-
setParams.push(serializeValue(value, column));
|
|
840
|
-
}
|
|
705
|
+
const setMap = {};
|
|
706
|
+
for (const [key, value] of entries) {
|
|
707
|
+
const column = this._table._.columns[key];
|
|
708
|
+
if (!column) throw new ColumnNotFoundError(key, this._table._.name);
|
|
709
|
+
setMap[column._.name] = serializeValue(value, column);
|
|
841
710
|
}
|
|
842
|
-
for (const op of this.
|
|
711
|
+
for (const op of this._incrementDecrementOps) {
|
|
843
712
|
const sign = op.op === "increment" ? "+" : "-";
|
|
844
|
-
|
|
845
|
-
setParams.push(op.value);
|
|
713
|
+
setMap[op.column] = sql2.raw(`${op.column} ${sign} ${op.value}`);
|
|
846
714
|
}
|
|
847
|
-
|
|
848
|
-
const sql2 = `${tablePart} SET ${setClause}${whereClause}`;
|
|
849
|
-
const params = [...setParams, ...whereParams];
|
|
850
|
-
return { sql: sql2, params };
|
|
715
|
+
return setMap;
|
|
851
716
|
}
|
|
852
717
|
async execute() {
|
|
853
|
-
if (!this.
|
|
854
|
-
throw new MissingWhereClauseError("UPDATE", this.
|
|
855
|
-
}
|
|
856
|
-
const
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
const
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
const
|
|
863
|
-
return
|
|
718
|
+
if (!this._hasWhereClause && !this._allowGlobal) {
|
|
719
|
+
throw new MissingWhereClauseError("UPDATE", this._table._.name);
|
|
720
|
+
}
|
|
721
|
+
const setMap = this.buildSetClause();
|
|
722
|
+
let builder = this._builder.set(setMap);
|
|
723
|
+
if (this._returningColumns.length > 0) {
|
|
724
|
+
const cols = this._returningColumns.map(
|
|
725
|
+
(k) => this._table._.columns[k]._.name
|
|
726
|
+
);
|
|
727
|
+
const rows = await builder.returning(cols).execute();
|
|
728
|
+
return this.mapReturningRows(rows);
|
|
864
729
|
}
|
|
730
|
+
const result = await builder.executeTakeFirst();
|
|
731
|
+
return [{ rowsAffected: Number(result?.numUpdatedRows ?? 0) }];
|
|
865
732
|
}
|
|
866
733
|
async returningAll() {
|
|
867
|
-
const
|
|
868
|
-
|
|
869
|
-
);
|
|
870
|
-
return this.returning(...allColumns).execute();
|
|
734
|
+
const allCols = Object.keys(this._table._.columns);
|
|
735
|
+
return this.returning(...allCols).execute();
|
|
871
736
|
}
|
|
872
737
|
async returningFirst() {
|
|
873
|
-
const
|
|
874
|
-
this.table._.columns
|
|
875
|
-
);
|
|
876
|
-
const results = await this.returning(...allColumns).execute();
|
|
738
|
+
const results = await this.returningAll();
|
|
877
739
|
return results[0];
|
|
878
740
|
}
|
|
879
741
|
toSQL() {
|
|
880
|
-
const
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
};
|
|
742
|
+
const setMap = this.buildSetClause();
|
|
743
|
+
let builder = this._builder.set(setMap);
|
|
744
|
+
if (this._returningColumns.length > 0) {
|
|
745
|
+
builder = builder.returning(
|
|
746
|
+
this._returningColumns.map((k) => this._table._.columns[k]._.name)
|
|
747
|
+
);
|
|
887
748
|
}
|
|
888
|
-
|
|
749
|
+
const compiled = builder.compile();
|
|
750
|
+
return { sql: compiled.sql, params: [...compiled.parameters] };
|
|
889
751
|
}
|
|
890
752
|
};
|
|
891
753
|
|
|
892
754
|
// src/builders/insert.ts
|
|
893
|
-
var InsertQueryBuilder = class
|
|
894
|
-
constructor(
|
|
895
|
-
|
|
896
|
-
this.
|
|
897
|
-
this.
|
|
898
|
-
}
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
755
|
+
var InsertQueryBuilder = class {
|
|
756
|
+
constructor(kysely, table) {
|
|
757
|
+
this.kysely = kysely;
|
|
758
|
+
this._table = table;
|
|
759
|
+
this._builder = kysely.insertInto(table._.name);
|
|
760
|
+
}
|
|
761
|
+
_builder;
|
|
762
|
+
_table;
|
|
763
|
+
_dataSets = [];
|
|
764
|
+
_returningColumns = [];
|
|
765
|
+
_onConflictAction = null;
|
|
766
|
+
_conflictTarget = [];
|
|
767
|
+
_updateSet = {};
|
|
904
768
|
values(data) {
|
|
905
|
-
const
|
|
906
|
-
this.
|
|
769
|
+
const arr = Array.isArray(data) ? data : [data];
|
|
770
|
+
this._dataSets.push(...arr);
|
|
907
771
|
return this;
|
|
908
772
|
}
|
|
909
773
|
returning(...columns) {
|
|
910
|
-
this.
|
|
774
|
+
this._returningColumns.push(...columns);
|
|
911
775
|
return this;
|
|
912
776
|
}
|
|
913
777
|
onConflictDoNothing(target) {
|
|
914
|
-
this.
|
|
778
|
+
this._onConflictAction = "nothing";
|
|
915
779
|
if (target) {
|
|
916
|
-
this.
|
|
780
|
+
this._conflictTarget = Array.isArray(target) ? target : [target];
|
|
917
781
|
}
|
|
918
782
|
return this;
|
|
919
783
|
}
|
|
920
784
|
onConflictDoUpdate(config) {
|
|
921
|
-
this.
|
|
922
|
-
this.
|
|
923
|
-
this.
|
|
785
|
+
this._onConflictAction = "update";
|
|
786
|
+
this._conflictTarget = Array.isArray(config.target) ? config.target : [config.target];
|
|
787
|
+
this._updateSet = config.set;
|
|
924
788
|
return this;
|
|
925
789
|
}
|
|
926
|
-
|
|
927
|
-
const
|
|
928
|
-
for (const [key, column] of Object.entries(this.
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
finalData[typedKey] = column.options.$defaultFn();
|
|
933
|
-
}
|
|
790
|
+
processDefaults(data) {
|
|
791
|
+
const out = { ...data };
|
|
792
|
+
for (const [key, column] of Object.entries(this._table._.columns)) {
|
|
793
|
+
if (out[key] === void 0 && column.options.$defaultFn) {
|
|
794
|
+
;
|
|
795
|
+
out[key] = column.options.$defaultFn();
|
|
934
796
|
}
|
|
935
797
|
}
|
|
936
|
-
return
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
clause += ` DO UPDATE SET ${setClause}`;
|
|
798
|
+
return out;
|
|
799
|
+
}
|
|
800
|
+
mapReturningRows(rows) {
|
|
801
|
+
const dbNameToTs = {};
|
|
802
|
+
for (const [tsName, col] of Object.entries(this._table._.columns)) {
|
|
803
|
+
dbNameToTs[col._.name] = tsName;
|
|
804
|
+
}
|
|
805
|
+
const norm = (k) => k.startsWith('"') && k.endsWith('"') ? k.slice(1, -1) : k;
|
|
806
|
+
return rows.map((row) => {
|
|
807
|
+
const out = {};
|
|
808
|
+
for (const [dbKey, value] of Object.entries(row)) {
|
|
809
|
+
const logicalKey = norm(dbKey);
|
|
810
|
+
const tsName = dbNameToTs[logicalKey] ?? logicalKey;
|
|
811
|
+
const column = this._table._.columns[tsName];
|
|
812
|
+
out[tsName] = column ? deserializeValue(value, column) : value;
|
|
952
813
|
}
|
|
814
|
+
return out;
|
|
815
|
+
});
|
|
816
|
+
}
|
|
817
|
+
serializeDataSet(data) {
|
|
818
|
+
const out = {};
|
|
819
|
+
for (const [key, value] of Object.entries(data)) {
|
|
820
|
+
const column = this._table._.columns[key];
|
|
821
|
+
out[column ? column._.name : key] = column ? serializeValue(value, column) : value;
|
|
953
822
|
}
|
|
954
|
-
return
|
|
823
|
+
return out;
|
|
955
824
|
}
|
|
956
825
|
async execute() {
|
|
957
|
-
if (this.
|
|
958
|
-
throw new InsertValidationError(
|
|
959
|
-
|
|
960
|
-
const processedDataSets = this.dataSets.map(
|
|
961
|
-
(data) => this.processDefaultValues(data)
|
|
962
|
-
);
|
|
963
|
-
const groups = /* @__PURE__ */ new Map();
|
|
964
|
-
for (const dataSet of processedDataSets) {
|
|
965
|
-
const keys = Object.keys(dataSet).sort().join(",");
|
|
966
|
-
if (!groups.has(keys)) {
|
|
967
|
-
groups.set(keys, []);
|
|
968
|
-
}
|
|
969
|
-
groups.get(keys).push(dataSet);
|
|
970
|
-
}
|
|
971
|
-
let results = [];
|
|
972
|
-
let lastInsertId;
|
|
973
|
-
let rowsAffected = 0;
|
|
974
|
-
for (const [_, dataSets] of groups) {
|
|
975
|
-
const columns = Object.keys(dataSets[0]);
|
|
976
|
-
const columnNames = columns.map(
|
|
977
|
-
(key) => this.table._.columns[key]._.name
|
|
978
|
-
);
|
|
979
|
-
const placeholders = `(${columns.map(() => "?").join(", ")})`;
|
|
980
|
-
const valuesSql = dataSets.map(() => placeholders).join(", ");
|
|
981
|
-
const conflictClause = this.buildConflictClause();
|
|
982
|
-
const finalQuery = `${this.query} (${columnNames.join(
|
|
983
|
-
", "
|
|
984
|
-
)}) VALUES ${valuesSql}${conflictClause}`;
|
|
985
|
-
const params = dataSets.flatMap(
|
|
986
|
-
(data) => columns.map((col) => {
|
|
987
|
-
const value = data[col] ?? null;
|
|
988
|
-
const column = this.table._.columns[col];
|
|
989
|
-
return column ? serializeValue(value, column) : value;
|
|
990
|
-
})
|
|
826
|
+
if (this._dataSets.length === 0) {
|
|
827
|
+
throw new InsertValidationError(
|
|
828
|
+
"No data provided for insert. Use .values() to provide data."
|
|
991
829
|
);
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
830
|
+
}
|
|
831
|
+
const processed = this._dataSets.map((d) => this.serializeDataSet(this.processDefaults(d)));
|
|
832
|
+
let builder = this._builder.values(processed.length === 1 ? processed[0] : processed);
|
|
833
|
+
if (this._onConflictAction === "nothing") {
|
|
834
|
+
if (this._conflictTarget.length > 0) {
|
|
835
|
+
const targetCols = this._conflictTarget.map((c) => c._.name);
|
|
836
|
+
builder = builder.onConflict(
|
|
837
|
+
(oc) => oc.columns(targetCols).doNothing()
|
|
998
838
|
);
|
|
999
|
-
params.push(...setValues);
|
|
1000
|
-
}
|
|
1001
|
-
if (this.returningColumns.length > 0) {
|
|
1002
|
-
const returningNames = this.returningColumns.map((col) => this.table._.columns[col]._.name).join(", ");
|
|
1003
|
-
const queryWithReturning = `${finalQuery} RETURNING ${returningNames}`;
|
|
1004
|
-
const rows = await this.db.select(queryWithReturning, params);
|
|
1005
|
-
results = results.concat(rows);
|
|
1006
839
|
} else {
|
|
1007
|
-
|
|
1008
|
-
lastInsertId = result.lastInsertId;
|
|
1009
|
-
rowsAffected += result.rowsAffected;
|
|
840
|
+
builder = builder.onConflict((oc) => oc.doNothing());
|
|
1010
841
|
}
|
|
842
|
+
} else if (this._onConflictAction === "update") {
|
|
843
|
+
const targetCols = this._conflictTarget.map((c) => c._.name);
|
|
844
|
+
const updateData = this.serializeDataSet(this._updateSet);
|
|
845
|
+
builder = builder.onConflict(
|
|
846
|
+
(oc) => oc.columns(targetCols).doUpdateSet(updateData)
|
|
847
|
+
);
|
|
1011
848
|
}
|
|
1012
|
-
if (this.
|
|
1013
|
-
|
|
849
|
+
if (this._returningColumns.length > 0) {
|
|
850
|
+
const cols = this._returningColumns.map(
|
|
851
|
+
(k) => this._table._.columns[k]._.name
|
|
852
|
+
);
|
|
853
|
+
const rows = await builder.returning(cols).execute();
|
|
854
|
+
return this.mapReturningRows(rows);
|
|
1014
855
|
}
|
|
1015
|
-
|
|
856
|
+
const result = await builder.executeTakeFirst();
|
|
857
|
+
return [
|
|
858
|
+
{
|
|
859
|
+
lastInsertId: Number(result?.insertId ?? 0),
|
|
860
|
+
rowsAffected: Number(result?.numInsertedOrUpdatedRows ?? 0)
|
|
861
|
+
}
|
|
862
|
+
];
|
|
1016
863
|
}
|
|
1017
864
|
async returningAll() {
|
|
1018
|
-
const
|
|
1019
|
-
|
|
1020
|
-
);
|
|
1021
|
-
return this.returning(...allColumns).execute();
|
|
865
|
+
const allCols = Object.keys(this._table._.columns);
|
|
866
|
+
return this.returning(...allCols).execute();
|
|
1022
867
|
}
|
|
1023
868
|
async returningFirst() {
|
|
1024
|
-
const
|
|
1025
|
-
this.table._.columns
|
|
1026
|
-
);
|
|
1027
|
-
const results = await this.returning(...allColumns).execute();
|
|
869
|
+
const results = await this.returningAll();
|
|
1028
870
|
return results[0];
|
|
1029
871
|
}
|
|
1030
872
|
toSQL() {
|
|
1031
|
-
if (this.
|
|
1032
|
-
throw new InsertValidationError(
|
|
873
|
+
if (this._dataSets.length === 0) {
|
|
874
|
+
throw new InsertValidationError(
|
|
875
|
+
"No data provided for insert. Use .values() to provide data."
|
|
876
|
+
);
|
|
1033
877
|
}
|
|
1034
|
-
const
|
|
1035
|
-
|
|
1036
|
-
)
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
const params = processedDataSets.flatMap(
|
|
1049
|
-
(data) => columns.map((col) => {
|
|
1050
|
-
const value = data[col] ?? null;
|
|
1051
|
-
const column = this.table._.columns[col];
|
|
1052
|
-
return column ? serializeValue(value, column) : value;
|
|
1053
|
-
})
|
|
1054
|
-
);
|
|
1055
|
-
if (this.onConflictAction === "update") {
|
|
1056
|
-
const setValues = Object.entries(this.updateSet).map(
|
|
1057
|
-
([key, value]) => {
|
|
1058
|
-
const column = this.table._.columns[key];
|
|
1059
|
-
return column ? serializeValue(value, column) : value;
|
|
1060
|
-
}
|
|
878
|
+
const processed = this._dataSets.map((d) => this.serializeDataSet(this.processDefaults(d)));
|
|
879
|
+
let builder = this._builder.values(processed.length === 1 ? processed[0] : processed);
|
|
880
|
+
if (this._onConflictAction === "nothing") {
|
|
881
|
+
if (this._conflictTarget.length > 0) {
|
|
882
|
+
builder = builder.onConflict(
|
|
883
|
+
(oc) => oc.columns(this._conflictTarget.map((c) => c._.name)).doNothing()
|
|
884
|
+
);
|
|
885
|
+
} else {
|
|
886
|
+
builder = builder.onConflict((oc) => oc.doNothing());
|
|
887
|
+
}
|
|
888
|
+
} else if (this._onConflictAction === "update") {
|
|
889
|
+
const updateData = this.serializeDataSet(this._updateSet);
|
|
890
|
+
builder = builder.onConflict(
|
|
891
|
+
(oc) => oc.columns(this._conflictTarget.map((c) => c._.name)).doUpdateSet(updateData)
|
|
1061
892
|
);
|
|
1062
|
-
params.push(...setValues);
|
|
1063
893
|
}
|
|
1064
|
-
if (this.
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
params
|
|
1069
|
-
};
|
|
894
|
+
if (this._returningColumns.length > 0) {
|
|
895
|
+
builder = builder.returning(
|
|
896
|
+
this._returningColumns.map((k) => this._table._.columns[k]._.name)
|
|
897
|
+
);
|
|
1070
898
|
}
|
|
1071
|
-
|
|
899
|
+
const compiled = builder.compile();
|
|
900
|
+
return { sql: compiled.sql, params: [...compiled.parameters] };
|
|
1072
901
|
}
|
|
1073
902
|
};
|
|
1074
903
|
|
|
1075
904
|
// src/builders/delete.ts
|
|
1076
|
-
var DeleteQueryBuilder = class
|
|
1077
|
-
constructor(
|
|
1078
|
-
|
|
1079
|
-
this.
|
|
1080
|
-
this.
|
|
1081
|
-
}
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
905
|
+
var DeleteQueryBuilder = class {
|
|
906
|
+
constructor(kysely, table) {
|
|
907
|
+
this.kysely = kysely;
|
|
908
|
+
this._table = table;
|
|
909
|
+
this._builder = kysely.deleteFrom(table._.name);
|
|
910
|
+
}
|
|
911
|
+
_builder;
|
|
912
|
+
_table;
|
|
913
|
+
_returningColumns = [];
|
|
914
|
+
_hasWhereClause = false;
|
|
915
|
+
_allowGlobal = false;
|
|
916
|
+
mapReturningRows(rows) {
|
|
917
|
+
const dbNameToTs = {};
|
|
918
|
+
for (const [tsName, col] of Object.entries(this._table._.columns)) {
|
|
919
|
+
dbNameToTs[col._.name] = tsName;
|
|
920
|
+
}
|
|
921
|
+
const norm = (k) => k.startsWith('"') && k.endsWith('"') ? k.slice(1, -1) : k;
|
|
922
|
+
return rows.map((row) => {
|
|
923
|
+
const out = {};
|
|
924
|
+
for (const [dbKey, value] of Object.entries(row)) {
|
|
925
|
+
const logicalKey = norm(dbKey);
|
|
926
|
+
const tsName = dbNameToTs[logicalKey] ?? logicalKey;
|
|
927
|
+
const column = this._table._.columns[tsName];
|
|
928
|
+
out[tsName] = column ? deserializeValue(value, column) : value;
|
|
929
|
+
}
|
|
930
|
+
return out;
|
|
931
|
+
});
|
|
932
|
+
}
|
|
1085
933
|
where(condition) {
|
|
1086
|
-
this.
|
|
1087
|
-
|
|
934
|
+
this._hasWhereClause = true;
|
|
935
|
+
this._builder = this._builder.where(condition);
|
|
936
|
+
return this;
|
|
1088
937
|
}
|
|
1089
938
|
allowGlobalOperation() {
|
|
1090
|
-
this.
|
|
939
|
+
this._allowGlobal = true;
|
|
1091
940
|
return this;
|
|
1092
941
|
}
|
|
1093
942
|
returning(...columns) {
|
|
1094
|
-
this.
|
|
943
|
+
this._returningColumns.push(...columns);
|
|
1095
944
|
return this;
|
|
1096
945
|
}
|
|
1097
946
|
async execute() {
|
|
1098
|
-
if (!this.
|
|
1099
|
-
throw new MissingWhereClauseError("DELETE", this.
|
|
947
|
+
if (!this._hasWhereClause && !this._allowGlobal) {
|
|
948
|
+
throw new MissingWhereClauseError("DELETE", this._table._.name);
|
|
1100
949
|
}
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
const result = await this.db.execute(sql2, params);
|
|
1108
|
-
return [{ rowsAffected: result.rowsAffected }];
|
|
950
|
+
if (this._returningColumns.length > 0) {
|
|
951
|
+
const cols = this._returningColumns.map(
|
|
952
|
+
(k) => this._table._.columns[k]._.name
|
|
953
|
+
);
|
|
954
|
+
const rows = await this._builder.returning(cols).execute();
|
|
955
|
+
return this.mapReturningRows(rows);
|
|
1109
956
|
}
|
|
957
|
+
const result = await this._builder.executeTakeFirst();
|
|
958
|
+
return [{ rowsAffected: Number(result?.numDeletedRows ?? 0) }];
|
|
1110
959
|
}
|
|
1111
960
|
async returningAll() {
|
|
1112
|
-
const
|
|
1113
|
-
return this.returning(...
|
|
961
|
+
const allCols = Object.keys(this._table._.columns);
|
|
962
|
+
return this.returning(...allCols).execute();
|
|
1114
963
|
}
|
|
1115
964
|
async returningFirst() {
|
|
1116
|
-
const
|
|
1117
|
-
const results = await this.returning(...allColumns).execute();
|
|
965
|
+
const results = await this.returningAll();
|
|
1118
966
|
return results[0];
|
|
1119
967
|
}
|
|
1120
968
|
toSQL() {
|
|
1121
|
-
|
|
1122
|
-
if (this.
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
params
|
|
1127
|
-
};
|
|
969
|
+
let builder = this._builder;
|
|
970
|
+
if (this._returningColumns.length > 0) {
|
|
971
|
+
builder = builder.returning(
|
|
972
|
+
this._returningColumns.map((k) => this._table._.columns[k]._.name)
|
|
973
|
+
);
|
|
1128
974
|
}
|
|
1129
|
-
|
|
975
|
+
const compiled = builder.compile();
|
|
976
|
+
return { sql: compiled.sql, params: [...compiled.parameters] };
|
|
1130
977
|
}
|
|
1131
978
|
};
|
|
1132
979
|
|
|
1133
980
|
// src/builders/with.ts
|
|
1134
981
|
var WithQueryBuilder = class {
|
|
1135
|
-
constructor(
|
|
1136
|
-
this.
|
|
982
|
+
constructor(kysely) {
|
|
983
|
+
this.kysely = kysely;
|
|
1137
984
|
}
|
|
1138
|
-
|
|
985
|
+
_ctes = [];
|
|
1139
986
|
with(alias2, query) {
|
|
1140
|
-
this.
|
|
987
|
+
this._ctes.push({ alias: alias2, query: query.toKyselyExpression() });
|
|
1141
988
|
return this;
|
|
1142
989
|
}
|
|
990
|
+
applyWith(builder) {
|
|
991
|
+
let b = builder;
|
|
992
|
+
for (const { alias: alias2, query } of this._ctes) {
|
|
993
|
+
b = b.with(alias2, () => query);
|
|
994
|
+
}
|
|
995
|
+
return b;
|
|
996
|
+
}
|
|
1143
997
|
select(table, columns) {
|
|
1144
|
-
|
|
1145
|
-
this.applyWithClause(builder);
|
|
1146
|
-
return builder;
|
|
998
|
+
return new SelectQueryBuilder(this.kysely, table, columns);
|
|
1147
999
|
}
|
|
1148
1000
|
insert(table) {
|
|
1149
|
-
|
|
1150
|
-
this.applyWithClause(builder);
|
|
1151
|
-
return builder;
|
|
1001
|
+
return new InsertQueryBuilder(this.kysely, table);
|
|
1152
1002
|
}
|
|
1153
1003
|
update(table) {
|
|
1154
|
-
|
|
1155
|
-
this.applyWithClause(builder);
|
|
1156
|
-
return builder;
|
|
1004
|
+
return new UpdateQueryBuilder(this.kysely, table);
|
|
1157
1005
|
}
|
|
1158
1006
|
delete(table) {
|
|
1159
|
-
|
|
1160
|
-
this.applyWithClause(builder);
|
|
1161
|
-
return builder;
|
|
1162
|
-
}
|
|
1163
|
-
applyWithClause(builder) {
|
|
1164
|
-
if (this.ctes.length > 0) {
|
|
1165
|
-
const cteSql = this.ctes.map((cte) => `${cte.alias} AS (${cte.query})`).join(", ");
|
|
1166
|
-
builder["query"] = `WITH ${cteSql} ${builder["query"]}`;
|
|
1167
|
-
builder["params"] = [
|
|
1168
|
-
...this.ctes.flatMap((cte) => cte.params),
|
|
1169
|
-
...builder["params"]
|
|
1170
|
-
];
|
|
1171
|
-
}
|
|
1007
|
+
return new DeleteQueryBuilder(this.kysely, table);
|
|
1172
1008
|
}
|
|
1173
1009
|
};
|
|
1174
1010
|
|
|
@@ -1211,7 +1047,7 @@ var SQLiteColumn = class _SQLiteColumn {
|
|
|
1211
1047
|
unique() {
|
|
1212
1048
|
return new _SQLiteColumn(this._.name, this.type, { ...this.options, unique: true, mode: this._.mode });
|
|
1213
1049
|
}
|
|
1214
|
-
references(ref, column) {
|
|
1050
|
+
references(ref, column, options) {
|
|
1215
1051
|
const columnKey = typeof column === "string" ? column : column._.name;
|
|
1216
1052
|
const columnObj = typeof column === "string" ? ref._.columns[column] : column;
|
|
1217
1053
|
return new _SQLiteColumn(
|
|
@@ -1221,7 +1057,9 @@ var SQLiteColumn = class _SQLiteColumn {
|
|
|
1221
1057
|
...this.options,
|
|
1222
1058
|
references: {
|
|
1223
1059
|
table: ref,
|
|
1224
|
-
column: columnObj
|
|
1060
|
+
column: columnObj,
|
|
1061
|
+
onDelete: options?.onDelete,
|
|
1062
|
+
onUpdate: options?.onUpdate
|
|
1225
1063
|
},
|
|
1226
1064
|
mode: this._.mode
|
|
1227
1065
|
}
|
|
@@ -1250,39 +1088,14 @@ var Table = class {
|
|
|
1250
1088
|
var sqliteTable = (tableName, columns) => {
|
|
1251
1089
|
return new Table(tableName, columns);
|
|
1252
1090
|
};
|
|
1253
|
-
var asc = (column) => (
|
|
1254
|
-
|
|
1255
|
-
params: []
|
|
1256
|
-
});
|
|
1257
|
-
var desc = (column) => ({
|
|
1258
|
-
sql: `${column._.name} DESC`,
|
|
1259
|
-
params: []
|
|
1260
|
-
});
|
|
1261
|
-
var sql = (strings, ...values) => {
|
|
1262
|
-
const queryParts = [];
|
|
1263
|
-
const params = [];
|
|
1264
|
-
strings.forEach((str, i) => {
|
|
1265
|
-
queryParts.push(str);
|
|
1266
|
-
if (values[i] !== void 0) {
|
|
1267
|
-
if (typeof values[i] === "object" && values[i].sql) {
|
|
1268
|
-
queryParts.push(values[i].sql);
|
|
1269
|
-
params.push(...values[i].params);
|
|
1270
|
-
} else {
|
|
1271
|
-
queryParts.push("?");
|
|
1272
|
-
params.push(values[i]);
|
|
1273
|
-
}
|
|
1274
|
-
}
|
|
1275
|
-
});
|
|
1276
|
-
return {
|
|
1277
|
-
sql: queryParts.join(""),
|
|
1278
|
-
params
|
|
1279
|
-
};
|
|
1280
|
-
};
|
|
1091
|
+
var asc = (column) => kyselySql`${kyselySql.ref(column._.name)} ASC`;
|
|
1092
|
+
var desc = (column) => kyselySql`${kyselySql.ref(column._.name)} DESC`;
|
|
1281
1093
|
var TauriORM = class {
|
|
1282
1094
|
constructor(db, schema = void 0) {
|
|
1283
1095
|
this.db = db;
|
|
1096
|
+
this.kysely = new Kysely4({ dialect: new TauriDialect(db) });
|
|
1284
1097
|
if (schema) {
|
|
1285
|
-
for (const [
|
|
1098
|
+
for (const [, value] of Object.entries(schema)) {
|
|
1286
1099
|
if (value instanceof Table) {
|
|
1287
1100
|
this.tables.set(value._.name, value);
|
|
1288
1101
|
}
|
|
@@ -1290,24 +1103,31 @@ var TauriORM = class {
|
|
|
1290
1103
|
}
|
|
1291
1104
|
}
|
|
1292
1105
|
tables = /* @__PURE__ */ new Map();
|
|
1106
|
+
kysely;
|
|
1293
1107
|
buildColumnDefinition(col, forAlterTable = false) {
|
|
1294
|
-
let
|
|
1108
|
+
let sql6 = `${col._.name} ${col.type}`;
|
|
1295
1109
|
if (col.options.primaryKey && !forAlterTable) {
|
|
1296
|
-
|
|
1110
|
+
sql6 += " PRIMARY KEY";
|
|
1297
1111
|
if (col._.autoincrement) {
|
|
1298
|
-
|
|
1112
|
+
sql6 += " AUTOINCREMENT";
|
|
1299
1113
|
}
|
|
1300
1114
|
}
|
|
1301
|
-
if (col._.notNull)
|
|
1302
|
-
if (col.options.unique)
|
|
1115
|
+
if (col._.notNull) sql6 += " NOT NULL";
|
|
1116
|
+
if (col.options.unique) sql6 += " UNIQUE";
|
|
1303
1117
|
if (col.options.default !== void 0) {
|
|
1304
1118
|
const value = col.options.default;
|
|
1305
|
-
|
|
1119
|
+
sql6 += ` DEFAULT ${typeof value === "string" ? `'${value.replace(/'/g, "''")}'` : value}`;
|
|
1306
1120
|
}
|
|
1307
1121
|
if (col.options.references) {
|
|
1308
|
-
|
|
1122
|
+
sql6 += ` REFERENCES ${col.options.references.table._.name}(${col.options.references.column._.name})`;
|
|
1123
|
+
if (col.options.references.onDelete) {
|
|
1124
|
+
sql6 += ` ON DELETE ${col.options.references.onDelete.toUpperCase()}`;
|
|
1125
|
+
}
|
|
1126
|
+
if (col.options.references.onUpdate) {
|
|
1127
|
+
sql6 += ` ON UPDATE ${col.options.references.onUpdate.toUpperCase()}`;
|
|
1128
|
+
}
|
|
1309
1129
|
}
|
|
1310
|
-
return
|
|
1130
|
+
return sql6;
|
|
1311
1131
|
}
|
|
1312
1132
|
async checkMigration() {
|
|
1313
1133
|
const dbTables = await this.db.select(
|
|
@@ -1349,15 +1169,16 @@ var TauriORM = class {
|
|
|
1349
1169
|
const schemaColumns = table._.columns;
|
|
1350
1170
|
let needsRecreate = false;
|
|
1351
1171
|
for (const [colName, column] of Object.entries(schemaColumns)) {
|
|
1352
|
-
const
|
|
1172
|
+
const dbColName = column._.name;
|
|
1173
|
+
const existing = existingColumns.get(dbColName);
|
|
1353
1174
|
if (!existing) {
|
|
1354
1175
|
if (!this.canAddColumnWithAlter(column)) {
|
|
1355
1176
|
needsRecreate = true;
|
|
1356
1177
|
break;
|
|
1357
1178
|
}
|
|
1358
|
-
changes.columnsToAdd.push({ table: tableName, column:
|
|
1179
|
+
changes.columnsToAdd.push({ table: tableName, column: dbColName });
|
|
1359
1180
|
} else {
|
|
1360
|
-
const hasUniqueInDB = uniqueColumns.has(
|
|
1181
|
+
const hasUniqueInDB = uniqueColumns.has(dbColName);
|
|
1361
1182
|
const wantsUnique = !!column.options.unique;
|
|
1362
1183
|
if (hasUniqueInDB !== wantsUnique || this.hasColumnDefinitionChanged(column, existing)) {
|
|
1363
1184
|
needsRecreate = true;
|
|
@@ -1366,7 +1187,8 @@ var TauriORM = class {
|
|
|
1366
1187
|
}
|
|
1367
1188
|
}
|
|
1368
1189
|
for (const existingCol of existingColumns.keys()) {
|
|
1369
|
-
|
|
1190
|
+
const schemaHasCol = Object.values(schemaColumns).some((c) => c._.name === existingCol);
|
|
1191
|
+
if (!schemaHasCol) {
|
|
1370
1192
|
needsRecreate = true;
|
|
1371
1193
|
break;
|
|
1372
1194
|
}
|
|
@@ -1418,7 +1240,8 @@ var TauriORM = class {
|
|
|
1418
1240
|
let needsRecreate = false;
|
|
1419
1241
|
const columnsToAdd = [];
|
|
1420
1242
|
for (const [colName, column] of Object.entries(schemaColumns)) {
|
|
1421
|
-
const
|
|
1243
|
+
const dbColName = column._.name;
|
|
1244
|
+
const existing = existingColumns.get(dbColName);
|
|
1422
1245
|
if (!existing) {
|
|
1423
1246
|
if (this.canAddColumnWithAlter(column)) {
|
|
1424
1247
|
columnsToAdd.push(column);
|
|
@@ -1427,7 +1250,7 @@ var TauriORM = class {
|
|
|
1427
1250
|
break;
|
|
1428
1251
|
}
|
|
1429
1252
|
} else {
|
|
1430
|
-
const hasUniqueInDB = uniqueColumns.has(
|
|
1253
|
+
const hasUniqueInDB = uniqueColumns.has(dbColName);
|
|
1431
1254
|
const wantsUnique = !!column.options.unique;
|
|
1432
1255
|
if (hasUniqueInDB !== wantsUnique) {
|
|
1433
1256
|
needsRecreate = true;
|
|
@@ -1441,7 +1264,8 @@ var TauriORM = class {
|
|
|
1441
1264
|
}
|
|
1442
1265
|
if (options?.performDestructiveActions) {
|
|
1443
1266
|
for (const existingCol of existingColumns.keys()) {
|
|
1444
|
-
|
|
1267
|
+
const schemaHasCol = Object.values(schemaColumns).some((c) => c._.name === existingCol);
|
|
1268
|
+
if (!schemaHasCol) {
|
|
1445
1269
|
needsRecreate = true;
|
|
1446
1270
|
break;
|
|
1447
1271
|
}
|
|
@@ -1486,7 +1310,7 @@ var TauriORM = class {
|
|
|
1486
1310
|
await this.db.execute(`CREATE TABLE ${tempTableName} (${columnsSql})`);
|
|
1487
1311
|
const oldColumns = await this.db.select(`PRAGMA table_info('${tableName}')`);
|
|
1488
1312
|
const oldColumnNames = oldColumns.map((c) => c.name);
|
|
1489
|
-
const newColumnNames = Object.
|
|
1313
|
+
const newColumnNames = Object.values(table._.columns).map((c) => c._.name);
|
|
1490
1314
|
const commonColumns = oldColumnNames.filter((name) => newColumnNames.includes(name));
|
|
1491
1315
|
if (commonColumns.length > 0) {
|
|
1492
1316
|
const columnsList = commonColumns.join(", ");
|
|
@@ -1503,18 +1327,18 @@ var TauriORM = class {
|
|
|
1503
1327
|
console.warn(
|
|
1504
1328
|
`[Tauri-ORM] Table "${table._.name}" was not passed in the schema to the ORM constructor. Relations will not be available.`
|
|
1505
1329
|
);
|
|
1506
|
-
return new SelectQueryBuilder(this.
|
|
1330
|
+
return new SelectQueryBuilder(this.kysely, table, columns);
|
|
1507
1331
|
}
|
|
1508
|
-
return new SelectQueryBuilder(this.
|
|
1332
|
+
return new SelectQueryBuilder(this.kysely, internalTable, columns);
|
|
1509
1333
|
}
|
|
1510
1334
|
insert(table) {
|
|
1511
|
-
return new InsertQueryBuilder(this.
|
|
1335
|
+
return new InsertQueryBuilder(this.kysely, table);
|
|
1512
1336
|
}
|
|
1513
1337
|
update(table) {
|
|
1514
|
-
return new UpdateQueryBuilder(this.
|
|
1338
|
+
return new UpdateQueryBuilder(this.kysely, table);
|
|
1515
1339
|
}
|
|
1516
1340
|
delete(table) {
|
|
1517
|
-
return new DeleteQueryBuilder(this.
|
|
1341
|
+
return new DeleteQueryBuilder(this.kysely, table);
|
|
1518
1342
|
}
|
|
1519
1343
|
async upsert(table, data, conflictTarget) {
|
|
1520
1344
|
const columns = conflictTarget.map((col) => table._.columns[col]);
|
|
@@ -1524,7 +1348,7 @@ var TauriORM = class {
|
|
|
1524
1348
|
}).execute();
|
|
1525
1349
|
}
|
|
1526
1350
|
$with(alias2) {
|
|
1527
|
-
const withBuilder = new WithQueryBuilder(this.
|
|
1351
|
+
const withBuilder = new WithQueryBuilder(this.kysely);
|
|
1528
1352
|
return {
|
|
1529
1353
|
as: (query) => {
|
|
1530
1354
|
withBuilder.with(alias2, query);
|
|
@@ -1533,14 +1357,14 @@ var TauriORM = class {
|
|
|
1533
1357
|
};
|
|
1534
1358
|
}
|
|
1535
1359
|
async transaction(callback) {
|
|
1536
|
-
await this.db.execute("BEGIN
|
|
1360
|
+
await this.db.execute("BEGIN");
|
|
1537
1361
|
try {
|
|
1538
1362
|
const result = await callback(this);
|
|
1539
1363
|
await this.db.execute("COMMIT");
|
|
1540
1364
|
return result;
|
|
1541
|
-
} catch (
|
|
1365
|
+
} catch (e) {
|
|
1542
1366
|
await this.db.execute("ROLLBACK");
|
|
1543
|
-
throw
|
|
1367
|
+
throw e;
|
|
1544
1368
|
}
|
|
1545
1369
|
}
|
|
1546
1370
|
rollback() {
|
|
@@ -1592,7 +1416,9 @@ var TauriORM = class {
|
|
|
1592
1416
|
unique: !!col.options.unique,
|
|
1593
1417
|
dv: col.options.default && typeof col.options.default === "object" && col.options.default.raw ? { raw: col.options.default.raw } : col.options.default ?? null,
|
|
1594
1418
|
hasDefaultFn: col.options.$defaultFn !== void 0,
|
|
1595
|
-
hasOnUpdateFn: col.options.$onUpdateFn !== void 0
|
|
1419
|
+
hasOnUpdateFn: col.options.$onUpdateFn !== void 0,
|
|
1420
|
+
onDelete: col.options.references?.onDelete ?? null,
|
|
1421
|
+
onUpdate: col.options.references?.onUpdate ?? null
|
|
1596
1422
|
};
|
|
1597
1423
|
}
|
|
1598
1424
|
computeModelSignature() {
|
|
@@ -1660,12 +1486,6 @@ var ManyRelation = class extends Relation {
|
|
|
1660
1486
|
super(foreignTable);
|
|
1661
1487
|
}
|
|
1662
1488
|
};
|
|
1663
|
-
var ManyToManyRelation = class extends Relation {
|
|
1664
|
-
constructor(foreignTable, config) {
|
|
1665
|
-
super(foreignTable);
|
|
1666
|
-
this.config = config;
|
|
1667
|
-
}
|
|
1668
|
-
};
|
|
1669
1489
|
var relations = (table, relationsCallback) => {
|
|
1670
1490
|
const builtRelations = relationsCallback({
|
|
1671
1491
|
one: (foreignTable, config) => {
|
|
@@ -1673,9 +1493,6 @@ var relations = (table, relationsCallback) => {
|
|
|
1673
1493
|
},
|
|
1674
1494
|
many: (foreignTable) => {
|
|
1675
1495
|
return new ManyRelation(foreignTable);
|
|
1676
|
-
},
|
|
1677
|
-
manyToMany: (foreignTable, config) => {
|
|
1678
|
-
return new ManyToManyRelation(foreignTable, config);
|
|
1679
1496
|
}
|
|
1680
1497
|
});
|
|
1681
1498
|
for (const [name, relation] of Object.entries(builtRelations)) {
|
|
@@ -1691,14 +1508,6 @@ var relations = (table, relationsCallback) => {
|
|
|
1691
1508
|
type: "many",
|
|
1692
1509
|
foreignTable: relation.foreignTable
|
|
1693
1510
|
};
|
|
1694
|
-
} else if (relation instanceof ManyToManyRelation) {
|
|
1695
|
-
table.relations[name] = {
|
|
1696
|
-
type: "manyToMany",
|
|
1697
|
-
foreignTable: relation.foreignTable,
|
|
1698
|
-
junctionTable: relation.config.junctionTable,
|
|
1699
|
-
junctionFields: relation.config.junctionFields,
|
|
1700
|
-
junctionReferences: relation.config.junctionReferences
|
|
1701
|
-
};
|
|
1702
1511
|
}
|
|
1703
1512
|
}
|
|
1704
1513
|
return builtRelations;
|
|
@@ -1710,51 +1519,101 @@ var alias = (table, alias2) => {
|
|
|
1710
1519
|
return table;
|
|
1711
1520
|
};
|
|
1712
1521
|
|
|
1522
|
+
// src/operators.ts
|
|
1523
|
+
import { isExpression, sql as sql3 } from "kysely";
|
|
1524
|
+
var eq = (column, value, tableAlias) => {
|
|
1525
|
+
const colRef = tableAlias ? `${tableAlias}.${column._.name}` : column._.name;
|
|
1526
|
+
const serialized = serializeValue(value, column);
|
|
1527
|
+
return sql3`${sql3.ref(colRef)} = ${sql3.val(serialized)}`;
|
|
1528
|
+
};
|
|
1529
|
+
var ne = (column, value, tableAlias) => {
|
|
1530
|
+
const colRef = tableAlias ? `${tableAlias}.${column._.name}` : column._.name;
|
|
1531
|
+
const serialized = serializeValue(value, column);
|
|
1532
|
+
return sql3`${sql3.ref(colRef)} != ${sql3.val(serialized)}`;
|
|
1533
|
+
};
|
|
1534
|
+
var and = (...conditions) => {
|
|
1535
|
+
if (conditions.length === 0) return sql3`1 = 1`;
|
|
1536
|
+
if (conditions.length === 1) return conditions[0];
|
|
1537
|
+
return sql3`(${sql3.join(conditions.map((c) => sql3`(${c})`), sql3` AND `)})`;
|
|
1538
|
+
};
|
|
1539
|
+
var or = (...conditions) => {
|
|
1540
|
+
if (conditions.length === 0) return sql3`1 = 1`;
|
|
1541
|
+
if (conditions.length === 1) return conditions[0];
|
|
1542
|
+
return sql3`(${sql3.join(conditions.map((c) => sql3`(${c})`), sql3` OR `)})`;
|
|
1543
|
+
};
|
|
1544
|
+
var not = (condition) => sql3`NOT (${condition})`;
|
|
1545
|
+
var gt = (column, value) => {
|
|
1546
|
+
const serialized = serializeValue(value, column);
|
|
1547
|
+
return sql3`${sql3.ref(column._.name)} > ${sql3.val(serialized)}`;
|
|
1548
|
+
};
|
|
1549
|
+
var gte = (column, value) => {
|
|
1550
|
+
const serialized = serializeValue(value, column);
|
|
1551
|
+
return sql3`${sql3.ref(column._.name)} >= ${sql3.val(serialized)}`;
|
|
1552
|
+
};
|
|
1553
|
+
var lt = (column, value) => {
|
|
1554
|
+
const serialized = serializeValue(value, column);
|
|
1555
|
+
return sql3`${sql3.ref(column._.name)} < ${sql3.val(serialized)}`;
|
|
1556
|
+
};
|
|
1557
|
+
var lte = (column, value) => {
|
|
1558
|
+
const serialized = serializeValue(value, column);
|
|
1559
|
+
return sql3`${sql3.ref(column._.name)} <= ${sql3.val(serialized)}`;
|
|
1560
|
+
};
|
|
1561
|
+
var like = (column, pattern) => sql3`${sql3.ref(column._.name)} LIKE ${sql3.val(pattern)}`;
|
|
1562
|
+
var ilike = (column, pattern) => sql3`${sql3.ref(column._.name)} LIKE ${sql3.val(pattern)} COLLATE NOCASE`;
|
|
1563
|
+
var startsWith = (column, value) => sql3`${sql3.ref(column._.name)} LIKE ${sql3.val(`${value}%`)}`;
|
|
1564
|
+
var endsWith = (column, value) => sql3`${sql3.ref(column._.name)} LIKE ${sql3.val(`%${value}`)}`;
|
|
1565
|
+
var contains = (column, value) => sql3`${sql3.ref(column._.name)} LIKE ${sql3.val(`%${value}%`)}`;
|
|
1566
|
+
var isNull = (column) => sql3`${sql3.ref(column._.name)} IS NULL`;
|
|
1567
|
+
var isNotNull = (column) => sql3`${sql3.ref(column._.name)} IS NOT NULL`;
|
|
1568
|
+
var exists = (subquery2) => sql3`EXISTS ${subquery2}`;
|
|
1569
|
+
var notExists = (subquery2) => sql3`NOT EXISTS ${subquery2}`;
|
|
1570
|
+
var eqSubquery = (column, subquery2) => sql3`${sql3.ref(column._.name)} = (${subquery2})`;
|
|
1571
|
+
var neSubquery = (column, subquery2) => sql3`${sql3.ref(column._.name)} != (${subquery2})`;
|
|
1572
|
+
var gtSubquery = (column, subquery2) => sql3`${sql3.ref(column._.name)} > (${subquery2})`;
|
|
1573
|
+
var gteSubquery = (column, subquery2) => sql3`${sql3.ref(column._.name)} >= (${subquery2})`;
|
|
1574
|
+
var ltSubquery = (column, subquery2) => sql3`${sql3.ref(column._.name)} < (${subquery2})`;
|
|
1575
|
+
var lteSubquery = (column, subquery2) => sql3`${sql3.ref(column._.name)} <= (${subquery2})`;
|
|
1576
|
+
var inArray = (column, values) => {
|
|
1577
|
+
if (isExpression(values)) {
|
|
1578
|
+
return sql3`${sql3.ref(column._.name)} IN (${values})`;
|
|
1579
|
+
}
|
|
1580
|
+
const arr = values;
|
|
1581
|
+
if (arr.length === 0) return sql3`1 = 0`;
|
|
1582
|
+
const serialized = arr.map((v) => sql3.val(serializeValue(v, column)));
|
|
1583
|
+
return sql3`${sql3.ref(column._.name)} IN (${sql3.join(serialized)})`;
|
|
1584
|
+
};
|
|
1585
|
+
var notIn = (column, values) => {
|
|
1586
|
+
if (isExpression(values)) {
|
|
1587
|
+
return sql3`${sql3.ref(column._.name)} NOT IN (${values})`;
|
|
1588
|
+
}
|
|
1589
|
+
const arr = values;
|
|
1590
|
+
if (arr.length === 0) return sql3`1 = 1`;
|
|
1591
|
+
const serialized = arr.map((v) => sql3.val(serializeValue(v, column)));
|
|
1592
|
+
return sql3`${sql3.ref(column._.name)} NOT IN (${sql3.join(serialized)})`;
|
|
1593
|
+
};
|
|
1594
|
+
var between = (column, min2, max2) => {
|
|
1595
|
+
const serializedMin = serializeValue(min2, column);
|
|
1596
|
+
const serializedMax = serializeValue(max2, column);
|
|
1597
|
+
return sql3`${sql3.ref(column._.name)} BETWEEN ${sql3.val(serializedMin)} AND ${sql3.val(serializedMax)}`;
|
|
1598
|
+
};
|
|
1599
|
+
|
|
1713
1600
|
// src/aggregates.ts
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
})
|
|
1718
|
-
var
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
})
|
|
1722
|
-
var
|
|
1723
|
-
sql: `SUM(${column._.name})`,
|
|
1724
|
-
params: []
|
|
1725
|
-
});
|
|
1726
|
-
var avg = (column) => ({
|
|
1727
|
-
sql: `AVG(${column._.name})`,
|
|
1728
|
-
params: []
|
|
1729
|
-
});
|
|
1730
|
-
var max = (column) => ({
|
|
1731
|
-
sql: `MAX(${column._.name})`,
|
|
1732
|
-
params: []
|
|
1733
|
-
});
|
|
1734
|
-
var min = (column) => ({
|
|
1735
|
-
sql: `MIN(${column._.name})`,
|
|
1736
|
-
params: []
|
|
1737
|
-
});
|
|
1738
|
-
var groupConcat = (column, separator = ",") => ({
|
|
1739
|
-
sql: `GROUP_CONCAT(${column._.name}, ?)`,
|
|
1740
|
-
params: [separator]
|
|
1741
|
-
});
|
|
1742
|
-
var as = (aggregate, alias2) => ({
|
|
1743
|
-
...aggregate,
|
|
1744
|
-
alias: alias2
|
|
1745
|
-
});
|
|
1601
|
+
import { sql as sql4 } from "kysely";
|
|
1602
|
+
var count = (column) => sql4`COUNT(${column ? sql4.ref(column._.name) : sql4.raw("*")})`;
|
|
1603
|
+
var countDistinct = (column) => sql4`COUNT(DISTINCT ${sql4.ref(column._.name)})`;
|
|
1604
|
+
var sum = (column) => sql4`SUM(${sql4.ref(column._.name)})`;
|
|
1605
|
+
var avg = (column) => sql4`AVG(${sql4.ref(column._.name)})`;
|
|
1606
|
+
var max = (column) => sql4`MAX(${sql4.ref(column._.name)})`;
|
|
1607
|
+
var min = (column) => sql4`MIN(${sql4.ref(column._.name)})`;
|
|
1608
|
+
var groupConcat = (column, separator = ",") => sql4`GROUP_CONCAT(${sql4.ref(column._.name)}, ${sql4.val(separator)})`;
|
|
1609
|
+
var as = (aggregate, alias2) => Object.assign(aggregate, { alias: alias2 });
|
|
1746
1610
|
|
|
1747
1611
|
// src/subquery.ts
|
|
1748
1612
|
var subquery = (query) => {
|
|
1749
|
-
|
|
1750
|
-
return {
|
|
1751
|
-
sql: `(${sql2})`,
|
|
1752
|
-
params,
|
|
1753
|
-
_isSubquery: true
|
|
1754
|
-
};
|
|
1613
|
+
return query.toKyselyExpression();
|
|
1755
1614
|
};
|
|
1756
1615
|
var scalarSubquery = (query) => {
|
|
1757
|
-
return
|
|
1616
|
+
return query.toKyselyExpression();
|
|
1758
1617
|
};
|
|
1759
1618
|
|
|
1760
1619
|
// src/column-helpers.ts
|
|
@@ -1773,14 +1632,15 @@ function numeric(name, config) {
|
|
|
1773
1632
|
return new SQLiteColumn(name, "NUMERIC", config);
|
|
1774
1633
|
}
|
|
1775
1634
|
var enumType = (name, values) => text(name, { enum: values });
|
|
1635
|
+
|
|
1636
|
+
// src/index.ts
|
|
1637
|
+
import { sql as sql5 } from "kysely";
|
|
1776
1638
|
export {
|
|
1777
|
-
BaseQueryBuilder,
|
|
1778
1639
|
ColumnNotFoundError,
|
|
1779
1640
|
DeleteQueryBuilder,
|
|
1780
1641
|
InsertQueryBuilder,
|
|
1781
1642
|
InsertValidationError,
|
|
1782
1643
|
ManyRelation,
|
|
1783
|
-
ManyToManyRelation,
|
|
1784
1644
|
MigrationError,
|
|
1785
1645
|
MissingWhereClauseError,
|
|
1786
1646
|
OneRelation,
|
|
@@ -1791,6 +1651,7 @@ export {
|
|
|
1791
1651
|
SelectQueryBuilder,
|
|
1792
1652
|
Table,
|
|
1793
1653
|
TableNotFoundError,
|
|
1654
|
+
TauriDialect,
|
|
1794
1655
|
TauriORM,
|
|
1795
1656
|
TauriORMError,
|
|
1796
1657
|
UpdateQueryBuilder,
|
|
@@ -1842,7 +1703,7 @@ export {
|
|
|
1842
1703
|
real,
|
|
1843
1704
|
relations,
|
|
1844
1705
|
scalarSubquery,
|
|
1845
|
-
sql,
|
|
1706
|
+
sql5 as sql,
|
|
1846
1707
|
sqliteTable,
|
|
1847
1708
|
startsWith,
|
|
1848
1709
|
subquery,
|