@balena/pinejs 15.0.0-delete-state-default-user-permissions-981931563dc47b2a8b873bc787d7dacfcc6c52e3 → 15.0.0-deprecate-node12-8a99d72ae66d7708293afc56c5d7eb19b39081cd

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. package/.resinci.yml +0 -1
  2. package/.versionbot/CHANGELOG.yml +226 -8
  3. package/CHANGELOG.md +85 -1
  4. package/out/bin/utils.js +1 -1
  5. package/out/bin/utils.js.map +1 -1
  6. package/out/config-loader/config-loader.d.ts +3 -5
  7. package/out/config-loader/config-loader.js +35 -31
  8. package/out/config-loader/config-loader.js.map +1 -1
  9. package/out/data-server/sbvr-server.js +8 -8
  10. package/out/data-server/sbvr-server.js.map +1 -1
  11. package/out/database-layer/db.d.ts +1 -1
  12. package/out/database-layer/db.js +3 -3
  13. package/out/database-layer/db.js.map +1 -1
  14. package/out/express-emulator/express.js +1 -1
  15. package/out/express-emulator/express.js.map +1 -1
  16. package/out/http-transactions/transactions.js +4 -4
  17. package/out/http-transactions/transactions.js.map +1 -1
  18. package/out/migrator/sync.d.ts +9 -0
  19. package/out/migrator/sync.js +121 -0
  20. package/out/migrator/sync.js.map +1 -0
  21. package/out/migrator/utils.d.ts +28 -0
  22. package/out/migrator/utils.js +104 -0
  23. package/out/migrator/utils.js.map +1 -0
  24. package/out/odata-metadata/odata-metadata-generator.js +6 -9
  25. package/out/odata-metadata/odata-metadata-generator.js.map +1 -1
  26. package/out/passport-pinejs/passport-pinejs.js +4 -3
  27. package/out/passport-pinejs/passport-pinejs.js.map +1 -1
  28. package/out/pinejs-session-store/pinejs-session-store.js +1 -1
  29. package/out/pinejs-session-store/pinejs-session-store.js.map +1 -1
  30. package/out/sbvr-api/abstract-sql.d.ts +1 -1
  31. package/out/sbvr-api/abstract-sql.js.map +1 -1
  32. package/out/sbvr-api/control-flow.js.map +1 -1
  33. package/out/sbvr-api/hooks.d.ts +6 -3
  34. package/out/sbvr-api/hooks.js +3 -3
  35. package/out/sbvr-api/hooks.js.map +1 -1
  36. package/out/sbvr-api/odata-response.js +5 -5
  37. package/out/sbvr-api/odata-response.js.map +1 -1
  38. package/out/sbvr-api/permissions.js +25 -20
  39. package/out/sbvr-api/permissions.js.map +1 -1
  40. package/out/sbvr-api/sbvr-utils.d.ts +4 -3
  41. package/out/sbvr-api/sbvr-utils.js +71 -48
  42. package/out/sbvr-api/sbvr-utils.js.map +1 -1
  43. package/out/sbvr-api/uri-parser.d.ts +13 -11
  44. package/out/sbvr-api/uri-parser.js +4 -4
  45. package/out/sbvr-api/uri-parser.js.map +1 -1
  46. package/out/server-glue/module.d.ts +2 -2
  47. package/out/server-glue/module.js +2 -1
  48. package/out/server-glue/module.js.map +1 -1
  49. package/package.json +19 -19
  50. package/src/bin/utils.ts +1 -1
  51. package/src/config-loader/config-loader.ts +69 -44
  52. package/src/data-server/sbvr-server.js +8 -8
  53. package/src/database-layer/db.ts +11 -11
  54. package/src/express-emulator/express.js +1 -1
  55. package/src/http-transactions/transactions.js +4 -4
  56. package/src/migrator/sync.ts +169 -0
  57. package/src/migrator/utils.ts +154 -0
  58. package/src/odata-metadata/odata-metadata-generator.ts +8 -11
  59. package/src/passport-pinejs/passport-pinejs.ts +3 -2
  60. package/src/sbvr-api/abstract-sql.ts +6 -3
  61. package/src/sbvr-api/control-flow.ts +2 -2
  62. package/src/sbvr-api/hooks.ts +18 -8
  63. package/src/sbvr-api/odata-response.ts +4 -4
  64. package/src/sbvr-api/permissions.ts +42 -36
  65. package/src/sbvr-api/sbvr-utils.ts +121 -58
  66. package/src/sbvr-api/uri-parser.ts +29 -21
  67. package/src/server-glue/module.ts +4 -3
  68. package/tsconfig.json +1 -3
  69. package/typings/lf-to-abstract-sql.d.ts +6 -9
  70. package/out/migrator/migrator.d.ts +0 -17
  71. package/out/migrator/migrator.js +0 -185
  72. package/out/migrator/migrator.js.map +0 -1
  73. package/src/migrator/migrator.ts +0 -278
@@ -1 +1 @@
1
- {"version":3,"file":"module.js","sourceRoot":"","sources":["../../src/server-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C,CAAC;QAE5C,MAAM,QAAQ,GAAyB,EAAE,CAAC;QAC1C,IAAI,OAAO,CAAC,GAAG,CAAC,mBAAmB,EAAE;YACpC,MAAM,UAAU,GAAG,2CAAa,4BAA4B,EAAC,CAAC;YAC9D,MAAM,YAAY,GAAG,OAAO,CAAC,mCAAmC,CAAC,CAAC;YAClE,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;YACvD,QAAQ,CAAC,IAAI,CACZ,SAAS;iBACP,UAAU,CAAC,YAAY,CAAC,MAAM,CAAC;iBAC/B,IAAI,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAChD,CAAC;SACF;QACD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,sBAAsB,EAAE;YACxC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,qBAAqB,CAAC,MAAM,CAAC,CAAC,CAAC;SACvD;QACD,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAE5B,OAAO,SAAS,CAAC;KACjB;IAAC,OAAO,GAAQ,EAAE;QAClB,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,GAAG,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC;QAC3D,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KAChB;AACF,CAAC,CAAC;AAlCW,QAAA,IAAI,QAkCf"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@balena/pinejs",
3
- "version": "15.0.0-delete-state-default-user-permissions-981931563dc47b2a8b873bc787d7dacfcc6c52e3",
3
+ "version": "15.0.0-deprecate-node12-8a99d72ae66d7708293afc56c5d7eb19b39081cd",
4
4
  "main": "out/server-glue/module",
5
5
  "repository": "git@github.com:balena-io/pinejs.git",
6
6
  "license": "Apache-2.0",
@@ -21,7 +21,7 @@
21
21
  "lint": "balena-lint -e js -e ts src build typings Gruntfile.ts && npx tsc --project tsconfig.dev.json --noEmit",
22
22
  "test": "npm run lint & npm run build && npm run webpack-build",
23
23
  "test:compose": "npm run compose:build && npm run compose:postgres && docker-compose -f docker-compose.test.yml run sut npm run mocha",
24
- "test:fast": " npm run compose:postgres && npm run mocha:local",
24
+ "test:fast": "npm run compose:postgres && npm run mocha:local",
25
25
  "mocha": "TS_NODE_FILES=true mocha",
26
26
  "mocha:local": "DATABASE_URL=postgres://docker:docker@localhost:5431/postgres npm run mocha",
27
27
  "compose:postgres": "docker-compose -f docker-compose.test.yml up -d postgres",
@@ -33,8 +33,8 @@
33
33
  "@balena/abstract-sql-compiler": "^7.20.0",
34
34
  "@balena/abstract-sql-to-typescript": "^1.2.0",
35
35
  "@balena/lf-to-abstract-sql": "^4.5.1",
36
- "@balena/odata-parser": "^2.2.9",
37
- "@balena/odata-to-abstract-sql": "^5.4.11",
36
+ "@balena/odata-parser": "^2.3.0",
37
+ "@balena/odata-to-abstract-sql": "^5.4.14",
38
38
  "@balena/sbvr-parser": "^1.4.1",
39
39
  "@balena/sbvr-types": "^3.4.7",
40
40
  "@types/body-parser": "^1.19.2",
@@ -42,20 +42,20 @@
42
42
  "@types/cookie-parser": "^1.4.3",
43
43
  "@types/deep-freeze": "^0.1.2",
44
44
  "@types/express": "^4.17.13",
45
- "@types/express-session": "^1.17.4",
45
+ "@types/express-session": "^1.17.5",
46
46
  "@types/lodash": "^4.14.182",
47
47
  "@types/memoizee": "^0.4.8",
48
48
  "@types/method-override": "^0.0.32",
49
49
  "@types/multer": "^1.4.7",
50
50
  "@types/mysql": "^2.15.21",
51
- "@types/node": "^12.20.52",
52
- "@types/passport": "^1.0.7",
51
+ "@types/node": "^12.20.55",
52
+ "@types/passport": "^1.0.9",
53
53
  "@types/passport-local": "^1.0.34",
54
54
  "@types/passport-strategy": "^0.2.35",
55
55
  "@types/pg": "^8.6.5",
56
56
  "@types/randomstring": "^1.1.8",
57
57
  "@types/websql": "^0.0.27",
58
- "commander": "^9.2.0",
58
+ "commander": "^9.4.0",
59
59
  "deep-freeze": "^0.0.1",
60
60
  "eventemitter3": "^4.0.7",
61
61
  "express-session": "^1.17.3",
@@ -87,18 +87,18 @@
87
87
  "grunt-ts": "^6.0.0-beta.22",
88
88
  "grunt-webpack": "^5.0.0",
89
89
  "husky": "^4.3.8",
90
- "lint-staged": "^12.4.1",
90
+ "lint-staged": "^12.5.0",
91
91
  "load-grunt-tasks": "^5.1.0",
92
92
  "mocha": "^9.2.2",
93
93
  "raw-loader": "^4.0.2",
94
94
  "require-npm4-to-publish": "^1.0.0",
95
- "supertest": "^6.2.3",
96
- "terser-webpack-plugin": "^5.3.1",
97
- "ts-loader": "^9.3.0",
98
- "ts-node": "^10.7.0",
99
- "typescript": "^4.7.2",
100
- "webpack": "^5.72.1",
101
- "webpack-dev-server": "^4.9.0"
95
+ "supertest": "^6.2.4",
96
+ "terser-webpack-plugin": "^5.3.3",
97
+ "ts-loader": "^9.3.1",
98
+ "ts-node": "^10.9.1",
99
+ "typescript": "^4.7.4",
100
+ "webpack": "^5.73.0",
101
+ "webpack-dev-server": "^4.9.3"
102
102
  },
103
103
  "optionalDependencies": {
104
104
  "bcrypt": "^5.0.1",
@@ -109,14 +109,14 @@
109
109
  "method-override": "^3.0.0",
110
110
  "multer": "^1.4.4",
111
111
  "mysql": "^2.18.1",
112
- "passport": "^0.5.3",
112
+ "passport": "^0.6.0",
113
113
  "passport-local": "^1.0.0",
114
114
  "pg": "^8.7.3",
115
115
  "pg-connection-string": "^2.5.0",
116
116
  "serve-static": "^1.15.0"
117
117
  },
118
118
  "engines": {
119
- "node": ">=10.0.0",
119
+ "node": ">=14.0.0",
120
120
  "npm": ">=6.0.0"
121
121
  },
122
122
  "husky": {
@@ -144,6 +144,6 @@
144
144
  "recursive": true
145
145
  },
146
146
  "versionist": {
147
- "publishedAt": "2022-06-03T17:04:33.364Z"
147
+ "publishedAt": "2022-08-02T12:27:59.997Z"
148
148
  }
149
149
  }
package/src/bin/utils.ts CHANGED
@@ -87,7 +87,7 @@ export const getAbstractSqlModelFromFile = (
87
87
  let lfModel;
88
88
  try {
89
89
  lfModel = generateLfModel(seModel);
90
- } catch (e) {
90
+ } catch (e: any) {
91
91
  throw new Error(
92
92
  `Got '${e.message}' whilst trying to parse the model file as sbvr, if you're using a transpiled language for the model file you will need to either transpile in advance or run via its loader`,
93
93
  );
@@ -1,9 +1,15 @@
1
1
  import type * as Express from 'express';
2
2
  import type { AbstractSqlModel } from '@balena/abstract-sql-compiler';
3
3
  import type { Database } from '../database-layer/db';
4
- import type { Migration } from '../migrator/migrator';
5
4
  import type { AnyObject, Resolvable } from '../sbvr-api/common-types';
6
5
 
6
+ import {
7
+ Migration,
8
+ Migrations,
9
+ defaultMigrationCategory,
10
+ MigrationCategories,
11
+ } from '../migrator/utils';
12
+
7
13
  import * as fs from 'fs';
8
14
  import * as _ from 'lodash';
9
15
  import * as path from 'path';
@@ -25,9 +31,7 @@ export interface Model {
25
31
  modelText?: string;
26
32
  abstractSql?: AbstractSqlModel;
27
33
  migrationsPath?: string;
28
- migrations?: {
29
- [index: string]: Migration;
30
- };
34
+ migrations?: Migrations;
31
35
  initSqlPath?: string;
32
36
  initSql?: string;
33
37
  customServerCode?:
@@ -40,7 +44,7 @@ export interface Model {
40
44
  export interface User {
41
45
  username: string;
42
46
  password: string;
43
- permissions: string[];
47
+ permissions?: string[];
44
48
  }
45
49
  export interface Config {
46
50
  models: Model[];
@@ -77,7 +81,7 @@ const getOrCreatePermission = async (
77
81
  ) => {
78
82
  try {
79
83
  return await getOrCreate(authApiTx, 'permission', { name: permissionName });
80
- } catch (e) {
84
+ } catch (e: any) {
81
85
  e.message = `Could not create or find permission "${permissionName}": ${e.message}`;
82
86
  throw e;
83
87
  }
@@ -99,20 +103,17 @@ export const setup = (app: Express.Application) => {
99
103
  const permissionsCache: {
100
104
  [index: string]: Promise<number>;
101
105
  } = {};
102
- users.forEach((user) => {
106
+ for (const user of users) {
103
107
  if (user.permissions == null) {
104
- return;
108
+ continue;
105
109
  }
106
- user.permissions.forEach((permissionName) => {
107
- if (permissionsCache[permissionName] != null) {
108
- return;
109
- }
110
- permissionsCache[permissionName] = getOrCreatePermission(
110
+ for (const permissionName of user.permissions) {
111
+ permissionsCache[permissionName] ??= getOrCreatePermission(
111
112
  authApiTx,
112
113
  permissionName,
113
114
  );
114
- });
115
- });
115
+ }
116
+ }
116
117
 
117
118
  await Promise.all(
118
119
  users.map(async (user) => {
@@ -128,34 +129,17 @@ export const setup = (app: Express.Application) => {
128
129
  },
129
130
  );
130
131
  if (user.permissions != null) {
131
- const permissionIds = await Promise.all(
132
+ await Promise.all(
132
133
  user.permissions.map(async (permissionName) => {
133
134
  const permissionID = await permissionsCache[permissionName];
134
135
  await getOrCreate(authApiTx, 'user__has__permission', {
135
136
  user: userID,
136
137
  permission: permissionID,
137
138
  });
138
- return permissionID;
139
139
  }),
140
140
  );
141
-
142
- await authApiTx.delete({
143
- resource: 'user__has__permission',
144
- options: {
145
- $filter: {
146
- user: userID,
147
- ...(permissionIds.length > 0 && {
148
- $not: {
149
- permission: {
150
- $in: permissionIds,
151
- },
152
- },
153
- }),
154
- },
155
- },
156
- });
157
141
  }
158
- } catch (e) {
142
+ } catch (e: any) {
159
143
  e.message = `Could not create or find user "${user.username}": ${e.message}`;
160
144
  throw e;
161
145
  }
@@ -182,7 +166,7 @@ export const setup = (app: Express.Application) => {
182
166
  console.info(
183
167
  'Successfully executed ' + model.modelName + ' model.',
184
168
  );
185
- } catch (err) {
169
+ } catch (err: any) {
186
170
  const message = `Failed to execute ${model.modelName} model from ${model.modelFile}`;
187
171
  if (_.isError(err)) {
188
172
  err.message = message;
@@ -196,11 +180,11 @@ export const setup = (app: Express.Application) => {
196
180
  if (typeof model.customServerCode === 'string') {
197
181
  try {
198
182
  customCode = nodeRequire(model.customServerCode).setup;
199
- } catch (e) {
183
+ } catch (e: any) {
200
184
  e.message = `Error loading custom server code: '${e.message}'`;
201
185
  throw e;
202
186
  }
203
- } else if (_.isObject(model.customServerCode)) {
187
+ } else if (typeof model.customServerCode === 'object') {
204
188
  customCode = model.customServerCode.setup;
205
189
  } else {
206
190
  throw new Error(
@@ -234,7 +218,7 @@ export const setup = (app: Express.Application) => {
234
218
  } else if (typeof config === 'string') {
235
219
  root = path.dirname(config);
236
220
  configObj = await loadConfigFile(config);
237
- } else if (_.isObject(config)) {
221
+ } else if (typeof config === 'object') {
238
222
  root = process.cwd();
239
223
  configObj = config;
240
224
  } else {
@@ -268,18 +252,59 @@ export const setup = (app: Express.Application) => {
268
252
  await Promise.all(
269
253
  fileNames.map(async (filename) => {
270
254
  const filePath = path.join(migrationsPath, filename);
255
+ const fileNameParts = filename.split('.', 3);
256
+ const fileExtension = path.extname(filename);
271
257
  const [migrationKey] = filename.split('-', 1);
258
+ let migrationCategory = defaultMigrationCategory;
272
259
 
273
- switch (path.extname(filename)) {
260
+ if (fileNameParts.length === 3) {
261
+ if (fileNameParts[1] in MigrationCategories) {
262
+ migrationCategory = fileNameParts[1] as MigrationCategories;
263
+ } else {
264
+ console.error(
265
+ `Unrecognised migration file category ${
266
+ fileNameParts[1]
267
+ }, skipping: ${path.extname(filename)}`,
268
+ );
269
+ return;
270
+ }
271
+ }
272
+
273
+ /**
274
+ * helper to assign migrations with category level to model
275
+ * example migration file names:
276
+ *
277
+ * key0-name.ts ==> defaults startup migration
278
+ * key1-name1.sql ==> defaults startup migration
279
+ * key2-name2.sync.sql ==> explicit synchrony migration
280
+ *
281
+ */
282
+ const assignMigrationWithCategory = (
283
+ newMigrationKey: string,
284
+ newMigration: Migration,
285
+ ) => {
286
+ const catMigrations = migrations[migrationCategory] || {};
287
+ if (typeof catMigrations === 'object') {
288
+ migrations[migrationCategory] = {
289
+ [newMigrationKey]: newMigration,
290
+ ...catMigrations,
291
+ };
292
+ }
293
+ };
294
+
295
+ switch (fileExtension) {
274
296
  case '.coffee':
275
297
  case '.ts':
276
298
  case '.js':
277
- migrations[migrationKey] = nodeRequire(filePath);
299
+ assignMigrationWithCategory(
300
+ migrationKey,
301
+ nodeRequire(filePath),
302
+ );
278
303
  break;
279
304
  case '.sql':
280
- migrations[migrationKey] = await fs.promises.readFile(
281
- filePath,
282
- 'utf8',
305
+ assignMigrationWithCategory(
306
+ migrationKey,
307
+ await fs.promises.readFile(filePath, 'utf8'),
283
308
  );
284
309
  break;
285
310
  default:
@@ -299,7 +324,7 @@ export const setup = (app: Express.Application) => {
299
324
  }),
300
325
  );
301
326
  await loadConfig(configObj);
302
- } catch (err) {
327
+ } catch (err: any) {
303
328
  console.error('Error loading application config', err, err.stack);
304
329
  process.exit(1);
305
330
  }
@@ -228,7 +228,7 @@ export async function setup(app, sbvrUtils, db) {
228
228
  await setupModels(tx);
229
229
  });
230
230
  res.status(200).end();
231
- } catch (err) {
231
+ } catch (/** @type any */ err) {
232
232
  console.error('Error clearing db', err, err.stack);
233
233
  res.status(503).end();
234
234
  }
@@ -255,7 +255,7 @@ export async function setup(app, sbvrUtils, db) {
255
255
  }
256
256
  });
257
257
  res.status(200).end();
258
- } catch (err) {
258
+ } catch (/** @type any */ err) {
259
259
  console.error('Error importing db', err, err.stack);
260
260
  res.status(404).end();
261
261
  }
@@ -278,11 +278,11 @@ export async function setup(app, sbvrUtils, db) {
278
278
  'SELECT * FROM "' + tableName + '";',
279
279
  );
280
280
  let insQuery = '';
281
- result.rows.forEach((currRow) => {
281
+ for (const currRow of result.rows) {
282
282
  let notFirst = false;
283
283
  insQuery += 'INSERT INTO "' + tableName + '" (';
284
284
  let valQuery = '';
285
- for (let propName of Object.keys(currRow)) {
285
+ for (const propName of Object.keys(currRow)) {
286
286
  if (notFirst) {
287
287
  insQuery += ',';
288
288
  valQuery += ',';
@@ -293,13 +293,13 @@ export async function setup(app, sbvrUtils, db) {
293
293
  valQuery += "'" + currRow[propName] + "'";
294
294
  }
295
295
  insQuery += ') values (' + valQuery + ');\n';
296
- });
296
+ }
297
297
  exported += insQuery;
298
298
  }),
299
299
  );
300
300
  });
301
301
  res.json(exported);
302
- } catch (err) {
302
+ } catch (/** @type any */ err) {
303
303
  console.error('Error exporting db', err, err.stack);
304
304
  res.status(503).end();
305
305
  }
@@ -329,7 +329,7 @@ export async function setup(app, sbvrUtils, db) {
329
329
  );
330
330
  });
331
331
  res.status(200).end();
332
- } catch (err) {
332
+ } catch (/** @type any */ err) {
333
333
  console.error('Error backing up db', err, err.stack);
334
334
  res.status(404).end();
335
335
  }
@@ -358,7 +358,7 @@ export async function setup(app, sbvrUtils, db) {
358
358
  );
359
359
  });
360
360
  res.status(200).end();
361
- } catch (err) {
361
+ } catch (/** @type any */ err) {
362
362
  console.error('Error restoring db', err, err.stack);
363
363
  res.status(404).end();
364
364
  }
@@ -36,7 +36,7 @@ const isSqlError = (value: any): value is SQLError => {
36
36
  };
37
37
 
38
38
  export class DatabaseError extends TypedError {
39
- public code: number | string;
39
+ public code?: number | string;
40
40
  constructor(message?: string | CodedError | SQLError) {
41
41
  if (isSqlError(message)) {
42
42
  // If this is a SQLError we have to handle it specially (since it's not actually an instance of Error)
@@ -309,7 +309,7 @@ export abstract class Tx {
309
309
  const t0 = Date.now();
310
310
  try {
311
311
  return await this._executeSql(sql, bindings, ...args);
312
- } catch (err) {
312
+ } catch (err: any) {
313
313
  throw wrapDatabaseError(err);
314
314
  } finally {
315
315
  this.automaticClose.decrementPending();
@@ -420,7 +420,7 @@ const createTransaction = (createFunc: CreateTransactionFn): TransactionFn => {
420
420
  let tx;
421
421
  try {
422
422
  tx = await createFunc(stackTraceErr);
423
- } catch (err) {
423
+ } catch (err: any) {
424
424
  throw wrapDatabaseError(err);
425
425
  }
426
426
  if (fn) {
@@ -428,7 +428,7 @@ const createTransaction = (createFunc: CreateTransactionFn): TransactionFn => {
428
428
  const result = await fn(tx);
429
429
  await tx.end();
430
430
  return result;
431
- } catch (err) {
431
+ } catch (err: any) {
432
432
  try {
433
433
  await tx.rollback();
434
434
  } catch {
@@ -559,7 +559,7 @@ if (maybePg != null) {
559
559
  text: sql,
560
560
  values: bindings,
561
561
  });
562
- } catch (err) {
562
+ } catch (err: any) {
563
563
  if (err.code === PG_UNIQUE_VIOLATION) {
564
564
  throw new UniqueConstraintError(err);
565
565
  }
@@ -584,12 +584,12 @@ if (maybePg != null) {
584
584
  const queryQueue = this.db.queryQueue as Pg.Query[];
585
585
  if (queryQueue.length > 0) {
586
586
  const err = new DatabaseError('Rolling back transaction');
587
- queryQueue.forEach((query) => {
587
+ for (const query of queryQueue) {
588
588
  process.nextTick(() => {
589
589
  // @ts-expect-error typings do not include this function
590
590
  query.handleError(err, this.db.connection);
591
591
  });
592
- });
592
+ }
593
593
  queryQueue.length = 0;
594
594
  }
595
595
  await timeout(
@@ -598,7 +598,7 @@ if (maybePg != null) {
598
598
  'Rolling back transaction timed out',
599
599
  );
600
600
  this.db.release();
601
- } catch (err) {
601
+ } catch (err: any) {
602
602
  err = wrapDatabaseError(err);
603
603
  this.db.release(err);
604
604
  throw err;
@@ -609,7 +609,7 @@ if (maybePg != null) {
609
609
  try {
610
610
  await this.$executeSql('COMMIT;');
611
611
  this.db.release();
612
- } catch (err) {
612
+ } catch (err: any) {
613
613
  this.db.release(err);
614
614
  throw err;
615
615
  }
@@ -719,7 +719,7 @@ if (maybeMysql != null) {
719
719
  result = await fromCallback<MysqlRowArray>((callback) => {
720
720
  this.db.query(sql, bindings, callback);
721
721
  });
722
- } catch (err) {
722
+ } catch (err: any) {
723
723
  if (err.code === MYSQL_UNIQUE_VIOLATION) {
724
724
  // We know that the type is an IError for mysql, but typescript doesn't like the catch obj sugar
725
725
  throw new UniqueConstraintError(err as Mysql.MysqlError);
@@ -852,7 +852,7 @@ if (typeof window !== 'undefined' && window.openDatabase != null) {
852
852
  let result;
853
853
  try {
854
854
  result = await this.tx.executeSql(sql, bindings);
855
- } catch (err) {
855
+ } catch (err: any) {
856
856
  if (err.code === WEBSQL_CONSTRAINT_ERR) {
857
857
  throw new ConstraintError('Constraint failed.');
858
858
  }
@@ -49,7 +49,7 @@ const app = (function () {
49
49
  match,
50
50
  paramName,
51
51
  // Flatten middleware list to handle arrays of middleware in the arg list.
52
- middleware: _.flattenDeep(middleware),
52
+ middleware: middleware.flat(Infinity),
53
53
  });
54
54
  };
55
55
  const process = async function (
@@ -54,7 +54,7 @@ SELECT NOT EXISTS(
54
54
  ) AS result;`,
55
55
  [request.resourceName, id],
56
56
  );
57
- } catch (err) {
57
+ } catch (/** @type any */ err) {
58
58
  logger.error('Unable to check resource locks', err, err.stack);
59
59
  throw new Error('Unable to check resource locks');
60
60
  }
@@ -137,7 +137,7 @@ WHERE "conditional resource"."transaction" = ?;\
137
137
  [transactionID],
138
138
  );
139
139
 
140
- conditionalResources.rows.forEach((conditionalResource) => {
140
+ for (const conditionalResource of conditionalResources.rows) {
141
141
  const { placeholder } = conditionalResource;
142
142
  if (placeholder != null && placeholder.length > 0) {
143
143
  /** @type {Function} */
@@ -151,7 +151,7 @@ WHERE "conditional resource"."transaction" = ?;\
151
151
  // @ts-ignore
152
152
  placeholders[placeholder] = { promise, resolve, reject };
153
153
  }
154
- });
154
+ }
155
155
 
156
156
  // get conditional resources (if exist)
157
157
  await Promise.all(
@@ -255,7 +255,7 @@ WHERE "conditional resource"."transaction" = ?;\
255
255
  await endTransaction(id);
256
256
 
257
257
  res.status(200).end();
258
- } catch (err) {
258
+ } catch (/** @type any */ err) {
259
259
  console.error('Error ending transaction', err, err.stack);
260
260
  res.status(404).json(err);
261
261
  }