@autofleet/sequelize-utils 5.0.0
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 +6 -0
- package/dist/index.js +59 -0
- package/package.json +33 -0
- package/src/index.ts +49 -0
- package/tsconfig.json +10 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
/* eslint-disable import/prefer-default-export */
|
|
16
|
+
const sequelize_1 = require("sequelize");
|
|
17
|
+
const logger_1 = __importDefault(require("../../logger"));
|
|
18
|
+
exports.default = (sequelize) => {
|
|
19
|
+
const transactionWithRetry = (funcToRun, retriesCount = 2) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
try {
|
|
21
|
+
const transValue = yield sequelize.transaction((transaction) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
+
const funcValue = yield funcToRun(transaction);
|
|
23
|
+
return funcValue;
|
|
24
|
+
}));
|
|
25
|
+
return transValue;
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
if (e instanceof sequelize_1.DatabaseError) {
|
|
29
|
+
if (retriesCount === 0) {
|
|
30
|
+
logger_1.default.error('error inside transactionWithRetry - will stop retry', e);
|
|
31
|
+
throw e;
|
|
32
|
+
}
|
|
33
|
+
logger_1.default.error(`error inside transactionWithRetry - will retry times ${retriesCount - 1}`, e);
|
|
34
|
+
return transactionWithRetry(funcToRun, retriesCount - 1);
|
|
35
|
+
}
|
|
36
|
+
throw e;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
40
|
+
const httpBasedTransaction = (req, cb) => transactionWithRetry((transaction) => __awaiter(void 0, void 0, void 0, function* () {
|
|
41
|
+
req.on('aborted', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
|
+
yield transaction.rollback();
|
|
43
|
+
}));
|
|
44
|
+
try {
|
|
45
|
+
const response = yield cb(transaction);
|
|
46
|
+
return response;
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
if (e.message.includes('rollback has been called on this transaction')) {
|
|
50
|
+
throw new Error('Transaction cancelled due to request cancellation');
|
|
51
|
+
}
|
|
52
|
+
throw e;
|
|
53
|
+
}
|
|
54
|
+
}));
|
|
55
|
+
return {
|
|
56
|
+
httpBasedTransaction,
|
|
57
|
+
transactionWithRetry,
|
|
58
|
+
};
|
|
59
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@autofleet/sequelize-utils",
|
|
3
|
+
"version": "5.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
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 .",
|
|
12
|
+
"test": "jest --forceExit",
|
|
13
|
+
"test-local": "jest --forceExit",
|
|
14
|
+
"coverage": "jest --coverage --forceExit --runInBand && rm -rf ./coverage"
|
|
15
|
+
},
|
|
16
|
+
"author": "",
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"sequelize": "^5.22.3"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/bluebird": "^3.5.36",
|
|
23
|
+
"@types/validator": "^13.6.3",
|
|
24
|
+
"@typescript-eslint/eslint-plugin": "^4.28.3",
|
|
25
|
+
"@typescript-eslint/parser": "^4.28.3",
|
|
26
|
+
"eslint": "^7.31.0",
|
|
27
|
+
"eslint-config-airbnb-base": "^14.2.1",
|
|
28
|
+
"eslint-plugin-import": "^2.23.4",
|
|
29
|
+
"ts-node": "^10.1.0",
|
|
30
|
+
"tsc": "^2.0.3",
|
|
31
|
+
"typescript": "^4.3.5"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/* eslint-disable import/prefer-default-export */
|
|
2
|
+
import { DatabaseError, Sequelize, Transaction } from 'sequelize';
|
|
3
|
+
import logger from '../../logger';
|
|
4
|
+
|
|
5
|
+
export default (sequelize: Sequelize): {
|
|
6
|
+
transactionWithRetry: any,
|
|
7
|
+
httpBasedTransaction: any
|
|
8
|
+
} => {
|
|
9
|
+
const transactionWithRetry = async (funcToRun: any, retriesCount = 2): Promise<any> => {
|
|
10
|
+
try {
|
|
11
|
+
const transValue = await sequelize.transaction(async (transaction) => {
|
|
12
|
+
const funcValue = await funcToRun(transaction);
|
|
13
|
+
return funcValue;
|
|
14
|
+
});
|
|
15
|
+
return transValue;
|
|
16
|
+
} catch (e) {
|
|
17
|
+
if (e instanceof DatabaseError) {
|
|
18
|
+
if (retriesCount === 0) {
|
|
19
|
+
logger.error('error inside transactionWithRetry - will stop retry', e);
|
|
20
|
+
throw e;
|
|
21
|
+
}
|
|
22
|
+
logger.error(`error inside transactionWithRetry - will retry times ${retriesCount - 1}`, e);
|
|
23
|
+
return transactionWithRetry(funcToRun, retriesCount - 1);
|
|
24
|
+
}
|
|
25
|
+
throw e;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
30
|
+
const httpBasedTransaction = (req: any, cb: Function) => transactionWithRetry(async (transaction: Transaction) => {
|
|
31
|
+
req.on('aborted', async () => {
|
|
32
|
+
await transaction.rollback();
|
|
33
|
+
});
|
|
34
|
+
try {
|
|
35
|
+
const response = await cb(transaction);
|
|
36
|
+
return response;
|
|
37
|
+
} catch (e) {
|
|
38
|
+
if (e.message.includes('rollback has been called on this transaction')) {
|
|
39
|
+
throw new Error('Transaction cancelled due to request cancellation');
|
|
40
|
+
}
|
|
41
|
+
throw e;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
httpBasedTransaction,
|
|
47
|
+
transactionWithRetry,
|
|
48
|
+
};
|
|
49
|
+
};
|