knex 1.0.7 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/CHANGELOG.md +51 -3
  2. package/CONTRIBUTING.md +10 -0
  3. package/UPGRADING.md +4 -0
  4. package/lib/client.js +1 -4
  5. package/lib/dialects/cockroachdb/crdb-columncompiler.js +14 -0
  6. package/lib/dialects/cockroachdb/index.js +5 -0
  7. package/lib/dialects/index.js +34 -0
  8. package/lib/dialects/mssql/schema/mssql-compiler.js +9 -2
  9. package/lib/dialects/mysql/query/mysql-querycompiler.js +1 -1
  10. package/lib/dialects/mysql/schema/mysql-compiler.js +2 -2
  11. package/lib/dialects/oracledb/index.js +2 -1
  12. package/lib/dialects/postgres/schema/pg-columncompiler.js +7 -1
  13. package/lib/dialects/postgres/schema/pg-compiler.js +2 -2
  14. package/lib/dialects/postgres/schema/pg-tablecompiler.js +33 -6
  15. package/lib/dialects/sqlite3/index.js +11 -4
  16. package/lib/dialects/sqlite3/query/sqlite-querycompiler.js +1 -1
  17. package/lib/execution/runner.js +2 -9
  18. package/lib/knex-builder/Knex.js +28 -0
  19. package/lib/knex-builder/internal/config-resolver.js +2 -3
  20. package/lib/knex-builder/internal/parse-connection.js +4 -2
  21. package/lib/migrations/migrate/MigrationGenerator.js +1 -1
  22. package/lib/migrations/migrate/stub/js-schema.stub +22 -0
  23. package/lib/migrations/migrate/stub/ts-schema.stub +21 -0
  24. package/lib/schema/builder.js +12 -0
  25. package/lib/schema/columnbuilder.js +12 -0
  26. package/lib/schema/columncompiler.js +4 -2
  27. package/lib/schema/tablebuilder.js +12 -0
  28. package/lib/schema/viewbuilder.js +12 -0
  29. package/package.json +29 -24
  30. package/scripts/clean.js +29 -0
  31. package/scripts/runkit-example.js +1 -1
  32. package/scripts/update_gitignore_for_tsc_output.js +85 -0
  33. package/types/index.d.ts +843 -392
package/CHANGELOG.md CHANGED
@@ -1,19 +1,67 @@
1
1
  # Master (Unreleased)
2
2
 
3
- # 1.0.7 - 13 March, 2022
3
+ # 2.2.0 - 19 July, 2022
4
+
5
+ ### New features:
6
+
7
+ - Inline primary key creation for postgres flavours #5233
8
+ - SQLite: Add warning for undefined connection file #5223
9
+ - MSSQL: Add JSON parameter support for connection #5200
10
+
11
+ ### Bug fixes:
12
+
13
+ - PostgreSQL: add primaryKey option for uuid #5212
14
+
15
+ ### Typings:
16
+
17
+ - Add promisable and better types #5222
18
+ - Update raw query bind parameter type #5208
19
+
20
+ # 2.1.0 - 26 May, 2022
21
+
22
+ ### New features:
23
+
24
+ - Improve bundling experience to safely import dialects while using static paths #5142
25
+ - Implement extendable builders #5041
26
+ - PostgreSQL: Refresh materialized view concurrently #5166
27
+
28
+ ### Bug fixes:
29
+
30
+ - Use correct paths in package.json browser field #5174
31
+ - MariaDB: Fix 'NULL' returned instead of NULL on MariaDB 10.2.6+ #5181
32
+ - MySQL: fix hasColumn Error (hasColumn ('a_id') is true, but hasColumn('a_Id') is false) #5148
33
+ - MSSQL: Fix .hasTable result when using .withSchema #5176
34
+ - Oracle: correctly INSERTS Buffer #4869
35
+
36
+ ### Typings:
37
+
38
+ - Update type definitions for pg connection #5139
39
+
40
+ # 2.0.0 - 21 April, 2022
41
+
42
+ ### Breaking changes
43
+
44
+ - Restore sqlite3 package #5136
45
+
46
+ ### Test / internal changes:
47
+
48
+ - Migrate Husky from 4 to 7 #5137
49
+ - Migrate Jake to 10.8.5 #5138
50
+
51
+ # 1.0.7 - 13 April, 2022
4
52
 
5
53
  ### Bug fixes:
6
54
 
7
55
  - CLI: Fix cli migrate:make SQLite dependency #5106
8
56
 
9
- # 1.0.6 - 12 March, 2022
57
+ # 1.0.6 - 12 April, 2022
10
58
 
11
59
  ### Bug fixes:
12
60
 
13
61
  - PostgreSQL: Wait for search path to be set before returning connection #5107
14
62
  - CLI: No client override during migrate:make #5109
15
63
 
16
- # 1.0.5 - 05 March, 2022
64
+ # 1.0.5 - 05 April, 2022
17
65
 
18
66
  ### New features:
19
67
 
package/CONTRIBUTING.md CHANGED
@@ -169,6 +169,16 @@ Once this is done, check it works by attempting to login:
169
169
  psql -h localhost -U postgres -d knex_test
170
170
  ```
171
171
 
172
+ ## Typescript source files
173
+
174
+ > TL;DR: Starting with release 2.0.0 Knex is adding support for Typescript source files. Thus to develop in this repo you will need to run `npm run build` each time you edit `.ts` files to generate the resulting `.js` files. This is automatically run whenever you run `npm install` or checkout a new Git branch so when developing in Javascript you don't have to worry about it. It is encouraged that new functionality and sources be written in Typescript but this is not required.
175
+
176
+ Starting with release 2.0.0, Knex is support source additions in Typescript! This allows for better safety in the code added. However, pre-2.0.0 Knex was always written in pure Javascript and thus a "hybrid" approach is being used for 2.0.0 to allow for the new `.ts` files to exist along `.js` files that make up the majority of this repository.
177
+
178
+ To develop in this repository use the `npm run build` and `npm run clean` commands to compile and delete the `.js` and related files from `.ts` files. If you wish to have the `tsc` compiled watch and recompile on changes then run `npm run build:ts -- --watch`. Note that for easy integration with Javascript the outputted files are done in a "side-by-side" manner meaning that `lib/foo/bar.ts` will result in `lib/foo/bar.js`. This is done automatically via the npm script command `"prepare"` whenever you run `npm install` and Git hook for `post-checkout` (added by Husky) which executes when you run commands like `git checkout` , thus making it easier to not have to worry about this if you're working in pure Javascript.
179
+
180
+ The script file `./scripts/update_gitignore_for_tsc_output.js` file is called as part of the `npm run build` command which will update the `lib/.gitignore` file which is used to ensure generated `.js` and related files from `tsc` compilation are not checked into the git repo.
181
+
172
182
  ## Want to be Collaborator?
173
183
 
174
184
  There is always room for more collaborators. Be active on resolving github issues / sending pull requests / reviewing code and we will ask you to join.
package/UPGRADING.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## Upgrading to new knex.js versions
2
2
 
3
+ ### Upgrading to version 2.0.0+
4
+
5
+ * Since `sqlite3` is maintained again, we switched back to it. If you are using `@vscode/sqlite3` driver dependency, please replace it with `sqlite3` in your `package.json`;
6
+
3
7
  ### Upgrading to version 1.0.0+
4
8
 
5
9
  * Node.js older than 12 is no longer supported, make sure to update your environment;
package/lib/client.js CHANGED
@@ -189,10 +189,7 @@ class Client extends EventEmitter {
189
189
  try {
190
190
  this.driver = this._driver();
191
191
  } catch (e) {
192
- const driverName = this.aliasDriverName
193
- ? this.aliasDriverName
194
- : this.driverName;
195
- const message = `Knex: run\n$ npm install ${driverName} --save`;
192
+ const message = `Knex: run\n$ npm install ${this.driverName} --save`;
196
193
  this.logger.error(`${message}\n${e.message}\n${e.stack}`);
197
194
  throw new Error(`${message}\n${e.message}`);
198
195
  }
@@ -0,0 +1,14 @@
1
+ const ColumnCompiler_PG = require('../postgres/schema/pg-columncompiler.js');
2
+
3
+ class ColumnCompiler_CRDB extends ColumnCompiler_PG {
4
+ uuid(options = { primaryKey: false }) {
5
+ return (
6
+ 'uuid' +
7
+ (this.tableCompiler._canBeAddPrimaryKey(options)
8
+ ? ' primary key default gen_random_uuid()'
9
+ : '')
10
+ );
11
+ }
12
+ }
13
+
14
+ module.exports = ColumnCompiler_CRDB;
@@ -3,6 +3,7 @@
3
3
  const Client_PostgreSQL = require('../postgres');
4
4
  const Transaction = require('../postgres/execution/pg-transaction');
5
5
  const QueryCompiler = require('./crdb-querycompiler');
6
+ const ColumnCompiler = require('./crdb-columncompiler');
6
7
  const TableCompiler = require('./crdb-tablecompiler');
7
8
  const ViewCompiler = require('./crdb-viewcompiler');
8
9
  const QueryBuilder = require('./crdb-querybuilder');
@@ -19,6 +20,10 @@ class Client_CockroachDB extends Client_PostgreSQL {
19
20
  return new QueryCompiler(this, builder, formatter);
20
21
  }
21
22
 
23
+ columnCompiler() {
24
+ return new ColumnCompiler(this, ...arguments);
25
+ }
26
+
22
27
  tableCompiler() {
23
28
  return new TableCompiler(this, ...arguments);
24
29
  }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDialectByNameOrAlias = void 0;
4
+ const { resolveClientNameWithAliases } = require('../util/helpers');
5
+ const dbNameToDialectLoader = Object.freeze({
6
+ 'better-sqlite3': () => require('./better-sqlite3'),
7
+ cockroachdb: () => require('./cockroachdb'),
8
+ mssql: () => require('./mssql'),
9
+ mysql: () => require('./mysql'),
10
+ mysql2: () => require('./mysql2'),
11
+ oracle: () => require('./oracle'),
12
+ oracledb: () => require('./oracledb'),
13
+ pgnative: () => require('./pgnative'),
14
+ postgres: () => require('./postgres'),
15
+ redshift: () => require('./redshift'),
16
+ sqlite3: () => require('./sqlite3'),
17
+ });
18
+ /**
19
+ * Gets the Dialect object with the given client name or throw an
20
+ * error if not found.
21
+ *
22
+ * NOTE: This is a replacement for prior practice of doing dynamic
23
+ * string construction for imports of Dialect objects.
24
+ */
25
+ function getDialectByNameOrAlias(clientName) {
26
+ const resolvedClientName = resolveClientNameWithAliases(clientName);
27
+ const dialectLoader = dbNameToDialectLoader[resolvedClientName];
28
+ if (!dialectLoader) {
29
+ throw new Error(`Invalid clientName given: ${clientName}`);
30
+ }
31
+ return dialectLoader();
32
+ }
33
+ exports.getDialectByNameOrAlias = getDialectByNameOrAlias;
34
+ //# sourceMappingURL=index.js.map
@@ -49,10 +49,17 @@ class SchemaCompiler_MSSQL extends SchemaCompiler {
49
49
  this.builder,
50
50
  this.bindingsHolder
51
51
  );
52
- const sql =
52
+ const bindings = [tableName];
53
+ let sql =
53
54
  `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES ` +
54
55
  `WHERE TABLE_NAME = ${formattedTable}`;
55
- this.pushQuery({ sql, output: (resp) => resp.length > 0 });
56
+
57
+ if (this.schema) {
58
+ sql += ' AND TABLE_SCHEMA = ?';
59
+ bindings.push(this.schema);
60
+ }
61
+
62
+ this.pushQuery({ sql, bindings, output: (resp) => resp.length > 0 });
56
63
  }
57
64
 
58
65
  // Check whether a column exists on the schema.
@@ -131,7 +131,7 @@ class QueryCompiler_MySQL extends QueryCompiler {
131
131
  output(resp) {
132
132
  const out = resp.reduce(function (columns, val) {
133
133
  columns[val.COLUMN_NAME] = {
134
- defaultValue: val.COLUMN_DEFAULT,
134
+ defaultValue: val.COLUMN_DEFAULT === 'NULL' ? null : val.COLUMN_DEFAULT,
135
135
  type: val.DATA_TYPE,
136
136
  maxLength: val.CHARACTER_MAXIMUM_LENGTH,
137
137
  nullable: val.IS_NULLABLE === 'YES',
@@ -48,8 +48,8 @@ class SchemaCompiler_MySQL extends SchemaCompiler {
48
48
  output(resp) {
49
49
  return resp.some((row) => {
50
50
  return (
51
- this.client.wrapIdentifier(row.Field) ===
52
- this.client.wrapIdentifier(column)
51
+ this.client.wrapIdentifier(row.Field.toLowerCase()) ===
52
+ this.client.wrapIdentifier(column.toLowerCase())
53
53
  );
54
54
  });
55
55
  },
@@ -111,7 +111,8 @@ class Client_Oracledb extends Client_Oracle {
111
111
  this
112
112
  );
113
113
  } else if (value instanceof BlobHelper) {
114
- return 'EMPTY_BLOB()';
114
+ formatter.bindings.push(value.value);
115
+ return '?';
115
116
  }
116
117
  return unwrapRaw(value, true, builder, this, formatter) || '?';
117
118
  }
@@ -124,6 +124,13 @@ class ColumnCompiler_PG extends ColumnCompiler {
124
124
  (this.tableCompiler._canBeAddPrimaryKey(options) ? ' primary key' : '')
125
125
  );
126
126
  }
127
+
128
+ uuid(options = { primaryKey: false }) {
129
+ return (
130
+ 'uuid' +
131
+ (this.tableCompiler._canBeAddPrimaryKey(options) ? ' primary key' : '')
132
+ );
133
+ }
127
134
  }
128
135
 
129
136
  ColumnCompiler_PG.prototype.bigint = 'bigint';
@@ -133,7 +140,6 @@ ColumnCompiler_PG.prototype.double = 'double precision';
133
140
  ColumnCompiler_PG.prototype.floating = 'real';
134
141
  ColumnCompiler_PG.prototype.smallint = 'smallint';
135
142
  ColumnCompiler_PG.prototype.tinyint = 'smallint';
136
- ColumnCompiler_PG.prototype.uuid = 'uuid';
137
143
 
138
144
  function jsonColumn(client, jsonb) {
139
145
  if (
@@ -118,9 +118,9 @@ class SchemaCompiler_PG extends SchemaCompiler {
118
118
  );
119
119
  }
120
120
 
121
- refreshMaterializedView(viewName) {
121
+ refreshMaterializedView(viewName, concurrently = false) {
122
122
  this.pushQuery({
123
- sql: `refresh materialized view ${this.formatter.wrap(viewName)}`,
123
+ sql: `refresh materialized view${concurrently ? " concurrently" : ""} ${this.formatter.wrap(viewName)}`,
124
124
  });
125
125
  }
126
126
 
@@ -44,7 +44,10 @@ class TableCompiler_PG extends TableCompiler {
44
44
  const createStatement = ifNot
45
45
  ? 'create table if not exists '
46
46
  : 'create table ';
47
- const columnsSql = ' (' + columns.sql.join(', ') + this._addChecks() + ')';
47
+ const columnsSql = ` (${columns.sql.join(', ')}${
48
+ this.primaryKeys() || ''
49
+ }${this._addChecks()})`;
50
+
48
51
  let sql =
49
52
  createStatement +
50
53
  this.tableName() +
@@ -65,6 +68,28 @@ class TableCompiler_PG extends TableCompiler {
65
68
  if (hasComment) this.comment(this.single.comment);
66
69
  }
67
70
 
71
+ primaryKeys() {
72
+ const pks = (this.grouped.alterTable || []).filter(
73
+ (k) => k.method === 'primary'
74
+ );
75
+ if (pks.length > 0 && pks[0].args.length > 0) {
76
+ const columns = pks[0].args[0];
77
+ let constraintName = pks[0].args[1] || '';
78
+ let deferrable;
79
+ if (isObject(constraintName)) {
80
+ ({ constraintName, deferrable } = constraintName);
81
+ }
82
+ deferrable = deferrable ? ` deferrable initially ${deferrable}` : '';
83
+ constraintName = constraintName
84
+ ? this.formatter.wrap(constraintName)
85
+ : this.formatter.wrap(`${this.tableNameRaw}_pkey`);
86
+
87
+ return `, constraint ${constraintName} primary key (${this.formatter.columnize(
88
+ columns
89
+ )})${deferrable}`;
90
+ }
91
+ }
92
+
68
93
  addColumns(columns, prefix, colCompilers) {
69
94
  if (prefix === this.alterColumnsPrefix) {
70
95
  // alter columns
@@ -153,11 +178,13 @@ class TableCompiler_PG extends TableCompiler {
153
178
  constraintName = constraintName
154
179
  ? this.formatter.wrap(constraintName)
155
180
  : this.formatter.wrap(`${this.tableNameRaw}_pkey`);
156
- this.pushQuery(
157
- `alter table ${this.tableName()} add constraint ${constraintName} primary key (${this.formatter.columnize(
158
- columns
159
- )})${deferrable}`
160
- );
181
+ if (this.method !== 'create' && this.method !== 'createIfNot') {
182
+ this.pushQuery(
183
+ `alter table ${this.tableName()} add constraint ${constraintName} primary key (${this.formatter.columnize(
184
+ columns
185
+ )})${deferrable}`
186
+ );
187
+ }
161
188
  }
162
189
 
163
190
  unique(columns, indexName) {
@@ -20,17 +20,26 @@ const QueryBuilder = require('./query/sqlite-querybuilder');
20
20
  class Client_SQLite3 extends Client {
21
21
  constructor(config) {
22
22
  super(config);
23
+
24
+ if (config.connection && config.connection.filename === undefined) {
25
+ this.logger.warn(
26
+ 'Could not find `connection.filename` in config. Please specify ' +
27
+ 'the database path and name to avoid errors. ' +
28
+ '(see docs https://knexjs.org/guide/#configuration-options)'
29
+ );
30
+ }
31
+
23
32
  if (config.useNullAsDefault === undefined) {
24
33
  this.logger.warn(
25
34
  'sqlite does not support inserting default values. Set the ' +
26
35
  '`useNullAsDefault` flag to hide this warning. ' +
27
- '(see docs http://knexjs.org/#Builder-insert).'
36
+ '(see docs https://knexjs.org/guide/query-builder.html#insert).'
28
37
  );
29
38
  }
30
39
  }
31
40
 
32
41
  _driver() {
33
- return require('@vscode/sqlite3');
42
+ return require('sqlite3');
34
43
  }
35
44
 
36
45
  schemaCompiler() {
@@ -232,8 +241,6 @@ Object.assign(Client_SQLite3.prototype, {
232
241
  dialect: 'sqlite3',
233
242
 
234
243
  driverName: 'sqlite3',
235
- // SqlLite3 driver package name is different from driver name.
236
- aliasDriverName: '@vscode/sqlite3',
237
244
  });
238
245
 
239
246
  module.exports = Client_SQLite3;
@@ -76,7 +76,7 @@ class QueryCompiler_SQLite3 extends QueryCompiler {
76
76
  throw new TypeError(
77
77
  '`sqlite` does not support inserting default values. Specify ' +
78
78
  'values explicitly or use the `useNullAsDefault` config flag. ' +
79
- '(see docs http://knexjs.org/#Builder-insert).'
79
+ '(see docs https://knexjs.org/guide/query-builder.html#insert).'
80
80
  );
81
81
  });
82
82
  });
@@ -61,22 +61,15 @@ class Runner {
61
61
  Transform = Transform || require('stream').Transform;
62
62
 
63
63
  const queryContext = this.builder.queryContext();
64
- let queryStream;
65
64
 
66
65
  const stream = new Transform({
67
66
  objectMode: true,
68
67
  transform: (chunk, _, callback) => {
69
68
  callback(null, this.client.postProcessResponse(chunk, queryContext));
70
69
  },
71
- destroy() {
72
- // For some reason destroy is not available for mssql on Node 14. Might be a problem with tedious: https://github.com/tediousjs/tedious/issues/1139
73
- if (queryStream && queryStream.destroy) {
74
- queryStream.destroy(new Error('stream destroyed'));
75
- }
76
- },
77
70
  });
78
- stream.on('pipe', (qs) => {
79
- queryStream = qs;
71
+ stream.on('close', () => {
72
+ this.client.releaseConnection(this.connection);
80
73
  });
81
74
 
82
75
  const connectionAcquirePromise = this.ensureConnection(
@@ -5,6 +5,10 @@ const QueryInterface = require('../query/method-constants');
5
5
  const makeKnex = require('./make-knex');
6
6
  const { KnexTimeoutError } = require('../util/timeout');
7
7
  const { resolveConfig } = require('./internal/config-resolver');
8
+ const SchemaBuilder = require('../schema/builder');
9
+ const ViewBuilder = require('../schema/viewbuilder');
10
+ const ColumnBuilder = require('../schema/columnbuilder');
11
+ const TableBuilder = require('../schema/tablebuilder');
8
12
 
9
13
  function knex(config) {
10
14
  const { resolvedConfig, Dialect } = resolveConfig(...arguments);
@@ -28,4 +32,28 @@ knex.QueryBuilder = {
28
32
  },
29
33
  };
30
34
 
35
+ knex.SchemaBuilder = {
36
+ extend: function (methodName, fn) {
37
+ SchemaBuilder.extend(methodName, fn);
38
+ },
39
+ };
40
+
41
+ knex.ViewBuilder = {
42
+ extend: function (methodName, fn) {
43
+ ViewBuilder.extend(methodName, fn);
44
+ },
45
+ };
46
+
47
+ knex.ColumnBuilder = {
48
+ extend: function (methodName, fn) {
49
+ ColumnBuilder.extend(methodName, fn);
50
+ },
51
+ };
52
+
53
+ knex.TableBuilder = {
54
+ extend: function (methodName, fn) {
55
+ TableBuilder.extend(methodName, fn);
56
+ },
57
+ };
58
+
31
59
  module.exports = knex;
@@ -2,7 +2,7 @@ const Client = require('../../client');
2
2
  const { SUPPORTED_CLIENTS } = require('../../constants');
3
3
 
4
4
  const parseConnection = require('./parse-connection');
5
- const { resolveClientNameWithAliases } = require('../../util/helpers');
5
+ const { getDialectByNameOrAlias } = require('../../dialects');
6
6
 
7
7
  function resolveConfig(config) {
8
8
  let Dialect;
@@ -34,8 +34,7 @@ function resolveConfig(config) {
34
34
  );
35
35
  }
36
36
 
37
- const resolvedClientName = resolveClientNameWithAliases(clientName);
38
- Dialect = require(`../../dialects/${resolvedClientName}/index.js`);
37
+ Dialect = getDialectByNameOrAlias(clientName);
39
38
  }
40
39
 
41
40
  // If config connection parameter is passed as string, try to parse it
@@ -69,8 +69,10 @@ function connectionObject(parsed) {
69
69
  }
70
70
  if (parsed.searchParams) {
71
71
  for (const [key, value] of parsed.searchParams.entries()) {
72
- const isMySQLLike = ['mysql:', 'mariadb:'].includes(parsed.protocol);
73
- if (isMySQLLike) {
72
+ const isNestedConfigSupported = ['mysql:', 'mariadb:', 'mssql:'].includes(
73
+ parsed.protocol
74
+ );
75
+ if (isNestedConfigSupported) {
74
76
  try {
75
77
  connection[key] = JSON.parse(value);
76
78
  } catch (err) {
@@ -41,7 +41,7 @@ class MigrationGenerator {
41
41
 
42
42
  _getNewMigrationName(name) {
43
43
  if (name[0] === '-') name = name.slice(1);
44
- return yyyymmddhhmmss() + '_' + name + '.' + this.config.extension;
44
+ return yyyymmddhhmmss() + '_' + name + '.' + this.config.extension.split('-')[0];
45
45
  }
46
46
 
47
47
  _getNewMigrationPath(name) {
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @param { import("knex").Knex } knex
3
+ * @returns { Promise<void> }
4
+ */
5
+ exports.up = function({schema}) {
6
+ <% if (d.tableName) { %>
7
+ return schema.createTable("<%= d.tableName %>", function(t) {
8
+ t.increments();
9
+ t.timestamp();
10
+ });
11
+ <% } %>
12
+ };
13
+
14
+ /**
15
+ * @param { import("knex").Knex } knex
16
+ * @returns { Promise<void> }
17
+ */
18
+ exports.down = function({schema}) {
19
+ <% if (d.tableName) { %>
20
+ return schema.dropTable("<%= d.tableName %>");
21
+ <% } %>
22
+ };
@@ -0,0 +1,21 @@
1
+ import { Knex } from "knex";
2
+
3
+ <% if (d.tableName) { %>
4
+ export async function up({schema}: Knex): Promise<Knex.SchemaBuilder> {
5
+ return schema.createTable("<%= d.tableName %>", (t) => {
6
+ t.increments();
7
+ t.timestamps();
8
+ });
9
+ }
10
+ <% } else { %>
11
+ export async function up({schema}: Knex): Promise<void> {
12
+ }
13
+ <% } %>
14
+ <% if (d.tableName) { %>
15
+ export async function down({schema}: Knex): Promise<Knex.SchemaBuilder> {
16
+ return schema.dropTable("<%= d.tableName %>");
17
+ }
18
+ <% } else { %>
19
+ export async function down({schema}: Knex): Promise<void> {
20
+ }
21
+ <% } %>
@@ -1,5 +1,6 @@
1
1
  const { EventEmitter } = require('events');
2
2
  const toArray = require('lodash/toArray');
3
+ const assign = require('lodash/assign');
3
4
  const { addQueryContext } = require('../util/helpers');
4
5
  const saveAsyncStack = require('../util/save-async-stack');
5
6
  const {
@@ -96,6 +97,17 @@ class SchemaBuilder extends EventEmitter {
96
97
  };
97
98
  });
98
99
 
100
+
101
+ SchemaBuilder.extend = (methodName, fn) => {
102
+ if (Object.prototype.hasOwnProperty.call(SchemaBuilder.prototype, methodName)) {
103
+ throw new Error(
104
+ `Can't extend SchemaBuilder with existing method ('${methodName}').`
105
+ );
106
+ }
107
+
108
+ assign(SchemaBuilder.prototype, { [methodName]: fn });
109
+ };
110
+
99
111
  augmentWithBuilderInterface(SchemaBuilder);
100
112
  addQueryContext(SchemaBuilder);
101
113
 
@@ -1,4 +1,5 @@
1
1
  const extend = require('lodash/extend');
2
+ const assign = require('lodash/assign');
2
3
  const toArray = require('lodash/toArray');
3
4
  const { addQueryContext } = require('../util/helpers');
4
5
 
@@ -87,6 +88,17 @@ ColumnBuilder.prototype.notNull = ColumnBuilder.prototype.notNullable =
87
88
  };
88
89
  });
89
90
 
91
+
92
+ ColumnBuilder.extend = (methodName, fn) => {
93
+ if (Object.prototype.hasOwnProperty.call(ColumnBuilder.prototype, methodName)) {
94
+ throw new Error(
95
+ `Can't extend ColumnBuilder with existing method ('${methodName}').`
96
+ );
97
+ }
98
+
99
+ assign(ColumnBuilder.prototype, { [methodName]: fn });
100
+ };
101
+
90
102
  const AlterMethods = {};
91
103
 
92
104
  // Specify that the column is to be dropped. This takes precedence
@@ -277,8 +277,10 @@ ColumnCompiler.prototype.geography = 'geography';
277
277
  ColumnCompiler.prototype.point = 'point';
278
278
  ColumnCompiler.prototype.enu = 'varchar';
279
279
  ColumnCompiler.prototype.bit = ColumnCompiler.prototype.json = 'text';
280
- ColumnCompiler.prototype.uuid = ({ useBinaryUuid = false } = {}) =>
281
- useBinaryUuid ? 'binary(16)' : 'char(36)';
280
+ ColumnCompiler.prototype.uuid = ({
281
+ useBinaryUuid = false,
282
+ primaryKey = false,
283
+ } = {}) => (useBinaryUuid ? 'binary(16)' : 'char(36)');
282
284
  ColumnCompiler.prototype.integer =
283
285
  ColumnCompiler.prototype.smallint =
284
286
  ColumnCompiler.prototype.mediumint =
@@ -8,6 +8,7 @@
8
8
  // ------
9
9
  const each = require('lodash/each');
10
10
  const extend = require('lodash/extend');
11
+ const assign = require('lodash/assign');
11
12
  const toArray = require('lodash/toArray');
12
13
  const helpers = require('../util/helpers');
13
14
  const { isString, isFunction, isObject } = require('../util/is');
@@ -360,4 +361,15 @@ AlterMethods.dropColumn = AlterMethods.dropColumns = function () {
360
361
  return this;
361
362
  };
362
363
 
364
+
365
+ TableBuilder.extend = (methodName, fn) => {
366
+ if (Object.prototype.hasOwnProperty.call(TableBuilder.prototype, methodName)) {
367
+ throw new Error(
368
+ `Can't extend TableBuilder with existing method ('${methodName}').`
369
+ );
370
+ }
371
+
372
+ assign(TableBuilder.prototype, { [methodName]: fn });
373
+ };
374
+
363
375
  module.exports = TableBuilder;
@@ -1,5 +1,6 @@
1
1
  const helpers = require('../util/helpers');
2
2
  const extend = require('lodash/extend');
3
+ const assign = require('lodash/assign');
3
4
 
4
5
  class ViewBuilder {
5
6
  constructor(client, method, viewName, fn) {
@@ -78,4 +79,15 @@ const AlterMethods = {
78
79
 
79
80
  helpers.addQueryContext(ViewBuilder);
80
81
 
82
+
83
+ ViewBuilder.extend = (methodName, fn) => {
84
+ if (Object.prototype.hasOwnProperty.call(ViewBuilder.prototype, methodName)) {
85
+ throw new Error(
86
+ `Can't extend ViewBuilder with existing method ('${methodName}').`
87
+ );
88
+ }
89
+
90
+ assign(ViewBuilder.prototype, { [methodName]: fn });
91
+ };
92
+
81
93
  module.exports = ViewBuilder;