@ddd-ts/store-inmemory 0.0.36 → 0.0.38
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/LICENSE +21 -0
- package/dist/in-memory.transaction.js +35 -35
- package/dist/in-memory.transaction.mjs +34 -0
- package/dist/index.js +13 -15
- package/dist/index.mjs +5 -0
- package/dist/store/in-memory.collection.js +61 -71
- package/dist/store/in-memory.collection.mjs +64 -0
- package/dist/store/in-memory.database.js +153 -175
- package/dist/store/in-memory.database.mjs +152 -0
- package/dist/store/in-memory.storage.js +31 -38
- package/dist/store/in-memory.storage.mjs +31 -0
- package/dist/store/in-memory.store.d.ts +1 -1
- package/dist/store/in-memory.store.d.ts.map +1 -1
- package/dist/store/in-memory.store.js +79 -83
- package/dist/store/in-memory.store.mjs +80 -0
- package/package.json +32 -21
- package/dist/in-memory.transaction.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/store/in-memory.collection.js.map +0 -1
- package/dist/store/in-memory.database.js.map +0 -1
- package/dist/store/in-memory.storage.js.map +0 -1
- package/dist/store/in-memory.store.js.map +0 -1
|
@@ -1,177 +1,155 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
1
|
+
const require_in_memory_storage = require('./in-memory.storage.js');
|
|
2
|
+
const require_in_memory_transaction = require('../in-memory.transaction.js');
|
|
3
|
+
require('../index.js');
|
|
4
|
+
let _ddd_ts_core = require("@ddd-ts/core");
|
|
5
|
+
|
|
6
|
+
//#region src/store/in-memory.database.ts
|
|
7
|
+
var CannotReadAfterWrites = class extends Error {
|
|
8
|
+
constructor() {
|
|
9
|
+
super("Cannot read after having written into a transaction");
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var TransactionCollision = class extends Error {
|
|
13
|
+
constructor() {
|
|
14
|
+
super("Transaction has collided with other extern writes");
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var DocumentAlreadyExists = class extends _ddd_ts_core.ConcurrencyError {
|
|
18
|
+
constructor() {
|
|
19
|
+
super("Document already exists");
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
var TransactionCollidedTooManyTimes = class extends Error {
|
|
23
|
+
constructor(tries) {
|
|
24
|
+
super(`Transaction collided too many times (${tries})`);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var InMemoryUnderlyingTransaction = class {
|
|
28
|
+
operations = [];
|
|
29
|
+
id = Math.random().toString().substring(2);
|
|
30
|
+
ensureNoWrites() {
|
|
31
|
+
if (this.operations.some((operation) => operation.type === "write" || operation.type === "create" || operation.type === "delete")) throw new CannotReadAfterWrites();
|
|
32
|
+
}
|
|
33
|
+
markRead(collectionName, id, savedAt) {
|
|
34
|
+
this.ensureNoWrites();
|
|
35
|
+
this.operations.push({
|
|
36
|
+
type: "read",
|
|
37
|
+
collectionName,
|
|
38
|
+
id,
|
|
39
|
+
savedAt
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
markWritten(collectionName, id, data) {
|
|
43
|
+
this.operations.push({
|
|
44
|
+
type: "write",
|
|
45
|
+
collectionName,
|
|
46
|
+
id,
|
|
47
|
+
data
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
markCreated(collectionName, id, data) {
|
|
51
|
+
this.operations.push({
|
|
52
|
+
type: "create",
|
|
53
|
+
collectionName,
|
|
54
|
+
id,
|
|
55
|
+
data
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
markDeleted(collectionName, id, savedAt) {
|
|
59
|
+
this.operations.push({
|
|
60
|
+
type: "delete",
|
|
61
|
+
collectionName,
|
|
62
|
+
id,
|
|
63
|
+
savedAt
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
checkConsistency(storage) {
|
|
67
|
+
for (const operation of this.operations) {
|
|
68
|
+
if (operation.type === "read") {
|
|
69
|
+
const collection = storage.getCollection(operation.collectionName);
|
|
70
|
+
if (!collection) return false;
|
|
71
|
+
if (operation.savedAt !== collection.getRaw(operation.id)?.savedAt) return false;
|
|
72
|
+
}
|
|
73
|
+
if (operation.type === "create") {
|
|
74
|
+
const collection = storage.getCollection(operation.collectionName);
|
|
75
|
+
if (collection && collection.getRaw(operation.id)?.savedAt !== void 0) throw new DocumentAlreadyExists();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
var InMemoryDatabase = class InMemoryDatabase {
|
|
82
|
+
storage = new require_in_memory_storage.Storage();
|
|
83
|
+
clear(collectionName) {
|
|
84
|
+
this.storage.getCollection(collectionName).clear();
|
|
85
|
+
}
|
|
86
|
+
load(collectionName, id, trx) {
|
|
87
|
+
const collection = this.storage.getCollection(collectionName);
|
|
88
|
+
const data = collection.get(id);
|
|
89
|
+
if (trx) {
|
|
90
|
+
const doc = collection.getRaw(id);
|
|
91
|
+
trx.markRead(collectionName, id, doc?.savedAt);
|
|
92
|
+
}
|
|
93
|
+
return data;
|
|
94
|
+
}
|
|
95
|
+
delete(collectionName, id, trx) {
|
|
96
|
+
if (trx) {
|
|
97
|
+
const doc = this.storage.getCollection(collectionName).getRaw(id);
|
|
98
|
+
trx.markDeleted(collectionName, id, doc?.savedAt);
|
|
99
|
+
} else this.storage.getCollection(collectionName).delete(id);
|
|
100
|
+
}
|
|
101
|
+
countAll(collectionName) {
|
|
102
|
+
return this.storage.getCollection(collectionName).countAll();
|
|
103
|
+
}
|
|
104
|
+
loadAll(collectionName) {
|
|
105
|
+
return this.storage.getCollection(collectionName).getAllRaw();
|
|
106
|
+
}
|
|
107
|
+
loadLatestSnapshot(id) {
|
|
108
|
+
return this.storage.getCollection("snapshots").getLatestSnapshot(id);
|
|
109
|
+
}
|
|
110
|
+
save(collectionName, id, data, trx) {
|
|
111
|
+
if (trx) trx.markWritten(collectionName, id, data);
|
|
112
|
+
else this.storage.getCollection(collectionName).save(id, data);
|
|
113
|
+
}
|
|
114
|
+
create(collectionName, id, data, trx) {
|
|
115
|
+
if (trx) trx.markCreated(collectionName, id, data);
|
|
116
|
+
else {
|
|
117
|
+
if (this.storage.getCollection(collectionName).get(id)) throw new Error(`Document with id ${id} already exists`);
|
|
118
|
+
this.storage.getCollection(collectionName).save(id, data);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
static transactionTries = 5;
|
|
122
|
+
async transactionally(fn) {
|
|
123
|
+
let trx = new require_in_memory_transaction.InMemoryTransaction(new InMemoryUnderlyingTransaction());
|
|
124
|
+
let retry = InMemoryDatabase.transactionTries;
|
|
125
|
+
let latestReturnValue = void 0;
|
|
126
|
+
while (retry--) try {
|
|
127
|
+
latestReturnValue = await fn(trx);
|
|
128
|
+
this.streamId(trx);
|
|
129
|
+
break;
|
|
130
|
+
} catch (error) {
|
|
131
|
+
if (error instanceof TransactionCollision) trx = new require_in_memory_transaction.InMemoryTransaction(new InMemoryUnderlyingTransaction());
|
|
132
|
+
else throw error;
|
|
133
|
+
}
|
|
134
|
+
if (retry === -1) throw new TransactionCollidedTooManyTimes(InMemoryDatabase.transactionTries);
|
|
135
|
+
return latestReturnValue;
|
|
136
|
+
}
|
|
137
|
+
streamId(trx) {
|
|
138
|
+
if (!trx.transaction.checkConsistency(this.storage)) throw new TransactionCollision();
|
|
139
|
+
for (const operation of trx.transaction.operations) {
|
|
140
|
+
if (operation.type === "read") continue;
|
|
141
|
+
if (operation.type === "write") this.save(operation.collectionName, operation.id, operation.data);
|
|
142
|
+
if (operation.type === "delete") this.delete(operation.collectionName, operation.id);
|
|
143
|
+
if (operation.type === "create") this.create(operation.collectionName, operation.id, operation.data);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
print() {
|
|
147
|
+
console.log(["Database:", this.storage.toPretty()].join("\n"));
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
12
152
|
exports.CannotReadAfterWrites = CannotReadAfterWrites;
|
|
13
|
-
class TransactionCollision extends Error {
|
|
14
|
-
constructor() {
|
|
15
|
-
super("Transaction has collided with other extern writes");
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
exports.TransactionCollision = TransactionCollision;
|
|
19
|
-
class DocumentAlreadyExists extends core_1.ConcurrencyError {
|
|
20
|
-
constructor() {
|
|
21
|
-
super("Document already exists");
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
exports.DocumentAlreadyExists = DocumentAlreadyExists;
|
|
25
|
-
class TransactionCollidedTooManyTimes extends Error {
|
|
26
|
-
constructor(tries) {
|
|
27
|
-
super(`Transaction collided too many times (${tries})`);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
exports.TransactionCollidedTooManyTimes = TransactionCollidedTooManyTimes;
|
|
31
|
-
class InMemoryUnderlyingTransaction {
|
|
32
|
-
operations = [];
|
|
33
|
-
id = Math.random().toString().substring(2);
|
|
34
|
-
ensureNoWrites() {
|
|
35
|
-
if (this.operations.some((operation) => operation.type === "write" ||
|
|
36
|
-
operation.type === "create" ||
|
|
37
|
-
operation.type === "delete")) {
|
|
38
|
-
throw new CannotReadAfterWrites();
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
markRead(collectionName, id, savedAt) {
|
|
42
|
-
this.ensureNoWrites();
|
|
43
|
-
this.operations.push({ type: "read", collectionName, id, savedAt });
|
|
44
|
-
}
|
|
45
|
-
markWritten(collectionName, id, data) {
|
|
46
|
-
this.operations.push({ type: "write", collectionName, id, data });
|
|
47
|
-
}
|
|
48
|
-
markCreated(collectionName, id, data) {
|
|
49
|
-
this.operations.push({ type: "create", collectionName, id, data });
|
|
50
|
-
}
|
|
51
|
-
markDeleted(collectionName, id, savedAt) {
|
|
52
|
-
this.operations.push({ type: "delete", collectionName, id, savedAt });
|
|
53
|
-
}
|
|
54
|
-
checkConsistency(storage) {
|
|
55
|
-
for (const operation of this.operations) {
|
|
56
|
-
if (operation.type === "read") {
|
|
57
|
-
const collection = storage.getCollection(operation.collectionName);
|
|
58
|
-
if (!collection) {
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
if (operation.savedAt !== collection.getRaw(operation.id)?.savedAt) {
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
if (operation.type === "create") {
|
|
66
|
-
const collection = storage.getCollection(operation.collectionName);
|
|
67
|
-
if (collection &&
|
|
68
|
-
collection.getRaw(operation.id)?.savedAt !== undefined) {
|
|
69
|
-
throw new DocumentAlreadyExists();
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return true;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
exports.InMemoryUnderlyingTransaction = InMemoryUnderlyingTransaction;
|
|
77
|
-
class InMemoryDatabase {
|
|
78
|
-
storage = new in_memory_storage_1.Storage();
|
|
79
|
-
clear(collectionName) {
|
|
80
|
-
this.storage.getCollection(collectionName).clear();
|
|
81
|
-
}
|
|
82
|
-
load(collectionName, id, trx) {
|
|
83
|
-
const collection = this.storage.getCollection(collectionName);
|
|
84
|
-
const data = collection.get(id);
|
|
85
|
-
if (trx) {
|
|
86
|
-
const doc = collection.getRaw(id);
|
|
87
|
-
trx.markRead(collectionName, id, doc?.savedAt);
|
|
88
|
-
}
|
|
89
|
-
return data;
|
|
90
|
-
}
|
|
91
|
-
delete(collectionName, id, trx) {
|
|
92
|
-
if (trx) {
|
|
93
|
-
const doc = this.storage.getCollection(collectionName).getRaw(id);
|
|
94
|
-
trx.markDeleted(collectionName, id, doc?.savedAt);
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
this.storage.getCollection(collectionName).delete(id);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
countAll(collectionName) {
|
|
101
|
-
return this.storage.getCollection(collectionName).countAll();
|
|
102
|
-
}
|
|
103
|
-
loadAll(collectionName) {
|
|
104
|
-
return this.storage.getCollection(collectionName).getAllRaw();
|
|
105
|
-
}
|
|
106
|
-
loadLatestSnapshot(id) {
|
|
107
|
-
return this.storage.getCollection("snapshots").getLatestSnapshot(id);
|
|
108
|
-
}
|
|
109
|
-
save(collectionName, id, data, trx) {
|
|
110
|
-
if (trx) {
|
|
111
|
-
trx.markWritten(collectionName, id, data);
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
this.storage.getCollection(collectionName).save(id, data);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
create(collectionName, id, data, trx) {
|
|
118
|
-
if (trx) {
|
|
119
|
-
trx.markCreated(collectionName, id, data);
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
if (this.storage.getCollection(collectionName).get(id)) {
|
|
123
|
-
throw new Error(`Document with id ${id} already exists`);
|
|
124
|
-
}
|
|
125
|
-
this.storage.getCollection(collectionName).save(id, data);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
static transactionTries = 5;
|
|
129
|
-
async transactionally(fn) {
|
|
130
|
-
let trx = new __1.InMemoryTransaction(new InMemoryUnderlyingTransaction());
|
|
131
|
-
let retry = InMemoryDatabase.transactionTries;
|
|
132
|
-
let latestReturnValue = undefined;
|
|
133
|
-
while (retry--) {
|
|
134
|
-
try {
|
|
135
|
-
latestReturnValue = await fn(trx);
|
|
136
|
-
this.streamId(trx);
|
|
137
|
-
break;
|
|
138
|
-
}
|
|
139
|
-
catch (error) {
|
|
140
|
-
if (error instanceof TransactionCollision) {
|
|
141
|
-
trx = new __1.InMemoryTransaction(new InMemoryUnderlyingTransaction());
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
throw error;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
if (retry === -1) {
|
|
149
|
-
throw new TransactionCollidedTooManyTimes(InMemoryDatabase.transactionTries);
|
|
150
|
-
}
|
|
151
|
-
return latestReturnValue;
|
|
152
|
-
}
|
|
153
|
-
streamId(trx) {
|
|
154
|
-
if (!trx.transaction.checkConsistency(this.storage)) {
|
|
155
|
-
throw new TransactionCollision();
|
|
156
|
-
}
|
|
157
|
-
for (const operation of trx.transaction.operations) {
|
|
158
|
-
if (operation.type === "read") {
|
|
159
|
-
continue;
|
|
160
|
-
}
|
|
161
|
-
if (operation.type === "write") {
|
|
162
|
-
this.save(operation.collectionName, operation.id, operation.data);
|
|
163
|
-
}
|
|
164
|
-
if (operation.type === "delete") {
|
|
165
|
-
this.delete(operation.collectionName, operation.id);
|
|
166
|
-
}
|
|
167
|
-
if (operation.type === "create") {
|
|
168
|
-
this.create(operation.collectionName, operation.id, operation.data);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
print() {
|
|
173
|
-
console.log(["Database:", this.storage.toPretty()].join("\n"));
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
153
|
exports.InMemoryDatabase = InMemoryDatabase;
|
|
177
|
-
|
|
154
|
+
exports.InMemoryUnderlyingTransaction = InMemoryUnderlyingTransaction;
|
|
155
|
+
exports.TransactionCollidedTooManyTimes = TransactionCollidedTooManyTimes;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { Storage } from "./in-memory.storage.mjs";
|
|
2
|
+
import { InMemoryTransaction } from "../in-memory.transaction.mjs";
|
|
3
|
+
import "../index.mjs";
|
|
4
|
+
import { ConcurrencyError } from "@ddd-ts/core";
|
|
5
|
+
|
|
6
|
+
//#region src/store/in-memory.database.ts
|
|
7
|
+
var CannotReadAfterWrites = class extends Error {
|
|
8
|
+
constructor() {
|
|
9
|
+
super("Cannot read after having written into a transaction");
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var TransactionCollision = class extends Error {
|
|
13
|
+
constructor() {
|
|
14
|
+
super("Transaction has collided with other extern writes");
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var DocumentAlreadyExists = class extends ConcurrencyError {
|
|
18
|
+
constructor() {
|
|
19
|
+
super("Document already exists");
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
var TransactionCollidedTooManyTimes = class extends Error {
|
|
23
|
+
constructor(tries) {
|
|
24
|
+
super(`Transaction collided too many times (${tries})`);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var InMemoryUnderlyingTransaction = class {
|
|
28
|
+
operations = [];
|
|
29
|
+
id = Math.random().toString().substring(2);
|
|
30
|
+
ensureNoWrites() {
|
|
31
|
+
if (this.operations.some((operation) => operation.type === "write" || operation.type === "create" || operation.type === "delete")) throw new CannotReadAfterWrites();
|
|
32
|
+
}
|
|
33
|
+
markRead(collectionName, id, savedAt) {
|
|
34
|
+
this.ensureNoWrites();
|
|
35
|
+
this.operations.push({
|
|
36
|
+
type: "read",
|
|
37
|
+
collectionName,
|
|
38
|
+
id,
|
|
39
|
+
savedAt
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
markWritten(collectionName, id, data) {
|
|
43
|
+
this.operations.push({
|
|
44
|
+
type: "write",
|
|
45
|
+
collectionName,
|
|
46
|
+
id,
|
|
47
|
+
data
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
markCreated(collectionName, id, data) {
|
|
51
|
+
this.operations.push({
|
|
52
|
+
type: "create",
|
|
53
|
+
collectionName,
|
|
54
|
+
id,
|
|
55
|
+
data
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
markDeleted(collectionName, id, savedAt) {
|
|
59
|
+
this.operations.push({
|
|
60
|
+
type: "delete",
|
|
61
|
+
collectionName,
|
|
62
|
+
id,
|
|
63
|
+
savedAt
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
checkConsistency(storage) {
|
|
67
|
+
for (const operation of this.operations) {
|
|
68
|
+
if (operation.type === "read") {
|
|
69
|
+
const collection = storage.getCollection(operation.collectionName);
|
|
70
|
+
if (!collection) return false;
|
|
71
|
+
if (operation.savedAt !== collection.getRaw(operation.id)?.savedAt) return false;
|
|
72
|
+
}
|
|
73
|
+
if (operation.type === "create") {
|
|
74
|
+
const collection = storage.getCollection(operation.collectionName);
|
|
75
|
+
if (collection && collection.getRaw(operation.id)?.savedAt !== void 0) throw new DocumentAlreadyExists();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
var InMemoryDatabase = class InMemoryDatabase {
|
|
82
|
+
storage = new Storage();
|
|
83
|
+
clear(collectionName) {
|
|
84
|
+
this.storage.getCollection(collectionName).clear();
|
|
85
|
+
}
|
|
86
|
+
load(collectionName, id, trx) {
|
|
87
|
+
const collection = this.storage.getCollection(collectionName);
|
|
88
|
+
const data = collection.get(id);
|
|
89
|
+
if (trx) {
|
|
90
|
+
const doc = collection.getRaw(id);
|
|
91
|
+
trx.markRead(collectionName, id, doc?.savedAt);
|
|
92
|
+
}
|
|
93
|
+
return data;
|
|
94
|
+
}
|
|
95
|
+
delete(collectionName, id, trx) {
|
|
96
|
+
if (trx) {
|
|
97
|
+
const doc = this.storage.getCollection(collectionName).getRaw(id);
|
|
98
|
+
trx.markDeleted(collectionName, id, doc?.savedAt);
|
|
99
|
+
} else this.storage.getCollection(collectionName).delete(id);
|
|
100
|
+
}
|
|
101
|
+
countAll(collectionName) {
|
|
102
|
+
return this.storage.getCollection(collectionName).countAll();
|
|
103
|
+
}
|
|
104
|
+
loadAll(collectionName) {
|
|
105
|
+
return this.storage.getCollection(collectionName).getAllRaw();
|
|
106
|
+
}
|
|
107
|
+
loadLatestSnapshot(id) {
|
|
108
|
+
return this.storage.getCollection("snapshots").getLatestSnapshot(id);
|
|
109
|
+
}
|
|
110
|
+
save(collectionName, id, data, trx) {
|
|
111
|
+
if (trx) trx.markWritten(collectionName, id, data);
|
|
112
|
+
else this.storage.getCollection(collectionName).save(id, data);
|
|
113
|
+
}
|
|
114
|
+
create(collectionName, id, data, trx) {
|
|
115
|
+
if (trx) trx.markCreated(collectionName, id, data);
|
|
116
|
+
else {
|
|
117
|
+
if (this.storage.getCollection(collectionName).get(id)) throw new Error(`Document with id ${id} already exists`);
|
|
118
|
+
this.storage.getCollection(collectionName).save(id, data);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
static transactionTries = 5;
|
|
122
|
+
async transactionally(fn) {
|
|
123
|
+
let trx = new InMemoryTransaction(new InMemoryUnderlyingTransaction());
|
|
124
|
+
let retry = InMemoryDatabase.transactionTries;
|
|
125
|
+
let latestReturnValue = void 0;
|
|
126
|
+
while (retry--) try {
|
|
127
|
+
latestReturnValue = await fn(trx);
|
|
128
|
+
this.streamId(trx);
|
|
129
|
+
break;
|
|
130
|
+
} catch (error) {
|
|
131
|
+
if (error instanceof TransactionCollision) trx = new InMemoryTransaction(new InMemoryUnderlyingTransaction());
|
|
132
|
+
else throw error;
|
|
133
|
+
}
|
|
134
|
+
if (retry === -1) throw new TransactionCollidedTooManyTimes(InMemoryDatabase.transactionTries);
|
|
135
|
+
return latestReturnValue;
|
|
136
|
+
}
|
|
137
|
+
streamId(trx) {
|
|
138
|
+
if (!trx.transaction.checkConsistency(this.storage)) throw new TransactionCollision();
|
|
139
|
+
for (const operation of trx.transaction.operations) {
|
|
140
|
+
if (operation.type === "read") continue;
|
|
141
|
+
if (operation.type === "write") this.save(operation.collectionName, operation.id, operation.data);
|
|
142
|
+
if (operation.type === "delete") this.delete(operation.collectionName, operation.id);
|
|
143
|
+
if (operation.type === "create") this.create(operation.collectionName, operation.id, operation.data);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
print() {
|
|
147
|
+
console.log(["Database:", this.storage.toPretty()].join("\n"));
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
152
|
+
export { CannotReadAfterWrites, InMemoryDatabase, InMemoryUnderlyingTransaction, TransactionCollidedTooManyTimes };
|
|
@@ -1,38 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return [`Collection: "${collectionName}"`, collection.toPretty()].join("\n");
|
|
33
|
-
})
|
|
34
|
-
.join("\n");
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
exports.Storage = Storage;
|
|
38
|
-
//# sourceMappingURL=in-memory.storage.js.map
|
|
1
|
+
const require_in_memory_collection = require('./in-memory.collection.js');
|
|
2
|
+
|
|
3
|
+
//#region src/store/in-memory.storage.ts
|
|
4
|
+
var Storage = class Storage {
|
|
5
|
+
constructor(collections = /* @__PURE__ */ new Map()) {
|
|
6
|
+
this.collections = collections;
|
|
7
|
+
}
|
|
8
|
+
clone() {
|
|
9
|
+
const clone = /* @__PURE__ */ new Map();
|
|
10
|
+
for (const [collectionName, collection] of this.collections) clone.set(collectionName, collection.clone());
|
|
11
|
+
return new Storage(clone);
|
|
12
|
+
}
|
|
13
|
+
merge(other) {
|
|
14
|
+
const collections = /* @__PURE__ */ new Map();
|
|
15
|
+
for (const [collectionName, collection] of other.collections) collections.set(collectionName, this.getCollection(collectionName).merge(collection));
|
|
16
|
+
return new Storage(collections);
|
|
17
|
+
}
|
|
18
|
+
getCollection(collectionName) {
|
|
19
|
+
const collection = this.collections.get(collectionName) || new require_in_memory_collection.Collection();
|
|
20
|
+
this.collections.set(collectionName, collection);
|
|
21
|
+
return collection;
|
|
22
|
+
}
|
|
23
|
+
toPretty() {
|
|
24
|
+
return [...this.collections.entries()].map(([collectionName, collection]) => {
|
|
25
|
+
return [`Collection: "${collectionName}"`, collection.toPretty()].join("\n");
|
|
26
|
+
}).join("\n");
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
exports.Storage = Storage;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Collection } from "./in-memory.collection.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/store/in-memory.storage.ts
|
|
4
|
+
var Storage = class Storage {
|
|
5
|
+
constructor(collections = /* @__PURE__ */ new Map()) {
|
|
6
|
+
this.collections = collections;
|
|
7
|
+
}
|
|
8
|
+
clone() {
|
|
9
|
+
const clone = /* @__PURE__ */ new Map();
|
|
10
|
+
for (const [collectionName, collection] of this.collections) clone.set(collectionName, collection.clone());
|
|
11
|
+
return new Storage(clone);
|
|
12
|
+
}
|
|
13
|
+
merge(other) {
|
|
14
|
+
const collections = /* @__PURE__ */ new Map();
|
|
15
|
+
for (const [collectionName, collection] of other.collections) collections.set(collectionName, this.getCollection(collectionName).merge(collection));
|
|
16
|
+
return new Storage(collections);
|
|
17
|
+
}
|
|
18
|
+
getCollection(collectionName) {
|
|
19
|
+
const collection = this.collections.get(collectionName) || new Collection();
|
|
20
|
+
this.collections.set(collectionName, collection);
|
|
21
|
+
return collection;
|
|
22
|
+
}
|
|
23
|
+
toPretty() {
|
|
24
|
+
return [...this.collections.entries()].map(([collectionName, collection]) => {
|
|
25
|
+
return [`Collection: "${collectionName}"`, collection.toPretty()].join("\n");
|
|
26
|
+
}).join("\n");
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
export { Storage };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ISerializer, Store,
|
|
1
|
+
import type { ISerializer, Store, IIdentifiable } from "@ddd-ts/core";
|
|
2
2
|
import { InMemoryDatabase } from "./in-memory.database";
|
|
3
3
|
import { InMemoryTransaction } from "../in-memory.transaction";
|
|
4
4
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.store.d.ts","sourceRoot":"","sources":["../../src/store/in-memory.store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"in-memory.store.d.ts","sourceRoot":"","sources":["../../src/store/in-memory.store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AACtE,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D;;;GAGG;AACH,qBAAa,aAAa,CAAC,CAAC,SAAS,aAAa,CAAE,YAAW,KAAK,CAAC,CAAC,CAAC;aAEnD,UAAU,EAAE,MAAM;aAClB,QAAQ,EAAE,gBAAgB;aAC1B,UAAU,EAAE,WAAW,CAAC,CAAC,CAAC;aAC1B,KAAK,CAAC,EAAE,MAAM;gBAHd,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,gBAAgB,EAC1B,UAAU,EAAE,WAAW,CAAC,CAAC,CAAC,EAC1B,KAAK,CAAC,EAAE,MAAM,YAAA;IAG1B,MAAM,CACV,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,EAChC,GAAG,CAAC,EAAE,mBAAmB,GACxB,OAAO,CAAC,CAAC,EAAE,CAAC;IAiBf,KAAK;IAIC,MAAM,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAU1D,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAUxD,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAI9D,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAiB1E,OAAO,CAAC,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC;IAc1C,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC;IAKjE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAI7D,QAAQ;IAIP,SAAS,IAAI,aAAa,CAAC,CAAC,CAAC;CAKrC"}
|