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
package/lib/runner.js CHANGED
@@ -1,285 +1,285 @@
1
- const { KnexTimeoutError } = require('./util/timeout');
2
- const { timeout } = require('./util/timeout');
3
-
4
- let Transform;
5
-
6
- // The "Runner" constructor takes a "builder" (query, schema, or raw)
7
- // and runs through each of the query statements, calling any additional
8
- // "output" method provided alongside the query and bindings.
9
- function Runner(client, builder) {
10
- this.client = client;
11
- this.builder = builder;
12
- this.queries = [];
13
-
14
- // The "connection" object is set on the runner when
15
- // "run" is called.
16
- this.connection = void 0;
17
- }
18
-
19
- Object.assign(Runner.prototype, {
20
- // "Run" the target, calling "toSQL" on the builder, returning
21
- // an object or array of queries to run, each of which are run on
22
- // a single connection.
23
- run() {
24
- const runner = this;
25
- return (
26
- this.ensureConnection(function (connection) {
27
- runner.connection = connection;
28
-
29
- runner.client.emit('start', runner.builder);
30
- runner.builder.emit('start', runner.builder);
31
- const sql = runner.builder.toSQL();
32
-
33
- if (runner.builder._debug) {
34
- runner.client.logger.debug(sql);
35
- }
36
-
37
- if (Array.isArray(sql)) {
38
- return runner.queryArray(sql);
39
- }
40
- return runner.query(sql);
41
- })
42
-
43
- // If there are any "error" listeners, we fire an error event
44
- // and then re-throw the error to be eventually handled by
45
- // the promise chain. Useful if you're wrapping in a custom `Promise`.
46
- .catch(function (err) {
47
- if (runner.builder._events && runner.builder._events.error) {
48
- runner.builder.emit('error', err);
49
- }
50
- throw err;
51
- })
52
-
53
- // Fire a single "end" event on the builder when
54
- // all queries have successfully completed.
55
- .then(function (res) {
56
- runner.builder.emit('end');
57
- return res;
58
- })
59
- );
60
- },
61
-
62
- // Stream the result set, by passing through to the dialect's streaming
63
- // capabilities. If the options are
64
- stream(options, handler) {
65
- // If we specify stream(handler).then(...
66
- if (arguments.length === 1) {
67
- if (typeof options === 'function') {
68
- handler = options;
69
- options = {};
70
- }
71
- }
72
-
73
- // Determines whether we emit an error or throw here.
74
- const hasHandler = typeof handler === 'function';
75
-
76
- // Lazy-load the "Transform" dependency.
77
- Transform = Transform || require('stream').Transform;
78
-
79
- const runner = this;
80
- const queryContext = this.builder.queryContext();
81
- let queryStream;
82
-
83
- const stream = new Transform({
84
- objectMode: true,
85
- transform: (chunk, _, callback) => {
86
- callback(null, this.client.postProcessResponse(chunk, queryContext));
87
- },
88
- destroy() {
89
- // 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
90
- if (queryStream && queryStream.destroy) {
91
- queryStream.destroy(new Error('stream destroyed'));
92
- }
93
- },
94
- });
95
- stream.on('pipe', (qs) => {
96
- queryStream = qs;
97
- });
98
-
99
- let hasConnection = false;
100
- const promise = this.ensureConnection(function (connection) {
101
- hasConnection = true;
102
- runner.connection = connection;
103
- try {
104
- const sql = runner.builder.toSQL();
105
-
106
- if (Array.isArray(sql) && hasHandler) {
107
- throw new Error(
108
- 'The stream may only be used with a single query statement.'
109
- );
110
- }
111
-
112
- return runner.client.stream(runner.connection, sql, stream, options);
113
- } catch (e) {
114
- stream.emit('error', e);
115
- throw e;
116
- }
117
- });
118
-
119
- // If a function is passed to handle the stream, send the stream
120
- // there and return the promise, otherwise just return the stream
121
- // and the promise will take care of itself.
122
- if (hasHandler) {
123
- handler(stream);
124
- return promise;
125
- }
126
-
127
- // Emit errors on the stream if the error occurred before a connection
128
- // could be acquired.
129
- // If the connection was acquired, assume the error occurred in the client
130
- // code and has already been emitted on the stream. Don't emit it twice.
131
- promise.catch(function (err) {
132
- if (!hasConnection) stream.emit('error', err);
133
- });
134
- return stream;
135
- },
136
-
137
- // Allow you to pipe the stream to a writable stream.
138
- pipe(writable, options) {
139
- return this.stream(options).pipe(writable);
140
- },
141
-
142
- // "Runs" a query, returning a promise. All queries specified by the builder are guaranteed
143
- // to run in sequence, and on the same connection, especially helpful when schema building
144
- // and dealing with foreign key constraints, etc.
145
- query: async function (obj) {
146
- const { __knexUid, __knexTxId } = this.connection;
147
-
148
- this.builder.emit('query', Object.assign({ __knexUid, __knexTxId }, obj));
149
-
150
- const runner = this;
151
- let queryPromise = this.client.query(this.connection, obj);
152
-
153
- if (obj.timeout) {
154
- queryPromise = timeout(queryPromise, obj.timeout);
155
- }
156
-
157
- // Await the return value of client.processResponse; in the case of sqlite3's
158
- // dropColumn()/renameColumn(), it will be a Promise for the transaction
159
- // containing the complete rename procedure.
160
- return queryPromise
161
- .then((resp) => this.client.processResponse(resp, runner))
162
- .then((processedResponse) => {
163
- const queryContext = this.builder.queryContext();
164
- const postProcessedResponse = this.client.postProcessResponse(
165
- processedResponse,
166
- queryContext
167
- );
168
-
169
- this.builder.emit(
170
- 'query-response',
171
- postProcessedResponse,
172
- Object.assign({ __knexUid, __knexTxId }, obj),
173
- this.builder
174
- );
175
-
176
- this.client.emit(
177
- 'query-response',
178
- postProcessedResponse,
179
- Object.assign({ __knexUid, __knexTxId }, obj),
180
- this.builder
181
- );
182
-
183
- return postProcessedResponse;
184
- })
185
- .catch((error) => {
186
- if (!(error instanceof KnexTimeoutError)) {
187
- return Promise.reject(error);
188
- }
189
- const { timeout, sql, bindings } = obj;
190
-
191
- let cancelQuery;
192
- if (obj.cancelOnTimeout) {
193
- cancelQuery = this.client.cancelQuery(this.connection);
194
- } else {
195
- // If we don't cancel the query, we need to mark the connection as disposed so that
196
- // it gets destroyed by the pool and is never used again. If we don't do this and
197
- // return the connection to the pool, it will be useless until the current operation
198
- // that timed out, finally finishes.
199
- this.connection.__knex__disposed = error;
200
- cancelQuery = Promise.resolve();
201
- }
202
-
203
- return cancelQuery
204
- .catch((cancelError) => {
205
- // If the cancellation failed, we need to mark the connection as disposed so that
206
- // it gets destroyed by the pool and is never used again. If we don't do this and
207
- // return the connection to the pool, it will be useless until the current operation
208
- // that timed out, finally finishes.
209
- this.connection.__knex__disposed = error;
210
-
211
- // cancellation failed
212
- throw Object.assign(cancelError, {
213
- message: `After query timeout of ${timeout}ms exceeded, cancelling of query failed.`,
214
- sql,
215
- bindings,
216
- timeout,
217
- });
218
- })
219
- .then(() => {
220
- // cancellation succeeded, rethrow timeout error
221
- throw Object.assign(error, {
222
- message: `Defined query timeout of ${timeout}ms exceeded when running query.`,
223
- sql,
224
- bindings,
225
- timeout,
226
- });
227
- });
228
- })
229
- .catch((error) => {
230
- this.builder.emit(
231
- 'query-error',
232
- error,
233
- Object.assign({ __knexUid, __knexTxId }, obj)
234
- );
235
- throw error;
236
- });
237
- },
238
-
239
- // In the case of the "schema builder" we call `queryArray`, which runs each
240
- // of the queries in sequence.
241
- async queryArray(queries) {
242
- if (queries.length === 1) {
243
- return this.query(queries[0]);
244
- }
245
-
246
- const results = [];
247
- for (const query of queries) {
248
- results.push(await this.query(query));
249
- }
250
- return results;
251
- },
252
-
253
- // Check whether there's a transaction flag, and that it has a connection.
254
- async ensureConnection(cb) {
255
- // Use override from a builder if passed
256
- if (this.builder._connection) {
257
- return cb(this.builder._connection);
258
- }
259
-
260
- if (this.connection) {
261
- return cb(this.connection);
262
- }
263
- return this.client
264
- .acquireConnection()
265
- .catch((error) => {
266
- if (!(error instanceof KnexTimeoutError)) {
267
- return Promise.reject(error);
268
- }
269
- if (this.builder) {
270
- error.sql = this.builder.sql;
271
- error.bindings = this.builder.bindings;
272
- }
273
- throw error;
274
- })
275
- .then(async (connection) => {
276
- try {
277
- return await cb(connection);
278
- } finally {
279
- await this.client.releaseConnection(this.connection);
280
- }
281
- });
282
- },
283
- });
284
-
285
- module.exports = Runner;
1
+ const { KnexTimeoutError } = require('./util/timeout');
2
+ const { timeout } = require('./util/timeout');
3
+
4
+ let Transform;
5
+
6
+ // The "Runner" constructor takes a "builder" (query, schema, or raw)
7
+ // and runs through each of the query statements, calling any additional
8
+ // "output" method provided alongside the query and bindings.
9
+ function Runner(client, builder) {
10
+ this.client = client;
11
+ this.builder = builder;
12
+ this.queries = [];
13
+
14
+ // The "connection" object is set on the runner when
15
+ // "run" is called.
16
+ this.connection = void 0;
17
+ }
18
+
19
+ Object.assign(Runner.prototype, {
20
+ // "Run" the target, calling "toSQL" on the builder, returning
21
+ // an object or array of queries to run, each of which are run on
22
+ // a single connection.
23
+ run() {
24
+ const runner = this;
25
+ return (
26
+ this.ensureConnection(function (connection) {
27
+ runner.connection = connection;
28
+
29
+ runner.client.emit('start', runner.builder);
30
+ runner.builder.emit('start', runner.builder);
31
+ const sql = runner.builder.toSQL();
32
+
33
+ if (runner.builder._debug) {
34
+ runner.client.logger.debug(sql);
35
+ }
36
+
37
+ if (Array.isArray(sql)) {
38
+ return runner.queryArray(sql);
39
+ }
40
+ return runner.query(sql);
41
+ })
42
+
43
+ // If there are any "error" listeners, we fire an error event
44
+ // and then re-throw the error to be eventually handled by
45
+ // the promise chain. Useful if you're wrapping in a custom `Promise`.
46
+ .catch(function (err) {
47
+ if (runner.builder._events && runner.builder._events.error) {
48
+ runner.builder.emit('error', err);
49
+ }
50
+ throw err;
51
+ })
52
+
53
+ // Fire a single "end" event on the builder when
54
+ // all queries have successfully completed.
55
+ .then(function (res) {
56
+ runner.builder.emit('end');
57
+ return res;
58
+ })
59
+ );
60
+ },
61
+
62
+ // Stream the result set, by passing through to the dialect's streaming
63
+ // capabilities. If the options are
64
+ stream(options, handler) {
65
+ // If we specify stream(handler).then(...
66
+ if (arguments.length === 1) {
67
+ if (typeof options === 'function') {
68
+ handler = options;
69
+ options = {};
70
+ }
71
+ }
72
+
73
+ // Determines whether we emit an error or throw here.
74
+ const hasHandler = typeof handler === 'function';
75
+
76
+ // Lazy-load the "Transform" dependency.
77
+ Transform = Transform || require('stream').Transform;
78
+
79
+ const runner = this;
80
+ const queryContext = this.builder.queryContext();
81
+ let queryStream;
82
+
83
+ const stream = new Transform({
84
+ objectMode: true,
85
+ transform: (chunk, _, callback) => {
86
+ callback(null, this.client.postProcessResponse(chunk, queryContext));
87
+ },
88
+ destroy() {
89
+ // 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
90
+ if (queryStream && queryStream.destroy) {
91
+ queryStream.destroy(new Error('stream destroyed'));
92
+ }
93
+ },
94
+ });
95
+ stream.on('pipe', (qs) => {
96
+ queryStream = qs;
97
+ });
98
+
99
+ let hasConnection = false;
100
+ const promise = this.ensureConnection(function (connection) {
101
+ hasConnection = true;
102
+ runner.connection = connection;
103
+ try {
104
+ const sql = runner.builder.toSQL();
105
+
106
+ if (Array.isArray(sql) && hasHandler) {
107
+ throw new Error(
108
+ 'The stream may only be used with a single query statement.'
109
+ );
110
+ }
111
+
112
+ return runner.client.stream(runner.connection, sql, stream, options);
113
+ } catch (e) {
114
+ stream.emit('error', e);
115
+ throw e;
116
+ }
117
+ });
118
+
119
+ // If a function is passed to handle the stream, send the stream
120
+ // there and return the promise, otherwise just return the stream
121
+ // and the promise will take care of itself.
122
+ if (hasHandler) {
123
+ handler(stream);
124
+ return promise;
125
+ }
126
+
127
+ // Emit errors on the stream if the error occurred before a connection
128
+ // could be acquired.
129
+ // If the connection was acquired, assume the error occurred in the client
130
+ // code and has already been emitted on the stream. Don't emit it twice.
131
+ promise.catch(function (err) {
132
+ if (!hasConnection) stream.emit('error', err);
133
+ });
134
+ return stream;
135
+ },
136
+
137
+ // Allow you to pipe the stream to a writable stream.
138
+ pipe(writable, options) {
139
+ return this.stream(options).pipe(writable);
140
+ },
141
+
142
+ // "Runs" a query, returning a promise. All queries specified by the builder are guaranteed
143
+ // to run in sequence, and on the same connection, especially helpful when schema building
144
+ // and dealing with foreign key constraints, etc.
145
+ query: async function (obj) {
146
+ const { __knexUid, __knexTxId } = this.connection;
147
+
148
+ this.builder.emit('query', Object.assign({ __knexUid, __knexTxId }, obj));
149
+
150
+ const runner = this;
151
+ let queryPromise = this.client.query(this.connection, obj);
152
+
153
+ if (obj.timeout) {
154
+ queryPromise = timeout(queryPromise, obj.timeout);
155
+ }
156
+
157
+ // Await the return value of client.processResponse; in the case of sqlite3's
158
+ // dropColumn()/renameColumn(), it will be a Promise for the transaction
159
+ // containing the complete rename procedure.
160
+ return queryPromise
161
+ .then((resp) => this.client.processResponse(resp, runner))
162
+ .then((processedResponse) => {
163
+ const queryContext = this.builder.queryContext();
164
+ const postProcessedResponse = this.client.postProcessResponse(
165
+ processedResponse,
166
+ queryContext
167
+ );
168
+
169
+ this.builder.emit(
170
+ 'query-response',
171
+ postProcessedResponse,
172
+ Object.assign({ __knexUid, __knexTxId }, obj),
173
+ this.builder
174
+ );
175
+
176
+ this.client.emit(
177
+ 'query-response',
178
+ postProcessedResponse,
179
+ Object.assign({ __knexUid, __knexTxId }, obj),
180
+ this.builder
181
+ );
182
+
183
+ return postProcessedResponse;
184
+ })
185
+ .catch((error) => {
186
+ if (!(error instanceof KnexTimeoutError)) {
187
+ return Promise.reject(error);
188
+ }
189
+ const { timeout, sql, bindings } = obj;
190
+
191
+ let cancelQuery;
192
+ if (obj.cancelOnTimeout) {
193
+ cancelQuery = this.client.cancelQuery(this.connection);
194
+ } else {
195
+ // If we don't cancel the query, we need to mark the connection as disposed so that
196
+ // it gets destroyed by the pool and is never used again. If we don't do this and
197
+ // return the connection to the pool, it will be useless until the current operation
198
+ // that timed out, finally finishes.
199
+ this.connection.__knex__disposed = error;
200
+ cancelQuery = Promise.resolve();
201
+ }
202
+
203
+ return cancelQuery
204
+ .catch((cancelError) => {
205
+ // If the cancellation failed, we need to mark the connection as disposed so that
206
+ // it gets destroyed by the pool and is never used again. If we don't do this and
207
+ // return the connection to the pool, it will be useless until the current operation
208
+ // that timed out, finally finishes.
209
+ this.connection.__knex__disposed = error;
210
+
211
+ // cancellation failed
212
+ throw Object.assign(cancelError, {
213
+ message: `After query timeout of ${timeout}ms exceeded, cancelling of query failed.`,
214
+ sql,
215
+ bindings,
216
+ timeout,
217
+ });
218
+ })
219
+ .then(() => {
220
+ // cancellation succeeded, rethrow timeout error
221
+ throw Object.assign(error, {
222
+ message: `Defined query timeout of ${timeout}ms exceeded when running query.`,
223
+ sql,
224
+ bindings,
225
+ timeout,
226
+ });
227
+ });
228
+ })
229
+ .catch((error) => {
230
+ this.builder.emit(
231
+ 'query-error',
232
+ error,
233
+ Object.assign({ __knexUid, __knexTxId }, obj)
234
+ );
235
+ throw error;
236
+ });
237
+ },
238
+
239
+ // In the case of the "schema builder" we call `queryArray`, which runs each
240
+ // of the queries in sequence.
241
+ async queryArray(queries) {
242
+ if (queries.length === 1) {
243
+ return this.query(queries[0]);
244
+ }
245
+
246
+ const results = [];
247
+ for (const query of queries) {
248
+ results.push(await this.query(query));
249
+ }
250
+ return results;
251
+ },
252
+
253
+ // Check whether there's a transaction flag, and that it has a connection.
254
+ async ensureConnection(cb) {
255
+ // Use override from a builder if passed
256
+ if (this.builder._connection) {
257
+ return cb(this.builder._connection);
258
+ }
259
+
260
+ if (this.connection) {
261
+ return cb(this.connection);
262
+ }
263
+ return this.client
264
+ .acquireConnection()
265
+ .catch((error) => {
266
+ if (!(error instanceof KnexTimeoutError)) {
267
+ return Promise.reject(error);
268
+ }
269
+ if (this.builder) {
270
+ error.sql = this.builder.sql;
271
+ error.bindings = this.builder.bindings;
272
+ }
273
+ throw error;
274
+ })
275
+ .then(async (connection) => {
276
+ try {
277
+ return await cb(connection);
278
+ } finally {
279
+ await this.client.releaseConnection(this.connection);
280
+ }
281
+ });
282
+ },
283
+ });
284
+
285
+ module.exports = Runner;