@ddd-ts/store-inmemory 0.0.36 → 0.0.37
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 +15 -6
- package/dist/in-memory.transaction.d.ts +0 -18
- package/dist/in-memory.transaction.d.ts.map +0 -1
- package/dist/in-memory.transaction.js +0 -36
- package/dist/in-memory.transaction.js.map +0 -1
- package/dist/index.d.ts +0 -4
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -15
- package/dist/index.js.map +0 -1
- package/dist/store/in-memory.collection.d.ts +0 -29
- package/dist/store/in-memory.collection.d.ts.map +0 -1
- package/dist/store/in-memory.collection.js +0 -75
- package/dist/store/in-memory.collection.js.map +0 -1
- package/dist/store/in-memory.database.d.ts +0 -73
- package/dist/store/in-memory.database.d.ts.map +0 -1
- package/dist/store/in-memory.database.js +0 -177
- package/dist/store/in-memory.database.js.map +0 -1
- package/dist/store/in-memory.storage.d.ts +0 -10
- package/dist/store/in-memory.storage.d.ts.map +0 -1
- package/dist/store/in-memory.storage.js +0 -38
- package/dist/store/in-memory.storage.js.map +0 -1
- package/dist/store/in-memory.store.d.ts +0 -26
- package/dist/store/in-memory.store.d.ts.map +0 -1
- package/dist/store/in-memory.store.js +0 -84
- package/dist/store/in-memory.store.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,22 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ddd-ts/store-inmemory",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"main": "dist/index.js",
|
|
3
|
+
"version": "0.0.37",
|
|
5
4
|
"types": "dist/index.d.ts",
|
|
6
5
|
"license": "MIT",
|
|
7
6
|
"repository": {
|
|
8
7
|
"url": "git+https://github.com/ddd-ts/monorepo"
|
|
9
8
|
},
|
|
10
9
|
"scripts": {
|
|
11
|
-
"build": "
|
|
10
|
+
"build": "tsdown --config node_modules/@ddd-ts/tools/tsdown.config.js"
|
|
12
11
|
},
|
|
13
12
|
"dependencies": {
|
|
14
|
-
"@ddd-ts/core": "0.0.
|
|
13
|
+
"@ddd-ts/core": "0.0.37"
|
|
15
14
|
},
|
|
16
15
|
"devDependencies": {
|
|
17
|
-
"@ddd-ts/tools": "0.0.
|
|
18
|
-
"@ddd-ts/types": "0.0.
|
|
16
|
+
"@ddd-ts/tools": "0.0.37",
|
|
17
|
+
"@ddd-ts/types": "0.0.37",
|
|
19
18
|
"@types/jest": "^29.5.1",
|
|
20
19
|
"@types/node": "^20.12.4"
|
|
20
|
+
},
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./src/index.ts",
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"exports": {
|
|
27
|
+
".": "./dist/index.js",
|
|
28
|
+
"./package.json": "./package.json"
|
|
29
|
+
}
|
|
21
30
|
}
|
|
22
31
|
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { TransactionPerformer } from "@ddd-ts/core";
|
|
2
|
-
import { InMemoryDatabase, InMemoryUnderlyingTransaction } from "./store/in-memory.database";
|
|
3
|
-
export declare class InMemoryTransaction {
|
|
4
|
-
readonly transaction: InMemoryUnderlyingTransaction;
|
|
5
|
-
commitListeners: (() => void)[];
|
|
6
|
-
constructor(transaction: InMemoryUnderlyingTransaction);
|
|
7
|
-
counter: number;
|
|
8
|
-
increment(): number;
|
|
9
|
-
onCommit(callback: () => void): void;
|
|
10
|
-
executeCommitListeners(): Promise<void>;
|
|
11
|
-
}
|
|
12
|
-
export declare class InMemoryTransactionPerformer extends TransactionPerformer<InMemoryTransaction> {
|
|
13
|
-
constructor(db: InMemoryDatabase);
|
|
14
|
-
}
|
|
15
|
-
export declare class FakeInMemoryTransactionPerformer extends TransactionPerformer<InMemoryTransaction> {
|
|
16
|
-
constructor();
|
|
17
|
-
}
|
|
18
|
-
//# sourceMappingURL=in-memory.transaction.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.transaction.d.ts","sourceRoot":"","sources":["../src/in-memory.transaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EACL,gBAAgB,EAChB,6BAA6B,EAC9B,MAAM,4BAA4B,CAAC;AAEpC,qBAAa,mBAAmB;aAGF,WAAW,EAAE,6BAA6B;IAFtE,eAAe,EAAE,CAAC,MAAM,IAAI,CAAC,EAAE,CAAM;gBAET,WAAW,EAAE,6BAA6B;IAEtE,OAAO,SAAM;IAEb,SAAS;IAKT,QAAQ,CAAC,QAAQ,EAAE,MAAM,IAAI;IAIvB,sBAAsB;CAG7B;AAED,qBAAa,4BAA6B,SAAQ,oBAAoB,CAAC,mBAAmB,CAAC;gBAC7E,EAAE,EAAE,gBAAgB;CAGjC;AAED,qBAAa,gCAAiC,SAAQ,oBAAoB,CAAC,mBAAmB,CAAC;;CAI9F"}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FakeInMemoryTransactionPerformer = exports.InMemoryTransactionPerformer = exports.InMemoryTransaction = void 0;
|
|
4
|
-
const core_1 = require("@ddd-ts/core");
|
|
5
|
-
class InMemoryTransaction {
|
|
6
|
-
transaction;
|
|
7
|
-
commitListeners = [];
|
|
8
|
-
constructor(transaction) {
|
|
9
|
-
this.transaction = transaction;
|
|
10
|
-
}
|
|
11
|
-
counter = -1;
|
|
12
|
-
increment() {
|
|
13
|
-
this.counter++;
|
|
14
|
-
return this.counter;
|
|
15
|
-
}
|
|
16
|
-
onCommit(callback) {
|
|
17
|
-
this.commitListeners.push(callback);
|
|
18
|
-
}
|
|
19
|
-
async executeCommitListeners() {
|
|
20
|
-
await Promise.all(this.commitListeners.map((cb) => cb()));
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
exports.InMemoryTransaction = InMemoryTransaction;
|
|
24
|
-
class InMemoryTransactionPerformer extends core_1.TransactionPerformer {
|
|
25
|
-
constructor(db) {
|
|
26
|
-
super((effect) => db.transactionally((trx) => effect(trx)));
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
exports.InMemoryTransactionPerformer = InMemoryTransactionPerformer;
|
|
30
|
-
class FakeInMemoryTransactionPerformer extends core_1.TransactionPerformer {
|
|
31
|
-
constructor() {
|
|
32
|
-
super((effect) => effect(new InMemoryTransaction(null)));
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
exports.FakeInMemoryTransactionPerformer = FakeInMemoryTransactionPerformer;
|
|
36
|
-
//# sourceMappingURL=in-memory.transaction.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.transaction.js","sourceRoot":"","sources":["../src/in-memory.transaction.ts"],"names":[],"mappings":";;;AAAA,uCAAoD;AAMpD,MAAa,mBAAmB;IAGF;IAF5B,eAAe,GAAmB,EAAE,CAAC;IAErC,YAA4B,WAA0C;QAA1C,gBAAW,GAAX,WAAW,CAA+B;IAAG,CAAC;IAE1E,OAAO,GAAG,CAAC,CAAC,CAAC;IAEb,SAAS;QACP,IAAI,CAAC,OAAO,EAAE,CAAC;QACf,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,QAAQ,CAAC,QAAoB;QAC3B,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACtC,CAAC;IAED,KAAK,CAAC,sBAAsB;QAC1B,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IAC5D,CAAC;CACF;AAnBD,kDAmBC;AAED,MAAa,4BAA6B,SAAQ,2BAAyC;IACzF,YAAY,EAAoB;QAC9B,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC,eAAe,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAC9D,CAAC;CACF;AAJD,oEAIC;AAED,MAAa,gCAAiC,SAAQ,2BAAyC;IAC7F;QACE,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,mBAAmB,CAAC,IAAW,CAAC,CAAC,CAAC,CAAC;IAClE,CAAC;CACF;AAJD,4EAIC","sourcesContent":["import { TransactionPerformer } from \"@ddd-ts/core\";\nimport {\n InMemoryDatabase,\n InMemoryUnderlyingTransaction,\n} from \"./store/in-memory.database\";\n\nexport class InMemoryTransaction {\n commitListeners: (() => void)[] = [];\n\n constructor(public readonly transaction: InMemoryUnderlyingTransaction) {}\n\n counter = -1;\n\n increment() {\n this.counter++;\n return this.counter;\n }\n\n onCommit(callback: () => void) {\n this.commitListeners.push(callback);\n }\n\n async executeCommitListeners() {\n await Promise.all(this.commitListeners.map((cb) => cb()));\n }\n}\n\nexport class InMemoryTransactionPerformer extends TransactionPerformer<InMemoryTransaction> {\n constructor(db: InMemoryDatabase) {\n super((effect) => db.transactionally((trx) => effect(trx)));\n }\n}\n\nexport class FakeInMemoryTransactionPerformer extends TransactionPerformer<InMemoryTransaction> {\n constructor() {\n super((effect) => effect(new InMemoryTransaction(null as any)));\n }\n}\n"]}
|
package/dist/index.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export { InMemoryTransactionPerformer, FakeInMemoryTransactionPerformer, InMemoryTransaction, } from "./in-memory.transaction";
|
|
2
|
-
export { InMemoryDatabase, InMemoryUnderlyingTransaction, CannotReadAfterWrites, TransactionCollidedTooManyTimes, } from "./store/in-memory.database";
|
|
3
|
-
export { InMemoryStore } from "./store/in-memory.store";
|
|
4
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,4BAA4B,EAC5B,gCAAgC,EAChC,mBAAmB,GACnB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACN,gBAAgB,EAChB,6BAA6B,EAC7B,qBAAqB,EACrB,+BAA+B,GAC/B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC"}
|
package/dist/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.InMemoryStore = exports.TransactionCollidedTooManyTimes = exports.CannotReadAfterWrites = exports.InMemoryUnderlyingTransaction = exports.InMemoryDatabase = exports.InMemoryTransaction = exports.FakeInMemoryTransactionPerformer = exports.InMemoryTransactionPerformer = void 0;
|
|
4
|
-
var in_memory_transaction_1 = require("./in-memory.transaction");
|
|
5
|
-
Object.defineProperty(exports, "InMemoryTransactionPerformer", { enumerable: true, get: function () { return in_memory_transaction_1.InMemoryTransactionPerformer; } });
|
|
6
|
-
Object.defineProperty(exports, "FakeInMemoryTransactionPerformer", { enumerable: true, get: function () { return in_memory_transaction_1.FakeInMemoryTransactionPerformer; } });
|
|
7
|
-
Object.defineProperty(exports, "InMemoryTransaction", { enumerable: true, get: function () { return in_memory_transaction_1.InMemoryTransaction; } });
|
|
8
|
-
var in_memory_database_1 = require("./store/in-memory.database");
|
|
9
|
-
Object.defineProperty(exports, "InMemoryDatabase", { enumerable: true, get: function () { return in_memory_database_1.InMemoryDatabase; } });
|
|
10
|
-
Object.defineProperty(exports, "InMemoryUnderlyingTransaction", { enumerable: true, get: function () { return in_memory_database_1.InMemoryUnderlyingTransaction; } });
|
|
11
|
-
Object.defineProperty(exports, "CannotReadAfterWrites", { enumerable: true, get: function () { return in_memory_database_1.CannotReadAfterWrites; } });
|
|
12
|
-
Object.defineProperty(exports, "TransactionCollidedTooManyTimes", { enumerable: true, get: function () { return in_memory_database_1.TransactionCollidedTooManyTimes; } });
|
|
13
|
-
var in_memory_store_1 = require("./store/in-memory.store");
|
|
14
|
-
Object.defineProperty(exports, "InMemoryStore", { enumerable: true, get: function () { return in_memory_store_1.InMemoryStore; } });
|
|
15
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iEAIiC;AAHhC,qIAAA,4BAA4B,OAAA;AAC5B,yIAAA,gCAAgC,OAAA;AAChC,4HAAA,mBAAmB,OAAA;AAEpB,iEAKoC;AAJnC,sHAAA,gBAAgB,OAAA;AAChB,mIAAA,6BAA6B,OAAA;AAC7B,2HAAA,qBAAqB,OAAA;AACrB,qIAAA,+BAA+B,OAAA;AAEhC,2DAAwD;AAA/C,gHAAA,aAAa,OAAA","sourcesContent":["export {\n\tInMemoryTransactionPerformer,\n\tFakeInMemoryTransactionPerformer,\n\tInMemoryTransaction,\n} from \"./in-memory.transaction\";\nexport {\n\tInMemoryDatabase,\n\tInMemoryUnderlyingTransaction,\n\tCannotReadAfterWrites,\n\tTransactionCollidedTooManyTimes,\n} from \"./store/in-memory.database\";\nexport { InMemoryStore } from \"./store/in-memory.store\";\n"]}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
export declare class Collection {
|
|
2
|
-
private data;
|
|
3
|
-
constructor(data?: Map<string, {
|
|
4
|
-
savedAt: bigint;
|
|
5
|
-
data: any;
|
|
6
|
-
}>);
|
|
7
|
-
clear(): void;
|
|
8
|
-
getLatestSnapshot(id: string): any;
|
|
9
|
-
clone(): Collection;
|
|
10
|
-
merge(other: Collection): Collection;
|
|
11
|
-
delete(id: string): void;
|
|
12
|
-
getRaw(id: string): {
|
|
13
|
-
savedAt: bigint;
|
|
14
|
-
data: any;
|
|
15
|
-
} | undefined;
|
|
16
|
-
countAll(): number;
|
|
17
|
-
get(id: string): any;
|
|
18
|
-
getAllRaw(): {
|
|
19
|
-
id: string;
|
|
20
|
-
data: {
|
|
21
|
-
savedAt: bigint;
|
|
22
|
-
data: any;
|
|
23
|
-
};
|
|
24
|
-
}[];
|
|
25
|
-
getAll(): any[];
|
|
26
|
-
save(id: string, data: any): void;
|
|
27
|
-
toPretty(): string;
|
|
28
|
-
}
|
|
29
|
-
//# sourceMappingURL=in-memory.collection.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.collection.d.ts","sourceRoot":"","sources":["../../src/store/in-memory.collection.ts"],"names":[],"mappings":"AAOA,qBAAa,UAAU;IAEnB,OAAO,CAAC,IAAI;gBAAJ,IAAI,GAAE,GAAG,CAAC,MAAM,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,GAAG,CAAA;KAAE,CAAa;IAGvE,KAAK;IAIL,iBAAiB,CAAC,EAAE,EAAE,MAAM;IAO5B,KAAK;IAQL,KAAK,CAAC,KAAK,EAAE,UAAU;IAWvB,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;IAIxB,MAAM,CAAC,EAAE,EAAE,MAAM;iBArCsB,MAAM;cAAQ,GAAG;;IAyCxD,QAAQ;IAIR,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,GAAG;IAIpB,SAAS;;;qBAjD8B,MAAM;kBAAQ,GAAG;;;IAqDxD,MAAM,IAAI,GAAG,EAAE;IAIf,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,GAAG,IAAI;IAIjC,QAAQ;CAQT"}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Collection = void 0;
|
|
4
|
-
function now() {
|
|
5
|
-
if (typeof process === "object" && typeof process.hrtime === "function") {
|
|
6
|
-
return process.hrtime.bigint() / BigInt(1000);
|
|
7
|
-
}
|
|
8
|
-
return BigInt(Date.now() * 1_000);
|
|
9
|
-
}
|
|
10
|
-
class Collection {
|
|
11
|
-
data;
|
|
12
|
-
constructor(data = new Map()) {
|
|
13
|
-
this.data = data;
|
|
14
|
-
}
|
|
15
|
-
clear() {
|
|
16
|
-
this.data.clear();
|
|
17
|
-
}
|
|
18
|
-
getLatestSnapshot(id) {
|
|
19
|
-
const data = [...this.data.values()];
|
|
20
|
-
const sameId = data.filter((d) => d.data.id === id);
|
|
21
|
-
const sorted = sameId.sort((a, b) => (b.savedAt > a.savedAt ? 1 : -1));
|
|
22
|
-
return sorted[0]?.data;
|
|
23
|
-
}
|
|
24
|
-
clone() {
|
|
25
|
-
const clone = new Map();
|
|
26
|
-
for (const [key, value] of this.data) {
|
|
27
|
-
clone.set(key, value);
|
|
28
|
-
}
|
|
29
|
-
return new Collection(clone);
|
|
30
|
-
}
|
|
31
|
-
merge(other) {
|
|
32
|
-
const merge = new Map();
|
|
33
|
-
for (const [key, value] of this.data) {
|
|
34
|
-
merge.set(key, value);
|
|
35
|
-
}
|
|
36
|
-
for (const [key, value] of other.data) {
|
|
37
|
-
merge.set(key, value);
|
|
38
|
-
}
|
|
39
|
-
return new Collection(merge);
|
|
40
|
-
}
|
|
41
|
-
delete(id) {
|
|
42
|
-
this.data.delete(id);
|
|
43
|
-
}
|
|
44
|
-
getRaw(id) {
|
|
45
|
-
return this.data.get(id);
|
|
46
|
-
}
|
|
47
|
-
countAll() {
|
|
48
|
-
return this.data.size;
|
|
49
|
-
}
|
|
50
|
-
get(id) {
|
|
51
|
-
return this.data.get(id)?.data;
|
|
52
|
-
}
|
|
53
|
-
getAllRaw() {
|
|
54
|
-
return [...this.data.entries()].map(([id, data]) => ({ id, data }));
|
|
55
|
-
}
|
|
56
|
-
getAll() {
|
|
57
|
-
return [...this.data.entries()].map(([id, data]) => data.data);
|
|
58
|
-
}
|
|
59
|
-
save(id, data) {
|
|
60
|
-
this.data.set(id, { savedAt: now(), data });
|
|
61
|
-
}
|
|
62
|
-
toPretty() {
|
|
63
|
-
return [...this.data.entries()]
|
|
64
|
-
.map(([id, data]) => `\t\t"${id}": ${JSON.stringify(data.data, replaceBigInt)}`)
|
|
65
|
-
.join(",\n");
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
exports.Collection = Collection;
|
|
69
|
-
function replaceBigInt(key, value) {
|
|
70
|
-
if (typeof value === "bigint") {
|
|
71
|
-
return value.toString();
|
|
72
|
-
}
|
|
73
|
-
return value;
|
|
74
|
-
}
|
|
75
|
-
//# sourceMappingURL=in-memory.collection.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.collection.js","sourceRoot":"","sources":["../../src/store/in-memory.collection.ts"],"names":[],"mappings":";;;AAAA,SAAS,GAAG;IACV,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;QACxE,OAAO,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAChD,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,CAAC;AACpC,CAAC;AAED,MAAa,UAAU;IAEX;IADV,YACU,OAAoD,IAAI,GAAG,EAAE;QAA7D,SAAI,GAAJ,IAAI,CAAyD;IACpE,CAAC;IAEJ,KAAK;QACH,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACpB,CAAC;IAED,iBAAiB,CAAC,EAAU;QAC1B,MAAM,IAAI,GAAG,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;QACrC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;QACpD,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC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AAG,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC;IACjC,CAAC;IAED,SAAS;QACP,OAAO,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;IACtE,CAAC;IAED,MAAM;QACJ,OAAO,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACjE,CAAC;IAED,IAAI,CAAC,EAAU,EAAE,IAAS;QACxB,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;IAC9C,CAAC;IAED,QAAQ;QACN,OAAO,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;aAC5B,GAAG,CACF,CAAC,CAAC,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,CACb,QAAQ,EAAE,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,aAAa,CAAC,EAAE,CAC7D;aACA,IAAI,CAAC,KAAK,CAAC,CAAC;IACjB,CAAC;CACF;AAvED,gCAuEC;AAED,SAAS,aAAa,CAAC,GAAW,EAAE,KAAU;IAC5C,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;IAC1B,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["function now() {\n if (typeof process === \"object\" && typeof process.hrtime === \"function\") {\n return process.hrtime.bigint() / BigInt(1000);\n }\n return BigInt(Date.now() * 1_000);\n}\n\nexport class Collection {\n constructor(\n private data: Map<string, { savedAt: bigint; data: any }> = new Map(),\n ) {}\n\n clear() {\n this.data.clear();\n }\n\n getLatestSnapshot(id: string) {\n const data = [...this.data.values()];\n const sameId = data.filter((d) => d.data.id === id);\n const sorted = sameId.sort((a, b) => (b.savedAt > a.savedAt ? 1 : -1));\n return sorted[0]?.data;\n }\n\n clone() {\n const clone = new Map();\n for (const [key, value] of this.data) {\n clone.set(key, value);\n }\n return new Collection(clone);\n }\n\n merge(other: Collection) {\n const merge = new Map();\n for (const [key, value] of this.data) {\n merge.set(key, value);\n }\n for (const [key, value] of other.data) {\n merge.set(key, value);\n }\n return new Collection(merge);\n }\n\n delete(id: string): void {\n this.data.delete(id);\n }\n\n getRaw(id: string) {\n return this.data.get(id);\n }\n\n countAll() {\n return this.data.size;\n }\n\n get(id: string): any {\n return this.data.get(id)?.data;\n }\n\n getAllRaw() {\n return [...this.data.entries()].map(([id, data]) => ({ id, data }));\n }\n\n getAll(): any[] {\n return [...this.data.entries()].map(([id, data]) => data.data);\n }\n\n save(id: string, data: any): void {\n this.data.set(id, { savedAt: now(), data });\n }\n\n toPretty() {\n return [...this.data.entries()]\n .map(\n ([id, data]) =>\n `\\t\\t\"${id}\": ${JSON.stringify(data.data, replaceBigInt)}`,\n )\n .join(\",\\n\");\n }\n}\n\nfunction replaceBigInt(key: string, value: any) {\n if (typeof value === \"bigint\") {\n return value.toString();\n }\n return value;\n}\n"]}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { ConcurrencyError } from "@ddd-ts/core";
|
|
2
|
-
import { InMemoryTransaction } from "..";
|
|
3
|
-
import { Storage } from "./in-memory.storage";
|
|
4
|
-
export declare class CannotReadAfterWrites extends Error {
|
|
5
|
-
constructor();
|
|
6
|
-
}
|
|
7
|
-
export declare class TransactionCollision extends Error {
|
|
8
|
-
constructor();
|
|
9
|
-
}
|
|
10
|
-
export declare class DocumentAlreadyExists extends ConcurrencyError {
|
|
11
|
-
constructor();
|
|
12
|
-
}
|
|
13
|
-
export declare class TransactionCollidedTooManyTimes extends Error {
|
|
14
|
-
constructor(tries: number);
|
|
15
|
-
}
|
|
16
|
-
type ReadOperation = {
|
|
17
|
-
type: "read";
|
|
18
|
-
collectionName: string;
|
|
19
|
-
id: string;
|
|
20
|
-
savedAt: bigint | undefined;
|
|
21
|
-
};
|
|
22
|
-
type WriteOperation = {
|
|
23
|
-
type: "write";
|
|
24
|
-
collectionName: string;
|
|
25
|
-
id: string;
|
|
26
|
-
data: any;
|
|
27
|
-
};
|
|
28
|
-
type CreateOperation = {
|
|
29
|
-
type: "create";
|
|
30
|
-
collectionName: string;
|
|
31
|
-
id: string;
|
|
32
|
-
data: any;
|
|
33
|
-
};
|
|
34
|
-
type DeleteOperation = {
|
|
35
|
-
type: "delete";
|
|
36
|
-
collectionName: string;
|
|
37
|
-
id: string;
|
|
38
|
-
savedAt: bigint | undefined;
|
|
39
|
-
};
|
|
40
|
-
type TransactionOperation = ReadOperation | WriteOperation | DeleteOperation | CreateOperation;
|
|
41
|
-
export declare class InMemoryUnderlyingTransaction {
|
|
42
|
-
readonly operations: TransactionOperation[];
|
|
43
|
-
readonly id: string;
|
|
44
|
-
private ensureNoWrites;
|
|
45
|
-
markRead(collectionName: string, id: string, savedAt: bigint | undefined): void;
|
|
46
|
-
markWritten(collectionName: string, id: any, data: any): void;
|
|
47
|
-
markCreated(collectionName: string, id: any, data: any): void;
|
|
48
|
-
markDeleted(collectionName: string, id: any, savedAt: bigint | undefined): void;
|
|
49
|
-
checkConsistency(storage: Storage): boolean;
|
|
50
|
-
}
|
|
51
|
-
export declare class InMemoryDatabase {
|
|
52
|
-
storage: Storage;
|
|
53
|
-
clear(collectionName: string): void;
|
|
54
|
-
load(collectionName: string, id: string, trx?: InMemoryUnderlyingTransaction): any;
|
|
55
|
-
delete(collectionName: string, id: string, trx?: InMemoryUnderlyingTransaction): void;
|
|
56
|
-
countAll(collectionName: string): number;
|
|
57
|
-
loadAll(collectionName: string): {
|
|
58
|
-
id: string;
|
|
59
|
-
data: {
|
|
60
|
-
savedAt: bigint;
|
|
61
|
-
data: any;
|
|
62
|
-
};
|
|
63
|
-
}[];
|
|
64
|
-
loadLatestSnapshot(id: string): any;
|
|
65
|
-
save(collectionName: string, id: string, data: any, trx?: InMemoryUnderlyingTransaction): void;
|
|
66
|
-
create(collectionName: string, id: string, data: any, trx?: InMemoryUnderlyingTransaction): void;
|
|
67
|
-
private static transactionTries;
|
|
68
|
-
transactionally(fn: (trx: InMemoryTransaction) => any): Promise<any>;
|
|
69
|
-
private streamId;
|
|
70
|
-
print(): void;
|
|
71
|
-
}
|
|
72
|
-
export {};
|
|
73
|
-
//# sourceMappingURL=in-memory.database.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.database.d.ts","sourceRoot":"","sources":["../../src/store/in-memory.database.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,mBAAmB,EAAE,MAAM,IAAI,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAE9C,qBAAa,qBAAsB,SAAQ,KAAK;;CAI/C;AAED,qBAAa,oBAAqB,SAAQ,KAAK;;CAI9C;AAED,qBAAa,qBAAsB,SAAQ,gBAAgB;;CAI1D;AAED,qBAAa,+BAAgC,SAAQ,KAAK;gBAC5C,KAAK,EAAE,MAAM;CAG1B;AAED,KAAK,aAAa,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,cAAc,EAAE,MAAM,CAAC;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;CAC7B,CAAC;AAEF,KAAK,cAAc,GAAG;IACpB,IAAI,EAAE,OAAO,CAAC;IACd,cAAc,EAAE,MAAM,CAAC;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,GAAG,CAAC;CACX,CAAC;AAEF,KAAK,e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/B,OAAO,CAAC,cAAc,EAAE,MAAM;;;;;;;IAI9B,kBAAkB,CAAC,EAAE,EAAE,MAAM;IAI7B,IAAI,CACF,cAAc,EAAE,MAAM,EACtB,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,GAAG,EACT,GAAG,CAAC,EAAE,6BAA6B,GAClC,IAAI;IAQP,MAAM,CACJ,cAAc,EAAE,MAAM,EACtB,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,GAAG,EACT,GAAG,CAAC,EAAE,6BAA6B,GAClC,IAAI;IAWP,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAAK;IAE9B,eAAe,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,mBAAmB,KAAK,GAAG;IA2B3D,OAAO,CAAC,QAAQ;IAqBhB,KAAK;CAGN"}
|
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.InMemoryDatabase = exports.InMemoryUnderlyingTransaction = exports.TransactionCollidedTooManyTimes = exports.DocumentAlreadyExists = exports.TransactionCollision = exports.CannotReadAfterWrites = void 0;
|
|
4
|
-
const core_1 = require("@ddd-ts/core");
|
|
5
|
-
const __1 = require("..");
|
|
6
|
-
const in_memory_storage_1 = require("./in-memory.storage");
|
|
7
|
-
class CannotReadAfterWrites extends Error {
|
|
8
|
-
constructor() {
|
|
9
|
-
super("Cannot read after having written into a transaction");
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
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
|
-
exports.InMemoryDatabase = InMemoryDatabase;
|
|
177
|
-
//# sourceMappingURL=in-memory.database.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.database.js","sourceRoot":"","sources":["../../src/store/in-memory.database.ts"],"names":[],"mappings":";;;AAAA,uCAAgD;AAChD,0BAAyC;AACzC,2DAA8C;AAE9C,MAAa,qBAAsB,SAAQ,KAAK;IAC9C;QACE,KAAK,CAAC,qDAAqD,CAAC,CAAC;IAC/D,CAAC;CACF;AAJD,sDAIC;AAED,MAAa,oBAAqB,SAAQ,KAAK;IAC7C;QACE,KAAK,CAAC,mDAAmD,CAAC,CAAC;IAC7D,CAAC;CACF;AAJD,oDAIC;AAED,MAAa,qBAAsB,SAAQ,uBAAgB;IACzD;QACE,KAAK,CAAC,yBAAyB,CAAC,CAAC;IACnC,CAAC;CACF;AAJD,sDAIC;AAED,MAAa,+BAAgC,SAAQ,KAAK;IACxD,YAAY,KAAa;QACvB,KAAK,CAAC,wCAAwC,KAAK,GAAG,CAAC,CAAC;IAC1D,CAAC;CACF;AAJD,0EAIC;AAoCD,MAAa,6BAA6B;IACxB,UAAU,GAA2B,EAAE,CAAC;IAExC,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;IAEnD,cAAc;QACpB,IACE,IAAI,CAAC,UAAU,CAAC,IAAI,CAClB,CAAC,SAAS,EAAE,EAAE,CACZ,SAAS,CAAC,IAAI,KAAK,OAAO;YAC1B,SAAS,CAAC,IAAI,KAA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MAAM,SAAS,IAAI,GAAG,CAAC,WAAW,CAAC,UAAU,EAAE,CAAC;YACnD,IAAI,SAAS,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;gBAC9B,SAAS;YACX,CAAC;YACD,IAAI,SAAS,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;gBAC/B,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,cAAc,EAAE,SAAS,CAAC,EAAE,EAAE,SAAS,CAAC,IAAI,CAAC,CAAC;YACpE,CAAC;YACD,IAAI,SAAS,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;gBAChC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,EAAE,SAAS,CAAC,EAAE,CAAC,CAAC;YACtD,CAAC;YACD,IAAI,SAAS,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;gBAChC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,EAAE,SAAS,CAAC,EAAE,EAAE,SAAS,CAAC,IAAI,CAAC,CAAC;YACtE,CAAC;QACH,CAAC;IACH,CAAC;IAED,KAAK;QACH,OAAO,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACjE,CAAC;;AA/HH,4CAgIC","sourcesContent":["import { ConcurrencyError } from \"@ddd-ts/core\";\nimport { InMemoryTransaction } from \"..\";\nimport { Storage } from \"./in-memory.storage\";\n\nexport class CannotReadAfterWrites extends Error {\n constructor() {\n super(\"Cannot read after having written into a transaction\");\n }\n}\n\nexport class TransactionCollision extends Error {\n constructor() {\n super(\"Transaction has collided with other extern writes\");\n }\n}\n\nexport class DocumentAlreadyExists extends ConcurrencyError {\n constructor() {\n super(\"Document already exists\");\n }\n}\n\nexport class TransactionCollidedTooManyTimes extends Error {\n constructor(tries: number) {\n super(`Transaction collided too many times (${tries})`);\n }\n}\n\ntype ReadOperation = {\n type: \"read\";\n collectionName: string;\n id: string;\n savedAt: bigint | undefined;\n};\n\ntype WriteOperation = {\n type: \"write\";\n collectionName: string;\n id: string;\n data: any;\n};\n\ntype CreateOperation = {\n type: \"create\";\n collectionName: string;\n id: string;\n data: any;\n};\n\ntype DeleteOperation = {\n type: \"delete\";\n collectionName: string;\n id: string;\n savedAt: bigint | undefined;\n};\n\ntype TransactionOperation =\n | ReadOperation\n | WriteOperation\n | DeleteOperation\n | CreateOperation;\n\nexport class InMemoryUnderlyingTransaction {\n public readonly operations: TransactionOperation[] = [];\n\n public readonly id = Math.random().toString().substring(2);\n\n private ensureNoWrites() {\n if (\n this.operations.some(\n (operation) =>\n operation.type === \"write\" ||\n operation.type === \"create\" ||\n operation.type === \"delete\",\n )\n ) {\n throw new CannotReadAfterWrites();\n }\n }\n\n public markRead(\n collectionName: string,\n id: string,\n savedAt: bigint | undefined,\n ) {\n this.ensureNoWrites();\n this.operations.push({ type: \"read\", collectionName, id, savedAt });\n }\n\n public markWritten(collectionName: string, id: any, data: any) {\n this.operations.push({ type: \"write\", collectionName, id, data });\n }\n\n public markCreated(collectionName: string, id: any, data: any) {\n this.operations.push({ type: \"create\", collectionName, id, data });\n }\n\n public markDeleted(\n collectionName: string,\n id: any,\n savedAt: bigint | undefined,\n ) {\n this.operations.push({ type: \"delete\", collectionName, id, savedAt });\n }\n\n public checkConsistency(storage: Storage) {\n for (const operation of this.operations) {\n if (operation.type === \"read\") {\n const collection = storage.getCollection(operation.collectionName);\n\n if (!collection) {\n return false;\n }\n\n if (operation.savedAt !== collection.getRaw(operation.id)?.savedAt) {\n return false;\n }\n }\n\n if (operation.type === \"create\") {\n const collection = storage.getCollection(operation.collectionName);\n\n if (\n collection &&\n collection.getRaw(operation.id)?.savedAt !== undefined\n ) {\n throw new DocumentAlreadyExists();\n }\n }\n }\n return true;\n }\n}\n\nexport class InMemoryDatabase {\n public storage = new Storage();\n\n clear(collectionName: string) {\n this.storage.getCollection(collectionName).clear();\n }\n\n load(\n collectionName: string,\n id: string,\n trx?: InMemoryUnderlyingTransaction,\n ): any {\n const collection = this.storage.getCollection(collectionName);\n const data = collection.get(id);\n if (trx) {\n const doc = collection.getRaw(id);\n trx.markRead(collectionName, id, doc?.savedAt);\n }\n return data;\n }\n\n delete(\n collectionName: string,\n id: string,\n trx?: InMemoryUnderlyingTransaction,\n ): void {\n if (trx) {\n const doc = this.storage.getCollection(collectionName).getRaw(id);\n trx.markDeleted(collectionName, id, doc?.savedAt);\n } else {\n this.storage.getCollection(collectionName).delete(id);\n }\n }\n\n countAll(collectionName: string) {\n return this.storage.getCollection(collectionName).countAll();\n }\n\n loadAll(collectionName: string) {\n return this.storage.getCollection(collectionName).getAllRaw();\n }\n\n loadLatestSnapshot(id: string) {\n return this.storage.getCollection(\"snapshots\").getLatestSnapshot(id);\n }\n\n save(\n collectionName: string,\n id: string,\n data: any,\n trx?: InMemoryUnderlyingTransaction,\n ): void {\n if (trx) {\n trx.markWritten(collectionName, id, data);\n } else {\n this.storage.getCollection(collectionName).save(id, data);\n }\n }\n\n create(\n collectionName: string,\n id: string,\n data: any,\n trx?: InMemoryUnderlyingTransaction,\n ): void {\n if (trx) {\n trx.markCreated(collectionName, id, data);\n } else {\n if (this.storage.getCollection(collectionName).get(id)) {\n throw new Error(`Document with id ${id} already exists`);\n }\n this.storage.getCollection(collectionName).save(id, data);\n }\n }\n\n private static transactionTries = 5;\n\n async transactionally(fn: (trx: InMemoryTransaction) => any) {\n let trx = new InMemoryTransaction(new InMemoryUnderlyingTransaction());\n let retry = InMemoryDatabase.transactionTries;\n let latestReturnValue = undefined;\n while (retry--) {\n try {\n latestReturnValue = await fn(trx);\n this.streamId(trx);\n break;\n } catch (error) {\n if (error instanceof TransactionCollision) {\n trx = new InMemoryTransaction(new InMemoryUnderlyingTransaction());\n } else {\n throw error;\n }\n }\n }\n\n if (retry === -1) {\n throw new TransactionCollidedTooManyTimes(\n InMemoryDatabase.transactionTries,\n );\n }\n\n return latestReturnValue;\n }\n\n private streamId(trx: InMemoryTransaction) {\n if (!trx.transaction.checkConsistency(this.storage)) {\n throw new TransactionCollision();\n }\n\n for (const operation of trx.transaction.operations) {\n if (operation.type === \"read\") {\n continue;\n }\n if (operation.type === \"write\") {\n this.save(operation.collectionName, operation.id, operation.data);\n }\n if (operation.type === \"delete\") {\n this.delete(operation.collectionName, operation.id);\n }\n if (operation.type === \"create\") {\n this.create(operation.collectionName, operation.id, operation.data);\n }\n }\n }\n\n print() {\n console.log([\"Database:\", this.storage.toPretty()].join(\"\\n\"));\n }\n}\n"]}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { Collection } from "./in-memory.collection";
|
|
2
|
-
export declare class Storage {
|
|
3
|
-
collections: Map<string, Collection>;
|
|
4
|
-
constructor(collections?: Map<string, Collection>);
|
|
5
|
-
clone(): Storage;
|
|
6
|
-
merge(other: Storage): Storage;
|
|
7
|
-
getCollection(collectionName: string): Collection;
|
|
8
|
-
toPretty(): string;
|
|
9
|
-
}
|
|
10
|
-
//# sourceMappingURL=in-memory.storage.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.storage.d.ts","sourceRoot":"","sources":["../../src/store/in-memory.storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEpD,qBAAa,OAAO;IACC,WAAW,EAAE,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC;gBAApC,WAAW,GAAE,GAAG,CAAC,MAAM,EAAE,UAAU,CAAa;IAEnE,KAAK;IASL,KAAK,CAAC,KAAK,EAAE,OAAO;IAWpB,aAAa,CAAC,cAAc,EAAE,MAAM,GAAG,UAAU;IAMjD,QAAQ;CAST"}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Storage = void 0;
|
|
4
|
-
const in_memory_collection_1 = require("./in-memory.collection");
|
|
5
|
-
class Storage {
|
|
6
|
-
collections;
|
|
7
|
-
constructor(collections = new Map()) {
|
|
8
|
-
this.collections = collections;
|
|
9
|
-
}
|
|
10
|
-
clone() {
|
|
11
|
-
const clone = new Map();
|
|
12
|
-
for (const [collectionName, collection] of this.collections) {
|
|
13
|
-
clone.set(collectionName, collection.clone());
|
|
14
|
-
}
|
|
15
|
-
return new Storage(clone);
|
|
16
|
-
}
|
|
17
|
-
merge(other) {
|
|
18
|
-
const collections = new Map();
|
|
19
|
-
for (const [collectionName, collection] of other.collections) {
|
|
20
|
-
collections.set(collectionName, this.getCollection(collectionName).merge(collection));
|
|
21
|
-
}
|
|
22
|
-
return new Storage(collections);
|
|
23
|
-
}
|
|
24
|
-
getCollection(collectionName) {
|
|
25
|
-
const collection = this.collections.get(collectionName) || new in_memory_collection_1.Collection();
|
|
26
|
-
this.collections.set(collectionName, collection);
|
|
27
|
-
return collection;
|
|
28
|
-
}
|
|
29
|
-
toPretty() {
|
|
30
|
-
return [...this.collections.entries()]
|
|
31
|
-
.map(([collectionName, collection]) => {
|
|
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 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.storage.js","sourceRoot":"","sources":["../../src/store/in-memory.storage.ts"],"names":[],"mappings":";;;AAAA,iEAAoD;AAEpD,MAAa,OAAO;IACC;IAAnB,YAAmB,cAAuC,IAAI,GAAG,EAAE;QAAhD,gBAAW,GAAX,WAAW,CAAqC;IAAG,CAAC;IAEvE,KAAK;QACH,MAAM,KAAK,GAAG,IAAI,GAAG,EAAsB,CAAC;QAC5C,KAAK,MAAM,CAAC,cAAc,EAAE,UAAU,CAAC,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YAC5D,KAAK,CAAC,GAAG,CAAC,cAAc,EAAE,UAAU,CAAC,KAAK,EAAE,CAAC,CAAC;QAChD,CAAC;QAED,OAAO,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,KAAc;QAClB,MAAM,WAAW,GAAG,IAAI,GAAG,EAAsB,CAAC;QAClD,KAAK,MAAM,CAAC,cAAc,EAAE,UAAU,CAAC,IAAI,KAAK,CAAC,WAAW,EAAE,CAAC;YAC7D,WAAW,CAAC,GAAG,CACb,cAAc,EACd,IAAI,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,CACrD,CAAC;QACJ,CAAC;QACD,OAAO,IAAI,OAAO,CAAC,WAAW,CAAC,CAAC;IAClC,CAAC;IAED,aAAa,CAAC,cAAsB;QAClC,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,cAAc,CAAC,IAAI,IAAI,iCAAU,EAAE,CAAC;QAC5E,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,cAAc,EAAE,UAAU,CAAC,CAAC;QACjD,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,QAAQ;QACN,OAAO,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC;aACnC,GAAG,CAAC,CAAC,CAAC,cAAc,EAAE,UAAU,CAAC,EAAE,EAAE;YACpC,OAAO,CAAC,gBAAgB,cAAc,GAAG,EAAE,UAAU,CAAC,QAAQ,EAAE,CAAC,CAAC,IAAI,CACpE,IAAI,CACL,CAAC;QACJ,CAAC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,CAAC;IAChB,CAAC;CACF;AAtCD,0BAsCC","sourcesContent":["import { Collection } from \"./in-memory.collection\";\n\nexport class Storage {\n constructor(public collections: Map<string, Collection> = new Map()) {}\n\n clone() {\n const clone = new Map<string, Collection>();\n for (const [collectionName, collection] of this.collections) {\n clone.set(collectionName, collection.clone());\n }\n\n return new Storage(clone);\n }\n\n merge(other: Storage) {\n const collections = new Map<string, Collection>();\n for (const [collectionName, collection] of other.collections) {\n collections.set(\n collectionName,\n this.getCollection(collectionName).merge(collection),\n );\n }\n return new Storage(collections);\n }\n\n getCollection(collectionName: string): Collection {\n const collection = this.collections.get(collectionName) || new Collection();\n this.collections.set(collectionName, collection);\n return collection;\n }\n\n toPretty() {\n return [...this.collections.entries()]\n .map(([collectionName, collection]) => {\n return [`Collection: \"${collectionName}\"`, collection.toPretty()].join(\n \"\\n\",\n );\n })\n .join(\"\\n\");\n }\n}\n"]}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { ISerializer, Store, type IIdentifiable } from "@ddd-ts/core";
|
|
2
|
-
import { InMemoryDatabase } from "./in-memory.database";
|
|
3
|
-
import { InMemoryTransaction } from "../in-memory.transaction";
|
|
4
|
-
/**
|
|
5
|
-
* This in memory store is a copy store. It stores a copy of the actual model.
|
|
6
|
-
* It is the recommended inmemory store to use, as it reflects more closely the behaviour of a real store.
|
|
7
|
-
*/
|
|
8
|
-
export declare class InMemoryStore<M extends IIdentifiable> implements Store<M> {
|
|
9
|
-
readonly collection: string;
|
|
10
|
-
readonly database: InMemoryDatabase;
|
|
11
|
-
readonly serializer: ISerializer<M>;
|
|
12
|
-
readonly $name?: string | undefined;
|
|
13
|
-
constructor(collection: string, database: InMemoryDatabase, serializer: ISerializer<M>, $name?: string | undefined);
|
|
14
|
-
filter(predicate: (model: M) => boolean, trx?: InMemoryTransaction): Promise<M[]>;
|
|
15
|
-
clear(): void;
|
|
16
|
-
create(model: M, trx?: InMemoryTransaction): Promise<void>;
|
|
17
|
-
save(model: M, trx?: InMemoryTransaction): Promise<void>;
|
|
18
|
-
saveAll(models: M[], trx?: InMemoryTransaction): Promise<void>;
|
|
19
|
-
load(id: M["id"], trx?: InMemoryTransaction): Promise<M | undefined>;
|
|
20
|
-
loadAll(trx?: InMemoryTransaction): Promise<M[]>;
|
|
21
|
-
loadMany(ids: M["id"][], trx?: InMemoryTransaction): Promise<M[]>;
|
|
22
|
-
delete(id: M["id"], trx?: InMemoryTransaction): Promise<void>;
|
|
23
|
-
countAll(): Promise<number>;
|
|
24
|
-
streamAll(): AsyncIterable<M>;
|
|
25
|
-
}
|
|
26
|
-
//# sourceMappingURL=in-memory.store.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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,KAAK,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"}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.InMemoryStore = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* This in memory store is a copy store. It stores a copy of the actual model.
|
|
6
|
-
* It is the recommended inmemory store to use, as it reflects more closely the behaviour of a real store.
|
|
7
|
-
*/
|
|
8
|
-
class InMemoryStore {
|
|
9
|
-
collection;
|
|
10
|
-
database;
|
|
11
|
-
serializer;
|
|
12
|
-
$name;
|
|
13
|
-
constructor(collection, database, serializer, $name) {
|
|
14
|
-
this.collection = collection;
|
|
15
|
-
this.database = database;
|
|
16
|
-
this.serializer = serializer;
|
|
17
|
-
this.$name = $name;
|
|
18
|
-
}
|
|
19
|
-
async filter(predicate, trx) {
|
|
20
|
-
const filtered = await Promise.all(this.database.loadAll(this.collection).map(async (e) => {
|
|
21
|
-
const deserialized = await this.serializer.deserialize({
|
|
22
|
-
...(this.$name ? { $name: this.$name } : {}),
|
|
23
|
-
...e.data.data,
|
|
24
|
-
});
|
|
25
|
-
if (!predicate(deserialized)) {
|
|
26
|
-
return undefined;
|
|
27
|
-
}
|
|
28
|
-
trx?.transaction.markRead(this.collection, e.id, e.data.savedAt);
|
|
29
|
-
return deserialized;
|
|
30
|
-
}));
|
|
31
|
-
return filtered.filter((e) => Boolean(e));
|
|
32
|
-
}
|
|
33
|
-
clear() {
|
|
34
|
-
this.database.clear(this.collection);
|
|
35
|
-
}
|
|
36
|
-
async create(model, trx) {
|
|
37
|
-
const serialized = await this.serializer.serialize(model);
|
|
38
|
-
await this.database.create(this.collection, model.id.serialize(), { ...(this.$name ? { $name: this.$name } : {}), ...serialized }, trx?.transaction);
|
|
39
|
-
}
|
|
40
|
-
async save(model, trx) {
|
|
41
|
-
const serialized = await this.serializer.serialize(model);
|
|
42
|
-
await this.database.save(this.collection, model.id.serialize(), { ...(this.$name ? { $name: this.$name } : {}), ...serialized }, trx?.transaction);
|
|
43
|
-
}
|
|
44
|
-
async saveAll(models, trx) {
|
|
45
|
-
await Promise.all(models.map((m) => this.save(m, trx)));
|
|
46
|
-
}
|
|
47
|
-
async load(id, trx) {
|
|
48
|
-
const serialized = await this.database.load(this.collection, id.serialize(), trx?.transaction);
|
|
49
|
-
if (!serialized) {
|
|
50
|
-
return undefined;
|
|
51
|
-
}
|
|
52
|
-
return this.serializer.deserialize({
|
|
53
|
-
...(this.$name ? { $name: this.$name } : {}),
|
|
54
|
-
...serialized,
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
loadAll(trx) {
|
|
58
|
-
const serialized = this.database.loadAll(this.collection);
|
|
59
|
-
return Promise.all(serialized.map(async (s) => {
|
|
60
|
-
trx?.transaction.markRead(this.collection, s.id, s.data.savedAt);
|
|
61
|
-
return this.serializer.deserialize({
|
|
62
|
-
...(this.$name ? { $name: this.$name } : {}),
|
|
63
|
-
...s.data.data,
|
|
64
|
-
});
|
|
65
|
-
}));
|
|
66
|
-
}
|
|
67
|
-
async loadMany(ids, trx) {
|
|
68
|
-
const result = await Promise.all(ids.map((id) => this.load(id, trx)));
|
|
69
|
-
return result.filter((m) => m !== undefined);
|
|
70
|
-
}
|
|
71
|
-
async delete(id, trx) {
|
|
72
|
-
this.database.delete(this.collection, id.serialize(), trx?.transaction);
|
|
73
|
-
}
|
|
74
|
-
async countAll() {
|
|
75
|
-
return this.database.countAll(this.collection);
|
|
76
|
-
}
|
|
77
|
-
async *streamAll() {
|
|
78
|
-
for (const item of await this.filter(() => true)) {
|
|
79
|
-
yield item;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
exports.InMemoryStore = InMemoryStore;
|
|
84
|
-
//# sourceMappingURL=in-memory.store.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.store.js","sourceRoot":"","sources":["../../src/store/in-memory.store.ts"],"names":[],"mappings":";;;AAGA;;;GAGG;AACH,MAAa,aAAa;IAEN;IACA;IACA;IACA;IAJlB,YACkB,UAAkB,EAClB,QAA0B,EAC1B,UAA0B,EAC1B,KAAc;QAHd,eAAU,GAAV,UAAU,CAAQ;QAClB,aAAQ,GAAR,QAAQ,CAAkB;QAC1B,eAAU,GAAV,UAAU,CAAgB;QAC1B,UAAK,GAAL,KAAK,CAAS;IAC7B,CAAC;IAEJ,KAAK,CAAC,MAAM,CACV,SAAgC,EAChC,GAAyB;QAEzB,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,GAAG,CAChC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,EAAE,EAAE;YACrD,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC;gBACrD,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBAC5C,GAAG,C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,OAAO,CAAC,CAAC;YACjE,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC;gBACjC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBAC5C,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI;aACf,CAAC,CAAC;QACL,CAAC,CAAC,CACH,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,GAAc,EAAE,GAAyB;QACtD,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;QACtE,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,SAAS,CAAQ,CAAC;IACtD,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,EAAW,EAAE,GAAyB;QACjD,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,CAAC,SAAS,EAAE,EAAE,GAAG,EAAE,WAAW,CAAC,CAAC;IAC1E,CAAC;IAED,KAAK,CAAC,QAAQ;QACZ,OAAO,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACjD,CAAC;IAED,KAAK,CAAC,CAAC,SAAS;QACd,KAAK,MAAM,IAAI,IAAI,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;YACjD,MAAM,IAAI,CAAC;QACb,CAAC;IACH,CAAC;CACF;AAzGD,sCAyGC","sourcesContent":["import { ISerializer, Store, type IIdentifiable } from \"@ddd-ts/core\";\nimport { InMemoryDatabase } from \"./in-memory.database\";\nimport { InMemoryTransaction } from \"../in-memory.transaction\";\n/**\n * This in memory store is a copy store. It stores a copy of the actual model.\n * It is the recommended inmemory store to use, as it reflects more closely the behaviour of a real store.\n */\nexport class InMemoryStore<M extends IIdentifiable> implements Store<M> {\n constructor(\n public readonly collection: string,\n public readonly database: InMemoryDatabase,\n public readonly serializer: ISerializer<M>,\n public readonly $name?: string,\n ) {}\n\n async filter(\n predicate: (model: M) => boolean,\n trx?: InMemoryTransaction,\n ): Promise<M[]> {\n const filtered = await Promise.all(\n this.database.loadAll(this.collection).map(async (e) => {\n const deserialized = await this.serializer.deserialize({\n ...(this.$name ? { $name: this.$name } : {}),\n ...e.data.data,\n });\n if (!predicate(deserialized)) {\n return undefined;\n }\n trx?.transaction.markRead(this.collection, e.id, e.data.savedAt);\n return deserialized;\n }),\n );\n return filtered.filter((e): e is NonNullable<typeof e> => Boolean(e));\n }\n\n clear() {\n this.database.clear(this.collection);\n }\n\n async create(model: M, trx?: InMemoryTransaction): Promise<void> {\n const serialized = await this.serializer.serialize(model);\n await this.database.create(\n this.collection,\n model.id.serialize(),\n { ...(this.$name ? { $name: this.$name } : {}), ...serialized },\n trx?.transaction,\n );\n }\n\n async save(model: M, trx?: InMemoryTransaction): Promise<void> {\n const serialized = await this.serializer.serialize(model);\n await this.database.save(\n this.collection,\n model.id.serialize(),\n { ...(this.$name ? { $name: this.$name } : {}), ...serialized },\n trx?.transaction,\n );\n }\n\n async saveAll(models: M[], trx?: InMemoryTransaction): Promise<void> {\n await Promise.all(models.map((m) => this.save(m, trx)));\n }\n\n async load(id: M[\"id\"], trx?: InMemoryTransaction): Promise<M | undefined> {\n const serialized = await this.database.load(\n this.collection,\n id.serialize(),\n trx?.transaction,\n );\n\n if (!serialized) {\n return undefined;\n }\n\n return this.serializer.deserialize({\n ...(this.$name ? { $name: this.$name } : {}),\n ...serialized,\n });\n }\n\n loadAll(trx?: InMemoryTransaction): Promise<M[]> {\n const serialized = this.database.loadAll(this.collection);\n\n return Promise.all(\n serialized.map(async (s) => {\n trx?.transaction.markRead(this.collection, s.id, s.data.savedAt);\n return this.serializer.deserialize({\n ...(this.$name ? { $name: this.$name } : {}),\n ...s.data.data,\n });\n }),\n );\n }\n\n async loadMany(ids: M[\"id\"][], trx?: InMemoryTransaction): Promise<M[]> {\n const result = await Promise.all(ids.map((id) => this.load(id, trx)));\n return result.filter((m) => m !== undefined) as M[];\n }\n\n async delete(id: M[\"id\"], trx?: InMemoryTransaction): Promise<void> {\n this.database.delete(this.collection, id.serialize(), trx?.transaction);\n }\n\n async countAll() {\n return this.database.countAll(this.collection);\n }\n\n async *streamAll(): AsyncIterable<M> {\n for (const item of await this.filter(() => true)) {\n yield item;\n }\n }\n}\n"]}
|