@autofleet/matmon 2.0.0-beta-44 → 2.0.0-beta-45
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.
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import RedisCache from '../redis';
|
|
2
2
|
export interface ModelOptions {
|
|
3
3
|
name: string;
|
|
4
|
-
associations:
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
associations: AssociationOptions[];
|
|
5
|
+
}
|
|
6
|
+
export interface AssociationOptions {
|
|
7
|
+
name: string;
|
|
8
|
+
alias: string;
|
|
9
|
+
accessKey?: string;
|
|
10
|
+
associations?: AssociationOptions[];
|
|
8
11
|
}
|
|
9
12
|
export interface Adapter {
|
|
10
13
|
ormInstance: any;
|
|
@@ -19,15 +19,15 @@ const INVALIDATION_HOOKS = ['afterSave', 'afterUpdate', 'afterDestroy'];
|
|
|
19
19
|
const BULK_HOOKS = ['beforeBulkUpdate', 'beforeBulkDestroy'];
|
|
20
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,
|
|
23
|
-
if (Array.isArray(instance[
|
|
24
|
-
return instance[
|
|
22
|
+
const getAssociation = (instance, associationOptions, associationId) => {
|
|
23
|
+
if (Array.isArray(instance[associationOptions.alias])) {
|
|
24
|
+
return instance[associationOptions.alias].find(association => association[associationOptions.accessKey || 'id'] === associationId);
|
|
25
25
|
}
|
|
26
|
-
return instance[
|
|
26
|
+
return instance[associationOptions.alias];
|
|
27
27
|
};
|
|
28
28
|
const getInstanceDependencyKeys = (modelOptions, instance) => {
|
|
29
29
|
const keys = modelOptions.associations
|
|
30
|
-
.filter(association => instance[association.alias])
|
|
30
|
+
.filter((association) => instance[association.alias])
|
|
31
31
|
.map((association) => {
|
|
32
32
|
if (Array.isArray(instance[association.alias])) {
|
|
33
33
|
return instance[association.alias]
|
|
@@ -99,8 +99,8 @@ class SequelizeAdapter {
|
|
|
99
99
|
removeMulti.del(instanceKey);
|
|
100
100
|
return removeMultiAsync();
|
|
101
101
|
});
|
|
102
|
-
const invalidateModelInstanceByAssociation = (
|
|
103
|
-
const dependentInstancesKeys = yield cache.getClient().smembersAsync(generateDependencyKey(
|
|
102
|
+
const invalidateModelInstanceByAssociation = (association, associationId) => __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
const dependentInstancesKeys = yield cache.getClient().smembersAsync(generateDependencyKey(modelOptions.name, association, associationId));
|
|
104
104
|
this.debug('Invalidating dependent instances', { dependentInstancesKeys });
|
|
105
105
|
const removeMulti = cache.getClient().multi();
|
|
106
106
|
const removeMultiAsync = util_1.promisify(removeMulti.exec).bind(removeMulti);
|
|
@@ -111,7 +111,7 @@ class SequelizeAdapter {
|
|
|
111
111
|
}
|
|
112
112
|
const dependencyKeys = modelOptions.associations
|
|
113
113
|
.filter(association => instance[association.alias])
|
|
114
|
-
.map(association => generateDependencyKey(
|
|
114
|
+
.map(association => generateDependencyKey(modelOptions.name, association.name, instance[association.alias].id));
|
|
115
115
|
dependencyKeys.reduce((multi, key) => multi.srem(key, instanceKey), removeMulti);
|
|
116
116
|
removeMulti.del(instanceKey);
|
|
117
117
|
return dependencyKeys;
|
|
@@ -124,7 +124,7 @@ class SequelizeAdapter {
|
|
|
124
124
|
BULK_HOOKS.map(hook => model.addHook(hook, options => options.individualHook = true));
|
|
125
125
|
modelOptions.associations.map(association => {
|
|
126
126
|
const associationModel = this.getModel(association.name);
|
|
127
|
-
INVALIDATION_HOOKS.map(hook => associationModel.addHook(hook, (instance, options) => handleTransactionHook(instance, options, ({ id }) => invalidateModelInstanceByAssociation(
|
|
127
|
+
INVALIDATION_HOOKS.map(hook => associationModel.addHook(hook, (instance, options) => handleTransactionHook(instance, options, ({ id }) => invalidateModelInstanceByAssociation(association.name, id))));
|
|
128
128
|
BULK_HOOKS.map(hook => associationModel.addHook(hook, options => options.individualHook = true));
|
|
129
129
|
});
|
|
130
130
|
}
|