@acodeninja/persist 3.0.0-next.7 → 3.0.0-next.9
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/package.json +1 -1
- package/src/SchemaCompiler.js +6 -2
- package/src/engine/StorageEngine.js +138 -24
- package/src/type/Model.js +35 -2
package/package.json
CHANGED
package/src/SchemaCompiler.js
CHANGED
@@ -79,7 +79,11 @@ class SchemaCompiler {
|
|
79
79
|
schema.properties[name].items.format = property?._items._format;
|
80
80
|
}
|
81
81
|
|
82
|
-
|
82
|
+
const prop = typeof property?._items === 'function' &&
|
83
|
+
!/^class/.test(Function.prototype.toString.call(property?._items)) ?
|
84
|
+
property?._items() : property?._items;
|
85
|
+
|
86
|
+
if (Type.Model.isModel(prop)) {
|
83
87
|
schema.properties[name].items = {
|
84
88
|
type: 'object',
|
85
89
|
additionalProperties: false,
|
@@ -87,7 +91,7 @@ class SchemaCompiler {
|
|
87
91
|
properties: {
|
88
92
|
id: {
|
89
93
|
type: 'string',
|
90
|
-
pattern: `^${
|
94
|
+
pattern: `^${prop.toString()}/[A-Z0-9]+$`,
|
91
95
|
},
|
92
96
|
},
|
93
97
|
};
|
@@ -86,7 +86,7 @@ export default class StorageEngine {
|
|
86
86
|
|
87
87
|
await this._putSearchIndex(modelConstructor, {
|
88
88
|
...index || {},
|
89
|
-
...Object.fromEntries(models.map(m => [m.id, m.
|
89
|
+
...Object.fromEntries(models.map(m => [m.id, m.toSearchData()])),
|
90
90
|
});
|
91
91
|
})),
|
92
92
|
]);
|
@@ -108,26 +108,118 @@ export default class StorageEngine {
|
|
108
108
|
}
|
109
109
|
|
110
110
|
/**
|
111
|
-
*
|
111
|
+
* Delete a model and update indexes that reference it
|
112
112
|
* @param {Type.Model} model
|
113
113
|
* @throws {ModelNotRegisteredStorageEngineError}
|
114
114
|
* @throws {ModelNotFoundStorageEngineError}
|
115
115
|
*/
|
116
116
|
async delete(model) {
|
117
|
-
|
118
|
-
|
117
|
+
const modelsToDelete = [];
|
118
|
+
const modelsToPut = [];
|
119
|
+
const indexCache = {};
|
120
|
+
const indexActions = {};
|
121
|
+
const searchIndexCache = {};
|
122
|
+
const searchIndexActions = {};
|
123
|
+
|
124
|
+
const processModel = async (modelToProcess) => {
|
125
|
+
if (!Object.keys(this.models).includes(modelToProcess.constructor.name))
|
126
|
+
throw new ModelNotRegisteredStorageEngineError(modelToProcess, this);
|
119
127
|
|
120
|
-
|
128
|
+
const currentModel = await this.get(model.id);
|
121
129
|
|
122
|
-
|
130
|
+
modelsToDelete.push(currentModel.id);
|
123
131
|
|
124
|
-
|
132
|
+
const modelToProcessConstructor = this.getModelConstructorFromId(modelToProcess.id);
|
133
|
+
indexActions[modelToProcessConstructor] = indexActions[modelToProcessConstructor] ?? [];
|
134
|
+
searchIndexActions[modelToProcessConstructor] = searchIndexActions[modelToProcessConstructor] ?? [];
|
125
135
|
|
126
|
-
|
136
|
+
if (currentModel.constructor.indexedPropertiesResolved().length) {
|
137
|
+
indexActions[modelToProcessConstructor].push(['delete', modelToProcess]);
|
138
|
+
}
|
127
139
|
|
128
|
-
|
140
|
+
if (currentModel.constructor.searchProperties().length) {
|
141
|
+
searchIndexActions[modelToProcessConstructor].push(['delete', modelToProcess]);
|
142
|
+
}
|
143
|
+
|
144
|
+
const linkedModels = await this.getInstancesLinkedTo(modelToProcess, indexCache);
|
145
|
+
const links = this.getLinksFor(modelToProcess.constructor);
|
146
|
+
Object.values(Object.fromEntries(await Promise.all(
|
147
|
+
Object.entries(linkedModels)
|
148
|
+
.map(async ([constructor, updatableModels]) => [
|
149
|
+
constructor,
|
150
|
+
await Promise.all(updatableModels.map(m => this.get(m.id))),
|
151
|
+
]),
|
152
|
+
))).flat(1)
|
153
|
+
.forEach(m =>
|
154
|
+
Object.entries(links[m.constructor.name])
|
155
|
+
.forEach(([linkName, _]) => {
|
156
|
+
m[linkName] = undefined;
|
157
|
+
modelsToPut.push(m);
|
158
|
+
|
159
|
+
const modelToProcessConstructor = this.getModelConstructorFromId(m.id);
|
160
|
+
indexActions[modelToProcessConstructor].push(['reindex', m]);
|
161
|
+
|
162
|
+
if (m.constructor.searchProperties().length) {
|
163
|
+
const modelToProcessConstructor = this.getModelConstructorFromId(m.id);
|
164
|
+
searchIndexActions[modelToProcessConstructor].push(['reindex', m]);
|
165
|
+
}
|
166
|
+
}),
|
167
|
+
);
|
168
|
+
};
|
169
|
+
|
170
|
+
await processModel(model);
|
171
|
+
|
172
|
+
await Promise.all([
|
173
|
+
Promise.all(Object.entries(indexActions).map(async ([constructorName, actions]) => {
|
174
|
+
const modelConstructor = this.models[constructorName];
|
175
|
+
indexCache[constructorName] = indexCache[constructorName] ?? await this._getIndex(modelConstructor);
|
176
|
+
|
177
|
+
actions.forEach(([action, model]) => {
|
178
|
+
switch (action) {
|
179
|
+
case 'delete':
|
180
|
+
indexCache[constructorName] = _.omit(indexCache[constructorName], [model.id]);
|
181
|
+
break;
|
182
|
+
case 'reindex':
|
183
|
+
indexCache[constructorName] = {
|
184
|
+
...indexCache[constructorName],
|
185
|
+
[model.id]: model.toIndexData(),
|
186
|
+
};
|
187
|
+
break;
|
188
|
+
}
|
189
|
+
});
|
190
|
+
})),
|
191
|
+
Promise.all(Object.entries(searchIndexActions).map(async ([constructorName, actions]) => {
|
192
|
+
const modelConstructor = this.models[constructorName];
|
193
|
+
searchIndexCache[constructorName] = searchIndexCache[constructorName] ?? await this._getSearchIndex(modelConstructor);
|
194
|
+
|
195
|
+
actions.forEach(([action, model]) => {
|
196
|
+
switch (action) {
|
197
|
+
case 'delete':
|
198
|
+
searchIndexCache[constructorName] = _.omit(searchIndexCache[constructorName], [model.id]);
|
199
|
+
break;
|
200
|
+
case 'reindex':
|
201
|
+
searchIndexCache[constructorName] = {
|
202
|
+
...searchIndexCache[constructorName],
|
203
|
+
[model.id]: model.toSearchData(),
|
204
|
+
};
|
205
|
+
break;
|
206
|
+
}
|
207
|
+
});
|
208
|
+
})),
|
209
|
+
]);
|
129
210
|
|
130
|
-
await
|
211
|
+
await Promise.all([
|
212
|
+
Promise.all(modelsToDelete.map(m => this._deleteModel(m))),
|
213
|
+
Promise.all(modelsToPut.map(m => this._putModel(m))),
|
214
|
+
Promise.all(
|
215
|
+
Object.entries(indexCache)
|
216
|
+
.map(([constructorName, _]) => this._putIndex(this.models[constructorName], indexCache[constructorName])),
|
217
|
+
),
|
218
|
+
Promise.all(
|
219
|
+
Object.entries(searchIndexCache)
|
220
|
+
.map(([constructorName, _]) => this._putSearchIndex(this.models[constructorName], searchIndexCache[constructorName])),
|
221
|
+
),
|
222
|
+
]);
|
131
223
|
}
|
132
224
|
|
133
225
|
/**
|
@@ -144,6 +236,36 @@ export default class StorageEngine {
|
|
144
236
|
return constructor;
|
145
237
|
}
|
146
238
|
|
239
|
+
/**
|
240
|
+
* Get model instance that are directly linked to the given model in either direction
|
241
|
+
* @param {Type.Model} model
|
242
|
+
* @param {object} cache
|
243
|
+
* @return {Record<string, Record<string, Type.Model>>}
|
244
|
+
*/
|
245
|
+
async getInstancesLinkedTo(model, cache = {}) {
|
246
|
+
return Object.fromEntries(
|
247
|
+
Object.entries(
|
248
|
+
await Promise.all(
|
249
|
+
Object.entries(this.getLinksFor(model.constructor))
|
250
|
+
.map(([name, _]) =>
|
251
|
+
cache[name] ? Promise.resolve([name, Object.values(cache[name])]) :
|
252
|
+
this._getIndex(this.models[name])
|
253
|
+
.then(i => {
|
254
|
+
cache[name] = i;
|
255
|
+
return [name, Object.values(i)];
|
256
|
+
}),
|
257
|
+
),
|
258
|
+
).then(Object.fromEntries),
|
259
|
+
).map(([name, i]) => [
|
260
|
+
name,
|
261
|
+
i.map(i => Object.fromEntries(
|
262
|
+
Object.entries(i)
|
263
|
+
.filter(([name, property]) => name === 'id' || property?.id === model.id),
|
264
|
+
)).filter(i => Object.keys(i).length > 1),
|
265
|
+
]),
|
266
|
+
);
|
267
|
+
}
|
268
|
+
|
147
269
|
/**
|
148
270
|
* Get model classes that are directly linked to the given model in either direction
|
149
271
|
* @param {Type.Model.constructor} model
|
@@ -171,9 +293,9 @@ export default class StorageEngine {
|
|
171
293
|
registeredModelName,
|
172
294
|
propertyName,
|
173
295
|
typeof propertyType === 'function' &&
|
174
|
-
|
175
|
-
|
176
|
-
|
296
|
+
!/^class/.test(Function.prototype.toString.call(propertyType)) &&
|
297
|
+
!Type.Model.isModel(propertyType) ?
|
298
|
+
propertyType() : propertyType,
|
177
299
|
])
|
178
300
|
.filter(([_m, _p, type]) => Type.Model.isModel(type))
|
179
301
|
.map(([containingModel, propertyName, propertyType]) => ({
|
@@ -279,18 +401,14 @@ export default class StorageEngine {
|
|
279
401
|
|
280
402
|
|
281
403
|
/**
|
282
|
-
* Decide if two models indexable fields
|
404
|
+
* Decide if two models indexable fields are different
|
283
405
|
* @param {Type.Model} currentModel
|
284
406
|
* @param {Type.Model} modelToProcess
|
285
407
|
* @return {boolean}
|
286
408
|
* @private
|
287
409
|
*/
|
288
410
|
function indexedFieldsHaveChanged(currentModel, modelToProcess) {
|
289
|
-
return !currentModel ||
|
290
|
-
modelToProcess.constructor.indexedProperties()
|
291
|
-
.filter(field => JSON.stringify(_.get(currentModel, field)) !== JSON.stringify(_.get(modelToProcess, field)))
|
292
|
-
.length,
|
293
|
-
);
|
411
|
+
return !currentModel || JSON.stringify(currentModel.toIndexData()) !== JSON.stringify(modelToProcess.toIndexData());
|
294
412
|
}
|
295
413
|
|
296
414
|
/**
|
@@ -301,11 +419,7 @@ function indexedFieldsHaveChanged(currentModel, modelToProcess) {
|
|
301
419
|
* @private
|
302
420
|
*/
|
303
421
|
function searchableFieldsHaveChanged(currentModel, modelToProcess) {
|
304
|
-
return !currentModel ||
|
305
|
-
modelToProcess.constructor.searchProperties()
|
306
|
-
.filter(field => JSON.stringify(_.get(currentModel, field)) !== JSON.stringify(_.get(modelToProcess, field)))
|
307
|
-
.length,
|
308
|
-
);
|
422
|
+
return !currentModel || JSON.stringify(currentModel.toSearchData()) !== JSON.stringify(modelToProcess.toSearchData());
|
309
423
|
}
|
310
424
|
|
311
425
|
/**
|
package/src/type/Model.js
CHANGED
@@ -100,11 +100,11 @@ class Model {
|
|
100
100
|
|
101
101
|
/**
|
102
102
|
* Extracts data from the model based on the indexed properties defined in the class.
|
103
|
-
*
|
103
|
+
* Includes the ID of any linked models.
|
104
104
|
* @returns {Object} - A representation of the model's indexed data.
|
105
105
|
*/
|
106
106
|
toIndexData() {
|
107
|
-
return this._extractData(this.constructor.
|
107
|
+
return this._extractData(this.constructor.indexedPropertiesResolved());
|
108
108
|
}
|
109
109
|
|
110
110
|
/**
|
@@ -187,6 +187,39 @@ class Model {
|
|
187
187
|
return [];
|
188
188
|
}
|
189
189
|
|
190
|
+
/**
|
191
|
+
* Returns a list of properties that are indexed including links to other models.
|
192
|
+
*
|
193
|
+
* @returns {Array<string>} - The indexed properties.
|
194
|
+
* @abstract
|
195
|
+
* @static
|
196
|
+
*/
|
197
|
+
static indexedPropertiesResolved() {
|
198
|
+
return []
|
199
|
+
.concat(
|
200
|
+
Object.entries(this)
|
201
|
+
.filter(([_, type]) =>
|
202
|
+
this.isModel(
|
203
|
+
typeof type === 'function' &&
|
204
|
+
!/^class/.test(Function.prototype.toString.call(type)) ?
|
205
|
+
type() : type,
|
206
|
+
),
|
207
|
+
)
|
208
|
+
.map(([name, _]) => `${name}.id`),
|
209
|
+
)
|
210
|
+
.concat(
|
211
|
+
Object.entries(this)
|
212
|
+
.filter(([_, type]) =>
|
213
|
+
type?._type === 'array' && this.isModel(
|
214
|
+
typeof type._items === 'function' &&
|
215
|
+
!/^class/.test(Function.prototype.toString.call(type._items)) ?
|
216
|
+
type._items() : type._items,
|
217
|
+
),
|
218
|
+
).map(([name, _]) => `${name}.[*].id`),
|
219
|
+
)
|
220
|
+
.concat(this.indexedProperties());
|
221
|
+
}
|
222
|
+
|
190
223
|
/**
|
191
224
|
* Returns a list of properties used for search.
|
192
225
|
*
|