@acodeninja/persist 3.0.0-next.20 → 3.0.0-next.22

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acodeninja/persist",
3
- "version": "3.0.0-next.20",
3
+ "version": "3.0.0-next.22",
4
4
  "description": "A JSON based data modelling and persistence module with alternate storage mechanisms.",
5
5
  "type": "module",
6
6
  "scripts": {
package/src/Connection.js CHANGED
@@ -189,11 +189,12 @@ export default class Connection {
189
189
 
190
190
  if (modelToProcessHasChanged) modelsToPut.push(modelToProcess);
191
191
 
192
+ const modelToProcessConstructor = this.#getModelConstructorFromId(modelToProcess.id).name;
193
+
192
194
  if (
193
195
  Boolean(modelToProcess.constructor.indexedProperties().length) &&
194
196
  (!currentModel || !_.isEqual(currentModel.toIndexData(), modelToProcess.toIndexData()))
195
197
  ) {
196
- const modelToProcessConstructor = this.#getModelConstructorFromId(modelToProcess.id);
197
198
  modelsToReindex[modelToProcessConstructor] = modelsToReindex[modelToProcessConstructor] || [];
198
199
  modelsToReindex[modelToProcessConstructor].push(modelToProcess);
199
200
  }
@@ -202,7 +203,6 @@ export default class Connection {
202
203
  Boolean(modelToProcess.constructor.searchProperties().length) &&
203
204
  (!currentModel || !_.isEqual(currentModel.toSearchData(), modelToProcess.toSearchData()))
204
205
  ) {
205
- const modelToProcessConstructor = this.#getModelConstructorFromId(modelToProcess.id);
206
206
  modelsToReindexSearch[modelToProcessConstructor] = modelsToReindexSearch[modelToProcessConstructor] || [];
207
207
  modelsToReindexSearch[modelToProcessConstructor].push(modelToProcess);
208
208
  }
@@ -223,7 +223,7 @@ export default class Connection {
223
223
  const index = await this.#storage.getIndex(modelConstructor);
224
224
 
225
225
  await this.#storage.putIndex(modelConstructor, {
226
- ...index || {},
226
+ ...index,
227
227
  ...Object.fromEntries(models.map(m => [m.id, m.toIndexData()])),
228
228
  });
229
229
  })),
@@ -232,7 +232,7 @@ export default class Connection {
232
232
  const index = await this.#storage.getSearchIndex(modelConstructor);
233
233
 
234
234
  await this.#storage.putSearchIndex(modelConstructor, {
235
- ...index || {},
235
+ ...index,
236
236
  ...Object.fromEntries(models.map(m => [m.id, m.toSearchData()])),
237
237
  });
238
238
  })),
@@ -277,7 +277,8 @@ export default class Connection {
277
277
 
278
278
  if (!modelsToDelete.includes(currentModel.id)) modelsToDelete.push(currentModel.id);
279
279
 
280
- const modelToProcessConstructor = this.#getModelConstructorFromId(modelToProcess.id);
280
+ const modelToProcessConstructor = this.#getModelConstructorFromId(modelToProcess.id).name;
281
+
281
282
  indexActions[modelToProcessConstructor] = indexActions[modelToProcessConstructor] ?? [];
282
283
  searchIndexActions[modelToProcessConstructor] = searchIndexActions[modelToProcessConstructor] ?? [];
283
284
 
@@ -437,9 +438,9 @@ export default class Connection {
437
438
 
438
439
  const engine = CreateTransactionalStorageEngine(transactions, this.#storage);
439
440
 
440
- const connection = new this.constructor(engine, this.#cache, Object.values(this.#models));
441
+ const transaction = new this.constructor(engine, this.#cache, Object.values(this.#models));
441
442
 
442
- connection.commit = async () => {
443
+ transaction.commit = async () => {
443
444
  try {
444
445
  for (const [index, transaction] of transactions.entries()) {
445
446
  try {
@@ -449,6 +450,12 @@ export default class Connection {
449
450
  if (transaction.method === 'deleteModel')
450
451
  transactions[index].original = await this.#storage.getModel(transaction.args[0]);
451
452
 
453
+ if (transaction.method === 'putIndex')
454
+ transactions[index].original = await this.#storage.getIndex(transaction.args[0]);
455
+
456
+ if (transaction.method === 'putSearchIndex')
457
+ transactions[index].original = await this.#storage.getSearchIndex(transaction.args[0]);
458
+
452
459
  await this.#storage[transaction.method](...transaction.args);
453
460
 
454
461
  transactions[index].committed = true;
@@ -458,17 +465,24 @@ export default class Connection {
458
465
  }
459
466
  }
460
467
  } catch (error) {
461
- await Promise.all(
462
- transactions
463
- .filter(t => t.committed && t.original)
464
- .map(t => this.#storage.putModel(t.original)),
465
- );
468
+ for (const transaction of transactions) {
469
+ if (transaction.committed && transaction.original) {
470
+ if (['putModel', 'deleteModel'].includes(transaction.method))
471
+ await this.#storage.putModel(transaction.original);
472
+
473
+ if ('putIndex' === transaction.method)
474
+ await this.#storage.putIndex(transaction.args[0], transaction.original);
475
+
476
+ if ('putSearchIndex' === transaction.method)
477
+ await this.#storage.putSearchIndex(transaction.args[0], transaction.original);
478
+ }
479
+ }
466
480
 
467
481
  throw new CommitFailedTransactionError(transactions, error);
468
482
  }
469
483
  };
470
484
 
471
- return connection;
485
+ return transaction;
472
486
  }
473
487
 
474
488
  /**
@@ -525,13 +539,11 @@ export default class Connection {
525
539
  })),
526
540
  )
527
541
  .flat()
528
- .reduce((accumulator, {containingModel, propertyName, propertyProperty}) => ({
529
- ...accumulator,
530
- [containingModel]: {
531
- ...accumulator[containingModel] || {},
532
- [propertyName]: propertyProperty,
533
- },
534
- }), {});
542
+ .reduce((accumulator, {containingModel, propertyName, propertyProperty}) => {
543
+ accumulator[containingModel] = accumulator[containingModel] ?? {};
544
+ accumulator[containingModel][propertyName] = propertyProperty;
545
+ return accumulator;
546
+ }, {});
535
547
  }
536
548
 
537
549
  /**
@@ -29,7 +29,7 @@ export default class HTTPStorageEngine extends StorageEngine {
29
29
  * Get a model
30
30
  * @param {string} id
31
31
  * @throws ModelNotFoundStorageEngineError
32
- * @return Promise<Model>
32
+ * @return Promise<Object>
33
33
  */
34
34
  getModel(id) {
35
35
  return this.#processFetch(this.#generateURL([id]), this.#getReadOptions())
@@ -43,7 +43,7 @@ export default class HTTPStorageEngine extends StorageEngine {
43
43
 
44
44
  /**
45
45
  * Update a model
46
- * @param {object} model
46
+ * @param {Object} model
47
47
  * @throws HTTPRequestFailedError
48
48
  * @return Promise<void>
49
49
  */
@@ -75,7 +75,7 @@ export default class HTTPStorageEngine extends StorageEngine {
75
75
  /**
76
76
  * Get a model's index data
77
77
  * @param {Model.constructor} modelConstructor
78
- * @return Promise<void>
78
+ * @return Promise<Record<String, Object>>
79
79
  */
80
80
  getIndex(modelConstructor) {
81
81
  return this.#processFetch(
@@ -88,7 +88,7 @@ export default class HTTPStorageEngine extends StorageEngine {
88
88
  /**
89
89
  * Put a model's index data
90
90
  * @param {Model.constructor} modelConstructor
91
- * @param {object} index
91
+ * @param {Record<String, Object>} index
92
92
  * @throws MethodNotImplementedStorageEngineError
93
93
  * @return Promise<void>
94
94
  */
@@ -103,7 +103,7 @@ export default class HTTPStorageEngine extends StorageEngine {
103
103
  * Get a model's raw search index data
104
104
  * @param {Model.constructor} modelConstructor
105
105
  * @throws MethodNotImplementedStorageEngineError
106
- * @return Promise<object>
106
+ * @return Promise<Record<String, Object>>
107
107
  */
108
108
  getSearchIndex(modelConstructor) {
109
109
  return this.#processFetch(
@@ -48,7 +48,7 @@ class S3StorageEngine extends StorageEngine {
48
48
 
49
49
  /**
50
50
  * Upload an object to S3
51
- * @param {object} model - The model object to upload.
51
+ * @param {Object} model - The model object to upload.
52
52
  * @returns {Promise<void>}
53
53
  */
54
54
  async putModel(model) {
@@ -87,7 +87,7 @@ class S3StorageEngine extends StorageEngine {
87
87
  * Get a model's index data
88
88
  * @param {Model.constructor} modelConstructor
89
89
  * @throws MethodNotImplementedStorageEngineError
90
- * @return Promise<object>
90
+ * @return Promise<Record<String, Object>>
91
91
  */
92
92
  async getIndex(modelConstructor) {
93
93
  const Key = this.#generatePath([modelConstructor.name, '_index.json']);
@@ -106,7 +106,7 @@ class S3StorageEngine extends StorageEngine {
106
106
  /**
107
107
  * Put a model's index data
108
108
  * @param {Model.constructor} modelConstructor
109
- * @param {object} index
109
+ * @param {Record<String, Object>} index
110
110
  * @throws MethodNotImplementedStorageEngineError
111
111
  * @return Promise<void>
112
112
  */
@@ -13,7 +13,7 @@ export default class StorageEngine {
13
13
  * @param {string} _id
14
14
  * @throws MethodNotImplementedStorageEngineError
15
15
  * @throws ModelNotFoundStorageEngineError
16
- * @return Promise<Model>
16
+ * @return Promise<Object>
17
17
  */
18
18
  getModel(_id) {
19
19
  return Promise.reject(new MethodNotImplementedStorageEngineError('getModel', this));
@@ -21,7 +21,7 @@ export default class StorageEngine {
21
21
 
22
22
  /**
23
23
  * Update a model
24
- * @param {object} _model
24
+ * @param {Object} _model
25
25
  * @throws MethodNotImplementedStorageEngineError
26
26
  * @return Promise<void>
27
27
  */
@@ -44,7 +44,7 @@ export default class StorageEngine {
44
44
  * Get a model's index data
45
45
  * @param {Model.constructor} _modelConstructor
46
46
  * @throws MethodNotImplementedStorageEngineError
47
- * @return Promise<void>
47
+ * @return Promise<Record<String, Object>>
48
48
  */
49
49
  getIndex(_modelConstructor) {
50
50
  return Promise.reject(new MethodNotImplementedStorageEngineError('getIndex', this));
@@ -53,7 +53,7 @@ export default class StorageEngine {
53
53
  /**
54
54
  * Put a model's index data
55
55
  * @param {Model.constructor} _modelConstructor
56
- * @param {object} _data
56
+ * @param {Record<String, Object>} _data
57
57
  * @throws MethodNotImplementedStorageEngineError
58
58
  * @return Promise<void>
59
59
  */
@@ -65,7 +65,7 @@ export default class StorageEngine {
65
65
  * Get a model's raw search index data
66
66
  * @param {Model.constructor} _modelConstructor
67
67
  * @throws MethodNotImplementedStorageEngineError
68
- * @return Promise<object>
68
+ * @return Promise<Record<String, Object>>
69
69
  */
70
70
  getSearchIndex(_modelConstructor) {
71
71
  return Promise.reject(new MethodNotImplementedStorageEngineError('getSearchIndex', this));
@@ -76,7 +76,7 @@ export default class StorageEngine {
76
76
  * @param {Model.constructor} _constructor
77
77
  * @param {Record<string, object>} _index
78
78
  * @throws MethodNotImplementedStorageEngineError
79
- * @return Promise<void>
79
+ * @return Promise<Record<String, Object>>
80
80
  */
81
81
  putSearchIndex(_constructor, _index) {
82
82
  return Promise.reject(new MethodNotImplementedStorageEngineError('putSearchIndex', this));