@autofleet/sequelize-utils 5.0.4 → 5.0.6
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.js +23 -14
- package/package.json +1 -1
- package/src/index.ts +25 -14
package/dist/index.js
CHANGED
|
@@ -16,6 +16,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
16
16
|
const sequelize_1 = require("sequelize");
|
|
17
17
|
const debug_1 = __importDefault(require("debug"));
|
|
18
18
|
const log = debug_1.default('sequelize-utils');
|
|
19
|
+
const rollbackErrorText = 'rollback has been called on this transaction';
|
|
20
|
+
const abortErrorText = 'Transaction cancelled due to request cancellation';
|
|
19
21
|
exports.default = (sequelize) => {
|
|
20
22
|
const transactionWithRetry = (funcToRun, retriesCount = 2) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
23
|
try {
|
|
@@ -38,22 +40,29 @@ exports.default = (sequelize) => {
|
|
|
38
40
|
}
|
|
39
41
|
});
|
|
40
42
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
41
|
-
const httpBasedTransaction = (req, cb) =>
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}));
|
|
46
|
-
try {
|
|
47
|
-
const response = yield cb(transaction);
|
|
48
|
-
return response;
|
|
43
|
+
const httpBasedTransaction = (req, cb) => {
|
|
44
|
+
if (req.aborted) {
|
|
45
|
+
log(abortErrorText);
|
|
46
|
+
throw new Error(abortErrorText);
|
|
49
47
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
return transactionWithRetry((transaction) => __awaiter(void 0, void 0, void 0, function* () {
|
|
49
|
+
// https://github.com/nodejs/node/blob/e46c680bf2b211bbd52cf959ca17ee98c7f657f5/test/parallel/test-http-aborted.js#L9
|
|
50
|
+
req.on('aborted', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
51
|
+
log(abortErrorText);
|
|
52
|
+
yield transaction.rollback();
|
|
53
|
+
}));
|
|
54
|
+
try {
|
|
55
|
+
const response = yield cb(transaction);
|
|
56
|
+
return response;
|
|
53
57
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
catch (e) {
|
|
59
|
+
if (e.message.includes(rollbackErrorText)) {
|
|
60
|
+
throw new Error(abortErrorText);
|
|
61
|
+
}
|
|
62
|
+
throw e;
|
|
63
|
+
}
|
|
64
|
+
}));
|
|
65
|
+
};
|
|
57
66
|
return {
|
|
58
67
|
httpBasedTransaction,
|
|
59
68
|
transactionWithRetry,
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -4,6 +4,9 @@ import debug from 'debug';
|
|
|
4
4
|
|
|
5
5
|
const log = debug('sequelize-utils');
|
|
6
6
|
|
|
7
|
+
const rollbackErrorText = 'rollback has been called on this transaction';
|
|
8
|
+
const abortErrorText = 'Transaction cancelled due to request cancellation';
|
|
9
|
+
|
|
7
10
|
export default (sequelize: Sequelize): {
|
|
8
11
|
transactionWithRetry: any,
|
|
9
12
|
httpBasedTransaction: any
|
|
@@ -29,21 +32,29 @@ export default (sequelize: Sequelize): {
|
|
|
29
32
|
};
|
|
30
33
|
|
|
31
34
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
32
|
-
const httpBasedTransaction = (req: any, cb: Function) =>
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
});
|
|
37
|
-
try {
|
|
38
|
-
const response = await cb(transaction);
|
|
39
|
-
return response;
|
|
40
|
-
} catch (e) {
|
|
41
|
-
if (e.message.includes('rollback has been called on this transaction')) {
|
|
42
|
-
throw new Error('Transaction cancelled due to request cancellation');
|
|
43
|
-
}
|
|
44
|
-
throw e;
|
|
35
|
+
const httpBasedTransaction = (req: any, cb: Function) => {
|
|
36
|
+
if (req.aborted) {
|
|
37
|
+
log(abortErrorText);
|
|
38
|
+
throw new Error(abortErrorText);
|
|
45
39
|
}
|
|
46
|
-
|
|
40
|
+
return transactionWithRetry(async (transaction: Transaction) => {
|
|
41
|
+
// https://github.com/nodejs/node/blob/e46c680bf2b211bbd52cf959ca17ee98c7f657f5/test/parallel/test-http-aborted.js#L9
|
|
42
|
+
req.on('aborted', async () => {
|
|
43
|
+
log(abortErrorText);
|
|
44
|
+
await transaction.rollback();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
const response = await cb(transaction);
|
|
49
|
+
return response;
|
|
50
|
+
} catch (e) {
|
|
51
|
+
if (e.message.includes(rollbackErrorText)) {
|
|
52
|
+
throw new Error(abortErrorText);
|
|
53
|
+
}
|
|
54
|
+
throw e;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
};
|
|
47
58
|
|
|
48
59
|
return {
|
|
49
60
|
httpBasedTransaction,
|