@autofleet/matmon 2.0.0-beta-42 → 2.0.0-beta-44
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.
|
@@ -17,8 +17,26 @@ const util_1 = require("util");
|
|
|
17
17
|
const { AF_SERVICE_NAME } = process.env;
|
|
18
18
|
const INVALIDATION_HOOKS = ['afterSave', 'afterUpdate', 'afterDestroy'];
|
|
19
19
|
const BULK_HOOKS = ['beforeBulkUpdate', 'beforeBulkDestroy'];
|
|
20
|
-
const generateInstanceKey = (modelOptions,
|
|
20
|
+
const generateInstanceKey = (modelOptions, id) => `${AF_SERVICE_NAME}:${modelOptions.name}_${id}_INCLUDING_${modelOptions.associations.map(a => a.name).join('_')}`;
|
|
21
21
|
const generateDependencyKey = (modelName, associationName, associationId) => `${AF_SERVICE_NAME}:${modelName}_${associationName}_${associationId}_DEPENDENCIES`;
|
|
22
|
+
const getAssociation = (instance, associationAlias, associationId) => {
|
|
23
|
+
if (Array.isArray(instance[associationAlias])) {
|
|
24
|
+
return instance[associationAlias].find(association => association.id === associationId);
|
|
25
|
+
}
|
|
26
|
+
return instance[associationAlias];
|
|
27
|
+
};
|
|
28
|
+
const getInstanceDependencyKeys = (modelOptions, instance) => {
|
|
29
|
+
const keys = modelOptions.associations
|
|
30
|
+
.filter(association => instance[association.alias])
|
|
31
|
+
.map((association) => {
|
|
32
|
+
if (Array.isArray(instance[association.alias])) {
|
|
33
|
+
return instance[association.alias]
|
|
34
|
+
.map(a => generateDependencyKey(modelOptions.name, a.name, instance[a.alias].id));
|
|
35
|
+
}
|
|
36
|
+
return [generateDependencyKey(modelOptions.name, association.name, instance[association.alias].id)];
|
|
37
|
+
});
|
|
38
|
+
return keys.reduce((flattenArray, array) => flattenArray.concat(array), []);
|
|
39
|
+
};
|
|
22
40
|
const handleTransactionHook = (instance, options, func) => {
|
|
23
41
|
const { transaction } = options;
|
|
24
42
|
if (transaction) {
|
|
@@ -42,10 +60,8 @@ class SequelizeAdapter {
|
|
|
42
60
|
}
|
|
43
61
|
}
|
|
44
62
|
injectGetWithCacheFunction(cache, modelOptions) {
|
|
45
|
-
const addDependencies = (
|
|
46
|
-
const dependencyKeys = modelOptions
|
|
47
|
-
.filter(association => instance[association.alias])
|
|
48
|
-
.map(association => generateDependencyKey(modelName, association.name, instance[association.alias].id));
|
|
63
|
+
const addDependencies = (instance) => __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
const dependencyKeys = getInstanceDependencyKeys(modelOptions, instance);
|
|
49
65
|
const instanceKey = generateInstanceKey(modelOptions, instance.id);
|
|
50
66
|
this.debug('Adding dependencies', { instanceKey, dependencyKeys });
|
|
51
67
|
const addDependenciesMulti = cache.getClient().multi();
|
|
@@ -63,7 +79,7 @@ class SequelizeAdapter {
|
|
|
63
79
|
this.debug('Value from DB', { value: value || 'not found', cacheKey });
|
|
64
80
|
const [setRes] = yield Promise.all([
|
|
65
81
|
cache.getClient().setAsync(cacheKey, JSON.stringify(value)),
|
|
66
|
-
value && addDependencies(
|
|
82
|
+
value && addDependencies(value),
|
|
67
83
|
]);
|
|
68
84
|
}
|
|
69
85
|
else {
|
|
@@ -73,10 +89,8 @@ class SequelizeAdapter {
|
|
|
73
89
|
});
|
|
74
90
|
}
|
|
75
91
|
addInvalidationHooks(cache, modelOptions) {
|
|
76
|
-
const invalidateModelInstance = (
|
|
77
|
-
const dependencyKeys = modelOptions
|
|
78
|
-
.filter(association => instance[association.alias])
|
|
79
|
-
.map(association => generateDependencyKey(modelName, association.name, instance[association.alias].id));
|
|
92
|
+
const invalidateModelInstance = (instance) => __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
const dependencyKeys = getInstanceDependencyKeys(modelOptions, instance);
|
|
80
94
|
const instanceKey = generateInstanceKey(modelOptions, instance.id);
|
|
81
95
|
this.debug('Removing dependencies', { instance, instanceKey, dependencyKeys });
|
|
82
96
|
const removeMulti = cache.getClient().multi();
|
|
@@ -106,7 +120,7 @@ class SequelizeAdapter {
|
|
|
106
120
|
return removeMultiAsync();
|
|
107
121
|
});
|
|
108
122
|
const model = this.getModel(modelOptions.name);
|
|
109
|
-
INVALIDATION_HOOKS.map(hook => model.addHook(hook, (instance, options) => handleTransactionHook(instance, options, instance => invalidateModelInstance(
|
|
123
|
+
INVALIDATION_HOOKS.map(hook => model.addHook(hook, (instance, options) => handleTransactionHook(instance, options, instance => invalidateModelInstance(instance))));
|
|
110
124
|
BULK_HOOKS.map(hook => model.addHook(hook, options => options.individualHook = true));
|
|
111
125
|
modelOptions.associations.map(association => {
|
|
112
126
|
const associationModel = this.getModel(association.name);
|