kysely 0.26.2 → 0.27.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 (215) hide show
  1. package/dist/cjs/dialect/dialect-adapter-base.d.ts +8 -0
  2. package/dist/cjs/dialect/dialect-adapter-base.js +3 -0
  3. package/dist/cjs/dialect/dialect-adapter.d.ts +8 -0
  4. package/dist/cjs/dialect/mssql/mssql-adapter.d.ts +78 -0
  5. package/dist/cjs/dialect/mssql/mssql-adapter.js +30 -0
  6. package/dist/cjs/dialect/mssql/mssql-dialect-config.d.ts +143 -0
  7. package/dist/cjs/dialect/mssql/mssql-dialect-config.js +2 -0
  8. package/dist/cjs/dialect/mssql/mssql-dialect.d.ts +70 -0
  9. package/dist/cjs/dialect/mssql/mssql-dialect.js +65 -0
  10. package/dist/cjs/dialect/mssql/mssql-driver.d.ts +54 -0
  11. package/dist/cjs/dialect/mssql/mssql-driver.js +268 -0
  12. package/dist/cjs/dialect/mssql/mssql-introspector.d.ts +20 -0
  13. package/dist/cjs/dialect/mssql/mssql-introspector.js +97 -0
  14. package/dist/cjs/dialect/mssql/mssql-query-compiler.d.ts +11 -0
  15. package/dist/cjs/dialect/mssql/mssql-query-compiler.js +64 -0
  16. package/dist/cjs/dialect/mysql/mysql-query-compiler.d.ts +2 -0
  17. package/dist/cjs/dialect/mysql/mysql-query-compiler.js +28 -0
  18. package/dist/cjs/dialect/postgres/postgres-driver.js +1 -1
  19. package/dist/cjs/dialect/sqlite/sqlite-adapter.d.ts +1 -1
  20. package/dist/cjs/dialect/sqlite/sqlite-adapter.js +2 -1
  21. package/dist/cjs/dialect/sqlite/sqlite-dialect-config.d.ts +1 -0
  22. package/dist/cjs/dialect/sqlite/sqlite-driver.js +15 -2
  23. package/dist/cjs/driver/driver.d.ts +1 -1
  24. package/dist/cjs/driver/driver.js +1 -0
  25. package/dist/cjs/driver/runtime-driver.js +5 -1
  26. package/dist/cjs/driver/single-connection-provider.js +7 -8
  27. package/dist/cjs/expression/expression-builder.d.ts +66 -73
  28. package/dist/cjs/expression/expression-builder.js +8 -17
  29. package/dist/cjs/expression/expression-wrapper.d.ts +8 -8
  30. package/dist/cjs/helpers/mssql.d.ts +152 -0
  31. package/dist/cjs/helpers/mssql.js +160 -0
  32. package/dist/cjs/helpers/mysql.d.ts +3 -3
  33. package/dist/cjs/helpers/sqlite.d.ts +3 -3
  34. package/dist/cjs/index.d.ts +8 -2
  35. package/dist/cjs/index.js +6 -0
  36. package/dist/cjs/migration/migrator.js +12 -16
  37. package/dist/cjs/operation-node/add-index-node.d.ts +21 -0
  38. package/dist/cjs/operation-node/add-index-node.js +31 -0
  39. package/dist/cjs/operation-node/alter-table-node.d.ts +5 -1
  40. package/dist/cjs/operation-node/column-definition-node.d.ts +1 -0
  41. package/dist/cjs/operation-node/column-update-node.d.ts +2 -3
  42. package/dist/cjs/operation-node/create-index-node.d.ts +1 -0
  43. package/dist/cjs/operation-node/create-table-node.d.ts +1 -0
  44. package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
  45. package/dist/cjs/operation-node/operation-node-transformer.js +17 -0
  46. package/dist/cjs/operation-node/operation-node-visitor.d.ts +2 -0
  47. package/dist/cjs/operation-node/operation-node-visitor.js +1 -0
  48. package/dist/cjs/operation-node/operation-node.d.ts +1 -1
  49. package/dist/cjs/operation-node/operator-node.d.ts +3 -3
  50. package/dist/cjs/operation-node/operator-node.js +2 -0
  51. package/dist/cjs/operation-node/unique-constraint-node.d.ts +4 -1
  52. package/dist/cjs/operation-node/unique-constraint-node.js +8 -1
  53. package/dist/cjs/parser/binary-operation-parser.js +1 -1
  54. package/dist/cjs/parser/coalesce-parser.d.ts +12 -2
  55. package/dist/cjs/parser/expression-parser.d.ts +2 -2
  56. package/dist/cjs/parser/reference-parser.d.ts +3 -2
  57. package/dist/cjs/parser/returning-parser.d.ts +2 -1
  58. package/dist/cjs/parser/select-parser.d.ts +20 -9
  59. package/dist/cjs/parser/update-set-parser.d.ts +5 -2
  60. package/dist/cjs/parser/update-set-parser.js +13 -3
  61. package/dist/cjs/parser/value-parser.d.ts +2 -2
  62. package/dist/cjs/query-builder/aggregate-function-builder.d.ts +4 -4
  63. package/dist/cjs/query-builder/case-builder.d.ts +5 -4
  64. package/dist/cjs/query-builder/delete-query-builder.d.ts +8 -6
  65. package/dist/cjs/query-builder/function-module.d.ts +98 -27
  66. package/dist/cjs/query-builder/function-module.js +17 -3
  67. package/dist/cjs/query-builder/having-interface.d.ts +3 -5
  68. package/dist/cjs/query-builder/insert-query-builder.d.ts +32 -12
  69. package/dist/cjs/query-builder/insert-query-builder.js +3 -3
  70. package/dist/cjs/query-builder/insert-result.d.ts +8 -1
  71. package/dist/cjs/query-builder/insert-result.js +8 -1
  72. package/dist/cjs/query-builder/json-path-builder.d.ts +3 -2
  73. package/dist/cjs/query-builder/json-path-builder.js +6 -3
  74. package/dist/cjs/query-builder/on-conflict-builder.d.ts +58 -16
  75. package/dist/cjs/query-builder/on-conflict-builder.js +1 -6
  76. package/dist/cjs/query-builder/over-builder.d.ts +2 -2
  77. package/dist/cjs/query-builder/returning-interface.d.ts +3 -2
  78. package/dist/cjs/query-builder/select-query-builder-expression.d.ts +20 -0
  79. package/dist/cjs/query-builder/select-query-builder-expression.js +2 -0
  80. package/dist/cjs/query-builder/select-query-builder.d.ts +48 -29
  81. package/dist/cjs/query-builder/select-query-builder.js +5 -2
  82. package/dist/cjs/query-builder/update-query-builder.d.ts +25 -7
  83. package/dist/cjs/query-builder/update-query-builder.js +2 -2
  84. package/dist/cjs/query-builder/where-interface.d.ts +3 -3
  85. package/dist/cjs/query-compiler/default-query-compiler.d.ts +9 -1
  86. package/dist/cjs/query-compiler/default-query-compiler.js +65 -12
  87. package/dist/cjs/query-creator.d.ts +5 -3
  88. package/dist/cjs/query-creator.js +0 -47
  89. package/dist/cjs/raw-builder/raw-builder.d.ts +0 -3
  90. package/dist/cjs/schema/alter-table-add-index-builder.d.ts +104 -0
  91. package/dist/cjs/schema/alter-table-add-index-builder.js +146 -0
  92. package/dist/cjs/schema/alter-table-builder.d.ts +41 -1
  93. package/dist/cjs/schema/alter-table-builder.js +59 -2
  94. package/dist/cjs/schema/column-definition-builder.d.ts +25 -0
  95. package/dist/cjs/schema/column-definition-builder.js +27 -0
  96. package/dist/cjs/schema/create-index-builder.d.ts +23 -0
  97. package/dist/cjs/schema/create-index-builder.js +30 -0
  98. package/dist/cjs/schema/create-table-builder.d.ts +28 -2
  99. package/dist/cjs/schema/create-table-builder.js +38 -3
  100. package/dist/cjs/schema/create-view-builder.d.ts +2 -2
  101. package/dist/cjs/schema/unique-constraint-builder.d.ts +14 -0
  102. package/dist/cjs/schema/unique-constraint-builder.js +24 -0
  103. package/dist/cjs/util/infer-result.d.ts +2 -1
  104. package/dist/cjs/util/log.js +5 -1
  105. package/dist/cjs/util/object-utils.d.ts +1 -1
  106. package/dist/cjs/util/type-utils.d.ts +27 -20
  107. package/dist/esm/dialect/dialect-adapter-base.d.ts +8 -0
  108. package/dist/esm/dialect/dialect-adapter-base.js +3 -0
  109. package/dist/esm/dialect/dialect-adapter.d.ts +8 -0
  110. package/dist/esm/dialect/mssql/mssql-adapter.d.ts +78 -0
  111. package/dist/esm/dialect/mssql/mssql-adapter.js +27 -0
  112. package/dist/esm/dialect/mssql/mssql-dialect-config.d.ts +143 -0
  113. package/dist/esm/dialect/mssql/mssql-dialect-config.js +2 -0
  114. package/dist/esm/dialect/mssql/mssql-dialect.d.ts +70 -0
  115. package/dist/esm/dialect/mssql/mssql-dialect.js +62 -0
  116. package/dist/esm/dialect/mssql/mssql-driver.d.ts +54 -0
  117. package/dist/esm/dialect/mssql/mssql-driver.js +265 -0
  118. package/dist/esm/dialect/mssql/mssql-introspector.d.ts +20 -0
  119. package/dist/esm/dialect/mssql/mssql-introspector.js +94 -0
  120. package/dist/esm/dialect/mssql/mssql-query-compiler.d.ts +11 -0
  121. package/dist/esm/dialect/mssql/mssql-query-compiler.js +61 -0
  122. package/dist/esm/dialect/mysql/mysql-query-compiler.d.ts +2 -0
  123. package/dist/esm/dialect/mysql/mysql-query-compiler.js +28 -0
  124. package/dist/esm/dialect/postgres/postgres-driver.js +1 -1
  125. package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +1 -1
  126. package/dist/esm/dialect/sqlite/sqlite-adapter.js +2 -1
  127. package/dist/esm/dialect/sqlite/sqlite-dialect-config.d.ts +1 -0
  128. package/dist/esm/dialect/sqlite/sqlite-driver.js +15 -2
  129. package/dist/esm/driver/driver.d.ts +1 -1
  130. package/dist/esm/driver/driver.js +1 -0
  131. package/dist/esm/driver/runtime-driver.js +5 -1
  132. package/dist/esm/driver/single-connection-provider.js +7 -8
  133. package/dist/esm/expression/expression-builder.d.ts +66 -73
  134. package/dist/esm/expression/expression-builder.js +10 -19
  135. package/dist/esm/expression/expression-wrapper.d.ts +8 -8
  136. package/dist/esm/helpers/mssql.d.ts +152 -0
  137. package/dist/esm/helpers/mssql.js +155 -0
  138. package/dist/esm/helpers/mysql.d.ts +3 -3
  139. package/dist/esm/helpers/sqlite.d.ts +3 -3
  140. package/dist/esm/index.d.ts +8 -2
  141. package/dist/esm/index.js +6 -0
  142. package/dist/esm/migration/migrator.js +12 -16
  143. package/dist/esm/operation-node/add-index-node.d.ts +21 -0
  144. package/dist/esm/operation-node/add-index-node.js +29 -0
  145. package/dist/esm/operation-node/alter-table-node.d.ts +5 -1
  146. package/dist/esm/operation-node/column-definition-node.d.ts +1 -0
  147. package/dist/esm/operation-node/column-update-node.d.ts +2 -3
  148. package/dist/esm/operation-node/create-index-node.d.ts +1 -0
  149. package/dist/esm/operation-node/create-table-node.d.ts +1 -0
  150. package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
  151. package/dist/esm/operation-node/operation-node-transformer.js +17 -0
  152. package/dist/esm/operation-node/operation-node-visitor.d.ts +2 -0
  153. package/dist/esm/operation-node/operation-node-visitor.js +1 -0
  154. package/dist/esm/operation-node/operation-node.d.ts +1 -1
  155. package/dist/esm/operation-node/operator-node.d.ts +3 -3
  156. package/dist/esm/operation-node/operator-node.js +2 -0
  157. package/dist/esm/operation-node/unique-constraint-node.d.ts +4 -1
  158. package/dist/esm/operation-node/unique-constraint-node.js +8 -1
  159. package/dist/esm/parser/binary-operation-parser.js +1 -1
  160. package/dist/esm/parser/coalesce-parser.d.ts +12 -2
  161. package/dist/esm/parser/expression-parser.d.ts +2 -2
  162. package/dist/esm/parser/reference-parser.d.ts +3 -2
  163. package/dist/esm/parser/returning-parser.d.ts +2 -1
  164. package/dist/esm/parser/select-parser.d.ts +20 -9
  165. package/dist/esm/parser/update-set-parser.d.ts +5 -2
  166. package/dist/esm/parser/update-set-parser.js +10 -1
  167. package/dist/esm/parser/value-parser.d.ts +2 -2
  168. package/dist/esm/query-builder/aggregate-function-builder.d.ts +4 -4
  169. package/dist/esm/query-builder/case-builder.d.ts +5 -4
  170. package/dist/esm/query-builder/delete-query-builder.d.ts +8 -6
  171. package/dist/esm/query-builder/function-module.d.ts +98 -27
  172. package/dist/esm/query-builder/function-module.js +17 -3
  173. package/dist/esm/query-builder/having-interface.d.ts +3 -5
  174. package/dist/esm/query-builder/insert-query-builder.d.ts +32 -12
  175. package/dist/esm/query-builder/insert-query-builder.js +4 -4
  176. package/dist/esm/query-builder/insert-result.d.ts +8 -1
  177. package/dist/esm/query-builder/insert-result.js +8 -1
  178. package/dist/esm/query-builder/json-path-builder.d.ts +3 -2
  179. package/dist/esm/query-builder/json-path-builder.js +6 -3
  180. package/dist/esm/query-builder/on-conflict-builder.d.ts +58 -16
  181. package/dist/esm/query-builder/on-conflict-builder.js +2 -7
  182. package/dist/esm/query-builder/over-builder.d.ts +2 -2
  183. package/dist/esm/query-builder/returning-interface.d.ts +3 -2
  184. package/dist/esm/query-builder/select-query-builder-expression.d.ts +20 -0
  185. package/dist/esm/query-builder/select-query-builder-expression.js +2 -0
  186. package/dist/esm/query-builder/select-query-builder.d.ts +48 -29
  187. package/dist/esm/query-builder/select-query-builder.js +5 -2
  188. package/dist/esm/query-builder/update-query-builder.d.ts +25 -7
  189. package/dist/esm/query-builder/update-query-builder.js +3 -3
  190. package/dist/esm/query-builder/where-interface.d.ts +3 -3
  191. package/dist/esm/query-compiler/default-query-compiler.d.ts +9 -1
  192. package/dist/esm/query-compiler/default-query-compiler.js +65 -12
  193. package/dist/esm/query-creator.d.ts +5 -3
  194. package/dist/esm/query-creator.js +0 -47
  195. package/dist/esm/raw-builder/raw-builder.d.ts +0 -3
  196. package/dist/esm/schema/alter-table-add-index-builder.d.ts +104 -0
  197. package/dist/esm/schema/alter-table-add-index-builder.js +143 -0
  198. package/dist/esm/schema/alter-table-builder.d.ts +41 -1
  199. package/dist/esm/schema/alter-table-builder.js +59 -2
  200. package/dist/esm/schema/column-definition-builder.d.ts +25 -0
  201. package/dist/esm/schema/column-definition-builder.js +27 -0
  202. package/dist/esm/schema/create-index-builder.d.ts +23 -0
  203. package/dist/esm/schema/create-index-builder.js +30 -0
  204. package/dist/esm/schema/create-table-builder.d.ts +28 -2
  205. package/dist/esm/schema/create-table-builder.js +38 -3
  206. package/dist/esm/schema/create-view-builder.d.ts +2 -2
  207. package/dist/esm/schema/unique-constraint-builder.d.ts +14 -0
  208. package/dist/esm/schema/unique-constraint-builder.js +21 -0
  209. package/dist/esm/util/infer-result.d.ts +2 -1
  210. package/dist/esm/util/log.js +5 -1
  211. package/dist/esm/util/object-utils.d.ts +1 -1
  212. package/dist/esm/util/type-utils.d.ts +27 -20
  213. package/helpers/mssql.d.ts +1 -0
  214. package/helpers/mssql.js +1 -0
  215. package/package.json +17 -6
@@ -183,6 +183,31 @@ export declare class ColumnDefinitionBuilder implements OperationNodeSource {
183
183
  * ```
184
184
  */
185
185
  modifyFront(modifier: Expression<any>): ColumnDefinitionBuilder;
186
+ /**
187
+ * Adds `nulls not distinct` specifier.
188
+ * Should be used with `unique` constraint.
189
+ *
190
+ * This only works on some dialects like PostgreSQL.
191
+ *
192
+ * ### Examples
193
+ *
194
+ * ```ts
195
+ * db.schema.createTable('person')
196
+ * .addColumn('id', 'integer', col => col.primaryKey())
197
+ * .addColumn('first_name', 'varchar(30)', col => col.unique().nullsNotDistinct())
198
+ * .execute()
199
+ * ```
200
+ *
201
+ * The generated SQL (PostgreSQL):
202
+ *
203
+ * ```sql
204
+ * create table "person" (
205
+ * "id" integer primary key,
206
+ * "first_name" varchar(30) unique nulls not distinct
207
+ * )
208
+ * ```
209
+ */
210
+ nullsNotDistinct(): ColumnDefinitionBuilder;
186
211
  /**
187
212
  * This can be used to add any additional SQL to the end of the column definition.
188
213
  *
@@ -258,6 +258,33 @@ class ColumnDefinitionBuilder {
258
258
  modifyFront(modifier) {
259
259
  return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWithFrontModifier(this.#node, modifier.toOperationNode()));
260
260
  }
261
+ /**
262
+ * Adds `nulls not distinct` specifier.
263
+ * Should be used with `unique` constraint.
264
+ *
265
+ * This only works on some dialects like PostgreSQL.
266
+ *
267
+ * ### Examples
268
+ *
269
+ * ```ts
270
+ * db.schema.createTable('person')
271
+ * .addColumn('id', 'integer', col => col.primaryKey())
272
+ * .addColumn('first_name', 'varchar(30)', col => col.unique().nullsNotDistinct())
273
+ * .execute()
274
+ * ```
275
+ *
276
+ * The generated SQL (PostgreSQL):
277
+ *
278
+ * ```sql
279
+ * create table "person" (
280
+ * "id" integer primary key,
281
+ * "first_name" varchar(30) unique nulls not distinct
282
+ * )
283
+ * ```
284
+ */
285
+ nullsNotDistinct() {
286
+ return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, { nullsNotDistinct: true }));
287
+ }
261
288
  /**
262
289
  * This can be used to add any additional SQL to the end of the column definition.
263
290
  *
@@ -22,6 +22,29 @@ export declare class CreateIndexBuilder<C = never> implements OperationNodeSourc
22
22
  * Makes the index unique.
23
23
  */
24
24
  unique(): CreateIndexBuilder<C>;
25
+ /**
26
+ * Adds `nulls not distinct` specifier to index.
27
+ * This only works on some dialects like PostgreSQL.
28
+ *
29
+ * ### Examples
30
+ *
31
+ * ```ts
32
+ * db.schema.createIndex('person_first_name_index')
33
+ * .on('person')
34
+ * .column('first_name')
35
+ * .nullsNotDistinct()
36
+ * .execute()
37
+ * ```
38
+ *
39
+ * The generated SQL (PostgreSQL):
40
+ *
41
+ * ```sql
42
+ * create index "person_first_name_index"
43
+ * on "test" ("first_name")
44
+ * nulls not distinct;
45
+ * ```
46
+ */
47
+ nullsNotDistinct(): CreateIndexBuilder<C>;
25
48
  /**
26
49
  * Specifies the table for the index.
27
50
  */
@@ -39,6 +39,36 @@ class CreateIndexBuilder {
39
39
  }),
40
40
  });
41
41
  }
42
+ /**
43
+ * Adds `nulls not distinct` specifier to index.
44
+ * This only works on some dialects like PostgreSQL.
45
+ *
46
+ * ### Examples
47
+ *
48
+ * ```ts
49
+ * db.schema.createIndex('person_first_name_index')
50
+ * .on('person')
51
+ * .column('first_name')
52
+ * .nullsNotDistinct()
53
+ * .execute()
54
+ * ```
55
+ *
56
+ * The generated SQL (PostgreSQL):
57
+ *
58
+ * ```sql
59
+ * create index "person_first_name_index"
60
+ * on "test" ("first_name")
61
+ * nulls not distinct;
62
+ * ```
63
+ */
64
+ nullsNotDistinct() {
65
+ return new CreateIndexBuilder({
66
+ ...this.#props,
67
+ node: create_index_node_js_1.CreateIndexNode.cloneWith(this.#props.node, {
68
+ nullsNotDistinct: true,
69
+ }),
70
+ });
71
+ }
42
72
  /**
43
73
  * Specifies the table for the index.
44
74
  */
@@ -8,6 +8,7 @@ import { QueryId } from '../util/query-id.js';
8
8
  import { ForeignKeyConstraintBuilder } from './foreign-key-constraint-builder.js';
9
9
  import { DataTypeExpression } from '../parser/data-type-parser.js';
10
10
  import { Expression } from '../expression/expression.js';
11
+ import { UniqueConstraintNodeBuilderCallback } from './unique-constraint-builder.js';
11
12
  /**
12
13
  * This builder can be used to create a `create table` query.
13
14
  */
@@ -56,7 +57,7 @@ export declare class CreateTableBuilder<TB extends string, C extends string = ne
56
57
  * ```
57
58
  *
58
59
  * With this method, it's once again good to remember that Kysely just builds the
59
- * query and doesn't provide the same API for all databses. For example, some
60
+ * query and doesn't provide the same API for all databases. For example, some
60
61
  * databases like older MySQL don't support the `references` statement in the
61
62
  * column definition. Instead foreign key constraints need to be defined in the
62
63
  * `create table` query. See the next example:
@@ -104,8 +105,13 @@ export declare class CreateTableBuilder<TB extends string, C extends string = ne
104
105
  * ```ts
105
106
  * addUniqueConstraint('first_name_last_name_unique', ['first_name', 'last_name'])
106
107
  * ```
108
+ *
109
+ * In dialects such as PostgreSQL you can specify `nulls not distinct` as follows:
110
+ * ```ts
111
+ * addUniqueConstraint('first_name_last_name_unique', ['first_name', 'last_name'], (builder) => builder.nullsNotDistinct())
112
+ * ```
107
113
  */
108
- addUniqueConstraint(constraintName: string, columns: C[]): CreateTableBuilder<TB, C>;
114
+ addUniqueConstraint(constraintName: string, columns: C[], build?: UniqueConstraintNodeBuilderCallback): CreateTableBuilder<TB, C>;
109
115
  /**
110
116
  * Adds a check constraint.
111
117
  *
@@ -205,6 +211,26 @@ export declare class CreateTableBuilder<TB extends string, C extends string = ne
205
211
  * ```
206
212
  */
207
213
  modifyEnd(modifier: Expression<any>): CreateTableBuilder<TB, C>;
214
+ /**
215
+ * Allows to create table from `select` query.
216
+ *
217
+ * ### Examples
218
+ *
219
+ * ```ts
220
+ * db.schema.createTable('copy')
221
+ * .temporary()
222
+ * .as(db.selectFrom('person').select(['first_name', 'last_name']))
223
+ * .execute()
224
+ * ```
225
+ *
226
+ * The generated SQL (PostgreSQL):
227
+ *
228
+ * ```sql
229
+ * create temporary table "copy" as
230
+ * select "first_name", "last_name" from "person"
231
+ * ```
232
+ */
233
+ as(expression: Expression<unknown>): CreateTableBuilder<string, never>;
208
234
  /**
209
235
  * Calls the given function passing `this` as the only argument.
210
236
  *
@@ -15,6 +15,8 @@ const unique_constraint_node_js_1 = require("../operation-node/unique-constraint
15
15
  const check_constraint_node_js_1 = require("../operation-node/check-constraint-node.js");
16
16
  const table_parser_js_1 = require("../parser/table-parser.js");
17
17
  const on_commit_action_parse_js_1 = require("../parser/on-commit-action-parse.js");
18
+ const unique_constraint_builder_js_1 = require("./unique-constraint-builder.js");
19
+ const expression_parser_js_1 = require("../parser/expression-parser.js");
18
20
  /**
19
21
  * This builder can be used to create a `create table` query.
20
22
  */
@@ -86,7 +88,7 @@ class CreateTableBuilder {
86
88
  * ```
87
89
  *
88
90
  * With this method, it's once again good to remember that Kysely just builds the
89
- * query and doesn't provide the same API for all databses. For example, some
91
+ * query and doesn't provide the same API for all databases. For example, some
90
92
  * databases like older MySQL don't support the `references` statement in the
91
93
  * column definition. Instead foreign key constraints need to be defined in the
92
94
  * `create table` query. See the next example:
@@ -145,11 +147,17 @@ class CreateTableBuilder {
145
147
  * ```ts
146
148
  * addUniqueConstraint('first_name_last_name_unique', ['first_name', 'last_name'])
147
149
  * ```
150
+ *
151
+ * In dialects such as PostgreSQL you can specify `nulls not distinct` as follows:
152
+ * ```ts
153
+ * addUniqueConstraint('first_name_last_name_unique', ['first_name', 'last_name'], (builder) => builder.nullsNotDistinct())
154
+ * ```
148
155
  */
149
- addUniqueConstraint(constraintName, columns) {
156
+ addUniqueConstraint(constraintName, columns, build = object_utils_js_1.noop) {
157
+ const uniqueConstraintBuilder = build(new unique_constraint_builder_js_1.UniqueConstraintNodeBuilder(unique_constraint_node_js_1.UniqueConstraintNode.create(columns, constraintName)));
150
158
  return new CreateTableBuilder({
151
159
  ...this.#props,
152
- node: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.node, unique_constraint_node_js_1.UniqueConstraintNode.create(columns, constraintName)),
160
+ node: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.node, uniqueConstraintBuilder.toOperationNode()),
153
161
  });
154
162
  }
155
163
  /**
@@ -272,6 +280,33 @@ class CreateTableBuilder {
272
280
  node: create_table_node_js_1.CreateTableNode.cloneWithEndModifier(this.#props.node, modifier.toOperationNode()),
273
281
  });
274
282
  }
283
+ /**
284
+ * Allows to create table from `select` query.
285
+ *
286
+ * ### Examples
287
+ *
288
+ * ```ts
289
+ * db.schema.createTable('copy')
290
+ * .temporary()
291
+ * .as(db.selectFrom('person').select(['first_name', 'last_name']))
292
+ * .execute()
293
+ * ```
294
+ *
295
+ * The generated SQL (PostgreSQL):
296
+ *
297
+ * ```sql
298
+ * create temporary table "copy" as
299
+ * select "first_name", "last_name" from "person"
300
+ * ```
301
+ */
302
+ as(expression) {
303
+ return new CreateTableBuilder({
304
+ ...this.#props,
305
+ node: create_table_node_js_1.CreateTableNode.cloneWith(this.#props.node, {
306
+ selectQuery: (0, expression_parser_js_1.parseExpression)(expression),
307
+ }),
308
+ });
309
+ }
275
310
  /**
276
311
  * Calls the given function passing `this` as the only argument.
277
312
  *
@@ -4,8 +4,8 @@ import { Compilable } from '../util/compilable.js';
4
4
  import { QueryExecutor } from '../query-executor/query-executor.js';
5
5
  import { QueryId } from '../util/query-id.js';
6
6
  import { CreateViewNode } from '../operation-node/create-view-node.js';
7
- import { AnySelectQueryBuilder } from '../util/type-utils.js';
8
7
  import { RawBuilder } from '../raw-builder/raw-builder.js';
8
+ import { SelectQueryBuilder } from '../query-builder/select-query-builder.js';
9
9
  export declare class CreateViewBuilder implements OperationNodeSource, Compilable {
10
10
  #private;
11
11
  constructor(props: CreateViewBuilderProps);
@@ -31,7 +31,7 @@ export declare class CreateViewBuilder implements OperationNodeSource, Compilabl
31
31
  * string opening an SQL injection vulnerability. DO NOT pass unchecked user input
32
32
  * into the query or raw expression passed to this method!
33
33
  */
34
- as(query: AnySelectQueryBuilder | RawBuilder<any>): CreateViewBuilder;
34
+ as(query: SelectQueryBuilder<any, any, any> | RawBuilder<any>): CreateViewBuilder;
35
35
  /**
36
36
  * Simply calls the provided function passing `this` as the only argument. `$call` returns
37
37
  * what the provided function returns.
@@ -0,0 +1,14 @@
1
+ import { OperationNodeSource } from '../operation-node/operation-node-source.js';
2
+ import { UniqueConstraintNode } from '../operation-node/unique-constraint-node.js';
3
+ export declare class UniqueConstraintNodeBuilder implements OperationNodeSource {
4
+ #private;
5
+ constructor(node: UniqueConstraintNode);
6
+ toOperationNode(): UniqueConstraintNode;
7
+ /**
8
+ * Adds `nulls not distinct` to the unique constraint definition
9
+ *
10
+ * Supported by PostgreSQL dialect only
11
+ */
12
+ nullsNotDistinct(): UniqueConstraintNodeBuilder;
13
+ }
14
+ export type UniqueConstraintNodeBuilderCallback = (builder: UniqueConstraintNodeBuilder) => UniqueConstraintNodeBuilder;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UniqueConstraintNodeBuilder = void 0;
4
+ const unique_constraint_node_js_1 = require("../operation-node/unique-constraint-node.js");
5
+ const prevent_await_js_1 = require("../util/prevent-await.js");
6
+ class UniqueConstraintNodeBuilder {
7
+ #node;
8
+ constructor(node) {
9
+ this.#node = node;
10
+ }
11
+ toOperationNode() {
12
+ return this.#node;
13
+ }
14
+ /**
15
+ * Adds `nulls not distinct` to the unique constraint definition
16
+ *
17
+ * Supported by PostgreSQL dialect only
18
+ */
19
+ nullsNotDistinct() {
20
+ return new UniqueConstraintNodeBuilder(unique_constraint_node_js_1.UniqueConstraintNode.cloneWith(this.#node, { nullsNotDistinct: true }));
21
+ }
22
+ }
23
+ exports.UniqueConstraintNodeBuilder = UniqueConstraintNodeBuilder;
24
+ (0, prevent_await_js_1.preventAwait)(UniqueConstraintNodeBuilder, "don't await UniqueConstraintNodeBuilder instances directly.");
@@ -3,6 +3,7 @@ import { InsertResult } from '../query-builder/insert-result.js';
3
3
  import { UpdateResult } from '../query-builder/update-result.js';
4
4
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
5
5
  import { Compilable } from './compilable.js';
6
+ import { Simplify } from './type-utils.js';
6
7
  /**
7
8
  * A helper type that allows inferring a select/insert/update/delete query's result
8
9
  * type from a query builder or compiled query.
@@ -42,5 +43,5 @@ import { Compilable } from './compilable.js';
42
43
  * ```
43
44
  */
44
45
  export type InferResult<C extends Compilable<any> | CompiledQuery<any>> = C extends Compilable<infer O> ? ResolveResult<O> : C extends CompiledQuery<infer O> ? ResolveResult<O> : never;
45
- type ResolveResult<O> = O extends InsertResult | UpdateResult | DeleteResult ? O : O[];
46
+ type ResolveResult<O> = O extends InsertResult | UpdateResult | DeleteResult ? O : Simplify<O>[];
46
47
  export {};
@@ -47,7 +47,11 @@ function defaultLogger(event) {
47
47
  console.error(`kysely:error: ${event.error.stack ?? event.error.message}`);
48
48
  }
49
49
  else {
50
- console.error(`kysely:error: ${event}`);
50
+ console.error(`kysely:error: ${JSON.stringify({
51
+ error: event.error,
52
+ query: event.query.sql,
53
+ queryDurationMillis: event.queryDurationMillis,
54
+ })}`);
51
55
  }
52
56
  }
53
57
  }
@@ -6,7 +6,7 @@ export declare function isNumber(obj: unknown): obj is number;
6
6
  export declare function isBoolean(obj: unknown): obj is boolean;
7
7
  export declare function isNull(obj: unknown): obj is null;
8
8
  export declare function isDate(obj: unknown): obj is Date;
9
- export declare function isBigInt(obj: unknown): obj is BigInt;
9
+ export declare function isBigInt(obj: unknown): obj is bigint;
10
10
  export declare function isBuffer(obj: unknown): obj is {
11
11
  length: number;
12
12
  };
@@ -1,4 +1,3 @@
1
- import { SelectQueryBuilder } from '../query-builder/select-query-builder.js';
2
1
  import { InsertResult } from '../query-builder/insert-result.js';
3
2
  import { DeleteResult } from '../query-builder/delete-result.js';
4
3
  import { UpdateResult } from '../query-builder/update-result.js';
@@ -34,15 +33,15 @@ import { KyselyTypeError } from './type-error.js';
34
33
  * // Columns == 'id' | 'name' | 'species'
35
34
  * ```
36
35
  */
37
- export type AnyColumn<DB, TB extends keyof DB> = DrainOuterGeneric<{
36
+ export type AnyColumn<DB, TB extends keyof DB> = {
38
37
  [T in TB]: keyof DB[T];
39
- }[TB] & string>;
38
+ }[TB] & string;
40
39
  /**
41
40
  * Extracts a column type.
42
41
  */
43
- export type ExtractColumnType<DB, TB extends keyof DB, C> = DrainOuterGeneric<{
42
+ export type ExtractColumnType<DB, TB extends keyof DB, C> = {
44
43
  [T in TB]: C extends keyof DB[T] ? DB[T][C] : never;
45
- }[TB]>;
44
+ }[TB];
46
45
  /**
47
46
  * Given a database type and a union of table names in that db, returns
48
47
  * a union type with all possible `table`.`column` combinations.
@@ -74,29 +73,21 @@ export type ExtractColumnType<DB, TB extends keyof DB, C> = DrainOuterGeneric<{
74
73
  * // Columns == 'person.id' | 'pet.name' | 'pet.species'
75
74
  * ```
76
75
  */
77
- export type AnyColumnWithTable<DB, TB extends keyof DB> = DrainOuterGeneric<{
78
- [T in TB]: T extends string ? keyof DB[T] extends string ? `${T}.${keyof DB[T]}` : never : never;
79
- }[TB]>;
76
+ export type AnyColumnWithTable<DB, TB extends keyof DB> = {
77
+ [T in TB]: `${T & string}.${keyof DB[T] & string}`;
78
+ }[TB];
80
79
  /**
81
80
  * Just like {@link AnyColumn} but with a ` as <string>` suffix.
82
81
  */
83
- export type AnyAliasedColumn<DB, TB extends keyof DB> = DrainOuterGeneric<{
84
- [T in TB]: T extends string ? keyof DB[T] extends string ? `${keyof DB[T]} as ${string}` : never : never;
85
- }[TB]>;
82
+ export type AnyAliasedColumn<DB, TB extends keyof DB> = `${AnyColumn<DB, TB>} as ${string}`;
86
83
  /**
87
84
  * Just like {@link AnyColumnWithTable} but with a ` as <string>` suffix.
88
85
  */
89
- export type AnyAliasedColumnWithTable<DB, TB extends keyof DB> = DrainOuterGeneric<{
90
- [T in TB]: T extends string ? keyof DB[T] extends string ? `${T}.${keyof DB[T]} as ${string}` : never : never;
91
- }[TB]>;
86
+ export type AnyAliasedColumnWithTable<DB, TB extends keyof DB> = `${AnyColumnWithTable<DB, TB>} as ${string}`;
92
87
  /**
93
88
  * Extracts the item type of an array.
94
89
  */
95
90
  export type ArrayItemType<T> = T extends ReadonlyArray<infer I> ? I : never;
96
- /**
97
- * Any select query builder.
98
- */
99
- export type AnySelectQueryBuilder = SelectQueryBuilder<any, any, any>;
100
91
  export type SimplifySingleResult<O> = O extends InsertResult ? O : O extends DeleteResult ? O : O extends UpdateResult ? O : Simplify<O> | undefined;
101
92
  export type SimplifyResult<O> = O extends InsertResult ? O : O extends DeleteResult ? O : O extends UpdateResult ? O : Simplify<O>;
102
93
  export type Simplify<T> = DrainOuterGeneric<{
@@ -124,9 +115,25 @@ export type IsAny<T> = 0 extends T & 1 ? true : false;
124
115
  * Evaluates to `true` if the types `T` and `U` are equal.
125
116
  */
126
117
  export type Equals<T, U> = (<G>() => G extends T ? 1 : 2) extends <G>() => G extends U ? 1 : 2 ? true : false;
127
- export type NarrowPartial<S, T> = DrainOuterGeneric<T extends object ? {
128
- [K in keyof S & string]: K extends keyof T ? T[K] extends S[K] ? T[K] : KyselyTypeError<`$narrowType() call failed: passed type does not exist in '${K}'s type union`> : S[K];
118
+ export type NarrowPartial<O, T> = DrainOuterGeneric<T extends object ? {
119
+ [K in keyof O & string]: K extends keyof T ? T[K] extends NotNull ? Exclude<O[K], null> : T[K] extends O[K] ? T[K] : KyselyTypeError<`$narrowType() call failed: passed type does not exist in '${K}'s type union`> : O[K];
129
120
  } : never>;
121
+ /**
122
+ * A type constant for marking a column as not null. Can be used with `$narrowPartial`.
123
+ *
124
+ * Example:
125
+ *
126
+ * ```ts
127
+ * const person = await db.selectFrom('person')
128
+ * .where('nullable_column', 'is not', null)
129
+ * .selectAll()
130
+ * .$narrowType<{ nullable_column: NotNull }>()
131
+ * .executeTakeFirstOrThrow()
132
+ * ```
133
+ */
134
+ export type NotNull = {
135
+ readonly __notNull__: unique symbol;
136
+ };
130
137
  export type SqlBool = boolean | 0 | 1;
131
138
  /**
132
139
  * Utility to reduce depth of TypeScript's internal type instantiation stack.
@@ -7,6 +7,14 @@ import { DialectAdapter, MigrationLockOptions } from './dialect-adapter.js';
7
7
  * they are added and there will be less breaking changes.
8
8
  */
9
9
  export declare abstract class DialectAdapterBase implements DialectAdapter {
10
+ /**
11
+ * Whether or not this dialect supports `if not exists` in creation of tables/schemas/views/etc.
12
+ *
13
+ * If this is false, Kysely's internal migrations tables and schemas are created
14
+ * without `if not exists` in migrations. This is not a problem if the dialect
15
+ * supports transactional DDL.
16
+ */
17
+ get supportsCreateIfNotExists(): boolean;
10
18
  /**
11
19
  * Whether or not this dialect supports transactional DDL.
12
20
  *
@@ -6,6 +6,9 @@
6
6
  * they are added and there will be less breaking changes.
7
7
  */
8
8
  export class DialectAdapterBase {
9
+ get supportsCreateIfNotExists() {
10
+ return true;
11
+ }
9
12
  get supportsTransactionalDdl() {
10
13
  return false;
11
14
  }
@@ -8,6 +8,14 @@ import { Kysely } from '../kysely.js';
8
8
  * it. For that there's a `supportsTransactionalDdl` boolean in this interface.
9
9
  */
10
10
  export interface DialectAdapter {
11
+ /**
12
+ * Whether or not this dialect supports `if not exists` in creation of tables/schemas/views/etc.
13
+ *
14
+ * If this is false, Kysely's internal migrations tables and schemas are created
15
+ * without `if not exists` in migrations. This is not a problem if the dialect
16
+ * supports transactional DDL.
17
+ */
18
+ readonly supportsCreateIfNotExists: boolean;
11
19
  /**
12
20
  * Whether or not this dialect supports transactional DDL.
13
21
  *
@@ -0,0 +1,78 @@
1
+ import { Kysely } from '../../kysely.js';
2
+ import { DialectAdapterBase } from '../dialect-adapter-base.js';
3
+ export declare class MssqlAdapter extends DialectAdapterBase {
4
+ /**
5
+ * Whether or not this dialect supports `if not exists` in creation of tables/schemas/views/etc.
6
+ *
7
+ * If this is false, Kysely's internal migrations tables and schemas are created
8
+ * without `if not exists` in migrations. This is not a problem if the dialect
9
+ * supports transactional DDL.
10
+ */
11
+ get supportsCreateIfNotExists(): boolean;
12
+ /**
13
+ * Whether or not this dialect supports transactional DDL.
14
+ *
15
+ * If this is true, migrations are executed inside a transaction.
16
+ */
17
+ get supportsTransactionalDdl(): boolean;
18
+ /**
19
+ * Whether or not this dialect supports the `returning` in inserts
20
+ * updates and deletes.
21
+ */
22
+ get supportsReturning(): boolean;
23
+ /**
24
+ * This method is used to acquire a lock for the migrations so that
25
+ * it's not possible for two migration operations to run in parallel.
26
+ *
27
+ * Most dialects have explicit locks that can be used, like advisory locks
28
+ * in PostgreSQL and the get_lock function in MySQL.
29
+ *
30
+ * If the dialect doesn't have explicit locks the {@link MigrationLockOptions.lockTable}
31
+ * created by Kysely can be used instead. You can access it through the `options` object.
32
+ * The lock table has two columns `id` and `is_locked` and there's only one row in the table
33
+ * whose id is {@link MigrationLockOptions.lockRowId}. `is_locked` is an integer. Kysely
34
+ * takes care of creating the lock table and inserting the one single row to it before this
35
+ * method is executed. If the dialect supports schemas and the user has specified a custom
36
+ * schema in their migration settings, the options object also contains the schema name in
37
+ * {@link MigrationLockOptions.lockTableSchema}.
38
+ *
39
+ * Here's an example of how you might implement this method for a dialect that doesn't
40
+ * have explicit locks but supports `FOR UPDATE` row locks and transactional DDL:
41
+ *
42
+ * ```ts
43
+ * {
44
+ * async acquireMigrationLock(db, options): Promise<void> {
45
+ * const queryDb = options.lockTableSchema
46
+ * ? db.withSchema(options.lockTableSchema)
47
+ * : db
48
+ *
49
+ * // Since our imaginary dialect supports transactional DDL and has
50
+ * // row locks, we can simply take a row lock here and it will guarantee
51
+ * // all subsequent calls to this method from other transactions will
52
+ * // wait until this transaction finishes.
53
+ * await queryDb
54
+ * .selectFrom(options.lockTable)
55
+ * .selectAll()
56
+ * .where('id', '=', options.lockRowId)
57
+ * .forUpdate()
58
+ * .execute()
59
+ * }
60
+ * }
61
+ * ```
62
+ *
63
+ * If `supportsTransactionalDdl` is `true` then the `db` passed to this method
64
+ * is a transaction inside which the migrations will be executed. Otherwise
65
+ * `db` is a single connection (session) that will be used to execute the
66
+ * migrations.
67
+ */
68
+ acquireMigrationLock(db: Kysely<any>): Promise<void>;
69
+ /**
70
+ * Releases the migration lock. See {@link acquireMigrationLock}.
71
+ *
72
+ * If `supportsTransactionalDdl` is `true` then the `db` passed to this method
73
+ * is a transaction inside which the migrations were executed. Otherwise `db`
74
+ * is a single connection (session) that was used to execute the migrations
75
+ * and the `acquireMigrationLock` call.
76
+ */
77
+ releaseMigrationLock(): Promise<void>;
78
+ }
@@ -0,0 +1,27 @@
1
+ /// <reference types="./mssql-adapter.d.ts" />
2
+ import { DEFAULT_MIGRATION_TABLE } from '../../migration/migrator.js';
3
+ import { sql } from '../../raw-builder/sql.js';
4
+ import { DialectAdapterBase } from '../dialect-adapter-base.js';
5
+ export class MssqlAdapter extends DialectAdapterBase {
6
+ get supportsCreateIfNotExists() {
7
+ return false;
8
+ }
9
+ get supportsTransactionalDdl() {
10
+ return true;
11
+ }
12
+ get supportsReturning() {
13
+ // mssql should support returning with the `output` clause.
14
+ // we need to figure this out when we'll introduce support for it.
15
+ return false;
16
+ }
17
+ async acquireMigrationLock(db) {
18
+ // Acquire a transaction-level exclusive lock on the migrations table.
19
+ // https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-getapplock-transact-sql?view=sql-server-ver16
20
+ await sql `exec sp_getapplock @DbPrincipal = ${sql.lit('dbo')}, @Resource = ${sql.lit(DEFAULT_MIGRATION_TABLE)}, @LockMode = ${sql.lit('Exclusive')}`.execute(db);
21
+ }
22
+ async releaseMigrationLock() {
23
+ // Nothing to do here. `sp_getapplock` is automatically released at the
24
+ // end of the transaction and since `supportsTransactionalDdl` true, we know
25
+ // the `db` instance passed to acquireMigrationLock is actually a transaction.
26
+ }
27
+ }