kysely 0.26.3 → 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 (185) 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 -75
  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/index.d.ts +7 -1
  33. package/dist/cjs/index.js +6 -0
  34. package/dist/cjs/migration/migrator.js +12 -16
  35. package/dist/cjs/operation-node/add-index-node.d.ts +21 -0
  36. package/dist/cjs/operation-node/add-index-node.js +31 -0
  37. package/dist/cjs/operation-node/alter-table-node.d.ts +5 -1
  38. package/dist/cjs/operation-node/column-definition-node.d.ts +1 -0
  39. package/dist/cjs/operation-node/column-update-node.d.ts +2 -3
  40. package/dist/cjs/operation-node/create-index-node.d.ts +1 -0
  41. package/dist/cjs/operation-node/create-table-node.d.ts +1 -0
  42. package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
  43. package/dist/cjs/operation-node/operation-node-transformer.js +17 -0
  44. package/dist/cjs/operation-node/operation-node-visitor.d.ts +2 -0
  45. package/dist/cjs/operation-node/operation-node-visitor.js +1 -0
  46. package/dist/cjs/operation-node/operation-node.d.ts +1 -1
  47. package/dist/cjs/operation-node/operator-node.d.ts +3 -3
  48. package/dist/cjs/operation-node/operator-node.js +2 -0
  49. package/dist/cjs/operation-node/unique-constraint-node.d.ts +4 -1
  50. package/dist/cjs/operation-node/unique-constraint-node.js +8 -1
  51. package/dist/cjs/parser/binary-operation-parser.js +1 -1
  52. package/dist/cjs/parser/coalesce-parser.d.ts +12 -2
  53. package/dist/cjs/parser/reference-parser.d.ts +2 -1
  54. package/dist/cjs/parser/update-set-parser.d.ts +5 -2
  55. package/dist/cjs/parser/update-set-parser.js +13 -3
  56. package/dist/cjs/query-builder/aggregate-function-builder.d.ts +4 -4
  57. package/dist/cjs/query-builder/case-builder.d.ts +5 -4
  58. package/dist/cjs/query-builder/delete-query-builder.d.ts +3 -3
  59. package/dist/cjs/query-builder/function-module.d.ts +96 -25
  60. package/dist/cjs/query-builder/function-module.js +17 -3
  61. package/dist/cjs/query-builder/having-interface.d.ts +3 -5
  62. package/dist/cjs/query-builder/insert-query-builder.d.ts +27 -9
  63. package/dist/cjs/query-builder/insert-query-builder.js +3 -3
  64. package/dist/cjs/query-builder/insert-result.d.ts +8 -1
  65. package/dist/cjs/query-builder/insert-result.js +8 -1
  66. package/dist/cjs/query-builder/json-path-builder.d.ts +3 -2
  67. package/dist/cjs/query-builder/json-path-builder.js +6 -3
  68. package/dist/cjs/query-builder/on-conflict-builder.d.ts +58 -16
  69. package/dist/cjs/query-builder/on-conflict-builder.js +1 -6
  70. package/dist/cjs/query-builder/over-builder.d.ts +2 -2
  71. package/dist/cjs/query-builder/select-query-builder.d.ts +38 -22
  72. package/dist/cjs/query-builder/update-query-builder.d.ts +20 -4
  73. package/dist/cjs/query-builder/update-query-builder.js +2 -2
  74. package/dist/cjs/query-builder/where-interface.d.ts +3 -3
  75. package/dist/cjs/query-compiler/default-query-compiler.d.ts +9 -1
  76. package/dist/cjs/query-compiler/default-query-compiler.js +65 -12
  77. package/dist/cjs/schema/alter-table-add-index-builder.d.ts +104 -0
  78. package/dist/cjs/schema/alter-table-add-index-builder.js +146 -0
  79. package/dist/cjs/schema/alter-table-builder.d.ts +41 -1
  80. package/dist/cjs/schema/alter-table-builder.js +59 -2
  81. package/dist/cjs/schema/column-definition-builder.d.ts +25 -0
  82. package/dist/cjs/schema/column-definition-builder.js +27 -0
  83. package/dist/cjs/schema/create-index-builder.d.ts +23 -0
  84. package/dist/cjs/schema/create-index-builder.js +30 -0
  85. package/dist/cjs/schema/create-table-builder.d.ts +28 -2
  86. package/dist/cjs/schema/create-table-builder.js +38 -3
  87. package/dist/cjs/schema/unique-constraint-builder.d.ts +14 -0
  88. package/dist/cjs/schema/unique-constraint-builder.js +24 -0
  89. package/dist/cjs/util/log.js +5 -1
  90. package/dist/cjs/util/object-utils.d.ts +1 -1
  91. package/dist/cjs/util/type-utils.d.ts +27 -19
  92. package/dist/esm/dialect/dialect-adapter-base.d.ts +8 -0
  93. package/dist/esm/dialect/dialect-adapter-base.js +3 -0
  94. package/dist/esm/dialect/dialect-adapter.d.ts +8 -0
  95. package/dist/esm/dialect/mssql/mssql-adapter.d.ts +78 -0
  96. package/dist/esm/dialect/mssql/mssql-adapter.js +27 -0
  97. package/dist/esm/dialect/mssql/mssql-dialect-config.d.ts +143 -0
  98. package/dist/esm/dialect/mssql/mssql-dialect-config.js +2 -0
  99. package/dist/esm/dialect/mssql/mssql-dialect.d.ts +70 -0
  100. package/dist/esm/dialect/mssql/mssql-dialect.js +62 -0
  101. package/dist/esm/dialect/mssql/mssql-driver.d.ts +54 -0
  102. package/dist/esm/dialect/mssql/mssql-driver.js +265 -0
  103. package/dist/esm/dialect/mssql/mssql-introspector.d.ts +20 -0
  104. package/dist/esm/dialect/mssql/mssql-introspector.js +94 -0
  105. package/dist/esm/dialect/mssql/mssql-query-compiler.d.ts +11 -0
  106. package/dist/esm/dialect/mssql/mssql-query-compiler.js +61 -0
  107. package/dist/esm/dialect/mysql/mysql-query-compiler.d.ts +2 -0
  108. package/dist/esm/dialect/mysql/mysql-query-compiler.js +28 -0
  109. package/dist/esm/dialect/postgres/postgres-driver.js +1 -1
  110. package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +1 -1
  111. package/dist/esm/dialect/sqlite/sqlite-adapter.js +2 -1
  112. package/dist/esm/dialect/sqlite/sqlite-dialect-config.d.ts +1 -0
  113. package/dist/esm/dialect/sqlite/sqlite-driver.js +15 -2
  114. package/dist/esm/driver/driver.d.ts +1 -1
  115. package/dist/esm/driver/driver.js +1 -0
  116. package/dist/esm/driver/runtime-driver.js +5 -1
  117. package/dist/esm/driver/single-connection-provider.js +7 -8
  118. package/dist/esm/expression/expression-builder.d.ts +66 -75
  119. package/dist/esm/expression/expression-builder.js +10 -19
  120. package/dist/esm/expression/expression-wrapper.d.ts +8 -8
  121. package/dist/esm/helpers/mssql.d.ts +152 -0
  122. package/dist/esm/helpers/mssql.js +155 -0
  123. package/dist/esm/index.d.ts +7 -1
  124. package/dist/esm/index.js +6 -0
  125. package/dist/esm/migration/migrator.js +12 -16
  126. package/dist/esm/operation-node/add-index-node.d.ts +21 -0
  127. package/dist/esm/operation-node/add-index-node.js +29 -0
  128. package/dist/esm/operation-node/alter-table-node.d.ts +5 -1
  129. package/dist/esm/operation-node/column-definition-node.d.ts +1 -0
  130. package/dist/esm/operation-node/column-update-node.d.ts +2 -3
  131. package/dist/esm/operation-node/create-index-node.d.ts +1 -0
  132. package/dist/esm/operation-node/create-table-node.d.ts +1 -0
  133. package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
  134. package/dist/esm/operation-node/operation-node-transformer.js +17 -0
  135. package/dist/esm/operation-node/operation-node-visitor.d.ts +2 -0
  136. package/dist/esm/operation-node/operation-node-visitor.js +1 -0
  137. package/dist/esm/operation-node/operation-node.d.ts +1 -1
  138. package/dist/esm/operation-node/operator-node.d.ts +3 -3
  139. package/dist/esm/operation-node/operator-node.js +2 -0
  140. package/dist/esm/operation-node/unique-constraint-node.d.ts +4 -1
  141. package/dist/esm/operation-node/unique-constraint-node.js +8 -1
  142. package/dist/esm/parser/binary-operation-parser.js +1 -1
  143. package/dist/esm/parser/coalesce-parser.d.ts +12 -2
  144. package/dist/esm/parser/reference-parser.d.ts +2 -1
  145. package/dist/esm/parser/update-set-parser.d.ts +5 -2
  146. package/dist/esm/parser/update-set-parser.js +10 -1
  147. package/dist/esm/query-builder/aggregate-function-builder.d.ts +4 -4
  148. package/dist/esm/query-builder/case-builder.d.ts +5 -4
  149. package/dist/esm/query-builder/delete-query-builder.d.ts +3 -3
  150. package/dist/esm/query-builder/function-module.d.ts +96 -25
  151. package/dist/esm/query-builder/function-module.js +17 -3
  152. package/dist/esm/query-builder/having-interface.d.ts +3 -5
  153. package/dist/esm/query-builder/insert-query-builder.d.ts +27 -9
  154. package/dist/esm/query-builder/insert-query-builder.js +4 -4
  155. package/dist/esm/query-builder/insert-result.d.ts +8 -1
  156. package/dist/esm/query-builder/insert-result.js +8 -1
  157. package/dist/esm/query-builder/json-path-builder.d.ts +3 -2
  158. package/dist/esm/query-builder/json-path-builder.js +6 -3
  159. package/dist/esm/query-builder/on-conflict-builder.d.ts +58 -16
  160. package/dist/esm/query-builder/on-conflict-builder.js +2 -7
  161. package/dist/esm/query-builder/over-builder.d.ts +2 -2
  162. package/dist/esm/query-builder/select-query-builder.d.ts +38 -22
  163. package/dist/esm/query-builder/update-query-builder.d.ts +20 -4
  164. package/dist/esm/query-builder/update-query-builder.js +3 -3
  165. package/dist/esm/query-builder/where-interface.d.ts +3 -3
  166. package/dist/esm/query-compiler/default-query-compiler.d.ts +9 -1
  167. package/dist/esm/query-compiler/default-query-compiler.js +65 -12
  168. package/dist/esm/schema/alter-table-add-index-builder.d.ts +104 -0
  169. package/dist/esm/schema/alter-table-add-index-builder.js +143 -0
  170. package/dist/esm/schema/alter-table-builder.d.ts +41 -1
  171. package/dist/esm/schema/alter-table-builder.js +59 -2
  172. package/dist/esm/schema/column-definition-builder.d.ts +25 -0
  173. package/dist/esm/schema/column-definition-builder.js +27 -0
  174. package/dist/esm/schema/create-index-builder.d.ts +23 -0
  175. package/dist/esm/schema/create-index-builder.js +30 -0
  176. package/dist/esm/schema/create-table-builder.d.ts +28 -2
  177. package/dist/esm/schema/create-table-builder.js +38 -3
  178. package/dist/esm/schema/unique-constraint-builder.d.ts +14 -0
  179. package/dist/esm/schema/unique-constraint-builder.js +21 -0
  180. package/dist/esm/util/log.js +5 -1
  181. package/dist/esm/util/object-utils.d.ts +1 -1
  182. package/dist/esm/util/type-utils.d.ts +27 -19
  183. package/helpers/mssql.d.ts +1 -0
  184. package/helpers/mssql.js +1 -0
  185. package/package.json +17 -6
@@ -33,19 +33,15 @@ import { KyselyTypeError } from './type-error.js';
33
33
  * // Columns == 'id' | 'name' | 'species'
34
34
  * ```
35
35
  */
36
- export type AnyColumn<DB, TB extends keyof DB> = [
37
- DB
38
- ] extends [unknown] ? {
36
+ export type AnyColumn<DB, TB extends keyof DB> = {
39
37
  [T in TB]: keyof DB[T];
40
- }[TB] & string : never;
38
+ }[TB] & string;
41
39
  /**
42
40
  * Extracts a column type.
43
41
  */
44
- export type ExtractColumnType<DB, TB extends keyof DB, C> = [
45
- DB
46
- ] extends [unknown] ? {
42
+ export type ExtractColumnType<DB, TB extends keyof DB, C> = {
47
43
  [T in TB]: C extends keyof DB[T] ? DB[T][C] : never;
48
- }[TB] : never;
44
+ }[TB];
49
45
  /**
50
46
  * Given a database type and a union of table names in that db, returns
51
47
  * a union type with all possible `table`.`column` combinations.
@@ -77,21 +73,17 @@ export type ExtractColumnType<DB, TB extends keyof DB, C> = [
77
73
  * // Columns == 'person.id' | 'pet.name' | 'pet.species'
78
74
  * ```
79
75
  */
80
- export type AnyColumnWithTable<DB, TB extends keyof DB> = DrainOuterGeneric<{
81
- [T in TB]: T extends string ? keyof DB[T] extends string ? `${T}.${keyof DB[T]}` : never : never;
82
- }[TB]>;
76
+ export type AnyColumnWithTable<DB, TB extends keyof DB> = {
77
+ [T in TB]: `${T & string}.${keyof DB[T] & string}`;
78
+ }[TB];
83
79
  /**
84
80
  * Just like {@link AnyColumn} but with a ` as <string>` suffix.
85
81
  */
86
- export type AnyAliasedColumn<DB, TB extends keyof DB> = DrainOuterGeneric<{
87
- [T in TB]: T extends string ? keyof DB[T] extends string ? `${keyof DB[T]} as ${string}` : never : never;
88
- }[TB]>;
82
+ export type AnyAliasedColumn<DB, TB extends keyof DB> = `${AnyColumn<DB, TB>} as ${string}`;
89
83
  /**
90
84
  * Just like {@link AnyColumnWithTable} but with a ` as <string>` suffix.
91
85
  */
92
- export type AnyAliasedColumnWithTable<DB, TB extends keyof DB> = DrainOuterGeneric<{
93
- [T in TB]: T extends string ? keyof DB[T] extends string ? `${T}.${keyof DB[T]} as ${string}` : never : never;
94
- }[TB]>;
86
+ export type AnyAliasedColumnWithTable<DB, TB extends keyof DB> = `${AnyColumnWithTable<DB, TB>} as ${string}`;
95
87
  /**
96
88
  * Extracts the item type of an array.
97
89
  */
@@ -123,9 +115,25 @@ export type IsAny<T> = 0 extends T & 1 ? true : false;
123
115
  * Evaluates to `true` if the types `T` and `U` are equal.
124
116
  */
125
117
  export type Equals<T, U> = (<G>() => G extends T ? 1 : 2) extends <G>() => G extends U ? 1 : 2 ? true : false;
126
- export type NarrowPartial<S, T> = DrainOuterGeneric<T extends object ? {
127
- [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];
128
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
+ };
129
137
  export type SqlBool = boolean | 0 | 1;
130
138
  /**
131
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
+ }
@@ -0,0 +1,143 @@
1
+ export interface MssqlDialectConfig {
2
+ /**
3
+ * This dialect uses the `tarn` package to manage the connection pool to your
4
+ * database. To use it as a peer dependency and not bundle it with Kysely's code,
5
+ * you need to pass the `tarn` package itself. You also need to pass some pool options
6
+ * (excluding `create`, `destroy` and `validate` functions which are controlled by this dialect),
7
+ * `min` & `max` connections at the very least.
8
+ *
9
+ * Example:
10
+ *
11
+ * ```ts
12
+ * import * as Tarn from 'tarn'
13
+ *
14
+ * const dialect = new MssqlDialect({
15
+ * // ...
16
+ * tarn: {
17
+ * ...Tarn,
18
+ * options: {
19
+ * // ...
20
+ * min: 0,
21
+ * max: 10,
22
+ * },
23
+ * },
24
+ * })
25
+ * ```
26
+ */
27
+ tarn: Tarn;
28
+ /**
29
+ * This dialect uses the `tedious` package to communicate with your MS SQL Server
30
+ * database. To use it as a peer dependency and not bundle it with Kysely's code,
31
+ * you need to pass the `tedious` package itself. You also need to pass a factory
32
+ * function that creates new `tedious` `Connection` instances on demand.
33
+ *
34
+ * Example:
35
+ *
36
+ * ```ts
37
+ * import * as Tedious from 'tedious'
38
+ *
39
+ * const dialect = new MssqlDialect({
40
+ * // ...
41
+ * tedious: {
42
+ * ...Tedious,
43
+ * connectionFactory: () => new Tedious.Connection({ ... }),
44
+ * },
45
+ * })
46
+ * ```
47
+ */
48
+ tedious: Tedious;
49
+ }
50
+ export interface Tedious {
51
+ connectionFactory: () => TediousConnection | Promise<TediousConnection>;
52
+ ISOLATION_LEVEL: TediousIsolationLevel;
53
+ Request: typeof TediousRequest;
54
+ TYPES: TediousTypes;
55
+ }
56
+ export type TediousIsolationLevel = Record<'NO_CHANGE' | 'READ_UNCOMMITTED' | 'READ_COMMITTED' | 'REPEATABLE_READ' | 'SERIALIZABLE' | 'SNAPSHOT', number>;
57
+ export type TediousTypes = Record<'BigInt' | 'Binary' | 'Bit' | 'Char' | 'Date' | 'DateTime' | 'DateTime2' | 'DateTimeOffset' | 'Decimal' | 'Float' | 'Image' | 'Int' | 'Money' | 'NChar' | 'NText' | 'Null' | 'Numeric' | 'NVarChar' | 'Real' | 'SmallDateTime' | 'SmallInt' | 'SmallMoney' | 'Text' | 'Time' | 'TinyInt' | 'TVP' | 'UDT' | 'UniqueIdentifier' | 'VarBinary' | 'VarChar' | 'Xml', {
58
+ name: string;
59
+ type: string;
60
+ }>;
61
+ export interface TediousType {
62
+ name: string;
63
+ type: string;
64
+ }
65
+ export interface TediousConnection {
66
+ beginTransaction(callback: (error?: Error) => void, transactionId?: string | undefined, isolationLevel?: number | undefined): void;
67
+ cancel(): void;
68
+ close(): void;
69
+ commitTransaction(callback: (error?: Error) => void): void;
70
+ connect(callback: (error?: Error) => void): void;
71
+ execSql(request: TediousRequest): void;
72
+ reset(callback: (error?: Error) => void): void;
73
+ rollbackTransaction(callback: (error?: Error) => void): void;
74
+ once(event: 'end', listener: () => void): void;
75
+ }
76
+ export declare class TediousRequest {
77
+ constructor(sql: string, callback: (error: Error, rowCount: number, rows: any[]) => void);
78
+ addParameter(name: string, type: TediousType, value: any, options?: {
79
+ length?: number | 'max' | undefined;
80
+ precision?: number | undefined;
81
+ scale?: number | undefined;
82
+ }): void;
83
+ off(event: 'row', listener: (...args: any[]) => void): void;
84
+ on(event: 'row', listener: (columns: TediousColumnValue[]) => void): void;
85
+ once(event: 'requestCompleted', listener: (...args: any[]) => void): void;
86
+ }
87
+ export interface TediousColumnValue {
88
+ metadata: {
89
+ colName: string;
90
+ };
91
+ value: any;
92
+ }
93
+ export interface Tarn {
94
+ /**
95
+ * Tarn.js' pool options, excluding `create`, `destroy` and `validate` functions,
96
+ * which must be implemented by this dialect.
97
+ */
98
+ options: Omit<TarnPoolOptions<any>, 'create' | 'destroy' | 'validate'>;
99
+ /**
100
+ * Tarn.js' Pool class.
101
+ *
102
+ * Example:
103
+ *
104
+ * ```ts
105
+ * import { Pool } from 'tarn'
106
+ *
107
+ * const dialect = new MssqlDialect({
108
+ * // ...
109
+ * tarn: {
110
+ * // ...
111
+ * Pool,
112
+ * },
113
+ * })
114
+ * ```
115
+ */
116
+ Pool: typeof TarnPool;
117
+ }
118
+ export declare class TarnPool<R> {
119
+ constructor(opt: TarnPoolOptions<R>);
120
+ acquire(): TarnPendingRequest<R>;
121
+ destroy(): any;
122
+ release(resource: R): void;
123
+ }
124
+ export interface TarnPoolOptions<R> {
125
+ acquireTimeoutMillis?: number;
126
+ create(cb: (err: Error | null, resource: R) => void): any | (() => Promise<R>);
127
+ createRetryIntervalMillis?: number;
128
+ createTimeoutMillis?: number;
129
+ destroy(resource: R): any;
130
+ destroyTimeoutMillis?: number;
131
+ idleTimeoutMillis?: number;
132
+ log?(msg: string): any;
133
+ max: number;
134
+ min: number;
135
+ propagateCreateError?: boolean;
136
+ reapIntervalMillis?: number;
137
+ validate(resource: R): boolean;
138
+ }
139
+ export interface TarnPendingRequest<R> {
140
+ promise: Promise<R>;
141
+ resolve: (resource: R) => void;
142
+ reject: (err: Error) => void;
143
+ }
@@ -0,0 +1,2 @@
1
+ /// <reference types="./mssql-dialect-config.d.ts" />
2
+ export {};
@@ -0,0 +1,70 @@
1
+ import { Driver } from '../../driver/driver.js';
2
+ import { Kysely } from '../../kysely.js';
3
+ import { QueryCompiler } from '../../query-compiler/query-compiler.js';
4
+ import { DatabaseIntrospector } from '../database-introspector.js';
5
+ import { DialectAdapter } from '../dialect-adapter.js';
6
+ import { Dialect } from '../dialect.js';
7
+ import { MssqlDialectConfig } from './mssql-dialect-config.js';
8
+ /**
9
+ * MS SQL Server dialect that uses the [tedious](https://tediousjs.github.io/tedious)
10
+ * library.
11
+ *
12
+ * The constructor takes an instance of {@link MssqlDialectConfig}.
13
+ *
14
+ * ```ts
15
+ * import * as Tedious from 'tedious'
16
+ * import * as Tarn from 'tarn'
17
+ *
18
+ * const dialect = new MssqlDialect({
19
+ * tarn: {
20
+ * ...Tarn,
21
+ * options: {
22
+ * min: 0,
23
+ * max: 10,
24
+ * },
25
+ * },
26
+ * tedious: {
27
+ * ...Tedious,
28
+ * connectionFactory: () => new Tedious.Connection({
29
+ * authentication: {
30
+ * options: {
31
+ * password: 'password',
32
+ * userName: 'username',
33
+ * },
34
+ * type: 'default',
35
+ * },
36
+ * options: {
37
+ * database: 'some_db',
38
+ * port: 1433,
39
+ * trustServerCertificate: true,
40
+ * },
41
+ * server: 'localhost',
42
+ * }),
43
+ * },
44
+ * })
45
+ * ```
46
+ */
47
+ export declare class MssqlDialect implements Dialect {
48
+ #private;
49
+ constructor(config: MssqlDialectConfig);
50
+ /**
51
+ * Creates a driver for the dialect.
52
+ */
53
+ createDriver(): Driver;
54
+ /**
55
+ * Creates a query compiler for the dialect.
56
+ */
57
+ createQueryCompiler(): QueryCompiler;
58
+ /**
59
+ * Creates an adapter for the dialect.
60
+ */
61
+ createAdapter(): DialectAdapter;
62
+ /**
63
+ * Creates a database introspector that can be used to get database metadata
64
+ * such as the table names and column names of those tables.
65
+ *
66
+ * `db` never has any plugins installed. It's created using
67
+ * {@link Kysely.withoutPlugins}.
68
+ */
69
+ createIntrospector(db: Kysely<any>): DatabaseIntrospector;
70
+ }
@@ -0,0 +1,62 @@
1
+ /// <reference types="./mssql-dialect.d.ts" />
2
+ import { MssqlAdapter } from './mssql-adapter.js';
3
+ import { MssqlDriver } from './mssql-driver.js';
4
+ import { MssqlIntrospector } from './mssql-introspector.js';
5
+ import { MssqlQueryCompiler } from './mssql-query-compiler.js';
6
+ /**
7
+ * MS SQL Server dialect that uses the [tedious](https://tediousjs.github.io/tedious)
8
+ * library.
9
+ *
10
+ * The constructor takes an instance of {@link MssqlDialectConfig}.
11
+ *
12
+ * ```ts
13
+ * import * as Tedious from 'tedious'
14
+ * import * as Tarn from 'tarn'
15
+ *
16
+ * const dialect = new MssqlDialect({
17
+ * tarn: {
18
+ * ...Tarn,
19
+ * options: {
20
+ * min: 0,
21
+ * max: 10,
22
+ * },
23
+ * },
24
+ * tedious: {
25
+ * ...Tedious,
26
+ * connectionFactory: () => new Tedious.Connection({
27
+ * authentication: {
28
+ * options: {
29
+ * password: 'password',
30
+ * userName: 'username',
31
+ * },
32
+ * type: 'default',
33
+ * },
34
+ * options: {
35
+ * database: 'some_db',
36
+ * port: 1433,
37
+ * trustServerCertificate: true,
38
+ * },
39
+ * server: 'localhost',
40
+ * }),
41
+ * },
42
+ * })
43
+ * ```
44
+ */
45
+ export class MssqlDialect {
46
+ #config;
47
+ constructor(config) {
48
+ this.#config = config;
49
+ }
50
+ createDriver() {
51
+ return new MssqlDriver(this.#config);
52
+ }
53
+ createQueryCompiler() {
54
+ return new MssqlQueryCompiler();
55
+ }
56
+ createAdapter() {
57
+ return new MssqlAdapter();
58
+ }
59
+ createIntrospector(db) {
60
+ return new MssqlIntrospector(db);
61
+ }
62
+ }
@@ -0,0 +1,54 @@
1
+ import { DatabaseConnection, QueryResult } from '../../driver/database-connection.js';
2
+ import { Driver, TransactionSettings } from '../../driver/driver.js';
3
+ import { MssqlDialectConfig, Tedious, TediousConnection } from './mssql-dialect-config.js';
4
+ import { CompiledQuery } from '../../query-compiler/compiled-query.js';
5
+ declare const PRIVATE_RELEASE_METHOD: unique symbol;
6
+ declare const PRIVATE_DESTROY_METHOD: unique symbol;
7
+ export declare class MssqlDriver implements Driver {
8
+ #private;
9
+ constructor(config: MssqlDialectConfig);
10
+ /**
11
+ * Initializes the driver.
12
+ *
13
+ * After calling this method the driver should be usable and `acquireConnection` etc.
14
+ * methods should be callable.
15
+ */
16
+ init(): Promise<void>;
17
+ /**
18
+ * Acquires a new connection from the pool.
19
+ */
20
+ acquireConnection(): Promise<DatabaseConnection>;
21
+ /**
22
+ * Begins a transaction.
23
+ */
24
+ beginTransaction(connection: MssqlConnection, settings: TransactionSettings): Promise<void>;
25
+ /**
26
+ * Commits a transaction.
27
+ */
28
+ commitTransaction(connection: MssqlConnection): Promise<void>;
29
+ /**
30
+ * Rolls back a transaction.
31
+ */
32
+ rollbackTransaction(connection: MssqlConnection): Promise<void>;
33
+ /**
34
+ * Releases a connection back to the pool.
35
+ */
36
+ releaseConnection(connection: MssqlConnection): Promise<void>;
37
+ /**
38
+ * Destroys the driver and releases all resources.
39
+ */
40
+ destroy(): Promise<void>;
41
+ }
42
+ declare class MssqlConnection implements DatabaseConnection {
43
+ #private;
44
+ constructor(connection: TediousConnection, tedious: Tedious);
45
+ beginTransaction(settings: TransactionSettings): Promise<void>;
46
+ commitTransaction(): Promise<void>;
47
+ executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>>;
48
+ rollbackTransaction(): Promise<void>;
49
+ streamQuery<O>(compiledQuery: CompiledQuery, chunkSize: number): AsyncIterableIterator<QueryResult<O>>;
50
+ validate(): Promise<boolean>;
51
+ [PRIVATE_RELEASE_METHOD](): Promise<void>;
52
+ [PRIVATE_DESTROY_METHOD](): Promise<void>;
53
+ }
54
+ export {};