kysely 0.23.1 → 0.23.3

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 (70) hide show
  1. package/README.md +5 -0
  2. package/dist/cjs/dialect/mysql/mysql-introspector.js +1 -1
  3. package/dist/cjs/dialect/postgres/postgres-introspector.js +1 -1
  4. package/dist/cjs/dialect/sqlite/sqlite-introspector.js +1 -1
  5. package/dist/cjs/index.d.ts +1 -1
  6. package/dist/cjs/operation-node/on-conflict-node.d.ts +2 -0
  7. package/dist/cjs/operation-node/on-conflict-node.js +12 -0
  8. package/dist/cjs/operation-node/query-node.d.ts +1 -0
  9. package/dist/cjs/operation-node/query-node.js +6 -0
  10. package/dist/cjs/operation-node/select-query-node.d.ts +4 -0
  11. package/dist/cjs/operation-node/select-query-node.js +24 -0
  12. package/dist/cjs/parser/binary-operation-parser.d.ts +1 -1
  13. package/dist/cjs/parser/update-set-parser.d.ts +3 -3
  14. package/dist/cjs/parser/update-set-parser.js +2 -2
  15. package/dist/cjs/query-builder/delete-query-builder.d.ts +63 -1
  16. package/dist/cjs/query-builder/delete-query-builder.js +51 -1
  17. package/dist/cjs/query-builder/insert-query-builder.d.ts +46 -3
  18. package/dist/cjs/query-builder/insert-query-builder.js +45 -1
  19. package/dist/cjs/query-builder/on-conflict-builder.d.ts +40 -2
  20. package/dist/cjs/query-builder/on-conflict-builder.js +12 -0
  21. package/dist/cjs/query-builder/select-query-builder.d.ts +138 -1
  22. package/dist/cjs/query-builder/select-query-builder.js +146 -1
  23. package/dist/cjs/query-builder/update-query-builder.d.ts +67 -3
  24. package/dist/cjs/query-builder/update-query-builder.js +53 -1
  25. package/dist/cjs/query-builder/where-interface.d.ts +19 -0
  26. package/dist/cjs/schema/alter-table-add-foreign-key-constraint-builder.d.ts +23 -0
  27. package/dist/cjs/schema/alter-table-add-foreign-key-constraint-builder.js +38 -0
  28. package/dist/cjs/schema/alter-table-builder.d.ts +3 -35
  29. package/dist/cjs/schema/alter-table-builder.js +10 -102
  30. package/dist/cjs/schema/alter-table-drop-constraint-builder.d.ts +21 -0
  31. package/dist/cjs/schema/alter-table-drop-constraint-builder.js +54 -0
  32. package/dist/cjs/schema/alter-table-executor.d.ts +18 -0
  33. package/dist/cjs/schema/alter-table-executor.js +22 -0
  34. package/dist/cjs/util/type-error.d.ts +3 -0
  35. package/dist/cjs/util/type-error.js +2 -0
  36. package/dist/esm/dialect/mysql/mysql-introspector.js +1 -1
  37. package/dist/esm/dialect/postgres/postgres-introspector.js +1 -1
  38. package/dist/esm/dialect/sqlite/sqlite-introspector.js +1 -1
  39. package/dist/esm/index.d.ts +1 -1
  40. package/dist/esm/operation-node/on-conflict-node.d.ts +2 -0
  41. package/dist/esm/operation-node/on-conflict-node.js +12 -0
  42. package/dist/esm/operation-node/query-node.d.ts +1 -0
  43. package/dist/esm/operation-node/query-node.js +6 -0
  44. package/dist/esm/operation-node/select-query-node.d.ts +4 -0
  45. package/dist/esm/operation-node/select-query-node.js +24 -0
  46. package/dist/esm/parser/binary-operation-parser.d.ts +1 -1
  47. package/dist/esm/parser/update-set-parser.d.ts +3 -3
  48. package/dist/esm/parser/update-set-parser.js +2 -2
  49. package/dist/esm/query-builder/delete-query-builder.d.ts +63 -1
  50. package/dist/esm/query-builder/delete-query-builder.js +51 -1
  51. package/dist/esm/query-builder/insert-query-builder.d.ts +46 -3
  52. package/dist/esm/query-builder/insert-query-builder.js +45 -1
  53. package/dist/esm/query-builder/on-conflict-builder.d.ts +40 -2
  54. package/dist/esm/query-builder/on-conflict-builder.js +12 -0
  55. package/dist/esm/query-builder/select-query-builder.d.ts +138 -1
  56. package/dist/esm/query-builder/select-query-builder.js +146 -1
  57. package/dist/esm/query-builder/update-query-builder.d.ts +67 -3
  58. package/dist/esm/query-builder/update-query-builder.js +54 -2
  59. package/dist/esm/query-builder/where-interface.d.ts +19 -0
  60. package/dist/esm/schema/alter-table-add-foreign-key-constraint-builder.d.ts +23 -0
  61. package/dist/esm/schema/alter-table-add-foreign-key-constraint-builder.js +35 -0
  62. package/dist/esm/schema/alter-table-builder.d.ts +3 -35
  63. package/dist/esm/schema/alter-table-builder.js +4 -93
  64. package/dist/esm/schema/alter-table-drop-constraint-builder.d.ts +21 -0
  65. package/dist/esm/schema/alter-table-drop-constraint-builder.js +51 -0
  66. package/dist/esm/schema/alter-table-executor.d.ts +18 -0
  67. package/dist/esm/schema/alter-table-executor.js +19 -0
  68. package/dist/esm/util/type-error.d.ts +3 -0
  69. package/dist/esm/util/type-error.js +2 -0
  70. package/package.json +2 -2
package/README.md CHANGED
@@ -37,6 +37,7 @@ You can find a more thorough introduction [here](https://www.jakso.me/blog/kysel
37
37
  - [Installation](#installation)
38
38
  - [3rd party dialects](#3rd-party-dialects)
39
39
  - [Minimal example](#minimal-example)
40
+ - [Playground](#playground)
40
41
  - [Generating types](#generating-types)
41
42
  - [Query examples](#query-examples)
42
43
  - [Select queries](#select-queries)
@@ -180,6 +181,10 @@ type InsertablePerson = Insertable<PersonTable>
180
181
  type UpdateablePerson = Updateable<PersonTable>
181
182
  ```
182
183
 
184
+ # Playground
185
+
186
+ [@wirekang](https://github.com/wirekang) has created a [playground for Kysely](https://wirekang.github.io/kysely-playground/#eyJkaWFsZWN0IjoibXlzcWwiLCJ0cyI6ImludGVyZmFjZSBEQiB7XG4gIHVzZXI6IFVzZXJUYWJsZVxufVxuXG5pbnRlcmZhY2UgVXNlclRhYmxlIHtcbiAgaWQ6IEdlbmVyYXRlZDxzdHJpbmc+XG4gIGZpcnN0X25hbWU6IHN0cmluZyB8IG51bGxcbiAgbGFzdF9uYW1lOiBzdHJpbmcgfCBudWxsXG4gIGNyZWF0ZWRfYXQ6IEdlbmVyYXRlZDxEYXRlPlxufVxuXG5yZXN1bHQgPSBreXNlbHlcbiAgLnNlbGVjdEZyb20oXCJ1c2VyXCIpXG4gIC5zZWxlY3RBbGwoKVxuICAub3JkZXJCeShcImNyZWF0ZWRfYXRcIikifQ==). You can use to quickly test stuff out and for creating code examples for your issues, PRs and discord messages.
187
+
183
188
  # Generating types
184
189
 
185
190
  If you want to generate the table types automatically from the database schema please
@@ -31,7 +31,7 @@ class MysqlIntrospector {
31
31
  ])
32
32
  .where('table_schema', '=', (0, sql_js_1.sql) `database()`)
33
33
  .orderBy('table_name')
34
- .orderBy('column_name')
34
+ .orderBy('ordinal_position')
35
35
  .castTo();
36
36
  if (!options.withInternalKyselyTables) {
37
37
  query = query
@@ -57,7 +57,7 @@ class PostgresIntrospector {
57
57
  .where('a.attisdropped', '!=', true)
58
58
  .orderBy('ns.nspname')
59
59
  .orderBy('c.relname')
60
- .orderBy('a.attname')
60
+ .orderBy('a.attnum')
61
61
  .castTo();
62
62
  if (!options.withInternalKyselyTables) {
63
63
  query = query
@@ -52,7 +52,7 @@ class SqliteIntrospector {
52
52
  const columns = await db
53
53
  .selectFrom((0, sql_js_1.sql) `pragma_table_info(${table})`.as('table_info'))
54
54
  .select(['name', 'type', 'notnull', 'dflt_value'])
55
- .orderBy('name')
55
+ .orderBy('cid')
56
56
  .execute();
57
57
  return {
58
58
  name: table,
@@ -172,7 +172,7 @@ export { ValueExpression, ValueExpressionOrList, } from './parser/value-parser.j
172
172
  export { TableExpression, TableExpressionOrList, } from './parser/table-parser.js';
173
173
  export { JoinReferenceExpression, JoinCallbackExpression, } from './parser/join-parser.js';
174
174
  export { InsertObject } from './parser/insert-values-parser.js';
175
- export { MutationObject } from './parser/update-set-parser.js';
175
+ export { UpdateObject } from './parser/update-set-parser.js';
176
176
  export { OrderByExpression, OrderByDirectionExpression, } from './parser/order-by-parser.js';
177
177
  export { ComparisonOperatorExpression, OperandValueExpression, OperandValueExpressionOrList, } from './parser/binary-operation-parser.js';
178
178
  export { ExistsExpression } from './parser/unary-operation-parser.js';
@@ -25,4 +25,6 @@ export declare const OnConflictNode: Readonly<{
25
25
  cloneWithIndexOrWhere(node: OnConflictNode, operation: OperationNode): OnConflictNode;
26
26
  cloneWithUpdateWhere(node: OnConflictNode, operation: OperationNode): OnConflictNode;
27
27
  cloneWithUpdateOrWhere(node: OnConflictNode, operation: OperationNode): OnConflictNode;
28
+ cloneWithoutIndexWhere(node: OnConflictNode): OnConflictNode;
29
+ cloneWithoutUpdateWhere(node: OnConflictNode): OnConflictNode;
28
30
  }>;
@@ -53,4 +53,16 @@ exports.OnConflictNode = (0, object_utils_js_1.freeze)({
53
53
  : where_node_js_1.WhereNode.create(operation),
54
54
  });
55
55
  },
56
+ cloneWithoutIndexWhere(node) {
57
+ return (0, object_utils_js_1.freeze)({
58
+ ...node,
59
+ indexWhere: undefined,
60
+ });
61
+ },
62
+ cloneWithoutUpdateWhere(node) {
63
+ return (0, object_utils_js_1.freeze)({
64
+ ...node,
65
+ updateWhere: undefined,
66
+ });
67
+ },
56
68
  });
@@ -17,4 +17,5 @@ export declare const QueryNode: Readonly<{
17
17
  cloneWithOrWhere<T_1 extends FilterableQueryNode>(node: T_1, operation: OperationNode): T_1;
18
18
  cloneWithJoin<T_2 extends FilterableQueryNode>(node: T_2, join: JoinNode): T_2;
19
19
  cloneWithReturning<T_3 extends MutatingQueryNode>(node: T_3, selections: ReadonlyArray<SelectionNode>): T_3;
20
+ cloneWithoutWhere<T_4 extends FilterableQueryNode>(node: T_4): T_4;
20
21
  }>;
@@ -48,4 +48,10 @@ exports.QueryNode = (0, object_utils_js_1.freeze)({
48
48
  : returning_node_js_1.ReturningNode.create(selections),
49
49
  });
50
50
  },
51
+ cloneWithoutWhere(node) {
52
+ return (0, object_utils_js_1.freeze)({
53
+ ...node,
54
+ where: undefined,
55
+ });
56
+ },
51
57
  });
@@ -50,4 +50,8 @@ export declare const SelectQueryNode: Readonly<{
50
50
  cloneWithOrHaving(selectNode: SelectQueryNode, operation: OperationNode): SelectQueryNode;
51
51
  cloneWithSetOperation(selectNode: SelectQueryNode, setOperation: SetOperationNode): SelectQueryNode;
52
52
  cloneWithExplain(selectNode: SelectQueryNode, explain: ExplainNode): SelectQueryNode;
53
+ cloneWithoutSelections(select: SelectQueryNode): SelectQueryNode;
54
+ cloneWithoutLimit(select: SelectQueryNode): SelectQueryNode;
55
+ cloneWithoutOffset(select: SelectQueryNode): SelectQueryNode;
56
+ cloneWithoutOrderBy(select: SelectQueryNode): SelectQueryNode;
53
57
  }>;
@@ -110,4 +110,28 @@ exports.SelectQueryNode = (0, object_utils_js_1.freeze)({
110
110
  explain,
111
111
  });
112
112
  },
113
+ cloneWithoutSelections(select) {
114
+ return (0, object_utils_js_1.freeze)({
115
+ ...select,
116
+ selections: [],
117
+ });
118
+ },
119
+ cloneWithoutLimit(select) {
120
+ return (0, object_utils_js_1.freeze)({
121
+ ...select,
122
+ limit: undefined,
123
+ });
124
+ },
125
+ cloneWithoutOffset(select) {
126
+ return (0, object_utils_js_1.freeze)({
127
+ ...select,
128
+ offset: undefined,
129
+ });
130
+ },
131
+ cloneWithoutOrderBy(select) {
132
+ return (0, object_utils_js_1.freeze)({
133
+ ...select,
134
+ orderBy: undefined,
135
+ });
136
+ },
113
137
  });
@@ -7,7 +7,7 @@ import { HavingInterface } from '../query-builder/having-interface.js';
7
7
  import { OperationNode } from '../operation-node/operation-node.js';
8
8
  import { Expression } from '../expression/expression.js';
9
9
  export declare type OperandValueExpression<DB, TB extends keyof DB, RE> = ValueExpression<DB, TB, ExtractTypeFromReferenceExpression<DB, TB, RE>>;
10
- export declare type OperandValueExpressionOrList<DB, TB extends keyof DB, RE> = ValueExpressionOrList<DB, TB, ExtractTypeFromReferenceExpression<DB, TB, RE>>;
10
+ export declare type OperandValueExpressionOrList<DB, TB extends keyof DB, RE> = ValueExpressionOrList<DB, TB, ExtractTypeFromReferenceExpression<DB, TB, RE> | null>;
11
11
  export declare type WhereGrouper<DB, TB extends keyof DB> = (qb: WhereInterface<DB, TB>) => WhereInterface<DB, TB>;
12
12
  export declare type HavingGrouper<DB, TB extends keyof DB> = (qb: HavingInterface<DB, TB>) => HavingInterface<DB, TB>;
13
13
  export declare type ComparisonOperatorExpression = ComparisonOperator | Expression<any>;
@@ -1,7 +1,7 @@
1
1
  import { ColumnUpdateNode } from '../operation-node/column-update-node.js';
2
2
  import { UpdateKeys, UpdateType } from '../util/column-type.js';
3
3
  import { ValueExpression } from './value-parser.js';
4
- export declare type MutationObject<DB, TB extends keyof DB, TM extends keyof DB> = {
5
- [C in UpdateKeys<DB[TM]>]?: ValueExpression<DB, TB, UpdateType<DB[TM][C]>> | undefined;
4
+ export declare type UpdateObject<DB, TB extends keyof DB, UT extends keyof DB> = {
5
+ [C in UpdateKeys<DB[UT]>]?: ValueExpression<DB, TB, UpdateType<DB[UT][C]>> | undefined;
6
6
  };
7
- export declare function parseUpdateObject(row: MutationObject<any, any, any>): ReadonlyArray<ColumnUpdateNode>;
7
+ export declare function parseUpdateObject(update: UpdateObject<any, any, any>): ReadonlyArray<ColumnUpdateNode>;
@@ -4,8 +4,8 @@ exports.parseUpdateObject = void 0;
4
4
  const column_node_js_1 = require("../operation-node/column-node.js");
5
5
  const column_update_node_js_1 = require("../operation-node/column-update-node.js");
6
6
  const value_parser_js_1 = require("./value-parser.js");
7
- function parseUpdateObject(row) {
8
- return Object.entries(row)
7
+ function parseUpdateObject(update) {
8
+ return Object.entries(update)
9
9
  .filter(([_, value]) => value !== undefined)
10
10
  .map(([key, value]) => {
11
11
  return column_update_node_js_1.ColumnUpdateNode.create(column_node_js_1.ColumnNode.create(key), (0, value_parser_js_1.parseValueExpression)(value));
@@ -21,6 +21,7 @@ import { Explainable, ExplainFormat } from '../util/explainable.js';
21
21
  import { AliasedExpression, Expression } from '../expression/expression.js';
22
22
  import { ComparisonOperatorExpression, OperandValueExpressionOrList, WhereGrouper } from '../parser/binary-operation-parser.js';
23
23
  import { ExistsExpression } from '../parser/unary-operation-parser.js';
24
+ import { KyselyTypeError } from '../util/type-error.js';
24
25
  export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements WhereInterface<DB, TB>, ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable<O>, Explainable {
25
26
  #private;
26
27
  constructor(props: DeleteQueryBuilderProps);
@@ -408,6 +409,25 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
408
409
  * Just like {@link whereExists} but creates an `or not exists` clause.
409
410
  */
410
411
  orWhereNotExists(arg: ExistsExpression<DB, TB>): DeleteQueryBuilder<DB, TB, O>;
412
+ /**
413
+ * Clears all where clauses from the query.
414
+ *
415
+ * ### Examples
416
+ *
417
+ * ```ts
418
+ * db.selectFrom('person')
419
+ * .selectAll()
420
+ * .where('id','=',42)
421
+ * .clearWhere()
422
+ * ```
423
+ *
424
+ * The generated SQL(PostgreSQL):
425
+ *
426
+ * ```sql
427
+ * select * from "person"
428
+ * ```
429
+ */
430
+ clearWhere(): DeleteQueryBuilder<DB, TB, O>;
411
431
  /**
412
432
  * Joins another table to the query using an inner join.
413
433
  *
@@ -662,7 +682,7 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
662
682
  *
663
683
  * ### Examples
664
684
  *
665
- * The next example uses a helper funtion `log` to log a query:
685
+ * The next example uses a helper function `log` to log a query:
666
686
  *
667
687
  * ```ts
668
688
  * function log<T extends Compilable>(qb: T): T {
@@ -719,6 +739,48 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
719
739
  * don't support your use case.
720
740
  */
721
741
  castTo<T>(): DeleteQueryBuilder<DB, TB, T>;
742
+ /**
743
+ * Asserts that query's output row type equals the given type `T`.
744
+ *
745
+ * This method can be used to simplify excessively complex types to make typescript happy
746
+ * and much faster.
747
+ *
748
+ * Kysely uses complex type magic to achieve its type safety. This complexity is sometimes too much
749
+ * for typescript and you get errors like this:
750
+ *
751
+ * ```
752
+ * error TS2589: Type instantiation is excessively deep and possibly infinite.
753
+ * ```
754
+ *
755
+ * In these case you can often use this method to help typescript a little bit. When you use this
756
+ * method to assert the output type of a query, Kysely can drop the complex output type that
757
+ * consists of multiple nested helper types and replace it with the simple asserted type.
758
+ *
759
+ * Using this method doesn't reduce type safety at all. You have to pass in a type that is
760
+ * structurally equal to the current type.
761
+ *
762
+ * ### Examples
763
+ *
764
+ * ```ts
765
+ * const result = await db
766
+ * .with('deleted_person', (qb) => qb
767
+ * .deleteFrom('person')
768
+ * .where('id', '=', person.id)
769
+ * .returning('first_name')
770
+ * .assertType<{ first_name: string }>()
771
+ * )
772
+ * .with('deleted_pet', (qb) => qb
773
+ * .deleteFrom('pet')
774
+ * .where('owner_id', '=', person.id)
775
+ * .returning(['name as pet_name', 'species'])
776
+ * .assertType<{ pet_name: string, species: Species }>()
777
+ * )
778
+ * .selectFrom(['deleted_person', 'deleted_pet'])
779
+ * .selectAll()
780
+ * .executeTakeFirstOrThrow()
781
+ * ```
782
+ */
783
+ assertType<T extends O>(): O extends T ? DeleteQueryBuilder<DB, TB, T> : KyselyTypeError<`assertType() call failed: The type passed in is not equal to the output type of the query.`>;
722
784
  /**
723
785
  * Returns a copy of this DeleteQueryBuilder instance with the given plugin installed.
724
786
  */
@@ -67,6 +67,12 @@ class DeleteQueryBuilder {
67
67
  queryNode: query_node_js_1.QueryNode.cloneWithOrWhere(this.#props.queryNode, (0, unary_operation_parser_js_1.parseNotExists)(arg)),
68
68
  });
69
69
  }
70
+ clearWhere() {
71
+ return new DeleteQueryBuilder({
72
+ ...this.#props,
73
+ queryNode: query_node_js_1.QueryNode.cloneWithoutWhere(this.#props.queryNode),
74
+ });
75
+ }
70
76
  innerJoin(...args) {
71
77
  return new DeleteQueryBuilder({
72
78
  ...this.#props,
@@ -175,7 +181,7 @@ class DeleteQueryBuilder {
175
181
  *
176
182
  * ### Examples
177
183
  *
178
- * The next example uses a helper funtion `log` to log a query:
184
+ * The next example uses a helper function `log` to log a query:
179
185
  *
180
186
  * ```ts
181
187
  * function log<T extends Compilable>(qb: T): T {
@@ -243,6 +249,50 @@ class DeleteQueryBuilder {
243
249
  castTo() {
244
250
  return new DeleteQueryBuilder(this.#props);
245
251
  }
252
+ /**
253
+ * Asserts that query's output row type equals the given type `T`.
254
+ *
255
+ * This method can be used to simplify excessively complex types to make typescript happy
256
+ * and much faster.
257
+ *
258
+ * Kysely uses complex type magic to achieve its type safety. This complexity is sometimes too much
259
+ * for typescript and you get errors like this:
260
+ *
261
+ * ```
262
+ * error TS2589: Type instantiation is excessively deep and possibly infinite.
263
+ * ```
264
+ *
265
+ * In these case you can often use this method to help typescript a little bit. When you use this
266
+ * method to assert the output type of a query, Kysely can drop the complex output type that
267
+ * consists of multiple nested helper types and replace it with the simple asserted type.
268
+ *
269
+ * Using this method doesn't reduce type safety at all. You have to pass in a type that is
270
+ * structurally equal to the current type.
271
+ *
272
+ * ### Examples
273
+ *
274
+ * ```ts
275
+ * const result = await db
276
+ * .with('deleted_person', (qb) => qb
277
+ * .deleteFrom('person')
278
+ * .where('id', '=', person.id)
279
+ * .returning('first_name')
280
+ * .assertType<{ first_name: string }>()
281
+ * )
282
+ * .with('deleted_pet', (qb) => qb
283
+ * .deleteFrom('pet')
284
+ * .where('owner_id', '=', person.id)
285
+ * .returning(['name as pet_name', 'species'])
286
+ * .assertType<{ pet_name: string, species: Species }>()
287
+ * )
288
+ * .selectFrom(['deleted_person', 'deleted_pet'])
289
+ * .selectAll()
290
+ * .executeTakeFirstOrThrow()
291
+ * ```
292
+ */
293
+ assertType() {
294
+ return new DeleteQueryBuilder(this.#props);
295
+ }
246
296
  /**
247
297
  * Returns a copy of this DeleteQueryBuilder instance with the given plugin installed.
248
298
  */
@@ -4,7 +4,7 @@ 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
6
  import { MergePartial, SingleResultType } from '../util/type-utils.js';
7
- import { MutationObject } from '../parser/update-set-parser.js';
7
+ import { UpdateObject } 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';
@@ -18,6 +18,7 @@ import { OnConflictBuilder, OnConflictDoNothingBuilder, OnConflictUpdateBuilder
18
18
  import { Selectable } from '../util/column-type.js';
19
19
  import { Explainable, ExplainFormat } from '../util/explainable.js';
20
20
  import { Expression } from '../expression/expression.js';
21
+ import { KyselyTypeError } from '../util/type-error.js';
21
22
  export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable<O>, Explainable {
22
23
  #private;
23
24
  constructor(props: InsertQueryBuilderProps);
@@ -384,7 +385,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
384
385
  * .onDuplicateKeyUpdate({ species: 'hamster' })
385
386
  * ```
386
387
  */
387
- onDuplicateKeyUpdate(updates: MutationObject<DB, TB, TB>): InsertQueryBuilder<DB, TB, O>;
388
+ onDuplicateKeyUpdate(updates: UpdateObject<DB, TB, TB>): InsertQueryBuilder<DB, TB, O>;
388
389
  /**
389
390
  * Allows you to return data from modified rows.
390
391
  *
@@ -459,7 +460,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
459
460
  *
460
461
  * ### Examples
461
462
  *
462
- * The next example uses a helper funtion `log` to log a query:
463
+ * The next example uses a helper function `log` to log a query:
463
464
  *
464
465
  * ```ts
465
466
  * function log<T extends Compilable>(qb: T): T {
@@ -517,6 +518,48 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
517
518
  * don't support your use case.
518
519
  */
519
520
  castTo<T>(): InsertQueryBuilder<DB, TB, T>;
521
+ /**
522
+ * Asserts that query's output row type equals the given type `T`.
523
+ *
524
+ * This method can be used to simplify excessively complex types to make typescript happy
525
+ * and much faster.
526
+ *
527
+ * Kysely uses complex type magic to achieve its type safety. This complexity is sometimes too much
528
+ * for typescript and you get errors like this:
529
+ *
530
+ * ```
531
+ * error TS2589: Type instantiation is excessively deep and possibly infinite.
532
+ * ```
533
+ *
534
+ * In these case you can often use this method to help typescript a little bit. When you use this
535
+ * method to assert the output type of a query, Kysely can drop the complex output type that
536
+ * consists of multiple nested helper types and replace it with the simple asserted type.
537
+ *
538
+ * Using this method doesn't reduce type safety at all. You have to pass in a type that is
539
+ * structurally equal to the current type.
540
+ *
541
+ * ### Examples
542
+ *
543
+ * ```ts
544
+ * const result = await db
545
+ * .with('new_person', (qb) => qb
546
+ * .insertInto('person')
547
+ * .values(person)
548
+ * .returning('id')
549
+ * .assertType<{ id: string }>()
550
+ * )
551
+ * .with('new_pet', (qb) => qb
552
+ * .insertInto('pet')
553
+ * .values({ owner_id: (eb) => eb.selectFrom('new_person').select('id'), ...pet })
554
+ * .returning(['name as pet_name', 'species'])
555
+ * .assertType<{ pet_name: string, species: Species }>()
556
+ * )
557
+ * .selectFrom(['new_person', 'new_pet'])
558
+ * .selectAll()
559
+ * .executeTakeFirstOrThrow()
560
+ * ```
561
+ */
562
+ assertType<T extends O>(): O extends T ? InsertQueryBuilder<DB, TB, T> : KyselyTypeError<`assertType() call failed: The type passed in is not equal to the output type of the query.`>;
520
563
  /**
521
564
  * Returns a copy of this InsertQueryBuilder instance with the given plugin installed.
522
565
  */
@@ -324,7 +324,7 @@ class InsertQueryBuilder {
324
324
  *
325
325
  * ### Examples
326
326
  *
327
- * The next example uses a helper funtion `log` to log a query:
327
+ * The next example uses a helper function `log` to log a query:
328
328
  *
329
329
  * ```ts
330
330
  * function log<T extends Compilable>(qb: T): T {
@@ -393,6 +393,50 @@ class InsertQueryBuilder {
393
393
  castTo() {
394
394
  return new InsertQueryBuilder(this.#props);
395
395
  }
396
+ /**
397
+ * Asserts that query's output row type equals the given type `T`.
398
+ *
399
+ * This method can be used to simplify excessively complex types to make typescript happy
400
+ * and much faster.
401
+ *
402
+ * Kysely uses complex type magic to achieve its type safety. This complexity is sometimes too much
403
+ * for typescript and you get errors like this:
404
+ *
405
+ * ```
406
+ * error TS2589: Type instantiation is excessively deep and possibly infinite.
407
+ * ```
408
+ *
409
+ * In these case you can often use this method to help typescript a little bit. When you use this
410
+ * method to assert the output type of a query, Kysely can drop the complex output type that
411
+ * consists of multiple nested helper types and replace it with the simple asserted type.
412
+ *
413
+ * Using this method doesn't reduce type safety at all. You have to pass in a type that is
414
+ * structurally equal to the current type.
415
+ *
416
+ * ### Examples
417
+ *
418
+ * ```ts
419
+ * const result = await db
420
+ * .with('new_person', (qb) => qb
421
+ * .insertInto('person')
422
+ * .values(person)
423
+ * .returning('id')
424
+ * .assertType<{ id: string }>()
425
+ * )
426
+ * .with('new_pet', (qb) => qb
427
+ * .insertInto('pet')
428
+ * .values({ owner_id: (eb) => eb.selectFrom('new_person').select('id'), ...pet })
429
+ * .returning(['name as pet_name', 'species'])
430
+ * .assertType<{ pet_name: string, species: Species }>()
431
+ * )
432
+ * .selectFrom(['new_person', 'new_pet'])
433
+ * .selectAll()
434
+ * .executeTakeFirstOrThrow()
435
+ * ```
436
+ */
437
+ assertType() {
438
+ return new InsertQueryBuilder(this.#props);
439
+ }
396
440
  /**
397
441
  * Returns a copy of this InsertQueryBuilder instance with the given plugin installed.
398
442
  */
@@ -4,7 +4,7 @@ import { OperationNodeSource } from '../operation-node/operation-node-source.js'
4
4
  import { ComparisonOperatorExpression, OperandValueExpressionOrList, WhereGrouper } from '../parser/binary-operation-parser.js';
5
5
  import { ExpressionOrFactory } from '../parser/expression-parser.js';
6
6
  import { ReferenceExpression } from '../parser/reference-parser.js';
7
- import { MutationObject } from '../parser/update-set-parser.js';
7
+ import { UpdateObject } from '../parser/update-set-parser.js';
8
8
  import { AnyColumn } from '../util/type-utils.js';
9
9
  import { WhereInterface } from './where-interface.js';
10
10
  export declare class OnConflictBuilder<DB, TB extends keyof DB> implements WhereInterface<DB, TB> {
@@ -339,6 +339,25 @@ export declare class OnConflictBuilder<DB, TB extends keyof DB> implements Where
339
339
  * See {@link WhereInterface.orWhereNotExists} for more info.
340
340
  */
341
341
  orWhereNotExists(arg: ExpressionOrFactory<DB, TB, any>): OnConflictBuilder<DB, TB>;
342
+ /**
343
+ * Clears all where clauses from the query.
344
+ *
345
+ * ### Examples
346
+ *
347
+ * ```ts
348
+ * db.selectFrom('person')
349
+ * .selectAll()
350
+ * .where('id','=',42)
351
+ * .clearWhere()
352
+ * ```
353
+ *
354
+ * The generated SQL(PostgreSQL):
355
+ *
356
+ * ```sql
357
+ * select * from "person"
358
+ * ```
359
+ */
360
+ clearWhere(): OnConflictBuilder<DB, TB>;
342
361
  /**
343
362
  * Adds the "do nothing" conflict action.
344
363
  *
@@ -387,7 +406,7 @@ export declare class OnConflictBuilder<DB, TB extends keyof DB> implements Where
387
406
  * do update set "first_name" = $3
388
407
  * ```
389
408
  */
390
- doUpdateSet(updates: MutationObject<OnConflictDatabase<DB, TB>, OnConflictTables<TB>, OnConflictTables<TB>>): OnConflictUpdateBuilder<OnConflictDatabase<DB, TB>, OnConflictTables<TB>>;
409
+ doUpdateSet(updates: UpdateObject<OnConflictDatabase<DB, TB>, OnConflictTables<TB>, OnConflictTables<TB>>): OnConflictUpdateBuilder<OnConflictDatabase<DB, TB>, OnConflictTables<TB>>;
391
410
  }
392
411
  export interface OnConflictBuilderProps {
393
412
  readonly onConflictNode: OnConflictNode;
@@ -703,5 +722,24 @@ export declare class OnConflictUpdateBuilder<DB, TB extends keyof DB> implements
703
722
  * See {@link WhereInterface.orWhereNotExists} for more info.
704
723
  */
705
724
  orWhereNotExists(arg: ExpressionOrFactory<DB, TB, any>): OnConflictUpdateBuilder<DB, TB>;
725
+ /**
726
+ * Clears all where clauses from the query.
727
+ *
728
+ * ### Examples
729
+ *
730
+ * ```ts
731
+ * db.selectFrom('person')
732
+ * .selectAll()
733
+ * .where('id','=',42)
734
+ * .clearWhere()
735
+ * ```
736
+ *
737
+ * The generated SQL(PostgreSQL):
738
+ *
739
+ * ```sql
740
+ * select * from "person"
741
+ * ```
742
+ */
743
+ clearWhere(): OnConflictUpdateBuilder<DB, TB>;
706
744
  toOperationNode(): OnConflictNode;
707
745
  }
@@ -156,6 +156,12 @@ class OnConflictBuilder {
156
156
  onConflictNode: on_conflict_node_js_1.OnConflictNode.cloneWithIndexOrWhere(this.#props.onConflictNode, (0, unary_operation_parser_js_1.parseNotExists)(arg)),
157
157
  });
158
158
  }
159
+ clearWhere() {
160
+ return new OnConflictBuilder({
161
+ ...this.#props,
162
+ onConflictNode: on_conflict_node_js_1.OnConflictNode.cloneWithoutIndexWhere(this.#props.onConflictNode),
163
+ });
164
+ }
159
165
  /**
160
166
  * Adds the "do nothing" conflict action.
161
167
  *
@@ -316,6 +322,12 @@ class OnConflictUpdateBuilder {
316
322
  onConflictNode: on_conflict_node_js_1.OnConflictNode.cloneWithUpdateOrWhere(this.#props.onConflictNode, (0, unary_operation_parser_js_1.parseNotExists)(arg)),
317
323
  });
318
324
  }
325
+ clearWhere() {
326
+ return new OnConflictUpdateBuilder({
327
+ ...this.#props,
328
+ onConflictNode: on_conflict_node_js_1.OnConflictNode.cloneWithoutUpdateWhere(this.#props.onConflictNode),
329
+ });
330
+ }
319
331
  toOperationNode() {
320
332
  return this.#props.onConflictNode;
321
333
  }