@autofleet/sequelize-utils 5.1.2 → 5.1.4
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 +11 -20
- package/package.json +1 -1
- package/src/index.ts +6 -5
- package/tsconfig.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -19,12 +10,12 @@ const log = debug_1.default('sequelize-utils');
|
|
|
19
10
|
const rollbackErrorText = 'rollback has been called on this transaction';
|
|
20
11
|
const abortErrorText = 'Transaction cancelled due to request cancellation';
|
|
21
12
|
exports.default = (sequelize) => {
|
|
22
|
-
const transactionWithRetry = (funcToRun, retriesCount = 2) =>
|
|
13
|
+
const transactionWithRetry = async (funcToRun, retriesCount = 2, options) => {
|
|
23
14
|
try {
|
|
24
|
-
const transValue =
|
|
25
|
-
const funcValue =
|
|
15
|
+
const transValue = await sequelize.transaction(options || {}, async (transaction) => {
|
|
16
|
+
const funcValue = await funcToRun(transaction);
|
|
26
17
|
return funcValue;
|
|
27
|
-
})
|
|
18
|
+
});
|
|
28
19
|
return transValue;
|
|
29
20
|
}
|
|
30
21
|
catch (e) {
|
|
@@ -38,22 +29,22 @@ exports.default = (sequelize) => {
|
|
|
38
29
|
}
|
|
39
30
|
throw e;
|
|
40
31
|
}
|
|
41
|
-
}
|
|
32
|
+
};
|
|
42
33
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
43
34
|
const httpBasedTransaction = (req, cb) => {
|
|
44
35
|
if (req.socket.destroyed) {
|
|
45
36
|
log(abortErrorText);
|
|
46
37
|
throw new Error(abortErrorText);
|
|
47
38
|
}
|
|
48
|
-
return sequelize.transaction((transaction) =>
|
|
39
|
+
return sequelize.transaction(async (transaction) => {
|
|
49
40
|
// https://github.com/nodejs/node/blob/e46c680bf2b211bbd52cf959ca17ee98c7f657f5/test/parallel/test-http-aborted.js#L9
|
|
50
|
-
const rollback = () =>
|
|
41
|
+
const rollback = async () => {
|
|
51
42
|
log(abortErrorText);
|
|
52
|
-
|
|
53
|
-
}
|
|
43
|
+
await transaction.rollback();
|
|
44
|
+
};
|
|
54
45
|
req.socket.once('close', rollback);
|
|
55
46
|
try {
|
|
56
|
-
const response =
|
|
47
|
+
const response = await cb(transaction);
|
|
57
48
|
req.socket.removeListener('close', rollback);
|
|
58
49
|
return response;
|
|
59
50
|
}
|
|
@@ -64,7 +55,7 @@ exports.default = (sequelize) => {
|
|
|
64
55
|
req.socket.removeListener('close', rollback);
|
|
65
56
|
throw e;
|
|
66
57
|
}
|
|
67
|
-
})
|
|
58
|
+
});
|
|
68
59
|
};
|
|
69
60
|
return {
|
|
70
61
|
httpBasedTransaction,
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -11,12 +11,13 @@ export default (sequelize: Sequelize): {
|
|
|
11
11
|
transactionWithRetry: any,
|
|
12
12
|
httpBasedTransaction: any
|
|
13
13
|
} => {
|
|
14
|
-
const transactionWithRetry = async (funcToRun: any, retriesCount = 2): Promise<any> => {
|
|
14
|
+
const transactionWithRetry = async (funcToRun: any, retriesCount = 2, options?: any): Promise<any> => {
|
|
15
15
|
try {
|
|
16
|
-
const transValue = await sequelize.transaction(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
const transValue = await sequelize.transaction(options || {},
|
|
17
|
+
async (transaction) => {
|
|
18
|
+
const funcValue = await funcToRun(transaction);
|
|
19
|
+
return funcValue;
|
|
20
|
+
});
|
|
20
21
|
return transValue;
|
|
21
22
|
} catch (e) {
|
|
22
23
|
if (e instanceof DatabaseError) {
|