@autofleet/matmon 2.0.0-beta-43 → 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.
@@ -19,6 +19,24 @@ 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, 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 = (modelName, instance) => __awaiter(this, void 0, void 0, function* () {
46
- const dependencyKeys = modelOptions.associations
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(modelOptions.name, value),
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 = (modelName, instance) => __awaiter(this, void 0, void 0, function* () {
77
- const dependencyKeys = modelOptions.associations
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(modelOptions.name, instance))));
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/matmon",
3
- "version": "2.0.0-beta-43",
3
+ "version": "2.0.0-beta-44",
4
4
  "description": "manage cache",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",