zen-code 1.0.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/LICENSE +201 -0
- package/README.md +3 -0
- package/dist/_commonjsHelpers-DQNKXVTB.mjs +33 -0
- package/dist/app-CV_FJyjI.mjs +100971 -0
- package/dist/assets/worker-I4QjJYba.js +1 -0
- package/dist/checkpoint-1sAx_j1E-WswZeQFl.mjs +318 -0
- package/dist/checkpoint-DxiUsHMy-BWKnWBL7.mjs +393 -0
- package/dist/chunk-YWE62C55-DID9N9eS.mjs +193 -0
- package/dist/devtools-m8Lnvjy_.mjs +2658 -0
- package/dist/index-BUGHAUbY.mjs +117 -0
- package/dist/index-Butw72lT.mjs +462 -0
- package/dist/index-C5j-48Ft.mjs +75 -0
- package/dist/index-DS5HVciX.mjs +216 -0
- package/dist/kysely-Bchvsze0.mjs +4316 -0
- package/dist/migrator-BatO36Tk.mjs +592 -0
- package/dist/pg-adapter-BFtir1GE-BU2H39HC.mjs +62 -0
- package/dist/postgres-dialect-DaHvQ_AZ.mjs +211 -0
- package/dist/queue-BSCnCent-Dtq-TPST.mjs +81 -0
- package/dist/shallow-BKrykESK.mjs +330 -0
- package/dist/sql-CJsUpKEQ.mjs +8106 -0
- package/dist/sqlite-adapter-5PeLHaxe-CUccULPN.mjs +64 -0
- package/dist/zen-code.mjs +1 -0
- package/package.json +49 -0
|
@@ -0,0 +1,4316 @@
|
|
|
1
|
+
import { bb as r, ah as m, aV as G, a2 as x, b6 as ae, aw as Fe, bk as Ue, aW as ze, bl as H, bm as ce, aS as ke, bn as E, bo as w, ax as Ge, b5 as qe, aR as He, bp as Je, bq as Pe, br as Ye, c as d, V as pe, K as je, P as Xe, bs as Ze, bj as Ne, bi as V, Q as Ke, C as Be, $ as _e, bt as et, g as tt, l as rt, bu as nt } from "./sql-CJsUpKEQ.mjs";
|
|
2
|
+
const s = r({
|
|
3
|
+
is(t) {
|
|
4
|
+
return t.kind === "AlterTableNode";
|
|
5
|
+
},
|
|
6
|
+
create(t) {
|
|
7
|
+
return r({
|
|
8
|
+
kind: "AlterTableNode",
|
|
9
|
+
table: t
|
|
10
|
+
});
|
|
11
|
+
},
|
|
12
|
+
cloneWithTableProps(t, e) {
|
|
13
|
+
return r({
|
|
14
|
+
...t,
|
|
15
|
+
...e
|
|
16
|
+
});
|
|
17
|
+
},
|
|
18
|
+
cloneWithColumnAlteration(t, e) {
|
|
19
|
+
return r({
|
|
20
|
+
...t,
|
|
21
|
+
columnAlterations: t.columnAlterations ? [...t.columnAlterations, e] : [e]
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}), O = r({
|
|
25
|
+
is(t) {
|
|
26
|
+
return t.kind === "CreateIndexNode";
|
|
27
|
+
},
|
|
28
|
+
create(t) {
|
|
29
|
+
return r({
|
|
30
|
+
kind: "CreateIndexNode",
|
|
31
|
+
name: m.create(t)
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
cloneWith(t, e) {
|
|
35
|
+
return r({
|
|
36
|
+
...t,
|
|
37
|
+
...e
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
cloneWithColumns(t, e) {
|
|
41
|
+
return r({
|
|
42
|
+
...t,
|
|
43
|
+
columns: [...t.columns || [], ...e]
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}), ve = r({
|
|
47
|
+
is(t) {
|
|
48
|
+
return t.kind === "CreateSchemaNode";
|
|
49
|
+
},
|
|
50
|
+
create(t, e) {
|
|
51
|
+
return r({
|
|
52
|
+
kind: "CreateSchemaNode",
|
|
53
|
+
schema: m.create(t),
|
|
54
|
+
...e
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
cloneWith(t, e) {
|
|
58
|
+
return r({
|
|
59
|
+
...t,
|
|
60
|
+
...e
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}), it = ["preserve rows", "delete rows", "drop"], p = r({
|
|
64
|
+
is(t) {
|
|
65
|
+
return t.kind === "CreateTableNode";
|
|
66
|
+
},
|
|
67
|
+
create(t) {
|
|
68
|
+
return r({
|
|
69
|
+
kind: "CreateTableNode",
|
|
70
|
+
table: t,
|
|
71
|
+
columns: r([])
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
cloneWithColumn(t, e) {
|
|
75
|
+
return r({
|
|
76
|
+
...t,
|
|
77
|
+
columns: r([...t.columns, e])
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
cloneWithConstraint(t, e) {
|
|
81
|
+
return r({
|
|
82
|
+
...t,
|
|
83
|
+
constraints: t.constraints ? r([...t.constraints, e]) : r([e])
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
cloneWithFrontModifier(t, e) {
|
|
87
|
+
return r({
|
|
88
|
+
...t,
|
|
89
|
+
frontModifiers: t.frontModifiers ? r([...t.frontModifiers, e]) : r([e])
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
cloneWithEndModifier(t, e) {
|
|
93
|
+
return r({
|
|
94
|
+
...t,
|
|
95
|
+
endModifiers: t.endModifiers ? r([...t.endModifiers, e]) : r([e])
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
cloneWith(t, e) {
|
|
99
|
+
return r({
|
|
100
|
+
...t,
|
|
101
|
+
...e
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}), Z = r({
|
|
105
|
+
is(t) {
|
|
106
|
+
return t.kind === "DropIndexNode";
|
|
107
|
+
},
|
|
108
|
+
create(t, e) {
|
|
109
|
+
return r({
|
|
110
|
+
kind: "DropIndexNode",
|
|
111
|
+
name: G.create(t),
|
|
112
|
+
...e
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
cloneWith(t, e) {
|
|
116
|
+
return r({
|
|
117
|
+
...t,
|
|
118
|
+
...e
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}), ye = r({
|
|
122
|
+
is(t) {
|
|
123
|
+
return t.kind === "DropSchemaNode";
|
|
124
|
+
},
|
|
125
|
+
create(t, e) {
|
|
126
|
+
return r({
|
|
127
|
+
kind: "DropSchemaNode",
|
|
128
|
+
schema: m.create(t),
|
|
129
|
+
...e
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
cloneWith(t, e) {
|
|
133
|
+
return r({
|
|
134
|
+
...t,
|
|
135
|
+
...e
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}), fe = r({
|
|
139
|
+
is(t) {
|
|
140
|
+
return t.kind === "DropTableNode";
|
|
141
|
+
},
|
|
142
|
+
create(t, e) {
|
|
143
|
+
return r({
|
|
144
|
+
kind: "DropTableNode",
|
|
145
|
+
table: t,
|
|
146
|
+
...e
|
|
147
|
+
});
|
|
148
|
+
},
|
|
149
|
+
cloneWith(t, e) {
|
|
150
|
+
return r({
|
|
151
|
+
...t,
|
|
152
|
+
...e
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}), Ee = r({
|
|
156
|
+
is(t) {
|
|
157
|
+
return t.kind === "AddColumnNode";
|
|
158
|
+
},
|
|
159
|
+
create(t) {
|
|
160
|
+
return r({
|
|
161
|
+
kind: "AddColumnNode",
|
|
162
|
+
column: t
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}), c = r({
|
|
166
|
+
is(t) {
|
|
167
|
+
return t.kind === "ColumnDefinitionNode";
|
|
168
|
+
},
|
|
169
|
+
create(t, e) {
|
|
170
|
+
return r({
|
|
171
|
+
kind: "ColumnDefinitionNode",
|
|
172
|
+
column: x.create(t),
|
|
173
|
+
dataType: e
|
|
174
|
+
});
|
|
175
|
+
},
|
|
176
|
+
cloneWithFrontModifier(t, e) {
|
|
177
|
+
return r({
|
|
178
|
+
...t,
|
|
179
|
+
frontModifiers: t.frontModifiers ? r([...t.frontModifiers, e]) : [e]
|
|
180
|
+
});
|
|
181
|
+
},
|
|
182
|
+
cloneWithEndModifier(t, e) {
|
|
183
|
+
return r({
|
|
184
|
+
...t,
|
|
185
|
+
endModifiers: t.endModifiers ? r([...t.endModifiers, e]) : [e]
|
|
186
|
+
});
|
|
187
|
+
},
|
|
188
|
+
cloneWith(t, e) {
|
|
189
|
+
return r({
|
|
190
|
+
...t,
|
|
191
|
+
...e
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}), Qe = r({
|
|
195
|
+
is(t) {
|
|
196
|
+
return t.kind === "DropColumnNode";
|
|
197
|
+
},
|
|
198
|
+
create(t) {
|
|
199
|
+
return r({
|
|
200
|
+
kind: "DropColumnNode",
|
|
201
|
+
column: x.create(t)
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}), De = r({
|
|
205
|
+
is(t) {
|
|
206
|
+
return t.kind === "RenameColumnNode";
|
|
207
|
+
},
|
|
208
|
+
create(t, e) {
|
|
209
|
+
return r({
|
|
210
|
+
kind: "RenameColumnNode",
|
|
211
|
+
column: x.create(t),
|
|
212
|
+
renameTo: x.create(e)
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
}), xe = r({
|
|
216
|
+
is(t) {
|
|
217
|
+
return t.kind === "CheckConstraintNode";
|
|
218
|
+
},
|
|
219
|
+
create(t, e) {
|
|
220
|
+
return r({
|
|
221
|
+
kind: "CheckConstraintNode",
|
|
222
|
+
expression: t,
|
|
223
|
+
name: e ? m.create(e) : void 0
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}), ot = [
|
|
227
|
+
"no action",
|
|
228
|
+
"restrict",
|
|
229
|
+
"cascade",
|
|
230
|
+
"set null",
|
|
231
|
+
"set default"
|
|
232
|
+
], re = r({
|
|
233
|
+
is(t) {
|
|
234
|
+
return t.kind === "ReferencesNode";
|
|
235
|
+
},
|
|
236
|
+
create(t, e) {
|
|
237
|
+
return r({
|
|
238
|
+
kind: "ReferencesNode",
|
|
239
|
+
table: t,
|
|
240
|
+
columns: r([...e])
|
|
241
|
+
});
|
|
242
|
+
},
|
|
243
|
+
cloneWithOnDelete(t, e) {
|
|
244
|
+
return r({
|
|
245
|
+
...t,
|
|
246
|
+
onDelete: e
|
|
247
|
+
});
|
|
248
|
+
},
|
|
249
|
+
cloneWithOnUpdate(t, e) {
|
|
250
|
+
return r({
|
|
251
|
+
...t,
|
|
252
|
+
onUpdate: e
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
function Se(t) {
|
|
257
|
+
return Fe(t) ? t.toOperationNode() : ae.createImmediate(t);
|
|
258
|
+
}
|
|
259
|
+
const te = r({
|
|
260
|
+
is(t) {
|
|
261
|
+
return t.kind === "GeneratedNode";
|
|
262
|
+
},
|
|
263
|
+
create(t) {
|
|
264
|
+
return r({
|
|
265
|
+
kind: "GeneratedNode",
|
|
266
|
+
...t
|
|
267
|
+
});
|
|
268
|
+
},
|
|
269
|
+
createWithExpression(t) {
|
|
270
|
+
return r({
|
|
271
|
+
kind: "GeneratedNode",
|
|
272
|
+
always: !0,
|
|
273
|
+
expression: t
|
|
274
|
+
});
|
|
275
|
+
},
|
|
276
|
+
cloneWith(t, e) {
|
|
277
|
+
return r({
|
|
278
|
+
...t,
|
|
279
|
+
...e
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
}), st = r({
|
|
283
|
+
is(t) {
|
|
284
|
+
return t.kind === "DefaultValueNode";
|
|
285
|
+
},
|
|
286
|
+
create(t) {
|
|
287
|
+
return r({
|
|
288
|
+
kind: "DefaultValueNode",
|
|
289
|
+
defaultValue: t
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
function ue(t) {
|
|
294
|
+
if (ot.includes(t))
|
|
295
|
+
return t;
|
|
296
|
+
throw new Error(`invalid OnModifyForeignAction ${t}`);
|
|
297
|
+
}
|
|
298
|
+
class a {
|
|
299
|
+
#e;
|
|
300
|
+
constructor(e) {
|
|
301
|
+
this.#e = e;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Adds `auto_increment` or `autoincrement` to the column definition
|
|
305
|
+
* depending on the dialect.
|
|
306
|
+
*
|
|
307
|
+
* Some dialects like PostgreSQL don't support this. On PostgreSQL
|
|
308
|
+
* you can use the `serial` or `bigserial` data type instead.
|
|
309
|
+
*
|
|
310
|
+
* ### Examples
|
|
311
|
+
*
|
|
312
|
+
* ```ts
|
|
313
|
+
* await db.schema
|
|
314
|
+
* .createTable('person')
|
|
315
|
+
* .addColumn('id', 'integer', col => col.autoIncrement().primaryKey())
|
|
316
|
+
* .execute()
|
|
317
|
+
* ```
|
|
318
|
+
*
|
|
319
|
+
* The generated SQL (MySQL):
|
|
320
|
+
*
|
|
321
|
+
* ```sql
|
|
322
|
+
* create table `person` (
|
|
323
|
+
* `id` integer primary key auto_increment
|
|
324
|
+
* )
|
|
325
|
+
* ```
|
|
326
|
+
*/
|
|
327
|
+
autoIncrement() {
|
|
328
|
+
return new a(c.cloneWith(this.#e, { autoIncrement: !0 }));
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Makes the column an identity column.
|
|
332
|
+
*
|
|
333
|
+
* This only works on some dialects like MS SQL Server (MSSQL).
|
|
334
|
+
*
|
|
335
|
+
* For PostgreSQL's `generated always as identity` use {@link generatedAlwaysAsIdentity}.
|
|
336
|
+
*
|
|
337
|
+
* ### Examples
|
|
338
|
+
*
|
|
339
|
+
* ```ts
|
|
340
|
+
* await db.schema
|
|
341
|
+
* .createTable('person')
|
|
342
|
+
* .addColumn('id', 'integer', col => col.identity().primaryKey())
|
|
343
|
+
* .execute()
|
|
344
|
+
* ```
|
|
345
|
+
*
|
|
346
|
+
* The generated SQL (MSSQL):
|
|
347
|
+
*
|
|
348
|
+
* ```sql
|
|
349
|
+
* create table "person" (
|
|
350
|
+
* "id" integer identity primary key
|
|
351
|
+
* )
|
|
352
|
+
* ```
|
|
353
|
+
*/
|
|
354
|
+
identity() {
|
|
355
|
+
return new a(c.cloneWith(this.#e, { identity: !0 }));
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Makes the column the primary key.
|
|
359
|
+
*
|
|
360
|
+
* If you want to specify a composite primary key use the
|
|
361
|
+
* {@link CreateTableBuilder.addPrimaryKeyConstraint} method.
|
|
362
|
+
*
|
|
363
|
+
* ### Examples
|
|
364
|
+
*
|
|
365
|
+
* ```ts
|
|
366
|
+
* await db.schema
|
|
367
|
+
* .createTable('person')
|
|
368
|
+
* .addColumn('id', 'integer', col => col.primaryKey())
|
|
369
|
+
* .execute()
|
|
370
|
+
* ```
|
|
371
|
+
*
|
|
372
|
+
* The generated SQL (MySQL):
|
|
373
|
+
*
|
|
374
|
+
* ```sql
|
|
375
|
+
* create table `person` (
|
|
376
|
+
* `id` integer primary key
|
|
377
|
+
* )
|
|
378
|
+
*/
|
|
379
|
+
primaryKey() {
|
|
380
|
+
return new a(c.cloneWith(this.#e, { primaryKey: !0 }));
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Adds a foreign key constraint for the column.
|
|
384
|
+
*
|
|
385
|
+
* If your database engine doesn't support foreign key constraints in the
|
|
386
|
+
* column definition (like MySQL 5) you need to call the table level
|
|
387
|
+
* {@link CreateTableBuilder.addForeignKeyConstraint} method instead.
|
|
388
|
+
*
|
|
389
|
+
* ### Examples
|
|
390
|
+
*
|
|
391
|
+
* ```ts
|
|
392
|
+
* await db.schema
|
|
393
|
+
* .createTable('pet')
|
|
394
|
+
* .addColumn('owner_id', 'integer', (col) => col.references('person.id'))
|
|
395
|
+
* .execute()
|
|
396
|
+
* ```
|
|
397
|
+
*
|
|
398
|
+
* The generated SQL (PostgreSQL):
|
|
399
|
+
*
|
|
400
|
+
* ```sql
|
|
401
|
+
* create table "pet" (
|
|
402
|
+
* "owner_id" integer references "person" ("id")
|
|
403
|
+
* )
|
|
404
|
+
* ```
|
|
405
|
+
*/
|
|
406
|
+
references(e) {
|
|
407
|
+
const n = Ue(e);
|
|
408
|
+
if (!n.table || ze.is(n.column))
|
|
409
|
+
throw new Error(`invalid call references('${e}'). The reference must have format table.column or schema.table.column`);
|
|
410
|
+
return new a(c.cloneWith(this.#e, {
|
|
411
|
+
references: re.create(n.table, [
|
|
412
|
+
n.column
|
|
413
|
+
])
|
|
414
|
+
}));
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Adds an `on delete` constraint for the foreign key column.
|
|
418
|
+
*
|
|
419
|
+
* If your database engine doesn't support foreign key constraints in the
|
|
420
|
+
* column definition (like MySQL 5) you need to call the table level
|
|
421
|
+
* {@link CreateTableBuilder.addForeignKeyConstraint} method instead.
|
|
422
|
+
*
|
|
423
|
+
* ### Examples
|
|
424
|
+
*
|
|
425
|
+
* ```ts
|
|
426
|
+
* await db.schema
|
|
427
|
+
* .createTable('pet')
|
|
428
|
+
* .addColumn(
|
|
429
|
+
* 'owner_id',
|
|
430
|
+
* 'integer',
|
|
431
|
+
* (col) => col.references('person.id').onDelete('cascade')
|
|
432
|
+
* )
|
|
433
|
+
* .execute()
|
|
434
|
+
* ```
|
|
435
|
+
*
|
|
436
|
+
* The generated SQL (PostgreSQL):
|
|
437
|
+
*
|
|
438
|
+
* ```sql
|
|
439
|
+
* create table "pet" (
|
|
440
|
+
* "owner_id" integer references "person" ("id") on delete cascade
|
|
441
|
+
* )
|
|
442
|
+
* ```
|
|
443
|
+
*/
|
|
444
|
+
onDelete(e) {
|
|
445
|
+
if (!this.#e.references)
|
|
446
|
+
throw new Error("on delete constraint can only be added for foreign keys");
|
|
447
|
+
return new a(c.cloneWith(this.#e, {
|
|
448
|
+
references: re.cloneWithOnDelete(this.#e.references, ue(e))
|
|
449
|
+
}));
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Adds an `on update` constraint for the foreign key column.
|
|
453
|
+
*
|
|
454
|
+
* If your database engine doesn't support foreign key constraints in the
|
|
455
|
+
* column definition (like MySQL 5) you need to call the table level
|
|
456
|
+
* {@link CreateTableBuilder.addForeignKeyConstraint} method instead.
|
|
457
|
+
*
|
|
458
|
+
* ### Examples
|
|
459
|
+
*
|
|
460
|
+
* ```ts
|
|
461
|
+
* await db.schema
|
|
462
|
+
* .createTable('pet')
|
|
463
|
+
* .addColumn(
|
|
464
|
+
* 'owner_id',
|
|
465
|
+
* 'integer',
|
|
466
|
+
* (col) => col.references('person.id').onUpdate('cascade')
|
|
467
|
+
* )
|
|
468
|
+
* .execute()
|
|
469
|
+
* ```
|
|
470
|
+
*
|
|
471
|
+
* The generated SQL (PostgreSQL):
|
|
472
|
+
*
|
|
473
|
+
* ```sql
|
|
474
|
+
* create table "pet" (
|
|
475
|
+
* "owner_id" integer references "person" ("id") on update cascade
|
|
476
|
+
* )
|
|
477
|
+
* ```
|
|
478
|
+
*/
|
|
479
|
+
onUpdate(e) {
|
|
480
|
+
if (!this.#e.references)
|
|
481
|
+
throw new Error("on update constraint can only be added for foreign keys");
|
|
482
|
+
return new a(c.cloneWith(this.#e, {
|
|
483
|
+
references: re.cloneWithOnUpdate(this.#e.references, ue(e))
|
|
484
|
+
}));
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Adds a unique constraint for the column.
|
|
488
|
+
*
|
|
489
|
+
* ### Examples
|
|
490
|
+
*
|
|
491
|
+
* ```ts
|
|
492
|
+
* await db.schema
|
|
493
|
+
* .createTable('person')
|
|
494
|
+
* .addColumn('email', 'varchar(255)', col => col.unique())
|
|
495
|
+
* .execute()
|
|
496
|
+
* ```
|
|
497
|
+
*
|
|
498
|
+
* The generated SQL (MySQL):
|
|
499
|
+
*
|
|
500
|
+
* ```sql
|
|
501
|
+
* create table `person` (
|
|
502
|
+
* `email` varchar(255) unique
|
|
503
|
+
* )
|
|
504
|
+
* ```
|
|
505
|
+
*/
|
|
506
|
+
unique() {
|
|
507
|
+
return new a(c.cloneWith(this.#e, { unique: !0 }));
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Adds a `not null` constraint for the column.
|
|
511
|
+
*
|
|
512
|
+
* ### Examples
|
|
513
|
+
*
|
|
514
|
+
* ```ts
|
|
515
|
+
* await db.schema
|
|
516
|
+
* .createTable('person')
|
|
517
|
+
* .addColumn('first_name', 'varchar(255)', col => col.notNull())
|
|
518
|
+
* .execute()
|
|
519
|
+
* ```
|
|
520
|
+
*
|
|
521
|
+
* The generated SQL (MySQL):
|
|
522
|
+
*
|
|
523
|
+
* ```sql
|
|
524
|
+
* create table `person` (
|
|
525
|
+
* `first_name` varchar(255) not null
|
|
526
|
+
* )
|
|
527
|
+
* ```
|
|
528
|
+
*/
|
|
529
|
+
notNull() {
|
|
530
|
+
return new a(c.cloneWith(this.#e, { notNull: !0 }));
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Adds a `unsigned` modifier for the column.
|
|
534
|
+
*
|
|
535
|
+
* This only works on some dialects like MySQL.
|
|
536
|
+
*
|
|
537
|
+
* ### Examples
|
|
538
|
+
*
|
|
539
|
+
* ```ts
|
|
540
|
+
* await db.schema
|
|
541
|
+
* .createTable('person')
|
|
542
|
+
* .addColumn('age', 'integer', col => col.unsigned())
|
|
543
|
+
* .execute()
|
|
544
|
+
* ```
|
|
545
|
+
*
|
|
546
|
+
* The generated SQL (MySQL):
|
|
547
|
+
*
|
|
548
|
+
* ```sql
|
|
549
|
+
* create table `person` (
|
|
550
|
+
* `age` integer unsigned
|
|
551
|
+
* )
|
|
552
|
+
* ```
|
|
553
|
+
*/
|
|
554
|
+
unsigned() {
|
|
555
|
+
return new a(c.cloneWith(this.#e, { unsigned: !0 }));
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Adds a default value constraint for the column.
|
|
559
|
+
*
|
|
560
|
+
* ### Examples
|
|
561
|
+
*
|
|
562
|
+
* ```ts
|
|
563
|
+
* await db.schema
|
|
564
|
+
* .createTable('pet')
|
|
565
|
+
* .addColumn('number_of_legs', 'integer', (col) => col.defaultTo(4))
|
|
566
|
+
* .execute()
|
|
567
|
+
* ```
|
|
568
|
+
*
|
|
569
|
+
* The generated SQL (MySQL):
|
|
570
|
+
*
|
|
571
|
+
* ```sql
|
|
572
|
+
* create table `pet` (
|
|
573
|
+
* `number_of_legs` integer default 4
|
|
574
|
+
* )
|
|
575
|
+
* ```
|
|
576
|
+
*
|
|
577
|
+
* Values passed to `defaultTo` are interpreted as value literals by default. You can define
|
|
578
|
+
* an arbitrary SQL expression using the {@link sql} template tag:
|
|
579
|
+
*
|
|
580
|
+
* ```ts
|
|
581
|
+
* import { sql } from 'kysely'
|
|
582
|
+
*
|
|
583
|
+
* await db.schema
|
|
584
|
+
* .createTable('pet')
|
|
585
|
+
* .addColumn(
|
|
586
|
+
* 'created_at',
|
|
587
|
+
* 'timestamp',
|
|
588
|
+
* (col) => col.defaultTo(sql`CURRENT_TIMESTAMP`)
|
|
589
|
+
* )
|
|
590
|
+
* .execute()
|
|
591
|
+
* ```
|
|
592
|
+
*
|
|
593
|
+
* The generated SQL (MySQL):
|
|
594
|
+
*
|
|
595
|
+
* ```sql
|
|
596
|
+
* create table `pet` (
|
|
597
|
+
* `created_at` timestamp default CURRENT_TIMESTAMP
|
|
598
|
+
* )
|
|
599
|
+
* ```
|
|
600
|
+
*/
|
|
601
|
+
defaultTo(e) {
|
|
602
|
+
return new a(c.cloneWith(this.#e, {
|
|
603
|
+
defaultTo: st.create(Se(e))
|
|
604
|
+
}));
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Adds a check constraint for the column.
|
|
608
|
+
*
|
|
609
|
+
* ### Examples
|
|
610
|
+
*
|
|
611
|
+
* ```ts
|
|
612
|
+
* import { sql } from 'kysely'
|
|
613
|
+
*
|
|
614
|
+
* await db.schema
|
|
615
|
+
* .createTable('pet')
|
|
616
|
+
* .addColumn('number_of_legs', 'integer', (col) =>
|
|
617
|
+
* col.check(sql`number_of_legs < 5`)
|
|
618
|
+
* )
|
|
619
|
+
* .execute()
|
|
620
|
+
* ```
|
|
621
|
+
*
|
|
622
|
+
* The generated SQL (MySQL):
|
|
623
|
+
*
|
|
624
|
+
* ```sql
|
|
625
|
+
* create table `pet` (
|
|
626
|
+
* `number_of_legs` integer check (number_of_legs < 5)
|
|
627
|
+
* )
|
|
628
|
+
* ```
|
|
629
|
+
*/
|
|
630
|
+
check(e) {
|
|
631
|
+
return new a(c.cloneWith(this.#e, {
|
|
632
|
+
check: xe.create(e.toOperationNode())
|
|
633
|
+
}));
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* Makes the column a generated column using a `generated always as` statement.
|
|
637
|
+
*
|
|
638
|
+
* ### Examples
|
|
639
|
+
*
|
|
640
|
+
* ```ts
|
|
641
|
+
* import { sql } from 'kysely'
|
|
642
|
+
*
|
|
643
|
+
* await db.schema
|
|
644
|
+
* .createTable('person')
|
|
645
|
+
* .addColumn('full_name', 'varchar(255)',
|
|
646
|
+
* (col) => col.generatedAlwaysAs(sql`concat(first_name, ' ', last_name)`)
|
|
647
|
+
* )
|
|
648
|
+
* .execute()
|
|
649
|
+
* ```
|
|
650
|
+
*
|
|
651
|
+
* The generated SQL (MySQL):
|
|
652
|
+
*
|
|
653
|
+
* ```sql
|
|
654
|
+
* create table `person` (
|
|
655
|
+
* `full_name` varchar(255) generated always as (concat(first_name, ' ', last_name))
|
|
656
|
+
* )
|
|
657
|
+
* ```
|
|
658
|
+
*/
|
|
659
|
+
generatedAlwaysAs(e) {
|
|
660
|
+
return new a(c.cloneWith(this.#e, {
|
|
661
|
+
generated: te.createWithExpression(e.toOperationNode())
|
|
662
|
+
}));
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Adds the `generated always as identity` specifier.
|
|
666
|
+
*
|
|
667
|
+
* This only works on some dialects like PostgreSQL.
|
|
668
|
+
*
|
|
669
|
+
* For MS SQL Server (MSSQL)'s identity column use {@link identity}.
|
|
670
|
+
*
|
|
671
|
+
* ### Examples
|
|
672
|
+
*
|
|
673
|
+
* ```ts
|
|
674
|
+
* await db.schema
|
|
675
|
+
* .createTable('person')
|
|
676
|
+
* .addColumn('id', 'integer', col => col.generatedAlwaysAsIdentity().primaryKey())
|
|
677
|
+
* .execute()
|
|
678
|
+
* ```
|
|
679
|
+
*
|
|
680
|
+
* The generated SQL (PostgreSQL):
|
|
681
|
+
*
|
|
682
|
+
* ```sql
|
|
683
|
+
* create table "person" (
|
|
684
|
+
* "id" integer generated always as identity primary key
|
|
685
|
+
* )
|
|
686
|
+
* ```
|
|
687
|
+
*/
|
|
688
|
+
generatedAlwaysAsIdentity() {
|
|
689
|
+
return new a(c.cloneWith(this.#e, {
|
|
690
|
+
generated: te.create({ identity: !0, always: !0 })
|
|
691
|
+
}));
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Adds the `generated by default as identity` specifier on supported dialects.
|
|
695
|
+
*
|
|
696
|
+
* This only works on some dialects like PostgreSQL.
|
|
697
|
+
*
|
|
698
|
+
* For MS SQL Server (MSSQL)'s identity column use {@link identity}.
|
|
699
|
+
*
|
|
700
|
+
* ### Examples
|
|
701
|
+
*
|
|
702
|
+
* ```ts
|
|
703
|
+
* await db.schema
|
|
704
|
+
* .createTable('person')
|
|
705
|
+
* .addColumn('id', 'integer', col => col.generatedByDefaultAsIdentity().primaryKey())
|
|
706
|
+
* .execute()
|
|
707
|
+
* ```
|
|
708
|
+
*
|
|
709
|
+
* The generated SQL (PostgreSQL):
|
|
710
|
+
*
|
|
711
|
+
* ```sql
|
|
712
|
+
* create table "person" (
|
|
713
|
+
* "id" integer generated by default as identity primary key
|
|
714
|
+
* )
|
|
715
|
+
* ```
|
|
716
|
+
*/
|
|
717
|
+
generatedByDefaultAsIdentity() {
|
|
718
|
+
return new a(c.cloneWith(this.#e, {
|
|
719
|
+
generated: te.create({ identity: !0, byDefault: !0 })
|
|
720
|
+
}));
|
|
721
|
+
}
|
|
722
|
+
/**
|
|
723
|
+
* Makes a generated column stored instead of virtual. This method can only
|
|
724
|
+
* be used with {@link generatedAlwaysAs}
|
|
725
|
+
*
|
|
726
|
+
* ### Examples
|
|
727
|
+
*
|
|
728
|
+
* ```ts
|
|
729
|
+
* import { sql } from 'kysely'
|
|
730
|
+
*
|
|
731
|
+
* await db.schema
|
|
732
|
+
* .createTable('person')
|
|
733
|
+
* .addColumn('full_name', 'varchar(255)', (col) => col
|
|
734
|
+
* .generatedAlwaysAs(sql`concat(first_name, ' ', last_name)`)
|
|
735
|
+
* .stored()
|
|
736
|
+
* )
|
|
737
|
+
* .execute()
|
|
738
|
+
* ```
|
|
739
|
+
*
|
|
740
|
+
* The generated SQL (MySQL):
|
|
741
|
+
*
|
|
742
|
+
* ```sql
|
|
743
|
+
* create table `person` (
|
|
744
|
+
* `full_name` varchar(255) generated always as (concat(first_name, ' ', last_name)) stored
|
|
745
|
+
* )
|
|
746
|
+
* ```
|
|
747
|
+
*/
|
|
748
|
+
stored() {
|
|
749
|
+
if (!this.#e.generated)
|
|
750
|
+
throw new Error("stored() can only be called after generatedAlwaysAs");
|
|
751
|
+
return new a(c.cloneWith(this.#e, {
|
|
752
|
+
generated: te.cloneWith(this.#e.generated, {
|
|
753
|
+
stored: !0
|
|
754
|
+
})
|
|
755
|
+
}));
|
|
756
|
+
}
|
|
757
|
+
/**
|
|
758
|
+
* This can be used to add any additional SQL right after the column's data type.
|
|
759
|
+
*
|
|
760
|
+
* ### Examples
|
|
761
|
+
*
|
|
762
|
+
* ```ts
|
|
763
|
+
* import { sql } from 'kysely'
|
|
764
|
+
*
|
|
765
|
+
* await db.schema
|
|
766
|
+
* .createTable('person')
|
|
767
|
+
* .addColumn('id', 'integer', col => col.primaryKey())
|
|
768
|
+
* .addColumn(
|
|
769
|
+
* 'first_name',
|
|
770
|
+
* 'varchar(36)',
|
|
771
|
+
* (col) => col.modifyFront(sql`collate utf8mb4_general_ci`).notNull()
|
|
772
|
+
* )
|
|
773
|
+
* .execute()
|
|
774
|
+
* ```
|
|
775
|
+
*
|
|
776
|
+
* The generated SQL (MySQL):
|
|
777
|
+
*
|
|
778
|
+
* ```sql
|
|
779
|
+
* create table `person` (
|
|
780
|
+
* `id` integer primary key,
|
|
781
|
+
* `first_name` varchar(36) collate utf8mb4_general_ci not null
|
|
782
|
+
* )
|
|
783
|
+
* ```
|
|
784
|
+
*/
|
|
785
|
+
modifyFront(e) {
|
|
786
|
+
return new a(c.cloneWithFrontModifier(this.#e, e.toOperationNode()));
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* Adds `nulls not distinct` specifier.
|
|
790
|
+
* Should be used with `unique` constraint.
|
|
791
|
+
*
|
|
792
|
+
* This only works on some dialects like PostgreSQL.
|
|
793
|
+
*
|
|
794
|
+
* ### Examples
|
|
795
|
+
*
|
|
796
|
+
* ```ts
|
|
797
|
+
* db.schema
|
|
798
|
+
* .createTable('person')
|
|
799
|
+
* .addColumn('id', 'integer', col => col.primaryKey())
|
|
800
|
+
* .addColumn('first_name', 'varchar(30)', col => col.unique().nullsNotDistinct())
|
|
801
|
+
* .execute()
|
|
802
|
+
* ```
|
|
803
|
+
*
|
|
804
|
+
* The generated SQL (PostgreSQL):
|
|
805
|
+
*
|
|
806
|
+
* ```sql
|
|
807
|
+
* create table "person" (
|
|
808
|
+
* "id" integer primary key,
|
|
809
|
+
* "first_name" varchar(30) unique nulls not distinct
|
|
810
|
+
* )
|
|
811
|
+
* ```
|
|
812
|
+
*/
|
|
813
|
+
nullsNotDistinct() {
|
|
814
|
+
return new a(c.cloneWith(this.#e, { nullsNotDistinct: !0 }));
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* Adds `if not exists` specifier. This only works for PostgreSQL.
|
|
818
|
+
*
|
|
819
|
+
* ### Examples
|
|
820
|
+
*
|
|
821
|
+
* ```ts
|
|
822
|
+
* await db.schema
|
|
823
|
+
* .alterTable('person')
|
|
824
|
+
* .addColumn('email', 'varchar(255)', col => col.unique().ifNotExists())
|
|
825
|
+
* .execute()
|
|
826
|
+
* ```
|
|
827
|
+
*
|
|
828
|
+
* The generated SQL (PostgreSQL):
|
|
829
|
+
*
|
|
830
|
+
* ```sql
|
|
831
|
+
* alter table "person" add column if not exists "email" varchar(255) unique
|
|
832
|
+
* ```
|
|
833
|
+
*/
|
|
834
|
+
ifNotExists() {
|
|
835
|
+
return new a(c.cloneWith(this.#e, { ifNotExists: !0 }));
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* This can be used to add any additional SQL to the end of the column definition.
|
|
839
|
+
*
|
|
840
|
+
* ### Examples
|
|
841
|
+
*
|
|
842
|
+
* ```ts
|
|
843
|
+
* import { sql } from 'kysely'
|
|
844
|
+
*
|
|
845
|
+
* await db.schema
|
|
846
|
+
* .createTable('person')
|
|
847
|
+
* .addColumn('id', 'integer', col => col.primaryKey())
|
|
848
|
+
* .addColumn(
|
|
849
|
+
* 'age',
|
|
850
|
+
* 'integer',
|
|
851
|
+
* col => col.unsigned()
|
|
852
|
+
* .notNull()
|
|
853
|
+
* .modifyEnd(sql`comment ${sql.lit('it is not polite to ask a woman her age')}`)
|
|
854
|
+
* )
|
|
855
|
+
* .execute()
|
|
856
|
+
* ```
|
|
857
|
+
*
|
|
858
|
+
* The generated SQL (MySQL):
|
|
859
|
+
*
|
|
860
|
+
* ```sql
|
|
861
|
+
* create table `person` (
|
|
862
|
+
* `id` integer primary key,
|
|
863
|
+
* `age` integer unsigned not null comment 'it is not polite to ask a woman her age'
|
|
864
|
+
* )
|
|
865
|
+
* ```
|
|
866
|
+
*/
|
|
867
|
+
modifyEnd(e) {
|
|
868
|
+
return new a(c.cloneWithEndModifier(this.#e, e.toOperationNode()));
|
|
869
|
+
}
|
|
870
|
+
/**
|
|
871
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
872
|
+
* what the provided function returns.
|
|
873
|
+
*/
|
|
874
|
+
$call(e) {
|
|
875
|
+
return e(this);
|
|
876
|
+
}
|
|
877
|
+
toOperationNode() {
|
|
878
|
+
return this.#e;
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
const Te = r({
|
|
882
|
+
is(t) {
|
|
883
|
+
return t.kind === "ModifyColumnNode";
|
|
884
|
+
},
|
|
885
|
+
create(t) {
|
|
886
|
+
return r({
|
|
887
|
+
kind: "ModifyColumnNode",
|
|
888
|
+
column: t
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
}), I = r({
|
|
892
|
+
is(t) {
|
|
893
|
+
return t.kind === "ForeignKeyConstraintNode";
|
|
894
|
+
},
|
|
895
|
+
create(t, e, n, i) {
|
|
896
|
+
return r({
|
|
897
|
+
kind: "ForeignKeyConstraintNode",
|
|
898
|
+
columns: t,
|
|
899
|
+
references: re.create(e, n),
|
|
900
|
+
name: i ? m.create(i) : void 0
|
|
901
|
+
});
|
|
902
|
+
},
|
|
903
|
+
cloneWith(t, e) {
|
|
904
|
+
return r({
|
|
905
|
+
...t,
|
|
906
|
+
...e
|
|
907
|
+
});
|
|
908
|
+
}
|
|
909
|
+
});
|
|
910
|
+
class g {
|
|
911
|
+
#e;
|
|
912
|
+
constructor(e) {
|
|
913
|
+
this.#e = e;
|
|
914
|
+
}
|
|
915
|
+
onDelete(e) {
|
|
916
|
+
return new g(I.cloneWith(this.#e, {
|
|
917
|
+
onDelete: ue(e)
|
|
918
|
+
}));
|
|
919
|
+
}
|
|
920
|
+
onUpdate(e) {
|
|
921
|
+
return new g(I.cloneWith(this.#e, {
|
|
922
|
+
onUpdate: ue(e)
|
|
923
|
+
}));
|
|
924
|
+
}
|
|
925
|
+
deferrable() {
|
|
926
|
+
return new g(I.cloneWith(this.#e, { deferrable: !0 }));
|
|
927
|
+
}
|
|
928
|
+
notDeferrable() {
|
|
929
|
+
return new g(I.cloneWith(this.#e, { deferrable: !1 }));
|
|
930
|
+
}
|
|
931
|
+
initiallyDeferred() {
|
|
932
|
+
return new g(I.cloneWith(this.#e, {
|
|
933
|
+
initiallyDeferred: !0
|
|
934
|
+
}));
|
|
935
|
+
}
|
|
936
|
+
initiallyImmediate() {
|
|
937
|
+
return new g(I.cloneWith(this.#e, {
|
|
938
|
+
initiallyDeferred: !1
|
|
939
|
+
}));
|
|
940
|
+
}
|
|
941
|
+
/**
|
|
942
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
943
|
+
* what the provided function returns.
|
|
944
|
+
*/
|
|
945
|
+
$call(e) {
|
|
946
|
+
return e(this);
|
|
947
|
+
}
|
|
948
|
+
toOperationNode() {
|
|
949
|
+
return this.#e;
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
const ne = r({
|
|
953
|
+
is(t) {
|
|
954
|
+
return t.kind === "AddConstraintNode";
|
|
955
|
+
},
|
|
956
|
+
create(t) {
|
|
957
|
+
return r({
|
|
958
|
+
kind: "AddConstraintNode",
|
|
959
|
+
constraint: t
|
|
960
|
+
});
|
|
961
|
+
}
|
|
962
|
+
}), D = r({
|
|
963
|
+
is(t) {
|
|
964
|
+
return t.kind === "UniqueConstraintNode";
|
|
965
|
+
},
|
|
966
|
+
create(t, e, n) {
|
|
967
|
+
return r({
|
|
968
|
+
kind: "UniqueConstraintNode",
|
|
969
|
+
columns: r(t.map(x.create)),
|
|
970
|
+
name: e ? m.create(e) : void 0,
|
|
971
|
+
nullsNotDistinct: n
|
|
972
|
+
});
|
|
973
|
+
},
|
|
974
|
+
cloneWith(t, e) {
|
|
975
|
+
return r({
|
|
976
|
+
...t,
|
|
977
|
+
...e
|
|
978
|
+
});
|
|
979
|
+
}
|
|
980
|
+
}), ie = r({
|
|
981
|
+
is(t) {
|
|
982
|
+
return t.kind === "DropConstraintNode";
|
|
983
|
+
},
|
|
984
|
+
create(t) {
|
|
985
|
+
return r({
|
|
986
|
+
kind: "DropConstraintNode",
|
|
987
|
+
constraintName: m.create(t)
|
|
988
|
+
});
|
|
989
|
+
},
|
|
990
|
+
cloneWith(t, e) {
|
|
991
|
+
return r({
|
|
992
|
+
...t,
|
|
993
|
+
...e
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
}), Y = r({
|
|
997
|
+
is(t) {
|
|
998
|
+
return t.kind === "AlterColumnNode";
|
|
999
|
+
},
|
|
1000
|
+
create(t, e, n) {
|
|
1001
|
+
return r({
|
|
1002
|
+
kind: "AlterColumnNode",
|
|
1003
|
+
column: x.create(t),
|
|
1004
|
+
[e]: n
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
});
|
|
1008
|
+
class Me {
|
|
1009
|
+
#e;
|
|
1010
|
+
constructor(e) {
|
|
1011
|
+
this.#e = e;
|
|
1012
|
+
}
|
|
1013
|
+
setDataType(e) {
|
|
1014
|
+
return new j(Y.create(this.#e, "dataType", H(e)));
|
|
1015
|
+
}
|
|
1016
|
+
setDefault(e) {
|
|
1017
|
+
return new j(Y.create(this.#e, "setDefault", Se(e)));
|
|
1018
|
+
}
|
|
1019
|
+
dropDefault() {
|
|
1020
|
+
return new j(Y.create(this.#e, "dropDefault", !0));
|
|
1021
|
+
}
|
|
1022
|
+
setNotNull() {
|
|
1023
|
+
return new j(Y.create(this.#e, "setNotNull", !0));
|
|
1024
|
+
}
|
|
1025
|
+
dropNotNull() {
|
|
1026
|
+
return new j(Y.create(this.#e, "dropNotNull", !0));
|
|
1027
|
+
}
|
|
1028
|
+
/**
|
|
1029
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
1030
|
+
* what the provided function returns.
|
|
1031
|
+
*/
|
|
1032
|
+
$call(e) {
|
|
1033
|
+
return e(this);
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
class j {
|
|
1037
|
+
#e;
|
|
1038
|
+
constructor(e) {
|
|
1039
|
+
this.#e = e;
|
|
1040
|
+
}
|
|
1041
|
+
toOperationNode() {
|
|
1042
|
+
return this.#e;
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
class L {
|
|
1046
|
+
#e;
|
|
1047
|
+
constructor(e) {
|
|
1048
|
+
this.#e = r(e);
|
|
1049
|
+
}
|
|
1050
|
+
toOperationNode() {
|
|
1051
|
+
return this.#e.executor.transformQuery(this.#e.node, this.#e.queryId);
|
|
1052
|
+
}
|
|
1053
|
+
compile() {
|
|
1054
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
1055
|
+
}
|
|
1056
|
+
async execute() {
|
|
1057
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
class k {
|
|
1061
|
+
#e;
|
|
1062
|
+
constructor(e) {
|
|
1063
|
+
this.#e = r(e);
|
|
1064
|
+
}
|
|
1065
|
+
onDelete(e) {
|
|
1066
|
+
return new k({
|
|
1067
|
+
...this.#e,
|
|
1068
|
+
constraintBuilder: this.#e.constraintBuilder.onDelete(e)
|
|
1069
|
+
});
|
|
1070
|
+
}
|
|
1071
|
+
onUpdate(e) {
|
|
1072
|
+
return new k({
|
|
1073
|
+
...this.#e,
|
|
1074
|
+
constraintBuilder: this.#e.constraintBuilder.onUpdate(e)
|
|
1075
|
+
});
|
|
1076
|
+
}
|
|
1077
|
+
deferrable() {
|
|
1078
|
+
return new k({
|
|
1079
|
+
...this.#e,
|
|
1080
|
+
constraintBuilder: this.#e.constraintBuilder.deferrable()
|
|
1081
|
+
});
|
|
1082
|
+
}
|
|
1083
|
+
notDeferrable() {
|
|
1084
|
+
return new k({
|
|
1085
|
+
...this.#e,
|
|
1086
|
+
constraintBuilder: this.#e.constraintBuilder.notDeferrable()
|
|
1087
|
+
});
|
|
1088
|
+
}
|
|
1089
|
+
initiallyDeferred() {
|
|
1090
|
+
return new k({
|
|
1091
|
+
...this.#e,
|
|
1092
|
+
constraintBuilder: this.#e.constraintBuilder.initiallyDeferred()
|
|
1093
|
+
});
|
|
1094
|
+
}
|
|
1095
|
+
initiallyImmediate() {
|
|
1096
|
+
return new k({
|
|
1097
|
+
...this.#e,
|
|
1098
|
+
constraintBuilder: this.#e.constraintBuilder.initiallyImmediate()
|
|
1099
|
+
});
|
|
1100
|
+
}
|
|
1101
|
+
/**
|
|
1102
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
1103
|
+
* what the provided function returns.
|
|
1104
|
+
*/
|
|
1105
|
+
$call(e) {
|
|
1106
|
+
return e(this);
|
|
1107
|
+
}
|
|
1108
|
+
toOperationNode() {
|
|
1109
|
+
return this.#e.executor.transformQuery(s.cloneWithTableProps(this.#e.node, {
|
|
1110
|
+
addConstraint: ne.create(this.#e.constraintBuilder.toOperationNode())
|
|
1111
|
+
}), this.#e.queryId);
|
|
1112
|
+
}
|
|
1113
|
+
compile() {
|
|
1114
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
1115
|
+
}
|
|
1116
|
+
async execute() {
|
|
1117
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
class z {
|
|
1121
|
+
#e;
|
|
1122
|
+
constructor(e) {
|
|
1123
|
+
this.#e = r(e);
|
|
1124
|
+
}
|
|
1125
|
+
ifExists() {
|
|
1126
|
+
return new z({
|
|
1127
|
+
...this.#e,
|
|
1128
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1129
|
+
dropConstraint: ie.cloneWith(this.#e.node.dropConstraint, {
|
|
1130
|
+
ifExists: !0
|
|
1131
|
+
})
|
|
1132
|
+
})
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1135
|
+
cascade() {
|
|
1136
|
+
return new z({
|
|
1137
|
+
...this.#e,
|
|
1138
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1139
|
+
dropConstraint: ie.cloneWith(this.#e.node.dropConstraint, {
|
|
1140
|
+
modifier: "cascade"
|
|
1141
|
+
})
|
|
1142
|
+
})
|
|
1143
|
+
});
|
|
1144
|
+
}
|
|
1145
|
+
restrict() {
|
|
1146
|
+
return new z({
|
|
1147
|
+
...this.#e,
|
|
1148
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1149
|
+
dropConstraint: ie.cloneWith(this.#e.node.dropConstraint, {
|
|
1150
|
+
modifier: "restrict"
|
|
1151
|
+
})
|
|
1152
|
+
})
|
|
1153
|
+
});
|
|
1154
|
+
}
|
|
1155
|
+
/**
|
|
1156
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
1157
|
+
* what the provided function returns.
|
|
1158
|
+
*/
|
|
1159
|
+
$call(e) {
|
|
1160
|
+
return e(this);
|
|
1161
|
+
}
|
|
1162
|
+
toOperationNode() {
|
|
1163
|
+
return this.#e.executor.transformQuery(this.#e.node, this.#e.queryId);
|
|
1164
|
+
}
|
|
1165
|
+
compile() {
|
|
1166
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
1167
|
+
}
|
|
1168
|
+
async execute() {
|
|
1169
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
const U = r({
|
|
1173
|
+
is(t) {
|
|
1174
|
+
return t.kind === "PrimaryKeyConstraintNode";
|
|
1175
|
+
},
|
|
1176
|
+
create(t, e) {
|
|
1177
|
+
return r({
|
|
1178
|
+
kind: "PrimaryKeyConstraintNode",
|
|
1179
|
+
columns: r(t.map(x.create)),
|
|
1180
|
+
name: e ? m.create(e) : void 0
|
|
1181
|
+
});
|
|
1182
|
+
},
|
|
1183
|
+
cloneWith(t, e) {
|
|
1184
|
+
return r({ ...t, ...e });
|
|
1185
|
+
}
|
|
1186
|
+
}), F = r({
|
|
1187
|
+
is(t) {
|
|
1188
|
+
return t.kind === "AddIndexNode";
|
|
1189
|
+
},
|
|
1190
|
+
create(t) {
|
|
1191
|
+
return r({
|
|
1192
|
+
kind: "AddIndexNode",
|
|
1193
|
+
name: m.create(t)
|
|
1194
|
+
});
|
|
1195
|
+
},
|
|
1196
|
+
cloneWith(t, e) {
|
|
1197
|
+
return r({
|
|
1198
|
+
...t,
|
|
1199
|
+
...e
|
|
1200
|
+
});
|
|
1201
|
+
},
|
|
1202
|
+
cloneWithColumns(t, e) {
|
|
1203
|
+
return r({
|
|
1204
|
+
...t,
|
|
1205
|
+
columns: [...t.columns || [], ...e]
|
|
1206
|
+
});
|
|
1207
|
+
}
|
|
1208
|
+
});
|
|
1209
|
+
class S {
|
|
1210
|
+
#e;
|
|
1211
|
+
constructor(e) {
|
|
1212
|
+
this.#e = r(e);
|
|
1213
|
+
}
|
|
1214
|
+
/**
|
|
1215
|
+
* Makes the index unique.
|
|
1216
|
+
*
|
|
1217
|
+
* ### Examples
|
|
1218
|
+
*
|
|
1219
|
+
* ```ts
|
|
1220
|
+
* await db.schema
|
|
1221
|
+
* .alterTable('person')
|
|
1222
|
+
* .addIndex('person_first_name_index')
|
|
1223
|
+
* .unique()
|
|
1224
|
+
* .column('email')
|
|
1225
|
+
* .execute()
|
|
1226
|
+
* ```
|
|
1227
|
+
*
|
|
1228
|
+
* The generated SQL (MySQL):
|
|
1229
|
+
*
|
|
1230
|
+
* ```sql
|
|
1231
|
+
* alter table `person` add unique index `person_first_name_index` (`email`)
|
|
1232
|
+
* ```
|
|
1233
|
+
*/
|
|
1234
|
+
unique() {
|
|
1235
|
+
return new S({
|
|
1236
|
+
...this.#e,
|
|
1237
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1238
|
+
addIndex: F.cloneWith(this.#e.node.addIndex, {
|
|
1239
|
+
unique: !0
|
|
1240
|
+
})
|
|
1241
|
+
})
|
|
1242
|
+
});
|
|
1243
|
+
}
|
|
1244
|
+
/**
|
|
1245
|
+
* Adds a column to the index.
|
|
1246
|
+
*
|
|
1247
|
+
* Also see {@link columns} for adding multiple columns at once or {@link expression}
|
|
1248
|
+
* for specifying an arbitrary expression.
|
|
1249
|
+
*
|
|
1250
|
+
* ### Examples
|
|
1251
|
+
*
|
|
1252
|
+
* ```ts
|
|
1253
|
+
* await db.schema
|
|
1254
|
+
* .alterTable('person')
|
|
1255
|
+
* .addIndex('person_first_name_and_age_index')
|
|
1256
|
+
* .column('first_name')
|
|
1257
|
+
* .column('age desc')
|
|
1258
|
+
* .execute()
|
|
1259
|
+
* ```
|
|
1260
|
+
*
|
|
1261
|
+
* The generated SQL (MySQL):
|
|
1262
|
+
*
|
|
1263
|
+
* ```sql
|
|
1264
|
+
* alter table `person` add index `person_first_name_and_age_index` (`first_name`, `age` desc)
|
|
1265
|
+
* ```
|
|
1266
|
+
*/
|
|
1267
|
+
column(e) {
|
|
1268
|
+
return new S({
|
|
1269
|
+
...this.#e,
|
|
1270
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1271
|
+
addIndex: F.cloneWithColumns(this.#e.node.addIndex, [
|
|
1272
|
+
ce(e)
|
|
1273
|
+
])
|
|
1274
|
+
})
|
|
1275
|
+
});
|
|
1276
|
+
}
|
|
1277
|
+
/**
|
|
1278
|
+
* Specifies a list of columns for the index.
|
|
1279
|
+
*
|
|
1280
|
+
* Also see {@link column} for adding a single column or {@link expression} for
|
|
1281
|
+
* specifying an arbitrary expression.
|
|
1282
|
+
*
|
|
1283
|
+
* ### Examples
|
|
1284
|
+
*
|
|
1285
|
+
* ```ts
|
|
1286
|
+
* await db.schema
|
|
1287
|
+
* .alterTable('person')
|
|
1288
|
+
* .addIndex('person_first_name_and_age_index')
|
|
1289
|
+
* .columns(['first_name', 'age desc'])
|
|
1290
|
+
* .execute()
|
|
1291
|
+
* ```
|
|
1292
|
+
*
|
|
1293
|
+
* The generated SQL (MySQL):
|
|
1294
|
+
*
|
|
1295
|
+
* ```sql
|
|
1296
|
+
* alter table `person` add index `person_first_name_and_age_index` (`first_name`, `age` desc)
|
|
1297
|
+
* ```
|
|
1298
|
+
*/
|
|
1299
|
+
columns(e) {
|
|
1300
|
+
return new S({
|
|
1301
|
+
...this.#e,
|
|
1302
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1303
|
+
addIndex: F.cloneWithColumns(this.#e.node.addIndex, e.map(ce))
|
|
1304
|
+
})
|
|
1305
|
+
});
|
|
1306
|
+
}
|
|
1307
|
+
/**
|
|
1308
|
+
* Specifies an arbitrary expression for the index.
|
|
1309
|
+
*
|
|
1310
|
+
* ### Examples
|
|
1311
|
+
*
|
|
1312
|
+
* ```ts
|
|
1313
|
+
* import { sql } from 'kysely'
|
|
1314
|
+
*
|
|
1315
|
+
* await db.schema
|
|
1316
|
+
* .alterTable('person')
|
|
1317
|
+
* .addIndex('person_first_name_index')
|
|
1318
|
+
* .expression(sql<boolean>`(first_name < 'Sami')`)
|
|
1319
|
+
* .execute()
|
|
1320
|
+
* ```
|
|
1321
|
+
*
|
|
1322
|
+
* The generated SQL (MySQL):
|
|
1323
|
+
*
|
|
1324
|
+
* ```sql
|
|
1325
|
+
* alter table `person` add index `person_first_name_index` ((first_name < 'Sami'))
|
|
1326
|
+
* ```
|
|
1327
|
+
*/
|
|
1328
|
+
expression(e) {
|
|
1329
|
+
return new S({
|
|
1330
|
+
...this.#e,
|
|
1331
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1332
|
+
addIndex: F.cloneWithColumns(this.#e.node.addIndex, [
|
|
1333
|
+
e.toOperationNode()
|
|
1334
|
+
])
|
|
1335
|
+
})
|
|
1336
|
+
});
|
|
1337
|
+
}
|
|
1338
|
+
using(e) {
|
|
1339
|
+
return new S({
|
|
1340
|
+
...this.#e,
|
|
1341
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1342
|
+
addIndex: F.cloneWith(this.#e.node.addIndex, {
|
|
1343
|
+
using: ke.createWithSql(e)
|
|
1344
|
+
})
|
|
1345
|
+
})
|
|
1346
|
+
});
|
|
1347
|
+
}
|
|
1348
|
+
/**
|
|
1349
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
1350
|
+
* what the provided function returns.
|
|
1351
|
+
*/
|
|
1352
|
+
$call(e) {
|
|
1353
|
+
return e(this);
|
|
1354
|
+
}
|
|
1355
|
+
toOperationNode() {
|
|
1356
|
+
return this.#e.executor.transformQuery(this.#e.node, this.#e.queryId);
|
|
1357
|
+
}
|
|
1358
|
+
compile() {
|
|
1359
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
1360
|
+
}
|
|
1361
|
+
async execute() {
|
|
1362
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
class v {
|
|
1366
|
+
#e;
|
|
1367
|
+
constructor(e) {
|
|
1368
|
+
this.#e = e;
|
|
1369
|
+
}
|
|
1370
|
+
/**
|
|
1371
|
+
* Adds `nulls not distinct` to the unique constraint definition
|
|
1372
|
+
*
|
|
1373
|
+
* Supported by PostgreSQL dialect only
|
|
1374
|
+
*/
|
|
1375
|
+
nullsNotDistinct() {
|
|
1376
|
+
return new v(D.cloneWith(this.#e, { nullsNotDistinct: !0 }));
|
|
1377
|
+
}
|
|
1378
|
+
deferrable() {
|
|
1379
|
+
return new v(D.cloneWith(this.#e, { deferrable: !0 }));
|
|
1380
|
+
}
|
|
1381
|
+
notDeferrable() {
|
|
1382
|
+
return new v(D.cloneWith(this.#e, { deferrable: !1 }));
|
|
1383
|
+
}
|
|
1384
|
+
initiallyDeferred() {
|
|
1385
|
+
return new v(D.cloneWith(this.#e, {
|
|
1386
|
+
initiallyDeferred: !0
|
|
1387
|
+
}));
|
|
1388
|
+
}
|
|
1389
|
+
initiallyImmediate() {
|
|
1390
|
+
return new v(D.cloneWith(this.#e, {
|
|
1391
|
+
initiallyDeferred: !1
|
|
1392
|
+
}));
|
|
1393
|
+
}
|
|
1394
|
+
/**
|
|
1395
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
1396
|
+
* what the provided function returns.
|
|
1397
|
+
*/
|
|
1398
|
+
$call(e) {
|
|
1399
|
+
return e(this);
|
|
1400
|
+
}
|
|
1401
|
+
toOperationNode() {
|
|
1402
|
+
return this.#e;
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
class A {
|
|
1406
|
+
#e;
|
|
1407
|
+
constructor(e) {
|
|
1408
|
+
this.#e = e;
|
|
1409
|
+
}
|
|
1410
|
+
deferrable() {
|
|
1411
|
+
return new A(U.cloneWith(this.#e, { deferrable: !0 }));
|
|
1412
|
+
}
|
|
1413
|
+
notDeferrable() {
|
|
1414
|
+
return new A(U.cloneWith(this.#e, { deferrable: !1 }));
|
|
1415
|
+
}
|
|
1416
|
+
initiallyDeferred() {
|
|
1417
|
+
return new A(U.cloneWith(this.#e, {
|
|
1418
|
+
initiallyDeferred: !0
|
|
1419
|
+
}));
|
|
1420
|
+
}
|
|
1421
|
+
initiallyImmediate() {
|
|
1422
|
+
return new A(U.cloneWith(this.#e, {
|
|
1423
|
+
initiallyDeferred: !1
|
|
1424
|
+
}));
|
|
1425
|
+
}
|
|
1426
|
+
/**
|
|
1427
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
1428
|
+
* what the provided function returns.
|
|
1429
|
+
*/
|
|
1430
|
+
$call(e) {
|
|
1431
|
+
return e(this);
|
|
1432
|
+
}
|
|
1433
|
+
toOperationNode() {
|
|
1434
|
+
return this.#e;
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
class Ae {
|
|
1438
|
+
#e;
|
|
1439
|
+
constructor(e) {
|
|
1440
|
+
this.#e = e;
|
|
1441
|
+
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
1444
|
+
* what the provided function returns.
|
|
1445
|
+
*/
|
|
1446
|
+
$call(e) {
|
|
1447
|
+
return e(this);
|
|
1448
|
+
}
|
|
1449
|
+
toOperationNode() {
|
|
1450
|
+
return this.#e;
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
const at = r({
|
|
1454
|
+
is(t) {
|
|
1455
|
+
return t.kind === "RenameConstraintNode";
|
|
1456
|
+
},
|
|
1457
|
+
create(t, e) {
|
|
1458
|
+
return r({
|
|
1459
|
+
kind: "RenameConstraintNode",
|
|
1460
|
+
oldName: m.create(t),
|
|
1461
|
+
newName: m.create(e)
|
|
1462
|
+
});
|
|
1463
|
+
}
|
|
1464
|
+
});
|
|
1465
|
+
class ct {
|
|
1466
|
+
#e;
|
|
1467
|
+
constructor(e) {
|
|
1468
|
+
this.#e = r(e);
|
|
1469
|
+
}
|
|
1470
|
+
renameTo(e) {
|
|
1471
|
+
return new L({
|
|
1472
|
+
...this.#e,
|
|
1473
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1474
|
+
renameTo: E(e)
|
|
1475
|
+
})
|
|
1476
|
+
});
|
|
1477
|
+
}
|
|
1478
|
+
setSchema(e) {
|
|
1479
|
+
return new L({
|
|
1480
|
+
...this.#e,
|
|
1481
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1482
|
+
setSchema: m.create(e)
|
|
1483
|
+
})
|
|
1484
|
+
});
|
|
1485
|
+
}
|
|
1486
|
+
alterColumn(e, n) {
|
|
1487
|
+
const i = n(new Me(e));
|
|
1488
|
+
return new N({
|
|
1489
|
+
...this.#e,
|
|
1490
|
+
node: s.cloneWithColumnAlteration(this.#e.node, i.toOperationNode())
|
|
1491
|
+
});
|
|
1492
|
+
}
|
|
1493
|
+
dropColumn(e) {
|
|
1494
|
+
return new N({
|
|
1495
|
+
...this.#e,
|
|
1496
|
+
node: s.cloneWithColumnAlteration(this.#e.node, Qe.create(e))
|
|
1497
|
+
});
|
|
1498
|
+
}
|
|
1499
|
+
renameColumn(e, n) {
|
|
1500
|
+
return new N({
|
|
1501
|
+
...this.#e,
|
|
1502
|
+
node: s.cloneWithColumnAlteration(this.#e.node, De.create(e, n))
|
|
1503
|
+
});
|
|
1504
|
+
}
|
|
1505
|
+
addColumn(e, n, i = w) {
|
|
1506
|
+
const o = i(new a(c.create(e, H(n))));
|
|
1507
|
+
return new N({
|
|
1508
|
+
...this.#e,
|
|
1509
|
+
node: s.cloneWithColumnAlteration(this.#e.node, Ee.create(o.toOperationNode()))
|
|
1510
|
+
});
|
|
1511
|
+
}
|
|
1512
|
+
modifyColumn(e, n, i = w) {
|
|
1513
|
+
const o = i(new a(c.create(e, H(n))));
|
|
1514
|
+
return new N({
|
|
1515
|
+
...this.#e,
|
|
1516
|
+
node: s.cloneWithColumnAlteration(this.#e.node, Te.create(o.toOperationNode()))
|
|
1517
|
+
});
|
|
1518
|
+
}
|
|
1519
|
+
/**
|
|
1520
|
+
* See {@link CreateTableBuilder.addUniqueConstraint}
|
|
1521
|
+
*/
|
|
1522
|
+
addUniqueConstraint(e, n, i = w) {
|
|
1523
|
+
const o = i(new v(D.create(n, e)));
|
|
1524
|
+
return new L({
|
|
1525
|
+
...this.#e,
|
|
1526
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1527
|
+
addConstraint: ne.create(o.toOperationNode())
|
|
1528
|
+
})
|
|
1529
|
+
});
|
|
1530
|
+
}
|
|
1531
|
+
/**
|
|
1532
|
+
* See {@link CreateTableBuilder.addCheckConstraint}
|
|
1533
|
+
*/
|
|
1534
|
+
addCheckConstraint(e, n, i = w) {
|
|
1535
|
+
const o = i(new Ae(xe.create(n.toOperationNode(), e)));
|
|
1536
|
+
return new L({
|
|
1537
|
+
...this.#e,
|
|
1538
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1539
|
+
addConstraint: ne.create(o.toOperationNode())
|
|
1540
|
+
})
|
|
1541
|
+
});
|
|
1542
|
+
}
|
|
1543
|
+
/**
|
|
1544
|
+
* See {@link CreateTableBuilder.addForeignKeyConstraint}
|
|
1545
|
+
*
|
|
1546
|
+
* Unlike {@link CreateTableBuilder.addForeignKeyConstraint} this method returns
|
|
1547
|
+
* the constraint builder and doesn't take a callback as the last argument. This
|
|
1548
|
+
* is because you can only add one column per `ALTER TABLE` query.
|
|
1549
|
+
*/
|
|
1550
|
+
addForeignKeyConstraint(e, n, i, o, u = w) {
|
|
1551
|
+
const h = u(new g(I.create(n.map(x.create), E(i), o.map(x.create), e)));
|
|
1552
|
+
return new k({
|
|
1553
|
+
...this.#e,
|
|
1554
|
+
constraintBuilder: h
|
|
1555
|
+
});
|
|
1556
|
+
}
|
|
1557
|
+
/**
|
|
1558
|
+
* See {@link CreateTableBuilder.addPrimaryKeyConstraint}
|
|
1559
|
+
*/
|
|
1560
|
+
addPrimaryKeyConstraint(e, n, i = w) {
|
|
1561
|
+
const o = i(new A(U.create(n, e)));
|
|
1562
|
+
return new L({
|
|
1563
|
+
...this.#e,
|
|
1564
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1565
|
+
addConstraint: ne.create(o.toOperationNode())
|
|
1566
|
+
})
|
|
1567
|
+
});
|
|
1568
|
+
}
|
|
1569
|
+
dropConstraint(e) {
|
|
1570
|
+
return new z({
|
|
1571
|
+
...this.#e,
|
|
1572
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1573
|
+
dropConstraint: ie.create(e)
|
|
1574
|
+
})
|
|
1575
|
+
});
|
|
1576
|
+
}
|
|
1577
|
+
renameConstraint(e, n) {
|
|
1578
|
+
return new z({
|
|
1579
|
+
...this.#e,
|
|
1580
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1581
|
+
renameConstraint: at.create(e, n)
|
|
1582
|
+
})
|
|
1583
|
+
});
|
|
1584
|
+
}
|
|
1585
|
+
/**
|
|
1586
|
+
* This can be used to add index to table.
|
|
1587
|
+
*
|
|
1588
|
+
* ### Examples
|
|
1589
|
+
*
|
|
1590
|
+
* ```ts
|
|
1591
|
+
* db.schema.alterTable('person')
|
|
1592
|
+
* .addIndex('person_email_index')
|
|
1593
|
+
* .column('email')
|
|
1594
|
+
* .unique()
|
|
1595
|
+
* .execute()
|
|
1596
|
+
* ```
|
|
1597
|
+
*
|
|
1598
|
+
* The generated SQL (MySQL):
|
|
1599
|
+
*
|
|
1600
|
+
* ```sql
|
|
1601
|
+
* alter table `person` add unique index `person_email_index` (`email`)
|
|
1602
|
+
* ```
|
|
1603
|
+
*/
|
|
1604
|
+
addIndex(e) {
|
|
1605
|
+
return new S({
|
|
1606
|
+
...this.#e,
|
|
1607
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1608
|
+
addIndex: F.create(e)
|
|
1609
|
+
})
|
|
1610
|
+
});
|
|
1611
|
+
}
|
|
1612
|
+
/**
|
|
1613
|
+
* This can be used to drop index from table.
|
|
1614
|
+
*
|
|
1615
|
+
* ### Examples
|
|
1616
|
+
*
|
|
1617
|
+
* ```ts
|
|
1618
|
+
* db.schema.alterTable('person')
|
|
1619
|
+
* .dropIndex('person_email_index')
|
|
1620
|
+
* .execute()
|
|
1621
|
+
* ```
|
|
1622
|
+
*
|
|
1623
|
+
* The generated SQL (MySQL):
|
|
1624
|
+
*
|
|
1625
|
+
* ```sql
|
|
1626
|
+
* alter table `person` drop index `test_first_name_index`
|
|
1627
|
+
* ```
|
|
1628
|
+
*/
|
|
1629
|
+
dropIndex(e) {
|
|
1630
|
+
return new L({
|
|
1631
|
+
...this.#e,
|
|
1632
|
+
node: s.cloneWithTableProps(this.#e.node, {
|
|
1633
|
+
dropIndex: Z.create(e)
|
|
1634
|
+
})
|
|
1635
|
+
});
|
|
1636
|
+
}
|
|
1637
|
+
/**
|
|
1638
|
+
* Calls the given function passing `this` as the only argument.
|
|
1639
|
+
*
|
|
1640
|
+
* See {@link CreateTableBuilder.$call}
|
|
1641
|
+
*/
|
|
1642
|
+
$call(e) {
|
|
1643
|
+
return e(this);
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
class N {
|
|
1647
|
+
#e;
|
|
1648
|
+
constructor(e) {
|
|
1649
|
+
this.#e = r(e);
|
|
1650
|
+
}
|
|
1651
|
+
alterColumn(e, n) {
|
|
1652
|
+
const i = n(new Me(e));
|
|
1653
|
+
return new N({
|
|
1654
|
+
...this.#e,
|
|
1655
|
+
node: s.cloneWithColumnAlteration(this.#e.node, i.toOperationNode())
|
|
1656
|
+
});
|
|
1657
|
+
}
|
|
1658
|
+
dropColumn(e) {
|
|
1659
|
+
return new N({
|
|
1660
|
+
...this.#e,
|
|
1661
|
+
node: s.cloneWithColumnAlteration(this.#e.node, Qe.create(e))
|
|
1662
|
+
});
|
|
1663
|
+
}
|
|
1664
|
+
renameColumn(e, n) {
|
|
1665
|
+
return new N({
|
|
1666
|
+
...this.#e,
|
|
1667
|
+
node: s.cloneWithColumnAlteration(this.#e.node, De.create(e, n))
|
|
1668
|
+
});
|
|
1669
|
+
}
|
|
1670
|
+
addColumn(e, n, i = w) {
|
|
1671
|
+
const o = i(new a(c.create(e, H(n))));
|
|
1672
|
+
return new N({
|
|
1673
|
+
...this.#e,
|
|
1674
|
+
node: s.cloneWithColumnAlteration(this.#e.node, Ee.create(o.toOperationNode()))
|
|
1675
|
+
});
|
|
1676
|
+
}
|
|
1677
|
+
modifyColumn(e, n, i = w) {
|
|
1678
|
+
const o = i(new a(c.create(e, H(n))));
|
|
1679
|
+
return new N({
|
|
1680
|
+
...this.#e,
|
|
1681
|
+
node: s.cloneWithColumnAlteration(this.#e.node, Te.create(o.toOperationNode()))
|
|
1682
|
+
});
|
|
1683
|
+
}
|
|
1684
|
+
toOperationNode() {
|
|
1685
|
+
return this.#e.executor.transformQuery(this.#e.node, this.#e.queryId);
|
|
1686
|
+
}
|
|
1687
|
+
compile() {
|
|
1688
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
1689
|
+
}
|
|
1690
|
+
async execute() {
|
|
1691
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1694
|
+
class $e extends Ge {
|
|
1695
|
+
transformPrimitiveValueList(e) {
|
|
1696
|
+
return qe.create(e.values.map(ae.createImmediate));
|
|
1697
|
+
}
|
|
1698
|
+
transformValue(e) {
|
|
1699
|
+
return ae.createImmediate(e.value);
|
|
1700
|
+
}
|
|
1701
|
+
}
|
|
1702
|
+
class f {
|
|
1703
|
+
#e;
|
|
1704
|
+
constructor(e) {
|
|
1705
|
+
this.#e = r(e);
|
|
1706
|
+
}
|
|
1707
|
+
/**
|
|
1708
|
+
* Adds the "if not exists" modifier.
|
|
1709
|
+
*
|
|
1710
|
+
* If the index already exists, no error is thrown if this method has been called.
|
|
1711
|
+
*/
|
|
1712
|
+
ifNotExists() {
|
|
1713
|
+
return new f({
|
|
1714
|
+
...this.#e,
|
|
1715
|
+
node: O.cloneWith(this.#e.node, {
|
|
1716
|
+
ifNotExists: !0
|
|
1717
|
+
})
|
|
1718
|
+
});
|
|
1719
|
+
}
|
|
1720
|
+
/**
|
|
1721
|
+
* Makes the index unique.
|
|
1722
|
+
*/
|
|
1723
|
+
unique() {
|
|
1724
|
+
return new f({
|
|
1725
|
+
...this.#e,
|
|
1726
|
+
node: O.cloneWith(this.#e.node, {
|
|
1727
|
+
unique: !0
|
|
1728
|
+
})
|
|
1729
|
+
});
|
|
1730
|
+
}
|
|
1731
|
+
/**
|
|
1732
|
+
* Adds `nulls not distinct` specifier to index.
|
|
1733
|
+
* This only works on some dialects like PostgreSQL.
|
|
1734
|
+
*
|
|
1735
|
+
* ### Examples
|
|
1736
|
+
*
|
|
1737
|
+
* ```ts
|
|
1738
|
+
* db.schema.createIndex('person_first_name_index')
|
|
1739
|
+
* .on('person')
|
|
1740
|
+
* .column('first_name')
|
|
1741
|
+
* .nullsNotDistinct()
|
|
1742
|
+
* .execute()
|
|
1743
|
+
* ```
|
|
1744
|
+
*
|
|
1745
|
+
* The generated SQL (PostgreSQL):
|
|
1746
|
+
*
|
|
1747
|
+
* ```sql
|
|
1748
|
+
* create index "person_first_name_index"
|
|
1749
|
+
* on "test" ("first_name")
|
|
1750
|
+
* nulls not distinct;
|
|
1751
|
+
* ```
|
|
1752
|
+
*/
|
|
1753
|
+
nullsNotDistinct() {
|
|
1754
|
+
return new f({
|
|
1755
|
+
...this.#e,
|
|
1756
|
+
node: O.cloneWith(this.#e.node, {
|
|
1757
|
+
nullsNotDistinct: !0
|
|
1758
|
+
})
|
|
1759
|
+
});
|
|
1760
|
+
}
|
|
1761
|
+
/**
|
|
1762
|
+
* Specifies the table for the index.
|
|
1763
|
+
*/
|
|
1764
|
+
on(e) {
|
|
1765
|
+
return new f({
|
|
1766
|
+
...this.#e,
|
|
1767
|
+
node: O.cloneWith(this.#e.node, {
|
|
1768
|
+
table: E(e)
|
|
1769
|
+
})
|
|
1770
|
+
});
|
|
1771
|
+
}
|
|
1772
|
+
/**
|
|
1773
|
+
* Adds a column to the index.
|
|
1774
|
+
*
|
|
1775
|
+
* Also see {@link columns} for adding multiple columns at once or {@link expression}
|
|
1776
|
+
* for specifying an arbitrary expression.
|
|
1777
|
+
*
|
|
1778
|
+
* ### Examples
|
|
1779
|
+
*
|
|
1780
|
+
* ```ts
|
|
1781
|
+
* await db.schema
|
|
1782
|
+
* .createIndex('person_first_name_and_age_index')
|
|
1783
|
+
* .on('person')
|
|
1784
|
+
* .column('first_name')
|
|
1785
|
+
* .column('age desc')
|
|
1786
|
+
* .execute()
|
|
1787
|
+
* ```
|
|
1788
|
+
*
|
|
1789
|
+
* The generated SQL (PostgreSQL):
|
|
1790
|
+
*
|
|
1791
|
+
* ```sql
|
|
1792
|
+
* create index "person_first_name_and_age_index" on "person" ("first_name", "age" desc)
|
|
1793
|
+
* ```
|
|
1794
|
+
*/
|
|
1795
|
+
column(e) {
|
|
1796
|
+
return new f({
|
|
1797
|
+
...this.#e,
|
|
1798
|
+
node: O.cloneWithColumns(this.#e.node, [
|
|
1799
|
+
ce(e)
|
|
1800
|
+
])
|
|
1801
|
+
});
|
|
1802
|
+
}
|
|
1803
|
+
/**
|
|
1804
|
+
* Specifies a list of columns for the index.
|
|
1805
|
+
*
|
|
1806
|
+
* Also see {@link column} for adding a single column or {@link expression} for
|
|
1807
|
+
* specifying an arbitrary expression.
|
|
1808
|
+
*
|
|
1809
|
+
* ### Examples
|
|
1810
|
+
*
|
|
1811
|
+
* ```ts
|
|
1812
|
+
* await db.schema
|
|
1813
|
+
* .createIndex('person_first_name_and_age_index')
|
|
1814
|
+
* .on('person')
|
|
1815
|
+
* .columns(['first_name', 'age desc'])
|
|
1816
|
+
* .execute()
|
|
1817
|
+
* ```
|
|
1818
|
+
*
|
|
1819
|
+
* The generated SQL (PostgreSQL):
|
|
1820
|
+
*
|
|
1821
|
+
* ```sql
|
|
1822
|
+
* create index "person_first_name_and_age_index" on "person" ("first_name", "age" desc)
|
|
1823
|
+
* ```
|
|
1824
|
+
*/
|
|
1825
|
+
columns(e) {
|
|
1826
|
+
return new f({
|
|
1827
|
+
...this.#e,
|
|
1828
|
+
node: O.cloneWithColumns(this.#e.node, e.map(ce))
|
|
1829
|
+
});
|
|
1830
|
+
}
|
|
1831
|
+
/**
|
|
1832
|
+
* Specifies an arbitrary expression for the index.
|
|
1833
|
+
*
|
|
1834
|
+
* ### Examples
|
|
1835
|
+
*
|
|
1836
|
+
* ```ts
|
|
1837
|
+
* import { sql } from 'kysely'
|
|
1838
|
+
*
|
|
1839
|
+
* await db.schema
|
|
1840
|
+
* .createIndex('person_first_name_index')
|
|
1841
|
+
* .on('person')
|
|
1842
|
+
* .expression(sql`first_name COLLATE "fi_FI"`)
|
|
1843
|
+
* .execute()
|
|
1844
|
+
* ```
|
|
1845
|
+
*
|
|
1846
|
+
* The generated SQL (PostgreSQL):
|
|
1847
|
+
*
|
|
1848
|
+
* ```sql
|
|
1849
|
+
* create index "person_first_name_index" on "person" (first_name COLLATE "fi_FI")
|
|
1850
|
+
* ```
|
|
1851
|
+
*/
|
|
1852
|
+
expression(e) {
|
|
1853
|
+
return new f({
|
|
1854
|
+
...this.#e,
|
|
1855
|
+
node: O.cloneWithColumns(this.#e.node, [
|
|
1856
|
+
e.toOperationNode()
|
|
1857
|
+
])
|
|
1858
|
+
});
|
|
1859
|
+
}
|
|
1860
|
+
using(e) {
|
|
1861
|
+
return new f({
|
|
1862
|
+
...this.#e,
|
|
1863
|
+
node: O.cloneWith(this.#e.node, {
|
|
1864
|
+
using: ke.createWithSql(e)
|
|
1865
|
+
})
|
|
1866
|
+
});
|
|
1867
|
+
}
|
|
1868
|
+
where(...e) {
|
|
1869
|
+
const n = new $e();
|
|
1870
|
+
return new f({
|
|
1871
|
+
...this.#e,
|
|
1872
|
+
node: He.cloneWithWhere(this.#e.node, n.transformNode(Je(e), this.#e.queryId))
|
|
1873
|
+
});
|
|
1874
|
+
}
|
|
1875
|
+
/**
|
|
1876
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
1877
|
+
* what the provided function returns.
|
|
1878
|
+
*/
|
|
1879
|
+
$call(e) {
|
|
1880
|
+
return e(this);
|
|
1881
|
+
}
|
|
1882
|
+
toOperationNode() {
|
|
1883
|
+
return this.#e.executor.transformQuery(this.#e.node, this.#e.queryId);
|
|
1884
|
+
}
|
|
1885
|
+
compile() {
|
|
1886
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
1887
|
+
}
|
|
1888
|
+
async execute() {
|
|
1889
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
class We {
|
|
1893
|
+
#e;
|
|
1894
|
+
constructor(e) {
|
|
1895
|
+
this.#e = r(e);
|
|
1896
|
+
}
|
|
1897
|
+
ifNotExists() {
|
|
1898
|
+
return new We({
|
|
1899
|
+
...this.#e,
|
|
1900
|
+
node: ve.cloneWith(this.#e.node, { ifNotExists: !0 })
|
|
1901
|
+
});
|
|
1902
|
+
}
|
|
1903
|
+
/**
|
|
1904
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
1905
|
+
* what the provided function returns.
|
|
1906
|
+
*/
|
|
1907
|
+
$call(e) {
|
|
1908
|
+
return e(this);
|
|
1909
|
+
}
|
|
1910
|
+
toOperationNode() {
|
|
1911
|
+
return this.#e.executor.transformQuery(this.#e.node, this.#e.queryId);
|
|
1912
|
+
}
|
|
1913
|
+
compile() {
|
|
1914
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
1915
|
+
}
|
|
1916
|
+
async execute() {
|
|
1917
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
1918
|
+
}
|
|
1919
|
+
}
|
|
1920
|
+
function ut(t) {
|
|
1921
|
+
if (it.includes(t))
|
|
1922
|
+
return t;
|
|
1923
|
+
throw new Error(`invalid OnCommitAction ${t}`);
|
|
1924
|
+
}
|
|
1925
|
+
class l {
|
|
1926
|
+
#e;
|
|
1927
|
+
constructor(e) {
|
|
1928
|
+
this.#e = r(e);
|
|
1929
|
+
}
|
|
1930
|
+
/**
|
|
1931
|
+
* Adds the "temporary" modifier.
|
|
1932
|
+
*
|
|
1933
|
+
* Use this to create a temporary table.
|
|
1934
|
+
*/
|
|
1935
|
+
temporary() {
|
|
1936
|
+
return new l({
|
|
1937
|
+
...this.#e,
|
|
1938
|
+
node: p.cloneWith(this.#e.node, {
|
|
1939
|
+
temporary: !0
|
|
1940
|
+
})
|
|
1941
|
+
});
|
|
1942
|
+
}
|
|
1943
|
+
/**
|
|
1944
|
+
* Adds an "on commit" statement.
|
|
1945
|
+
*
|
|
1946
|
+
* This can be used in conjunction with temporary tables on supported databases
|
|
1947
|
+
* like PostgreSQL.
|
|
1948
|
+
*/
|
|
1949
|
+
onCommit(e) {
|
|
1950
|
+
return new l({
|
|
1951
|
+
...this.#e,
|
|
1952
|
+
node: p.cloneWith(this.#e.node, {
|
|
1953
|
+
onCommit: ut(e)
|
|
1954
|
+
})
|
|
1955
|
+
});
|
|
1956
|
+
}
|
|
1957
|
+
/**
|
|
1958
|
+
* Adds the "if not exists" modifier.
|
|
1959
|
+
*
|
|
1960
|
+
* If the table already exists, no error is thrown if this method has been called.
|
|
1961
|
+
*/
|
|
1962
|
+
ifNotExists() {
|
|
1963
|
+
return new l({
|
|
1964
|
+
...this.#e,
|
|
1965
|
+
node: p.cloneWith(this.#e.node, {
|
|
1966
|
+
ifNotExists: !0
|
|
1967
|
+
})
|
|
1968
|
+
});
|
|
1969
|
+
}
|
|
1970
|
+
/**
|
|
1971
|
+
* Adds a column to the table.
|
|
1972
|
+
*
|
|
1973
|
+
* ### Examples
|
|
1974
|
+
*
|
|
1975
|
+
* ```ts
|
|
1976
|
+
* import { sql } from 'kysely'
|
|
1977
|
+
*
|
|
1978
|
+
* await db.schema
|
|
1979
|
+
* .createTable('person')
|
|
1980
|
+
* .addColumn('id', 'integer', (col) => col.autoIncrement().primaryKey())
|
|
1981
|
+
* .addColumn('first_name', 'varchar(50)', (col) => col.notNull())
|
|
1982
|
+
* .addColumn('last_name', 'varchar(255)')
|
|
1983
|
+
* .addColumn('bank_balance', 'numeric(8, 2)')
|
|
1984
|
+
* // You can specify any data type using the `sql` tag if the types
|
|
1985
|
+
* // don't include it.
|
|
1986
|
+
* .addColumn('data', sql`any_type_here`)
|
|
1987
|
+
* .addColumn('parent_id', 'integer', (col) =>
|
|
1988
|
+
* col.references('person.id').onDelete('cascade')
|
|
1989
|
+
* )
|
|
1990
|
+
* ```
|
|
1991
|
+
*
|
|
1992
|
+
* With this method, it's once again good to remember that Kysely just builds the
|
|
1993
|
+
* query and doesn't provide the same API for all databases. For example, some
|
|
1994
|
+
* databases like older MySQL don't support the `references` statement in the
|
|
1995
|
+
* column definition. Instead foreign key constraints need to be defined in the
|
|
1996
|
+
* `create table` query. See the next example:
|
|
1997
|
+
*
|
|
1998
|
+
* ```ts
|
|
1999
|
+
* await db.schema
|
|
2000
|
+
* .createTable('person')
|
|
2001
|
+
* .addColumn('id', 'integer', (col) => col.primaryKey())
|
|
2002
|
+
* .addColumn('parent_id', 'integer')
|
|
2003
|
+
* .addForeignKeyConstraint(
|
|
2004
|
+
* 'person_parent_id_fk',
|
|
2005
|
+
* ['parent_id'],
|
|
2006
|
+
* 'person',
|
|
2007
|
+
* ['id'],
|
|
2008
|
+
* (cb) => cb.onDelete('cascade')
|
|
2009
|
+
* )
|
|
2010
|
+
* .execute()
|
|
2011
|
+
* ```
|
|
2012
|
+
*
|
|
2013
|
+
* Another good example is that PostgreSQL doesn't support the `auto_increment`
|
|
2014
|
+
* keyword and you need to define an autoincrementing column for example using
|
|
2015
|
+
* `serial`:
|
|
2016
|
+
*
|
|
2017
|
+
* ```ts
|
|
2018
|
+
* await db.schema
|
|
2019
|
+
* .createTable('person')
|
|
2020
|
+
* .addColumn('id', 'serial', (col) => col.primaryKey())
|
|
2021
|
+
* .execute()
|
|
2022
|
+
* ```
|
|
2023
|
+
*/
|
|
2024
|
+
addColumn(e, n, i = w) {
|
|
2025
|
+
const o = i(new a(c.create(e, H(n))));
|
|
2026
|
+
return new l({
|
|
2027
|
+
...this.#e,
|
|
2028
|
+
node: p.cloneWithColumn(this.#e.node, o.toOperationNode())
|
|
2029
|
+
});
|
|
2030
|
+
}
|
|
2031
|
+
/**
|
|
2032
|
+
* Adds a primary key constraint for one or more columns.
|
|
2033
|
+
*
|
|
2034
|
+
* The constraint name can be anything you want, but it must be unique
|
|
2035
|
+
* across the whole database.
|
|
2036
|
+
*
|
|
2037
|
+
* ### Examples
|
|
2038
|
+
*
|
|
2039
|
+
* ```ts
|
|
2040
|
+
* await db.schema
|
|
2041
|
+
* .createTable('person')
|
|
2042
|
+
* .addColumn('first_name', 'varchar(64)')
|
|
2043
|
+
* .addColumn('last_name', 'varchar(64)')
|
|
2044
|
+
* .addPrimaryKeyConstraint('primary_key', ['first_name', 'last_name'])
|
|
2045
|
+
* .execute()
|
|
2046
|
+
* ```
|
|
2047
|
+
*/
|
|
2048
|
+
addPrimaryKeyConstraint(e, n, i = w) {
|
|
2049
|
+
const o = i(new A(U.create(n, e)));
|
|
2050
|
+
return new l({
|
|
2051
|
+
...this.#e,
|
|
2052
|
+
node: p.cloneWithConstraint(this.#e.node, o.toOperationNode())
|
|
2053
|
+
});
|
|
2054
|
+
}
|
|
2055
|
+
/**
|
|
2056
|
+
* Adds a unique constraint for one or more columns.
|
|
2057
|
+
*
|
|
2058
|
+
* The constraint name can be anything you want, but it must be unique
|
|
2059
|
+
* across the whole database.
|
|
2060
|
+
*
|
|
2061
|
+
* ### Examples
|
|
2062
|
+
*
|
|
2063
|
+
* ```ts
|
|
2064
|
+
* await db.schema
|
|
2065
|
+
* .createTable('person')
|
|
2066
|
+
* .addColumn('first_name', 'varchar(64)')
|
|
2067
|
+
* .addColumn('last_name', 'varchar(64)')
|
|
2068
|
+
* .addUniqueConstraint(
|
|
2069
|
+
* 'first_name_last_name_unique',
|
|
2070
|
+
* ['first_name', 'last_name']
|
|
2071
|
+
* )
|
|
2072
|
+
* .execute()
|
|
2073
|
+
* ```
|
|
2074
|
+
*
|
|
2075
|
+
* In dialects such as PostgreSQL you can specify `nulls not distinct` as follows:
|
|
2076
|
+
*
|
|
2077
|
+
* ```ts
|
|
2078
|
+
* await db.schema
|
|
2079
|
+
* .createTable('person')
|
|
2080
|
+
* .addColumn('first_name', 'varchar(64)')
|
|
2081
|
+
* .addColumn('last_name', 'varchar(64)')
|
|
2082
|
+
* .addUniqueConstraint(
|
|
2083
|
+
* 'first_name_last_name_unique',
|
|
2084
|
+
* ['first_name', 'last_name'],
|
|
2085
|
+
* (cb) => cb.nullsNotDistinct()
|
|
2086
|
+
* )
|
|
2087
|
+
* .execute()
|
|
2088
|
+
* ```
|
|
2089
|
+
*/
|
|
2090
|
+
addUniqueConstraint(e, n, i = w) {
|
|
2091
|
+
const o = i(new v(D.create(n, e)));
|
|
2092
|
+
return new l({
|
|
2093
|
+
...this.#e,
|
|
2094
|
+
node: p.cloneWithConstraint(this.#e.node, o.toOperationNode())
|
|
2095
|
+
});
|
|
2096
|
+
}
|
|
2097
|
+
/**
|
|
2098
|
+
* Adds a check constraint.
|
|
2099
|
+
*
|
|
2100
|
+
* The constraint name can be anything you want, but it must be unique
|
|
2101
|
+
* across the whole database.
|
|
2102
|
+
*
|
|
2103
|
+
* ### Examples
|
|
2104
|
+
*
|
|
2105
|
+
* ```ts
|
|
2106
|
+
* import { sql } from 'kysely'
|
|
2107
|
+
*
|
|
2108
|
+
* await db.schema
|
|
2109
|
+
* .createTable('animal')
|
|
2110
|
+
* .addColumn('number_of_legs', 'integer')
|
|
2111
|
+
* .addCheckConstraint('check_legs', sql`number_of_legs < 5`)
|
|
2112
|
+
* .execute()
|
|
2113
|
+
* ```
|
|
2114
|
+
*/
|
|
2115
|
+
addCheckConstraint(e, n, i = w) {
|
|
2116
|
+
const o = i(new Ae(xe.create(n.toOperationNode(), e)));
|
|
2117
|
+
return new l({
|
|
2118
|
+
...this.#e,
|
|
2119
|
+
node: p.cloneWithConstraint(this.#e.node, o.toOperationNode())
|
|
2120
|
+
});
|
|
2121
|
+
}
|
|
2122
|
+
/**
|
|
2123
|
+
* Adds a foreign key constraint.
|
|
2124
|
+
*
|
|
2125
|
+
* The constraint name can be anything you want, but it must be unique
|
|
2126
|
+
* across the whole database.
|
|
2127
|
+
*
|
|
2128
|
+
* ### Examples
|
|
2129
|
+
*
|
|
2130
|
+
* ```ts
|
|
2131
|
+
* await db.schema
|
|
2132
|
+
* .createTable('pet')
|
|
2133
|
+
* .addColumn('owner_id', 'integer')
|
|
2134
|
+
* .addForeignKeyConstraint(
|
|
2135
|
+
* 'owner_id_foreign',
|
|
2136
|
+
* ['owner_id'],
|
|
2137
|
+
* 'person',
|
|
2138
|
+
* ['id'],
|
|
2139
|
+
* )
|
|
2140
|
+
* .execute()
|
|
2141
|
+
* ```
|
|
2142
|
+
*
|
|
2143
|
+
* Add constraint for multiple columns:
|
|
2144
|
+
*
|
|
2145
|
+
* ```ts
|
|
2146
|
+
* await db.schema
|
|
2147
|
+
* .createTable('pet')
|
|
2148
|
+
* .addColumn('owner_id1', 'integer')
|
|
2149
|
+
* .addColumn('owner_id2', 'integer')
|
|
2150
|
+
* .addForeignKeyConstraint(
|
|
2151
|
+
* 'owner_id_foreign',
|
|
2152
|
+
* ['owner_id1', 'owner_id2'],
|
|
2153
|
+
* 'person',
|
|
2154
|
+
* ['id1', 'id2'],
|
|
2155
|
+
* (cb) => cb.onDelete('cascade')
|
|
2156
|
+
* )
|
|
2157
|
+
* .execute()
|
|
2158
|
+
* ```
|
|
2159
|
+
*/
|
|
2160
|
+
addForeignKeyConstraint(e, n, i, o, u = w) {
|
|
2161
|
+
const h = u(new g(I.create(n.map(x.create), E(i), o.map(x.create), e)));
|
|
2162
|
+
return new l({
|
|
2163
|
+
...this.#e,
|
|
2164
|
+
node: p.cloneWithConstraint(this.#e.node, h.toOperationNode())
|
|
2165
|
+
});
|
|
2166
|
+
}
|
|
2167
|
+
/**
|
|
2168
|
+
* This can be used to add any additional SQL to the front of the query __after__ the `create` keyword.
|
|
2169
|
+
*
|
|
2170
|
+
* Also see {@link temporary}.
|
|
2171
|
+
*
|
|
2172
|
+
* ### Examples
|
|
2173
|
+
*
|
|
2174
|
+
* ```ts
|
|
2175
|
+
* import { sql } from 'kysely'
|
|
2176
|
+
*
|
|
2177
|
+
* await db.schema
|
|
2178
|
+
* .createTable('person')
|
|
2179
|
+
* .modifyFront(sql`global temporary`)
|
|
2180
|
+
* .addColumn('id', 'integer', col => col.primaryKey())
|
|
2181
|
+
* .addColumn('first_name', 'varchar(64)', col => col.notNull())
|
|
2182
|
+
* .addColumn('last_name', 'varchar(64)', col => col.notNull())
|
|
2183
|
+
* .execute()
|
|
2184
|
+
* ```
|
|
2185
|
+
*
|
|
2186
|
+
* The generated SQL (Postgres):
|
|
2187
|
+
*
|
|
2188
|
+
* ```sql
|
|
2189
|
+
* create global temporary table "person" (
|
|
2190
|
+
* "id" integer primary key,
|
|
2191
|
+
* "first_name" varchar(64) not null,
|
|
2192
|
+
* "last_name" varchar(64) not null
|
|
2193
|
+
* )
|
|
2194
|
+
* ```
|
|
2195
|
+
*/
|
|
2196
|
+
modifyFront(e) {
|
|
2197
|
+
return new l({
|
|
2198
|
+
...this.#e,
|
|
2199
|
+
node: p.cloneWithFrontModifier(this.#e.node, e.toOperationNode())
|
|
2200
|
+
});
|
|
2201
|
+
}
|
|
2202
|
+
/**
|
|
2203
|
+
* This can be used to add any additional SQL to the end of the query.
|
|
2204
|
+
*
|
|
2205
|
+
* Also see {@link onCommit}.
|
|
2206
|
+
*
|
|
2207
|
+
* ### Examples
|
|
2208
|
+
*
|
|
2209
|
+
* ```ts
|
|
2210
|
+
* import { sql } from 'kysely'
|
|
2211
|
+
*
|
|
2212
|
+
* await db.schema
|
|
2213
|
+
* .createTable('person')
|
|
2214
|
+
* .addColumn('id', 'integer', col => col.primaryKey())
|
|
2215
|
+
* .addColumn('first_name', 'varchar(64)', col => col.notNull())
|
|
2216
|
+
* .addColumn('last_name', 'varchar(64)', col => col.notNull())
|
|
2217
|
+
* .modifyEnd(sql`collate utf8_unicode_ci`)
|
|
2218
|
+
* .execute()
|
|
2219
|
+
* ```
|
|
2220
|
+
*
|
|
2221
|
+
* The generated SQL (MySQL):
|
|
2222
|
+
*
|
|
2223
|
+
* ```sql
|
|
2224
|
+
* create table `person` (
|
|
2225
|
+
* `id` integer primary key,
|
|
2226
|
+
* `first_name` varchar(64) not null,
|
|
2227
|
+
* `last_name` varchar(64) not null
|
|
2228
|
+
* ) collate utf8_unicode_ci
|
|
2229
|
+
* ```
|
|
2230
|
+
*/
|
|
2231
|
+
modifyEnd(e) {
|
|
2232
|
+
return new l({
|
|
2233
|
+
...this.#e,
|
|
2234
|
+
node: p.cloneWithEndModifier(this.#e.node, e.toOperationNode())
|
|
2235
|
+
});
|
|
2236
|
+
}
|
|
2237
|
+
/**
|
|
2238
|
+
* Allows to create table from `select` query.
|
|
2239
|
+
*
|
|
2240
|
+
* ### Examples
|
|
2241
|
+
*
|
|
2242
|
+
* ```ts
|
|
2243
|
+
* await db.schema
|
|
2244
|
+
* .createTable('copy')
|
|
2245
|
+
* .temporary()
|
|
2246
|
+
* .as(db.selectFrom('person').select(['first_name', 'last_name']))
|
|
2247
|
+
* .execute()
|
|
2248
|
+
* ```
|
|
2249
|
+
*
|
|
2250
|
+
* The generated SQL (PostgreSQL):
|
|
2251
|
+
*
|
|
2252
|
+
* ```sql
|
|
2253
|
+
* create temporary table "copy" as
|
|
2254
|
+
* select "first_name", "last_name" from "person"
|
|
2255
|
+
* ```
|
|
2256
|
+
*/
|
|
2257
|
+
as(e) {
|
|
2258
|
+
return new l({
|
|
2259
|
+
...this.#e,
|
|
2260
|
+
node: p.cloneWith(this.#e.node, {
|
|
2261
|
+
selectQuery: Pe(e)
|
|
2262
|
+
})
|
|
2263
|
+
});
|
|
2264
|
+
}
|
|
2265
|
+
/**
|
|
2266
|
+
* Calls the given function passing `this` as the only argument.
|
|
2267
|
+
*
|
|
2268
|
+
* ### Examples
|
|
2269
|
+
*
|
|
2270
|
+
* ```ts
|
|
2271
|
+
* await db.schema
|
|
2272
|
+
* .createTable('test')
|
|
2273
|
+
* .$call((builder) => builder.addColumn('id', 'integer'))
|
|
2274
|
+
* .execute()
|
|
2275
|
+
* ```
|
|
2276
|
+
*
|
|
2277
|
+
* This is useful for creating reusable functions that can be called with a builder.
|
|
2278
|
+
*
|
|
2279
|
+
* ```ts
|
|
2280
|
+
* import { type CreateTableBuilder, sql } from 'kysely'
|
|
2281
|
+
*
|
|
2282
|
+
* const addDefaultColumns = (ctb: CreateTableBuilder<any, any>) => {
|
|
2283
|
+
* return ctb
|
|
2284
|
+
* .addColumn('id', 'integer', (col) => col.notNull())
|
|
2285
|
+
* .addColumn('created_at', 'date', (col) =>
|
|
2286
|
+
* col.notNull().defaultTo(sql`now()`)
|
|
2287
|
+
* )
|
|
2288
|
+
* .addColumn('updated_at', 'date', (col) =>
|
|
2289
|
+
* col.notNull().defaultTo(sql`now()`)
|
|
2290
|
+
* )
|
|
2291
|
+
* }
|
|
2292
|
+
*
|
|
2293
|
+
* await db.schema
|
|
2294
|
+
* .createTable('test')
|
|
2295
|
+
* .$call(addDefaultColumns)
|
|
2296
|
+
* .execute()
|
|
2297
|
+
* ```
|
|
2298
|
+
*/
|
|
2299
|
+
$call(e) {
|
|
2300
|
+
return e(this);
|
|
2301
|
+
}
|
|
2302
|
+
toOperationNode() {
|
|
2303
|
+
return this.#e.executor.transformQuery(this.#e.node, this.#e.queryId);
|
|
2304
|
+
}
|
|
2305
|
+
compile() {
|
|
2306
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
2307
|
+
}
|
|
2308
|
+
async execute() {
|
|
2309
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
2310
|
+
}
|
|
2311
|
+
}
|
|
2312
|
+
class K {
|
|
2313
|
+
#e;
|
|
2314
|
+
constructor(e) {
|
|
2315
|
+
this.#e = r(e);
|
|
2316
|
+
}
|
|
2317
|
+
/**
|
|
2318
|
+
* Specifies the table the index was created for. This is not needed
|
|
2319
|
+
* in all dialects.
|
|
2320
|
+
*/
|
|
2321
|
+
on(e) {
|
|
2322
|
+
return new K({
|
|
2323
|
+
...this.#e,
|
|
2324
|
+
node: Z.cloneWith(this.#e.node, {
|
|
2325
|
+
table: E(e)
|
|
2326
|
+
})
|
|
2327
|
+
});
|
|
2328
|
+
}
|
|
2329
|
+
ifExists() {
|
|
2330
|
+
return new K({
|
|
2331
|
+
...this.#e,
|
|
2332
|
+
node: Z.cloneWith(this.#e.node, {
|
|
2333
|
+
ifExists: !0
|
|
2334
|
+
})
|
|
2335
|
+
});
|
|
2336
|
+
}
|
|
2337
|
+
cascade() {
|
|
2338
|
+
return new K({
|
|
2339
|
+
...this.#e,
|
|
2340
|
+
node: Z.cloneWith(this.#e.node, {
|
|
2341
|
+
cascade: !0
|
|
2342
|
+
})
|
|
2343
|
+
});
|
|
2344
|
+
}
|
|
2345
|
+
/**
|
|
2346
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
2347
|
+
* what the provided function returns.
|
|
2348
|
+
*/
|
|
2349
|
+
$call(e) {
|
|
2350
|
+
return e(this);
|
|
2351
|
+
}
|
|
2352
|
+
toOperationNode() {
|
|
2353
|
+
return this.#e.executor.transformQuery(this.#e.node, this.#e.queryId);
|
|
2354
|
+
}
|
|
2355
|
+
compile() {
|
|
2356
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
2357
|
+
}
|
|
2358
|
+
async execute() {
|
|
2359
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
2360
|
+
}
|
|
2361
|
+
}
|
|
2362
|
+
class he {
|
|
2363
|
+
#e;
|
|
2364
|
+
constructor(e) {
|
|
2365
|
+
this.#e = r(e);
|
|
2366
|
+
}
|
|
2367
|
+
ifExists() {
|
|
2368
|
+
return new he({
|
|
2369
|
+
...this.#e,
|
|
2370
|
+
node: ye.cloneWith(this.#e.node, {
|
|
2371
|
+
ifExists: !0
|
|
2372
|
+
})
|
|
2373
|
+
});
|
|
2374
|
+
}
|
|
2375
|
+
cascade() {
|
|
2376
|
+
return new he({
|
|
2377
|
+
...this.#e,
|
|
2378
|
+
node: ye.cloneWith(this.#e.node, {
|
|
2379
|
+
cascade: !0
|
|
2380
|
+
})
|
|
2381
|
+
});
|
|
2382
|
+
}
|
|
2383
|
+
/**
|
|
2384
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
2385
|
+
* what the provided function returns.
|
|
2386
|
+
*/
|
|
2387
|
+
$call(e) {
|
|
2388
|
+
return e(this);
|
|
2389
|
+
}
|
|
2390
|
+
toOperationNode() {
|
|
2391
|
+
return this.#e.executor.transformQuery(this.#e.node, this.#e.queryId);
|
|
2392
|
+
}
|
|
2393
|
+
compile() {
|
|
2394
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
2395
|
+
}
|
|
2396
|
+
async execute() {
|
|
2397
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
2398
|
+
}
|
|
2399
|
+
}
|
|
2400
|
+
class de {
|
|
2401
|
+
#e;
|
|
2402
|
+
constructor(e) {
|
|
2403
|
+
this.#e = r(e);
|
|
2404
|
+
}
|
|
2405
|
+
ifExists() {
|
|
2406
|
+
return new de({
|
|
2407
|
+
...this.#e,
|
|
2408
|
+
node: fe.cloneWith(this.#e.node, {
|
|
2409
|
+
ifExists: !0
|
|
2410
|
+
})
|
|
2411
|
+
});
|
|
2412
|
+
}
|
|
2413
|
+
cascade() {
|
|
2414
|
+
return new de({
|
|
2415
|
+
...this.#e,
|
|
2416
|
+
node: fe.cloneWith(this.#e.node, {
|
|
2417
|
+
cascade: !0
|
|
2418
|
+
})
|
|
2419
|
+
});
|
|
2420
|
+
}
|
|
2421
|
+
/**
|
|
2422
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
2423
|
+
* what the provided function returns.
|
|
2424
|
+
*/
|
|
2425
|
+
$call(e) {
|
|
2426
|
+
return e(this);
|
|
2427
|
+
}
|
|
2428
|
+
toOperationNode() {
|
|
2429
|
+
return this.#e.executor.transformQuery(this.#e.node, this.#e.queryId);
|
|
2430
|
+
}
|
|
2431
|
+
compile() {
|
|
2432
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
2433
|
+
}
|
|
2434
|
+
async execute() {
|
|
2435
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
2436
|
+
}
|
|
2437
|
+
}
|
|
2438
|
+
const Q = r({
|
|
2439
|
+
is(t) {
|
|
2440
|
+
return t.kind === "CreateViewNode";
|
|
2441
|
+
},
|
|
2442
|
+
create(t) {
|
|
2443
|
+
return r({
|
|
2444
|
+
kind: "CreateViewNode",
|
|
2445
|
+
name: G.create(t)
|
|
2446
|
+
});
|
|
2447
|
+
},
|
|
2448
|
+
cloneWith(t, e) {
|
|
2449
|
+
return r({
|
|
2450
|
+
...t,
|
|
2451
|
+
...e
|
|
2452
|
+
});
|
|
2453
|
+
}
|
|
2454
|
+
});
|
|
2455
|
+
class ht {
|
|
2456
|
+
#e = new $e();
|
|
2457
|
+
transformQuery(e) {
|
|
2458
|
+
return this.#e.transformNode(e.node, e.queryId);
|
|
2459
|
+
}
|
|
2460
|
+
transformResult(e) {
|
|
2461
|
+
return Promise.resolve(e.result);
|
|
2462
|
+
}
|
|
2463
|
+
}
|
|
2464
|
+
class q {
|
|
2465
|
+
#e;
|
|
2466
|
+
constructor(e) {
|
|
2467
|
+
this.#e = r(e);
|
|
2468
|
+
}
|
|
2469
|
+
/**
|
|
2470
|
+
* Adds the "temporary" modifier.
|
|
2471
|
+
*
|
|
2472
|
+
* Use this to create a temporary view.
|
|
2473
|
+
*/
|
|
2474
|
+
temporary() {
|
|
2475
|
+
return new q({
|
|
2476
|
+
...this.#e,
|
|
2477
|
+
node: Q.cloneWith(this.#e.node, {
|
|
2478
|
+
temporary: !0
|
|
2479
|
+
})
|
|
2480
|
+
});
|
|
2481
|
+
}
|
|
2482
|
+
materialized() {
|
|
2483
|
+
return new q({
|
|
2484
|
+
...this.#e,
|
|
2485
|
+
node: Q.cloneWith(this.#e.node, {
|
|
2486
|
+
materialized: !0
|
|
2487
|
+
})
|
|
2488
|
+
});
|
|
2489
|
+
}
|
|
2490
|
+
/**
|
|
2491
|
+
* Only implemented on some dialects like SQLite. On most dialects, use {@link orReplace}.
|
|
2492
|
+
*/
|
|
2493
|
+
ifNotExists() {
|
|
2494
|
+
return new q({
|
|
2495
|
+
...this.#e,
|
|
2496
|
+
node: Q.cloneWith(this.#e.node, {
|
|
2497
|
+
ifNotExists: !0
|
|
2498
|
+
})
|
|
2499
|
+
});
|
|
2500
|
+
}
|
|
2501
|
+
orReplace() {
|
|
2502
|
+
return new q({
|
|
2503
|
+
...this.#e,
|
|
2504
|
+
node: Q.cloneWith(this.#e.node, {
|
|
2505
|
+
orReplace: !0
|
|
2506
|
+
})
|
|
2507
|
+
});
|
|
2508
|
+
}
|
|
2509
|
+
columns(e) {
|
|
2510
|
+
return new q({
|
|
2511
|
+
...this.#e,
|
|
2512
|
+
node: Q.cloneWith(this.#e.node, {
|
|
2513
|
+
columns: e.map(Ye)
|
|
2514
|
+
})
|
|
2515
|
+
});
|
|
2516
|
+
}
|
|
2517
|
+
/**
|
|
2518
|
+
* Sets the select query or a `values` statement that creates the view.
|
|
2519
|
+
*
|
|
2520
|
+
* WARNING!
|
|
2521
|
+
* Some dialects don't support parameterized queries in DDL statements and therefore
|
|
2522
|
+
* the query or raw {@link sql } expression passed here is interpolated into a single
|
|
2523
|
+
* string opening an SQL injection vulnerability. DO NOT pass unchecked user input
|
|
2524
|
+
* into the query or raw expression passed to this method!
|
|
2525
|
+
*/
|
|
2526
|
+
as(e) {
|
|
2527
|
+
const n = e.withPlugin(new ht()).toOperationNode();
|
|
2528
|
+
return new q({
|
|
2529
|
+
...this.#e,
|
|
2530
|
+
node: Q.cloneWith(this.#e.node, {
|
|
2531
|
+
as: n
|
|
2532
|
+
})
|
|
2533
|
+
});
|
|
2534
|
+
}
|
|
2535
|
+
/**
|
|
2536
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
2537
|
+
* what the provided function returns.
|
|
2538
|
+
*/
|
|
2539
|
+
$call(e) {
|
|
2540
|
+
return e(this);
|
|
2541
|
+
}
|
|
2542
|
+
toOperationNode() {
|
|
2543
|
+
return this.#e.executor.transformQuery(this.#e.node, this.#e.queryId);
|
|
2544
|
+
}
|
|
2545
|
+
compile() {
|
|
2546
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
2547
|
+
}
|
|
2548
|
+
async execute() {
|
|
2549
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
2550
|
+
}
|
|
2551
|
+
}
|
|
2552
|
+
const oe = r({
|
|
2553
|
+
is(t) {
|
|
2554
|
+
return t.kind === "DropViewNode";
|
|
2555
|
+
},
|
|
2556
|
+
create(t) {
|
|
2557
|
+
return r({
|
|
2558
|
+
kind: "DropViewNode",
|
|
2559
|
+
name: G.create(t)
|
|
2560
|
+
});
|
|
2561
|
+
},
|
|
2562
|
+
cloneWith(t, e) {
|
|
2563
|
+
return r({
|
|
2564
|
+
...t,
|
|
2565
|
+
...e
|
|
2566
|
+
});
|
|
2567
|
+
}
|
|
2568
|
+
});
|
|
2569
|
+
class B {
|
|
2570
|
+
#e;
|
|
2571
|
+
constructor(e) {
|
|
2572
|
+
this.#e = r(e);
|
|
2573
|
+
}
|
|
2574
|
+
materialized() {
|
|
2575
|
+
return new B({
|
|
2576
|
+
...this.#e,
|
|
2577
|
+
node: oe.cloneWith(this.#e.node, {
|
|
2578
|
+
materialized: !0
|
|
2579
|
+
})
|
|
2580
|
+
});
|
|
2581
|
+
}
|
|
2582
|
+
ifExists() {
|
|
2583
|
+
return new B({
|
|
2584
|
+
...this.#e,
|
|
2585
|
+
node: oe.cloneWith(this.#e.node, {
|
|
2586
|
+
ifExists: !0
|
|
2587
|
+
})
|
|
2588
|
+
});
|
|
2589
|
+
}
|
|
2590
|
+
cascade() {
|
|
2591
|
+
return new B({
|
|
2592
|
+
...this.#e,
|
|
2593
|
+
node: oe.cloneWith(this.#e.node, {
|
|
2594
|
+
cascade: !0
|
|
2595
|
+
})
|
|
2596
|
+
});
|
|
2597
|
+
}
|
|
2598
|
+
/**
|
|
2599
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
2600
|
+
* what the provided function returns.
|
|
2601
|
+
*/
|
|
2602
|
+
$call(e) {
|
|
2603
|
+
return e(this);
|
|
2604
|
+
}
|
|
2605
|
+
toOperationNode() {
|
|
2606
|
+
return this.#e.executor.transformQuery(this.#e.node, this.#e.queryId);
|
|
2607
|
+
}
|
|
2608
|
+
compile() {
|
|
2609
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
2610
|
+
}
|
|
2611
|
+
async execute() {
|
|
2612
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
2613
|
+
}
|
|
2614
|
+
}
|
|
2615
|
+
const Re = r({
|
|
2616
|
+
is(t) {
|
|
2617
|
+
return t.kind === "CreateTypeNode";
|
|
2618
|
+
},
|
|
2619
|
+
create(t) {
|
|
2620
|
+
return r({
|
|
2621
|
+
kind: "CreateTypeNode",
|
|
2622
|
+
name: t
|
|
2623
|
+
});
|
|
2624
|
+
},
|
|
2625
|
+
cloneWithEnum(t, e) {
|
|
2626
|
+
return r({
|
|
2627
|
+
...t,
|
|
2628
|
+
enum: qe.create(e.map(ae.createImmediate))
|
|
2629
|
+
});
|
|
2630
|
+
}
|
|
2631
|
+
});
|
|
2632
|
+
class Ce {
|
|
2633
|
+
#e;
|
|
2634
|
+
constructor(e) {
|
|
2635
|
+
this.#e = r(e);
|
|
2636
|
+
}
|
|
2637
|
+
toOperationNode() {
|
|
2638
|
+
return this.#e.executor.transformQuery(this.#e.node, this.#e.queryId);
|
|
2639
|
+
}
|
|
2640
|
+
/**
|
|
2641
|
+
* Creates an anum type.
|
|
2642
|
+
*
|
|
2643
|
+
* ### Examples
|
|
2644
|
+
*
|
|
2645
|
+
* ```ts
|
|
2646
|
+
* db.schema.createType('species').asEnum(['cat', 'dog', 'frog'])
|
|
2647
|
+
* ```
|
|
2648
|
+
*/
|
|
2649
|
+
asEnum(e) {
|
|
2650
|
+
return new Ce({
|
|
2651
|
+
...this.#e,
|
|
2652
|
+
node: Re.cloneWithEnum(this.#e.node, e)
|
|
2653
|
+
});
|
|
2654
|
+
}
|
|
2655
|
+
/**
|
|
2656
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
2657
|
+
* what the provided function returns.
|
|
2658
|
+
*/
|
|
2659
|
+
$call(e) {
|
|
2660
|
+
return e(this);
|
|
2661
|
+
}
|
|
2662
|
+
compile() {
|
|
2663
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
2664
|
+
}
|
|
2665
|
+
async execute() {
|
|
2666
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
2667
|
+
}
|
|
2668
|
+
}
|
|
2669
|
+
const Le = r({
|
|
2670
|
+
is(t) {
|
|
2671
|
+
return t.kind === "DropTypeNode";
|
|
2672
|
+
},
|
|
2673
|
+
create(t) {
|
|
2674
|
+
return r({
|
|
2675
|
+
kind: "DropTypeNode",
|
|
2676
|
+
name: t
|
|
2677
|
+
});
|
|
2678
|
+
},
|
|
2679
|
+
cloneWith(t, e) {
|
|
2680
|
+
return r({
|
|
2681
|
+
...t,
|
|
2682
|
+
...e
|
|
2683
|
+
});
|
|
2684
|
+
}
|
|
2685
|
+
});
|
|
2686
|
+
class Oe {
|
|
2687
|
+
#e;
|
|
2688
|
+
constructor(e) {
|
|
2689
|
+
this.#e = r(e);
|
|
2690
|
+
}
|
|
2691
|
+
ifExists() {
|
|
2692
|
+
return new Oe({
|
|
2693
|
+
...this.#e,
|
|
2694
|
+
node: Le.cloneWith(this.#e.node, {
|
|
2695
|
+
ifExists: !0
|
|
2696
|
+
})
|
|
2697
|
+
});
|
|
2698
|
+
}
|
|
2699
|
+
/**
|
|
2700
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
2701
|
+
* what the provided function returns.
|
|
2702
|
+
*/
|
|
2703
|
+
$call(e) {
|
|
2704
|
+
return e(this);
|
|
2705
|
+
}
|
|
2706
|
+
toOperationNode() {
|
|
2707
|
+
return this.#e.executor.transformQuery(this.#e.node, this.#e.queryId);
|
|
2708
|
+
}
|
|
2709
|
+
compile() {
|
|
2710
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
2711
|
+
}
|
|
2712
|
+
async execute() {
|
|
2713
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
2714
|
+
}
|
|
2715
|
+
}
|
|
2716
|
+
function Ie(t) {
|
|
2717
|
+
if (t.includes(".")) {
|
|
2718
|
+
const n = t.split(".").map(dt);
|
|
2719
|
+
if (n.length === 2)
|
|
2720
|
+
return G.createWithSchema(n[0], n[1]);
|
|
2721
|
+
throw new Error(`invalid schemable identifier ${t}`);
|
|
2722
|
+
} else
|
|
2723
|
+
return G.create(t);
|
|
2724
|
+
}
|
|
2725
|
+
function dt(t) {
|
|
2726
|
+
return t.trim();
|
|
2727
|
+
}
|
|
2728
|
+
const se = r({
|
|
2729
|
+
is(t) {
|
|
2730
|
+
return t.kind === "RefreshMaterializedViewNode";
|
|
2731
|
+
},
|
|
2732
|
+
create(t) {
|
|
2733
|
+
return r({
|
|
2734
|
+
kind: "RefreshMaterializedViewNode",
|
|
2735
|
+
name: G.create(t)
|
|
2736
|
+
});
|
|
2737
|
+
},
|
|
2738
|
+
cloneWith(t, e) {
|
|
2739
|
+
return r({
|
|
2740
|
+
...t,
|
|
2741
|
+
...e
|
|
2742
|
+
});
|
|
2743
|
+
}
|
|
2744
|
+
});
|
|
2745
|
+
class _ {
|
|
2746
|
+
#e;
|
|
2747
|
+
constructor(e) {
|
|
2748
|
+
this.#e = r(e);
|
|
2749
|
+
}
|
|
2750
|
+
/**
|
|
2751
|
+
* Adds the "concurrently" modifier.
|
|
2752
|
+
*
|
|
2753
|
+
* Use this to refresh the view without locking out concurrent selects on the materialized view.
|
|
2754
|
+
*
|
|
2755
|
+
* WARNING!
|
|
2756
|
+
* This cannot be used with the "with no data" modifier.
|
|
2757
|
+
*/
|
|
2758
|
+
concurrently() {
|
|
2759
|
+
return new _({
|
|
2760
|
+
...this.#e,
|
|
2761
|
+
node: se.cloneWith(this.#e.node, {
|
|
2762
|
+
concurrently: !0,
|
|
2763
|
+
withNoData: !1
|
|
2764
|
+
})
|
|
2765
|
+
});
|
|
2766
|
+
}
|
|
2767
|
+
/**
|
|
2768
|
+
* Adds the "with data" modifier.
|
|
2769
|
+
*
|
|
2770
|
+
* If specified (or defaults) the backing query is executed to provide the new data, and the materialized view is left in a scannable state
|
|
2771
|
+
*/
|
|
2772
|
+
withData() {
|
|
2773
|
+
return new _({
|
|
2774
|
+
...this.#e,
|
|
2775
|
+
node: se.cloneWith(this.#e.node, {
|
|
2776
|
+
withNoData: !1
|
|
2777
|
+
})
|
|
2778
|
+
});
|
|
2779
|
+
}
|
|
2780
|
+
/**
|
|
2781
|
+
* Adds the "with no data" modifier.
|
|
2782
|
+
*
|
|
2783
|
+
* If specified, no new data is generated and the materialized view is left in an unscannable state.
|
|
2784
|
+
*
|
|
2785
|
+
* WARNING!
|
|
2786
|
+
* This cannot be used with the "concurrently" modifier.
|
|
2787
|
+
*/
|
|
2788
|
+
withNoData() {
|
|
2789
|
+
return new _({
|
|
2790
|
+
...this.#e,
|
|
2791
|
+
node: se.cloneWith(this.#e.node, {
|
|
2792
|
+
withNoData: !0,
|
|
2793
|
+
concurrently: !1
|
|
2794
|
+
})
|
|
2795
|
+
});
|
|
2796
|
+
}
|
|
2797
|
+
/**
|
|
2798
|
+
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
2799
|
+
* what the provided function returns.
|
|
2800
|
+
*/
|
|
2801
|
+
$call(e) {
|
|
2802
|
+
return e(this);
|
|
2803
|
+
}
|
|
2804
|
+
toOperationNode() {
|
|
2805
|
+
return this.#e.executor.transformQuery(this.#e.node, this.#e.queryId);
|
|
2806
|
+
}
|
|
2807
|
+
compile() {
|
|
2808
|
+
return this.#e.executor.compileQuery(this.toOperationNode(), this.#e.queryId);
|
|
2809
|
+
}
|
|
2810
|
+
async execute() {
|
|
2811
|
+
await this.#e.executor.executeQuery(this.compile());
|
|
2812
|
+
}
|
|
2813
|
+
}
|
|
2814
|
+
class ee {
|
|
2815
|
+
#e;
|
|
2816
|
+
constructor(e) {
|
|
2817
|
+
this.#e = e;
|
|
2818
|
+
}
|
|
2819
|
+
/**
|
|
2820
|
+
* Create a new table.
|
|
2821
|
+
*
|
|
2822
|
+
* ### Examples
|
|
2823
|
+
*
|
|
2824
|
+
* This example creates a new table with columns `id`, `first_name`,
|
|
2825
|
+
* `last_name` and `gender`:
|
|
2826
|
+
*
|
|
2827
|
+
* ```ts
|
|
2828
|
+
* await db.schema
|
|
2829
|
+
* .createTable('person')
|
|
2830
|
+
* .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
|
|
2831
|
+
* .addColumn('first_name', 'varchar', col => col.notNull())
|
|
2832
|
+
* .addColumn('last_name', 'varchar', col => col.notNull())
|
|
2833
|
+
* .addColumn('gender', 'varchar')
|
|
2834
|
+
* .execute()
|
|
2835
|
+
* ```
|
|
2836
|
+
*
|
|
2837
|
+
* This example creates a table with a foreign key. Not all database
|
|
2838
|
+
* engines support column-level foreign key constraint definitions.
|
|
2839
|
+
* For example if you are using MySQL 5.X see the next example after
|
|
2840
|
+
* this one.
|
|
2841
|
+
*
|
|
2842
|
+
* ```ts
|
|
2843
|
+
* await db.schema
|
|
2844
|
+
* .createTable('pet')
|
|
2845
|
+
* .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
|
|
2846
|
+
* .addColumn('owner_id', 'integer', col => col
|
|
2847
|
+
* .references('person.id')
|
|
2848
|
+
* .onDelete('cascade')
|
|
2849
|
+
* )
|
|
2850
|
+
* .execute()
|
|
2851
|
+
* ```
|
|
2852
|
+
*
|
|
2853
|
+
* This example adds a foreign key constraint for a columns just
|
|
2854
|
+
* like the previous example, but using a table-level statement.
|
|
2855
|
+
* On MySQL 5.X you need to define foreign key constraints like
|
|
2856
|
+
* this:
|
|
2857
|
+
*
|
|
2858
|
+
* ```ts
|
|
2859
|
+
* await db.schema
|
|
2860
|
+
* .createTable('pet')
|
|
2861
|
+
* .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
|
|
2862
|
+
* .addColumn('owner_id', 'integer')
|
|
2863
|
+
* .addForeignKeyConstraint(
|
|
2864
|
+
* 'pet_owner_id_foreign', ['owner_id'], 'person', ['id'],
|
|
2865
|
+
* (constraint) => constraint.onDelete('cascade')
|
|
2866
|
+
* )
|
|
2867
|
+
* .execute()
|
|
2868
|
+
* ```
|
|
2869
|
+
*/
|
|
2870
|
+
createTable(e) {
|
|
2871
|
+
return new l({
|
|
2872
|
+
queryId: d(),
|
|
2873
|
+
executor: this.#e,
|
|
2874
|
+
node: p.create(E(e))
|
|
2875
|
+
});
|
|
2876
|
+
}
|
|
2877
|
+
/**
|
|
2878
|
+
* Drop a table.
|
|
2879
|
+
*
|
|
2880
|
+
* ### Examples
|
|
2881
|
+
*
|
|
2882
|
+
* ```ts
|
|
2883
|
+
* await db.schema
|
|
2884
|
+
* .dropTable('person')
|
|
2885
|
+
* .execute()
|
|
2886
|
+
* ```
|
|
2887
|
+
*/
|
|
2888
|
+
dropTable(e) {
|
|
2889
|
+
return new de({
|
|
2890
|
+
queryId: d(),
|
|
2891
|
+
executor: this.#e,
|
|
2892
|
+
node: fe.create(E(e))
|
|
2893
|
+
});
|
|
2894
|
+
}
|
|
2895
|
+
/**
|
|
2896
|
+
* Create a new index.
|
|
2897
|
+
*
|
|
2898
|
+
* ### Examples
|
|
2899
|
+
*
|
|
2900
|
+
* ```ts
|
|
2901
|
+
* await db.schema
|
|
2902
|
+
* .createIndex('person_full_name_unique_index')
|
|
2903
|
+
* .on('person')
|
|
2904
|
+
* .columns(['first_name', 'last_name'])
|
|
2905
|
+
* .execute()
|
|
2906
|
+
* ```
|
|
2907
|
+
*/
|
|
2908
|
+
createIndex(e) {
|
|
2909
|
+
return new f({
|
|
2910
|
+
queryId: d(),
|
|
2911
|
+
executor: this.#e,
|
|
2912
|
+
node: O.create(e)
|
|
2913
|
+
});
|
|
2914
|
+
}
|
|
2915
|
+
/**
|
|
2916
|
+
* Drop an index.
|
|
2917
|
+
*
|
|
2918
|
+
* ### Examples
|
|
2919
|
+
*
|
|
2920
|
+
* ```ts
|
|
2921
|
+
* await db.schema
|
|
2922
|
+
* .dropIndex('person_full_name_unique_index')
|
|
2923
|
+
* .execute()
|
|
2924
|
+
* ```
|
|
2925
|
+
*/
|
|
2926
|
+
dropIndex(e) {
|
|
2927
|
+
return new K({
|
|
2928
|
+
queryId: d(),
|
|
2929
|
+
executor: this.#e,
|
|
2930
|
+
node: Z.create(e)
|
|
2931
|
+
});
|
|
2932
|
+
}
|
|
2933
|
+
/**
|
|
2934
|
+
* Create a new schema.
|
|
2935
|
+
*
|
|
2936
|
+
* ### Examples
|
|
2937
|
+
*
|
|
2938
|
+
* ```ts
|
|
2939
|
+
* await db.schema
|
|
2940
|
+
* .createSchema('some_schema')
|
|
2941
|
+
* .execute()
|
|
2942
|
+
* ```
|
|
2943
|
+
*/
|
|
2944
|
+
createSchema(e) {
|
|
2945
|
+
return new We({
|
|
2946
|
+
queryId: d(),
|
|
2947
|
+
executor: this.#e,
|
|
2948
|
+
node: ve.create(e)
|
|
2949
|
+
});
|
|
2950
|
+
}
|
|
2951
|
+
/**
|
|
2952
|
+
* Drop a schema.
|
|
2953
|
+
*
|
|
2954
|
+
* ### Examples
|
|
2955
|
+
*
|
|
2956
|
+
* ```ts
|
|
2957
|
+
* await db.schema
|
|
2958
|
+
* .dropSchema('some_schema')
|
|
2959
|
+
* .execute()
|
|
2960
|
+
* ```
|
|
2961
|
+
*/
|
|
2962
|
+
dropSchema(e) {
|
|
2963
|
+
return new he({
|
|
2964
|
+
queryId: d(),
|
|
2965
|
+
executor: this.#e,
|
|
2966
|
+
node: ye.create(e)
|
|
2967
|
+
});
|
|
2968
|
+
}
|
|
2969
|
+
/**
|
|
2970
|
+
* Alter a table.
|
|
2971
|
+
*
|
|
2972
|
+
* ### Examples
|
|
2973
|
+
*
|
|
2974
|
+
* ```ts
|
|
2975
|
+
* await db.schema
|
|
2976
|
+
* .alterTable('person')
|
|
2977
|
+
* .alterColumn('first_name', (ac) => ac.setDataType('text'))
|
|
2978
|
+
* .execute()
|
|
2979
|
+
* ```
|
|
2980
|
+
*/
|
|
2981
|
+
alterTable(e) {
|
|
2982
|
+
return new ct({
|
|
2983
|
+
queryId: d(),
|
|
2984
|
+
executor: this.#e,
|
|
2985
|
+
node: s.create(E(e))
|
|
2986
|
+
});
|
|
2987
|
+
}
|
|
2988
|
+
/**
|
|
2989
|
+
* Create a new view.
|
|
2990
|
+
*
|
|
2991
|
+
* ### Examples
|
|
2992
|
+
*
|
|
2993
|
+
* ```ts
|
|
2994
|
+
* await db.schema
|
|
2995
|
+
* .createView('dogs')
|
|
2996
|
+
* .orReplace()
|
|
2997
|
+
* .as(db.selectFrom('pet').selectAll().where('species', '=', 'dog'))
|
|
2998
|
+
* .execute()
|
|
2999
|
+
* ```
|
|
3000
|
+
*/
|
|
3001
|
+
createView(e) {
|
|
3002
|
+
return new q({
|
|
3003
|
+
queryId: d(),
|
|
3004
|
+
executor: this.#e,
|
|
3005
|
+
node: Q.create(e)
|
|
3006
|
+
});
|
|
3007
|
+
}
|
|
3008
|
+
/**
|
|
3009
|
+
* Refresh a materialized view.
|
|
3010
|
+
*
|
|
3011
|
+
* ### Examples
|
|
3012
|
+
*
|
|
3013
|
+
* ```ts
|
|
3014
|
+
* await db.schema
|
|
3015
|
+
* .refreshMaterializedView('my_view')
|
|
3016
|
+
* .concurrently()
|
|
3017
|
+
* .execute()
|
|
3018
|
+
* ```
|
|
3019
|
+
*/
|
|
3020
|
+
refreshMaterializedView(e) {
|
|
3021
|
+
return new _({
|
|
3022
|
+
queryId: d(),
|
|
3023
|
+
executor: this.#e,
|
|
3024
|
+
node: se.create(e)
|
|
3025
|
+
});
|
|
3026
|
+
}
|
|
3027
|
+
/**
|
|
3028
|
+
* Drop a view.
|
|
3029
|
+
*
|
|
3030
|
+
* ### Examples
|
|
3031
|
+
*
|
|
3032
|
+
* ```ts
|
|
3033
|
+
* await db.schema
|
|
3034
|
+
* .dropView('dogs')
|
|
3035
|
+
* .ifExists()
|
|
3036
|
+
* .execute()
|
|
3037
|
+
* ```
|
|
3038
|
+
*/
|
|
3039
|
+
dropView(e) {
|
|
3040
|
+
return new B({
|
|
3041
|
+
queryId: d(),
|
|
3042
|
+
executor: this.#e,
|
|
3043
|
+
node: oe.create(e)
|
|
3044
|
+
});
|
|
3045
|
+
}
|
|
3046
|
+
/**
|
|
3047
|
+
* Create a new type.
|
|
3048
|
+
*
|
|
3049
|
+
* Only some dialects like PostgreSQL have user-defined types.
|
|
3050
|
+
*
|
|
3051
|
+
* ### Examples
|
|
3052
|
+
*
|
|
3053
|
+
* ```ts
|
|
3054
|
+
* await db.schema
|
|
3055
|
+
* .createType('species')
|
|
3056
|
+
* .asEnum(['dog', 'cat', 'frog'])
|
|
3057
|
+
* .execute()
|
|
3058
|
+
* ```
|
|
3059
|
+
*/
|
|
3060
|
+
createType(e) {
|
|
3061
|
+
return new Ce({
|
|
3062
|
+
queryId: d(),
|
|
3063
|
+
executor: this.#e,
|
|
3064
|
+
node: Re.create(Ie(e))
|
|
3065
|
+
});
|
|
3066
|
+
}
|
|
3067
|
+
/**
|
|
3068
|
+
* Drop a type.
|
|
3069
|
+
*
|
|
3070
|
+
* Only some dialects like PostgreSQL have user-defined types.
|
|
3071
|
+
*
|
|
3072
|
+
* ### Examples
|
|
3073
|
+
*
|
|
3074
|
+
* ```ts
|
|
3075
|
+
* await db.schema
|
|
3076
|
+
* .dropType('species')
|
|
3077
|
+
* .ifExists()
|
|
3078
|
+
* .execute()
|
|
3079
|
+
* ```
|
|
3080
|
+
*/
|
|
3081
|
+
dropType(e) {
|
|
3082
|
+
return new Oe({
|
|
3083
|
+
queryId: d(),
|
|
3084
|
+
executor: this.#e,
|
|
3085
|
+
node: Le.create(Ie(e))
|
|
3086
|
+
});
|
|
3087
|
+
}
|
|
3088
|
+
/**
|
|
3089
|
+
* Returns a copy of this schema module with the given plugin installed.
|
|
3090
|
+
*/
|
|
3091
|
+
withPlugin(e) {
|
|
3092
|
+
return new ee(this.#e.withPlugin(e));
|
|
3093
|
+
}
|
|
3094
|
+
/**
|
|
3095
|
+
* Returns a copy of this schema module without any plugins.
|
|
3096
|
+
*/
|
|
3097
|
+
withoutPlugins() {
|
|
3098
|
+
return new ee(this.#e.withoutPlugins());
|
|
3099
|
+
}
|
|
3100
|
+
/**
|
|
3101
|
+
* See {@link QueryCreator.withSchema}
|
|
3102
|
+
*/
|
|
3103
|
+
withSchema(e) {
|
|
3104
|
+
return new ee(this.#e.withPluginAtFront(new pe(e)));
|
|
3105
|
+
}
|
|
3106
|
+
}
|
|
3107
|
+
class lt {
|
|
3108
|
+
/**
|
|
3109
|
+
* Creates a dynamic reference to a column that is not know at compile time.
|
|
3110
|
+
*
|
|
3111
|
+
* Kysely is built in a way that by default you can't refer to tables or columns
|
|
3112
|
+
* that are not actually visible in the current query and context. This is all
|
|
3113
|
+
* done by TypeScript at compile time, which means that you need to know the
|
|
3114
|
+
* columns and tables at compile time. This is not always the case of course.
|
|
3115
|
+
*
|
|
3116
|
+
* This method is meant to be used in those cases where the column names
|
|
3117
|
+
* come from the user input or are not otherwise known at compile time.
|
|
3118
|
+
*
|
|
3119
|
+
* WARNING! Unlike values, column names are not escaped by the database engine
|
|
3120
|
+
* or Kysely and if you pass in unchecked column names using this method, you
|
|
3121
|
+
* create an SQL injection vulnerability. Always __always__ validate the user
|
|
3122
|
+
* input before passing it to this method.
|
|
3123
|
+
*
|
|
3124
|
+
* There are couple of examples below for some use cases, but you can pass
|
|
3125
|
+
* `ref` to other methods as well. If the types allow you to pass a `ref`
|
|
3126
|
+
* value to some place, it should work.
|
|
3127
|
+
*
|
|
3128
|
+
* ### Examples
|
|
3129
|
+
*
|
|
3130
|
+
* Filter by a column not know at compile time:
|
|
3131
|
+
*
|
|
3132
|
+
* ```ts
|
|
3133
|
+
* async function someQuery(filterColumn: string, filterValue: string) {
|
|
3134
|
+
* const { ref } = db.dynamic
|
|
3135
|
+
*
|
|
3136
|
+
* return await db
|
|
3137
|
+
* .selectFrom('person')
|
|
3138
|
+
* .selectAll()
|
|
3139
|
+
* .where(ref(filterColumn), '=', filterValue)
|
|
3140
|
+
* .execute()
|
|
3141
|
+
* }
|
|
3142
|
+
*
|
|
3143
|
+
* someQuery('first_name', 'Arnold')
|
|
3144
|
+
* someQuery('person.last_name', 'Aniston')
|
|
3145
|
+
* ```
|
|
3146
|
+
*
|
|
3147
|
+
* Order by a column not know at compile time:
|
|
3148
|
+
*
|
|
3149
|
+
* ```ts
|
|
3150
|
+
* async function someQuery(orderBy: string) {
|
|
3151
|
+
* const { ref } = db.dynamic
|
|
3152
|
+
*
|
|
3153
|
+
* return await db
|
|
3154
|
+
* .selectFrom('person')
|
|
3155
|
+
* .select('person.first_name as fn')
|
|
3156
|
+
* .orderBy(ref(orderBy))
|
|
3157
|
+
* .execute()
|
|
3158
|
+
* }
|
|
3159
|
+
*
|
|
3160
|
+
* someQuery('fn')
|
|
3161
|
+
* ```
|
|
3162
|
+
*
|
|
3163
|
+
* In this example we add selections dynamically:
|
|
3164
|
+
*
|
|
3165
|
+
* ```ts
|
|
3166
|
+
* const { ref } = db.dynamic
|
|
3167
|
+
*
|
|
3168
|
+
* // Some column name provided by the user. Value not known at compile time.
|
|
3169
|
+
* const columnFromUserInput: PossibleColumns = 'birthdate';
|
|
3170
|
+
*
|
|
3171
|
+
* // A type that lists all possible values `columnFromUserInput` can have.
|
|
3172
|
+
* // You can use `keyof Person` if any column of an interface is allowed.
|
|
3173
|
+
* type PossibleColumns = 'last_name' | 'first_name' | 'birthdate'
|
|
3174
|
+
*
|
|
3175
|
+
* const [person] = await db.selectFrom('person')
|
|
3176
|
+
* .select([
|
|
3177
|
+
* ref<PossibleColumns>(columnFromUserInput),
|
|
3178
|
+
* 'id'
|
|
3179
|
+
* ])
|
|
3180
|
+
* .execute()
|
|
3181
|
+
*
|
|
3182
|
+
* // The resulting type contains all `PossibleColumns` as optional fields
|
|
3183
|
+
* // because we cannot know which field was actually selected before
|
|
3184
|
+
* // running the code.
|
|
3185
|
+
* const lastName: string | null | undefined = person?.last_name
|
|
3186
|
+
* const firstName: string | undefined = person?.first_name
|
|
3187
|
+
* const birthDate: Date | null | undefined = person?.birthdate
|
|
3188
|
+
*
|
|
3189
|
+
* // The result type also contains the compile time selection `id`.
|
|
3190
|
+
* person?.id
|
|
3191
|
+
* ```
|
|
3192
|
+
*/
|
|
3193
|
+
ref(e) {
|
|
3194
|
+
return new je(e);
|
|
3195
|
+
}
|
|
3196
|
+
/**
|
|
3197
|
+
* Creates a table reference to a table that's not fully known at compile time.
|
|
3198
|
+
*
|
|
3199
|
+
* The type `T` is allowed to be a union of multiple tables.
|
|
3200
|
+
*
|
|
3201
|
+
* <!-- siteExample("select", "Generic find query", 130) -->
|
|
3202
|
+
*
|
|
3203
|
+
* A generic type-safe helper function for finding a row by a column value:
|
|
3204
|
+
*
|
|
3205
|
+
* ```ts
|
|
3206
|
+
* import { SelectType } from 'kysely'
|
|
3207
|
+
* import { Database } from 'type-editor'
|
|
3208
|
+
*
|
|
3209
|
+
* async function getRowByColumn<
|
|
3210
|
+
* T extends keyof Database,
|
|
3211
|
+
* C extends keyof Database[T] & string,
|
|
3212
|
+
* V extends SelectType<Database[T][C]>,
|
|
3213
|
+
* >(t: T, c: C, v: V) {
|
|
3214
|
+
* // We need to use the dynamic module since the table name
|
|
3215
|
+
* // is not known at compile time.
|
|
3216
|
+
* const { table, ref } = db.dynamic
|
|
3217
|
+
*
|
|
3218
|
+
* return await db
|
|
3219
|
+
* .selectFrom(table(t).as('t'))
|
|
3220
|
+
* .selectAll()
|
|
3221
|
+
* .where(ref(c), '=', v)
|
|
3222
|
+
* .orderBy('t.id')
|
|
3223
|
+
* .executeTakeFirstOrThrow()
|
|
3224
|
+
* }
|
|
3225
|
+
*
|
|
3226
|
+
* const person = await getRowByColumn('person', 'first_name', 'Arnold')
|
|
3227
|
+
* ```
|
|
3228
|
+
*/
|
|
3229
|
+
table(e) {
|
|
3230
|
+
return new Xe(e);
|
|
3231
|
+
}
|
|
3232
|
+
}
|
|
3233
|
+
class wt {
|
|
3234
|
+
#e;
|
|
3235
|
+
constructor(e) {
|
|
3236
|
+
this.#e = e;
|
|
3237
|
+
}
|
|
3238
|
+
async provideConnection(e) {
|
|
3239
|
+
const n = await this.#e.acquireConnection();
|
|
3240
|
+
try {
|
|
3241
|
+
return await e(n);
|
|
3242
|
+
} finally {
|
|
3243
|
+
await this.#e.releaseConnection(n);
|
|
3244
|
+
}
|
|
3245
|
+
}
|
|
3246
|
+
}
|
|
3247
|
+
class T extends Ze {
|
|
3248
|
+
#e;
|
|
3249
|
+
#t;
|
|
3250
|
+
#r;
|
|
3251
|
+
constructor(e, n, i, o = []) {
|
|
3252
|
+
super(o), this.#e = e, this.#t = n, this.#r = i;
|
|
3253
|
+
}
|
|
3254
|
+
get adapter() {
|
|
3255
|
+
return this.#t;
|
|
3256
|
+
}
|
|
3257
|
+
compileQuery(e, n) {
|
|
3258
|
+
return this.#e.compileQuery(e, n);
|
|
3259
|
+
}
|
|
3260
|
+
provideConnection(e) {
|
|
3261
|
+
return this.#r.provideConnection(e);
|
|
3262
|
+
}
|
|
3263
|
+
withPlugins(e) {
|
|
3264
|
+
return new T(this.#e, this.#t, this.#r, [...this.plugins, ...e]);
|
|
3265
|
+
}
|
|
3266
|
+
withPlugin(e) {
|
|
3267
|
+
return new T(this.#e, this.#t, this.#r, [...this.plugins, e]);
|
|
3268
|
+
}
|
|
3269
|
+
withPluginAtFront(e) {
|
|
3270
|
+
return new T(this.#e, this.#t, this.#r, [e, ...this.plugins]);
|
|
3271
|
+
}
|
|
3272
|
+
withConnectionProvider(e) {
|
|
3273
|
+
return new T(this.#e, this.#t, e, [...this.plugins]);
|
|
3274
|
+
}
|
|
3275
|
+
withoutPlugins() {
|
|
3276
|
+
return new T(this.#e, this.#t, this.#r, []);
|
|
3277
|
+
}
|
|
3278
|
+
}
|
|
3279
|
+
function me() {
|
|
3280
|
+
return typeof performance < "u" && Ne(performance.now) ? performance.now() : Date.now();
|
|
3281
|
+
}
|
|
3282
|
+
class pt {
|
|
3283
|
+
#e;
|
|
3284
|
+
#t;
|
|
3285
|
+
#r;
|
|
3286
|
+
#i;
|
|
3287
|
+
#n;
|
|
3288
|
+
#o = /* @__PURE__ */ new WeakSet();
|
|
3289
|
+
constructor(e, n) {
|
|
3290
|
+
this.#i = !1, this.#e = e, this.#t = n;
|
|
3291
|
+
}
|
|
3292
|
+
async init() {
|
|
3293
|
+
if (this.#n)
|
|
3294
|
+
throw new Error("driver has already been destroyed");
|
|
3295
|
+
this.#r || (this.#r = this.#e.init().then(() => {
|
|
3296
|
+
this.#i = !0;
|
|
3297
|
+
}).catch((e) => (this.#r = void 0, Promise.reject(e)))), await this.#r;
|
|
3298
|
+
}
|
|
3299
|
+
async acquireConnection() {
|
|
3300
|
+
if (this.#n)
|
|
3301
|
+
throw new Error("driver has already been destroyed");
|
|
3302
|
+
this.#i || await this.init();
|
|
3303
|
+
const e = await this.#e.acquireConnection();
|
|
3304
|
+
return this.#o.has(e) || (this.#u() && this.#h(e), this.#o.add(e)), e;
|
|
3305
|
+
}
|
|
3306
|
+
async releaseConnection(e) {
|
|
3307
|
+
await this.#e.releaseConnection(e);
|
|
3308
|
+
}
|
|
3309
|
+
beginTransaction(e, n) {
|
|
3310
|
+
return this.#e.beginTransaction(e, n);
|
|
3311
|
+
}
|
|
3312
|
+
commitTransaction(e) {
|
|
3313
|
+
return this.#e.commitTransaction(e);
|
|
3314
|
+
}
|
|
3315
|
+
rollbackTransaction(e) {
|
|
3316
|
+
return this.#e.rollbackTransaction(e);
|
|
3317
|
+
}
|
|
3318
|
+
savepoint(e, n, i) {
|
|
3319
|
+
if (this.#e.savepoint)
|
|
3320
|
+
return this.#e.savepoint(e, n, i);
|
|
3321
|
+
throw new Error("The `savepoint` method is not supported by this driver");
|
|
3322
|
+
}
|
|
3323
|
+
rollbackToSavepoint(e, n, i) {
|
|
3324
|
+
if (this.#e.rollbackToSavepoint)
|
|
3325
|
+
return this.#e.rollbackToSavepoint(e, n, i);
|
|
3326
|
+
throw new Error("The `rollbackToSavepoint` method is not supported by this driver");
|
|
3327
|
+
}
|
|
3328
|
+
releaseSavepoint(e, n, i) {
|
|
3329
|
+
if (this.#e.releaseSavepoint)
|
|
3330
|
+
return this.#e.releaseSavepoint(e, n, i);
|
|
3331
|
+
throw new Error("The `releaseSavepoint` method is not supported by this driver");
|
|
3332
|
+
}
|
|
3333
|
+
async destroy() {
|
|
3334
|
+
this.#r && (await this.#r, this.#n || (this.#n = this.#e.destroy().catch((e) => (this.#n = void 0, Promise.reject(e)))), await this.#n);
|
|
3335
|
+
}
|
|
3336
|
+
#u() {
|
|
3337
|
+
return this.#t.isLevelEnabled("query") || this.#t.isLevelEnabled("error");
|
|
3338
|
+
}
|
|
3339
|
+
// This method monkey patches the database connection's executeQuery method
|
|
3340
|
+
// by adding logging code around it. Monkey patching is not pretty, but it's
|
|
3341
|
+
// the best option in this case.
|
|
3342
|
+
#h(e) {
|
|
3343
|
+
const n = e.executeQuery, i = e.streamQuery, o = this;
|
|
3344
|
+
e.executeQuery = async (u) => {
|
|
3345
|
+
let h;
|
|
3346
|
+
const C = me();
|
|
3347
|
+
try {
|
|
3348
|
+
return await n.call(e, u);
|
|
3349
|
+
} catch (W) {
|
|
3350
|
+
throw h = W, await o.#s(W, u, C), W;
|
|
3351
|
+
} finally {
|
|
3352
|
+
h || await o.#a(u, C);
|
|
3353
|
+
}
|
|
3354
|
+
}, e.streamQuery = async function* (u, h) {
|
|
3355
|
+
let C;
|
|
3356
|
+
const W = me();
|
|
3357
|
+
try {
|
|
3358
|
+
for await (const y of i.call(e, u, h))
|
|
3359
|
+
yield y;
|
|
3360
|
+
} catch (y) {
|
|
3361
|
+
throw C = y, await o.#s(y, u, W), y;
|
|
3362
|
+
} finally {
|
|
3363
|
+
C || await o.#a(u, W, !0);
|
|
3364
|
+
}
|
|
3365
|
+
};
|
|
3366
|
+
}
|
|
3367
|
+
async #s(e, n, i) {
|
|
3368
|
+
await this.#t.error(() => ({
|
|
3369
|
+
level: "error",
|
|
3370
|
+
error: e,
|
|
3371
|
+
query: n,
|
|
3372
|
+
queryDurationMillis: this.#c(i)
|
|
3373
|
+
}));
|
|
3374
|
+
}
|
|
3375
|
+
async #a(e, n, i = !1) {
|
|
3376
|
+
await this.#t.query(() => ({
|
|
3377
|
+
level: "query",
|
|
3378
|
+
isStream: i,
|
|
3379
|
+
query: e,
|
|
3380
|
+
queryDurationMillis: this.#c(n)
|
|
3381
|
+
}));
|
|
3382
|
+
}
|
|
3383
|
+
#c(e) {
|
|
3384
|
+
return me() - e;
|
|
3385
|
+
}
|
|
3386
|
+
}
|
|
3387
|
+
const mt = () => {
|
|
3388
|
+
};
|
|
3389
|
+
class be {
|
|
3390
|
+
#e;
|
|
3391
|
+
#t;
|
|
3392
|
+
constructor(e) {
|
|
3393
|
+
this.#e = e;
|
|
3394
|
+
}
|
|
3395
|
+
async provideConnection(e) {
|
|
3396
|
+
for (; this.#t; )
|
|
3397
|
+
await this.#t.catch(mt);
|
|
3398
|
+
return this.#t = this.#r(e).finally(() => {
|
|
3399
|
+
this.#t = void 0;
|
|
3400
|
+
}), this.#t;
|
|
3401
|
+
}
|
|
3402
|
+
// Run the runner in an async function to make sure it doesn't
|
|
3403
|
+
// throw synchronous errors.
|
|
3404
|
+
async #r(e) {
|
|
3405
|
+
return await e(this.#e);
|
|
3406
|
+
}
|
|
3407
|
+
}
|
|
3408
|
+
const yt = ["read only", "read write"], ft = [
|
|
3409
|
+
"read uncommitted",
|
|
3410
|
+
"read committed",
|
|
3411
|
+
"repeatable read",
|
|
3412
|
+
"serializable",
|
|
3413
|
+
"snapshot"
|
|
3414
|
+
];
|
|
3415
|
+
function Ve(t) {
|
|
3416
|
+
if (t.accessMode && !yt.includes(t.accessMode))
|
|
3417
|
+
throw new Error(`invalid transaction access mode ${t.accessMode}`);
|
|
3418
|
+
if (t.isolationLevel && !ft.includes(t.isolationLevel))
|
|
3419
|
+
throw new Error(`invalid transaction isolation level ${t.isolationLevel}`);
|
|
3420
|
+
}
|
|
3421
|
+
const Nt = ["query", "error"];
|
|
3422
|
+
r(Nt);
|
|
3423
|
+
class xt {
|
|
3424
|
+
#e;
|
|
3425
|
+
#t;
|
|
3426
|
+
constructor(e) {
|
|
3427
|
+
Ne(e) ? (this.#t = e, this.#e = r({
|
|
3428
|
+
query: !0,
|
|
3429
|
+
error: !0
|
|
3430
|
+
})) : (this.#t = Wt, this.#e = r({
|
|
3431
|
+
query: e.includes("query"),
|
|
3432
|
+
error: e.includes("error")
|
|
3433
|
+
}));
|
|
3434
|
+
}
|
|
3435
|
+
isLevelEnabled(e) {
|
|
3436
|
+
return this.#e[e];
|
|
3437
|
+
}
|
|
3438
|
+
async query(e) {
|
|
3439
|
+
this.#e.query && await this.#t(e());
|
|
3440
|
+
}
|
|
3441
|
+
async error(e) {
|
|
3442
|
+
this.#e.error && await this.#t(e());
|
|
3443
|
+
}
|
|
3444
|
+
}
|
|
3445
|
+
function Wt(t) {
|
|
3446
|
+
if (t.level === "query") {
|
|
3447
|
+
const e = `kysely:query:${t.isStream ? "stream:" : ""}`;
|
|
3448
|
+
console.log(`${e} ${t.query.sql}`), console.log(`${e} duration: ${t.queryDurationMillis.toFixed(1)}ms`);
|
|
3449
|
+
} else t.level === "error" && (t.error instanceof Error ? console.error(`kysely:error: ${t.error.stack ?? t.error.message}`) : console.error(`kysely:error: ${JSON.stringify({
|
|
3450
|
+
error: t.error,
|
|
3451
|
+
query: t.query.sql,
|
|
3452
|
+
queryDurationMillis: t.queryDurationMillis
|
|
3453
|
+
})}`));
|
|
3454
|
+
}
|
|
3455
|
+
function Ct(t) {
|
|
3456
|
+
return V(t) && Ne(t.compile);
|
|
3457
|
+
}
|
|
3458
|
+
Symbol.asyncDispose ??= /* @__PURE__ */ Symbol("Symbol.asyncDispose");
|
|
3459
|
+
class $ extends Ke {
|
|
3460
|
+
#e;
|
|
3461
|
+
constructor(e) {
|
|
3462
|
+
let n, i;
|
|
3463
|
+
if (Ot(e))
|
|
3464
|
+
n = { executor: e.executor }, i = { ...e };
|
|
3465
|
+
else {
|
|
3466
|
+
const o = e.dialect, u = o.createDriver(), h = o.createQueryCompiler(), C = o.createAdapter(), W = new xt(e.log ?? []), y = new pt(u, W), J = new wt(y), ge = new T(h, C, J, e.plugins ?? []);
|
|
3467
|
+
n = { executor: ge }, i = {
|
|
3468
|
+
config: e,
|
|
3469
|
+
executor: ge,
|
|
3470
|
+
dialect: o,
|
|
3471
|
+
driver: y
|
|
3472
|
+
};
|
|
3473
|
+
}
|
|
3474
|
+
super(n), this.#e = r(i);
|
|
3475
|
+
}
|
|
3476
|
+
/**
|
|
3477
|
+
* Returns the {@link SchemaModule} module for building database schema.
|
|
3478
|
+
*/
|
|
3479
|
+
get schema() {
|
|
3480
|
+
return new ee(this.#e.executor);
|
|
3481
|
+
}
|
|
3482
|
+
/**
|
|
3483
|
+
* Returns a the {@link DynamicModule} module.
|
|
3484
|
+
*
|
|
3485
|
+
* The {@link DynamicModule} module can be used to bypass strict typing and
|
|
3486
|
+
* passing in dynamic values for the queries.
|
|
3487
|
+
*/
|
|
3488
|
+
get dynamic() {
|
|
3489
|
+
return new lt();
|
|
3490
|
+
}
|
|
3491
|
+
/**
|
|
3492
|
+
* Returns a {@link DatabaseIntrospector | database introspector}.
|
|
3493
|
+
*/
|
|
3494
|
+
get introspection() {
|
|
3495
|
+
return this.#e.dialect.createIntrospector(this.withoutPlugins());
|
|
3496
|
+
}
|
|
3497
|
+
case(e) {
|
|
3498
|
+
return new Be({
|
|
3499
|
+
node: _e.create(et(e) ? void 0 : Pe(e))
|
|
3500
|
+
});
|
|
3501
|
+
}
|
|
3502
|
+
/**
|
|
3503
|
+
* Returns a {@link FunctionModule} that can be used to write somewhat type-safe function
|
|
3504
|
+
* calls.
|
|
3505
|
+
*
|
|
3506
|
+
* ```ts
|
|
3507
|
+
* const { count } = db.fn
|
|
3508
|
+
*
|
|
3509
|
+
* await db.selectFrom('person')
|
|
3510
|
+
* .innerJoin('pet', 'pet.owner_id', 'person.id')
|
|
3511
|
+
* .select([
|
|
3512
|
+
* 'id',
|
|
3513
|
+
* count('pet.id').as('person_count'),
|
|
3514
|
+
* ])
|
|
3515
|
+
* .groupBy('person.id')
|
|
3516
|
+
* .having(count('pet.id'), '>', 10)
|
|
3517
|
+
* .execute()
|
|
3518
|
+
* ```
|
|
3519
|
+
*
|
|
3520
|
+
* The generated SQL (PostgreSQL):
|
|
3521
|
+
*
|
|
3522
|
+
* ```sql
|
|
3523
|
+
* select "person"."id", count("pet"."id") as "person_count"
|
|
3524
|
+
* from "person"
|
|
3525
|
+
* inner join "pet" on "pet"."owner_id" = "person"."id"
|
|
3526
|
+
* group by "person"."id"
|
|
3527
|
+
* having count("pet"."id") > $1
|
|
3528
|
+
* ```
|
|
3529
|
+
*
|
|
3530
|
+
* Why "somewhat" type-safe? Because the function calls are not bound to the
|
|
3531
|
+
* current query context. They allow you to reference columns and tables that
|
|
3532
|
+
* are not in the current query. E.g. remove the `innerJoin` from the previous
|
|
3533
|
+
* query and TypeScript won't even complain.
|
|
3534
|
+
*
|
|
3535
|
+
* If you want to make the function calls fully type-safe, you can use the
|
|
3536
|
+
* {@link ExpressionBuilder.fn} getter for a query context-aware, stricter {@link FunctionModule}.
|
|
3537
|
+
*
|
|
3538
|
+
* ```ts
|
|
3539
|
+
* await db.selectFrom('person')
|
|
3540
|
+
* .innerJoin('pet', 'pet.owner_id', 'person.id')
|
|
3541
|
+
* .select((eb) => [
|
|
3542
|
+
* 'person.id',
|
|
3543
|
+
* eb.fn.count('pet.id').as('pet_count')
|
|
3544
|
+
* ])
|
|
3545
|
+
* .groupBy('person.id')
|
|
3546
|
+
* .having((eb) => eb.fn.count('pet.id'), '>', 10)
|
|
3547
|
+
* .execute()
|
|
3548
|
+
* ```
|
|
3549
|
+
*/
|
|
3550
|
+
get fn() {
|
|
3551
|
+
return tt();
|
|
3552
|
+
}
|
|
3553
|
+
/**
|
|
3554
|
+
* Creates a {@link TransactionBuilder} that can be used to run queries inside a transaction.
|
|
3555
|
+
*
|
|
3556
|
+
* The returned {@link TransactionBuilder} can be used to configure the transaction. The
|
|
3557
|
+
* {@link TransactionBuilder.execute} method can then be called to run the transaction.
|
|
3558
|
+
* {@link TransactionBuilder.execute} takes a function that is run inside the
|
|
3559
|
+
* transaction. If the function throws an exception,
|
|
3560
|
+
* 1. the exception is caught,
|
|
3561
|
+
* 2. the transaction is rolled back, and
|
|
3562
|
+
* 3. the exception is thrown again.
|
|
3563
|
+
* Otherwise the transaction is committed.
|
|
3564
|
+
*
|
|
3565
|
+
* The callback function passed to the {@link TransactionBuilder.execute | execute}
|
|
3566
|
+
* method gets the transaction object as its only argument. The transaction is
|
|
3567
|
+
* of type {@link Transaction} which inherits {@link Kysely}. Any query
|
|
3568
|
+
* started through the transaction object is executed inside the transaction.
|
|
3569
|
+
*
|
|
3570
|
+
* To run a controlled transaction, allowing you to commit and rollback manually,
|
|
3571
|
+
* use {@link startTransaction} instead.
|
|
3572
|
+
*
|
|
3573
|
+
* ### Examples
|
|
3574
|
+
*
|
|
3575
|
+
* <!-- siteExample("transactions", "Simple transaction", 10) -->
|
|
3576
|
+
*
|
|
3577
|
+
* This example inserts two rows in a transaction. If an exception is thrown inside
|
|
3578
|
+
* the callback passed to the `execute` method,
|
|
3579
|
+
* 1. the exception is caught,
|
|
3580
|
+
* 2. the transaction is rolled back, and
|
|
3581
|
+
* 3. the exception is thrown again.
|
|
3582
|
+
* Otherwise the transaction is committed.
|
|
3583
|
+
*
|
|
3584
|
+
* ```ts
|
|
3585
|
+
* const catto = await db.transaction().execute(async (trx) => {
|
|
3586
|
+
* const jennifer = await trx.insertInto('person')
|
|
3587
|
+
* .values({
|
|
3588
|
+
* first_name: 'Jennifer',
|
|
3589
|
+
* last_name: 'Aniston',
|
|
3590
|
+
* age: 40,
|
|
3591
|
+
* })
|
|
3592
|
+
* .returning('id')
|
|
3593
|
+
* .executeTakeFirstOrThrow()
|
|
3594
|
+
*
|
|
3595
|
+
* return await trx.insertInto('pet')
|
|
3596
|
+
* .values({
|
|
3597
|
+
* owner_id: jennifer.id,
|
|
3598
|
+
* name: 'Catto',
|
|
3599
|
+
* species: 'cat',
|
|
3600
|
+
* is_favorite: false,
|
|
3601
|
+
* })
|
|
3602
|
+
* .returningAll()
|
|
3603
|
+
* .executeTakeFirst()
|
|
3604
|
+
* })
|
|
3605
|
+
* ```
|
|
3606
|
+
*
|
|
3607
|
+
* Setting the isolation level:
|
|
3608
|
+
*
|
|
3609
|
+
* ```ts
|
|
3610
|
+
* import type { Kysely } from 'kysely'
|
|
3611
|
+
*
|
|
3612
|
+
* await db
|
|
3613
|
+
* .transaction()
|
|
3614
|
+
* .setIsolationLevel('serializable')
|
|
3615
|
+
* .execute(async (trx) => {
|
|
3616
|
+
* await doStuff(trx)
|
|
3617
|
+
* })
|
|
3618
|
+
*
|
|
3619
|
+
* async function doStuff(kysely: typeof db) {
|
|
3620
|
+
* // ...
|
|
3621
|
+
* }
|
|
3622
|
+
* ```
|
|
3623
|
+
*/
|
|
3624
|
+
transaction() {
|
|
3625
|
+
return new le({ ...this.#e });
|
|
3626
|
+
}
|
|
3627
|
+
/**
|
|
3628
|
+
* Creates a {@link ControlledTransactionBuilder} that can be used to run queries inside a controlled transaction.
|
|
3629
|
+
*
|
|
3630
|
+
* The returned {@link ControlledTransactionBuilder} can be used to configure the transaction.
|
|
3631
|
+
* The {@link ControlledTransactionBuilder.execute} method can then be called
|
|
3632
|
+
* to start the transaction and return a {@link ControlledTransaction}.
|
|
3633
|
+
*
|
|
3634
|
+
* A {@link ControlledTransaction} allows you to commit and rollback manually,
|
|
3635
|
+
* execute savepoint commands. It extends {@link Transaction} which extends {@link Kysely},
|
|
3636
|
+
* so you can run queries inside the transaction. Once the transaction is committed,
|
|
3637
|
+
* or rolled back, it can't be used anymore - all queries will throw an error.
|
|
3638
|
+
* This is to prevent accidentally running queries outside the transaction - where
|
|
3639
|
+
* atomicity is not guaranteed anymore.
|
|
3640
|
+
*
|
|
3641
|
+
* ### Examples
|
|
3642
|
+
*
|
|
3643
|
+
* <!-- siteExample("transactions", "Controlled transaction", 11) -->
|
|
3644
|
+
*
|
|
3645
|
+
* A controlled transaction allows you to commit and rollback manually, execute
|
|
3646
|
+
* savepoint commands, and queries in general.
|
|
3647
|
+
*
|
|
3648
|
+
* In this example we start a transaction, use it to insert two rows and then commit
|
|
3649
|
+
* the transaction. If an error is thrown, we catch it and rollback the transaction.
|
|
3650
|
+
*
|
|
3651
|
+
* ```ts
|
|
3652
|
+
* const trx = await db.startTransaction().execute()
|
|
3653
|
+
*
|
|
3654
|
+
* try {
|
|
3655
|
+
* const jennifer = await trx.insertInto('person')
|
|
3656
|
+
* .values({
|
|
3657
|
+
* first_name: 'Jennifer',
|
|
3658
|
+
* last_name: 'Aniston',
|
|
3659
|
+
* age: 40,
|
|
3660
|
+
* })
|
|
3661
|
+
* .returning('id')
|
|
3662
|
+
* .executeTakeFirstOrThrow()
|
|
3663
|
+
*
|
|
3664
|
+
* const catto = await trx.insertInto('pet')
|
|
3665
|
+
* .values({
|
|
3666
|
+
* owner_id: jennifer.id,
|
|
3667
|
+
* name: 'Catto',
|
|
3668
|
+
* species: 'cat',
|
|
3669
|
+
* is_favorite: false,
|
|
3670
|
+
* })
|
|
3671
|
+
* .returningAll()
|
|
3672
|
+
* .executeTakeFirstOrThrow()
|
|
3673
|
+
*
|
|
3674
|
+
* await trx.commit().execute()
|
|
3675
|
+
*
|
|
3676
|
+
* // ...
|
|
3677
|
+
* } catch (error) {
|
|
3678
|
+
* await trx.rollback().execute()
|
|
3679
|
+
* }
|
|
3680
|
+
* ```
|
|
3681
|
+
*
|
|
3682
|
+
* <!-- siteExample("transactions", "Controlled transaction /w savepoints", 12) -->
|
|
3683
|
+
*
|
|
3684
|
+
* A controlled transaction allows you to commit and rollback manually, execute
|
|
3685
|
+
* savepoint commands, and queries in general.
|
|
3686
|
+
*
|
|
3687
|
+
* In this example we start a transaction, insert a person, create a savepoint,
|
|
3688
|
+
* try inserting a toy and a pet, and if an error is thrown, we rollback to the
|
|
3689
|
+
* savepoint. Eventually we release the savepoint, insert an audit record and
|
|
3690
|
+
* commit the transaction. If an error is thrown, we catch it and rollback the
|
|
3691
|
+
* transaction.
|
|
3692
|
+
*
|
|
3693
|
+
* ```ts
|
|
3694
|
+
* const trx = await db.startTransaction().execute()
|
|
3695
|
+
*
|
|
3696
|
+
* try {
|
|
3697
|
+
* const jennifer = await trx
|
|
3698
|
+
* .insertInto('person')
|
|
3699
|
+
* .values({
|
|
3700
|
+
* first_name: 'Jennifer',
|
|
3701
|
+
* last_name: 'Aniston',
|
|
3702
|
+
* age: 40,
|
|
3703
|
+
* })
|
|
3704
|
+
* .returning('id')
|
|
3705
|
+
* .executeTakeFirstOrThrow()
|
|
3706
|
+
*
|
|
3707
|
+
* const trxAfterJennifer = await trx.savepoint('after_jennifer').execute()
|
|
3708
|
+
*
|
|
3709
|
+
* try {
|
|
3710
|
+
* const catto = await trxAfterJennifer
|
|
3711
|
+
* .insertInto('pet')
|
|
3712
|
+
* .values({
|
|
3713
|
+
* owner_id: jennifer.id,
|
|
3714
|
+
* name: 'Catto',
|
|
3715
|
+
* species: 'cat',
|
|
3716
|
+
* })
|
|
3717
|
+
* .returning('id')
|
|
3718
|
+
* .executeTakeFirstOrThrow()
|
|
3719
|
+
*
|
|
3720
|
+
* await trxAfterJennifer
|
|
3721
|
+
* .insertInto('toy')
|
|
3722
|
+
* .values({ name: 'Bone', price: 1.99, pet_id: catto.id })
|
|
3723
|
+
* .execute()
|
|
3724
|
+
* } catch (error) {
|
|
3725
|
+
* await trxAfterJennifer.rollbackToSavepoint('after_jennifer').execute()
|
|
3726
|
+
* }
|
|
3727
|
+
*
|
|
3728
|
+
* await trxAfterJennifer.releaseSavepoint('after_jennifer').execute()
|
|
3729
|
+
*
|
|
3730
|
+
* await trx.insertInto('audit').values({ action: 'added Jennifer' }).execute()
|
|
3731
|
+
*
|
|
3732
|
+
* await trx.commit().execute()
|
|
3733
|
+
* } catch (error) {
|
|
3734
|
+
* await trx.rollback().execute()
|
|
3735
|
+
* }
|
|
3736
|
+
* ```
|
|
3737
|
+
*/
|
|
3738
|
+
startTransaction() {
|
|
3739
|
+
return new we({ ...this.#e });
|
|
3740
|
+
}
|
|
3741
|
+
/**
|
|
3742
|
+
* Provides a kysely instance bound to a single database connection.
|
|
3743
|
+
*
|
|
3744
|
+
* ### Examples
|
|
3745
|
+
*
|
|
3746
|
+
* ```ts
|
|
3747
|
+
* await db
|
|
3748
|
+
* .connection()
|
|
3749
|
+
* .execute(async (db) => {
|
|
3750
|
+
* // `db` is an instance of `Kysely` that's bound to a single
|
|
3751
|
+
* // database connection. All queries executed through `db` use
|
|
3752
|
+
* // the same connection.
|
|
3753
|
+
* await doStuff(db)
|
|
3754
|
+
* })
|
|
3755
|
+
*
|
|
3756
|
+
* async function doStuff(kysely: typeof db) {
|
|
3757
|
+
* // ...
|
|
3758
|
+
* }
|
|
3759
|
+
* ```
|
|
3760
|
+
*/
|
|
3761
|
+
connection() {
|
|
3762
|
+
return new bt({ ...this.#e });
|
|
3763
|
+
}
|
|
3764
|
+
/**
|
|
3765
|
+
* Returns a copy of this Kysely instance with the given plugin installed.
|
|
3766
|
+
*/
|
|
3767
|
+
withPlugin(e) {
|
|
3768
|
+
return new $({
|
|
3769
|
+
...this.#e,
|
|
3770
|
+
executor: this.#e.executor.withPlugin(e)
|
|
3771
|
+
});
|
|
3772
|
+
}
|
|
3773
|
+
/**
|
|
3774
|
+
* Returns a copy of this Kysely instance without any plugins.
|
|
3775
|
+
*/
|
|
3776
|
+
withoutPlugins() {
|
|
3777
|
+
return new $({
|
|
3778
|
+
...this.#e,
|
|
3779
|
+
executor: this.#e.executor.withoutPlugins()
|
|
3780
|
+
});
|
|
3781
|
+
}
|
|
3782
|
+
/**
|
|
3783
|
+
* @override
|
|
3784
|
+
*/
|
|
3785
|
+
withSchema(e) {
|
|
3786
|
+
return new $({
|
|
3787
|
+
...this.#e,
|
|
3788
|
+
executor: this.#e.executor.withPluginAtFront(new pe(e))
|
|
3789
|
+
});
|
|
3790
|
+
}
|
|
3791
|
+
/**
|
|
3792
|
+
* Returns a copy of this Kysely instance with tables added to its
|
|
3793
|
+
* database type.
|
|
3794
|
+
*
|
|
3795
|
+
* This method only modifies the types and doesn't affect any of the
|
|
3796
|
+
* executed queries in any way.
|
|
3797
|
+
*
|
|
3798
|
+
* ### Examples
|
|
3799
|
+
*
|
|
3800
|
+
* The following example adds and uses a temporary table:
|
|
3801
|
+
*
|
|
3802
|
+
* ```ts
|
|
3803
|
+
* await db.schema
|
|
3804
|
+
* .createTable('temp_table')
|
|
3805
|
+
* .temporary()
|
|
3806
|
+
* .addColumn('some_column', 'integer')
|
|
3807
|
+
* .execute()
|
|
3808
|
+
*
|
|
3809
|
+
* const tempDb = db.withTables<{
|
|
3810
|
+
* temp_table: {
|
|
3811
|
+
* some_column: number
|
|
3812
|
+
* }
|
|
3813
|
+
* }>()
|
|
3814
|
+
*
|
|
3815
|
+
* await tempDb
|
|
3816
|
+
* .insertInto('temp_table')
|
|
3817
|
+
* .values({ some_column: 100 })
|
|
3818
|
+
* .execute()
|
|
3819
|
+
* ```
|
|
3820
|
+
*/
|
|
3821
|
+
withTables() {
|
|
3822
|
+
return new $({ ...this.#e });
|
|
3823
|
+
}
|
|
3824
|
+
/**
|
|
3825
|
+
* Releases all resources and disconnects from the database.
|
|
3826
|
+
*
|
|
3827
|
+
* You need to call this when you are done using the `Kysely` instance.
|
|
3828
|
+
*/
|
|
3829
|
+
async destroy() {
|
|
3830
|
+
await this.#e.driver.destroy();
|
|
3831
|
+
}
|
|
3832
|
+
/**
|
|
3833
|
+
* Returns true if this `Kysely` instance is a transaction.
|
|
3834
|
+
*
|
|
3835
|
+
* You can also use `db instanceof Transaction`.
|
|
3836
|
+
*/
|
|
3837
|
+
get isTransaction() {
|
|
3838
|
+
return !1;
|
|
3839
|
+
}
|
|
3840
|
+
/**
|
|
3841
|
+
* @internal
|
|
3842
|
+
* @private
|
|
3843
|
+
*/
|
|
3844
|
+
getExecutor() {
|
|
3845
|
+
return this.#e.executor;
|
|
3846
|
+
}
|
|
3847
|
+
/**
|
|
3848
|
+
* Executes a given compiled query or query builder.
|
|
3849
|
+
*
|
|
3850
|
+
* See {@link https://github.com/kysely-org/kysely/blob/master/site/docs/recipes/0004-splitting-query-building-and-execution.md#execute-compiled-queries splitting build, compile and execute code recipe} for more information.
|
|
3851
|
+
*/
|
|
3852
|
+
executeQuery(e, n) {
|
|
3853
|
+
n !== void 0 && rt("Passing `queryId` in `db.executeQuery` is deprecated and will result in a compile-time error in the future.");
|
|
3854
|
+
const i = Ct(e) ? e.compile() : e;
|
|
3855
|
+
return this.getExecutor().executeQuery(i);
|
|
3856
|
+
}
|
|
3857
|
+
async [Symbol.asyncDispose]() {
|
|
3858
|
+
await this.destroy();
|
|
3859
|
+
}
|
|
3860
|
+
}
|
|
3861
|
+
class R extends $ {
|
|
3862
|
+
#e;
|
|
3863
|
+
constructor(e) {
|
|
3864
|
+
super(e), this.#e = e;
|
|
3865
|
+
}
|
|
3866
|
+
// The return type is `true` instead of `boolean` to make Kysely<DB>
|
|
3867
|
+
// unassignable to Transaction<DB> while allowing assignment the
|
|
3868
|
+
// other way around.
|
|
3869
|
+
get isTransaction() {
|
|
3870
|
+
return !0;
|
|
3871
|
+
}
|
|
3872
|
+
transaction() {
|
|
3873
|
+
throw new Error("calling the transaction method for a Transaction is not supported");
|
|
3874
|
+
}
|
|
3875
|
+
connection() {
|
|
3876
|
+
throw new Error("calling the connection method for a Transaction is not supported");
|
|
3877
|
+
}
|
|
3878
|
+
async destroy() {
|
|
3879
|
+
throw new Error("calling the destroy method for a Transaction is not supported");
|
|
3880
|
+
}
|
|
3881
|
+
withPlugin(e) {
|
|
3882
|
+
return new R({
|
|
3883
|
+
...this.#e,
|
|
3884
|
+
executor: this.#e.executor.withPlugin(e)
|
|
3885
|
+
});
|
|
3886
|
+
}
|
|
3887
|
+
withoutPlugins() {
|
|
3888
|
+
return new R({
|
|
3889
|
+
...this.#e,
|
|
3890
|
+
executor: this.#e.executor.withoutPlugins()
|
|
3891
|
+
});
|
|
3892
|
+
}
|
|
3893
|
+
withSchema(e) {
|
|
3894
|
+
return new R({
|
|
3895
|
+
...this.#e,
|
|
3896
|
+
executor: this.#e.executor.withPluginAtFront(new pe(e))
|
|
3897
|
+
});
|
|
3898
|
+
}
|
|
3899
|
+
withTables() {
|
|
3900
|
+
return new R({ ...this.#e });
|
|
3901
|
+
}
|
|
3902
|
+
}
|
|
3903
|
+
function Ot(t) {
|
|
3904
|
+
return V(t) && V(t.config) && V(t.driver) && V(t.executor) && V(t.dialect);
|
|
3905
|
+
}
|
|
3906
|
+
class bt {
|
|
3907
|
+
#e;
|
|
3908
|
+
constructor(e) {
|
|
3909
|
+
this.#e = r(e);
|
|
3910
|
+
}
|
|
3911
|
+
async execute(e) {
|
|
3912
|
+
return this.#e.executor.provideConnection(async (n) => {
|
|
3913
|
+
const i = this.#e.executor.withConnectionProvider(new be(n)), o = new $({
|
|
3914
|
+
...this.#e,
|
|
3915
|
+
executor: i
|
|
3916
|
+
});
|
|
3917
|
+
return await e(o);
|
|
3918
|
+
});
|
|
3919
|
+
}
|
|
3920
|
+
}
|
|
3921
|
+
class le {
|
|
3922
|
+
#e;
|
|
3923
|
+
constructor(e) {
|
|
3924
|
+
this.#e = r(e);
|
|
3925
|
+
}
|
|
3926
|
+
setAccessMode(e) {
|
|
3927
|
+
return new le({
|
|
3928
|
+
...this.#e,
|
|
3929
|
+
accessMode: e
|
|
3930
|
+
});
|
|
3931
|
+
}
|
|
3932
|
+
setIsolationLevel(e) {
|
|
3933
|
+
return new le({
|
|
3934
|
+
...this.#e,
|
|
3935
|
+
isolationLevel: e
|
|
3936
|
+
});
|
|
3937
|
+
}
|
|
3938
|
+
async execute(e) {
|
|
3939
|
+
const { isolationLevel: n, accessMode: i, ...o } = this.#e, u = { isolationLevel: n, accessMode: i };
|
|
3940
|
+
return Ve(u), this.#e.executor.provideConnection(async (h) => {
|
|
3941
|
+
const C = this.#e.executor.withConnectionProvider(new be(h)), W = new R({
|
|
3942
|
+
...o,
|
|
3943
|
+
executor: C
|
|
3944
|
+
});
|
|
3945
|
+
let y = !1;
|
|
3946
|
+
try {
|
|
3947
|
+
await this.#e.driver.beginTransaction(h, u), y = !0;
|
|
3948
|
+
const J = await e(W);
|
|
3949
|
+
return await this.#e.driver.commitTransaction(h), J;
|
|
3950
|
+
} catch (J) {
|
|
3951
|
+
throw y && await this.#e.driver.rollbackTransaction(h), J;
|
|
3952
|
+
}
|
|
3953
|
+
});
|
|
3954
|
+
}
|
|
3955
|
+
}
|
|
3956
|
+
class we {
|
|
3957
|
+
#e;
|
|
3958
|
+
constructor(e) {
|
|
3959
|
+
this.#e = r(e);
|
|
3960
|
+
}
|
|
3961
|
+
setAccessMode(e) {
|
|
3962
|
+
return new we({
|
|
3963
|
+
...this.#e,
|
|
3964
|
+
accessMode: e
|
|
3965
|
+
});
|
|
3966
|
+
}
|
|
3967
|
+
setIsolationLevel(e) {
|
|
3968
|
+
return new we({
|
|
3969
|
+
...this.#e,
|
|
3970
|
+
isolationLevel: e
|
|
3971
|
+
});
|
|
3972
|
+
}
|
|
3973
|
+
async execute() {
|
|
3974
|
+
const { isolationLevel: e, accessMode: n, ...i } = this.#e, o = { isolationLevel: e, accessMode: n };
|
|
3975
|
+
Ve(o);
|
|
3976
|
+
const u = await nt(this.#e.executor);
|
|
3977
|
+
return await this.#e.driver.beginTransaction(u.connection, o), new b({
|
|
3978
|
+
...i,
|
|
3979
|
+
connection: u,
|
|
3980
|
+
executor: this.#e.executor.withConnectionProvider(new be(u.connection))
|
|
3981
|
+
});
|
|
3982
|
+
}
|
|
3983
|
+
}
|
|
3984
|
+
class b extends R {
|
|
3985
|
+
#e;
|
|
3986
|
+
#t;
|
|
3987
|
+
#r;
|
|
3988
|
+
constructor(e) {
|
|
3989
|
+
const n = { isCommitted: !1, isRolledBack: !1 };
|
|
3990
|
+
e = {
|
|
3991
|
+
...e,
|
|
3992
|
+
executor: new P(e.executor, n)
|
|
3993
|
+
};
|
|
3994
|
+
const { connection: i, ...o } = e;
|
|
3995
|
+
super(o), this.#e = r(e), this.#r = n;
|
|
3996
|
+
const u = d();
|
|
3997
|
+
this.#t = (h) => e.executor.compileQuery(h, u);
|
|
3998
|
+
}
|
|
3999
|
+
get isCommitted() {
|
|
4000
|
+
return this.#r.isCommitted;
|
|
4001
|
+
}
|
|
4002
|
+
get isRolledBack() {
|
|
4003
|
+
return this.#r.isRolledBack;
|
|
4004
|
+
}
|
|
4005
|
+
/**
|
|
4006
|
+
* Commits the transaction.
|
|
4007
|
+
*
|
|
4008
|
+
* See {@link rollback}.
|
|
4009
|
+
*
|
|
4010
|
+
* ### Examples
|
|
4011
|
+
*
|
|
4012
|
+
* ```ts
|
|
4013
|
+
* import type { Kysely } from 'kysely'
|
|
4014
|
+
* import type { Database } from 'type-editor' // imaginary module
|
|
4015
|
+
*
|
|
4016
|
+
* const trx = await db.startTransaction().execute()
|
|
4017
|
+
*
|
|
4018
|
+
* try {
|
|
4019
|
+
* await doSomething(trx)
|
|
4020
|
+
*
|
|
4021
|
+
* await trx.commit().execute()
|
|
4022
|
+
* } catch (error) {
|
|
4023
|
+
* await trx.rollback().execute()
|
|
4024
|
+
* }
|
|
4025
|
+
*
|
|
4026
|
+
* async function doSomething(kysely: Kysely<Database>) {}
|
|
4027
|
+
* ```
|
|
4028
|
+
*/
|
|
4029
|
+
commit() {
|
|
4030
|
+
return M(this.#r), new X(async () => {
|
|
4031
|
+
await this.#e.driver.commitTransaction(this.#e.connection.connection), this.#r.isCommitted = !0, this.#e.connection.release();
|
|
4032
|
+
});
|
|
4033
|
+
}
|
|
4034
|
+
/**
|
|
4035
|
+
* Rolls back the transaction.
|
|
4036
|
+
*
|
|
4037
|
+
* See {@link commit} and {@link rollbackToSavepoint}.
|
|
4038
|
+
*
|
|
4039
|
+
* ### Examples
|
|
4040
|
+
*
|
|
4041
|
+
* ```ts
|
|
4042
|
+
* import type { Kysely } from 'kysely'
|
|
4043
|
+
* import type { Database } from 'type-editor' // imaginary module
|
|
4044
|
+
*
|
|
4045
|
+
* const trx = await db.startTransaction().execute()
|
|
4046
|
+
*
|
|
4047
|
+
* try {
|
|
4048
|
+
* await doSomething(trx)
|
|
4049
|
+
*
|
|
4050
|
+
* await trx.commit().execute()
|
|
4051
|
+
* } catch (error) {
|
|
4052
|
+
* await trx.rollback().execute()
|
|
4053
|
+
* }
|
|
4054
|
+
*
|
|
4055
|
+
* async function doSomething(kysely: Kysely<Database>) {}
|
|
4056
|
+
* ```
|
|
4057
|
+
*/
|
|
4058
|
+
rollback() {
|
|
4059
|
+
return M(this.#r), new X(async () => {
|
|
4060
|
+
await this.#e.driver.rollbackTransaction(this.#e.connection.connection), this.#r.isRolledBack = !0, this.#e.connection.release();
|
|
4061
|
+
});
|
|
4062
|
+
}
|
|
4063
|
+
/**
|
|
4064
|
+
* Creates a savepoint with a given name.
|
|
4065
|
+
*
|
|
4066
|
+
* See {@link rollbackToSavepoint} and {@link releaseSavepoint}.
|
|
4067
|
+
*
|
|
4068
|
+
* For a type-safe experience, you should use the returned instance from now on.
|
|
4069
|
+
*
|
|
4070
|
+
* ### Examples
|
|
4071
|
+
*
|
|
4072
|
+
* ```ts
|
|
4073
|
+
* import type { Kysely } from 'kysely'
|
|
4074
|
+
* import type { Database } from 'type-editor' // imaginary module
|
|
4075
|
+
*
|
|
4076
|
+
* const trx = await db.startTransaction().execute()
|
|
4077
|
+
*
|
|
4078
|
+
* await insertJennifer(trx)
|
|
4079
|
+
*
|
|
4080
|
+
* const trxAfterJennifer = await trx.savepoint('after_jennifer').execute()
|
|
4081
|
+
*
|
|
4082
|
+
* try {
|
|
4083
|
+
* await doSomething(trxAfterJennifer)
|
|
4084
|
+
* } catch (error) {
|
|
4085
|
+
* await trxAfterJennifer.rollbackToSavepoint('after_jennifer').execute()
|
|
4086
|
+
* }
|
|
4087
|
+
*
|
|
4088
|
+
* async function insertJennifer(kysely: Kysely<Database>) {}
|
|
4089
|
+
* async function doSomething(kysely: Kysely<Database>) {}
|
|
4090
|
+
* ```
|
|
4091
|
+
*/
|
|
4092
|
+
savepoint(e) {
|
|
4093
|
+
return M(this.#r), new X(async () => (await this.#e.driver.savepoint?.(this.#e.connection.connection, e, this.#t), new b({ ...this.#e })));
|
|
4094
|
+
}
|
|
4095
|
+
/**
|
|
4096
|
+
* Rolls back to a savepoint with a given name.
|
|
4097
|
+
*
|
|
4098
|
+
* See {@link savepoint} and {@link releaseSavepoint}.
|
|
4099
|
+
*
|
|
4100
|
+
* You must use the same instance returned by {@link savepoint}, or
|
|
4101
|
+
* escape the type-check by using `as any`.
|
|
4102
|
+
*
|
|
4103
|
+
* ### Examples
|
|
4104
|
+
*
|
|
4105
|
+
* ```ts
|
|
4106
|
+
* import type { Kysely } from 'kysely'
|
|
4107
|
+
* import type { Database } from 'type-editor' // imaginary module
|
|
4108
|
+
*
|
|
4109
|
+
* const trx = await db.startTransaction().execute()
|
|
4110
|
+
*
|
|
4111
|
+
* await insertJennifer(trx)
|
|
4112
|
+
*
|
|
4113
|
+
* const trxAfterJennifer = await trx.savepoint('after_jennifer').execute()
|
|
4114
|
+
*
|
|
4115
|
+
* try {
|
|
4116
|
+
* await doSomething(trxAfterJennifer)
|
|
4117
|
+
* } catch (error) {
|
|
4118
|
+
* await trxAfterJennifer.rollbackToSavepoint('after_jennifer').execute()
|
|
4119
|
+
* }
|
|
4120
|
+
*
|
|
4121
|
+
* async function insertJennifer(kysely: Kysely<Database>) {}
|
|
4122
|
+
* async function doSomething(kysely: Kysely<Database>) {}
|
|
4123
|
+
* ```
|
|
4124
|
+
*/
|
|
4125
|
+
rollbackToSavepoint(e) {
|
|
4126
|
+
return M(this.#r), new X(async () => (await this.#e.driver.rollbackToSavepoint?.(this.#e.connection.connection, e, this.#t), new b({ ...this.#e })));
|
|
4127
|
+
}
|
|
4128
|
+
/**
|
|
4129
|
+
* Releases a savepoint with a given name.
|
|
4130
|
+
*
|
|
4131
|
+
* See {@link savepoint} and {@link rollbackToSavepoint}.
|
|
4132
|
+
*
|
|
4133
|
+
* You must use the same instance returned by {@link savepoint}, or
|
|
4134
|
+
* escape the type-check by using `as any`.
|
|
4135
|
+
*
|
|
4136
|
+
* ### Examples
|
|
4137
|
+
*
|
|
4138
|
+
* ```ts
|
|
4139
|
+
* import type { Kysely } from 'kysely'
|
|
4140
|
+
* import type { Database } from 'type-editor' // imaginary module
|
|
4141
|
+
*
|
|
4142
|
+
* const trx = await db.startTransaction().execute()
|
|
4143
|
+
*
|
|
4144
|
+
* await insertJennifer(trx)
|
|
4145
|
+
*
|
|
4146
|
+
* const trxAfterJennifer = await trx.savepoint('after_jennifer').execute()
|
|
4147
|
+
*
|
|
4148
|
+
* try {
|
|
4149
|
+
* await doSomething(trxAfterJennifer)
|
|
4150
|
+
* } catch (error) {
|
|
4151
|
+
* await trxAfterJennifer.rollbackToSavepoint('after_jennifer').execute()
|
|
4152
|
+
* }
|
|
4153
|
+
*
|
|
4154
|
+
* await trxAfterJennifer.releaseSavepoint('after_jennifer').execute()
|
|
4155
|
+
*
|
|
4156
|
+
* await doSomethingElse(trx)
|
|
4157
|
+
*
|
|
4158
|
+
* async function insertJennifer(kysely: Kysely<Database>) {}
|
|
4159
|
+
* async function doSomething(kysely: Kysely<Database>) {}
|
|
4160
|
+
* async function doSomethingElse(kysely: Kysely<Database>) {}
|
|
4161
|
+
* ```
|
|
4162
|
+
*/
|
|
4163
|
+
releaseSavepoint(e) {
|
|
4164
|
+
return M(this.#r), new X(async () => (await this.#e.driver.releaseSavepoint?.(this.#e.connection.connection, e, this.#t), new b({ ...this.#e })));
|
|
4165
|
+
}
|
|
4166
|
+
withPlugin(e) {
|
|
4167
|
+
return new b({
|
|
4168
|
+
...this.#e,
|
|
4169
|
+
executor: this.#e.executor.withPlugin(e)
|
|
4170
|
+
});
|
|
4171
|
+
}
|
|
4172
|
+
withoutPlugins() {
|
|
4173
|
+
return new b({
|
|
4174
|
+
...this.#e,
|
|
4175
|
+
executor: this.#e.executor.withoutPlugins()
|
|
4176
|
+
});
|
|
4177
|
+
}
|
|
4178
|
+
withSchema(e) {
|
|
4179
|
+
return new b({
|
|
4180
|
+
...this.#e,
|
|
4181
|
+
executor: this.#e.executor.withPluginAtFront(new pe(e))
|
|
4182
|
+
});
|
|
4183
|
+
}
|
|
4184
|
+
withTables() {
|
|
4185
|
+
return new b({ ...this.#e });
|
|
4186
|
+
}
|
|
4187
|
+
}
|
|
4188
|
+
class X {
|
|
4189
|
+
#e;
|
|
4190
|
+
constructor(e) {
|
|
4191
|
+
this.#e = e;
|
|
4192
|
+
}
|
|
4193
|
+
/**
|
|
4194
|
+
* Executes the command.
|
|
4195
|
+
*/
|
|
4196
|
+
async execute() {
|
|
4197
|
+
return await this.#e();
|
|
4198
|
+
}
|
|
4199
|
+
}
|
|
4200
|
+
function M(t) {
|
|
4201
|
+
if (t.isCommitted)
|
|
4202
|
+
throw new Error("Transaction is already committed");
|
|
4203
|
+
if (t.isRolledBack)
|
|
4204
|
+
throw new Error("Transaction is already rolled back");
|
|
4205
|
+
}
|
|
4206
|
+
class P {
|
|
4207
|
+
#e;
|
|
4208
|
+
#t;
|
|
4209
|
+
constructor(e, n) {
|
|
4210
|
+
e instanceof P ? this.#e = e.#e : this.#e = e, this.#t = n;
|
|
4211
|
+
}
|
|
4212
|
+
get adapter() {
|
|
4213
|
+
return this.#e.adapter;
|
|
4214
|
+
}
|
|
4215
|
+
get plugins() {
|
|
4216
|
+
return this.#e.plugins;
|
|
4217
|
+
}
|
|
4218
|
+
transformQuery(e, n) {
|
|
4219
|
+
return this.#e.transformQuery(e, n);
|
|
4220
|
+
}
|
|
4221
|
+
compileQuery(e, n) {
|
|
4222
|
+
return this.#e.compileQuery(e, n);
|
|
4223
|
+
}
|
|
4224
|
+
provideConnection(e) {
|
|
4225
|
+
return this.#e.provideConnection(e);
|
|
4226
|
+
}
|
|
4227
|
+
executeQuery(e) {
|
|
4228
|
+
return M(this.#t), this.#e.executeQuery(e);
|
|
4229
|
+
}
|
|
4230
|
+
stream(e, n) {
|
|
4231
|
+
return M(this.#t), this.#e.stream(e, n);
|
|
4232
|
+
}
|
|
4233
|
+
withConnectionProvider(e) {
|
|
4234
|
+
return new P(this.#e.withConnectionProvider(e), this.#t);
|
|
4235
|
+
}
|
|
4236
|
+
withPlugin(e) {
|
|
4237
|
+
return new P(this.#e.withPlugin(e), this.#t);
|
|
4238
|
+
}
|
|
4239
|
+
withPlugins(e) {
|
|
4240
|
+
return new P(this.#e.withPlugins(e), this.#t);
|
|
4241
|
+
}
|
|
4242
|
+
withPluginAtFront(e) {
|
|
4243
|
+
return new P(this.#e.withPluginAtFront(e), this.#t);
|
|
4244
|
+
}
|
|
4245
|
+
withoutPlugins() {
|
|
4246
|
+
return new P(this.#e.withoutPlugins(), this.#t);
|
|
4247
|
+
}
|
|
4248
|
+
}
|
|
4249
|
+
export {
|
|
4250
|
+
Le as $,
|
|
4251
|
+
ct as A,
|
|
4252
|
+
ne as B,
|
|
4253
|
+
bt as C,
|
|
4254
|
+
T as D,
|
|
4255
|
+
F as E,
|
|
4256
|
+
g as F,
|
|
4257
|
+
Y as G,
|
|
4258
|
+
s as H,
|
|
4259
|
+
xe as I,
|
|
4260
|
+
c as J,
|
|
4261
|
+
$ as K,
|
|
4262
|
+
O as L,
|
|
4263
|
+
ve as M,
|
|
4264
|
+
p as N,
|
|
4265
|
+
it as O,
|
|
4266
|
+
Re as P,
|
|
4267
|
+
Q,
|
|
4268
|
+
_ as R,
|
|
4269
|
+
ee as S,
|
|
4270
|
+
R as T,
|
|
4271
|
+
se as U,
|
|
4272
|
+
st as V,
|
|
4273
|
+
Qe as W,
|
|
4274
|
+
ie as X,
|
|
4275
|
+
Z as Y,
|
|
4276
|
+
ye as Z,
|
|
4277
|
+
fe as _,
|
|
4278
|
+
le as a,
|
|
4279
|
+
oe as a0,
|
|
4280
|
+
I as a1,
|
|
4281
|
+
te as a2,
|
|
4282
|
+
Te as a3,
|
|
4283
|
+
U as a4,
|
|
4284
|
+
ot as a5,
|
|
4285
|
+
re as a6,
|
|
4286
|
+
De as a7,
|
|
4287
|
+
at as a8,
|
|
4288
|
+
D as a9,
|
|
4289
|
+
Ct as aa,
|
|
4290
|
+
xt as ab,
|
|
4291
|
+
we as b,
|
|
4292
|
+
b as c,
|
|
4293
|
+
X as d,
|
|
4294
|
+
l as e,
|
|
4295
|
+
Ce as f,
|
|
4296
|
+
de as g,
|
|
4297
|
+
Oe as h,
|
|
4298
|
+
Ot as i,
|
|
4299
|
+
f as j,
|
|
4300
|
+
K as k,
|
|
4301
|
+
We as l,
|
|
4302
|
+
he as m,
|
|
4303
|
+
a as n,
|
|
4304
|
+
N as o,
|
|
4305
|
+
q as p,
|
|
4306
|
+
B as q,
|
|
4307
|
+
Me as r,
|
|
4308
|
+
j as s,
|
|
4309
|
+
lt as t,
|
|
4310
|
+
yt as u,
|
|
4311
|
+
ft as v,
|
|
4312
|
+
Ve as w,
|
|
4313
|
+
wt as x,
|
|
4314
|
+
be as y,
|
|
4315
|
+
Ee as z
|
|
4316
|
+
};
|