@autofleet/sequelize-utils 5.2.7 → 6.0.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/.env +5 -0
- package/dist/events/index.d.ts +0 -3
- package/dist/events/index.js +0 -8
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -2
- package/dist/model-event-hooks.d.ts +3 -1
- package/dist/model-event-hooks.js +9 -5
- package/dist/test-utils/events.d.ts +3 -0
- package/dist/test-utils/events.js +8 -0
- package/package.json +3 -4
- package/src/events/index.ts +0 -4
- package/src/index.ts +4 -2
- package/src/model-event-hooks.ts +11 -5
- package/src/test-utils/events.ts +4 -0
package/.env
ADDED
package/dist/events/index.d.ts
CHANGED
package/dist/events/index.js
CHANGED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const events_1 = __importDefault(require("@autofleet/events"));
|
|
7
|
-
const logger_1 = __importDefault(require("../logger"));
|
|
8
|
-
exports.default = new events_1.default({ logger: logger_1.default });
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { Sequelize, Transaction } from 'sequelize';
|
|
2
|
+
import type { LoggerInstanceManager } from '@autofleet/logger';
|
|
2
3
|
import addModelEventHooks from './model-event-hooks';
|
|
3
4
|
import { transactionWithRetry as _transactionWithRetry } from './transaction-with-retry';
|
|
4
5
|
import { httpBasedTransaction as _httpBasedTransaction } from './http-based-transaction';
|
|
5
6
|
import runAfterTransactionCommits from './runAfterTransactionCommits';
|
|
6
7
|
type ArgsWithoutSequelize<T> = T extends (arg1: Sequelize, ...args: infer U) => any ? U : never;
|
|
7
|
-
declare const _default: (sequelize: Sequelize) => {
|
|
8
|
+
declare const _default: (sequelize: Sequelize, logger?: LoggerInstanceManager) => {
|
|
8
9
|
transactionWithRetry: <T>(...args: ArgsWithoutSequelize<typeof _transactionWithRetry<T>>) => Promise<T>;
|
|
9
10
|
httpBasedTransaction: <T>(...args: ArgsWithoutSequelize<typeof _httpBasedTransaction<T>>) => Promise<T>;
|
|
10
11
|
registerModelEventHooks: (...args: ArgsWithoutSequelize<typeof addModelEventHooks>) => void;
|
package/dist/index.js
CHANGED
|
@@ -7,10 +7,10 @@ const model_event_hooks_1 = __importDefault(require("./model-event-hooks"));
|
|
|
7
7
|
const transaction_with_retry_1 = require("./transaction-with-retry");
|
|
8
8
|
const http_based_transaction_1 = require("./http-based-transaction");
|
|
9
9
|
const runAfterTransactionCommits_1 = __importDefault(require("./runAfterTransactionCommits"));
|
|
10
|
-
exports.default = (sequelize) => {
|
|
10
|
+
exports.default = (sequelize, logger) => {
|
|
11
11
|
const transactionWithRetry = (funcToRun, retriesCount, options) => (0, transaction_with_retry_1.transactionWithRetry)(sequelize, funcToRun, retriesCount, options);
|
|
12
12
|
const httpBasedTransaction = (req, res, cb) => (0, http_based_transaction_1.httpBasedTransaction)(sequelize, req, res, cb);
|
|
13
|
-
const registerModelEventHooks = (modelTableMapping) => (0, model_event_hooks_1.default)(sequelize, modelTableMapping);
|
|
13
|
+
const registerModelEventHooks = (modelTableMapping, events) => (0, model_event_hooks_1.default)(sequelize, modelTableMapping, events, logger);
|
|
14
14
|
const useOrCreateTransaction = (transaction, cb) => {
|
|
15
15
|
if (transaction) {
|
|
16
16
|
return cb(transaction);
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { Sequelize } from 'sequelize';
|
|
2
|
+
import type Events from '@autofleet/events';
|
|
3
|
+
import type { LoggerInstanceManager } from '@autofleet/logger';
|
|
2
4
|
export interface ModelMapping {
|
|
3
5
|
[ModelName: string]: {
|
|
4
6
|
tableName: string;
|
|
5
7
|
};
|
|
6
8
|
}
|
|
7
|
-
declare const addModelEventHooks: (sequelize: Sequelize, modelTableMapping: ModelMapping) => void;
|
|
9
|
+
declare const addModelEventHooks: (sequelize: Sequelize, modelTableMapping: ModelMapping, events: Events, logger?: LoggerInstanceManager) => void;
|
|
8
10
|
export default addModelEventHooks;
|
|
@@ -3,7 +3,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const events_1 = __importDefault(require("./events"));
|
|
7
6
|
const logger_1 = __importDefault(require("./logger"));
|
|
8
7
|
const runAfterTransactionCommits_1 = __importDefault(require("./runAfterTransactionCommits"));
|
|
9
8
|
const isDate = (input) => input instanceof Date || Object.prototype.toString.call(input) === '[object Date]';
|
|
@@ -14,8 +13,13 @@ const formatDatesInObject = (obj) => {
|
|
|
14
13
|
});
|
|
15
14
|
return newObj;
|
|
16
15
|
};
|
|
17
|
-
const addModelEventHooks = (sequelize, modelTableMapping) => {
|
|
16
|
+
const addModelEventHooks = (sequelize, modelTableMapping, events, logger) => {
|
|
17
|
+
if (!events) {
|
|
18
|
+
logger.warn('Events object must be provided to addModelEventHooks');
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
18
21
|
const updateEventToDimTable = (object, isDelete = false) => {
|
|
22
|
+
const localLogger = logger || logger_1.default;
|
|
19
23
|
try {
|
|
20
24
|
const objectToSend = object.get();
|
|
21
25
|
if (isDelete) {
|
|
@@ -27,12 +31,12 @@ const addModelEventHooks = (sequelize, modelTableMapping) => {
|
|
|
27
31
|
if (!tableName) {
|
|
28
32
|
return;
|
|
29
33
|
}
|
|
30
|
-
|
|
31
|
-
|
|
34
|
+
events.sendObject(tableName, eventVersion, objectToSendFormatted, Object.keys(object.rawAttributes)).catch((e) => {
|
|
35
|
+
localLogger.error('sendObject error', { tableName, eventVersion, e });
|
|
32
36
|
});
|
|
33
37
|
}
|
|
34
38
|
catch (e) {
|
|
35
|
-
|
|
39
|
+
localLogger.error('dimTables error', { e });
|
|
36
40
|
}
|
|
37
41
|
};
|
|
38
42
|
sequelize.addHook('afterSave', (savedObject, options) => (0, runAfterTransactionCommits_1.default)(() => updateEventToDimTable(savedObject), options));
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const events_1 = __importDefault(require("@autofleet/events"));
|
|
7
|
+
const logger_1 = __importDefault(require("../logger"));
|
|
8
|
+
exports.default = new events_1.default({ logger: logger_1.default });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/sequelize-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0-0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -15,16 +15,15 @@
|
|
|
15
15
|
"author": "",
|
|
16
16
|
"license": "ISC",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@autofleet/errors": "^1.2.3",
|
|
19
|
-
"@autofleet/events": "^4.0.0",
|
|
20
|
-
"@autofleet/network": "^1.3.7",
|
|
21
18
|
"debug": "^4.3.2"
|
|
22
19
|
},
|
|
23
20
|
"peerDependencies": {
|
|
21
|
+
"@autofleet/events": ">=4",
|
|
24
22
|
"@autofleet/logger": ">=4",
|
|
25
23
|
"sequelize": ">=5"
|
|
26
24
|
},
|
|
27
25
|
"devDependencies": {
|
|
26
|
+
"@autofleet/network": "^1.3.7",
|
|
28
27
|
"@autofleet/logger": "^4.1.0",
|
|
29
28
|
"@types/debug": "^4.1.6",
|
|
30
29
|
"@types/express": "^4.17.13",
|
package/src/events/index.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -5,6 +5,8 @@ import type {
|
|
|
5
5
|
TransactionOptions,
|
|
6
6
|
} from 'sequelize';
|
|
7
7
|
import type { IncomingMessage, ServerResponse } from 'http';
|
|
8
|
+
import type { LoggerInstanceManager } from '@autofleet/logger';
|
|
9
|
+
import type Events from '@autofleet/events';
|
|
8
10
|
import addModelEventHooks, { ModelMapping } from './model-event-hooks';
|
|
9
11
|
import { transactionWithRetry as _transactionWithRetry } from './transaction-with-retry';
|
|
10
12
|
import { httpBasedTransaction as _httpBasedTransaction } from './http-based-transaction';
|
|
@@ -12,7 +14,7 @@ import runAfterTransactionCommits from './runAfterTransactionCommits';
|
|
|
12
14
|
|
|
13
15
|
type ArgsWithoutSequelize<T> = T extends (arg1: Sequelize, ...args: infer U) => any ? U : never;
|
|
14
16
|
|
|
15
|
-
export default (sequelize: Sequelize): {
|
|
17
|
+
export default (sequelize: Sequelize, logger?: LoggerInstanceManager): {
|
|
16
18
|
transactionWithRetry: <T>(...args: ArgsWithoutSequelize<typeof _transactionWithRetry<T>>) => Promise<T>;
|
|
17
19
|
httpBasedTransaction: <T>(...args: ArgsWithoutSequelize<typeof _httpBasedTransaction<T>>) => Promise<T>;
|
|
18
20
|
registerModelEventHooks: (...args: ArgsWithoutSequelize<typeof addModelEventHooks>) => void;
|
|
@@ -21,7 +23,7 @@ export default (sequelize: Sequelize): {
|
|
|
21
23
|
} => {
|
|
22
24
|
const transactionWithRetry = <T>(funcToRun: (transaction: Transaction) => Promise<T>, retriesCount?: number, options?: TransactionOptions): Promise<T> => _transactionWithRetry(sequelize, funcToRun, retriesCount, options);
|
|
23
25
|
const httpBasedTransaction = <T>(req: IncomingMessage, res: ServerResponse, cb: (transaction: Transaction) => Promise<T>): Promise<T> => _httpBasedTransaction(sequelize, req, res, cb);
|
|
24
|
-
const registerModelEventHooks = (modelTableMapping: ModelMapping) => addModelEventHooks(sequelize, modelTableMapping);
|
|
26
|
+
const registerModelEventHooks = (modelTableMapping: ModelMapping, events: Events) => addModelEventHooks(sequelize, modelTableMapping, events, logger);
|
|
25
27
|
const useOrCreateTransaction = <T>(transaction: Transaction, cb: (t: Transaction) => Promise<T>) => {
|
|
26
28
|
if (transaction) {
|
|
27
29
|
return cb(transaction);
|
package/src/model-event-hooks.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Model, Sequelize } from 'sequelize';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import type Events from '@autofleet/events';
|
|
3
|
+
import type { LoggerInstanceManager } from '@autofleet/logger';
|
|
4
|
+
import packageLogger from './logger';
|
|
4
5
|
import runAfterTransactionCommits from './runAfterTransactionCommits';
|
|
5
6
|
|
|
6
7
|
export interface ModelMapping {
|
|
@@ -18,8 +19,13 @@ const formatDatesInObject = (obj: object) => {
|
|
|
18
19
|
return newObj;
|
|
19
20
|
};
|
|
20
21
|
|
|
21
|
-
const addModelEventHooks = (sequelize: Sequelize, modelTableMapping: ModelMapping): void => {
|
|
22
|
+
const addModelEventHooks = (sequelize: Sequelize, modelTableMapping: ModelMapping, events: Events, logger?: LoggerInstanceManager): void => {
|
|
23
|
+
if (!events) {
|
|
24
|
+
logger.warn('Events object must be provided to addModelEventHooks');
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
22
27
|
const updateEventToDimTable = (object: Model, isDelete = false) => {
|
|
28
|
+
const localLogger = logger || packageLogger;
|
|
23
29
|
try {
|
|
24
30
|
const objectToSend = object.get();
|
|
25
31
|
if (isDelete) {
|
|
@@ -38,10 +44,10 @@ const addModelEventHooks = (sequelize: Sequelize, modelTableMapping: ModelMappin
|
|
|
38
44
|
// @ts-expect-error the rawAttributes is typed as static, while it actually is on the instance level.
|
|
39
45
|
Object.keys(object.rawAttributes),
|
|
40
46
|
).catch((e) => {
|
|
41
|
-
|
|
47
|
+
localLogger.error('sendObject error', { tableName, eventVersion, e });
|
|
42
48
|
});
|
|
43
49
|
} catch (e) {
|
|
44
|
-
|
|
50
|
+
localLogger.error('dimTables error', { e });
|
|
45
51
|
}
|
|
46
52
|
};
|
|
47
53
|
|