1 | ## Upgrading to new knex.js versions
|
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 |
|
7 | ### Upgrading to version 1.0.0+
|
8 |
|
9 | - Node.js older than 12 is no longer supported, make sure to update your environment;
|
10 | - If you are using `sqlite3` driver dependency, please replace it with `@vscode/sqlite3` in your `package.json`;
|
11 | - `RETURNING` operations now always return an object with column names;
|
12 | - Migrator now returns list of migrations as objects.
|
13 |
|
14 | ### Upgrading to version 0.95.0+
|
15 |
|
16 | - TypeScript type exports changed significantly. While `import Knex from 'knex';` used to import the knex instantiation function, the namespace and the interface for the knex instantiation function/object, there is now a clear distinction between them:
|
17 |
|
18 | ```typescript
|
19 | import { knex } from 'knex'; // this is a function that you call to instantiate knex
|
20 | import { Knex } from 'knex'; // this is a namespace, and a type of a knex object
|
21 | import KnexTimeoutError = Knex.KnexTimeoutError; // this is a class from the Knex namespace
|
22 |
|
23 | const config: Knex.Config = {}; // this is a type from the Knex namespace
|
24 | const knexInstance: Knex = knex(config);
|
25 | ```
|
26 |
|
27 | If your code looked like this:
|
28 |
|
29 | ```typescript
|
30 | import knex from 'knex';
|
31 |
|
32 | const config: knex.Config = {}; // this is a type from the Knex namespace
|
33 | const knexInstance = knex(config);
|
34 | ```
|
35 |
|
36 | Change it to
|
37 |
|
38 | ```typescript
|
39 | import { knex, Knex } from 'knex';
|
40 |
|
41 | const config: Knex.Config = {}; // this is a type from the Knex namespace
|
42 | const knexInstance = knex(config);
|
43 | ```
|
44 |
|
45 | - If you were importing types such as `Config` or `QueryBuilder` directly, use `Knex` namespace instead.
|
46 |
|
47 | So change this:
|
48 |
|
49 | ```ts
|
50 | import { QueryBuilder } from 'knex';
|
51 |
|
52 | const qb: QueryBuilder = knex('table').select('*');
|
53 | ```
|
54 |
|
55 | to this:
|
56 |
|
57 | ```ts
|
58 | import { Knex } from 'knex';
|
59 |
|
60 | const qb: Knex.QueryBuilder = knex('table').select('*');
|
61 | ```
|
62 |
|
63 | - IDE autocomplete may stop working if you are using JavaScript (not TypeScript). There are reports for autocomplete still working correctly if knex is used this way:
|
64 |
|
65 | ```js
|
66 | const knex = require('knex').knex({
|
67 | //connection parameters
|
68 | });
|
69 | ```
|
70 |
|
71 | It also works when using ESM imports:
|
72 |
|
73 | ```js
|
74 | import { knex } from 'knex';
|
75 |
|
76 | const kn = knex({
|
77 | //connection parameters
|
78 | });
|
79 | ```
|
80 |
|
81 | For usage as param it can be addressed like this:
|
82 |
|
83 | ```js
|
84 | /**
|
85 | * @param {import("knex").Knex} db
|
86 | */
|
87 | function up(db) {
|
88 | // Your code
|
89 | }
|
90 | ```
|
91 |
|
92 | - Syntax for QueryBuilder augmentation changed. Previously it looked like this:
|
93 |
|
94 | ```ts
|
95 | declare module 'knex' {
|
96 | interface QueryBuilder {
|
97 | paginate<TResult = any[]>(
|
98 | params: IPaginateParams
|
99 | ): KnexQB<any, IWithPagination<TResult>>;
|
100 | }
|
101 | }
|
102 | ```
|
103 |
|
104 | This should be changed into this:
|
105 |
|
106 | ```ts
|
107 | declare module 'knex' {
|
108 | namespace Knex {
|
109 | interface QueryBuilder {
|
110 | paginate<TResult = any[]>(
|
111 | params: IPaginateParams
|
112 | ): KnexQB<any, IWithPagination<TResult>>;
|
113 | }
|
114 | }
|
115 | }
|
116 | ```
|
117 |
|
118 | - TypeScript version 4.1+ is needed when using knex types now.
|
119 |
|
120 | - MSSQL driver was completely reworked in order to address the multitude of connection pool, error handling and performance issues. Since the new implementation uses `tedious` library directly instead of `mssql`, please replace `mssql` with `tedious` in your dependencies if you are using a MSSQL database.
|
121 |
|
122 | - Transaction rollback does not trigger a promise rejection for transactions with specified handler. If you want to preserve previous behavior, pass `config` object with `doNotRejectOnRollback: false`:
|
123 |
|
124 | ```js
|
125 | await knex.transaction(
|
126 | async (trx) => {
|
127 | const ids = await trx('catalogues').insert({ name: 'Old Books' }, 'id');
|
128 | },
|
129 | { doNotRejectOnRollback: false }
|
130 | );
|
131 | ```
|
132 |
|
133 | - Connection url parsing changed from legacy [url.parse](https://nodejs.org/docs/latest-v10.x/api/url.html#url_legacy_url_api) to [WHATWG URL](https://nodejs.org/docs/latest-v10.x/api/url.html#url_the_whatwg_url_api). If you have symbols, unusual for a URL (not A-z, not digits, not dot, not dash) - check [Node.js docs](https://nodejs.org/docs/latest-v10.x/api/url.html#url_percent_encoding_in_urls) for details
|
134 |
|
135 | - Global static `Knex.raw` support dropped, use instance `knex.raw` instead. (`require('knex').raw()` won't work anymore)
|
136 |
|
137 | - v8 flags are no longer supported in cli. To pass these flags use [`NODE_OPTIONS` environment variable](https://nodejs.org/api/cli.html#cli_node_options_options).
|
138 | For example `NODE_OPTIONS="--max-old-space-size=1536" npm run knex`
|
139 |
|
140 | - Clients are now classes instead of new-able functions. Please migrate your custom clients to classes.
|
141 |
|
142 | ```js
|
143 | const Client = require('knex');
|
144 | const { inherits } = require('util');
|
145 |
|
146 | // old
|
147 | function CustomClient(config) {
|
148 | Client.call(this, config);
|
149 | // construction logic
|
150 | }
|
151 | inherits(CustomClient, Client);
|
152 | CustomClient.prototype.methodOverride = function () {
|
153 | // logic
|
154 | };
|
155 |
|
156 | // new
|
157 | class CustomClient extends Client {
|
158 | // node 12+
|
159 | driverName = 'abcd';
|
160 | constructor(config) {
|
161 | super(config);
|
162 | this.driverName = 'abcd'; // bad way, will not work
|
163 | // construction logic
|
164 | }
|
165 | methodOverride() {
|
166 | // logic
|
167 | }
|
168 | }
|
169 | // alternative to declare driverName
|
170 | CustomClient.prototype.driverName = 'abcd';
|
171 | ```
|
172 |
|
173 | - There was a major internal restructuring and renaming effort. Most dialect-specific compilers/builder have dialect name as a prefix now. Also some files were moved. Make sure to make adjustments accordingly if you were referencing specific knex library files directly from your code.
|
174 |
|
175 | - "first" and "pluck" can no longer be both chained on the same operation. Previously only the last one chained was used, now this would throw an error.
|
176 |
|
177 | - Trying to execute an operation resulting in an empty query such as inserting an empty array, will now throw an error on all database drivers.
|
178 |
|
179 | ### Upgrading to version 0.21.0+
|
180 |
|
181 | - Node.js older than 10 is no longer supported, make sure to update your environment;
|
182 |
|
183 | ### Upgrading to version 0.19.0+
|
184 |
|
185 | - Passing unknown properties to connection pool configuration now throws errors (see https://github.com/Vincit/tarn.js/issues/19 for details);
|
186 | - `beforeDestroy` pool configuration option was removed. You should use tarn.js event handlers if you still need similar functionality.
|
187 |
|
188 | ### Upgrading to version 0.18.0+
|
189 |
|
190 | - Node.js older than 8 is no longer supported, make sure to update your environment;
|
191 | - Knex returns native promises instead of bluebird ones now. You will need to update your code not to rely on bluebird-specific functionality;
|
192 | - Knex.Promise was removed, use native promises;
|
193 | - Promise is no longer passed to migrations and seeds, use native one;
|
194 | - If you are using TypeScript, make sure to include 'es6' in compilerOptions.lib, otherwise you may get errors for methods `.catch()` and `then()` not being recognized.
|
195 |
|
196 | ### Upgrading to version 0.17.0+
|
197 |
|
198 | - Generic support was implemented for TypeScript bindings, which may break TS builds in some edge cases. Please refer to https://knexjs.org/#typescript-support for more elaborate documentation.
|
199 |
|
200 | ### Upgrading to version 0.16.0+
|
201 |
|
202 | - MSSQL: DB versions older than 2008 are no longer supported, make sure to update your DB;
|
203 | - PostgreSQL|MySQL: it is recommended to use options object for `table.datetime` and `table.timestamp` methods instead of argument options. See documentation for these methods for more details;
|
204 | - Node 6: There are known issues with duplicate event listeners when using knex.js with Node.js 6 (resulting in MaxListenersExceededWarning under certain use-cases (such as reusing single knex instance to run migrations or seeds multiple times)). Please upgrade to Node.js 8+ as soon as possible (knex 0.17.0 will be dropping Node.js 6 support altogether);
|
205 |
|
206 | ### Upgrading to version 0.15.0+
|
207 |
|
208 | - Node.js older than 6 is no longer supported, make sure to update your environment;
|
209 |
|
210 | - MSSQL: Creating a unique index on the table targeted by stored procedures that were created with QUOTED_IDENTIFIER = OFF fails.
|
211 |
|
212 | You can use this query to identify all affected stored procedures:
|
213 |
|
214 | ```
|
215 | SELECT name = OBJECT_NAME([object_id]), uses_quoted_identifier
|
216 | FROM sys.sql_modules
|
217 | WHERE uses_quoted_identifier = 0;
|
218 | ```
|
219 |
|
220 | The only known solution is to recreate all stored procedures with QUOTED_IDENTIFIER = OFF
|
221 |
|
222 | - MariaDB: `mariadb` dialect is no longer supported;
|
223 |
|
224 | Instead, use "mysql" or "mysql2" dialects.
|
225 |
|
226 | ### Upgrading to version 0.14.4+
|
227 |
|
228 | - Including schema in tableName parameter in migrations no longer works, so this is invalid:
|
229 |
|
230 | ```js
|
231 | await knex.migrate.latest({
|
232 | directory: 'src/services/orders/database/migrations',
|
233 | tableName: 'orders.orders_migrations',
|
234 | });
|
235 | ```
|
236 |
|
237 | Instead, starting from 0.14.5 you should use new parameter schemaName:
|
238 |
|
239 | ```js
|
240 | await knex.migrate.latest({
|
241 | directory: 'src/services/orders/database/migrations',
|
242 | tableName: 'orders_migrations',
|
243 | schemaName: 'orders',
|
244 | });
|
245 | ```
|