kysely 0.19.11 → 0.20.1

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 (116) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/dialect/mysql/mysql-introspector.js +2 -0
  3. package/dist/cjs/dialect/mysql/mysql-query-compiler.d.ts +5 -0
  4. package/dist/cjs/dialect/mysql/mysql-query-compiler.js +16 -0
  5. package/dist/cjs/dialect/postgres/postgres-introspector.js +17 -9
  6. package/dist/cjs/dialect/postgres/postgres-query-compiler.d.ts +1 -3
  7. package/dist/cjs/dialect/postgres/postgres-query-compiler.js +3 -8
  8. package/dist/cjs/dialect/sqlite/sqlite-introspector.js +2 -0
  9. package/dist/cjs/dialect/sqlite/sqlite-query-compiler.d.ts +3 -0
  10. package/dist/cjs/dialect/sqlite/sqlite-query-compiler.js +10 -0
  11. package/dist/cjs/index.d.ts +3 -0
  12. package/dist/cjs/index.js +3 -0
  13. package/dist/cjs/operation-node/create-table-node.d.ts +9 -2
  14. package/dist/cjs/operation-node/create-table-node.js +18 -1
  15. package/dist/cjs/operation-node/delete-query-node.d.ts +3 -0
  16. package/dist/cjs/operation-node/delete-query-node.js +6 -0
  17. package/dist/cjs/operation-node/explain-node.d.ts +16 -0
  18. package/dist/cjs/operation-node/explain-node.js +19 -0
  19. package/dist/cjs/operation-node/foreign-key-constraint-node.d.ts +2 -2
  20. package/dist/cjs/operation-node/insert-query-node.d.ts +2 -0
  21. package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
  22. package/dist/cjs/operation-node/operation-node-transformer.js +14 -0
  23. package/dist/cjs/operation-node/operation-node-visitor.d.ts +2 -0
  24. package/dist/cjs/operation-node/operation-node-visitor.js +1 -0
  25. package/dist/cjs/operation-node/operation-node.d.ts +1 -1
  26. package/dist/cjs/operation-node/reference-node.d.ts +1 -1
  27. package/dist/cjs/operation-node/reference-node.js +1 -2
  28. package/dist/cjs/operation-node/references-node.d.ts +3 -1
  29. package/dist/cjs/operation-node/references-node.js +8 -1
  30. package/dist/cjs/operation-node/select-query-node.d.ts +3 -0
  31. package/dist/cjs/operation-node/select-query-node.js +6 -0
  32. package/dist/cjs/operation-node/selection-node.d.ts +2 -1
  33. package/dist/cjs/operation-node/update-query-node.d.ts +3 -0
  34. package/dist/cjs/operation-node/update-query-node.js +6 -0
  35. package/dist/cjs/parser/on-commit-action-parse.d.ts +2 -0
  36. package/dist/cjs/parser/on-commit-action-parse.js +11 -0
  37. package/dist/cjs/parser/on-modify-action-parser.d.ts +2 -0
  38. package/dist/cjs/parser/on-modify-action-parser.js +11 -0
  39. package/dist/cjs/parser/select-parser.js +2 -1
  40. package/dist/cjs/query-builder/delete-query-builder.d.ts +19 -1
  41. package/dist/cjs/query-builder/delete-query-builder.js +24 -0
  42. package/dist/cjs/query-builder/function-builder.d.ts +3 -3
  43. package/dist/cjs/query-builder/insert-query-builder.d.ts +20 -2
  44. package/dist/cjs/query-builder/insert-query-builder.js +26 -0
  45. package/dist/cjs/query-builder/select-query-builder.d.ts +38 -1
  46. package/dist/cjs/query-builder/select-query-builder.js +45 -2
  47. package/dist/cjs/query-builder/update-query-builder.d.ts +20 -1
  48. package/dist/cjs/query-builder/update-query-builder.js +25 -0
  49. package/dist/cjs/query-compiler/default-query-compiler.d.ts +7 -0
  50. package/dist/cjs/query-compiler/default-query-compiler.js +74 -1
  51. package/dist/cjs/raw-builder/sql.d.ts +96 -96
  52. package/dist/cjs/raw-builder/sql.js +0 -96
  53. package/dist/cjs/schema/column-definition-builder.js +3 -2
  54. package/dist/cjs/schema/create-table-builder.d.ts +54 -0
  55. package/dist/cjs/schema/create-table-builder.js +66 -1
  56. package/dist/cjs/schema/foreign-key-constraint-builder.js +3 -2
  57. package/dist/cjs/util/explainable.d.ts +5 -0
  58. package/dist/cjs/util/explainable.js +2 -0
  59. package/dist/esm/dialect/mysql/mysql-introspector.js +2 -0
  60. package/dist/esm/dialect/mysql/mysql-query-compiler.d.ts +5 -0
  61. package/dist/esm/dialect/mysql/mysql-query-compiler.js +16 -0
  62. package/dist/esm/dialect/postgres/postgres-introspector.js +17 -9
  63. package/dist/esm/dialect/postgres/postgres-query-compiler.d.ts +1 -3
  64. package/dist/esm/dialect/postgres/postgres-query-compiler.js +3 -8
  65. package/dist/esm/dialect/sqlite/sqlite-introspector.js +2 -0
  66. package/dist/esm/dialect/sqlite/sqlite-query-compiler.d.ts +3 -0
  67. package/dist/esm/dialect/sqlite/sqlite-query-compiler.js +10 -0
  68. package/dist/esm/index.d.ts +3 -0
  69. package/dist/esm/index.js +3 -0
  70. package/dist/esm/operation-node/create-table-node.d.ts +9 -2
  71. package/dist/esm/operation-node/create-table-node.js +17 -0
  72. package/dist/esm/operation-node/delete-query-node.d.ts +3 -0
  73. package/dist/esm/operation-node/delete-query-node.js +6 -0
  74. package/dist/esm/operation-node/explain-node.d.ts +16 -0
  75. package/dist/esm/operation-node/explain-node.js +17 -0
  76. package/dist/esm/operation-node/foreign-key-constraint-node.d.ts +2 -2
  77. package/dist/esm/operation-node/insert-query-node.d.ts +2 -0
  78. package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
  79. package/dist/esm/operation-node/operation-node-transformer.js +14 -0
  80. package/dist/esm/operation-node/operation-node-visitor.d.ts +2 -0
  81. package/dist/esm/operation-node/operation-node-visitor.js +1 -0
  82. package/dist/esm/operation-node/operation-node.d.ts +1 -1
  83. package/dist/esm/operation-node/reference-node.d.ts +1 -1
  84. package/dist/esm/operation-node/reference-node.js +1 -2
  85. package/dist/esm/operation-node/references-node.d.ts +3 -1
  86. package/dist/esm/operation-node/references-node.js +7 -0
  87. package/dist/esm/operation-node/select-query-node.d.ts +3 -0
  88. package/dist/esm/operation-node/select-query-node.js +6 -0
  89. package/dist/esm/operation-node/selection-node.d.ts +2 -1
  90. package/dist/esm/operation-node/update-query-node.d.ts +3 -0
  91. package/dist/esm/operation-node/update-query-node.js +6 -0
  92. package/dist/esm/parser/on-commit-action-parse.d.ts +2 -0
  93. package/dist/esm/parser/on-commit-action-parse.js +8 -0
  94. package/dist/esm/parser/on-modify-action-parser.d.ts +2 -0
  95. package/dist/esm/parser/on-modify-action-parser.js +8 -0
  96. package/dist/esm/parser/select-parser.js +2 -1
  97. package/dist/esm/query-builder/delete-query-builder.d.ts +19 -1
  98. package/dist/esm/query-builder/delete-query-builder.js +24 -0
  99. package/dist/esm/query-builder/function-builder.d.ts +3 -3
  100. package/dist/esm/query-builder/insert-query-builder.d.ts +20 -2
  101. package/dist/esm/query-builder/insert-query-builder.js +26 -0
  102. package/dist/esm/query-builder/select-query-builder.d.ts +38 -1
  103. package/dist/esm/query-builder/select-query-builder.js +45 -2
  104. package/dist/esm/query-builder/update-query-builder.d.ts +20 -1
  105. package/dist/esm/query-builder/update-query-builder.js +25 -0
  106. package/dist/esm/query-compiler/default-query-compiler.d.ts +7 -0
  107. package/dist/esm/query-compiler/default-query-compiler.js +74 -1
  108. package/dist/esm/raw-builder/sql.d.ts +96 -96
  109. package/dist/esm/raw-builder/sql.js +0 -96
  110. package/dist/esm/schema/column-definition-builder.js +3 -2
  111. package/dist/esm/schema/create-table-builder.d.ts +54 -0
  112. package/dist/esm/schema/create-table-builder.js +66 -1
  113. package/dist/esm/schema/foreign-key-constraint-builder.js +3 -2
  114. package/dist/esm/util/explainable.d.ts +5 -0
  115. package/dist/esm/util/explainable.js +2 -0
  116. package/package.json +3 -1
@@ -1,5 +1,101 @@
1
1
  import { RawBuilder } from './raw-builder.js';
2
2
  export interface Sql {
3
+ /**
4
+ * Template tag for creating raw SQL snippets and queries.
5
+ *
6
+ * ```ts
7
+ * import { sql } from 'kysely'
8
+ *
9
+ * const id = 123
10
+ *
11
+ * const snippet = sql<Person[]>`select * from person where id = ${id}`
12
+ * ```
13
+ *
14
+ * Substitutions (the things inside `${}`) are automatically passed to the database
15
+ * as parameters and are NOT interpolated to the SQL string. There's no need to worry
16
+ * about SQL injection vulnerabilities.
17
+ *
18
+ * If you need your substitutions to be interpreted as identifiers, value literals or
19
+ * lists of things, see the {@link Sql.ref}, {@link Sql.table}, {@link Sql.id},
20
+ * {@link Sql.literal}, {@link Sql.raw} and {@link Sql.join} functions.
21
+ *
22
+ * You can pass sql snippets returned by the `sql` tag pretty much anywhere. Whenever
23
+ * something can't be done using the Kysely API, you should be able to drop down to
24
+ * raw SQL using the `sql` tag. Here's an example query that uses raw sql in a bunch
25
+ * of methods:
26
+ *
27
+ * ```ts
28
+ * const persons = await db
29
+ * .selectFrom('person')
30
+ * .select(
31
+ * // If you use `sql` in a select statement, remember to call the `as`
32
+ * // method to give it an alias. Otherwise Kysely is not able to add
33
+ * // the column to the result type.
34
+ * sql<string>`concat(first_name, ' ', last_name)`.as('full_name')
35
+ * )
36
+ * .where(sql`birthdate between ${date1} and ${date2}`)
37
+ * // Here we assume we have list of nicknames for the person
38
+ * // (a list of strings) and we use the PostgreSQL `@>` operator
39
+ * // to test if all of them are valid nicknames for the user.
40
+ * .where('nicknames', '@>', sql`ARRAY[${sql.join(nicknames)}]`)
41
+ * .orderBy(sql`concat(first_name, ' ', last_name)`)
42
+ * .execute()
43
+ * ```
44
+ *
45
+ * The generated SQL (PostgreSQL):
46
+ *
47
+ * ```sql
48
+ * select concat(first_name, ' ', last_name) as "full_name"
49
+ * from "person"
50
+ * where birthdate between $1 and $2
51
+ * and "nicknames" @> ARRAY[$3, $4, $5, $6, $7, $8, $9, $10]
52
+ * order by concat(first_name, ' ', last_name)
53
+ * ```
54
+ *
55
+ * SQL snippets can be executed by calling the `execute` method and passing a `Kysely`
56
+ * instance as the only argument:
57
+ *
58
+ * ```ts
59
+ * const result = await sql<Person[]>`select * from person`.execute(db)
60
+ * ```
61
+ *
62
+ * You can merge other `sql` expressions and queries using substitutions:
63
+ *
64
+ * ```ts
65
+ * const petName = db.selectFrom('pet').select('name').limit(1)
66
+ * const fullName = sql`concat(first_name, ' ', last_name)`
67
+ *
68
+ * sql`
69
+ * select ${fullName} as full_name, ${petName} as pet_name
70
+ * from person
71
+ * `
72
+ * ```
73
+ *
74
+ * Substitutions also handle {@link ExpressionBuilder.ref},
75
+ * {@link DynamicModule.ref} and pretty much anything else you
76
+ * throw at it. Here's an example of calling a function in a
77
+ * type-safe way:
78
+ *
79
+ * ```ts
80
+ * db.selectFrom('person')
81
+ * .select([
82
+ * 'first_name',
83
+ * 'last_name',
84
+ * (eb) => {
85
+ * // The `eb.ref` method is type-safe and only accepts
86
+ * // column references that are possible.
87
+ * const firstName = eb.ref('first_name')
88
+ * const lastName = eb.ref('last_name')
89
+ *
90
+ * const fullName = sql<string>`concat(${firstName}, ' ', ${lastName})`
91
+ * return fullName.as('full_name')
92
+ * }
93
+ * ])
94
+ * ```
95
+ *
96
+ * don't know if that amount of ceremony is worth the small increase in
97
+ * type-safety though... But it's possible.
98
+ */
3
99
  <T = unknown>(sqlFragments: TemplateStringsArray, ...parameters: unknown[]): RawBuilder<T>;
4
100
  /**
5
101
  * This can be used to add runtime column references to SQL snippets.
@@ -232,100 +328,4 @@ export interface Sql {
232
328
  */
233
329
  join(array: unknown[], separator?: RawBuilder<any>): RawBuilder<unknown>;
234
330
  }
235
- /**
236
- * Template tag for creating raw SQL snippets and queries.
237
- *
238
- * ```ts
239
- * import { sql } from 'kysely'
240
- *
241
- * const id = 123
242
- *
243
- * const snippet = sql<Person[]>`select * from person where id = ${id}`
244
- * ```
245
- *
246
- * Substitutions (the things inside `${}`) are automatically passed to the database
247
- * as parameters and are NOT interpolated to the SQL string. There's no need to worry
248
- * about SQL injection vulnerabilities.
249
- *
250
- * If you need your substitutions to be interpreted as identifiers, value literals or
251
- * lists of things, see the {@link Sql.ref}, {@link Sql.table}, {@link Sql.id},
252
- * {@link Sql.literal}, {@link Sql.raw} and {@link Sql.join} functions.
253
- *
254
- * You can pass sql snippets returned by the `sql` tag pretty much anywhere. Whenever
255
- * something can't be done using the Kysely API, you should be able to drop down to
256
- * raw SQL using the `sql` tag. Here's an example query that uses raw sql in a bunch
257
- * of methods:
258
- *
259
- * ```ts
260
- * const persons = await db
261
- * .selectFrom('person')
262
- * .select(
263
- * // If you use `sql` in a select statement, remember to call the `as`
264
- * // method to give it an alias. Otherwise Kysely is not able to add
265
- * // the column to the result type.
266
- * sql<string>`concat(first_name, ' ', last_name)`.as('full_name')
267
- * )
268
- * .where(sql`birthdate between ${date1} and ${date2}`)
269
- * // Here we assume we have list of nicknames for the person
270
- * // (a list of strings) and we use the PostgreSQL `@>` operator
271
- * // to test if all of them are valid nicknames for the user.
272
- * .where('nicknames', '@>', sql`ARRAY[${sql.join(nicknames)}]`)
273
- * .orderBy(sql`concat(first_name, ' ', last_name)`)
274
- * .execute()
275
- * ```
276
- *
277
- * The generated SQL (PostgreSQL):
278
- *
279
- * ```sql
280
- * select concat(first_name, ' ', last_name) as "full_name"
281
- * from "person"
282
- * where birthdate between $1 and $2
283
- * and "nicknames" @> ARRAY[$3, $4, $5, $6, $7, $8, $9, $10]
284
- * order by concat(first_name, ' ', last_name)
285
- * ```
286
- *
287
- * SQL snippets can be executed by calling the `execute` method and passing a `Kysely`
288
- * instance as the only argument:
289
- *
290
- * ```ts
291
- * const result = await sql<Person[]>`select * from person`.execute(db)
292
- * ```
293
- *
294
- * You can merge other `sql` expressions and queries using substitutions:
295
- *
296
- * ```ts
297
- * const petName = db.selectFrom('pet').select('name').limit(1)
298
- * const fullName = sql`concat(first_name, ' ', last_name)`
299
- *
300
- * sql`
301
- * select ${fullName} as full_name, ${petName} as pet_name
302
- * from person
303
- * `
304
- * ```
305
- *
306
- * Substitutions also handle {@link ExpressionBuilder.ref},
307
- * {@link DynamicModule.ref} and pretty much anything else you
308
- * throw at it. Here's an example of calling a function in a
309
- * type-safe way:
310
- *
311
- * ```ts
312
- * db.selectFrom('person')
313
- * .select([
314
- * 'first_name',
315
- * 'last_name',
316
- * (eb) => {
317
- * // The `eb.ref` method is type-safe and only accepts
318
- * // column references that are possible.
319
- * const firstName = eb.ref('first_name')
320
- * const lastName = eb.ref('last_name')
321
- *
322
- * const fullName = sql<string>`concat(${firstName}, ' ', ${lastName})`
323
- * return fullName.as('full_name')
324
- * }
325
- * ])
326
- * ```
327
- *
328
- * don't know if that amount of ceremony is worth the small increase in
329
- * type-safety though... But it's possible.
330
- */
331
331
  export declare const sql: Sql;
@@ -9,102 +9,6 @@ const table_parser_js_1 = require("../parser/table-parser.js");
9
9
  const value_parser_js_1 = require("../parser/value-parser.js");
10
10
  const query_id_js_1 = require("../util/query-id.js");
11
11
  const raw_builder_js_1 = require("./raw-builder.js");
12
- /**
13
- * Template tag for creating raw SQL snippets and queries.
14
- *
15
- * ```ts
16
- * import { sql } from 'kysely'
17
- *
18
- * const id = 123
19
- *
20
- * const snippet = sql<Person[]>`select * from person where id = ${id}`
21
- * ```
22
- *
23
- * Substitutions (the things inside `${}`) are automatically passed to the database
24
- * as parameters and are NOT interpolated to the SQL string. There's no need to worry
25
- * about SQL injection vulnerabilities.
26
- *
27
- * If you need your substitutions to be interpreted as identifiers, value literals or
28
- * lists of things, see the {@link Sql.ref}, {@link Sql.table}, {@link Sql.id},
29
- * {@link Sql.literal}, {@link Sql.raw} and {@link Sql.join} functions.
30
- *
31
- * You can pass sql snippets returned by the `sql` tag pretty much anywhere. Whenever
32
- * something can't be done using the Kysely API, you should be able to drop down to
33
- * raw SQL using the `sql` tag. Here's an example query that uses raw sql in a bunch
34
- * of methods:
35
- *
36
- * ```ts
37
- * const persons = await db
38
- * .selectFrom('person')
39
- * .select(
40
- * // If you use `sql` in a select statement, remember to call the `as`
41
- * // method to give it an alias. Otherwise Kysely is not able to add
42
- * // the column to the result type.
43
- * sql<string>`concat(first_name, ' ', last_name)`.as('full_name')
44
- * )
45
- * .where(sql`birthdate between ${date1} and ${date2}`)
46
- * // Here we assume we have list of nicknames for the person
47
- * // (a list of strings) and we use the PostgreSQL `@>` operator
48
- * // to test if all of them are valid nicknames for the user.
49
- * .where('nicknames', '@>', sql`ARRAY[${sql.join(nicknames)}]`)
50
- * .orderBy(sql`concat(first_name, ' ', last_name)`)
51
- * .execute()
52
- * ```
53
- *
54
- * The generated SQL (PostgreSQL):
55
- *
56
- * ```sql
57
- * select concat(first_name, ' ', last_name) as "full_name"
58
- * from "person"
59
- * where birthdate between $1 and $2
60
- * and "nicknames" @> ARRAY[$3, $4, $5, $6, $7, $8, $9, $10]
61
- * order by concat(first_name, ' ', last_name)
62
- * ```
63
- *
64
- * SQL snippets can be executed by calling the `execute` method and passing a `Kysely`
65
- * instance as the only argument:
66
- *
67
- * ```ts
68
- * const result = await sql<Person[]>`select * from person`.execute(db)
69
- * ```
70
- *
71
- * You can merge other `sql` expressions and queries using substitutions:
72
- *
73
- * ```ts
74
- * const petName = db.selectFrom('pet').select('name').limit(1)
75
- * const fullName = sql`concat(first_name, ' ', last_name)`
76
- *
77
- * sql`
78
- * select ${fullName} as full_name, ${petName} as pet_name
79
- * from person
80
- * `
81
- * ```
82
- *
83
- * Substitutions also handle {@link ExpressionBuilder.ref},
84
- * {@link DynamicModule.ref} and pretty much anything else you
85
- * throw at it. Here's an example of calling a function in a
86
- * type-safe way:
87
- *
88
- * ```ts
89
- * db.selectFrom('person')
90
- * .select([
91
- * 'first_name',
92
- * 'last_name',
93
- * (eb) => {
94
- * // The `eb.ref` method is type-safe and only accepts
95
- * // column references that are possible.
96
- * const firstName = eb.ref('first_name')
97
- * const lastName = eb.ref('last_name')
98
- *
99
- * const fullName = sql<string>`concat(${firstName}, ' ', ${lastName})`
100
- * return fullName.as('full_name')
101
- * }
102
- * ])
103
- * ```
104
- *
105
- * don't know if that amount of ceremony is worth the small increase in
106
- * type-safety though... But it's possible.
107
- */
108
12
  exports.sql = Object.assign((sqlFragments, ...parameters) => {
109
13
  return new raw_builder_js_1.RawBuilder({
110
14
  queryId: (0, query_id_js_1.createQueryId)(),
@@ -11,6 +11,7 @@ const column_definition_node_js_1 = require("../operation-node/column-definition
11
11
  const default_value_parser_js_1 = require("../parser/default-value-parser.js");
12
12
  const generated_node_js_1 = require("../operation-node/generated-node.js");
13
13
  const default_value_node_js_1 = require("../operation-node/default-value-node.js");
14
+ const on_modify_action_parser_js_1 = require("../parser/on-modify-action-parser.js");
14
15
  class ColumnDefinitionBuilder {
15
16
  #node;
16
17
  constructor(node) {
@@ -38,7 +39,7 @@ class ColumnDefinitionBuilder {
38
39
  throw new Error('on delete constraint can only be added for foreign keys');
39
40
  }
40
41
  return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, {
41
- references: references_node_js_1.ReferencesNode.cloneWithOnDelete(this.#node.references, onDelete),
42
+ references: references_node_js_1.ReferencesNode.cloneWithOnDelete(this.#node.references, (0, on_modify_action_parser_js_1.parseOnModifyForeignAction)(onDelete)),
42
43
  }));
43
44
  }
44
45
  onUpdate(onUpdate) {
@@ -46,7 +47,7 @@ class ColumnDefinitionBuilder {
46
47
  throw new Error('on update constraint can only be added for foreign keys');
47
48
  }
48
49
  return new ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.cloneWith(this.#node, {
49
- references: references_node_js_1.ReferencesNode.cloneWithOnUpdate(this.#node.references, onUpdate),
50
+ references: references_node_js_1.ReferencesNode.cloneWithOnUpdate(this.#node.references, (0, on_modify_action_parser_js_1.parseOnModifyForeignAction)(onUpdate)),
50
51
  }));
51
52
  }
52
53
  unique() {
@@ -149,6 +149,60 @@ export declare class CreateTableBuilder<TB extends string, C extends string = ne
149
149
  * ```
150
150
  */
151
151
  addForeignKeyConstraint(constraintName: string, columns: C[], targetTable: string, targetColumns: string[], build?: ForeignKeyConstraintBuilderCallback): CreateTableBuilder<TB, C>;
152
+ /**
153
+ * This can be used to add any additional SQL to the front of the query __after__ the `create` keyword.
154
+ *
155
+ * Also see {@link temporary}.
156
+ *
157
+ * ### Examples
158
+ *
159
+ * ```ts
160
+ * db.schema.createTable('person')
161
+ * .modifyFront(sql`global temporary`)
162
+ * .addColumn('id', 'integer', col => col => primaryKey())
163
+ * .addColumn('first_name', 'varchar(64)', col => col.notNull())
164
+ * .addColumn('last_name', 'varchar(64), col => col.notNull())
165
+ * .execute()
166
+ * ```
167
+ *
168
+ * The generated SQL (Postgres):
169
+ *
170
+ * ```sql
171
+ * create global temporary table "person" (
172
+ * "id" integer primary key,
173
+ * "first_name" varchar(64) not null,
174
+ * "last_name" varchar(64) not null
175
+ * )
176
+ * ```
177
+ */
178
+ modifyFront(modifier: AnyRawBuilder): CreateTableBuilder<TB, C>;
179
+ /**
180
+ * This can be used to add any additional SQL to the end of the query.
181
+ *
182
+ * Also see {@link onCommit}.
183
+ *
184
+ * ### Examples
185
+ *
186
+ * ```ts
187
+ * db.schema.createTable('person')
188
+ * .addColumn('id', 'integer', col => col => primaryKey())
189
+ * .addColumn('first_name', 'varchar(64)', col => col.notNull())
190
+ * .addColumn('last_name', 'varchar(64), col => col.notNull())
191
+ * .modifyEnd(sql`collate utf8_unicode_ci`)
192
+ * .execute()
193
+ * ```
194
+ *
195
+ * The generated SQL (MySQL):
196
+ *
197
+ * ```sql
198
+ * create table `person` (
199
+ * `id` integer primary key,
200
+ * `first_name` varchar(64) not null,
201
+ * `last_name` varchar(64) not null
202
+ * ) collate utf8_unicode_ci
203
+ * ```
204
+ */
205
+ modifyEnd(modifier: AnyRawBuilder): CreateTableBuilder<TB, C>;
152
206
  toOperationNode(): CreateTableNode;
153
207
  compile(): CompiledQuery;
154
208
  execute(): Promise<void>;
@@ -14,6 +14,7 @@ const primary_constraint_node_js_1 = require("../operation-node/primary-constrai
14
14
  const unique_constraint_node_js_1 = require("../operation-node/unique-constraint-node.js");
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
+ const on_commit_action_parse_js_1 = require("../parser/on-commit-action-parse.js");
17
18
  /**
18
19
  * This builder can be used to create a `create table` query.
19
20
  */
@@ -45,7 +46,7 @@ class CreateTableBuilder {
45
46
  return new CreateTableBuilder({
46
47
  ...this.#props,
47
48
  createTableNode: create_table_node_js_1.CreateTableNode.cloneWith(this.#props.createTableNode, {
48
- onCommit,
49
+ onCommit: (0, on_commit_action_parse_js_1.parseOnCommitAction)(onCommit),
49
50
  }),
50
51
  });
51
52
  }
@@ -205,6 +206,70 @@ class CreateTableBuilder {
205
206
  createTableNode: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.createTableNode, builder.toOperationNode()),
206
207
  });
207
208
  }
209
+ /**
210
+ * This can be used to add any additional SQL to the front of the query __after__ the `create` keyword.
211
+ *
212
+ * Also see {@link temporary}.
213
+ *
214
+ * ### Examples
215
+ *
216
+ * ```ts
217
+ * db.schema.createTable('person')
218
+ * .modifyFront(sql`global temporary`)
219
+ * .addColumn('id', 'integer', col => col => primaryKey())
220
+ * .addColumn('first_name', 'varchar(64)', col => col.notNull())
221
+ * .addColumn('last_name', 'varchar(64), col => col.notNull())
222
+ * .execute()
223
+ * ```
224
+ *
225
+ * The generated SQL (Postgres):
226
+ *
227
+ * ```sql
228
+ * create global temporary table "person" (
229
+ * "id" integer primary key,
230
+ * "first_name" varchar(64) not null,
231
+ * "last_name" varchar(64) not null
232
+ * )
233
+ * ```
234
+ */
235
+ modifyFront(modifier) {
236
+ return new CreateTableBuilder({
237
+ ...this.#props,
238
+ createTableNode: create_table_node_js_1.CreateTableNode.cloneWithFrontModifier(this.#props.createTableNode, modifier.toOperationNode()),
239
+ });
240
+ }
241
+ /**
242
+ * This can be used to add any additional SQL to the end of the query.
243
+ *
244
+ * Also see {@link onCommit}.
245
+ *
246
+ * ### Examples
247
+ *
248
+ * ```ts
249
+ * db.schema.createTable('person')
250
+ * .addColumn('id', 'integer', col => col => primaryKey())
251
+ * .addColumn('first_name', 'varchar(64)', col => col.notNull())
252
+ * .addColumn('last_name', 'varchar(64), col => col.notNull())
253
+ * .modifyEnd(sql`collate utf8_unicode_ci`)
254
+ * .execute()
255
+ * ```
256
+ *
257
+ * The generated SQL (MySQL):
258
+ *
259
+ * ```sql
260
+ * create table `person` (
261
+ * `id` integer primary key,
262
+ * `first_name` varchar(64) not null,
263
+ * `last_name` varchar(64) not null
264
+ * ) collate utf8_unicode_ci
265
+ * ```
266
+ */
267
+ modifyEnd(modifier) {
268
+ return new CreateTableBuilder({
269
+ ...this.#props,
270
+ createTableNode: create_table_node_js_1.CreateTableNode.cloneWithEndModifier(this.#props.createTableNode, modifier.toOperationNode()),
271
+ });
272
+ }
208
273
  toOperationNode() {
209
274
  return this.#props.executor.transformQuery(this.#props.createTableNode, this.#props.queryId);
210
275
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ForeignKeyConstraintBuilder = void 0;
4
4
  const foreign_key_constraint_node_js_1 = require("../operation-node/foreign-key-constraint-node.js");
5
+ const on_modify_action_parser_js_1 = require("../parser/on-modify-action-parser.js");
5
6
  const prevent_await_js_1 = require("../util/prevent-await.js");
6
7
  class ForeignKeyConstraintBuilder {
7
8
  #node;
@@ -10,12 +11,12 @@ class ForeignKeyConstraintBuilder {
10
11
  }
11
12
  onDelete(onDelete) {
12
13
  return new ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.cloneWith(this.#node, {
13
- onDelete,
14
+ onDelete: (0, on_modify_action_parser_js_1.parseOnModifyForeignAction)(onDelete),
14
15
  }));
15
16
  }
16
17
  onUpdate(onUpdate) {
17
18
  return new ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.cloneWith(this.#node, {
18
- onUpdate,
19
+ onUpdate: (0, on_modify_action_parser_js_1.parseOnModifyForeignAction)(onUpdate),
19
20
  }));
20
21
  }
21
22
  toOperationNode() {
@@ -0,0 +1,5 @@
1
+ import { AnyRawBuilder } from './type-utils.js';
2
+ export declare type ExplainFormat = 'text' | 'xml' | 'json' | 'yaml' | 'traditional' | 'tree';
3
+ export interface Explainable {
4
+ explain<O extends Record<string, any> = Record<string, any>>(format?: ExplainFormat, options?: AnyRawBuilder): Promise<O[]>;
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -28,6 +28,8 @@ export class MysqlIntrospector {
28
28
  'extra',
29
29
  ])
30
30
  .where('table_schema', '=', sql `database()`)
31
+ .orderBy('table_name')
32
+ .orderBy('column_name')
31
33
  .castTo();
32
34
  if (!options.withInternalKyselyTables) {
33
35
  query = query
@@ -1,6 +1,11 @@
1
1
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
2
2
  export declare class MysqlQueryCompiler extends DefaultQueryCompiler {
3
3
  protected getCurrentParameterPlaceholder(): string;
4
+ protected getLeftExplainOptionsWrapper(): string;
5
+ protected getExplainOptionAssignment(): string;
6
+ protected getExplainOptionsDelimiter(): string;
7
+ protected getRightExplainOptionsWrapper(): string;
4
8
  protected getLeftIdentifierWrapper(): string;
5
9
  protected getRightIdentifierWrapper(): string;
10
+ protected sanitizeIdentifier(identifier: string): string;
6
11
  }
@@ -1,13 +1,29 @@
1
1
  /// <reference types="./mysql-query-compiler.d.ts" />
2
2
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
3
+ const ID_WRAP_REGEX = /`/g;
3
4
  export class MysqlQueryCompiler extends DefaultQueryCompiler {
4
5
  getCurrentParameterPlaceholder() {
5
6
  return '?';
6
7
  }
8
+ getLeftExplainOptionsWrapper() {
9
+ return '';
10
+ }
11
+ getExplainOptionAssignment() {
12
+ return '=';
13
+ }
14
+ getExplainOptionsDelimiter() {
15
+ return ' ';
16
+ }
17
+ getRightExplainOptionsWrapper() {
18
+ return '';
19
+ }
7
20
  getLeftIdentifierWrapper() {
8
21
  return '`';
9
22
  }
10
23
  getRightIdentifierWrapper() {
11
24
  return '`';
12
25
  }
26
+ sanitizeIdentifier(identifier) {
27
+ return identifier.replace(ID_WRAP_REGEX, '``');
28
+ }
13
29
  }
@@ -19,33 +19,41 @@ export class PostgresIntrospector {
19
19
  let query = this.#db
20
20
  .selectFrom('pg_catalog.pg_attribute as a')
21
21
  .innerJoin('pg_catalog.pg_class as c', 'a.attrelid', 'c.oid')
22
- .innerJoin('pg_catalog.pg_tables as t', 't.tablename', 'c.relname')
22
+ .innerJoin('pg_catalog.pg_namespace as ns', 'c.relnamespace', 'ns.oid')
23
23
  .innerJoin('pg_catalog.pg_type as typ', 'a.atttypid', 'typ.oid')
24
24
  .select([
25
25
  'a.attname as column',
26
26
  'a.attnotnull as not_null',
27
27
  'a.atthasdef as has_default',
28
- 't.tablename as table',
29
- 't.schemaname as schema',
28
+ 'c.relname as table',
29
+ 'ns.nspname as schema',
30
30
  'typ.typname as type',
31
31
  // Detect if the column is auto incrementing by finding the sequence
32
32
  // that is created for `serial` and `bigserial` columns.
33
33
  this.#db
34
34
  .selectFrom('pg_class')
35
35
  .select(sql `true`.as('auto_incrementing'))
36
+ // Make sure the sequence is in the same schema as the table.
37
+ .whereRef('relnamespace', '=', 'c.relnamespace')
36
38
  .where('relkind', '=', 'S')
37
- .where('relname', '=', sql `t.tablename || '_' || a.attname || '_seq'`)
39
+ .where('relname', '=', sql `c.relname || '_' || a.attname || '_seq'`)
38
40
  .as('auto_incrementing'),
39
41
  ])
40
- .where('t.schemaname', '!~', '^pg_')
41
- .where('t.schemaname', '!=', 'information_schema')
42
- .where('a.attnum', '>=', 0) // No system columns
42
+ // r == normal table
43
+ .where('c.relkind', '=', 'r')
44
+ .where('ns.nspname', '!~', '^pg_')
45
+ .where('ns.nspname', '!=', 'information_schema')
46
+ // No system columns
47
+ .where('a.attnum', '>=', 0)
43
48
  .where('a.attisdropped', '!=', true)
49
+ .orderBy('ns.nspname')
50
+ .orderBy('c.relname')
51
+ .orderBy('a.attname')
44
52
  .castTo();
45
53
  if (!options.withInternalKyselyTables) {
46
54
  query = query
47
- .where('t.tablename', '!=', DEFAULT_MIGRATION_TABLE)
48
- .where('t.tablename', '!=', DEFAULT_MIGRATION_LOCK_TABLE);
55
+ .where('c.relname', '!=', DEFAULT_MIGRATION_TABLE)
56
+ .where('c.relname', '!=', DEFAULT_MIGRATION_LOCK_TABLE);
49
57
  }
50
58
  const rawColumns = await query.execute();
51
59
  return this.#parseTableMetadata(rawColumns);
@@ -1,6 +1,4 @@
1
1
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
2
2
  export declare class PostgresQueryCompiler extends DefaultQueryCompiler {
3
- protected getCurrentParameterPlaceholder(): string;
4
- protected getLeftIdentifierWrapper(): string;
5
- protected getRightIdentifierWrapper(): string;
3
+ protected sanitizeIdentifier(identifier: string): string;
6
4
  }
@@ -1,13 +1,8 @@
1
1
  /// <reference types="./postgres-query-compiler.d.ts" />
2
2
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
3
+ const ID_WRAP_REGEX = /"/g;
3
4
  export class PostgresQueryCompiler extends DefaultQueryCompiler {
4
- getCurrentParameterPlaceholder() {
5
- return '$' + this.numParameters;
6
- }
7
- getLeftIdentifierWrapper() {
8
- return '"';
9
- }
10
- getRightIdentifierWrapper() {
11
- return '"';
5
+ sanitizeIdentifier(identifier) {
6
+ return identifier.replace(ID_WRAP_REGEX, '""');
12
7
  }
13
8
  }
@@ -16,6 +16,7 @@ export class SqliteIntrospector {
16
16
  .where('type', '=', 'table')
17
17
  .where('name', 'not like', 'sqlite_%')
18
18
  .select('name')
19
+ .orderBy('name')
19
20
  .castTo();
20
21
  if (!options.withInternalKyselyTables) {
21
22
  query = query
@@ -48,6 +49,7 @@ export class SqliteIntrospector {
48
49
  const columns = await db
49
50
  .selectFrom(sql `pragma_table_info(${table})`.as('table_info'))
50
51
  .select(['name', 'type', 'notnull', 'dflt_value'])
52
+ .orderBy('name')
51
53
  .execute();
52
54
  return {
53
55
  name: table,
@@ -1,7 +1,10 @@
1
1
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
2
2
  export declare class SqliteQueryCompiler extends DefaultQueryCompiler {
3
3
  protected getCurrentParameterPlaceholder(): string;
4
+ protected getLeftExplainOptionsWrapper(): string;
5
+ protected getRightExplainOptionsWrapper(): string;
4
6
  protected getLeftIdentifierWrapper(): string;
5
7
  protected getRightIdentifierWrapper(): string;
6
8
  protected getAutoIncrement(): string;
9
+ protected sanitizeIdentifier(identifier: string): string;
7
10
  }