@autofleet/sequelize-utils 5.2.2-beta-1fc79104.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/events/index.d.ts +2 -1
- package/dist/index.d.ts +8 -5
- package/dist/index.js +6 -14
- package/dist/logger.d.ts +1 -1
- package/dist/logger.js +1 -1
- package/dist/model-event-hooks.d.ts +2 -2
- package/dist/model-event-hooks.js +8 -20
- package/dist/runAfterTransactionCommits.d.ts +3 -0
- package/dist/runAfterTransactionCommits.js +11 -0
- package/package.json +21 -24
- package/src/index.ts +15 -23
- package/src/model-event-hooks.ts +10 -22
- package/src/runAfterTransactionCommits.ts +10 -0
- package/tsconfig.build.json +8 -0
- package/tsconfig.json +8 -2
package/dist/events/index.d.ts
CHANGED
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:
|
|
4
|
-
httpBasedTransaction:
|
|
5
|
-
registerModelEventHooks:
|
|
6
|
-
runAfterTransactionCommits:
|
|
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
|
|
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
|
|
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:
|
|
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)();
|
|
@@ -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,39 +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
|
|
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
|
-
logger_1.default.info('updateEventToDimTable', { tableName, eventVersion, objectToSendFormatted, keys: Object.keys(object.rawAttributes) });
|
|
28
28
|
if (tableName) {
|
|
29
|
-
events_1.default.sendObject(tableName, eventVersion, objectToSendFormatted,
|
|
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));
|
|
30
32
|
}
|
|
31
33
|
}
|
|
32
34
|
catch (e) {
|
|
33
35
|
logger_1.default.error('dimTables error', { e });
|
|
34
36
|
}
|
|
35
37
|
};
|
|
36
|
-
sequelize.addHook('afterSave',
|
|
37
|
-
|
|
38
|
-
options.transaction.afterCommit(() => updateEventToDimTable(savedObject));
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
await updateEventToDimTable(savedObject);
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
sequelize.addHook('afterDestroy', async (savedObject, options) => {
|
|
45
|
-
if (options.transaction) {
|
|
46
|
-
options.transaction.afterCommit(() => updateEventToDimTable(savedObject, true));
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
await updateEventToDimTable(savedObject, true);
|
|
50
|
-
}
|
|
51
|
-
});
|
|
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));
|
|
52
40
|
};
|
|
53
41
|
exports.default = addModelEventHooks;
|
|
@@ -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.
|
|
3
|
+
"version": "5.2.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"start": "ts-node
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
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
|
|
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.
|
|
23
|
-
"@autofleet/events": "^
|
|
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
|
-
|
|
28
|
-
|
|
21
|
+
"debug": "^4.3.2"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"@autofleet/logger": ">=4",
|
|
25
|
+
"sequelize": ">=5"
|
|
29
26
|
},
|
|
30
27
|
"devDependencies": {
|
|
31
|
-
"@
|
|
28
|
+
"@autofleet/logger": "^4.1.0",
|
|
32
29
|
"@types/debug": "^4.1.6",
|
|
33
30
|
"@types/express": "^4.17.13",
|
|
34
|
-
"@types/jest": "^
|
|
35
|
-
"@types/
|
|
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
|
-
"
|
|
45
|
-
"
|
|
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": "^
|
|
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
|
-
|
|
6
|
+
TransactionOptions,
|
|
7
7
|
} from 'sequelize';
|
|
8
8
|
import debug from 'debug';
|
|
9
|
-
import
|
|
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:
|
|
18
|
-
httpBasedTransaction:
|
|
19
|
-
registerModelEventHooks:
|
|
20
|
-
runAfterTransactionCommits:
|
|
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:
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
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,
|
package/src/model-event-hooks.ts
CHANGED
|
@@ -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,23 +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
|
|
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
|
-
logger.info('updateEventToDimTable', { tableName, eventVersion, objectToSendFormatted,keys: Object.keys(object.rawAttributes) });
|
|
33
32
|
if (tableName) {
|
|
34
|
-
events.sendObject(
|
|
33
|
+
await events.sendObject(
|
|
35
34
|
tableName,
|
|
36
35
|
eventVersion,
|
|
37
36
|
objectToSendFormatted,
|
|
37
|
+
// @ts-expect-error the rawAttributes is typed as static, while it actually is on the instance level.
|
|
38
38
|
Object.keys(object.rawAttributes),
|
|
39
39
|
);
|
|
40
40
|
}
|
|
@@ -43,20 +43,8 @@ const addModelEventHooks = async (sequelize: Sequelize, modelTableMapping: Model
|
|
|
43
43
|
}
|
|
44
44
|
};
|
|
45
45
|
|
|
46
|
-
sequelize.addHook('afterSave',
|
|
47
|
-
|
|
48
|
-
options.transaction.afterCommit(() => updateEventToDimTable(savedObject));
|
|
49
|
-
} else {
|
|
50
|
-
await updateEventToDimTable(savedObject);
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
sequelize.addHook('afterDestroy', async (savedObject, options) => {
|
|
54
|
-
if (options.transaction) {
|
|
55
|
-
options.transaction.afterCommit(() => updateEventToDimTable(savedObject, true));
|
|
56
|
-
} else {
|
|
57
|
-
await updateEventToDimTable(savedObject, true);
|
|
58
|
-
}
|
|
59
|
-
});
|
|
46
|
+
sequelize.addHook('afterSave', (savedObject, options) => runAfterTransactionCommits(() => updateEventToDimTable(savedObject), options));
|
|
47
|
+
sequelize.addHook('afterDestroy', (savedObject, options) => runAfterTransactionCommits(() => updateEventToDimTable(savedObject, true), options));
|
|
60
48
|
};
|
|
61
49
|
|
|
62
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;
|
package/tsconfig.json
CHANGED