knex 2.1.0 → 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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Master (Unreleased)
2
2
 
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
+
3
20
  # 2.1.0 - 26 May, 2022
4
21
 
5
22
  ### New features:
package/bin/cli.js CHANGED
File without changes
@@ -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
  }
@@ -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 (
@@ -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,11 +20,20 @@ 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
  }
@@ -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(
@@ -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) {
@@ -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 =
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knex",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "A batteries-included SQL query & schema builder for PostgresSQL, MySQL, CockroachDB, MSSQL and SQLite3",
5
5
  "main": "knex",
6
6
  "types": "types/index.d.ts",
@@ -60,7 +60,7 @@
60
60
  "prepublishOnly": "npm run clean && npm run build"
61
61
  },
62
62
  "dependencies": {
63
- "colorette": "2.0.16",
63
+ "colorette": "2.0.19",
64
64
  "commander": "^9.1.0",
65
65
  "debug": "4.3.4",
66
66
  "escalade": "^3.1.1",
@@ -105,7 +105,7 @@
105
105
  },
106
106
  "devDependencies": {
107
107
  "@tsconfig/recommended": "^1.0.1",
108
- "@types/node": "^17.0.25",
108
+ "@types/node": "^18.0.4",
109
109
  "better-sqlite3": "^7.5.1",
110
110
  "chai": "^4.3.6",
111
111
  "chai-as-promised": "^7.1.1",
@@ -121,7 +121,7 @@
121
121
  "husky": "^8.0.1",
122
122
  "jake": "^10.8.5",
123
123
  "JSONStream": "^1.3.5",
124
- "lint-staged": "^12.3.7",
124
+ "lint-staged": "^13.0.0",
125
125
  "mocha": "^10.0.0",
126
126
  "mock-fs": "^5.1.2",
127
127
  "mysql": "^2.18.1",
@@ -141,8 +141,8 @@
141
141
  "tedious": "^14.4.0",
142
142
  "toxiproxy-node-client": "^2.0.6",
143
143
  "ts-node": "^10.7.0",
144
- "tsd": "^0.20.0",
145
- "typescript": "4.7.2"
144
+ "tsd": "^0.22.0",
145
+ "typescript": "4.7.4"
146
146
  },
147
147
  "buildDependencies": [
148
148
  "rimraf"