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

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.22",
3
+ "version": "3.0.0-next.23",
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
@@ -7,24 +7,6 @@ import Model from './data/Model.js';
7
7
  import SearchIndex from './data/SearchIndex.js';
8
8
  import _ from 'lodash';
9
9
 
10
- /**
11
- * Represents a transactional operation to be executed, typically queued and later committed.
12
- *
13
- * Stores the method to invoke, the arguments to apply, and tracks the result or error state
14
- * of the transaction once it's processed.
15
- *
16
- * @class Transaction
17
- */
18
- export class Transaction {
19
- constructor(method, ...args) {
20
- this.method = method;
21
- this.args = args;
22
- this.original = undefined;
23
- this.error = undefined;
24
- this.committed = false;
25
- }
26
- }
27
-
28
10
  /**
29
11
  * @class Connection
30
12
  */
@@ -434,51 +416,51 @@ export default class Connection {
434
416
  * @return {Connection}
435
417
  */
436
418
  transaction() {
437
- const transactions = [];
419
+ const operations = [];
438
420
 
439
- const engine = CreateTransactionalStorageEngine(transactions, this.#storage);
421
+ const engine = CreateTransactionalStorageEngine(operations, this.#storage);
440
422
 
441
423
  const transaction = new this.constructor(engine, this.#cache, Object.values(this.#models));
442
424
 
443
425
  transaction.commit = async () => {
444
426
  try {
445
- for (const [index, transaction] of transactions.entries()) {
427
+ for (const [index, operation] of operations.entries()) {
446
428
  try {
447
- if (transaction.method === 'putModel')
448
- transactions[index].original = await this.#storage.getModel(transaction.args[0].id).catch(() => undefined);
429
+ if (operation.method === 'putModel')
430
+ operations[index].original = await this.#storage.getModel(operation.args[0].id).catch(() => undefined);
449
431
 
450
- if (transaction.method === 'deleteModel')
451
- transactions[index].original = await this.#storage.getModel(transaction.args[0]);
432
+ if (operation.method === 'deleteModel')
433
+ operations[index].original = await this.#storage.getModel(operation.args[0]);
452
434
 
453
- if (transaction.method === 'putIndex')
454
- transactions[index].original = await this.#storage.getIndex(transaction.args[0]);
435
+ if (operation.method === 'putIndex')
436
+ operations[index].original = await this.#storage.getIndex(operation.args[0]);
455
437
 
456
- if (transaction.method === 'putSearchIndex')
457
- transactions[index].original = await this.#storage.getSearchIndex(transaction.args[0]);
438
+ if (operation.method === 'putSearchIndex')
439
+ operations[index].original = await this.#storage.getSearchIndex(operation.args[0]);
458
440
 
459
- await this.#storage[transaction.method](...transaction.args);
441
+ await this.#storage[operation.method](...operation.args);
460
442
 
461
- transactions[index].committed = true;
443
+ operations[index].committed = true;
462
444
  } catch (error) {
463
- transactions[index].error = error;
445
+ operations[index].error = error;
464
446
  throw error;
465
447
  }
466
448
  }
467
449
  } catch (error) {
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);
450
+ for (const operation of operations) {
451
+ if (operation.committed && operation.original) {
452
+ if (['putModel', 'deleteModel'].includes(operation.method))
453
+ await this.#storage.putModel(operation.original);
472
454
 
473
- if ('putIndex' === transaction.method)
474
- await this.#storage.putIndex(transaction.args[0], transaction.original);
455
+ if (operation.method === 'putIndex')
456
+ await this.#storage.putIndex(operation.args[0], operation.original);
475
457
 
476
- if ('putSearchIndex' === transaction.method)
477
- await this.#storage.putSearchIndex(transaction.args[0], transaction.original);
458
+ if (operation.method === 'putSearchIndex')
459
+ await this.#storage.putSearchIndex(operation.args[0], operation.original);
478
460
  }
479
461
  }
480
462
 
481
- throw new CommitFailedTransactionError(transactions, error);
463
+ throw new CommitFailedTransactionError(operations, error);
482
464
  }
483
465
  };
484
466
 
@@ -632,11 +614,11 @@ class TransactionError extends Error {
632
614
  export class CommitFailedTransactionError extends TransactionError {
633
615
  /**
634
616
  *
635
- * @param {Array<Transaction>} transactions
617
+ * @param {Array<Operation>} transactions
636
618
  * @param {Error} error
637
619
  */
638
620
  constructor(transactions, error) {
639
- super('Transaction failed to commit.');
621
+ super('Operation failed to commit.');
640
622
  this.transactions = transactions;
641
623
  this.error = error;
642
624
  }
@@ -1,5 +1,3 @@
1
- import {Transaction} from '../../Connection.js';
2
-
3
1
  export default class StorageEngine {
4
2
  /**
5
3
  * @param {Object} configuration
@@ -142,32 +140,50 @@ export class DeleteHasUnintendedConsequencesStorageEngineError extends StorageEn
142
140
  }
143
141
  }
144
142
 
143
+ /**
144
+ * Represents a transactional operation to be executed, typically queued and later committed.
145
+ *
146
+ * Stores the method to invoke, the arguments to apply, and tracks the result or error state
147
+ * of the transaction once it's processed.
148
+ *
149
+ * @class Operation
150
+ */
151
+ export class Operation {
152
+ constructor(method, ...args) {
153
+ this.method = method;
154
+ this.args = args;
155
+ this.original = undefined;
156
+ this.error = undefined;
157
+ this.committed = false;
158
+ }
159
+ }
160
+
145
161
  /**
146
162
  *
147
- * @param {Array<Transaction>} transactions
163
+ * @param {Array<Operation>} transactions
148
164
  * @param {StorageEngine} engine
149
165
  * @return {StorageEngine}
150
166
  */
151
- export function CreateTransactionalStorageEngine(transactions, engine) {
167
+ export function CreateTransactionalStorageEngine(operations, engine) {
152
168
  const transactionalEngine = Object.create(engine);
153
169
 
154
170
  transactionalEngine.putModel = (...args) => {
155
- transactions.push(new Transaction('putModel', ...args));
171
+ operations.push(new Operation('putModel', ...args));
156
172
  return Promise.resolve();
157
173
  };
158
174
 
159
175
  transactionalEngine.deleteModel = (...args) => {
160
- transactions.push(new Transaction('deleteModel', ...args));
176
+ operations.push(new Operation('deleteModel', ...args));
161
177
  return Promise.resolve();
162
178
  };
163
179
 
164
180
  transactionalEngine.putIndex = (...args) => {
165
- transactions.push(new Transaction('putIndex', ...args));
181
+ operations.push(new Operation('putIndex', ...args));
166
182
  return Promise.resolve();
167
183
  };
168
184
 
169
185
  transactionalEngine.putSearchIndex = (...args) => {
170
- transactions.push(new Transaction('putSearchIndex', ...args));
186
+ operations.push(new Operation('putSearchIndex', ...args));
171
187
  return Promise.resolve();
172
188
  };
173
189