kysely 0.8.1 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/README.md +47 -4
  2. package/dist/cjs/dialect/mysql/mysql-driver.js +2 -2
  3. package/dist/cjs/dialect/mysql/mysql-query-compiler.d.ts +0 -2
  4. package/dist/cjs/dialect/mysql/mysql-query-compiler.js +0 -29
  5. package/dist/cjs/operation-node/create-schema-node.d.ts +1 -1
  6. package/dist/cjs/operation-node/create-schema-node.js +2 -2
  7. package/dist/cjs/operation-node/create-table-node.d.ts +1 -5
  8. package/dist/cjs/operation-node/create-table-node.js +1 -31
  9. package/dist/cjs/operation-node/join-node.d.ts +2 -1
  10. package/dist/cjs/operation-node/join-node.js +8 -6
  11. package/dist/cjs/operation-node/operation-node-source.d.ts +1 -1
  12. package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
  13. package/dist/cjs/operation-node/operation-node-transformer.js +11 -1
  14. package/dist/cjs/operation-node/operation-node-visitor.d.ts +61 -59
  15. package/dist/cjs/operation-node/operation-node-visitor.js +1 -316
  16. package/dist/cjs/operation-node/operation-node.d.ts +1 -1
  17. package/dist/cjs/operation-node/primitive-value-list-node.js +1 -1
  18. package/dist/cjs/operation-node/query-node.d.ts +4 -3
  19. package/dist/cjs/operation-node/query-node.js +10 -2
  20. package/dist/cjs/operation-node/select-query-node.d.ts +5 -1
  21. package/dist/cjs/operation-node/select-query-node.js +18 -2
  22. package/dist/cjs/operation-node/union-node.d.ts +16 -0
  23. package/dist/cjs/operation-node/union-node.js +19 -0
  24. package/dist/cjs/parser/group-by-parser.d.ts +4 -0
  25. package/dist/cjs/parser/group-by-parser.js +9 -0
  26. package/dist/cjs/parser/insert-values-parser.d.ts +1 -1
  27. package/dist/cjs/parser/join-parser.d.ts +6 -3
  28. package/dist/cjs/parser/order-by-parser.d.ts +2 -4
  29. package/dist/cjs/parser/order-by-parser.js +6 -3
  30. package/dist/cjs/parser/reference-parser.d.ts +1 -1
  31. package/dist/cjs/parser/reference-parser.js +1 -1
  32. package/dist/cjs/parser/table-parser.d.ts +8 -8
  33. package/dist/cjs/parser/union-parser.d.ts +5 -0
  34. package/dist/cjs/parser/union-parser.js +13 -0
  35. package/dist/cjs/parser/value-parser.d.ts +1 -1
  36. package/dist/cjs/query-builder/function-builder.d.ts +6 -6
  37. package/dist/cjs/query-builder/function-builder.js +6 -6
  38. package/dist/cjs/query-builder/join-builder.js +8 -8
  39. package/dist/cjs/query-builder/query-builder.d.ts +30 -1
  40. package/dist/cjs/query-builder/query-builder.js +66 -22
  41. package/dist/cjs/query-compiler/compiled-query.d.ts +1 -1
  42. package/dist/cjs/query-compiler/default-query-compiler.d.ts +8 -2
  43. package/dist/cjs/query-compiler/default-query-compiler.js +33 -22
  44. package/dist/cjs/query-creator.d.ts +6 -3
  45. package/dist/cjs/query-creator.js +5 -2
  46. package/dist/cjs/schema/alter-table-builder.d.ts +3 -0
  47. package/dist/cjs/schema/alter-table-builder.js +3 -0
  48. package/dist/cjs/schema/column-definition-builder.d.ts +14 -5
  49. package/dist/cjs/schema/create-schema-builder.js +1 -1
  50. package/dist/cjs/schema/create-table-builder.d.ts +55 -10
  51. package/dist/cjs/schema/create-table-builder.js +59 -17
  52. package/dist/cjs/schema/schema.d.ts +37 -2
  53. package/dist/cjs/schema/schema.js +36 -1
  54. package/dist/cjs/util/object-utils.d.ts +12 -11
  55. package/dist/cjs/util/object-utils.js +6 -2
  56. package/dist/esm/dialect/mysql/mysql-driver.js +3 -3
  57. package/dist/esm/dialect/mysql/mysql-query-compiler.d.ts +0 -2
  58. package/dist/esm/dialect/mysql/mysql-query-compiler.js +0 -29
  59. package/dist/esm/operation-node/create-schema-node.d.ts +1 -1
  60. package/dist/esm/operation-node/create-schema-node.js +2 -2
  61. package/dist/esm/operation-node/create-table-node.d.ts +1 -5
  62. package/dist/esm/operation-node/create-table-node.js +1 -31
  63. package/dist/esm/operation-node/join-node.d.ts +2 -1
  64. package/dist/esm/operation-node/join-node.js +8 -6
  65. package/dist/esm/operation-node/operation-node-source.d.ts +1 -1
  66. package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
  67. package/dist/esm/operation-node/operation-node-transformer.js +11 -1
  68. package/dist/esm/operation-node/operation-node-visitor.d.ts +61 -59
  69. package/dist/esm/operation-node/operation-node-visitor.js +1 -316
  70. package/dist/esm/operation-node/operation-node.d.ts +1 -1
  71. package/dist/esm/operation-node/primitive-value-list-node.js +1 -1
  72. package/dist/esm/operation-node/query-node.d.ts +4 -3
  73. package/dist/esm/operation-node/query-node.js +10 -2
  74. package/dist/esm/operation-node/select-query-node.d.ts +5 -1
  75. package/dist/esm/operation-node/select-query-node.js +18 -2
  76. package/dist/esm/operation-node/union-node.d.ts +16 -0
  77. package/dist/esm/operation-node/union-node.js +16 -0
  78. package/dist/esm/parser/group-by-parser.d.ts +4 -0
  79. package/dist/esm/parser/group-by-parser.js +5 -0
  80. package/dist/esm/parser/insert-values-parser.d.ts +1 -1
  81. package/dist/esm/parser/join-parser.d.ts +6 -3
  82. package/dist/esm/parser/order-by-parser.d.ts +2 -4
  83. package/dist/esm/parser/order-by-parser.js +6 -2
  84. package/dist/esm/parser/reference-parser.d.ts +1 -1
  85. package/dist/esm/parser/reference-parser.js +2 -2
  86. package/dist/esm/parser/table-parser.d.ts +8 -8
  87. package/dist/esm/parser/union-parser.d.ts +5 -0
  88. package/dist/esm/parser/union-parser.js +9 -0
  89. package/dist/esm/parser/value-parser.d.ts +1 -1
  90. package/dist/esm/query-builder/function-builder.d.ts +6 -6
  91. package/dist/esm/query-builder/function-builder.js +6 -6
  92. package/dist/esm/query-builder/join-builder.js +8 -8
  93. package/dist/esm/query-builder/query-builder.d.ts +30 -1
  94. package/dist/esm/query-builder/query-builder.js +67 -23
  95. package/dist/esm/query-compiler/compiled-query.d.ts +1 -1
  96. package/dist/esm/query-compiler/default-query-compiler.d.ts +8 -2
  97. package/dist/esm/query-compiler/default-query-compiler.js +33 -22
  98. package/dist/esm/query-creator.d.ts +6 -3
  99. package/dist/esm/query-creator.js +5 -2
  100. package/dist/esm/schema/alter-table-builder.d.ts +3 -0
  101. package/dist/esm/schema/alter-table-builder.js +3 -0
  102. package/dist/esm/schema/column-definition-builder.d.ts +14 -5
  103. package/dist/esm/schema/create-schema-builder.js +1 -1
  104. package/dist/esm/schema/create-table-builder.d.ts +55 -10
  105. package/dist/esm/schema/create-table-builder.js +60 -18
  106. package/dist/esm/schema/schema.d.ts +37 -2
  107. package/dist/esm/schema/schema.js +36 -1
  108. package/dist/esm/util/object-utils.d.ts +12 -11
  109. package/dist/esm/util/object-utils.js +4 -1
  110. package/package.json +1 -1
@@ -23,6 +23,9 @@ const unique_constraint_node_js_1 = require("../operation-node/unique-constraint
23
23
  const check_constraint_node_js_1 = require("../operation-node/check-constraint-node.js");
24
24
  const foreign_key_constraint_node_js_1 = require("../operation-node/foreign-key-constraint-node.js");
25
25
  const column_node_js_1 = require("../operation-node/column-node.js");
26
+ /**
27
+ * This builder can be used to create a `alter table` query.
28
+ */
26
29
  class AlterTableBuilder {
27
30
  #props;
28
31
  constructor(props) {
@@ -6,11 +6,11 @@ import { AnyRawBuilder } from '../util/type-utils.js';
6
6
  import { DefaultValueExpression } from '../parser/default-value-parser.js';
7
7
  export interface ColumnDefinitionBuilderInterface<R> {
8
8
  /**
9
- * Makes the column automatically incrementing.
9
+ * Adds `auto_increment` or `autoincrement` to the column definition
10
+ * depending on the dialect.
10
11
  *
11
- * On some dialects this may change the column type as well. For example
12
- * on postgres this sets the column type to `serial` no matter what you
13
- * have specified before.
12
+ * Some dialects like PostgreSQL don't support this. On PostgreSQL
13
+ * you can use the `serial` or `bigserial` data type instead.
14
14
  */
15
15
  increments(): R;
16
16
  /**
@@ -23,6 +23,10 @@ export interface ColumnDefinitionBuilderInterface<R> {
23
23
  /**
24
24
  * Adds a foreign key constraint for the column.
25
25
  *
26
+ * If your database engine doesn't support foreign key constraints in the
27
+ * column definition (like MySQL 5) you need to call the table level
28
+ * {@link TableBuilder.addForeignKeyConstraint} method instead.
29
+ *
26
30
  * @example
27
31
  * ```ts
28
32
  * col.references('person.id')
@@ -31,6 +35,11 @@ export interface ColumnDefinitionBuilderInterface<R> {
31
35
  references(ref: string): R;
32
36
  /**
33
37
  * Adds an `on delete` constraint for the foreign key column.
38
+ *
39
+ * If your database engine doesn't support foreign key constraints in the
40
+ * column definition (like MySQL 5) you need to call the table level
41
+ * {@link TableBuilder.addForeignKeyConstraint} method instead.
42
+ *
34
43
  */
35
44
  onDelete(onDelete: OnModifyForeignAction): R;
36
45
  /**
@@ -64,7 +73,7 @@ export interface ColumnDefinitionBuilderInterface<R> {
64
73
  * ```ts
65
74
  * db.schema
66
75
  * .createTable('pet')
67
- * .addColumn('number_of_legs', (col) => col.check('number_of_legs < 5', 'integer'))
76
+ * .addColumn('number_of_legs', 'integer', (col) => col.check('number_of_legs < 5'))
68
77
  * .execute()
69
78
  * ```
70
79
  */
@@ -12,7 +12,7 @@ class CreateSchemaBuilder {
12
12
  ifNotExists() {
13
13
  return new CreateSchemaBuilder({
14
14
  ...this.#props,
15
- createSchemaNode: create_schema_node_js_1.CreateSchemaNode.cloneWithModifier(this.#props.createSchemaNode, 'IfNotExists'),
15
+ createSchemaNode: create_schema_node_js_1.CreateSchemaNode.cloneWith(this.#props.createSchemaNode, { modifier: 'IfNotExists' }),
16
16
  });
17
17
  }
18
18
  toOperationNode() {
@@ -7,7 +7,10 @@ import { ColumnDefinitionBuilder } from './column-definition-builder.js';
7
7
  import { QueryId } from '../util/query-id.js';
8
8
  import { ForeignKeyConstraintBuilder } from './foreign-key-constraint-builder.js';
9
9
  import { DataTypeExpression } from '../parser/data-type-parser.js';
10
- export declare class CreateTableBuilder implements OperationNodeSource, Compilable {
10
+ /**
11
+ * This builder can be used to create a `create table` query.
12
+ */
13
+ export declare class CreateTableBuilder<TB extends string, C extends string = never> implements OperationNodeSource, Compilable {
11
14
  #private;
12
15
  constructor(props: CreateTableBuilderProps);
13
16
  /**
@@ -15,7 +18,7 @@ export declare class CreateTableBuilder implements OperationNodeSource, Compilab
15
18
  *
16
19
  * If the table already exists, no error is thrown if this method has been called.
17
20
  */
18
- ifNotExists(): CreateTableBuilder;
21
+ ifNotExists(): CreateTableBuilder<TB, C>;
19
22
  /**
20
23
  * Adds a column to the table.
21
24
  *
@@ -23,44 +26,86 @@ export declare class CreateTableBuilder implements OperationNodeSource, Compilab
23
26
  * ```ts
24
27
  * await db.schema
25
28
  * .createTable('person')
26
- * .addColumn('int', 'id', (col) => col.increments().primary()),
29
+ * .addColumn('id', 'integer', (col) => col.increments().primaryKey()),
27
30
  * .addColumn('first_name', 'varchar(50), (col) => col.notNull())
28
- * .addColumn('varchar', 'last_name')
29
- * .addColumn('numeric(8, 2)', 'bank_balance')
31
+ * .addColumn('last_name', 'varchar')
32
+ * .addColumn('bank_balance', 'numeric(8, 2)')
30
33
  * .addColumn('data', db.raw('customtype'))
34
+ * .addColumn('parent_id', 'integer', (col) =>
35
+ * col.references('person.id').onDelete('cascade'))
36
+ * )
37
+ * ```
38
+ *
39
+ * With this method, it's once again good to remember that Kysely just builds the query
40
+ * that's as close to the structure of your builder method calls as possible, and doesn't
41
+ * provide the same API for all databses. For example, some databases like older MySQL
42
+ * don't support `references` statement in the column definition. Instead foreign key
43
+ * constraints need to be defined in at the level of the `create table` query. See
44
+ * the next example:
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * .addColumn('parent_id', 'integer')
49
+ * .addForeignKeyConstraint(
50
+ * 'person_parent_id_fk', ['parent_id'], 'person', ['id'],
51
+ * (cb) => cb.onDelete('cascade')
52
+ * )
53
+ * ```
54
+ *
55
+ * Another good example is that PostgreSQL doesn't support the `auto_increment`
56
+ * keyword and you need to define an autoincrementing column for example using
57
+ * `serial`:
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * await db.schema
62
+ * .createTable('person')
63
+ * .addColumn('id', 'serial', (col) => col.primaryKey()),
31
64
  * ```
32
65
  */
33
- addColumn(columnName: string, dataType: DataTypeExpression, build?: ColumnBuilderCallback): CreateTableBuilder;
66
+ addColumn<CN extends string>(columnName: CN, dataType: DataTypeExpression, build?: ColumnBuilderCallback): CreateTableBuilder<TB, C | CN>;
34
67
  /**
35
68
  * Adds a primary key constraint for one or more columns.
36
69
  *
70
+ * The constraint name can be anything you want, but it must be unique
71
+ * across the whole database.
72
+ *
37
73
  * @example
38
74
  * ```ts
39
75
  * addPrimaryKeyConstraint('primary_key', ['first_name', 'last_name'])
40
76
  * ```
41
77
  */
42
- addPrimaryKeyConstraint(constraintName: string, columns: string[]): CreateTableBuilder;
78
+ addPrimaryKeyConstraint(constraintName: string, columns: C[]): CreateTableBuilder<TB, C>;
43
79
  /**
44
80
  * Adds a unique constraint for one or more columns.
45
81
  *
82
+ * The constraint name can be anything you want, but it must be unique
83
+ * across the whole database.
84
+ *
46
85
  * @example
47
86
  * ```ts
48
87
  * addUniqueConstraint('first_name_last_name_unique', ['first_name', 'last_name'])
49
88
  * ```
50
89
  */
51
- addUniqueConstraint(constraintName: string, columns: string[]): CreateTableBuilder;
90
+ addUniqueConstraint(constraintName: string, columns: C[]): CreateTableBuilder<TB, C>;
52
91
  /**
53
92
  * Adds a check constraint.
54
93
  *
94
+ * The constraint name can be anything you want, but it must be unique
95
+ * across the whole database.
96
+ *
55
97
  * @example
56
98
  * ```ts
57
99
  * addCheckConstraint('check_legs', 'number_of_legs < 5')
58
100
  * ```
59
101
  */
60
- addCheckConstraint(constraintName: string, checkExpression: string): CreateTableBuilder;
102
+ addCheckConstraint(constraintName: string, checkExpression: string): CreateTableBuilder<TB, C>;
61
103
  /**
62
104
  * Adds a foreign key constraint.
63
105
  *
106
+ * The constraint name can be anything you want, but it must be unique
107
+ * across the whole database.
108
+ *
64
109
  * @example
65
110
  * ```ts
66
111
  * addForeignKeyConstraint(
@@ -82,7 +127,7 @@ export declare class CreateTableBuilder implements OperationNodeSource, Compilab
82
127
  * )
83
128
  * ```
84
129
  */
85
- addForeignKeyConstraint(constraintName: string, columns: string[], targetTable: string, targetColumns: string[], build?: ForeignKeyConstraintBuilderCallback): CreateTableBuilder;
130
+ addForeignKeyConstraint(constraintName: string, columns: C[], targetTable: string, targetColumns: string[], build?: ForeignKeyConstraintBuilderCallback): CreateTableBuilder<TB, C>;
86
131
  toOperationNode(): CreateTableNode;
87
132
  compile(): CompiledQuery;
88
133
  execute(): Promise<void>;
@@ -11,6 +11,12 @@ const column_node_js_1 = require("../operation-node/column-node.js");
11
11
  const table_node_js_1 = require("../operation-node/table-node.js");
12
12
  const foreign_key_constraint_builder_js_1 = require("./foreign-key-constraint-builder.js");
13
13
  const data_type_parser_js_1 = require("../parser/data-type-parser.js");
14
+ const primary_constraint_node_js_1 = require("../operation-node/primary-constraint-node.js");
15
+ const unique_constraint_node_js_1 = require("../operation-node/unique-constraint-node.js");
16
+ const check_constraint_node_js_1 = require("../operation-node/check-constraint-node.js");
17
+ /**
18
+ * This builder can be used to create a `create table` query.
19
+ */
14
20
  class CreateTableBuilder {
15
21
  #props;
16
22
  constructor(props) {
@@ -34,18 +40,45 @@ class CreateTableBuilder {
34
40
  * ```ts
35
41
  * await db.schema
36
42
  * .createTable('person')
37
- * .addColumn('int', 'id', (col) => col.increments().primary()),
43
+ * .addColumn('id', 'integer', (col) => col.increments().primaryKey()),
38
44
  * .addColumn('first_name', 'varchar(50), (col) => col.notNull())
39
- * .addColumn('varchar', 'last_name')
40
- * .addColumn('numeric(8, 2)', 'bank_balance')
45
+ * .addColumn('last_name', 'varchar')
46
+ * .addColumn('bank_balance', 'numeric(8, 2)')
41
47
  * .addColumn('data', db.raw('customtype'))
48
+ * .addColumn('parent_id', 'integer', (col) =>
49
+ * col.references('person.id').onDelete('cascade'))
50
+ * )
51
+ * ```
52
+ *
53
+ * With this method, it's once again good to remember that Kysely just builds the query
54
+ * that's as close to the structure of your builder method calls as possible, and doesn't
55
+ * provide the same API for all databses. For example, some databases like older MySQL
56
+ * don't support `references` statement in the column definition. Instead foreign key
57
+ * constraints need to be defined in at the level of the `create table` query. See
58
+ * the next example:
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * .addColumn('parent_id', 'integer')
63
+ * .addForeignKeyConstraint(
64
+ * 'person_parent_id_fk', ['parent_id'], 'person', ['id'],
65
+ * (cb) => cb.onDelete('cascade')
66
+ * )
67
+ * ```
68
+ *
69
+ * Another good example is that PostgreSQL doesn't support the `auto_increment`
70
+ * keyword and you need to define an autoincrementing column for example using
71
+ * `serial`:
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * await db.schema
76
+ * .createTable('person')
77
+ * .addColumn('id', 'serial', (col) => col.primaryKey()),
42
78
  * ```
43
79
  */
44
- addColumn(columnName, dataType, build) {
45
- let columnBuilder = new column_definition_builder_js_1.ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.create(columnName, (0, data_type_parser_js_1.parseDataTypeExpression)(dataType)));
46
- if (build) {
47
- columnBuilder = build(columnBuilder);
48
- }
80
+ addColumn(columnName, dataType, build = object_utils_js_1.noop) {
81
+ const columnBuilder = build(new column_definition_builder_js_1.ColumnDefinitionBuilder(column_definition_node_js_1.ColumnDefinitionNode.create(columnName, (0, data_type_parser_js_1.parseDataTypeExpression)(dataType))));
49
82
  return new CreateTableBuilder({
50
83
  ...this.#props,
51
84
  createTableNode: create_table_node_js_1.CreateTableNode.cloneWithColumn(this.#props.createTableNode, columnBuilder.toOperationNode()),
@@ -54,6 +87,9 @@ class CreateTableBuilder {
54
87
  /**
55
88
  * Adds a primary key constraint for one or more columns.
56
89
  *
90
+ * The constraint name can be anything you want, but it must be unique
91
+ * across the whole database.
92
+ *
57
93
  * @example
58
94
  * ```ts
59
95
  * addPrimaryKeyConstraint('primary_key', ['first_name', 'last_name'])
@@ -62,12 +98,15 @@ class CreateTableBuilder {
62
98
  addPrimaryKeyConstraint(constraintName, columns) {
63
99
  return new CreateTableBuilder({
64
100
  ...this.#props,
65
- createTableNode: create_table_node_js_1.CreateTableNode.cloneWithPrimaryKeyConstraint(this.#props.createTableNode, constraintName, columns),
101
+ createTableNode: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.createTableNode, primary_constraint_node_js_1.PrimaryConstraintNode.create(columns, constraintName)),
66
102
  });
67
103
  }
68
104
  /**
69
105
  * Adds a unique constraint for one or more columns.
70
106
  *
107
+ * The constraint name can be anything you want, but it must be unique
108
+ * across the whole database.
109
+ *
71
110
  * @example
72
111
  * ```ts
73
112
  * addUniqueConstraint('first_name_last_name_unique', ['first_name', 'last_name'])
@@ -76,12 +115,15 @@ class CreateTableBuilder {
76
115
  addUniqueConstraint(constraintName, columns) {
77
116
  return new CreateTableBuilder({
78
117
  ...this.#props,
79
- createTableNode: create_table_node_js_1.CreateTableNode.cloneWithUniqueConstraint(this.#props.createTableNode, constraintName, columns),
118
+ createTableNode: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.createTableNode, unique_constraint_node_js_1.UniqueConstraintNode.create(columns, constraintName)),
80
119
  });
81
120
  }
82
121
  /**
83
122
  * Adds a check constraint.
84
123
  *
124
+ * The constraint name can be anything you want, but it must be unique
125
+ * across the whole database.
126
+ *
85
127
  * @example
86
128
  * ```ts
87
129
  * addCheckConstraint('check_legs', 'number_of_legs < 5')
@@ -90,12 +132,15 @@ class CreateTableBuilder {
90
132
  addCheckConstraint(constraintName, checkExpression) {
91
133
  return new CreateTableBuilder({
92
134
  ...this.#props,
93
- createTableNode: create_table_node_js_1.CreateTableNode.cloneWithCheckConstraint(this.#props.createTableNode, constraintName, checkExpression),
135
+ createTableNode: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.createTableNode, check_constraint_node_js_1.CheckConstraintNode.create(checkExpression, constraintName)),
94
136
  });
95
137
  }
96
138
  /**
97
139
  * Adds a foreign key constraint.
98
140
  *
141
+ * The constraint name can be anything you want, but it must be unique
142
+ * across the whole database.
143
+ *
99
144
  * @example
100
145
  * ```ts
101
146
  * addForeignKeyConstraint(
@@ -117,14 +162,11 @@ class CreateTableBuilder {
117
162
  * )
118
163
  * ```
119
164
  */
120
- addForeignKeyConstraint(constraintName, columns, targetTable, targetColumns, build) {
121
- let builder = new foreign_key_constraint_builder_js_1.ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.create(columns.map(column_node_js_1.ColumnNode.create), table_node_js_1.TableNode.create(targetTable), targetColumns.map(column_node_js_1.ColumnNode.create), constraintName));
122
- if (build) {
123
- builder = build(builder);
124
- }
165
+ addForeignKeyConstraint(constraintName, columns, targetTable, targetColumns, build = object_utils_js_1.noop) {
166
+ const builder = build(new foreign_key_constraint_builder_js_1.ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.create(columns.map(column_node_js_1.ColumnNode.create), table_node_js_1.TableNode.create(targetTable), targetColumns.map(column_node_js_1.ColumnNode.create), constraintName)));
125
167
  return new CreateTableBuilder({
126
168
  ...this.#props,
127
- createTableNode: create_table_node_js_1.CreateTableNode.cloneWithForeignKeyConstraint(this.#props.createTableNode, builder.toOperationNode()),
169
+ createTableNode: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.createTableNode, builder.toOperationNode()),
128
170
  });
129
171
  }
130
172
  toOperationNode() {
@@ -22,14 +22,49 @@ export declare class SchemaModule {
22
22
  * ```ts
23
23
  * await db.schema
24
24
  * .createTable('person')
25
- * .addColumn('id', 'int', col => col.primaryKey().increments())
25
+ * .addColumn('id', 'integer', col => col.primaryKey().increments())
26
26
  * .addColumn('first_name', 'varchar', col => col.notNull())
27
27
  * .addColumn('last_name', 'varchar', col => col.notNull())
28
28
  * .addColumn('gender', 'varchar')
29
29
  * .execute()
30
30
  * ```
31
+ *
32
+ * @example
33
+ * This example creates a table with a foreign key. Not all database
34
+ * engines support column-level foreign key constraint definitions.
35
+ * For example if you are using MySQL 5.X see the next example after
36
+ * this one.
37
+ *
38
+ * ```ts
39
+ * await db.schema
40
+ * .createTable('pet')
41
+ * .addColumn('id', 'integer', col => col.primaryKey().increments())
42
+ * .addColumn('owner_id', 'integer', col => col
43
+ * .references('person.id')
44
+ * .onDelete('cascade')
45
+ * )
46
+ * .execute()
47
+ * ```
48
+ *
49
+ * @example
50
+ * This example adds a foreign key constraint for a columns just
51
+ * like the previous example, but using a table-level statement.
52
+ * On MySQL 5.X you need to define foreign key constraints like
53
+ * this:
54
+ *
55
+ * ```ts
56
+ * await db.schema
57
+ * .createTable('pet')
58
+ * .addColumn('id', 'integer', col => col.primaryKey().increments())
59
+ * .addColumn('owner_id', 'integer')
60
+ * .addForeignKeyConstraint(
61
+ * 'pet_owner_id_foreign', ['owner_id'], 'person', ['id'],
62
+ * (constraint) => constraint.onDelete('cascade')
63
+ * )
64
+ * .execute()
65
+ * ```
31
66
  */
32
- createTable(table: string): CreateTableBuilder;
67
+ createTable<TB extends string>(table: TB): CreateTableBuilder<TB, never>;
33
68
  /**
34
69
  * Drop a table.
35
70
  *
@@ -36,12 +36,47 @@ class SchemaModule {
36
36
  * ```ts
37
37
  * await db.schema
38
38
  * .createTable('person')
39
- * .addColumn('id', 'int', col => col.primaryKey().increments())
39
+ * .addColumn('id', 'integer', col => col.primaryKey().increments())
40
40
  * .addColumn('first_name', 'varchar', col => col.notNull())
41
41
  * .addColumn('last_name', 'varchar', col => col.notNull())
42
42
  * .addColumn('gender', 'varchar')
43
43
  * .execute()
44
44
  * ```
45
+ *
46
+ * @example
47
+ * This example creates a table with a foreign key. Not all database
48
+ * engines support column-level foreign key constraint definitions.
49
+ * For example if you are using MySQL 5.X see the next example after
50
+ * this one.
51
+ *
52
+ * ```ts
53
+ * await db.schema
54
+ * .createTable('pet')
55
+ * .addColumn('id', 'integer', col => col.primaryKey().increments())
56
+ * .addColumn('owner_id', 'integer', col => col
57
+ * .references('person.id')
58
+ * .onDelete('cascade')
59
+ * )
60
+ * .execute()
61
+ * ```
62
+ *
63
+ * @example
64
+ * This example adds a foreign key constraint for a columns just
65
+ * like the previous example, but using a table-level statement.
66
+ * On MySQL 5.X you need to define foreign key constraints like
67
+ * this:
68
+ *
69
+ * ```ts
70
+ * await db.schema
71
+ * .createTable('pet')
72
+ * .addColumn('id', 'integer', col => col.primaryKey().increments())
73
+ * .addColumn('owner_id', 'integer')
74
+ * .addForeignKeyConstraint(
75
+ * 'pet_owner_id_foreign', ['owner_id'], 'person', ['id'],
76
+ * (constraint) => constraint.onDelete('cascade')
77
+ * )
78
+ * .execute()
79
+ * ```
45
80
  */
46
81
  createTable(table) {
47
82
  return new create_table_builder_js_1.CreateTableBuilder({
@@ -1,18 +1,19 @@
1
1
  /// <reference types="node" />
2
2
  export declare type PrimitiveValue = string | number | boolean | null | Date | Buffer | BigInt;
3
- export declare function isEmpty(obj: ArrayLike<any> | string | object | Buffer): boolean;
4
- export declare function isString(obj: any): obj is string;
5
- export declare function isNumber(obj: any): obj is number;
6
- export declare function isBoolean(obj: any): obj is boolean;
7
- export declare function isNull(obj: any): obj is null;
8
- export declare function isDate(obj: any): obj is Date;
9
- export declare function isBuffer(obj: any): obj is Buffer;
10
- export declare function isBigInt(obj: any): obj is BigInt;
11
- export declare function isPrimitive(obj: any): obj is PrimitiveValue;
12
- export declare function isFunction(obj: any): obj is Function;
13
- export declare function isObject(obj: any): obj is Record<string, any>;
3
+ export declare function isEmpty(obj: ArrayLike<unknown> | string | object | Buffer): boolean;
4
+ export declare function isString(obj: unknown): obj is string;
5
+ export declare function isNumber(obj: unknown): obj is number;
6
+ export declare function isBoolean(obj: unknown): obj is boolean;
7
+ export declare function isNull(obj: unknown): obj is null;
8
+ export declare function isDate(obj: unknown): obj is Date;
9
+ export declare function isBuffer(obj: unknown): obj is Buffer;
10
+ export declare function isBigInt(obj: unknown): obj is BigInt;
11
+ export declare function isPrimitive(obj: unknown): obj is PrimitiveValue;
12
+ export declare function isFunction(obj: unknown): obj is Function;
13
+ export declare function isObject(obj: unknown): obj is Record<string, unknown>;
14
14
  export declare function getLast<T>(arr: ArrayLike<T>): T | undefined;
15
15
  export declare function freeze<T>(obj: T): Readonly<T>;
16
16
  export declare function asArray<T>(arg: T | T[]): T[];
17
17
  export declare function asReadonlyArray<T>(arg: T | ReadonlyArray<T>): ReadonlyArray<T>;
18
18
  export declare function isReadonlyArray(arg: unknown): arg is ReadonlyArray<unknown>;
19
+ export declare function noop<T>(obj: T): T;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isReadonlyArray = exports.asReadonlyArray = exports.asArray = exports.freeze = exports.getLast = exports.isObject = exports.isFunction = exports.isPrimitive = exports.isBigInt = exports.isBuffer = exports.isDate = exports.isNull = exports.isBoolean = exports.isNumber = exports.isString = exports.isEmpty = void 0;
3
+ exports.noop = exports.isReadonlyArray = exports.asReadonlyArray = exports.asArray = exports.freeze = exports.getLast = exports.isObject = exports.isFunction = exports.isPrimitive = exports.isBigInt = exports.isBuffer = exports.isDate = exports.isNull = exports.isBoolean = exports.isNumber = exports.isString = exports.isEmpty = void 0;
4
4
  function isEmpty(obj) {
5
5
  if (Array.isArray(obj) || isString(obj) || Buffer.isBuffer(obj)) {
6
6
  return obj.length === 0;
@@ -54,7 +54,7 @@ function isFunction(obj) {
54
54
  }
55
55
  exports.isFunction = isFunction;
56
56
  function isObject(obj) {
57
- return obj && typeof obj === 'object';
57
+ return typeof obj === 'object' && obj !== null;
58
58
  }
59
59
  exports.isObject = isObject;
60
60
  function getLast(arr) {
@@ -87,3 +87,7 @@ function isReadonlyArray(arg) {
87
87
  return Array.isArray(arg);
88
88
  }
89
89
  exports.isReadonlyArray = isReadonlyArray;
90
+ function noop(obj) {
91
+ return obj;
92
+ }
93
+ exports.noop = noop;
@@ -1,4 +1,4 @@
1
- import { isFunction, isObject } from '../../util/object-utils.js';
1
+ import { isFunction, isNumber, isObject, } from '../../util/object-utils.js';
2
2
  const PRIVATE_RELEASE_METHOD = Symbol();
3
3
  export class MysqlDriver {
4
4
  #config;
@@ -103,13 +103,13 @@ class MysqlConnection {
103
103
  async executeQuery(compiledQuery) {
104
104
  const result = await this.#executeQuery(compiledQuery);
105
105
  if (isObject(result) && 'insertId' in result && 'affectedRows' in result) {
106
- if (result.insertId) {
106
+ if (isNumber(result.insertId) && result.insertId > 0) {
107
107
  return {
108
108
  insertedPrimaryKey: result.insertId,
109
109
  rows: [],
110
110
  };
111
111
  }
112
- else {
112
+ else if (isNumber(result.affectedRows)) {
113
113
  return {
114
114
  numUpdatedOrDeletedRows: result.affectedRows,
115
115
  rows: [],
@@ -1,7 +1,5 @@
1
- import { ColumnDefinitionNode } from '../../operation-node/column-definition-node.js';
2
1
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
3
2
  export declare class MysqlQueryCompiler extends DefaultQueryCompiler {
4
- protected visitColumnDefinition(node: ColumnDefinitionNode): void;
5
3
  protected visitReturning(): void;
6
4
  protected getSql(): string;
7
5
  protected getCurrentParameterPlaceholder(): string;
@@ -1,34 +1,5 @@
1
1
  import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
2
2
  export class MysqlQueryCompiler extends DefaultQueryCompiler {
3
- visitColumnDefinition(node) {
4
- this.visitNode(node.column);
5
- this.append(' ');
6
- this.visitNode(node.dataType);
7
- if (node.defaultTo) {
8
- this.append(' default ');
9
- this.visitNode(node.defaultTo);
10
- }
11
- if (!node.isNullable || node.isAutoIncrementing) {
12
- this.append(' not null');
13
- }
14
- if (node.isAutoIncrementing) {
15
- this.append(' auto_increment');
16
- }
17
- if (node.isUnique) {
18
- this.append(' unique');
19
- }
20
- if (node.isPrimaryKey) {
21
- this.append(' primary key');
22
- }
23
- if (node.references) {
24
- this.append(' ');
25
- this.visitNode(node.references);
26
- }
27
- if (node.check) {
28
- this.append(' ');
29
- this.visitNode(node.check);
30
- }
31
- }
32
3
  visitReturning() {
33
4
  // Do nothing.
34
5
  }
@@ -13,5 +13,5 @@ export interface CreateSchemaNode extends OperationNode {
13
13
  export declare const CreateSchemaNode: Readonly<{
14
14
  is(node: OperationNode): node is CreateSchemaNode;
15
15
  create(schema: string, params?: CreateSchemaNodeParams | undefined): CreateSchemaNode;
16
- cloneWithModifier(createSchema: CreateSchemaNode, modifier: CreateSchemaNodeModifier): CreateSchemaNode;
16
+ cloneWith(createSchema: CreateSchemaNode, params: CreateSchemaNodeParams): CreateSchemaNode;
17
17
  }>;
@@ -14,10 +14,10 @@ export const CreateSchemaNode = freeze({
14
14
  ...params,
15
15
  });
16
16
  },
17
- cloneWithModifier(createSchema, modifier) {
17
+ cloneWith(createSchema, params) {
18
18
  return freeze({
19
19
  ...createSchema,
20
- modifier,
20
+ ...params,
21
21
  });
22
22
  },
23
23
  });
@@ -1,6 +1,5 @@
1
1
  import { OperationNode } from './operation-node.js';
2
2
  import { TableNode } from './table-node.js';
3
- import { ForeignKeyConstraintNode } from './foreign-key-constraint-node.js';
4
3
  import { ConstraintNode } from './constraint-node.js';
5
4
  import { ColumnDefinitionNode } from './column-definition-node.js';
6
5
  export declare type CreateTableNodeModifier = 'IfNotExists';
@@ -19,8 +18,5 @@ export declare const CreateTableNode: Readonly<{
19
18
  create(table: TableNode): CreateTableNode;
20
19
  cloneWithColumn(createTable: CreateTableNode, column: ColumnDefinitionNode): CreateTableNode;
21
20
  cloneWithModifier(createTable: CreateTableNode, modifier: CreateTableNodeModifier): CreateTableNode;
22
- cloneWithPrimaryKeyConstraint(createTable: CreateTableNode, constraintName: string, columns: string[]): CreateTableNode;
23
- cloneWithUniqueConstraint(createTable: CreateTableNode, constraintName: string, columns: string[]): CreateTableNode;
24
- cloneWithCheckConstraint(createTable: CreateTableNode, constraintName: string, sql: string): CreateTableNode;
25
- cloneWithForeignKeyConstraint(createTable: CreateTableNode, constraint: ForeignKeyConstraintNode): CreateTableNode;
21
+ cloneWithConstraint(createTable: CreateTableNode, constraint: ConstraintNode): CreateTableNode;
26
22
  }>;
@@ -1,7 +1,4 @@
1
1
  import { freeze } from '../util/object-utils.js';
2
- import { CheckConstraintNode } from './check-constraint-node.js';
3
- import { PrimaryConstraintNode } from './primary-constraint-node.js';
4
- import { UniqueConstraintNode } from './unique-constraint-node.js';
5
2
  /**
6
3
  * @internal
7
4
  */
@@ -28,34 +25,7 @@ export const CreateTableNode = freeze({
28
25
  modifier,
29
26
  });
30
27
  },
31
- cloneWithPrimaryKeyConstraint(createTable, constraintName, columns) {
32
- const constraint = PrimaryConstraintNode.create(columns, constraintName);
33
- return freeze({
34
- ...createTable,
35
- constraints: createTable.constraints
36
- ? freeze([...createTable.constraints, constraint])
37
- : freeze([constraint]),
38
- });
39
- },
40
- cloneWithUniqueConstraint(createTable, constraintName, columns) {
41
- const constraint = UniqueConstraintNode.create(columns, constraintName);
42
- return freeze({
43
- ...createTable,
44
- constraints: createTable.constraints
45
- ? freeze([...createTable.constraints, constraint])
46
- : freeze([constraint]),
47
- });
48
- },
49
- cloneWithCheckConstraint(createTable, constraintName, sql) {
50
- const constraint = CheckConstraintNode.create(sql, constraintName);
51
- return freeze({
52
- ...createTable,
53
- constraints: createTable.constraints
54
- ? freeze([...createTable.constraints, constraint])
55
- : freeze([constraint]),
56
- });
57
- },
58
- cloneWithForeignKeyConstraint(createTable, constraint) {
28
+ cloneWithConstraint(createTable, constraint) {
59
29
  return freeze({
60
30
  ...createTable,
61
31
  constraints: createTable.constraints
@@ -21,5 +21,6 @@ export declare const JoinNode: Readonly<{
21
21
  is(node: OperationNode): node is JoinNode;
22
22
  create(joinType: JoinType, table: JoinTableNode): JoinNode;
23
23
  createWithOn(joinType: JoinType, table: JoinTableNode, on: JoinNodeOnNode): JoinNode;
24
- cloneWithOn(joinNode: JoinNode, op: 'And' | 'Or', on: JoinNodeOnNode): JoinNode;
24
+ cloneWithOn(joinNode: JoinNode, on: JoinNodeOnNode): JoinNode;
25
+ cloneWithOrOn(joinNode: JoinNode, on: JoinNodeOnNode): JoinNode;
25
26
  }>;