@autofleet/sequelize-utils 5.2.2 → 5.2.3

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/dist/index.d.ts CHANGED
@@ -1,8 +1,11 @@
1
- import { Sequelize } from 'sequelize';
1
+ import { Sequelize, Transaction, TransactionOptions } from 'sequelize';
2
+ import type { IncomingMessage, ServerResponse } from 'http';
3
+ import { ModelMapping } from './model-event-hooks';
4
+ import runAfterTransactionCommits from './runAfterTransactionCommits';
2
5
  declare const _default: (sequelize: Sequelize) => {
3
- transactionWithRetry: any;
4
- httpBasedTransaction: any;
5
- registerModelEventHooks: any;
6
- runAfterTransactionCommits: any;
6
+ transactionWithRetry: <T>(funcToRun: (transaction: Transaction) => Promise<T>, retriesCount?: number, options?: TransactionOptions) => Promise<T>;
7
+ httpBasedTransaction: <T>(req: IncomingMessage, res: ServerResponse, cb: (transaction: Transaction) => Promise<T>) => Promise<T>;
8
+ registerModelEventHooks: (modelTableMapping: ModelMapping) => Promise<void>;
9
+ runAfterTransactionCommits: typeof runAfterTransactionCommits;
7
10
  };
8
11
  export default _default;
package/dist/index.js CHANGED
@@ -7,13 +7,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
7
7
  const sequelize_1 = require("sequelize");
8
8
  const debug_1 = __importDefault(require("debug"));
9
9
  const model_event_hooks_1 = __importDefault(require("./model-event-hooks"));
10
- const log = debug_1.default('sequelize-utils');
10
+ const runAfterTransactionCommits_1 = __importDefault(require("./runAfterTransactionCommits"));
11
+ const log = (0, debug_1.default)('sequelize-utils');
11
12
  const rollbackErrorText = 'rollback has been called on this transaction';
12
13
  const abortErrorText = 'Transaction cancelled due to request cancellation';
13
14
  exports.default = (sequelize) => {
14
- const transactionWithRetry = async (funcToRun, retriesCount = 2, options) => {
15
+ const transactionWithRetry = async (funcToRun, retriesCount = 2, options = {}) => {
15
16
  try {
16
- const transValue = await sequelize.transaction(options || {}, async (transaction) => {
17
+ const transValue = await sequelize.transaction(options, async (transaction) => {
17
18
  const funcValue = await funcToRun(transaction);
18
19
  return funcValue;
19
20
  });
@@ -31,7 +32,6 @@ exports.default = (sequelize) => {
31
32
  throw e;
32
33
  }
33
34
  };
34
- // eslint-disable-next-line @typescript-eslint/ban-types
35
35
  const httpBasedTransaction = (req, res, cb) => {
36
36
  let aborted = false;
37
37
  if (req.socket.destroyed) {
@@ -81,19 +81,11 @@ exports.default = (sequelize) => {
81
81
  }
82
82
  });
83
83
  };
84
- const registerModelEventHooks = (modelTableMapping) => model_event_hooks_1.default(sequelize, modelTableMapping);
85
- const runAfterTransactionCommits = (cb, options) => {
86
- if (options.transaction) {
87
- options.transaction.afterCommit(() => cb());
88
- }
89
- else {
90
- cb();
91
- }
92
- };
84
+ const registerModelEventHooks = (modelTableMapping) => (0, model_event_hooks_1.default)(sequelize, modelTableMapping);
93
85
  return {
94
86
  httpBasedTransaction,
95
87
  transactionWithRetry,
96
88
  registerModelEventHooks,
97
- runAfterTransactionCommits,
89
+ runAfterTransactionCommits: runAfterTransactionCommits_1.default,
98
90
  };
99
91
  };
package/dist/logger.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- declare const _default: any;
1
+ declare const _default: import("@autofleet/logger").LoggerInstanceManager;
2
2
  export default _default;
package/dist/logger.js CHANGED
@@ -4,4 +4,4 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const logger_1 = __importDefault(require("@autofleet/logger"));
7
- exports.default = logger_1.default();
7
+ exports.default = (0, logger_1.default)();
@@ -1,5 +1,5 @@
1
- import { Sequelize } from 'sequelize';
2
- interface ModelMapping {
1
+ import type { Sequelize } from 'sequelize';
2
+ export interface ModelMapping {
3
3
  [ModelName: string]: {
4
4
  tableName: string;
5
5
  };
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const events_1 = __importDefault(require("./events"));
7
7
  const logger_1 = __importDefault(require("./logger"));
8
+ const runAfterTransactionCommits_1 = __importDefault(require("./runAfterTransactionCommits"));
8
9
  const formatDatesInObject = (obj) => {
9
10
  const newObj = { ...obj };
10
11
  Object.keys(newObj).forEach((key) => {
@@ -15,38 +16,26 @@ const formatDatesInObject = (obj) => {
15
16
  return newObj;
16
17
  };
17
18
  const addModelEventHooks = async (sequelize, modelTableMapping) => {
18
- const updateEventToDimTable = (object, isDelete = false) => {
19
+ const updateEventToDimTable = async (object, isDelete = false) => {
19
20
  try {
20
21
  const objectToSend = object.get();
21
22
  if (isDelete) {
22
- objectToSend.deletedAt = new Date();
23
+ Object.assign(objectToSend, { deletedAt: new Date() });
23
24
  }
24
25
  const tableName = modelTableMapping[object.constructor.name]?.tableName;
25
26
  const eventVersion = '1';
26
27
  const objectToSendFormatted = formatDatesInObject(objectToSend);
27
28
  if (tableName) {
28
- events_1.default.sendObject(tableName, eventVersion, objectToSendFormatted, Object.keys(object.rawAttributes));
29
+ await events_1.default.sendObject(tableName, eventVersion, objectToSendFormatted,
30
+ // @ts-expect-error the rawAttributes is typed as static, while it actually is on the instance level.
31
+ Object.keys(object.rawAttributes));
29
32
  }
30
33
  }
31
34
  catch (e) {
32
35
  logger_1.default.error('dimTables error', { e });
33
36
  }
34
37
  };
35
- sequelize.addHook('afterSave', async (savedObject, options) => {
36
- if (options.transaction) {
37
- options.transaction.afterCommit(() => updateEventToDimTable(savedObject));
38
- }
39
- else {
40
- await updateEventToDimTable(savedObject);
41
- }
42
- });
43
- sequelize.addHook('afterDestroy', async (savedObject, options) => {
44
- if (options.transaction) {
45
- options.transaction.afterCommit(() => updateEventToDimTable(savedObject, true));
46
- }
47
- else {
48
- await updateEventToDimTable(savedObject, true);
49
- }
50
- });
38
+ sequelize.addHook('afterSave', (savedObject, options) => (0, runAfterTransactionCommits_1.default)(() => updateEventToDimTable(savedObject), options));
39
+ sequelize.addHook('afterDestroy', (savedObject, options) => (0, runAfterTransactionCommits_1.default)(() => updateEventToDimTable(savedObject, true), options));
51
40
  };
52
41
  exports.default = addModelEventHooks;
@@ -0,0 +1,3 @@
1
+ import { Transactionable } from 'sequelize';
2
+ declare const runAfterTransactionCommits: (cb: () => unknown, options: Transactionable) => Promise<void>;
3
+ export default runAfterTransactionCommits;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const runAfterTransactionCommits = async (cb, options) => {
4
+ if (options.transaction) {
5
+ options.transaction.afterCommit(() => cb());
6
+ }
7
+ else {
8
+ await cb();
9
+ }
10
+ };
11
+ exports.default = runAfterTransactionCommits;
package/package.json CHANGED
@@ -1,49 +1,46 @@
1
1
  {
2
2
  "name": "@autofleet/sequelize-utils",
3
- "version": "5.2.2",
3
+ "version": "5.2.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
- "start": "ts-node src/index.ts",
8
- "example": "ts-node src/example.ts",
9
- "prepublish": "tsc",
10
- "build": "tsc",
11
- "lint": "./node_modules/.bin/eslint .",
7
+ "start": "ts-node src/index.ts",
8
+ "prepublish": "tsc -p tsconfig.build.json",
9
+ "build": "tsc -p tsconfig.build.json",
10
+ "lint": "eslint .",
12
11
  "test": "jest --forceExit",
13
12
  "test-local": "jest --forceExit",
14
- "coverage": "jest --coverage --forceExit --runInBand && rm -rf ./coverage"
15
- },
16
- "jest": {
17
- "testEnvironment": "node"
13
+ "coverage": "jest --coverage --forceExit --runInBand"
18
14
  },
19
15
  "author": "",
20
16
  "license": "ISC",
21
17
  "dependencies": {
22
- "@autofleet/errors": "^1.1.0",
23
- "@autofleet/events": "^3.0.5",
24
- "@autofleet/logger": "^2.0.2",
18
+ "@autofleet/errors": "^1.2.3",
19
+ "@autofleet/events": "^3.0.7",
25
20
  "@autofleet/network": "^1.3.7",
26
- "debug": "^4.3.2",
27
- "jest": "^27.0.6",
28
- "sequelize": "^5.22.3"
21
+ "debug": "^4.3.2"
22
+ },
23
+ "peerDependencies": {
24
+ "@autofleet/logger": ">=4",
25
+ "sequelize": ">=5"
29
26
  },
30
27
  "devDependencies": {
31
- "@types/bluebird": "^3.5.36",
28
+ "@autofleet/logger": "^4.1.0",
32
29
  "@types/debug": "^4.1.6",
33
30
  "@types/express": "^4.17.13",
34
- "@types/jest": "^26.0.24",
35
- "@types/validator": "^13.6.3",
31
+ "@types/jest": "^29.5.14",
32
+ "@types/node": "^22.10.6",
36
33
  "@typescript-eslint/eslint-plugin": "^4.28.3",
37
34
  "@typescript-eslint/parser": "^4.28.3",
38
- "axios": "^0.26.1",
39
- "bluebird": "^3.7.2",
40
35
  "eslint": "^7.31.0",
41
36
  "eslint-config-airbnb-base": "^14.2.1",
42
37
  "eslint-plugin-import": "^2.23.4",
43
38
  "express": "^4.17.3",
44
- "pg": "^8.6.0",
45
- "ts-jest": "^27.0.3",
39
+ "jest": "^29.7.0",
40
+ "pg": "^8.13.1",
41
+ "sequelize": "^5.22.3",
42
+ "ts-jest": "^29.2.5",
46
43
  "ts-node": "^10.1.0",
47
- "typescript": "^4.3.5"
44
+ "typescript": "^5.5.4"
48
45
  }
49
46
  }
package/src/index.ts CHANGED
@@ -3,10 +3,12 @@ import {
3
3
  DatabaseError,
4
4
  Sequelize,
5
5
  Transaction,
6
- Transactionable,
6
+ TransactionOptions,
7
7
  } from 'sequelize';
8
8
  import debug from 'debug';
9
- import addModelEventHooks from './model-event-hooks';
9
+ import type { IncomingMessage, ServerResponse } from 'http';
10
+ import addModelEventHooks, { ModelMapping } from './model-event-hooks';
11
+ import runAfterTransactionCommits from './runAfterTransactionCommits';
10
12
 
11
13
  const log = debug('sequelize-utils');
12
14
 
@@ -14,18 +16,17 @@ const rollbackErrorText = 'rollback has been called on this transaction';
14
16
  const abortErrorText = 'Transaction cancelled due to request cancellation';
15
17
 
16
18
  export default (sequelize: Sequelize): {
17
- transactionWithRetry: any,
18
- httpBasedTransaction: any,
19
- registerModelEventHooks: any,
20
- runAfterTransactionCommits: any,
19
+ transactionWithRetry: <T>(funcToRun: (transaction: Transaction) => Promise<T>, retriesCount?: number, options?: TransactionOptions) => Promise<T>;
20
+ httpBasedTransaction: <T>(req: IncomingMessage, res: ServerResponse, cb: (transaction: Transaction) => Promise<T>) => Promise<T>;
21
+ registerModelEventHooks: (modelTableMapping: ModelMapping) => Promise<void>;
22
+ runAfterTransactionCommits: typeof runAfterTransactionCommits;
21
23
  } => {
22
- const transactionWithRetry = async (funcToRun: any, retriesCount = 2, options?: any): Promise<any> => {
24
+ const transactionWithRetry = async <T>(funcToRun: (transaction: Transaction) => Promise<T>, retriesCount = 2, options: TransactionOptions = {}): Promise<T> => {
23
25
  try {
24
- const transValue = await sequelize.transaction(options || {},
25
- async (transaction) => {
26
- const funcValue = await funcToRun(transaction);
27
- return funcValue;
28
- });
26
+ const transValue = await sequelize.transaction(options, async (transaction) => {
27
+ const funcValue = await funcToRun(transaction);
28
+ return funcValue;
29
+ });
29
30
  return transValue;
30
31
  } catch (e) {
31
32
  if (e instanceof DatabaseError || e?.constructor?.name === 'DatabaseError') {
@@ -40,8 +41,7 @@ export default (sequelize: Sequelize): {
40
41
  }
41
42
  };
42
43
 
43
- // eslint-disable-next-line @typescript-eslint/ban-types
44
- const httpBasedTransaction = (req: any, res: any, cb: Function) => {
44
+ const httpBasedTransaction = <T>(req: IncomingMessage, res: ServerResponse, cb: (transaction: Transaction) => Promise<T>): Promise<T> => {
45
45
  let aborted = false;
46
46
  if (req.socket.destroyed) {
47
47
  log(abortErrorText);
@@ -92,15 +92,7 @@ export default (sequelize: Sequelize): {
92
92
  });
93
93
  };
94
94
 
95
- const registerModelEventHooks = (modelTableMapping) => addModelEventHooks(sequelize, modelTableMapping);
96
-
97
- const runAfterTransactionCommits = (cb: () => any, options: Transactionable) => {
98
- if (options.transaction) {
99
- options.transaction.afterCommit(() => cb());
100
- } else {
101
- cb();
102
- }
103
- };
95
+ const registerModelEventHooks = (modelTableMapping: ModelMapping) => addModelEventHooks(sequelize, modelTableMapping);
104
96
 
105
97
  return {
106
98
  httpBasedTransaction,
@@ -1,8 +1,9 @@
1
- import { Sequelize } from 'sequelize';
1
+ import type { Model, Sequelize } from 'sequelize';
2
2
  import events from './events';
3
3
  import logger from './logger';
4
+ import runAfterTransactionCommits from './runAfterTransactionCommits';
4
5
 
5
- interface ModelMapping {
6
+ export interface ModelMapping {
6
7
  [ModelName: string]: {
7
8
  tableName: string
8
9
  };
@@ -18,21 +19,22 @@ const formatDatesInObject = (obj: any) => {
18
19
  return newObj;
19
20
  };
20
21
 
21
- const addModelEventHooks = async (sequelize: Sequelize, modelTableMapping: ModelMapping) => {
22
- const updateEventToDimTable = (object, isDelete = false) => {
22
+ const addModelEventHooks = async (sequelize: Sequelize, modelTableMapping: ModelMapping): Promise<void> => {
23
+ const updateEventToDimTable = async (object: Model, isDelete = false) => {
23
24
  try {
24
25
  const objectToSend = object.get();
25
26
  if (isDelete) {
26
- objectToSend.deletedAt = new Date();
27
+ Object.assign(objectToSend, { deletedAt: new Date() });
27
28
  }
28
29
  const tableName = modelTableMapping[object.constructor.name]?.tableName;
29
30
  const eventVersion = '1';
30
31
  const objectToSendFormatted = formatDatesInObject(objectToSend);
31
32
  if (tableName) {
32
- events.sendObject(
33
+ await events.sendObject(
33
34
  tableName,
34
35
  eventVersion,
35
36
  objectToSendFormatted,
37
+ // @ts-expect-error the rawAttributes is typed as static, while it actually is on the instance level.
36
38
  Object.keys(object.rawAttributes),
37
39
  );
38
40
  }
@@ -41,20 +43,8 @@ const addModelEventHooks = async (sequelize: Sequelize, modelTableMapping: Model
41
43
  }
42
44
  };
43
45
 
44
- sequelize.addHook('afterSave', async (savedObject, options) => {
45
- if (options.transaction) {
46
- options.transaction.afterCommit(() => updateEventToDimTable(savedObject));
47
- } else {
48
- await updateEventToDimTable(savedObject);
49
- }
50
- });
51
- sequelize.addHook('afterDestroy', async (savedObject, options) => {
52
- if (options.transaction) {
53
- options.transaction.afterCommit(() => updateEventToDimTable(savedObject, true));
54
- } else {
55
- await updateEventToDimTable(savedObject, true);
56
- }
57
- });
46
+ sequelize.addHook('afterSave', (savedObject, options) => runAfterTransactionCommits(() => updateEventToDimTable(savedObject), options));
47
+ sequelize.addHook('afterDestroy', (savedObject, options) => runAfterTransactionCommits(() => updateEventToDimTable(savedObject, true), options));
58
48
  };
59
49
 
60
50
  export default addModelEventHooks;
@@ -0,0 +1,10 @@
1
+ import { Transactionable } from 'sequelize';
2
+
3
+ const runAfterTransactionCommits = async (cb: () => unknown, options: Transactionable): Promise<void> => {
4
+ if (options.transaction) {
5
+ options.transaction.afterCommit(() => cb());
6
+ } else {
7
+ await cb();
8
+ }
9
+ };
10
+ export default runAfterTransactionCommits;
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "exclude": [
4
+ "node_modules",
5
+ "dist/**/*",
6
+ "**/*.test.*"
7
+ ]
8
+ }
package/tsconfig.json CHANGED
@@ -5,6 +5,12 @@
5
5
  "declaration": true,
6
6
  "outDir": "./dist",
7
7
  "strict": false,
8
- "esModuleInterop": true
9
- }
8
+ "esModuleInterop": true,
9
+ "skipLibCheck": true
10
+ },
11
+ "include": ["src/**/*"],
12
+ "exclude": [
13
+ "node_modules",
14
+ "dist/**/*"
15
+ ]
10
16
  }