knex 0.21.20 → 0.21.21

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 (141) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/CONTRIBUTING.md +184 -184
  3. package/LICENSE +22 -22
  4. package/README.md +95 -95
  5. package/bin/cli.js +414 -414
  6. package/bin/utils/cli-config-utils.js +151 -151
  7. package/bin/utils/constants.js +7 -7
  8. package/bin/utils/migrationsLister.js +37 -37
  9. package/knex.js +8 -8
  10. package/lib/client.js +413 -413
  11. package/lib/config-resolver.js +61 -61
  12. package/lib/constants.js +44 -44
  13. package/lib/dialects/mssql/index.js +390 -390
  14. package/lib/dialects/mssql/query/compiler.js +444 -444
  15. package/lib/dialects/mssql/schema/columncompiler.js +103 -103
  16. package/lib/dialects/mssql/schema/compiler.js +59 -59
  17. package/lib/dialects/mssql/schema/tablecompiler.js +245 -245
  18. package/lib/dialects/mssql/transaction.js +97 -97
  19. package/lib/dialects/mysql/index.js +191 -191
  20. package/lib/dialects/mysql/query/compiler.js +142 -142
  21. package/lib/dialects/mysql/schema/columncompiler.js +171 -171
  22. package/lib/dialects/mysql/schema/compiler.js +60 -60
  23. package/lib/dialects/mysql/schema/tablecompiler.js +262 -262
  24. package/lib/dialects/mysql/transaction.js +48 -48
  25. package/lib/dialects/mysql2/index.js +35 -35
  26. package/lib/dialects/mysql2/transaction.js +46 -46
  27. package/lib/dialects/oracle/DEAD_CODE.md +5 -5
  28. package/lib/dialects/oracle/formatter.js +20 -20
  29. package/lib/dialects/oracle/index.js +79 -79
  30. package/lib/dialects/oracle/query/compiler.js +327 -327
  31. package/lib/dialects/oracle/schema/columnbuilder.js +18 -18
  32. package/lib/dialects/oracle/schema/columncompiler.js +139 -139
  33. package/lib/dialects/oracle/schema/compiler.js +81 -81
  34. package/lib/dialects/oracle/schema/tablecompiler.js +165 -165
  35. package/lib/dialects/oracle/schema/trigger.js +126 -126
  36. package/lib/dialects/oracle/utils.js +86 -86
  37. package/lib/dialects/oracledb/index.js +489 -489
  38. package/lib/dialects/oracledb/query/compiler.js +363 -363
  39. package/lib/dialects/oracledb/schema/columncompiler.js +35 -35
  40. package/lib/dialects/oracledb/transaction.js +76 -76
  41. package/lib/dialects/oracledb/utils.js +14 -14
  42. package/lib/dialects/postgres/index.js +319 -319
  43. package/lib/dialects/postgres/query/compiler.js +206 -206
  44. package/lib/dialects/postgres/schema/columncompiler.js +125 -125
  45. package/lib/dialects/postgres/schema/compiler.js +109 -109
  46. package/lib/dialects/postgres/schema/tablecompiler.js +183 -183
  47. package/lib/dialects/redshift/index.js +73 -73
  48. package/lib/dialects/redshift/query/compiler.js +119 -119
  49. package/lib/dialects/redshift/schema/columnbuilder.js +20 -20
  50. package/lib/dialects/redshift/schema/columncompiler.js +60 -60
  51. package/lib/dialects/redshift/schema/compiler.js +14 -14
  52. package/lib/dialects/redshift/schema/tablecompiler.js +123 -123
  53. package/lib/dialects/redshift/transaction.js +18 -18
  54. package/lib/dialects/sqlite3/formatter.js +21 -21
  55. package/lib/dialects/sqlite3/index.js +169 -169
  56. package/lib/dialects/sqlite3/query/compiler.js +222 -222
  57. package/lib/dialects/sqlite3/schema/columncompiler.js +27 -27
  58. package/lib/dialects/sqlite3/schema/compiler.js +49 -49
  59. package/lib/dialects/sqlite3/schema/ddl.js +525 -525
  60. package/lib/dialects/sqlite3/schema/tablecompiler.js +238 -238
  61. package/lib/formatter.js +295 -295
  62. package/lib/functionhelper.js +14 -14
  63. package/lib/helpers.js +92 -92
  64. package/lib/index.js +3 -3
  65. package/lib/interface.js +115 -115
  66. package/lib/knex.js +42 -42
  67. package/lib/logger.js +76 -76
  68. package/lib/migrate/MigrationGenerator.js +82 -82
  69. package/lib/migrate/Migrator.js +611 -611
  70. package/lib/migrate/configuration-merger.js +60 -60
  71. package/lib/migrate/migrate-stub.js +17 -17
  72. package/lib/migrate/migration-list-resolver.js +36 -36
  73. package/lib/migrate/sources/fs-migrations.js +99 -99
  74. package/lib/migrate/stub/cjs.stub +15 -15
  75. package/lib/migrate/stub/coffee.stub +13 -13
  76. package/lib/migrate/stub/eg.stub +14 -14
  77. package/lib/migrate/stub/js.stub +15 -15
  78. package/lib/migrate/stub/knexfile-coffee.stub +34 -34
  79. package/lib/migrate/stub/knexfile-eg.stub +43 -43
  80. package/lib/migrate/stub/knexfile-js.stub +44 -44
  81. package/lib/migrate/stub/knexfile-ls.stub +35 -35
  82. package/lib/migrate/stub/knexfile-ts.stub +44 -44
  83. package/lib/migrate/stub/ls.stub +14 -14
  84. package/lib/migrate/stub/ts.stub +21 -21
  85. package/lib/migrate/table-creator.js +67 -67
  86. package/lib/migrate/table-resolver.js +27 -27
  87. package/lib/query/builder.js +1372 -1372
  88. package/lib/query/compiler.js +889 -889
  89. package/lib/query/constants.js +13 -13
  90. package/lib/query/joinclause.js +263 -263
  91. package/lib/query/methods.js +92 -92
  92. package/lib/query/string.js +190 -190
  93. package/lib/raw.js +188 -188
  94. package/lib/ref.js +39 -39
  95. package/lib/runner.js +285 -285
  96. package/lib/schema/builder.js +82 -82
  97. package/lib/schema/columnbuilder.js +117 -117
  98. package/lib/schema/columncompiler.js +177 -177
  99. package/lib/schema/compiler.js +101 -101
  100. package/lib/schema/helpers.js +51 -51
  101. package/lib/schema/tablebuilder.js +288 -288
  102. package/lib/schema/tablecompiler.js +296 -296
  103. package/lib/seed/Seeder.js +203 -203
  104. package/lib/seed/seed-stub.js +13 -13
  105. package/lib/seed/stub/coffee.stub +9 -9
  106. package/lib/seed/stub/eg.stub +11 -11
  107. package/lib/seed/stub/js.stub +13 -13
  108. package/lib/seed/stub/ls.stub +11 -11
  109. package/lib/seed/stub/ts.stub +13 -13
  110. package/lib/transaction.js +363 -363
  111. package/lib/util/batchInsert.js +59 -59
  112. package/lib/util/delay.js +6 -6
  113. package/lib/util/fake-client.js +9 -9
  114. package/lib/util/finally-mixin.js +13 -13
  115. package/lib/util/fs.js +76 -76
  116. package/lib/util/import-file.js +13 -13
  117. package/lib/util/is-module-type.js +14 -14
  118. package/lib/util/is.js +32 -32
  119. package/lib/util/make-knex.js +338 -338
  120. package/lib/util/nanoid.js +29 -29
  121. package/lib/util/noop.js +1 -1
  122. package/lib/util/parse-connection.js +66 -66
  123. package/lib/util/save-async-stack.js +14 -14
  124. package/lib/util/template.js +52 -52
  125. package/lib/util/timeout.js +29 -29
  126. package/lib/util/timestamp.js +16 -16
  127. package/package.json +1 -1
  128. package/scripts/build.js +125 -125
  129. package/scripts/docker-compose.yml +111 -111
  130. package/scripts/next-release-howto.md +24 -24
  131. package/scripts/release.sh +34 -34
  132. package/scripts/runkit-example.js +34 -34
  133. package/scripts/stress-test/README.txt +18 -18
  134. package/scripts/stress-test/docker-compose.yml +47 -47
  135. package/scripts/stress-test/knex-stress-test.js +196 -196
  136. package/scripts/stress-test/mysql2-random-hanging-every-now-and-then.js +145 -145
  137. package/scripts/stress-test/mysql2-sudden-exit-without-error.js +100 -100
  138. package/scripts/stress-test/reconnect-test-mysql-based-drivers.js +184 -184
  139. package/types/index.d.ts +2249 -2249
  140. package/types/result.d.ts +27 -27
  141. package/types/tables.d.ts +4 -4
@@ -1,288 +1,288 @@
1
- // TableBuilder
2
-
3
- // Takes the function passed to the "createTable" or "table/editTable"
4
- // functions and calls it with the "TableBuilder" as both the context and
5
- // the first argument. Inside this function we can specify what happens to the
6
- // method, pushing everything we want to do onto the "allStatements" array,
7
- // which is then compiled into sql.
8
- // ------
9
- const each = require('lodash/each');
10
- const extend = require('lodash/extend');
11
- const toArray = require('lodash/toArray');
12
- const helpers = require('../helpers');
13
- const { isString, isFunction } = require('../util/is');
14
-
15
- function TableBuilder(client, method, tableName, fn) {
16
- this.client = client;
17
- this._fn = fn;
18
- this._method = method;
19
- this._schemaName = undefined;
20
- this._tableName = tableName;
21
- this._statements = [];
22
- this._single = {};
23
-
24
- if (!isFunction(this._fn)) {
25
- throw new TypeError(
26
- 'A callback function must be supplied to calls against `.createTable` ' +
27
- 'and `.table`'
28
- );
29
- }
30
- }
31
-
32
- TableBuilder.prototype.setSchema = function (schemaName) {
33
- this._schemaName = schemaName;
34
- };
35
-
36
- // Convert the current tableBuilder object "toSQL"
37
- // giving us additional methods if we're altering
38
- // rather than creating the table.
39
- TableBuilder.prototype.toSQL = function () {
40
- if (this._method === 'alter') {
41
- extend(this, AlterMethods);
42
- }
43
- this._fn.call(this, this);
44
- return this.client.tableCompiler(this).toSQL();
45
- };
46
-
47
- each(
48
- [
49
- // Each of the index methods can be called individually, with the
50
- // column name to be used, e.g. table.unique('column').
51
- 'index',
52
- 'primary',
53
- 'unique',
54
-
55
- // Key specific
56
- 'dropPrimary',
57
- 'dropUnique',
58
- 'dropIndex',
59
- 'dropForeign',
60
- ],
61
- function (method) {
62
- TableBuilder.prototype[method] = function () {
63
- this._statements.push({
64
- grouping: 'alterTable',
65
- method,
66
- args: toArray(arguments),
67
- });
68
- return this;
69
- };
70
- }
71
- );
72
-
73
- // Warn for dialect-specific table methods, since that's the
74
- // only time these are supported.
75
- const specialMethods = {
76
- mysql: ['engine', 'charset', 'collate'],
77
- postgresql: ['inherits'],
78
- };
79
- each(specialMethods, function (methods, dialect) {
80
- methods.forEach(function (method) {
81
- TableBuilder.prototype[method] = function (value) {
82
- if (this.client.dialect !== dialect) {
83
- throw new Error(
84
- `Knex only supports ${method} statement with ${dialect}.`
85
- );
86
- }
87
- if (this._method === 'alter') {
88
- throw new Error(
89
- `Knex does not support altering the ${method} outside of create ` +
90
- `table, please use knex.raw statement.`
91
- );
92
- }
93
- this._single[method] = value;
94
- };
95
- });
96
- });
97
-
98
- helpers.addQueryContext(TableBuilder);
99
-
100
- // Each of the column types that we can add, we create a new ColumnBuilder
101
- // instance and push it onto the statements array.
102
- const columnTypes = [
103
- // Numeric
104
- 'tinyint',
105
- 'smallint',
106
- 'mediumint',
107
- 'int',
108
- 'bigint',
109
- 'decimal',
110
- 'float',
111
- 'double',
112
- 'real',
113
- 'bit',
114
- 'boolean',
115
- 'serial',
116
-
117
- // Date / Time
118
- 'date',
119
- 'datetime',
120
- 'timestamp',
121
- 'time',
122
- 'year',
123
-
124
- // String
125
- 'char',
126
- 'varchar',
127
- 'tinytext',
128
- 'tinyText',
129
- 'text',
130
- 'mediumtext',
131
- 'mediumText',
132
- 'longtext',
133
- 'longText',
134
- 'binary',
135
- 'varbinary',
136
- 'tinyblob',
137
- 'tinyBlob',
138
- 'mediumblob',
139
- 'mediumBlob',
140
- 'blob',
141
- 'longblob',
142
- 'longBlob',
143
- 'enum',
144
- 'set',
145
-
146
- // Increments, Aliases, and Additional
147
- 'bool',
148
- 'dateTime',
149
- 'increments',
150
- 'bigincrements',
151
- 'bigIncrements',
152
- 'integer',
153
- 'biginteger',
154
- 'bigInteger',
155
- 'string',
156
- 'json',
157
- 'jsonb',
158
- 'uuid',
159
- 'enu',
160
- 'specificType',
161
- ];
162
-
163
- // For each of the column methods, create a new "ColumnBuilder" interface,
164
- // push it onto the "allStatements" stack, and then return the interface,
165
- // with which we can add indexes, etc.
166
- each(columnTypes, function (type) {
167
- TableBuilder.prototype[type] = function () {
168
- const args = toArray(arguments);
169
- const builder = this.client.columnBuilder(this, type, args);
170
- this._statements.push({
171
- grouping: 'columns',
172
- builder,
173
- });
174
- return builder;
175
- };
176
- });
177
-
178
- // The "timestamps" call is really just sets the `created_at` and `updated_at` columns.
179
- TableBuilder.prototype.timestamps = function timestamps() {
180
- const method = arguments[0] === true ? 'timestamp' : 'datetime';
181
- const createdAt = this[method]('created_at');
182
- const updatedAt = this[method]('updated_at');
183
- if (arguments[1] === true) {
184
- const now = this.client.raw('CURRENT_TIMESTAMP');
185
- createdAt.notNullable().defaultTo(now);
186
- updatedAt.notNullable().defaultTo(now);
187
- }
188
- return;
189
- };
190
-
191
- // Set the comment value for a table, they're only allowed to be called
192
- // once per table.
193
- TableBuilder.prototype.comment = function (value) {
194
- if (typeof value !== 'string') {
195
- throw new TypeError('Table comment must be string');
196
- }
197
- this._single.comment = value;
198
- };
199
-
200
- // Set a foreign key on the table, calling
201
- // `table.foreign('column_name').references('column').on('table').onDelete()...
202
- // Also called from the ColumnBuilder context when chaining.
203
- TableBuilder.prototype.foreign = function (column, keyName) {
204
- const foreignData = { column: column, keyName: keyName };
205
- this._statements.push({
206
- grouping: 'alterTable',
207
- method: 'foreign',
208
- args: [foreignData],
209
- });
210
- let returnObj = {
211
- references(tableColumn) {
212
- let pieces;
213
- if (isString(tableColumn)) {
214
- pieces = tableColumn.split('.');
215
- }
216
- if (!pieces || pieces.length === 1) {
217
- foreignData.references = pieces ? pieces[0] : tableColumn;
218
- return {
219
- on(tableName) {
220
- if (typeof tableName !== 'string') {
221
- throw new TypeError(
222
- `Expected tableName to be a string, got: ${typeof tableName}`
223
- );
224
- }
225
- foreignData.inTable = tableName;
226
- return returnObj;
227
- },
228
- inTable() {
229
- return this.on.apply(this, arguments);
230
- },
231
- };
232
- }
233
- foreignData.inTable = pieces[0];
234
- foreignData.references = pieces[1];
235
- return returnObj;
236
- },
237
- withKeyName(keyName) {
238
- foreignData.keyName = keyName;
239
- return returnObj;
240
- },
241
- onUpdate(statement) {
242
- foreignData.onUpdate = statement;
243
- return returnObj;
244
- },
245
- onDelete(statement) {
246
- foreignData.onDelete = statement;
247
- return returnObj;
248
- },
249
- _columnBuilder(builder) {
250
- extend(builder, returnObj);
251
- returnObj = builder;
252
- return builder;
253
- },
254
- };
255
- return returnObj;
256
- };
257
-
258
- const AlterMethods = {
259
- // Renames the current column `from` the current
260
- // TODO: this.column(from).rename(to)
261
- renameColumn(from, to) {
262
- this._statements.push({
263
- grouping: 'alterTable',
264
- method: 'renameColumn',
265
- args: [from, to],
266
- });
267
- return this;
268
- },
269
-
270
- dropTimestamps() {
271
- return this.dropColumns(['created_at', 'updated_at']);
272
- },
273
-
274
- // TODO: changeType
275
- };
276
-
277
- // Drop a column from the current table.
278
- // TODO: Enable this.column(columnName).drop();
279
- AlterMethods.dropColumn = AlterMethods.dropColumns = function () {
280
- this._statements.push({
281
- grouping: 'alterTable',
282
- method: 'dropColumn',
283
- args: toArray(arguments),
284
- });
285
- return this;
286
- };
287
-
288
- module.exports = TableBuilder;
1
+ // TableBuilder
2
+
3
+ // Takes the function passed to the "createTable" or "table/editTable"
4
+ // functions and calls it with the "TableBuilder" as both the context and
5
+ // the first argument. Inside this function we can specify what happens to the
6
+ // method, pushing everything we want to do onto the "allStatements" array,
7
+ // which is then compiled into sql.
8
+ // ------
9
+ const each = require('lodash/each');
10
+ const extend = require('lodash/extend');
11
+ const toArray = require('lodash/toArray');
12
+ const helpers = require('../helpers');
13
+ const { isString, isFunction } = require('../util/is');
14
+
15
+ function TableBuilder(client, method, tableName, fn) {
16
+ this.client = client;
17
+ this._fn = fn;
18
+ this._method = method;
19
+ this._schemaName = undefined;
20
+ this._tableName = tableName;
21
+ this._statements = [];
22
+ this._single = {};
23
+
24
+ if (!isFunction(this._fn)) {
25
+ throw new TypeError(
26
+ 'A callback function must be supplied to calls against `.createTable` ' +
27
+ 'and `.table`'
28
+ );
29
+ }
30
+ }
31
+
32
+ TableBuilder.prototype.setSchema = function (schemaName) {
33
+ this._schemaName = schemaName;
34
+ };
35
+
36
+ // Convert the current tableBuilder object "toSQL"
37
+ // giving us additional methods if we're altering
38
+ // rather than creating the table.
39
+ TableBuilder.prototype.toSQL = function () {
40
+ if (this._method === 'alter') {
41
+ extend(this, AlterMethods);
42
+ }
43
+ this._fn.call(this, this);
44
+ return this.client.tableCompiler(this).toSQL();
45
+ };
46
+
47
+ each(
48
+ [
49
+ // Each of the index methods can be called individually, with the
50
+ // column name to be used, e.g. table.unique('column').
51
+ 'index',
52
+ 'primary',
53
+ 'unique',
54
+
55
+ // Key specific
56
+ 'dropPrimary',
57
+ 'dropUnique',
58
+ 'dropIndex',
59
+ 'dropForeign',
60
+ ],
61
+ function (method) {
62
+ TableBuilder.prototype[method] = function () {
63
+ this._statements.push({
64
+ grouping: 'alterTable',
65
+ method,
66
+ args: toArray(arguments),
67
+ });
68
+ return this;
69
+ };
70
+ }
71
+ );
72
+
73
+ // Warn for dialect-specific table methods, since that's the
74
+ // only time these are supported.
75
+ const specialMethods = {
76
+ mysql: ['engine', 'charset', 'collate'],
77
+ postgresql: ['inherits'],
78
+ };
79
+ each(specialMethods, function (methods, dialect) {
80
+ methods.forEach(function (method) {
81
+ TableBuilder.prototype[method] = function (value) {
82
+ if (this.client.dialect !== dialect) {
83
+ throw new Error(
84
+ `Knex only supports ${method} statement with ${dialect}.`
85
+ );
86
+ }
87
+ if (this._method === 'alter') {
88
+ throw new Error(
89
+ `Knex does not support altering the ${method} outside of create ` +
90
+ `table, please use knex.raw statement.`
91
+ );
92
+ }
93
+ this._single[method] = value;
94
+ };
95
+ });
96
+ });
97
+
98
+ helpers.addQueryContext(TableBuilder);
99
+
100
+ // Each of the column types that we can add, we create a new ColumnBuilder
101
+ // instance and push it onto the statements array.
102
+ const columnTypes = [
103
+ // Numeric
104
+ 'tinyint',
105
+ 'smallint',
106
+ 'mediumint',
107
+ 'int',
108
+ 'bigint',
109
+ 'decimal',
110
+ 'float',
111
+ 'double',
112
+ 'real',
113
+ 'bit',
114
+ 'boolean',
115
+ 'serial',
116
+
117
+ // Date / Time
118
+ 'date',
119
+ 'datetime',
120
+ 'timestamp',
121
+ 'time',
122
+ 'year',
123
+
124
+ // String
125
+ 'char',
126
+ 'varchar',
127
+ 'tinytext',
128
+ 'tinyText',
129
+ 'text',
130
+ 'mediumtext',
131
+ 'mediumText',
132
+ 'longtext',
133
+ 'longText',
134
+ 'binary',
135
+ 'varbinary',
136
+ 'tinyblob',
137
+ 'tinyBlob',
138
+ 'mediumblob',
139
+ 'mediumBlob',
140
+ 'blob',
141
+ 'longblob',
142
+ 'longBlob',
143
+ 'enum',
144
+ 'set',
145
+
146
+ // Increments, Aliases, and Additional
147
+ 'bool',
148
+ 'dateTime',
149
+ 'increments',
150
+ 'bigincrements',
151
+ 'bigIncrements',
152
+ 'integer',
153
+ 'biginteger',
154
+ 'bigInteger',
155
+ 'string',
156
+ 'json',
157
+ 'jsonb',
158
+ 'uuid',
159
+ 'enu',
160
+ 'specificType',
161
+ ];
162
+
163
+ // For each of the column methods, create a new "ColumnBuilder" interface,
164
+ // push it onto the "allStatements" stack, and then return the interface,
165
+ // with which we can add indexes, etc.
166
+ each(columnTypes, function (type) {
167
+ TableBuilder.prototype[type] = function () {
168
+ const args = toArray(arguments);
169
+ const builder = this.client.columnBuilder(this, type, args);
170
+ this._statements.push({
171
+ grouping: 'columns',
172
+ builder,
173
+ });
174
+ return builder;
175
+ };
176
+ });
177
+
178
+ // The "timestamps" call is really just sets the `created_at` and `updated_at` columns.
179
+ TableBuilder.prototype.timestamps = function timestamps() {
180
+ const method = arguments[0] === true ? 'timestamp' : 'datetime';
181
+ const createdAt = this[method]('created_at');
182
+ const updatedAt = this[method]('updated_at');
183
+ if (arguments[1] === true) {
184
+ const now = this.client.raw('CURRENT_TIMESTAMP');
185
+ createdAt.notNullable().defaultTo(now);
186
+ updatedAt.notNullable().defaultTo(now);
187
+ }
188
+ return;
189
+ };
190
+
191
+ // Set the comment value for a table, they're only allowed to be called
192
+ // once per table.
193
+ TableBuilder.prototype.comment = function (value) {
194
+ if (typeof value !== 'string') {
195
+ throw new TypeError('Table comment must be string');
196
+ }
197
+ this._single.comment = value;
198
+ };
199
+
200
+ // Set a foreign key on the table, calling
201
+ // `table.foreign('column_name').references('column').on('table').onDelete()...
202
+ // Also called from the ColumnBuilder context when chaining.
203
+ TableBuilder.prototype.foreign = function (column, keyName) {
204
+ const foreignData = { column: column, keyName: keyName };
205
+ this._statements.push({
206
+ grouping: 'alterTable',
207
+ method: 'foreign',
208
+ args: [foreignData],
209
+ });
210
+ let returnObj = {
211
+ references(tableColumn) {
212
+ let pieces;
213
+ if (isString(tableColumn)) {
214
+ pieces = tableColumn.split('.');
215
+ }
216
+ if (!pieces || pieces.length === 1) {
217
+ foreignData.references = pieces ? pieces[0] : tableColumn;
218
+ return {
219
+ on(tableName) {
220
+ if (typeof tableName !== 'string') {
221
+ throw new TypeError(
222
+ `Expected tableName to be a string, got: ${typeof tableName}`
223
+ );
224
+ }
225
+ foreignData.inTable = tableName;
226
+ return returnObj;
227
+ },
228
+ inTable() {
229
+ return this.on.apply(this, arguments);
230
+ },
231
+ };
232
+ }
233
+ foreignData.inTable = pieces[0];
234
+ foreignData.references = pieces[1];
235
+ return returnObj;
236
+ },
237
+ withKeyName(keyName) {
238
+ foreignData.keyName = keyName;
239
+ return returnObj;
240
+ },
241
+ onUpdate(statement) {
242
+ foreignData.onUpdate = statement;
243
+ return returnObj;
244
+ },
245
+ onDelete(statement) {
246
+ foreignData.onDelete = statement;
247
+ return returnObj;
248
+ },
249
+ _columnBuilder(builder) {
250
+ extend(builder, returnObj);
251
+ returnObj = builder;
252
+ return builder;
253
+ },
254
+ };
255
+ return returnObj;
256
+ };
257
+
258
+ const AlterMethods = {
259
+ // Renames the current column `from` the current
260
+ // TODO: this.column(from).rename(to)
261
+ renameColumn(from, to) {
262
+ this._statements.push({
263
+ grouping: 'alterTable',
264
+ method: 'renameColumn',
265
+ args: [from, to],
266
+ });
267
+ return this;
268
+ },
269
+
270
+ dropTimestamps() {
271
+ return this.dropColumns(['created_at', 'updated_at']);
272
+ },
273
+
274
+ // TODO: changeType
275
+ };
276
+
277
+ // Drop a column from the current table.
278
+ // TODO: Enable this.column(columnName).drop();
279
+ AlterMethods.dropColumn = AlterMethods.dropColumns = function () {
280
+ this._statements.push({
281
+ grouping: 'alterTable',
282
+ method: 'dropColumn',
283
+ args: toArray(arguments),
284
+ });
285
+ return this;
286
+ };
287
+
288
+ module.exports = TableBuilder;