kysely 0.23.3 → 0.23.5

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 (118) hide show
  1. package/README.md +25 -25
  2. package/dist/cjs/dialect/database-introspector.d.ts +4 -2
  3. package/dist/cjs/dialect/dialect-adapter-base.d.ts +32 -6
  4. package/dist/cjs/dialect/dialect-adapter.d.ts +50 -8
  5. package/dist/cjs/dialect/mysql/mysql-adapter.d.ts +32 -6
  6. package/dist/cjs/dialect/mysql/mysql-introspector.d.ts +1 -1
  7. package/dist/cjs/dialect/mysql/mysql-introspector.js +21 -15
  8. package/dist/cjs/dialect/postgres/postgres-adapter.d.ts +32 -6
  9. package/dist/cjs/dialect/postgres/postgres-introspector.d.ts +1 -1
  10. package/dist/cjs/dialect/postgres/postgres-introspector.js +5 -3
  11. package/dist/cjs/dialect/sqlite/sqlite-adapter.d.ts +32 -6
  12. package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +1 -1
  13. package/dist/cjs/dialect/sqlite/sqlite-introspector.js +8 -7
  14. package/dist/cjs/index.d.ts +1 -0
  15. package/dist/cjs/index.js +1 -0
  16. package/dist/cjs/migration/migrator.d.ts +8 -3
  17. package/dist/cjs/migration/migrator.js +15 -5
  18. package/dist/cjs/operation-node/aggregate-function-node.d.ts +3 -2
  19. package/dist/cjs/operation-node/aggregate-function-node.js +2 -2
  20. package/dist/cjs/operation-node/create-index-node.d.ts +1 -0
  21. package/dist/cjs/operation-node/delete-query-node.d.ts +4 -1
  22. package/dist/cjs/operation-node/delete-query-node.js +11 -2
  23. package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
  24. package/dist/cjs/operation-node/operation-node-transformer.js +10 -1
  25. package/dist/cjs/operation-node/operation-node-visitor.d.ts +2 -0
  26. package/dist/cjs/operation-node/operation-node-visitor.js +1 -0
  27. package/dist/cjs/operation-node/operation-node.d.ts +1 -1
  28. package/dist/cjs/operation-node/operator-node.d.ts +1 -1
  29. package/dist/cjs/operation-node/operator-node.js +1 -0
  30. package/dist/cjs/operation-node/using-node.d.ts +13 -0
  31. package/dist/cjs/operation-node/using-node.js +24 -0
  32. package/dist/cjs/parser/expression-parser.js +1 -1
  33. package/dist/cjs/parser/table-parser.d.ts +1 -0
  34. package/dist/cjs/parser/update-set-parser.d.ts +1 -1
  35. package/dist/cjs/plugin/camel-case/camel-case-plugin.d.ts +7 -0
  36. package/dist/cjs/plugin/camel-case/camel-case-plugin.js +10 -4
  37. package/dist/cjs/query-builder/delete-query-builder.d.ts +103 -11
  38. package/dist/cjs/query-builder/delete-query-builder.js +40 -9
  39. package/dist/cjs/query-builder/function-module.d.ts +273 -39
  40. package/dist/cjs/query-builder/function-module.js +156 -34
  41. package/dist/cjs/query-builder/insert-query-builder.d.ts +25 -10
  42. package/dist/cjs/query-builder/insert-query-builder.js +32 -9
  43. package/dist/cjs/query-builder/select-query-builder.d.ts +51 -34
  44. package/dist/cjs/query-builder/select-query-builder.js +53 -29
  45. package/dist/cjs/query-builder/update-query-builder.d.ts +26 -10
  46. package/dist/cjs/query-builder/update-query-builder.js +33 -9
  47. package/dist/cjs/query-compiler/default-query-compiler.d.ts +2 -0
  48. package/dist/cjs/query-compiler/default-query-compiler.js +12 -1
  49. package/dist/cjs/query-creator.d.ts +29 -0
  50. package/dist/cjs/query-creator.js +2 -21
  51. package/dist/cjs/raw-builder/raw-builder.d.ts +4 -0
  52. package/dist/cjs/raw-builder/raw-builder.js +8 -2
  53. package/dist/cjs/schema/alter-table-builder.d.ts +5 -1
  54. package/dist/cjs/schema/alter-table-builder.js +8 -2
  55. package/dist/cjs/schema/create-index-builder.d.ts +6 -0
  56. package/dist/cjs/schema/create-index-builder.js +13 -0
  57. package/dist/cjs/schema/create-table-builder.d.ts +6 -2
  58. package/dist/cjs/schema/create-table-builder.js +9 -3
  59. package/dist/cjs/util/type-utils.d.ts +20 -1
  60. package/dist/esm/dialect/database-introspector.d.ts +4 -2
  61. package/dist/esm/dialect/dialect-adapter-base.d.ts +32 -6
  62. package/dist/esm/dialect/dialect-adapter.d.ts +50 -8
  63. package/dist/esm/dialect/mysql/mysql-adapter.d.ts +32 -6
  64. package/dist/esm/dialect/mysql/mysql-introspector.d.ts +1 -1
  65. package/dist/esm/dialect/mysql/mysql-introspector.js +21 -15
  66. package/dist/esm/dialect/postgres/postgres-adapter.d.ts +32 -6
  67. package/dist/esm/dialect/postgres/postgres-introspector.d.ts +1 -1
  68. package/dist/esm/dialect/postgres/postgres-introspector.js +5 -3
  69. package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +32 -6
  70. package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +1 -1
  71. package/dist/esm/dialect/sqlite/sqlite-introspector.js +8 -7
  72. package/dist/esm/index.d.ts +1 -0
  73. package/dist/esm/index.js +1 -0
  74. package/dist/esm/migration/migrator.d.ts +8 -3
  75. package/dist/esm/migration/migrator.js +15 -5
  76. package/dist/esm/operation-node/aggregate-function-node.d.ts +3 -2
  77. package/dist/esm/operation-node/aggregate-function-node.js +2 -2
  78. package/dist/esm/operation-node/create-index-node.d.ts +1 -0
  79. package/dist/esm/operation-node/delete-query-node.d.ts +4 -1
  80. package/dist/esm/operation-node/delete-query-node.js +11 -2
  81. package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
  82. package/dist/esm/operation-node/operation-node-transformer.js +10 -1
  83. package/dist/esm/operation-node/operation-node-visitor.d.ts +2 -0
  84. package/dist/esm/operation-node/operation-node-visitor.js +1 -0
  85. package/dist/esm/operation-node/operation-node.d.ts +1 -1
  86. package/dist/esm/operation-node/operator-node.d.ts +1 -1
  87. package/dist/esm/operation-node/operator-node.js +1 -0
  88. package/dist/esm/operation-node/using-node.d.ts +13 -0
  89. package/dist/esm/operation-node/using-node.js +22 -0
  90. package/dist/esm/parser/expression-parser.js +2 -2
  91. package/dist/esm/parser/table-parser.d.ts +1 -0
  92. package/dist/esm/parser/update-set-parser.d.ts +1 -1
  93. package/dist/esm/plugin/camel-case/camel-case-plugin.d.ts +7 -0
  94. package/dist/esm/plugin/camel-case/camel-case-plugin.js +10 -4
  95. package/dist/esm/query-builder/delete-query-builder.d.ts +103 -11
  96. package/dist/esm/query-builder/delete-query-builder.js +40 -9
  97. package/dist/esm/query-builder/function-module.d.ts +273 -39
  98. package/dist/esm/query-builder/function-module.js +156 -34
  99. package/dist/esm/query-builder/insert-query-builder.d.ts +25 -10
  100. package/dist/esm/query-builder/insert-query-builder.js +33 -10
  101. package/dist/esm/query-builder/select-query-builder.d.ts +51 -34
  102. package/dist/esm/query-builder/select-query-builder.js +53 -29
  103. package/dist/esm/query-builder/update-query-builder.d.ts +26 -10
  104. package/dist/esm/query-builder/update-query-builder.js +33 -9
  105. package/dist/esm/query-compiler/default-query-compiler.d.ts +2 -0
  106. package/dist/esm/query-compiler/default-query-compiler.js +12 -1
  107. package/dist/esm/query-creator.d.ts +29 -0
  108. package/dist/esm/query-creator.js +2 -21
  109. package/dist/esm/raw-builder/raw-builder.d.ts +4 -0
  110. package/dist/esm/raw-builder/raw-builder.js +8 -2
  111. package/dist/esm/schema/alter-table-builder.d.ts +5 -1
  112. package/dist/esm/schema/alter-table-builder.js +8 -2
  113. package/dist/esm/schema/create-index-builder.d.ts +6 -0
  114. package/dist/esm/schema/create-index-builder.js +13 -0
  115. package/dist/esm/schema/create-table-builder.d.ts +6 -2
  116. package/dist/esm/schema/create-table-builder.js +9 -3
  117. package/dist/esm/util/type-utils.d.ts +20 -1
  118. package/package.json +1 -1
@@ -198,6 +198,8 @@ export declare class QueryCreator<DB> {
198
198
  *
199
199
  * ### Examples
200
200
  *
201
+ * Deleting person with id 1:
202
+ *
201
203
  * ```ts
202
204
  * const result = await db
203
205
  * .deleteFrom('person')
@@ -206,7 +208,34 @@ export declare class QueryCreator<DB> {
206
208
  *
207
209
  * console.log(result.numDeletedRows)
208
210
  * ```
211
+ *
212
+ * The generated SQL (PostgreSQL):
213
+ *
214
+ * ```sql
215
+ * delete from "person" where "person"."id" = $1
216
+ * ```
217
+ *
218
+ * Some databases such as MySQL support deleting from multiple tables:
219
+ *
220
+ * ```ts
221
+ * const result = await db
222
+ * .deleteFrom(['person', 'pet'])
223
+ * .using('person')
224
+ * .innerJoin('pet', 'pet.owner_id', '=', 'person.id')
225
+ * .where('person.id', '=', 1)
226
+ * .executeTakeFirst()
227
+ * ```
228
+ *
229
+ * The generated SQL (MySQL):
230
+ *
231
+ * ```sql
232
+ * delete from `person`, `pet`
233
+ * using `person`
234
+ * inner join `pet` on `pet`.`owner_id` = `person`.`id`
235
+ * where `person`.`id` = ?
236
+ * ```
209
237
  */
238
+ deleteFrom<TR extends TableReference<DB>>(tables: TR[]): DeleteQueryBuilder<From<DB, TR>, FromTables<DB, never, TR>, DeleteResult>;
210
239
  deleteFrom<TR extends TableReference<DB>>(table: TR): DeleteQueryBuilder<From<DB, TR>, FromTables<DB, never, TR>, DeleteResult>;
211
240
  /**
212
241
  * Creates an update query.
@@ -105,30 +105,11 @@ class QueryCreator {
105
105
  queryNode: insert_query_node_js_1.InsertQueryNode.create((0, table_parser_js_1.parseTable)(table), this.#props.withNode, true),
106
106
  });
107
107
  }
108
- /**
109
- * Creates a delete query.
110
- *
111
- * See the {@link DeleteQueryBuilder.where} method for examples on how to specify
112
- * a where clause for the delete operation.
113
- *
114
- * The return value of the query is an instance of {@link DeleteResult}.
115
- *
116
- * ### Examples
117
- *
118
- * ```ts
119
- * const result = await db
120
- * .deleteFrom('person')
121
- * .where('person.id', '=', 1)
122
- * .executeTakeFirst()
123
- *
124
- * console.log(result.numDeletedRows)
125
- * ```
126
- */
127
- deleteFrom(table) {
108
+ deleteFrom(tables) {
128
109
  return new delete_query_builder_js_1.DeleteQueryBuilder({
129
110
  queryId: (0, query_id_js_1.createQueryId)(),
130
111
  executor: this.#props.executor,
131
- queryNode: delete_query_node_js_1.DeleteQueryNode.create((0, table_parser_js_1.parseTableExpression)(table), this.#props.withNode),
112
+ queryNode: delete_query_node_js_1.DeleteQueryNode.create((0, table_parser_js_1.parseTableExpressionOrList)(tables), this.#props.withNode),
132
113
  });
133
114
  }
134
115
  /**
@@ -93,6 +93,10 @@ export declare class RawBuilder<O> implements Expression<O> {
93
93
  * This method call doesn't change the SQL in any way. This methods simply
94
94
  * returns a copy of this `RawBuilder` with a new output type.
95
95
  */
96
+ $castTo<T>(): RawBuilder<T>;
97
+ /**
98
+ * @deprecated Use `$castTo` instead.
99
+ */
96
100
  castTo<T>(): RawBuilder<T>;
97
101
  /**
98
102
  * Adds a plugin for this SQL snippet.
@@ -31,9 +31,15 @@ class RawBuilder {
31
31
  * This method call doesn't change the SQL in any way. This methods simply
32
32
  * returns a copy of this `RawBuilder` with a new output type.
33
33
  */
34
- castTo() {
34
+ $castTo() {
35
35
  return new RawBuilder({ ...this.#props });
36
36
  }
37
+ /**
38
+ * @deprecated Use `$castTo` instead.
39
+ */
40
+ castTo() {
41
+ return this.$castTo();
42
+ }
37
43
  /**
38
44
  * Adds a plugin for this SQL snippet.
39
45
  */
@@ -68,7 +74,7 @@ exports.RawBuilder = RawBuilder;
68
74
  function isRawBuilder(obj) {
69
75
  return ((0, object_utils_js_1.isObject)(obj) &&
70
76
  (0, object_utils_js_1.isFunction)(obj.as) &&
71
- (0, object_utils_js_1.isFunction)(obj.castTo) &&
77
+ (0, object_utils_js_1.isFunction)(obj.$castTo) &&
72
78
  (0, object_utils_js_1.isFunction)(obj.withPlugin) &&
73
79
  (0, object_utils_js_1.isFunction)(obj.toOperationNode) &&
74
80
  (0, object_utils_js_1.isFunction)(obj.execute));
@@ -52,7 +52,11 @@ export declare class AlterTableBuilder implements ColumnAlteringInterface {
52
52
  /**
53
53
  * Calls the given function passing `this` as the only argument.
54
54
  *
55
- * See {@link CreateTableBuilder.call}
55
+ * See {@link CreateTableBuilder.$call}
56
+ */
57
+ $call<T>(func: (qb: this) => T): T;
58
+ /**
59
+ * @deprecated Use `$call` instead
56
60
  */
57
61
  call<T>(func: (qb: this) => T): T;
58
62
  }
@@ -128,11 +128,17 @@ class AlterTableBuilder {
128
128
  /**
129
129
  * Calls the given function passing `this` as the only argument.
130
130
  *
131
- * See {@link CreateTableBuilder.call}
131
+ * See {@link CreateTableBuilder.$call}
132
132
  */
133
- call(func) {
133
+ $call(func) {
134
134
  return func(this);
135
135
  }
136
+ /**
137
+ * @deprecated Use `$call` instead
138
+ */
139
+ call(func) {
140
+ return this.$call(func);
141
+ }
136
142
  }
137
143
  exports.AlterTableBuilder = AlterTableBuilder;
138
144
  class AlterTableColumnAlteringBuilder {
@@ -8,6 +8,12 @@ import { Expression } from '../expression/expression.js';
8
8
  export declare class CreateIndexBuilder implements OperationNodeSource, Compilable {
9
9
  #private;
10
10
  constructor(props: CreateIndexBuilderProps);
11
+ /**
12
+ * Adds the "if not exists" modifier.
13
+ *
14
+ * If the index already exists, no error is thrown if this method has been called.
15
+ */
16
+ ifNotExists(): CreateIndexBuilder;
11
17
  /**
12
18
  * Makes the index unique.
13
19
  */
@@ -13,6 +13,19 @@ class CreateIndexBuilder {
13
13
  constructor(props) {
14
14
  this.#props = (0, object_utils_js_1.freeze)(props);
15
15
  }
16
+ /**
17
+ * Adds the "if not exists" modifier.
18
+ *
19
+ * If the index already exists, no error is thrown if this method has been called.
20
+ */
21
+ ifNotExists() {
22
+ return new CreateIndexBuilder({
23
+ ...this.#props,
24
+ node: create_index_node_js_1.CreateIndexNode.cloneWith(this.#props.node, {
25
+ ifNotExists: true,
26
+ }),
27
+ });
28
+ }
16
29
  /**
17
30
  * Makes the index unique.
18
31
  */
@@ -211,7 +211,7 @@ export declare class CreateTableBuilder<TB extends string, C extends string = ne
211
211
  * ```ts
212
212
  * db.schema
213
213
  * .createTable('test')
214
- * .call((builder) => builder.addColumn('id', 'integer'))
214
+ * .$call((builder) => builder.addColumn('id', 'integer'))
215
215
  * .execute()
216
216
  * ```
217
217
  *
@@ -231,10 +231,14 @@ export declare class CreateTableBuilder<TB extends string, C extends string = ne
231
231
  *
232
232
  * db.schema
233
233
  * .createTable('test')
234
- * .call(addDefaultColumns)
234
+ * .$call(addDefaultColumns)
235
235
  * .execute()
236
236
  * ```
237
237
  */
238
+ $call<T>(func: (qb: this) => T): T;
239
+ /**
240
+ * @deprecated Use `$call` instead
241
+ */
238
242
  call<T>(func: (qb: this) => T): T;
239
243
  toOperationNode(): CreateTableNode;
240
244
  compile(): CompiledQuery;
@@ -278,7 +278,7 @@ class CreateTableBuilder {
278
278
  * ```ts
279
279
  * db.schema
280
280
  * .createTable('test')
281
- * .call((builder) => builder.addColumn('id', 'integer'))
281
+ * .$call((builder) => builder.addColumn('id', 'integer'))
282
282
  * .execute()
283
283
  * ```
284
284
  *
@@ -298,13 +298,19 @@ class CreateTableBuilder {
298
298
  *
299
299
  * db.schema
300
300
  * .createTable('test')
301
- * .call(addDefaultColumns)
301
+ * .$call(addDefaultColumns)
302
302
  * .execute()
303
303
  * ```
304
304
  */
305
- call(func) {
305
+ $call(func) {
306
306
  return func(this);
307
307
  }
308
+ /**
309
+ * @deprecated Use `$call` instead
310
+ */
311
+ call(func) {
312
+ return this.$call(func);
313
+ }
308
314
  toOperationNode() {
309
315
  return this.#props.executor.transformQuery(this.#props.node, this.#props.queryId);
310
316
  }
@@ -96,9 +96,22 @@ export declare type AnyAliasedColumnWithTable<DB, TB extends keyof DB> = {
96
96
  * Extracts the item type of an array.
97
97
  */
98
98
  export declare type ArrayItemType<T> = T extends ReadonlyArray<infer I> ? I : never;
99
+ /**
100
+ * Any select query builder.
101
+ */
99
102
  export declare type AnySelectQueryBuilder = SelectQueryBuilder<any, any, any>;
100
- export declare type SingleResultType<O> = O extends InsertResult ? O : O extends DeleteResult ? O : O extends UpdateResult ? O : O | undefined;
103
+ export declare type SimplifySingleResult<O> = O extends InsertResult ? O : O extends DeleteResult ? O : O extends UpdateResult ? O : Simplify<O> | undefined;
104
+ export declare type SimplifyResult<O> = O extends InsertResult ? O : O extends DeleteResult ? O : O extends UpdateResult ? O : Simplify<O>;
105
+ export declare type Simplify<T> = {
106
+ [K in keyof T]: T[K];
107
+ } & {};
108
+ /**
109
+ * Represents a database row whose column names and their types are unknown.
110
+ */
101
111
  export declare type UnknownRow = Record<string, unknown>;
112
+ /**
113
+ * Makes all properties of object type `T` nullable.
114
+ */
102
115
  export declare type Nullable<T> = {
103
116
  [P in keyof T]: T[P] | null;
104
117
  };
@@ -123,5 +136,11 @@ export declare type Nullable<T> = {
123
136
  * // { name: string, age: number, species?: 'cat' | 'dog' }
124
137
  */
125
138
  export declare type MergePartial<T1, T2> = T1 & Partial<Omit<T2, keyof T1>>;
139
+ /**
140
+ * Evaluates to `true` if `T` is `never`.
141
+ */
126
142
  export declare type IsNever<T> = [T] extends [never] ? true : false;
143
+ /**
144
+ * Evaluates to `true` if the types `T` and `U` are equal.
145
+ */
127
146
  export declare type Equals<T, U> = (<G>() => G extends T ? 1 : 2) extends <G>() => G extends U ? 1 : 2 ? true : false;
@@ -7,7 +7,7 @@ export interface DatabaseIntrospector {
7
7
  */
8
8
  getSchemas(): Promise<SchemaMetadata[]>;
9
9
  /**
10
- * Get table metadata.
10
+ * Get tables and views metadata.
11
11
  */
12
12
  getTables(options?: DatabaseMetadataOptions): Promise<TableMetadata[]>;
13
13
  /**
@@ -29,12 +29,14 @@ export interface SchemaMetadata {
29
29
  }
30
30
  export interface DatabaseMetadata {
31
31
  /**
32
- * The tables found in the database.
32
+ * The tables and views found in the database.
33
+ * * The propery isView can be used to tell them apart.
33
34
  */
34
35
  readonly tables: TableMetadata[];
35
36
  }
36
37
  export interface TableMetadata {
37
38
  readonly name: string;
39
+ readonly isView: boolean;
38
40
  readonly columns: ColumnMetadata[];
39
41
  readonly schema?: string;
40
42
  }
@@ -25,12 +25,38 @@ export declare abstract class DialectAdapterBase implements DialectAdapter {
25
25
  * Most dialects have explicit locks that can be used, like advisory locks
26
26
  * in PostgreSQL and the get_lock function in MySQL.
27
27
  *
28
- * If the dialect doesn't have explicit locks the {@link MIGRATION_LOCK_TABLE}
29
- * created by Kysely can be used instead. {@link MIGRATION_LOCK_TABLE}
30
- * has two columns `id` and `is_locked` and there's only one row in the
31
- * table whose id is {@link MIGRATION_LOCK_ID}. `is_locked` is an integer.
32
- * Kysely takes care of creating the lock table and inserting the one single
33
- * row to it before this method is executed.
28
+ * If the dialect doesn't have explicit locks the {@link MigrationLockOptions.lockTable}
29
+ * created by Kysely can be used instead. You can access it through the `options` object.
30
+ * The lock table has two columns `id` and `is_locked` and there's only one row in the table
31
+ * whose id is {@link MigrationLockOptions.lockRowId}. `is_locked` is an integer. Kysely
32
+ * takes care of creating the lock table and inserting the one single row to it before this
33
+ * method is executed. If the dialect supports schemas and the user has specified a custom
34
+ * schema in their migration settings, the options object also contains the schema name in
35
+ * {@link MigrationLockOptions.lockTableSchema}.
36
+ *
37
+ * Here's an example of how you might implement this method for a dialect that doesn't
38
+ * have explicit locks but supports `FOR UPDATE` row locks and transactional DDL:
39
+ *
40
+ * ```ts
41
+ * {
42
+ * async acquireMigrationLock(db, options): Promise<void> {
43
+ * const queryDb = options.lockTableSchema
44
+ * ? db.withSchema(options.lockTableSchema)
45
+ * : db
46
+ *
47
+ * // Since our imaginary dialect supports transactional DDL and has
48
+ * // row locks, we can simply take a row lock here and it will guarantee
49
+ * // all subsequent calls to this method from other transactions will
50
+ * // wait until this transaction finishes.
51
+ * await queryDb
52
+ * .selectFrom(options.lockTable)
53
+ * .selectAll()
54
+ * .where('id', '=', options.lockRowId)
55
+ * .forUpdate()
56
+ * .execute()
57
+ * }
58
+ * }
59
+ * ```
34
60
  *
35
61
  * If `supportsTransactionalDdl` is `true` then the `db` passed to this method
36
62
  * is a transaction inside which the migrations will be executed. Otherwise
@@ -26,19 +26,45 @@ export interface DialectAdapter {
26
26
  * Most dialects have explicit locks that can be used, like advisory locks
27
27
  * in PostgreSQL and the get_lock function in MySQL.
28
28
  *
29
- * If the dialect doesn't have explicit locks the {@link MIGRATION_LOCK_TABLE}
30
- * created by Kysely can be used instead. {@link MIGRATION_LOCK_TABLE}
31
- * has two columns `id` and `is_locked` and there's only one row in the
32
- * table whose id is {@link MIGRATION_LOCK_ID}. `is_locked` is an integer.
33
- * Kysely takes care of creating the lock table and inserting the one single
34
- * row to it before this method is executed.
29
+ * If the dialect doesn't have explicit locks the {@link MigrationLockOptions.lockTable}
30
+ * created by Kysely can be used instead. You can access it through the `options` object.
31
+ * The lock table has two columns `id` and `is_locked` and there's only one row in the table
32
+ * whose id is {@link MigrationLockOptions.lockRowId}. `is_locked` is an integer. Kysely
33
+ * takes care of creating the lock table and inserting the one single row to it before this
34
+ * method is executed. If the dialect supports schemas and the user has specified a custom
35
+ * schema in their migration settings, the options object also contains the schema name in
36
+ * {@link MigrationLockOptions.lockTableSchema}.
37
+ *
38
+ * Here's an example of how you might implement this method for a dialect that doesn't
39
+ * have explicit locks but supports `FOR UPDATE` row locks and transactional DDL:
40
+ *
41
+ * ```ts
42
+ * {
43
+ * async acquireMigrationLock(db, options): Promise<void> {
44
+ * const queryDb = options.lockTableSchema
45
+ * ? db.withSchema(options.lockTableSchema)
46
+ * : db
47
+ *
48
+ * // Since our imaginary dialect supports transactional DDL and has
49
+ * // row locks, we can simply take a row lock here and it will guarantee
50
+ * // all subsequent calls to this method from other transactions will
51
+ * // wait until this transaction finishes.
52
+ * await queryDb
53
+ * .selectFrom(options.lockTable)
54
+ * .selectAll()
55
+ * .where('id', '=', options.lockRowId)
56
+ * .forUpdate()
57
+ * .execute()
58
+ * }
59
+ * }
60
+ * ```
35
61
  *
36
62
  * If `supportsTransactionalDdl` is `true` then the `db` passed to this method
37
63
  * is a transaction inside which the migrations will be executed. Otherwise
38
64
  * `db` is a single connection (session) that will be used to execute the
39
65
  * migrations.
40
66
  */
41
- acquireMigrationLock(db: Kysely<any>): Promise<void>;
67
+ acquireMigrationLock(db: Kysely<any>, options: MigrationLockOptions): Promise<void>;
42
68
  /**
43
69
  * Releases the migration lock. See {@link acquireMigrationLock}.
44
70
  *
@@ -47,5 +73,21 @@ export interface DialectAdapter {
47
73
  * is a single connection (session) that was used to execute the migrations
48
74
  * and the `acquireMigrationLock` call.
49
75
  */
50
- releaseMigrationLock(db: Kysely<any>): Promise<void>;
76
+ releaseMigrationLock(db: Kysely<any>, options: MigrationLockOptions): Promise<void>;
77
+ }
78
+ export interface MigrationLockOptions {
79
+ /**
80
+ * The name of the migration lock table.
81
+ */
82
+ readonly lockTable: string;
83
+ /**
84
+ * The id of the only row in the migration lock table.
85
+ */
86
+ readonly lockRowId: string;
87
+ /**
88
+ * The schema in which the migration lock table lives. This is only
89
+ * defined if the user has specified a custom schema in the migration
90
+ * settings.
91
+ */
92
+ readonly lockTableSchema?: string;
51
93
  }
@@ -19,12 +19,38 @@ export declare class MysqlAdapter extends DialectAdapterBase {
19
19
  * Most dialects have explicit locks that can be used, like advisory locks
20
20
  * in PostgreSQL and the get_lock function in MySQL.
21
21
  *
22
- * If the dialect doesn't have explicit locks the {@link MIGRATION_LOCK_TABLE}
23
- * created by Kysely can be used instead. {@link MIGRATION_LOCK_TABLE}
24
- * has two columns `id` and `is_locked` and there's only one row in the
25
- * table whose id is {@link MIGRATION_LOCK_ID}. `is_locked` is an integer.
26
- * Kysely takes care of creating the lock table and inserting the one single
27
- * row to it before this method is executed.
22
+ * If the dialect doesn't have explicit locks the {@link MigrationLockOptions.lockTable}
23
+ * created by Kysely can be used instead. You can access it through the `options` object.
24
+ * The lock table has two columns `id` and `is_locked` and there's only one row in the table
25
+ * whose id is {@link MigrationLockOptions.lockRowId}. `is_locked` is an integer. Kysely
26
+ * takes care of creating the lock table and inserting the one single row to it before this
27
+ * method is executed. If the dialect supports schemas and the user has specified a custom
28
+ * schema in their migration settings, the options object also contains the schema name in
29
+ * {@link MigrationLockOptions.lockTableSchema}.
30
+ *
31
+ * Here's an example of how you might implement this method for a dialect that doesn't
32
+ * have explicit locks but supports `FOR UPDATE` row locks and transactional DDL:
33
+ *
34
+ * ```ts
35
+ * {
36
+ * async acquireMigrationLock(db, options): Promise<void> {
37
+ * const queryDb = options.lockTableSchema
38
+ * ? db.withSchema(options.lockTableSchema)
39
+ * : db
40
+ *
41
+ * // Since our imaginary dialect supports transactional DDL and has
42
+ * // row locks, we can simply take a row lock here and it will guarantee
43
+ * // all subsequent calls to this method from other transactions will
44
+ * // wait until this transaction finishes.
45
+ * await queryDb
46
+ * .selectFrom(options.lockTable)
47
+ * .selectAll()
48
+ * .where('id', '=', options.lockRowId)
49
+ * .forUpdate()
50
+ * .execute()
51
+ * }
52
+ * }
53
+ * ```
28
54
  *
29
55
  * If `supportsTransactionalDdl` is `true` then the `db` passed to this method
30
56
  * is a transaction inside which the migrations will be executed. Otherwise
@@ -8,7 +8,7 @@ export declare class MysqlIntrospector implements DatabaseIntrospector {
8
8
  */
9
9
  getSchemas(): Promise<SchemaMetadata[]>;
10
10
  /**
11
- * Get table metadata.
11
+ * Get tables and views metadata.
12
12
  */
13
13
  getTables(options?: DatabaseMetadataOptions): Promise<TableMetadata[]>;
14
14
  /**
@@ -11,30 +11,35 @@ export class MysqlIntrospector {
11
11
  let rawSchemas = await this.#db
12
12
  .selectFrom('information_schema.schemata')
13
13
  .select('schema_name')
14
- .castTo()
14
+ .$castTo()
15
15
  .execute();
16
16
  return rawSchemas.map((it) => ({ name: it.SCHEMA_NAME }));
17
17
  }
18
18
  async getTables(options = { withInternalKyselyTables: false }) {
19
19
  let query = this.#db
20
- .selectFrom('information_schema.columns')
20
+ .selectFrom('information_schema.columns as columns')
21
+ .innerJoin('information_schema.tables as tables', (b) => b
22
+ .onRef('columns.TABLE_CATALOG', '=', 'tables.TABLE_CATALOG')
23
+ .onRef('columns.TABLE_SCHEMA', '=', 'tables.TABLE_SCHEMA')
24
+ .onRef('columns.TABLE_NAME', '=', 'tables.TABLE_NAME'))
21
25
  .select([
22
- 'COLUMN_NAME',
23
- 'COLUMN_DEFAULT',
24
- 'TABLE_NAME',
25
- 'TABLE_SCHEMA',
26
- 'IS_NULLABLE',
27
- 'DATA_TYPE',
28
- 'EXTRA',
26
+ 'columns.COLUMN_NAME',
27
+ 'columns.COLUMN_DEFAULT',
28
+ 'columns.TABLE_NAME',
29
+ 'columns.TABLE_SCHEMA',
30
+ 'tables.TABLE_TYPE',
31
+ 'columns.IS_NULLABLE',
32
+ 'columns.DATA_TYPE',
33
+ 'columns.EXTRA',
29
34
  ])
30
- .where('table_schema', '=', sql `database()`)
31
- .orderBy('table_name')
32
- .orderBy('ordinal_position')
33
- .castTo();
35
+ .where('columns.TABLE_SCHEMA', '=', sql `database()`)
36
+ .orderBy('columns.TABLE_NAME')
37
+ .orderBy('columns.ORDINAL_POSITION')
38
+ .$castTo();
34
39
  if (!options.withInternalKyselyTables) {
35
40
  query = query
36
- .where('table_name', '!=', DEFAULT_MIGRATION_TABLE)
37
- .where('table_name', '!=', DEFAULT_MIGRATION_LOCK_TABLE);
41
+ .where('columns.TABLE_NAME', '!=', DEFAULT_MIGRATION_TABLE)
42
+ .where('columns.TABLE_NAME', '!=', DEFAULT_MIGRATION_LOCK_TABLE);
38
43
  }
39
44
  const rawColumns = await query.execute();
40
45
  return this.#parseTableMetadata(rawColumns);
@@ -50,6 +55,7 @@ export class MysqlIntrospector {
50
55
  if (!table) {
51
56
  table = freeze({
52
57
  name: it.TABLE_NAME,
58
+ isView: it.TABLE_TYPE === 'VIEW',
53
59
  schema: it.TABLE_SCHEMA,
54
60
  columns: [],
55
61
  });
@@ -19,12 +19,38 @@ export declare class PostgresAdapter extends DialectAdapterBase {
19
19
  * Most dialects have explicit locks that can be used, like advisory locks
20
20
  * in PostgreSQL and the get_lock function in MySQL.
21
21
  *
22
- * If the dialect doesn't have explicit locks the {@link MIGRATION_LOCK_TABLE}
23
- * created by Kysely can be used instead. {@link MIGRATION_LOCK_TABLE}
24
- * has two columns `id` and `is_locked` and there's only one row in the
25
- * table whose id is {@link MIGRATION_LOCK_ID}. `is_locked` is an integer.
26
- * Kysely takes care of creating the lock table and inserting the one single
27
- * row to it before this method is executed.
22
+ * If the dialect doesn't have explicit locks the {@link MigrationLockOptions.lockTable}
23
+ * created by Kysely can be used instead. You can access it through the `options` object.
24
+ * The lock table has two columns `id` and `is_locked` and there's only one row in the table
25
+ * whose id is {@link MigrationLockOptions.lockRowId}. `is_locked` is an integer. Kysely
26
+ * takes care of creating the lock table and inserting the one single row to it before this
27
+ * method is executed. If the dialect supports schemas and the user has specified a custom
28
+ * schema in their migration settings, the options object also contains the schema name in
29
+ * {@link MigrationLockOptions.lockTableSchema}.
30
+ *
31
+ * Here's an example of how you might implement this method for a dialect that doesn't
32
+ * have explicit locks but supports `FOR UPDATE` row locks and transactional DDL:
33
+ *
34
+ * ```ts
35
+ * {
36
+ * async acquireMigrationLock(db, options): Promise<void> {
37
+ * const queryDb = options.lockTableSchema
38
+ * ? db.withSchema(options.lockTableSchema)
39
+ * : db
40
+ *
41
+ * // Since our imaginary dialect supports transactional DDL and has
42
+ * // row locks, we can simply take a row lock here and it will guarantee
43
+ * // all subsequent calls to this method from other transactions will
44
+ * // wait until this transaction finishes.
45
+ * await queryDb
46
+ * .selectFrom(options.lockTable)
47
+ * .selectAll()
48
+ * .where('id', '=', options.lockRowId)
49
+ * .forUpdate()
50
+ * .execute()
51
+ * }
52
+ * }
53
+ * ```
28
54
  *
29
55
  * If `supportsTransactionalDdl` is `true` then the `db` passed to this method
30
56
  * is a transaction inside which the migrations will be executed. Otherwise
@@ -8,7 +8,7 @@ export declare class PostgresIntrospector implements DatabaseIntrospector {
8
8
  */
9
9
  getSchemas(): Promise<SchemaMetadata[]>;
10
10
  /**
11
- * Get table metadata.
11
+ * Get tables and views metadata.
12
12
  */
13
13
  getTables(options?: DatabaseMetadataOptions): Promise<TableMetadata[]>;
14
14
  /**
@@ -11,7 +11,7 @@ export class PostgresIntrospector {
11
11
  let rawSchemas = await this.#db
12
12
  .selectFrom('pg_catalog.pg_namespace')
13
13
  .select('nspname')
14
- .castTo()
14
+ .$castTo()
15
15
  .execute();
16
16
  return rawSchemas.map((it) => ({ name: it.nspname }));
17
17
  }
@@ -32,6 +32,7 @@ export class PostgresIntrospector {
32
32
  'a.attnotnull as not_null',
33
33
  'a.atthasdef as has_default',
34
34
  'c.relname as table',
35
+ 'c.relkind as table_type',
35
36
  'ns.nspname as schema',
36
37
  'typ.typname as type',
37
38
  'dtns.nspname as type_schema',
@@ -47,7 +48,7 @@ export class PostgresIntrospector {
47
48
  .as('auto_incrementing'),
48
49
  ])
49
50
  // r == normal table
50
- .where('c.relkind', '=', 'r')
51
+ .where('c.relkind', 'in', ['r', 'v'])
51
52
  .where('ns.nspname', '!~', '^pg_')
52
53
  .where('ns.nspname', '!=', 'information_schema')
53
54
  // No system columns
@@ -56,7 +57,7 @@ export class PostgresIntrospector {
56
57
  .orderBy('ns.nspname')
57
58
  .orderBy('c.relname')
58
59
  .orderBy('a.attnum')
59
- .castTo();
60
+ .$castTo();
60
61
  if (!options.withInternalKyselyTables) {
61
62
  query = query
62
63
  .where('c.relname', '!=', DEFAULT_MIGRATION_TABLE)
@@ -76,6 +77,7 @@ export class PostgresIntrospector {
76
77
  if (!table) {
77
78
  table = freeze({
78
79
  name: it.table,
80
+ isView: it.table_type === 'v',
79
81
  schema: it.schema,
80
82
  columns: [],
81
83
  });