@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 +1 -1
- package/src/Connection.js +25 -43
- package/src/engine/storage/StorageEngine.js +24 -8
package/package.json
CHANGED
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
|
419
|
+
const operations = [];
|
438
420
|
|
439
|
-
const engine = CreateTransactionalStorageEngine(
|
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,
|
427
|
+
for (const [index, operation] of operations.entries()) {
|
446
428
|
try {
|
447
|
-
if (
|
448
|
-
|
429
|
+
if (operation.method === 'putModel')
|
430
|
+
operations[index].original = await this.#storage.getModel(operation.args[0].id).catch(() => undefined);
|
449
431
|
|
450
|
-
if (
|
451
|
-
|
432
|
+
if (operation.method === 'deleteModel')
|
433
|
+
operations[index].original = await this.#storage.getModel(operation.args[0]);
|
452
434
|
|
453
|
-
if (
|
454
|
-
|
435
|
+
if (operation.method === 'putIndex')
|
436
|
+
operations[index].original = await this.#storage.getIndex(operation.args[0]);
|
455
437
|
|
456
|
-
if (
|
457
|
-
|
438
|
+
if (operation.method === 'putSearchIndex')
|
439
|
+
operations[index].original = await this.#storage.getSearchIndex(operation.args[0]);
|
458
440
|
|
459
|
-
await this.#storage[
|
441
|
+
await this.#storage[operation.method](...operation.args);
|
460
442
|
|
461
|
-
|
443
|
+
operations[index].committed = true;
|
462
444
|
} catch (error) {
|
463
|
-
|
445
|
+
operations[index].error = error;
|
464
446
|
throw error;
|
465
447
|
}
|
466
448
|
}
|
467
449
|
} catch (error) {
|
468
|
-
for (const
|
469
|
-
if (
|
470
|
-
if (['putModel', 'deleteModel'].includes(
|
471
|
-
await this.#storage.putModel(
|
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'
|
474
|
-
await this.#storage.putIndex(
|
455
|
+
if (operation.method === 'putIndex')
|
456
|
+
await this.#storage.putIndex(operation.args[0], operation.original);
|
475
457
|
|
476
|
-
if ('putSearchIndex'
|
477
|
-
await this.#storage.putSearchIndex(
|
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(
|
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<
|
617
|
+
* @param {Array<Operation>} transactions
|
636
618
|
* @param {Error} error
|
637
619
|
*/
|
638
620
|
constructor(transactions, error) {
|
639
|
-
super('
|
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<
|
163
|
+
* @param {Array<Operation>} transactions
|
148
164
|
* @param {StorageEngine} engine
|
149
165
|
* @return {StorageEngine}
|
150
166
|
*/
|
151
|
-
export function CreateTransactionalStorageEngine(
|
167
|
+
export function CreateTransactionalStorageEngine(operations, engine) {
|
152
168
|
const transactionalEngine = Object.create(engine);
|
153
169
|
|
154
170
|
transactionalEngine.putModel = (...args) => {
|
155
|
-
|
171
|
+
operations.push(new Operation('putModel', ...args));
|
156
172
|
return Promise.resolve();
|
157
173
|
};
|
158
174
|
|
159
175
|
transactionalEngine.deleteModel = (...args) => {
|
160
|
-
|
176
|
+
operations.push(new Operation('deleteModel', ...args));
|
161
177
|
return Promise.resolve();
|
162
178
|
};
|
163
179
|
|
164
180
|
transactionalEngine.putIndex = (...args) => {
|
165
|
-
|
181
|
+
operations.push(new Operation('putIndex', ...args));
|
166
182
|
return Promise.resolve();
|
167
183
|
};
|
168
184
|
|
169
185
|
transactionalEngine.putSearchIndex = (...args) => {
|
170
|
-
|
186
|
+
operations.push(new Operation('putSearchIndex', ...args));
|
171
187
|
return Promise.resolve();
|
172
188
|
};
|
173
189
|
|