kysely 0.13.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +92 -32
- package/dist/cjs/{introspection → dialect}/database-introspector.d.ts +0 -0
- package/dist/cjs/{introspection → dialect}/database-introspector.js +0 -0
- package/dist/cjs/dialect/dialect.d.ts +1 -1
- package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +1 -1
- package/dist/cjs/dialect/mysql/mysql-introspector.d.ts +1 -1
- package/dist/cjs/dialect/mysql/mysql-introspector.js +3 -3
- package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +1 -1
- package/dist/cjs/dialect/postgres/postgres-introspector.d.ts +1 -1
- package/dist/cjs/dialect/postgres/postgres-introspector.js +3 -3
- package/dist/cjs/dialect/sqlite/sqlite-dialect.d.ts +1 -1
- package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +1 -1
- package/dist/cjs/dialect/sqlite/sqlite-introspector.js +3 -3
- package/dist/cjs/index.d.ts +6 -3
- package/dist/cjs/index.js +6 -3
- package/dist/cjs/kysely.d.ts +8 -40
- package/dist/cjs/kysely.js +8 -40
- package/dist/cjs/migration/file-migration-provider.d.ts +52 -0
- package/dist/cjs/migration/file-migration-provider.js +52 -0
- package/dist/{esm/migration/migration.d.ts → cjs/migration/migrator.d.ts} +159 -134
- package/dist/cjs/migration/{migration.js → migrator.js} +166 -57
- package/dist/cjs/operation-node/check-constraint-node.d.ts +1 -1
- package/dist/cjs/operation-node/check-constraint-node.js +2 -3
- package/dist/cjs/operation-node/column-definition-node.d.ts +2 -2
- package/dist/cjs/operation-node/data-type-node.d.ts +1 -1
- package/dist/cjs/operation-node/generated-node.d.ts +20 -0
- package/dist/cjs/operation-node/generated-node.js +31 -0
- package/dist/cjs/operation-node/on-conflict-node.d.ts +13 -2
- package/dist/cjs/operation-node/on-conflict-node.js +39 -1
- package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -2
- package/dist/cjs/operation-node/operation-node-transformer.js +11 -5
- package/dist/cjs/operation-node/operation-node-visitor.d.ts +2 -2
- package/dist/cjs/operation-node/operation-node-visitor.js +1 -1
- package/dist/cjs/operation-node/operation-node.d.ts +1 -1
- package/dist/cjs/parser/insert-values-parser.d.ts +4 -4
- package/dist/cjs/parser/insert-values-parser.js +2 -3
- package/dist/cjs/parser/reference-parser.d.ts +4 -3
- package/dist/cjs/parser/select-parser.d.ts +10 -4
- package/dist/cjs/parser/update-set-parser.d.ts +2 -1
- package/dist/cjs/query-builder/delete-query-builder.d.ts +2 -4
- package/dist/cjs/query-builder/insert-query-builder.d.ts +80 -95
- package/dist/cjs/query-builder/insert-query-builder.js +145 -15
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +686 -0
- package/dist/cjs/query-builder/on-conflict-builder.js +323 -0
- package/dist/cjs/query-builder/returning-interface.d.ts +2 -4
- package/dist/cjs/query-builder/select-query-builder.d.ts +1 -1
- package/dist/cjs/query-builder/update-query-builder.d.ts +2 -4
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +2 -2
- package/dist/cjs/query-compiler/default-query-compiler.js +33 -7
- package/dist/cjs/query-creator.d.ts +0 -2
- package/dist/cjs/query-creator.js +0 -2
- package/dist/cjs/schema/alter-table-builder.d.ts +29 -9
- package/dist/cjs/schema/alter-table-builder.js +33 -9
- package/dist/cjs/schema/column-definition-builder.d.ts +28 -8
- package/dist/cjs/schema/column-definition-builder.js +17 -7
- package/dist/cjs/schema/create-index-builder.d.ts +3 -2
- package/dist/cjs/schema/create-index-builder.js +2 -2
- package/dist/cjs/schema/create-table-builder.d.ts +3 -2
- package/dist/cjs/schema/create-table-builder.js +2 -2
- package/dist/cjs/util/column-type.d.ts +165 -0
- package/dist/cjs/util/column-type.js +2 -0
- package/dist/cjs/util/private-symbols.d.ts +5 -0
- package/dist/cjs/util/private-symbols.js +8 -0
- package/dist/cjs/util/type-utils.d.ts +6 -42
- package/dist/esm/{introspection → dialect}/database-introspector.d.ts +0 -0
- package/dist/esm/{introspection → dialect}/database-introspector.js +0 -0
- package/dist/esm/dialect/dialect.d.ts +1 -1
- package/dist/esm/dialect/mysql/mysql-dialect.d.ts +1 -1
- package/dist/esm/dialect/mysql/mysql-introspector.d.ts +1 -1
- package/dist/esm/dialect/mysql/mysql-introspector.js +1 -1
- package/dist/esm/dialect/postgres/postgres-dialect.d.ts +1 -1
- package/dist/esm/dialect/postgres/postgres-introspector.d.ts +1 -1
- package/dist/esm/dialect/postgres/postgres-introspector.js +1 -1
- package/dist/esm/dialect/sqlite/sqlite-dialect.d.ts +1 -1
- package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +1 -1
- package/dist/esm/dialect/sqlite/sqlite-introspector.js +1 -1
- package/dist/esm/index.d.ts +6 -3
- package/dist/esm/index.js +6 -3
- package/dist/esm/kysely.d.ts +8 -40
- package/dist/esm/kysely.js +8 -40
- package/dist/esm/migration/file-migration-provider.d.ts +52 -0
- package/dist/esm/migration/file-migration-provider.js +48 -0
- package/dist/{cjs/migration/migration.d.ts → esm/migration/migrator.d.ts} +159 -134
- package/dist/esm/migration/{migration.js → migrator.js} +165 -56
- package/dist/esm/operation-node/check-constraint-node.d.ts +1 -1
- package/dist/esm/operation-node/check-constraint-node.js +2 -3
- package/dist/esm/operation-node/column-definition-node.d.ts +2 -2
- package/dist/esm/operation-node/data-type-node.d.ts +1 -1
- package/dist/esm/operation-node/generated-node.d.ts +20 -0
- package/dist/esm/operation-node/generated-node.js +28 -0
- package/dist/esm/operation-node/on-conflict-node.d.ts +13 -2
- package/dist/esm/operation-node/on-conflict-node.js +39 -1
- package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -2
- package/dist/esm/operation-node/operation-node-transformer.js +11 -5
- package/dist/esm/operation-node/operation-node-visitor.d.ts +2 -2
- package/dist/esm/operation-node/operation-node-visitor.js +1 -1
- package/dist/esm/operation-node/operation-node.d.ts +1 -1
- package/dist/esm/parser/insert-values-parser.d.ts +4 -4
- package/dist/esm/parser/insert-values-parser.js +2 -3
- package/dist/esm/parser/reference-parser.d.ts +4 -3
- package/dist/esm/parser/select-parser.d.ts +10 -4
- package/dist/esm/parser/update-set-parser.d.ts +2 -1
- package/dist/esm/query-builder/delete-query-builder.d.ts +2 -4
- package/dist/esm/query-builder/insert-query-builder.d.ts +80 -95
- package/dist/esm/query-builder/insert-query-builder.js +145 -15
- package/dist/esm/query-builder/on-conflict-builder.d.ts +686 -0
- package/dist/esm/query-builder/on-conflict-builder.js +317 -0
- package/dist/esm/query-builder/returning-interface.d.ts +2 -4
- package/dist/esm/query-builder/select-query-builder.d.ts +1 -1
- package/dist/esm/query-builder/update-query-builder.d.ts +2 -4
- package/dist/esm/query-compiler/default-query-compiler.d.ts +2 -2
- package/dist/esm/query-compiler/default-query-compiler.js +33 -7
- package/dist/esm/query-creator.d.ts +0 -2
- package/dist/esm/query-creator.js +0 -2
- package/dist/esm/schema/alter-table-builder.d.ts +29 -9
- package/dist/esm/schema/alter-table-builder.js +33 -9
- package/dist/esm/schema/column-definition-builder.d.ts +28 -8
- package/dist/esm/schema/column-definition-builder.js +17 -7
- package/dist/esm/schema/create-index-builder.d.ts +3 -2
- package/dist/esm/schema/create-index-builder.js +2 -2
- package/dist/esm/schema/create-table-builder.d.ts +3 -2
- package/dist/esm/schema/create-table-builder.js +2 -2
- package/dist/esm/util/column-type.d.ts +165 -0
- package/dist/esm/util/column-type.js +1 -0
- package/dist/esm/util/private-symbols.d.ts +5 -0
- package/dist/esm/util/private-symbols.js +5 -0
- package/dist/esm/util/type-utils.d.ts +6 -42
- package/package.json +1 -1
- package/dist/cjs/operation-node/generated-always-as-node.d.ts +0 -16
- package/dist/cjs/operation-node/generated-always-as-node.js +0 -25
- package/dist/cjs/parser/on-conflict-parser.d.ts +0 -10
- package/dist/cjs/parser/on-conflict-parser.js +0 -35
- package/dist/cjs/util/generated-placeholder.d.ts +0 -3
- package/dist/cjs/util/generated-placeholder.js +0 -11
- package/dist/esm/operation-node/generated-always-as-node.d.ts +0 -16
- package/dist/esm/operation-node/generated-always-as-node.js +0 -22
- package/dist/esm/parser/on-conflict-parser.d.ts +0 -10
- package/dist/esm/parser/on-conflict-parser.js +0 -30
- package/dist/esm/util/generated-placeholder.d.ts +0 -3
- package/dist/esm/util/generated-placeholder.js +0 -7
|
@@ -3,18 +3,19 @@ import { CompiledQuery } from '../query-compiler/compiled-query.js';
|
|
|
3
3
|
import { SelectExpression } from '../parser/select-parser.js';
|
|
4
4
|
import { InsertObject } from '../parser/insert-values-parser.js';
|
|
5
5
|
import { InsertQueryNode } from '../operation-node/insert-query-node.js';
|
|
6
|
-
import {
|
|
6
|
+
import { SingleResultType } from '../util/type-utils.js';
|
|
7
7
|
import { MutationObject } from '../parser/update-set-parser.js';
|
|
8
8
|
import { Compilable } from '../util/compilable.js';
|
|
9
9
|
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
10
10
|
import { QueryId } from '../util/query-id.js';
|
|
11
|
-
import { OnConflictConstraintTarget } from '../parser/on-conflict-parser.js';
|
|
12
11
|
import { ParseContext } from '../parser/parse-context.js';
|
|
13
12
|
import { KyselyPlugin } from '../plugin/kysely-plugin.js';
|
|
14
13
|
import { ReturningRow } from '../parser/returning-parser.js';
|
|
15
14
|
import { NoResultErrorConstructor } from './no-result-error.js';
|
|
16
15
|
import { ComplexExpression } from '../parser/complex-expression-parser.js';
|
|
17
16
|
import { ReturningInterface } from './returning-interface.js';
|
|
17
|
+
import { OnConflictBuilder, OnConflictDoNothingBuilder, OnConflictUpdateBuilder } from './on-conflict-builder.js';
|
|
18
|
+
import { Selectable } from '../util/column-type.js';
|
|
18
19
|
export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable {
|
|
19
20
|
#private;
|
|
20
21
|
constructor(props: InsertQueryBuilderProps);
|
|
@@ -23,13 +24,10 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
23
24
|
*
|
|
24
25
|
* This method takes an object whose keys are column names and values are
|
|
25
26
|
* values to insert. In addition to the column's type, the values can be
|
|
26
|
-
* {@link Kysely.raw | raw} instances
|
|
27
|
-
* {@link Kysely.generated | generated} placeholder.
|
|
27
|
+
* {@link Kysely.raw | raw} instances or select queries.
|
|
28
28
|
*
|
|
29
|
-
* You must provide all
|
|
30
|
-
*
|
|
31
|
-
* identifiers can be marked with the {@link Kysely.generated} placeholder unless you
|
|
32
|
-
* want to insert a specific value instead of the generated default.
|
|
29
|
+
* You must provide all fields you haven't explicitly marked as nullable
|
|
30
|
+
* or optional using {@link Generated} or {@link ColumnType}.
|
|
33
31
|
*
|
|
34
32
|
* The return value of an `insert` query is an instance of {@link InsertResult}. The
|
|
35
33
|
* {@link InsertResult.insertId | insertId} field holds the auto incremented primary
|
|
@@ -48,7 +46,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
48
46
|
* const id = await db
|
|
49
47
|
* .insertInto('person')
|
|
50
48
|
* .values({
|
|
51
|
-
* id: db.generated,
|
|
52
49
|
* first_name: 'Jennifer',
|
|
53
50
|
* last_name: 'Aniston'
|
|
54
51
|
* })
|
|
@@ -70,11 +67,9 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
70
67
|
* await db
|
|
71
68
|
* .insertInto('person')
|
|
72
69
|
* .values([{
|
|
73
|
-
* id: db.generated,
|
|
74
70
|
* first_name: 'Jennifer',
|
|
75
71
|
* last_name: 'Aniston'
|
|
76
72
|
* }, {
|
|
77
|
-
* id: db.generated,
|
|
78
73
|
* first_name: 'Arnold',
|
|
79
74
|
* last_name: 'Schwarzenegger',
|
|
80
75
|
* }])
|
|
@@ -95,7 +90,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
95
90
|
* const row = await db
|
|
96
91
|
* .insertInto('person')
|
|
97
92
|
* .values({
|
|
98
|
-
* id: db.generated,
|
|
99
93
|
* first_name: 'Jennifer',
|
|
100
94
|
* last_name: 'Aniston'
|
|
101
95
|
* })
|
|
@@ -118,7 +112,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
118
112
|
* const result = await db
|
|
119
113
|
* .insertInto('person')
|
|
120
114
|
* .values({
|
|
121
|
-
* id: db.generated,
|
|
122
115
|
* first_name: 'Jennifer',
|
|
123
116
|
* last_name: db.raw('? || ?', ['Ani', 'ston']),
|
|
124
117
|
* age: db.selectFrom('person').select(raw('avg(age)')),
|
|
@@ -144,7 +137,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
144
137
|
* .select(['id', 'first_name', 'gender'])
|
|
145
138
|
* .limit(1)
|
|
146
139
|
* ).insertInto('pet').values({
|
|
147
|
-
* id: db.generated,
|
|
148
140
|
* owner_id: (eb) => eb.selectFrom('jennifer').select('id'),
|
|
149
141
|
* name: (eb) => eb.selectFrom('jennifer').select('first_name'),
|
|
150
142
|
* species: 'cat',
|
|
@@ -196,21 +188,32 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
196
188
|
*/
|
|
197
189
|
expression(expression: ComplexExpression<DB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
198
190
|
/**
|
|
199
|
-
*
|
|
200
|
-
* argument conflicts with the current rows in the database. The
|
|
201
|
-
* default behavior without this method call is to throw an error.
|
|
191
|
+
* Changes an `insert into` query to an `insert ignore into` query.
|
|
202
192
|
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
* and
|
|
193
|
+
* If you use the ignore modifier, ignorable errors that occur while executing the
|
|
194
|
+
* insert statement are ignored. For example, without ignore, a row that duplicates
|
|
195
|
+
* an existing unique index or primary key value in the table causes a duplicate-key
|
|
196
|
+
* error and the statement is aborted. With ignore, the row is discarded and no error
|
|
197
|
+
* occurs.
|
|
207
198
|
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
* similar results.
|
|
199
|
+
* This is only supported on some dialects like MySQL. On most dialects you should
|
|
200
|
+
* use the {@link onConflict} method.
|
|
211
201
|
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
202
|
+
* ### Examples
|
|
203
|
+
*
|
|
204
|
+
* ```ts
|
|
205
|
+
* await db.insertInto('person')
|
|
206
|
+
* .ignore()
|
|
207
|
+
* .values(values)
|
|
208
|
+
* .execute()
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
ignore(): InsertQueryBuilder<DB, TB, O>;
|
|
212
|
+
/**
|
|
213
|
+
* Adds an `on conflict` clause to the query.
|
|
214
|
+
*
|
|
215
|
+
* `on conflict` is only supported by some dialects like PostgreSQL and SQLite. On MySQL
|
|
216
|
+
* you can use {@link ignore} and {@link onDuplicateKeyUpdate} to achieve similar results.
|
|
214
217
|
*
|
|
215
218
|
* ### Examples
|
|
216
219
|
*
|
|
@@ -218,11 +221,13 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
218
221
|
* await db
|
|
219
222
|
* .insertInto('pet')
|
|
220
223
|
* .values({
|
|
221
|
-
* id: db.generated,
|
|
222
224
|
* name: 'Catto',
|
|
223
225
|
* species: 'cat',
|
|
224
226
|
* })
|
|
225
|
-
* .
|
|
227
|
+
* .onConflict((oc) => oc
|
|
228
|
+
* .column('name')
|
|
229
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
230
|
+
* )
|
|
226
231
|
* .execute()
|
|
227
232
|
* ```
|
|
228
233
|
*
|
|
@@ -231,22 +236,24 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
231
236
|
* ```sql
|
|
232
237
|
* insert into "pet" ("name", "species")
|
|
233
238
|
* values ($1, $2)
|
|
234
|
-
* on conflict ("name")
|
|
239
|
+
* on conflict ("name")
|
|
240
|
+
* do update set "species" = $3
|
|
235
241
|
* ```
|
|
236
242
|
*
|
|
237
|
-
* You can provide the name of the constraint instead of a column name
|
|
243
|
+
* You can provide the name of the constraint instead of a column name:
|
|
238
244
|
*
|
|
239
245
|
* ```ts
|
|
240
246
|
* await db
|
|
241
247
|
* .insertInto('pet')
|
|
242
248
|
* .values({
|
|
243
|
-
* id: db.generated,
|
|
244
249
|
* name: 'Catto',
|
|
245
250
|
* species: 'cat',
|
|
246
251
|
* })
|
|
247
|
-
* .
|
|
248
|
-
* constraint
|
|
249
|
-
*
|
|
252
|
+
* .onConflict((oc) => oc
|
|
253
|
+
* .constraint('pet_name_key')
|
|
254
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
255
|
+
* )
|
|
256
|
+
* .execute()
|
|
250
257
|
* .execute()
|
|
251
258
|
* ```
|
|
252
259
|
*
|
|
@@ -256,66 +263,49 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
256
263
|
* insert into "pet" ("name", "species")
|
|
257
264
|
* values ($1, $2)
|
|
258
265
|
* on conflict on constraint "pet_name_key"
|
|
259
|
-
* do
|
|
266
|
+
* do update set "species" = $3
|
|
260
267
|
* ```
|
|
261
|
-
*/
|
|
262
|
-
onConflictDoNothing(column: AnyColumn<DB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
263
|
-
onConflictDoNothing(columns: ReadonlyArray<AnyColumn<DB, TB>>): InsertQueryBuilder<DB, TB, O>;
|
|
264
|
-
onConflictDoNothing(constraint: OnConflictConstraintTarget): InsertQueryBuilder<DB, TB, O>;
|
|
265
|
-
/**
|
|
266
|
-
* Changes an `insert into` query to an `insert ignore into` query.
|
|
267
268
|
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
* an existing unique index or primary key value in the table causes a duplicate-key
|
|
271
|
-
* error and the statement is aborted. With ignore, the row is discarded and no error
|
|
272
|
-
* occurs.
|
|
273
|
-
*
|
|
274
|
-
* This is only supported on some dialects like MySQL. On most dialects you should
|
|
275
|
-
* use the {@link onConflictDoNothing} method.
|
|
276
|
-
*
|
|
277
|
-
* ### Examples
|
|
269
|
+
* You can also specify an expression as the conflict target in case
|
|
270
|
+
* the unique index is an expression index:
|
|
278
271
|
*
|
|
279
272
|
* ```ts
|
|
280
|
-
* await db
|
|
281
|
-
* .
|
|
282
|
-
* .values(
|
|
273
|
+
* await db
|
|
274
|
+
* .insertInto('pet')
|
|
275
|
+
* .values({
|
|
276
|
+
* name: 'Catto',
|
|
277
|
+
* species: 'cat',
|
|
278
|
+
* })
|
|
279
|
+
* .onConflict((oc) => oc
|
|
280
|
+
* .expression(db.raw('lower(name)'))
|
|
281
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
282
|
+
* )
|
|
283
283
|
* .execute()
|
|
284
284
|
* ```
|
|
285
|
-
*/
|
|
286
|
-
ignore(): InsertQueryBuilder<DB, TB, O>;
|
|
287
|
-
/**
|
|
288
|
-
* Ignores an insert if the column or constraint given as the first
|
|
289
|
-
* argument conflicts with the current rows in the database and
|
|
290
|
-
* performs an update on the conflicting row instead. This method
|
|
291
|
-
* can be used to implement an upsert operation.
|
|
292
|
-
*
|
|
293
|
-
* For example if a table has a field `name` that has a unique constraint
|
|
294
|
-
* and you try to insert a row with a `name` that already exists in the
|
|
295
|
-
* database, calling `onConfictUpdate('name', { species: 'hamster' })`
|
|
296
|
-
* will set the conflicting row's `species` field to value `'hamster'`.
|
|
297
|
-
* By default the query would throw.
|
|
298
|
-
*
|
|
299
|
-
* The second argument (updates) can be anything the {@link UpdateQueryBuilder.set | set}
|
|
300
|
-
* method accepts.
|
|
301
285
|
*
|
|
302
|
-
* The
|
|
303
|
-
* like PostgreSQL and SQLite. On MySQL you should use the {@link onDuplicateKeyUpdate}
|
|
304
|
-
* method instead.
|
|
286
|
+
* The generated SQL (PostgreSQL):
|
|
305
287
|
*
|
|
306
|
-
*
|
|
288
|
+
* ```sql
|
|
289
|
+
* insert into "pet" ("name", "species")
|
|
290
|
+
* values ($1, $2)
|
|
291
|
+
* on conflict (lower(name))
|
|
292
|
+
* do update set "species" = $3
|
|
293
|
+
* ```
|
|
307
294
|
*
|
|
308
|
-
*
|
|
295
|
+
* You can add a filter for the update statement like this:
|
|
309
296
|
*
|
|
310
297
|
* ```ts
|
|
311
298
|
* await db
|
|
312
299
|
* .insertInto('pet')
|
|
313
300
|
* .values({
|
|
314
|
-
* id: db.generated,
|
|
315
301
|
* name: 'Catto',
|
|
316
302
|
* species: 'cat',
|
|
317
303
|
* })
|
|
318
|
-
* .
|
|
304
|
+
* .onConflict((oc) => oc
|
|
305
|
+
* .column('name')
|
|
306
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
307
|
+
* .where('excluded.name', '!=', 'Catto'')
|
|
308
|
+
* )
|
|
319
309
|
* .execute()
|
|
320
310
|
* ```
|
|
321
311
|
*
|
|
@@ -324,22 +314,23 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
324
314
|
* ```sql
|
|
325
315
|
* insert into "pet" ("name", "species")
|
|
326
316
|
* values ($1, $2)
|
|
327
|
-
* on conflict ("name")
|
|
317
|
+
* on conflict ("name")
|
|
318
|
+
* do update set "species" = $3
|
|
319
|
+
* where "excluded"."name" != $4
|
|
328
320
|
* ```
|
|
329
321
|
*
|
|
330
|
-
* You can
|
|
322
|
+
* You can create an `on conflict do nothing` clauses like this:
|
|
331
323
|
*
|
|
332
324
|
* ```ts
|
|
333
325
|
* await db
|
|
334
326
|
* .insertInto('pet')
|
|
335
327
|
* .values({
|
|
336
|
-
* id: db.generated,
|
|
337
328
|
* name: 'Catto',
|
|
338
329
|
* species: 'cat',
|
|
339
330
|
* })
|
|
340
|
-
* .
|
|
341
|
-
*
|
|
342
|
-
*
|
|
331
|
+
* .onConflict((oc) => oc
|
|
332
|
+
* .column('name')
|
|
333
|
+
* .doNothing()
|
|
343
334
|
* )
|
|
344
335
|
* .execute()
|
|
345
336
|
* ```
|
|
@@ -349,13 +340,10 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
349
340
|
* ```sql
|
|
350
341
|
* insert into "pet" ("name", "species")
|
|
351
342
|
* values ($1, $2)
|
|
352
|
-
* on conflict
|
|
353
|
-
* do update set "species" = $3
|
|
343
|
+
* on conflict ("name") do nothing
|
|
354
344
|
* ```
|
|
355
345
|
*/
|
|
356
|
-
|
|
357
|
-
onConflictUpdate(columns: ReadonlyArray<AnyColumn<DB, TB>>, updates: MutationObject<DB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
358
|
-
onConflictUpdate(constraint: OnConflictConstraintTarget, updates: MutationObject<DB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
346
|
+
onConflict(callback: (builder: OnConflictBuilder<DB, TB>) => OnConflictDoNothingBuilder<DB, TB> | OnConflictUpdateBuilder<DB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
359
347
|
/**
|
|
360
348
|
* Adds `on duplicate key update` to the query.
|
|
361
349
|
*
|
|
@@ -363,7 +351,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
363
351
|
* a duplicate value in a unique index or primary key, an update of the old row occurs.
|
|
364
352
|
*
|
|
365
353
|
* This is only implemented by some dialects like MySQL. On most dialects you should
|
|
366
|
-
* use {@link
|
|
354
|
+
* use {@link onConflict} instead.
|
|
367
355
|
*
|
|
368
356
|
* ### Examples
|
|
369
357
|
*
|
|
@@ -395,7 +383,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
395
383
|
* const { id } = await db
|
|
396
384
|
* .insertInto('person')
|
|
397
385
|
* .values({
|
|
398
|
-
* id: db.generated,
|
|
399
386
|
* first_name: 'Jennifer',
|
|
400
387
|
* last_name: 'Aniston'
|
|
401
388
|
* })
|
|
@@ -409,7 +396,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
409
396
|
* const { id, first_name } = await db
|
|
410
397
|
* .insertInto('person')
|
|
411
398
|
* .values({
|
|
412
|
-
* id: db.generated,
|
|
413
399
|
* first_name: 'Jennifer',
|
|
414
400
|
* last_name: 'Aniston'
|
|
415
401
|
* })
|
|
@@ -425,7 +411,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
425
411
|
* const { id, full_name, first_pet_id } = await db
|
|
426
412
|
* .insertInto('person')
|
|
427
413
|
* .values({
|
|
428
|
-
* id: db.generated,
|
|
429
414
|
* first_name: 'Jennifer',
|
|
430
415
|
* last_name: 'Aniston'
|
|
431
416
|
* })
|
|
@@ -443,7 +428,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
443
428
|
* Adds a `returning *` to an insert/update/delete query on databases
|
|
444
429
|
* that support `returning` such as PostgreSQL.
|
|
445
430
|
*/
|
|
446
|
-
returningAll(): InsertQueryBuilder<DB, TB, DB[TB]
|
|
431
|
+
returningAll(): InsertQueryBuilder<DB, TB, Selectable<DB[TB]>>;
|
|
447
432
|
/**
|
|
448
433
|
* Simply calls the given function passing `this` as the only argument.
|
|
449
434
|
*
|
|
@@ -456,9 +441,9 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
456
441
|
* .selectAll()
|
|
457
442
|
* .call((qb) => {
|
|
458
443
|
* if (something) {
|
|
459
|
-
* return qb.
|
|
444
|
+
* return qb.onConflict((oc) => oc.column('something').doNothing())
|
|
460
445
|
* } else {
|
|
461
|
-
* return qb.
|
|
446
|
+
* return qb.onConflict((oc) => oc.column('something_else').doNothing())
|
|
462
447
|
* }
|
|
463
448
|
* })
|
|
464
449
|
* .execute()
|
|
@@ -4,13 +4,14 @@ import { InsertQueryNode } from '../operation-node/insert-query-node.js';
|
|
|
4
4
|
import { QueryNode } from '../operation-node/query-node.js';
|
|
5
5
|
import { parseUpdateObject, } from '../parser/update-set-parser.js';
|
|
6
6
|
import { preventAwait } from '../util/prevent-await.js';
|
|
7
|
-
import { parseOnConflictDoNothing, parseOnConflictUpdate, } from '../parser/on-conflict-parser.js';
|
|
8
7
|
import { freeze } from '../util/object-utils.js';
|
|
9
8
|
import { OnDuplicateKeyNode } from '../operation-node/on-duplicate-key-node.js';
|
|
10
9
|
import { InsertResult } from './insert-result.js';
|
|
11
10
|
import { NoResultError } from './no-result-error.js';
|
|
12
11
|
import { parseComplexExpression, } from '../parser/complex-expression-parser.js';
|
|
13
12
|
import { ColumnNode } from '../operation-node/column-node.js';
|
|
13
|
+
import { OnConflictBuilder, } from './on-conflict-builder.js';
|
|
14
|
+
import { OnConflictNode } from '../operation-node/on-conflict-node.js';
|
|
14
15
|
export class InsertQueryBuilder {
|
|
15
16
|
#props;
|
|
16
17
|
constructor(props) {
|
|
@@ -82,14 +83,6 @@ export class InsertQueryBuilder {
|
|
|
82
83
|
}),
|
|
83
84
|
});
|
|
84
85
|
}
|
|
85
|
-
onConflictDoNothing(target) {
|
|
86
|
-
return new InsertQueryBuilder({
|
|
87
|
-
...this.#props,
|
|
88
|
-
queryNode: InsertQueryNode.cloneWith(this.#props.queryNode, {
|
|
89
|
-
onConflict: parseOnConflictDoNothing(target),
|
|
90
|
-
}),
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
86
|
/**
|
|
94
87
|
* Changes an `insert into` query to an `insert ignore into` query.
|
|
95
88
|
*
|
|
@@ -100,7 +93,7 @@ export class InsertQueryBuilder {
|
|
|
100
93
|
* occurs.
|
|
101
94
|
*
|
|
102
95
|
* This is only supported on some dialects like MySQL. On most dialects you should
|
|
103
|
-
* use the {@link
|
|
96
|
+
* use the {@link onConflict} method.
|
|
104
97
|
*
|
|
105
98
|
* ### Examples
|
|
106
99
|
*
|
|
@@ -119,11 +112,148 @@ export class InsertQueryBuilder {
|
|
|
119
112
|
}),
|
|
120
113
|
});
|
|
121
114
|
}
|
|
122
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Adds an `on conflict` clause to the query.
|
|
117
|
+
*
|
|
118
|
+
* `on conflict` is only supported by some dialects like PostgreSQL and SQLite. On MySQL
|
|
119
|
+
* you can use {@link ignore} and {@link onDuplicateKeyUpdate} to achieve similar results.
|
|
120
|
+
*
|
|
121
|
+
* ### Examples
|
|
122
|
+
*
|
|
123
|
+
* ```ts
|
|
124
|
+
* await db
|
|
125
|
+
* .insertInto('pet')
|
|
126
|
+
* .values({
|
|
127
|
+
* name: 'Catto',
|
|
128
|
+
* species: 'cat',
|
|
129
|
+
* })
|
|
130
|
+
* .onConflict((oc) => oc
|
|
131
|
+
* .column('name')
|
|
132
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
133
|
+
* )
|
|
134
|
+
* .execute()
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* The generated SQL (PostgreSQL):
|
|
138
|
+
*
|
|
139
|
+
* ```sql
|
|
140
|
+
* insert into "pet" ("name", "species")
|
|
141
|
+
* values ($1, $2)
|
|
142
|
+
* on conflict ("name")
|
|
143
|
+
* do update set "species" = $3
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
* You can provide the name of the constraint instead of a column name:
|
|
147
|
+
*
|
|
148
|
+
* ```ts
|
|
149
|
+
* await db
|
|
150
|
+
* .insertInto('pet')
|
|
151
|
+
* .values({
|
|
152
|
+
* name: 'Catto',
|
|
153
|
+
* species: 'cat',
|
|
154
|
+
* })
|
|
155
|
+
* .onConflict((oc) => oc
|
|
156
|
+
* .constraint('pet_name_key')
|
|
157
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
158
|
+
* )
|
|
159
|
+
* .execute()
|
|
160
|
+
* .execute()
|
|
161
|
+
* ```
|
|
162
|
+
*
|
|
163
|
+
* The generated SQL (PostgreSQL):
|
|
164
|
+
*
|
|
165
|
+
* ```sql
|
|
166
|
+
* insert into "pet" ("name", "species")
|
|
167
|
+
* values ($1, $2)
|
|
168
|
+
* on conflict on constraint "pet_name_key"
|
|
169
|
+
* do update set "species" = $3
|
|
170
|
+
* ```
|
|
171
|
+
*
|
|
172
|
+
* You can also specify an expression as the conflict target in case
|
|
173
|
+
* the unique index is an expression index:
|
|
174
|
+
*
|
|
175
|
+
* ```ts
|
|
176
|
+
* await db
|
|
177
|
+
* .insertInto('pet')
|
|
178
|
+
* .values({
|
|
179
|
+
* name: 'Catto',
|
|
180
|
+
* species: 'cat',
|
|
181
|
+
* })
|
|
182
|
+
* .onConflict((oc) => oc
|
|
183
|
+
* .expression(db.raw('lower(name)'))
|
|
184
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
185
|
+
* )
|
|
186
|
+
* .execute()
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* The generated SQL (PostgreSQL):
|
|
190
|
+
*
|
|
191
|
+
* ```sql
|
|
192
|
+
* insert into "pet" ("name", "species")
|
|
193
|
+
* values ($1, $2)
|
|
194
|
+
* on conflict (lower(name))
|
|
195
|
+
* do update set "species" = $3
|
|
196
|
+
* ```
|
|
197
|
+
*
|
|
198
|
+
* You can add a filter for the update statement like this:
|
|
199
|
+
*
|
|
200
|
+
* ```ts
|
|
201
|
+
* await db
|
|
202
|
+
* .insertInto('pet')
|
|
203
|
+
* .values({
|
|
204
|
+
* name: 'Catto',
|
|
205
|
+
* species: 'cat',
|
|
206
|
+
* })
|
|
207
|
+
* .onConflict((oc) => oc
|
|
208
|
+
* .column('name')
|
|
209
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
210
|
+
* .where('excluded.name', '!=', 'Catto'')
|
|
211
|
+
* )
|
|
212
|
+
* .execute()
|
|
213
|
+
* ```
|
|
214
|
+
*
|
|
215
|
+
* The generated SQL (PostgreSQL):
|
|
216
|
+
*
|
|
217
|
+
* ```sql
|
|
218
|
+
* insert into "pet" ("name", "species")
|
|
219
|
+
* values ($1, $2)
|
|
220
|
+
* on conflict ("name")
|
|
221
|
+
* do update set "species" = $3
|
|
222
|
+
* where "excluded"."name" != $4
|
|
223
|
+
* ```
|
|
224
|
+
*
|
|
225
|
+
* You can create an `on conflict do nothing` clauses like this:
|
|
226
|
+
*
|
|
227
|
+
* ```ts
|
|
228
|
+
* await db
|
|
229
|
+
* .insertInto('pet')
|
|
230
|
+
* .values({
|
|
231
|
+
* name: 'Catto',
|
|
232
|
+
* species: 'cat',
|
|
233
|
+
* })
|
|
234
|
+
* .onConflict((oc) => oc
|
|
235
|
+
* .column('name')
|
|
236
|
+
* .doNothing()
|
|
237
|
+
* )
|
|
238
|
+
* .execute()
|
|
239
|
+
* ```
|
|
240
|
+
*
|
|
241
|
+
* The generated SQL (PostgreSQL):
|
|
242
|
+
*
|
|
243
|
+
* ```sql
|
|
244
|
+
* insert into "pet" ("name", "species")
|
|
245
|
+
* values ($1, $2)
|
|
246
|
+
* on conflict ("name") do nothing
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
onConflict(callback) {
|
|
123
250
|
return new InsertQueryBuilder({
|
|
124
251
|
...this.#props,
|
|
125
252
|
queryNode: InsertQueryNode.cloneWith(this.#props.queryNode, {
|
|
126
|
-
onConflict:
|
|
253
|
+
onConflict: callback(new OnConflictBuilder({
|
|
254
|
+
onConflictNode: OnConflictNode.create(),
|
|
255
|
+
parseContext: this.#props.parseContext,
|
|
256
|
+
})).toOperationNode(),
|
|
127
257
|
}),
|
|
128
258
|
});
|
|
129
259
|
}
|
|
@@ -134,7 +264,7 @@ export class InsertQueryBuilder {
|
|
|
134
264
|
* a duplicate value in a unique index or primary key, an update of the old row occurs.
|
|
135
265
|
*
|
|
136
266
|
* This is only implemented by some dialects like MySQL. On most dialects you should
|
|
137
|
-
* use {@link
|
|
267
|
+
* use {@link onConflict} instead.
|
|
138
268
|
*
|
|
139
269
|
* ### Examples
|
|
140
270
|
*
|
|
@@ -177,9 +307,9 @@ export class InsertQueryBuilder {
|
|
|
177
307
|
* .selectAll()
|
|
178
308
|
* .call((qb) => {
|
|
179
309
|
* if (something) {
|
|
180
|
-
* return qb.
|
|
310
|
+
* return qb.onConflict((oc) => oc.column('something').doNothing())
|
|
181
311
|
* } else {
|
|
182
|
-
* return qb.
|
|
312
|
+
* return qb.onConflict((oc) => oc.column('something_else').doNothing())
|
|
183
313
|
* }
|
|
184
314
|
* })
|
|
185
315
|
* .execute()
|