@acodeninja/persist 2.4.1-next.2 → 3.0.0-next.10

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.
@@ -0,0 +1,517 @@
1
+ import Type from '../type/index.js';
2
+ import _ from 'lodash';
3
+
4
+ export default class StorageEngine {
5
+ /**
6
+ * @param {Object} configuration
7
+ * @param {Array<Type.Model.constructor>?} models
8
+ */
9
+ constructor(configuration = {}, models = null) {
10
+ this.configuration = configuration;
11
+ this.models = Object.fromEntries((models ?? []).map(model => [model.name, model]));
12
+ }
13
+
14
+ /**
15
+ * Persists a model if it has changed, and updates all related models and their indexes
16
+ * @param {Type.Model} model
17
+ * @return {Promise<void>}
18
+ */
19
+ async put(model) {
20
+ const processedModels = [];
21
+ const modelsToPut = [];
22
+ const modelsToReindex = {};
23
+ const modelsToReindexSearch = {};
24
+
25
+ /**
26
+ * @param {Type.Model} modelToProcess
27
+ * @return {Promise<void>}
28
+ */
29
+ const processModel = async (modelToProcess) => {
30
+ if (processedModels.includes(modelToProcess.id))
31
+ return;
32
+
33
+ processedModels.push(modelToProcess.id);
34
+
35
+ if (!Object.keys(this.models).includes(modelToProcess.constructor.name))
36
+ throw new ModelNotRegisteredStorageEngineError(modelToProcess, this);
37
+
38
+ modelToProcess.validate();
39
+ const currentModel = await this.get(modelToProcess.id).catch(() => null);
40
+
41
+ const modelToProcessHasChanged = JSON.stringify(currentModel?.toData() || {}) !== JSON.stringify(modelToProcess.toData());
42
+
43
+ if (modelToProcessHasChanged) modelsToPut.push(modelToProcess);
44
+
45
+ if (
46
+ Boolean(modelToProcess.constructor.indexedProperties().length) &&
47
+ indexedFieldsHaveChanged(currentModel, modelToProcess)
48
+ ) {
49
+ const modelToProcessConstructor = this.getModelConstructorFromId(modelToProcess.id);
50
+ modelsToReindex[modelToProcessConstructor] = modelsToReindex[modelToProcessConstructor] || [];
51
+ modelsToReindex[modelToProcessConstructor].push(modelToProcess);
52
+ }
53
+
54
+ if (
55
+ Boolean(modelToProcess.constructor.searchProperties().length) &&
56
+ searchableFieldsHaveChanged(currentModel, modelToProcess)
57
+ ) {
58
+ const modelToProcessConstructor = this.getModelConstructorFromId(modelToProcess.id);
59
+ modelsToReindexSearch[modelToProcessConstructor] = modelsToReindexSearch[modelToProcessConstructor] || [];
60
+ modelsToReindexSearch[modelToProcessConstructor].push(modelToProcess);
61
+ }
62
+
63
+ for (const [field, value] of Object.entries(modelToProcess)) {
64
+ if (Type.Model.isModel(value)) {
65
+ await processModel(modelToProcess[field]);
66
+ }
67
+ }
68
+ };
69
+
70
+ await processModel(model);
71
+
72
+ await Promise.all([
73
+ Promise.all(modelsToPut.map(m => this._putModel(m.toData()))),
74
+ Promise.all(Object.entries(modelsToReindex).map(async ([constructorName, models]) => {
75
+ const modelConstructor = this.models[constructorName];
76
+ const index = await this._getIndex(modelConstructor);
77
+
78
+ await this._putIndex(modelConstructor, {
79
+ ...index || {},
80
+ ...Object.fromEntries(models.map(m => [m.id, m.toIndexData()])),
81
+ });
82
+ })),
83
+ Promise.all(Object.entries(modelsToReindexSearch).map(async ([constructorName, models]) => {
84
+ const modelConstructor = this.models[constructorName];
85
+ const index = await this._getSearchIndex(modelConstructor);
86
+
87
+ await this._putSearchIndex(modelConstructor, {
88
+ ...index || {},
89
+ ...Object.fromEntries(models.map(m => [m.id, m.toSearchData()])),
90
+ });
91
+ })),
92
+ ]);
93
+ }
94
+
95
+ /**
96
+ * Get a model by its id
97
+ * @param {string} modelId
98
+ * @throws {ModelNotFoundStorageEngineError}
99
+ * @return {Promise<Type.Model>}
100
+ */
101
+ get(modelId) {
102
+ try {
103
+ this.getModelConstructorFromId(modelId);
104
+ } catch (e) {
105
+ return Promise.reject(e);
106
+ }
107
+ return this._getModel(modelId);
108
+ }
109
+
110
+ /**
111
+ * Delete a model and update indexes that reference it
112
+ * @param {Type.Model} model
113
+ * @param {Array<string>} propagateTo - List of model ids that are expected to be deleted
114
+ * @throws {ModelNotRegisteredStorageEngineError}
115
+ * @throws {ModelNotFoundStorageEngineError}
116
+ */
117
+ async delete(model, propagateTo = []) {
118
+ const processedModels = [];
119
+ const modelsToDelete = [];
120
+ const modelsToPut = [];
121
+ const indexCache = {};
122
+ const indexActions = {};
123
+ const searchIndexCache = {};
124
+ const searchIndexActions = {};
125
+ const modelCache = {};
126
+
127
+ propagateTo.push(model.id);
128
+
129
+ /**
130
+ * Process a model for deletion
131
+ * @param {Type.Model} modelToProcess
132
+ * @return {Promise<void>}
133
+ */
134
+ const processModel = async (modelToProcess) => {
135
+ if (processedModels.includes(modelToProcess.id)) return;
136
+ processedModels.push(modelToProcess.id);
137
+
138
+ const modelsToProcess = [];
139
+ if (!Object.keys(this.models).includes(modelToProcess.constructor.name))
140
+ throw new ModelNotRegisteredStorageEngineError(modelToProcess, this);
141
+
142
+ const currentModel = modelCache[model.id] ?? await this.get(model.id);
143
+ modelCache[currentModel.id] = currentModel;
144
+
145
+ if (!modelsToDelete.includes(currentModel.id)) modelsToDelete.push(currentModel.id);
146
+
147
+ const modelToProcessConstructor = this.getModelConstructorFromId(modelToProcess.id);
148
+ indexActions[modelToProcessConstructor] = indexActions[modelToProcessConstructor] ?? [];
149
+ searchIndexActions[modelToProcessConstructor] = searchIndexActions[modelToProcessConstructor] ?? [];
150
+
151
+ if (currentModel.constructor.indexedPropertiesResolved().length) {
152
+ indexActions[modelToProcessConstructor].push(['delete', modelToProcess]);
153
+ }
154
+
155
+ if (currentModel.constructor.searchProperties().length) {
156
+ searchIndexActions[modelToProcessConstructor].push(['delete', modelToProcess]);
157
+ }
158
+
159
+ const linkedModels = await this.getInstancesLinkedTo(modelToProcess, indexCache);
160
+ const links = this.getLinksFor(modelToProcess.constructor);
161
+ Object.values(Object.fromEntries(await Promise.all(
162
+ Object.entries(linkedModels)
163
+ .map(async ([constructor, updatableModels]) => [
164
+ constructor,
165
+ await Promise.all(updatableModels.map(async m => {
166
+ const currentModel = modelCache[m.id] ?? await this.get(m.id);
167
+ modelCache[currentModel.id] = currentModel;
168
+ return currentModel;
169
+ })),
170
+ ]),
171
+ ))).flat(1)
172
+ .forEach(m =>
173
+ Object.entries(links[m.constructor.name])
174
+ .forEach(([linkName, modelConstructor]) => {
175
+ if ((
176
+ typeof modelConstructor[linkName] === 'function' &&
177
+ !/^class/.test(Function.prototype.toString.call(modelConstructor[linkName])) &&
178
+ !Type.Model.isModel(modelConstructor[linkName]) ?
179
+ modelConstructor[linkName]() : modelConstructor
180
+ )._required) {
181
+ if (!modelsToDelete.includes(m.id)) modelsToDelete.push(m.id);
182
+ modelsToProcess.push(m);
183
+ } else {
184
+ m[linkName] = undefined;
185
+ modelsToPut.push(m);
186
+
187
+ indexActions[this.getModelConstructorFromId(m.id)].push(['reindex', m]);
188
+
189
+ if (m.constructor.searchProperties().length) {
190
+ searchIndexActions[this.getModelConstructorFromId(m.id)].push(['reindex', m]);
191
+ }
192
+ }
193
+ }),
194
+ );
195
+
196
+ for (const model of modelsToProcess) {
197
+ await processModel(model);
198
+ }
199
+ };
200
+
201
+ await processModel(model);
202
+
203
+ const unrequestedDeletions = modelsToDelete.filter(m => !propagateTo.includes(m));
204
+ if (unrequestedDeletions.length) {
205
+ throw new DeleteHasUnintendedConsequencesStorageEngineError(model.id, {
206
+ willDelete: unrequestedDeletions,
207
+ });
208
+ }
209
+
210
+ await Promise.all([
211
+ Promise.all(Object.entries(indexActions).map(async ([constructorName, actions]) => {
212
+ const modelConstructor = this.models[constructorName];
213
+ indexCache[modelConstructor] = indexCache[modelConstructor] ?? await this._getIndex(modelConstructor);
214
+
215
+ actions.forEach(([action, actionModel]) => {
216
+ if (action === 'delete') {
217
+ indexCache[modelConstructor] = _.omit(indexCache[modelConstructor], [actionModel.id]);
218
+ }
219
+ if (action === 'reindex') {
220
+ indexCache[modelConstructor] = {
221
+ ...indexCache[modelConstructor],
222
+ [actionModel.id]: actionModel.toIndexData(),
223
+ };
224
+ }
225
+ });
226
+ })),
227
+ Promise.all(Object.entries(searchIndexActions).map(async ([constructorName, actions]) => {
228
+ const modelConstructor = this.models[constructorName];
229
+ searchIndexCache[modelConstructor] = searchIndexCache[modelConstructor] ?? await this._getSearchIndex(modelConstructor);
230
+
231
+ actions.forEach(([action, actionModel]) => {
232
+ if (action === 'delete') {
233
+ searchIndexCache[modelConstructor] = _.omit(searchIndexCache[modelConstructor], [actionModel.id]);
234
+ }
235
+ if (action === 'reindex') {
236
+ searchIndexCache[modelConstructor] = {
237
+ ...searchIndexCache[modelConstructor],
238
+ [actionModel.id]: actionModel.toSearchData(),
239
+ };
240
+ }
241
+ });
242
+ })),
243
+ ]);
244
+
245
+ await Promise.all([
246
+ Promise.all(modelsToDelete.map(m => this._deleteModel(m))),
247
+ Promise.all(modelsToPut.map(m => this._putModel(m))),
248
+ Promise.all(
249
+ Object.entries(indexCache)
250
+ .map(([constructorName, index]) => this._putIndex(this.models[constructorName], index)),
251
+ ),
252
+ Promise.all(
253
+ Object.entries(searchIndexCache)
254
+ .map(([constructorName, index]) => this._putSearchIndex(this.models[constructorName], index)),
255
+ ),
256
+ ]);
257
+ }
258
+
259
+ /**
260
+ * Get the model constructor from a model id
261
+ * @param {string} modelId
262
+ * @return {Model.constructor}
263
+ */
264
+ getModelConstructorFromId(modelId) {
265
+ const modelName = modelId.split('/')[0];
266
+ const constructor = this.models[modelName];
267
+
268
+ if (!constructor) throw new ModelNotRegisteredStorageEngineError(modelName, this);
269
+
270
+ return constructor;
271
+ }
272
+
273
+ /**
274
+ * Get model instance that are directly linked to the given model in either direction
275
+ * @param {Type.Model} model
276
+ * @param {object} cache
277
+ * @return {Record<string, Record<string, Type.Model>>}
278
+ */
279
+ async getInstancesLinkedTo(model, cache = {}) {
280
+ return Object.fromEntries(
281
+ Object.entries(
282
+ await Promise.all(
283
+ Object.entries(this.getLinksFor(model.constructor))
284
+ .map(([name, _index]) =>
285
+ cache[name] ? Promise.resolve([name, Object.values(cache[name])]) :
286
+ this._getIndex(this.models[name])
287
+ .then(i => {
288
+ cache[name] = i;
289
+ return [name, Object.values(i)];
290
+ }),
291
+ ),
292
+ ).then(Object.fromEntries),
293
+ ).map(([name, index]) => [
294
+ name,
295
+ index.map(item => Object.fromEntries(
296
+ Object.entries(item)
297
+ .filter(([propertyName, property]) => propertyName === 'id' || property?.id === model.id),
298
+ )).filter(item => Object.keys(item).length > 1),
299
+ ]),
300
+ );
301
+ }
302
+
303
+ /**
304
+ * Get model classes that are directly linked to the given model in either direction
305
+ * @param {Type.Model.constructor} model
306
+ * @return {Record<string, Record<string, Type.Model.constructor>>}
307
+ */
308
+ getLinksFor(model) {
309
+ return Object.fromEntries(
310
+ Object.entries(this.getAllModelLinks())
311
+ .filter(([modelName, links]) =>
312
+ model.name === modelName ||
313
+ Object.values(links).some((link) => link.name === model.name),
314
+ ),
315
+ );
316
+ }
317
+
318
+ /**
319
+ * Get all model links
320
+ * @return {Record<string, Record<string, Type.Model.constructor>>}
321
+ */
322
+ getAllModelLinks() {
323
+ return Object.entries(this.models)
324
+ .map(([registeredModelName, registeredModelClass]) =>
325
+ Object.entries(registeredModelClass)
326
+ .map(([propertyName, propertyType]) => [
327
+ registeredModelName,
328
+ propertyName,
329
+ typeof propertyType === 'function' &&
330
+ !/^class/.test(Function.prototype.toString.call(propertyType)) &&
331
+ !Type.Model.isModel(propertyType) ?
332
+ propertyType() : propertyType,
333
+ ])
334
+ .filter(([_m, _p, type]) => Type.Model.isModel(type))
335
+ .map(([containingModel, propertyName, propertyType]) => ({
336
+ containingModel,
337
+ propertyName,
338
+ propertyType,
339
+ })),
340
+ )
341
+ .flat()
342
+ .reduce((accumulator, {containingModel, propertyName, propertyType}) => ({
343
+ ...accumulator,
344
+ [containingModel]: {
345
+ ...accumulator[containingModel] || {},
346
+ [propertyName]: propertyType,
347
+ },
348
+ }), {});
349
+ }
350
+
351
+ /**
352
+ * Update a model
353
+ * @param {Model} _model
354
+ * @throws MethodNotImplementedStorageEngineError
355
+ * @return Promise<void>
356
+ */
357
+ _putModel(_model) {
358
+ return Promise.reject(new MethodNotImplementedStorageEngineError('_putModel', this));
359
+ }
360
+
361
+ /**
362
+ * Get a model
363
+ * @param {string} _id
364
+ * @throws MethodNotImplementedStorageEngineError
365
+ * @throws ModelNotFoundStorageEngineError
366
+ * @return Promise<Model>
367
+ */
368
+ _getModel(_id) {
369
+ return Promise.reject(new MethodNotImplementedStorageEngineError('_getModel', this));
370
+ }
371
+
372
+ /**
373
+ * Delete a model
374
+ * @param {string} _id
375
+ * @throws MethodNotImplementedStorageEngineError
376
+ * @throws ModelNotFoundStorageEngineError
377
+ * @return Promise<void>
378
+ */
379
+ _deleteModel(_id) {
380
+ return Promise.reject(new MethodNotImplementedStorageEngineError('_deleteModel', this));
381
+ }
382
+
383
+ /**
384
+ * Get a model's index data
385
+ * @param {Model.constructor} _modelConstructor
386
+ * @throws MethodNotImplementedStorageEngineError
387
+ * @return Promise<void>
388
+ */
389
+ _getIndex(_modelConstructor) {
390
+ return Promise.reject(new MethodNotImplementedStorageEngineError('_getIndex', this));
391
+ }
392
+
393
+ /**
394
+ * Put a model's index data
395
+ * @param {Model.constructor} _modelConstructor
396
+ * @param {object} _data
397
+ * @throws MethodNotImplementedStorageEngineError
398
+ * @return Promise<void>
399
+ */
400
+ _putIndex(_modelConstructor, _data) {
401
+ return Promise.reject(new MethodNotImplementedStorageEngineError('_putIndex', this));
402
+ }
403
+
404
+ /**
405
+ * Get a model's raw search index data
406
+ * @param {Model.constructor} _modelConstructor
407
+ * @throws MethodNotImplementedStorageEngineError
408
+ * @return Promise<void>
409
+ */
410
+ _getSearchIndex(_modelConstructor) {
411
+ return Promise.reject(new MethodNotImplementedStorageEngineError('_getSearchIndex', this));
412
+ }
413
+
414
+ /**
415
+ * Get a model's raw search index data
416
+ * @param {Model.constructor} _modelConstructor
417
+ * @throws MethodNotImplementedStorageEngineError
418
+ * @return Promise<void>
419
+ */
420
+ _getSearchIndexCompiled(_modelConstructor) {
421
+ return Promise.reject(new MethodNotImplementedStorageEngineError('_getSearchIndexCompiled', this));
422
+ }
423
+
424
+ /**
425
+ * Put a model's raw and compiled search index data
426
+ * @param {Model.constructor} _modelConstructor
427
+ * @param {object} _data
428
+ * @throws MethodNotImplementedStorageEngineError
429
+ * @return Promise<void>
430
+ */
431
+ _putSearchIndex(_modelConstructor, _data) {
432
+ return Promise.reject(new MethodNotImplementedStorageEngineError('_putSearchIndex', this));
433
+ }
434
+ }
435
+
436
+
437
+ /**
438
+ * Decide if two models indexable fields are different
439
+ * @param {Type.Model} currentModel
440
+ * @param {Type.Model} modelToProcess
441
+ * @return {boolean}
442
+ * @private
443
+ */
444
+ function indexedFieldsHaveChanged(currentModel, modelToProcess) {
445
+ return !currentModel || JSON.stringify(currentModel.toIndexData()) !== JSON.stringify(modelToProcess.toIndexData());
446
+ }
447
+
448
+ /**
449
+ * Decide if two models searchable fields have changed
450
+ * @param {Type.Model} currentModel
451
+ * @param {Type.Model} modelToProcess
452
+ * @return {boolean}
453
+ * @private
454
+ */
455
+ function searchableFieldsHaveChanged(currentModel, modelToProcess) {
456
+ return !currentModel || JSON.stringify(currentModel.toSearchData()) !== JSON.stringify(modelToProcess.toSearchData());
457
+ }
458
+
459
+ /**
460
+ * @class StorageEngineError
461
+ * @extends Error
462
+ */
463
+ export class StorageEngineError extends Error {
464
+ }
465
+
466
+ /**
467
+ * @class ModelNotRegisteredStorageEngineError
468
+ * @extends StorageEngineError
469
+ */
470
+ export class ModelNotRegisteredStorageEngineError extends StorageEngineError {
471
+ /**
472
+ * @param {Type.Model} model
473
+ * @param {StorageEngine} storageEngine
474
+ */
475
+ constructor(model, storageEngine) {
476
+ const modelName = typeof model === 'string' ? model : model.constructor.name;
477
+ super(`The model ${modelName} is not registered in the storage engine ${storageEngine.constructor.name}`);
478
+ }
479
+ }
480
+
481
+ /**
482
+ * @class MethodNotImplementedStorageEngineError
483
+ * @extends StorageEngineError
484
+ */
485
+ export class MethodNotImplementedStorageEngineError extends StorageEngineError {
486
+ /**
487
+ * @param {string} method
488
+ * @param {StorageEngine} storageEngine
489
+ */
490
+ constructor(method, storageEngine) {
491
+ super(`The method ${method} is not implemented in the storage engine ${storageEngine.constructor.name}`);
492
+ }
493
+ }
494
+
495
+ /**
496
+ * @class ModelNotFoundStorageEngineError
497
+ * @extends StorageEngineError
498
+ */
499
+ export class ModelNotFoundStorageEngineError extends StorageEngineError {
500
+ /**
501
+ * @param {string} modelId
502
+ */
503
+ constructor(modelId) {
504
+ super(`The model ${modelId} was not found`);
505
+ }
506
+ }
507
+
508
+ export class DeleteHasUnintendedConsequencesStorageEngineError extends StorageEngineError {
509
+ /**
510
+ * @param {string} modelId
511
+ * @param {object} consequences
512
+ */
513
+ constructor(modelId, consequences) {
514
+ super(`Deleting ${modelId} has unintended consequences`);
515
+ this.consequences = consequences;
516
+ }
517
+ }
@@ -1,35 +1,35 @@
1
- import Engine, { EngineError, MissConfiguredError } from './Engine.js';
1
+ import StorageEngine, { EngineError, MissConfiguredError } from './StorageEngine.js';
2
2
  import { dirname, join } from 'node:path';
3
3
  import fs from 'node:fs/promises';
4
4
 
5
5
  /**
6
- * Custom error class for FileEngine-related errors.
6
+ * Custom error class for FileStorageEngine-related errors.
7
7
  * Extends the base `EngineError` class.
8
8
  */
9
- class FileEngineError extends EngineError {}
9
+ class FileStorageEngineError extends EngineError {}
10
10
 
11
11
  /**
12
- * Error thrown when writing to a file fails in `FileEngine`.
13
- * Extends the `FileEngineError` class.
12
+ * Error thrown when writing to a file fails in `FileStorageEngine`.
13
+ * Extends the `FileStorageEngineError` class.
14
14
  */
15
- class FailedWriteFileEngineError extends FileEngineError {}
15
+ class FailedWriteFileStorageEngineError extends FileStorageEngineError {}
16
16
 
17
17
  /**
18
- * `FileEngine` class extends the base `Engine` class to implement
18
+ * `FileStorageEngine` class extends the base `StorageEngine` class to implement
19
19
  * file system-based storage and retrieval of model data.
20
20
  *
21
- * @class FileEngine
22
- * @extends Engine
21
+ * @class FileStorageEngine
22
+ * @extends StorageEngine
23
23
  */
24
- class FileEngine extends Engine {
24
+ class FileStorageEngine extends StorageEngine {
25
25
  /**
26
- * Configures the FileEngine with a given configuration object.
26
+ * Configures the FileStorageEngine with a given configuration object.
27
27
  * Adds default `filesystem` configuration if not provided.
28
28
  *
29
- * @param {Object} configuration - Configuration settings for FileEngine.
29
+ * @param {Object} configuration - Configuration settings for FileStorageEngine.
30
30
  * @param {Object} [configuration.filesystem] - Custom filesystem module (default: Node.js fs/promises).
31
31
  * @param {Object} [configuration.path] - The absolute path on the filesystem to write models to.
32
- * @returns {FileEngine} A configured instance of FileEngine.
32
+ * @returns {FileStorageEngine} A configured instance of FileStorageEngine.
33
33
  */
34
34
  static configure(configuration) {
35
35
  if (!configuration.filesystem) {
@@ -39,7 +39,7 @@ class FileEngine extends Engine {
39
39
  }
40
40
 
41
41
  /**
42
- * Checks if the FileEngine has been configured correctly.
42
+ * Checks if the FileStorageEngine has been configured correctly.
43
43
  * Ensures that `path` and `filesystem` settings are present.
44
44
  *
45
45
  * @throws {MissConfiguredError} Throws if required configuration is missing.
@@ -94,7 +94,7 @@ class FileEngine extends Engine {
94
94
  * Saves a model to the file system.
95
95
  *
96
96
  * @param {Model} model - The model to save.
97
- * @throws {FailedWriteFileEngineError} Throws if the model cannot be written to the file system.
97
+ * @throws {FailedWriteFileStorageEngineError} Throws if the model cannot be written to the file system.
98
98
  */
99
99
  static async putModel(model) {
100
100
  const filePath = join(this.configuration.path, `${model.id}.json`);
@@ -102,7 +102,7 @@ class FileEngine extends Engine {
102
102
  await this.configuration.filesystem.mkdir(dirname(filePath), { recursive: true });
103
103
  await this.configuration.filesystem.writeFile(filePath, JSON.stringify(model.toData()));
104
104
  } catch (error) {
105
- throw new FailedWriteFileEngineError(`Failed to put file://${filePath}`, error);
105
+ throw new FailedWriteFileStorageEngineError(`Failed to put file://${filePath}`, error);
106
106
  }
107
107
  }
108
108
 
@@ -110,9 +110,16 @@ class FileEngine extends Engine {
110
110
  * Saves the index for multiple models to the file system.
111
111
  *
112
112
  * @param {Object} index - An object where keys are locations and values are key value pairs of models and their ids.
113
- * @throws {FailedWriteFileEngineError} Throws if the index cannot be written to the file system.
113
+ * @throws {FailedWriteFileStorageEngineError} Throws if the index cannot be written to the file system.
114
114
  */
115
115
  static async putIndex(index) {
116
+ /**
117
+ * Process an index of models
118
+ * @param {string} location
119
+ * @param {Array<Model>} models
120
+ * @throws FailedWriteFileStorageEngineError
121
+ * @return {Promise<void>}
122
+ */
116
123
  const processIndex = async (location, models) => {
117
124
  const filePath = join(this.configuration.path, location, '_index.json');
118
125
  const currentIndex = JSON.parse((await this.configuration.filesystem.readFile(filePath).catch(() => '{}')).toString());
@@ -125,7 +132,7 @@ class FileEngine extends Engine {
125
132
  ),
126
133
  }));
127
134
  } catch (error) {
128
- throw new FailedWriteFileEngineError(`Failed to put file://${filePath}`, error);
135
+ throw new FailedWriteFileStorageEngineError(`Failed to put file://${filePath}`, error);
129
136
  }
130
137
  };
131
138
 
@@ -175,14 +182,14 @@ class FileEngine extends Engine {
175
182
  *
176
183
  * @param {Model.constructor} model - The model for which the compiled search index is saved.
177
184
  * @param {Object} compiledIndex - The compiled search index to save.
178
- * @throws {FailedWriteFileEngineError} Throws if the compiled index cannot be written to the file system.
185
+ * @throws {FailedWriteFileStorageEngineError} Throws if the compiled index cannot be written to the file system.
179
186
  */
180
187
  static async putSearchIndexCompiled(model, compiledIndex) {
181
188
  const filePath = join(this.configuration.path, model.toString(), '_search_index.json');
182
189
  try {
183
190
  await this.configuration.filesystem.writeFile(filePath, JSON.stringify(compiledIndex));
184
191
  } catch (error) {
185
- throw new FailedWriteFileEngineError(`Failed to put file://${filePath}`, error);
192
+ throw new FailedWriteFileStorageEngineError(`Failed to put file://${filePath}`, error);
186
193
  }
187
194
  }
188
195
 
@@ -191,16 +198,16 @@ class FileEngine extends Engine {
191
198
  *
192
199
  * @param {Model.constructor} model - The model for which the raw search index is saved.
193
200
  * @param {Object} rawIndex - The raw search index to save.
194
- * @throws {FailedWriteFileEngineError} Throws if the raw index cannot be written to the file system.
201
+ * @throws {FailedWriteFileStorageEngineError} Throws if the raw index cannot be written to the file system.
195
202
  */
196
203
  static async putSearchIndexRaw(model, rawIndex) {
197
204
  const filePath = join(this.configuration.path, model.toString(), '_search_index_raw.json');
198
205
  try {
199
206
  await this.configuration.filesystem.writeFile(filePath, JSON.stringify(rawIndex));
200
207
  } catch (error) {
201
- throw new FailedWriteFileEngineError(`Failed to put file://${filePath}`, error);
208
+ throw new FailedWriteFileStorageEngineError(`Failed to put file://${filePath}`, error);
202
209
  }
203
210
  }
204
211
  }
205
212
 
206
- export default FileEngine;
213
+ export default FileStorageEngine;