kysely 0.13.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/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/postgres/postgres-dialect.d.ts +1 -1
- package/dist/cjs/dialect/postgres/postgres-introspector.d.ts +1 -1
- 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/index.d.ts +2 -2
- package/dist/cjs/index.js +2 -2
- package/dist/cjs/kysely.d.ts +1 -1
- 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/query-builder/insert-query-builder.d.ts +76 -75
- package/dist/cjs/query-builder/insert-query-builder.js +150 -15
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +684 -0
- package/dist/cjs/query-builder/on-conflict-builder.js +323 -0
- 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/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/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/postgres/postgres-dialect.d.ts +1 -1
- package/dist/esm/dialect/postgres/postgres-introspector.d.ts +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/index.d.ts +2 -2
- package/dist/esm/index.js +2 -2
- package/dist/esm/kysely.d.ts +1 -1
- 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/query-builder/insert-query-builder.d.ts +76 -75
- package/dist/esm/query-builder/insert-query-builder.js +150 -15
- package/dist/esm/query-builder/on-conflict-builder.d.ts +684 -0
- package/dist/esm/query-builder/on-conflict-builder.js +317 -0
- 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/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/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/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
|
@@ -3,18 +3,18 @@ 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
18
|
export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable {
|
|
19
19
|
#private;
|
|
20
20
|
constructor(props: InsertQueryBuilderProps);
|
|
@@ -196,21 +196,32 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
196
196
|
*/
|
|
197
197
|
expression(expression: ComplexExpression<DB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
198
198
|
/**
|
|
199
|
-
*
|
|
200
|
-
* argument conflicts with the current rows in the database. The
|
|
201
|
-
* default behavior without this method call is to throw an error.
|
|
199
|
+
* Changes an `insert into` query to an `insert ignore into` query.
|
|
202
200
|
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
* and
|
|
201
|
+
* If you use the ignore modifier, ignorable errors that occur while executing the
|
|
202
|
+
* insert statement are ignored. For example, without ignore, a row that duplicates
|
|
203
|
+
* an existing unique index or primary key value in the table causes a duplicate-key
|
|
204
|
+
* error and the statement is aborted. With ignore, the row is discarded and no error
|
|
205
|
+
* occurs.
|
|
207
206
|
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
* similar results.
|
|
207
|
+
* This is only supported on some dialects like MySQL. On most dialects you should
|
|
208
|
+
* use the {@link onConflict} method.
|
|
211
209
|
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
210
|
+
* ### Examples
|
|
211
|
+
*
|
|
212
|
+
* ```ts
|
|
213
|
+
* await db.insertInto('person')
|
|
214
|
+
* .ignore()
|
|
215
|
+
* .values(values)
|
|
216
|
+
* .execute()
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
ignore(): InsertQueryBuilder<DB, TB, O>;
|
|
220
|
+
/**
|
|
221
|
+
* Adds an `on conflict` clause to the query.
|
|
222
|
+
*
|
|
223
|
+
* `on conflict` is only supported by some dialects like PostgreSQL and SQLite. On MySQL
|
|
224
|
+
* you can use {@link ignore} and {@link onDuplicateKeyUpdate} to achieve similar results.
|
|
214
225
|
*
|
|
215
226
|
* ### Examples
|
|
216
227
|
*
|
|
@@ -222,7 +233,10 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
222
233
|
* name: 'Catto',
|
|
223
234
|
* species: 'cat',
|
|
224
235
|
* })
|
|
225
|
-
* .
|
|
236
|
+
* .onConflict((oc) => oc
|
|
237
|
+
* .column('name')
|
|
238
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
239
|
+
* )
|
|
226
240
|
* .execute()
|
|
227
241
|
* ```
|
|
228
242
|
*
|
|
@@ -231,10 +245,11 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
231
245
|
* ```sql
|
|
232
246
|
* insert into "pet" ("name", "species")
|
|
233
247
|
* values ($1, $2)
|
|
234
|
-
* on conflict ("name")
|
|
248
|
+
* on conflict ("name")
|
|
249
|
+
* do update set "species" = $3
|
|
235
250
|
* ```
|
|
236
251
|
*
|
|
237
|
-
* You can provide the name of the constraint instead of a column name
|
|
252
|
+
* You can provide the name of the constraint instead of a column name:
|
|
238
253
|
*
|
|
239
254
|
* ```ts
|
|
240
255
|
* await db
|
|
@@ -244,9 +259,11 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
244
259
|
* name: 'Catto',
|
|
245
260
|
* species: 'cat',
|
|
246
261
|
* })
|
|
247
|
-
* .
|
|
248
|
-
* constraint
|
|
249
|
-
*
|
|
262
|
+
* .onConflict((oc) => oc
|
|
263
|
+
* .constraint('pet_name_key')
|
|
264
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
265
|
+
* )
|
|
266
|
+
* .execute()
|
|
250
267
|
* .execute()
|
|
251
268
|
* ```
|
|
252
269
|
*
|
|
@@ -256,56 +273,37 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
256
273
|
* insert into "pet" ("name", "species")
|
|
257
274
|
* values ($1, $2)
|
|
258
275
|
* on conflict on constraint "pet_name_key"
|
|
259
|
-
* do
|
|
276
|
+
* do update set "species" = $3
|
|
260
277
|
* ```
|
|
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
|
-
* If you use the ignore modifier, ignorable errors that occur while executing the
|
|
269
|
-
* insert statement are ignored. For example, without ignore, a row that duplicates
|
|
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
278
|
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
* ### Examples
|
|
279
|
+
* You can also specify an expression as the conflict target in case
|
|
280
|
+
* the unique index is an expression index:
|
|
278
281
|
*
|
|
279
282
|
* ```ts
|
|
280
|
-
* await db
|
|
281
|
-
* .
|
|
282
|
-
* .values(
|
|
283
|
+
* await db
|
|
284
|
+
* .insertInto('pet')
|
|
285
|
+
* .values({
|
|
286
|
+
* id: db.generated,
|
|
287
|
+
* name: 'Catto',
|
|
288
|
+
* species: 'cat',
|
|
289
|
+
* })
|
|
290
|
+
* .onConflict((oc) => oc
|
|
291
|
+
* .expression(db.raw('lower(name)'))
|
|
292
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
293
|
+
* )
|
|
283
294
|
* .execute()
|
|
284
295
|
* ```
|
|
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
296
|
*
|
|
302
|
-
* The
|
|
303
|
-
* like PostgreSQL and SQLite. On MySQL you should use the {@link onDuplicateKeyUpdate}
|
|
304
|
-
* method instead.
|
|
297
|
+
* The generated SQL (PostgreSQL):
|
|
305
298
|
*
|
|
306
|
-
*
|
|
299
|
+
* ```sql
|
|
300
|
+
* insert into "pet" ("name", "species")
|
|
301
|
+
* values ($1, $2)
|
|
302
|
+
* on conflict (lower(name))
|
|
303
|
+
* do update set "species" = $3
|
|
304
|
+
* ```
|
|
307
305
|
*
|
|
308
|
-
*
|
|
306
|
+
* You can add a filter for the update statement like this:
|
|
309
307
|
*
|
|
310
308
|
* ```ts
|
|
311
309
|
* await db
|
|
@@ -315,7 +313,11 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
315
313
|
* name: 'Catto',
|
|
316
314
|
* species: 'cat',
|
|
317
315
|
* })
|
|
318
|
-
* .
|
|
316
|
+
* .onConflict((oc) => oc
|
|
317
|
+
* .column('name')
|
|
318
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
319
|
+
* .where('excluded.name', '!=', 'Catto'')
|
|
320
|
+
* )
|
|
319
321
|
* .execute()
|
|
320
322
|
* ```
|
|
321
323
|
*
|
|
@@ -324,10 +326,12 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
324
326
|
* ```sql
|
|
325
327
|
* insert into "pet" ("name", "species")
|
|
326
328
|
* values ($1, $2)
|
|
327
|
-
* on conflict ("name")
|
|
329
|
+
* on conflict ("name")
|
|
330
|
+
* do update set "species" = $3
|
|
331
|
+
* where "excluded"."name" != $4
|
|
328
332
|
* ```
|
|
329
333
|
*
|
|
330
|
-
* You can
|
|
334
|
+
* You can create an `on conflict do nothing` clauses like this:
|
|
331
335
|
*
|
|
332
336
|
* ```ts
|
|
333
337
|
* await db
|
|
@@ -337,9 +341,9 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
337
341
|
* name: 'Catto',
|
|
338
342
|
* species: 'cat',
|
|
339
343
|
* })
|
|
340
|
-
* .
|
|
341
|
-
*
|
|
342
|
-
*
|
|
344
|
+
* .onConflict((oc) => oc
|
|
345
|
+
* .column('name')
|
|
346
|
+
* .doNothing()
|
|
343
347
|
* )
|
|
344
348
|
* .execute()
|
|
345
349
|
* ```
|
|
@@ -349,13 +353,10 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
349
353
|
* ```sql
|
|
350
354
|
* insert into "pet" ("name", "species")
|
|
351
355
|
* values ($1, $2)
|
|
352
|
-
* on conflict
|
|
353
|
-
* do update set "species" = $3
|
|
356
|
+
* on conflict ("name") do nothing
|
|
354
357
|
* ```
|
|
355
358
|
*/
|
|
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>;
|
|
359
|
+
onConflict(callback: (builder: OnConflictBuilder<DB, TB>) => OnConflictDoNothingBuilder<DB, TB> | OnConflictUpdateBuilder<DB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
359
360
|
/**
|
|
360
361
|
* Adds `on duplicate key update` to the query.
|
|
361
362
|
*
|
|
@@ -363,7 +364,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
363
364
|
* a duplicate value in a unique index or primary key, an update of the old row occurs.
|
|
364
365
|
*
|
|
365
366
|
* This is only implemented by some dialects like MySQL. On most dialects you should
|
|
366
|
-
* use {@link
|
|
367
|
+
* use {@link onConflict} instead.
|
|
367
368
|
*
|
|
368
369
|
* ### Examples
|
|
369
370
|
*
|
|
@@ -456,9 +457,9 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
456
457
|
* .selectAll()
|
|
457
458
|
* .call((qb) => {
|
|
458
459
|
* if (something) {
|
|
459
|
-
* return qb.
|
|
460
|
+
* return qb.onConflict((oc) => oc.column('something').doNothing())
|
|
460
461
|
* } else {
|
|
461
|
-
* return qb.
|
|
462
|
+
* return qb.onConflict((oc) => oc.column('something_else').doNothing())
|
|
462
463
|
* }
|
|
463
464
|
* })
|
|
464
465
|
* .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 { OnConflictNode } from '../index.js';
|
|
14
|
+
import { OnConflictBuilder, } from './on-conflict-builder.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,153 @@ 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
|
+
* id: db.generated,
|
|
128
|
+
* name: 'Catto',
|
|
129
|
+
* species: 'cat',
|
|
130
|
+
* })
|
|
131
|
+
* .onConflict((oc) => oc
|
|
132
|
+
* .column('name')
|
|
133
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
134
|
+
* )
|
|
135
|
+
* .execute()
|
|
136
|
+
* ```
|
|
137
|
+
*
|
|
138
|
+
* The generated SQL (PostgreSQL):
|
|
139
|
+
*
|
|
140
|
+
* ```sql
|
|
141
|
+
* insert into "pet" ("name", "species")
|
|
142
|
+
* values ($1, $2)
|
|
143
|
+
* on conflict ("name")
|
|
144
|
+
* do update set "species" = $3
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* You can provide the name of the constraint instead of a column name:
|
|
148
|
+
*
|
|
149
|
+
* ```ts
|
|
150
|
+
* await db
|
|
151
|
+
* .insertInto('pet')
|
|
152
|
+
* .values({
|
|
153
|
+
* id: db.generated,
|
|
154
|
+
* name: 'Catto',
|
|
155
|
+
* species: 'cat',
|
|
156
|
+
* })
|
|
157
|
+
* .onConflict((oc) => oc
|
|
158
|
+
* .constraint('pet_name_key')
|
|
159
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
160
|
+
* )
|
|
161
|
+
* .execute()
|
|
162
|
+
* .execute()
|
|
163
|
+
* ```
|
|
164
|
+
*
|
|
165
|
+
* The generated SQL (PostgreSQL):
|
|
166
|
+
*
|
|
167
|
+
* ```sql
|
|
168
|
+
* insert into "pet" ("name", "species")
|
|
169
|
+
* values ($1, $2)
|
|
170
|
+
* on conflict on constraint "pet_name_key"
|
|
171
|
+
* do update set "species" = $3
|
|
172
|
+
* ```
|
|
173
|
+
*
|
|
174
|
+
* You can also specify an expression as the conflict target in case
|
|
175
|
+
* the unique index is an expression index:
|
|
176
|
+
*
|
|
177
|
+
* ```ts
|
|
178
|
+
* await db
|
|
179
|
+
* .insertInto('pet')
|
|
180
|
+
* .values({
|
|
181
|
+
* id: db.generated,
|
|
182
|
+
* name: 'Catto',
|
|
183
|
+
* species: 'cat',
|
|
184
|
+
* })
|
|
185
|
+
* .onConflict((oc) => oc
|
|
186
|
+
* .expression(db.raw('lower(name)'))
|
|
187
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
188
|
+
* )
|
|
189
|
+
* .execute()
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* The generated SQL (PostgreSQL):
|
|
193
|
+
*
|
|
194
|
+
* ```sql
|
|
195
|
+
* insert into "pet" ("name", "species")
|
|
196
|
+
* values ($1, $2)
|
|
197
|
+
* on conflict (lower(name))
|
|
198
|
+
* do update set "species" = $3
|
|
199
|
+
* ```
|
|
200
|
+
*
|
|
201
|
+
* You can add a filter for the update statement like this:
|
|
202
|
+
*
|
|
203
|
+
* ```ts
|
|
204
|
+
* await db
|
|
205
|
+
* .insertInto('pet')
|
|
206
|
+
* .values({
|
|
207
|
+
* id: db.generated,
|
|
208
|
+
* name: 'Catto',
|
|
209
|
+
* species: 'cat',
|
|
210
|
+
* })
|
|
211
|
+
* .onConflict((oc) => oc
|
|
212
|
+
* .column('name')
|
|
213
|
+
* .doUpdateSet({ species: 'hamster' })
|
|
214
|
+
* .where('excluded.name', '!=', 'Catto'')
|
|
215
|
+
* )
|
|
216
|
+
* .execute()
|
|
217
|
+
* ```
|
|
218
|
+
*
|
|
219
|
+
* The generated SQL (PostgreSQL):
|
|
220
|
+
*
|
|
221
|
+
* ```sql
|
|
222
|
+
* insert into "pet" ("name", "species")
|
|
223
|
+
* values ($1, $2)
|
|
224
|
+
* on conflict ("name")
|
|
225
|
+
* do update set "species" = $3
|
|
226
|
+
* where "excluded"."name" != $4
|
|
227
|
+
* ```
|
|
228
|
+
*
|
|
229
|
+
* You can create an `on conflict do nothing` clauses like this:
|
|
230
|
+
*
|
|
231
|
+
* ```ts
|
|
232
|
+
* await db
|
|
233
|
+
* .insertInto('pet')
|
|
234
|
+
* .values({
|
|
235
|
+
* id: db.generated,
|
|
236
|
+
* name: 'Catto',
|
|
237
|
+
* species: 'cat',
|
|
238
|
+
* })
|
|
239
|
+
* .onConflict((oc) => oc
|
|
240
|
+
* .column('name')
|
|
241
|
+
* .doNothing()
|
|
242
|
+
* )
|
|
243
|
+
* .execute()
|
|
244
|
+
* ```
|
|
245
|
+
*
|
|
246
|
+
* The generated SQL (PostgreSQL):
|
|
247
|
+
*
|
|
248
|
+
* ```sql
|
|
249
|
+
* insert into "pet" ("name", "species")
|
|
250
|
+
* values ($1, $2)
|
|
251
|
+
* on conflict ("name") do nothing
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
onConflict(callback) {
|
|
123
255
|
return new InsertQueryBuilder({
|
|
124
256
|
...this.#props,
|
|
125
257
|
queryNode: InsertQueryNode.cloneWith(this.#props.queryNode, {
|
|
126
|
-
onConflict:
|
|
258
|
+
onConflict: callback(new OnConflictBuilder({
|
|
259
|
+
onConflictNode: OnConflictNode.create(),
|
|
260
|
+
parseContext: this.#props.parseContext,
|
|
261
|
+
})).toOperationNode(),
|
|
127
262
|
}),
|
|
128
263
|
});
|
|
129
264
|
}
|
|
@@ -134,7 +269,7 @@ export class InsertQueryBuilder {
|
|
|
134
269
|
* a duplicate value in a unique index or primary key, an update of the old row occurs.
|
|
135
270
|
*
|
|
136
271
|
* This is only implemented by some dialects like MySQL. On most dialects you should
|
|
137
|
-
* use {@link
|
|
272
|
+
* use {@link onConflict} instead.
|
|
138
273
|
*
|
|
139
274
|
* ### Examples
|
|
140
275
|
*
|
|
@@ -177,9 +312,9 @@ export class InsertQueryBuilder {
|
|
|
177
312
|
* .selectAll()
|
|
178
313
|
* .call((qb) => {
|
|
179
314
|
* if (something) {
|
|
180
|
-
* return qb.
|
|
315
|
+
* return qb.onConflict((oc) => oc.column('something').doNothing())
|
|
181
316
|
* } else {
|
|
182
|
-
* return qb.
|
|
317
|
+
* return qb.onConflict((oc) => oc.column('something_else').doNothing())
|
|
183
318
|
* }
|
|
184
319
|
* })
|
|
185
320
|
* .execute()
|