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