@ddd-ts/store-inmemory 0.0.0-feat.kraaft.018a0bc

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.
@@ -0,0 +1,9 @@
1
+ import { TransactionPerformer } from "@ddd-ts/model";
2
+ import { InMemoryDatabase, InMemoryTransaction } from "./store/in-memory.database";
3
+ export declare class InMemoryTransactionPerformer extends TransactionPerformer<InMemoryTransaction> {
4
+ constructor(db: InMemoryDatabase);
5
+ }
6
+ export declare class FakeInMemoryTransactionPerformer extends TransactionPerformer<null> {
7
+ constructor();
8
+ }
9
+ //# sourceMappingURL=in-memory.transaction.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-memory.transaction.d.ts","sourceRoot":"","sources":["../src/in-memory.transaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACpB,MAAM,4BAA4B,CAAC;AAEpC,qBAAa,4BAA6B,SAAQ,oBAAoB,CAAC,mBAAmB,CAAC;gBAC7E,EAAE,EAAE,gBAAgB;CAGjC;AAED,qBAAa,gCAAiC,SAAQ,oBAAoB,CAAC,IAAI,CAAC;;CAI/E"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FakeInMemoryTransactionPerformer = exports.InMemoryTransactionPerformer = void 0;
4
+ const model_1 = require("@ddd-ts/model");
5
+ class InMemoryTransactionPerformer extends model_1.TransactionPerformer {
6
+ constructor(db) {
7
+ super((effect) => db.transactionally(effect));
8
+ }
9
+ }
10
+ exports.InMemoryTransactionPerformer = InMemoryTransactionPerformer;
11
+ class FakeInMemoryTransactionPerformer extends model_1.TransactionPerformer {
12
+ constructor() {
13
+ super((effect) => effect(null));
14
+ }
15
+ }
16
+ exports.FakeInMemoryTransactionPerformer = FakeInMemoryTransactionPerformer;
17
+ //# sourceMappingURL=in-memory.transaction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-memory.transaction.js","sourceRoot":"","sources":["../src/in-memory.transaction.ts"],"names":[],"mappings":";;;AAAA,yCAAqD;AAMrD,MAAa,4BAA6B,SAAQ,4BAAyC;IACzF,YAAY,EAAoB;QAC9B,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC;IAChD,CAAC;CACF;AAJD,oEAIC;AAED,MAAa,gCAAiC,SAAQ,4BAA0B;IAC9E;QACE,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;IAClC,CAAC;CACF;AAJD,4EAIC"}
@@ -0,0 +1,4 @@
1
+ export { InMemoryTransactionPerformer, FakeInMemoryTransactionPerformer, } from "./in-memory.transaction";
2
+ export { InMemoryDatabase, InMemoryTransaction, } from "./store/in-memory.database";
3
+ export { InMemoryStore } from "./store/in-memory.store";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,4BAA4B,EAC5B,gCAAgC,GACjC,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,gBAAgB,EAChB,mBAAmB,GACpB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InMemoryStore = exports.InMemoryDatabase = 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
+ var in_memory_database_1 = require("./store/in-memory.database");
8
+ Object.defineProperty(exports, "InMemoryDatabase", { enumerable: true, get: function () { return in_memory_database_1.InMemoryDatabase; } });
9
+ var in_memory_store_1 = require("./store/in-memory.store");
10
+ Object.defineProperty(exports, "InMemoryStore", { enumerable: true, get: function () { return in_memory_store_1.InMemoryStore; } });
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iEAGiC;AAF/B,qIAAA,4BAA4B,OAAA;AAC5B,yIAAA,gCAAgC,OAAA;AAElC,iEAGoC;AAFlC,sHAAA,gBAAgB,OAAA;AAGlB,2DAAwD;AAA/C,gHAAA,aAAa,OAAA"}
@@ -0,0 +1,17 @@
1
+ export declare class Collection {
2
+ private data;
3
+ constructor(data?: Map<string, {
4
+ savedAt: number;
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
+ get(id: string): any;
13
+ getAll(): any[];
14
+ save(id: string, data: any): void;
15
+ toPretty(): string;
16
+ }
17
+ //# sourceMappingURL=in-memory.collection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-memory.collection.d.ts","sourceRoot":"","sources":["../../src/store/in-memory.collection.ts"],"names":[],"mappings":"AAAA,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,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,GAAG;IAIpB,MAAM,IAAI,GAAG,EAAE;IAIf,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,GAAG,IAAI;IAMjC,QAAQ;CAKT"}
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Collection = void 0;
4
+ class Collection {
5
+ data;
6
+ constructor(data = new Map()) {
7
+ this.data = data;
8
+ }
9
+ clear() {
10
+ this.data.clear();
11
+ }
12
+ getLatestSnapshot(id) {
13
+ const data = [...this.data.values()];
14
+ const sameId = data.filter((d) => d.data.id === id);
15
+ const sorted = sameId.sort((a, b) => b.savedAt - a.savedAt);
16
+ return sorted[0]?.data;
17
+ }
18
+ clone() {
19
+ const clone = new Map();
20
+ for (const [key, value] of this.data) {
21
+ clone.set(key, value);
22
+ }
23
+ return new Collection(clone);
24
+ }
25
+ merge(other) {
26
+ const merge = new Map();
27
+ for (const [key, value] of this.data) {
28
+ merge.set(key, value);
29
+ }
30
+ for (const [key, value] of other.data) {
31
+ merge.set(key, value);
32
+ }
33
+ return new Collection(merge);
34
+ }
35
+ delete(id) {
36
+ this.data.delete(id);
37
+ }
38
+ get(id) {
39
+ return this.data.get(id)?.data;
40
+ }
41
+ getAll() {
42
+ return [...this.data.entries()].map(([id, data]) => data.data);
43
+ }
44
+ save(id, data) {
45
+ const now = process.hrtime();
46
+ const total = now[0] * 1e9 + now[1];
47
+ this.data.set(id, { savedAt: total, data });
48
+ }
49
+ toPretty() {
50
+ return [...this.data.entries()]
51
+ .map(([id, data]) => `\t\t"${id}": ${JSON.stringify(data.data)}`)
52
+ .join(",\n");
53
+ }
54
+ }
55
+ exports.Collection = Collection;
56
+ //# sourceMappingURL=in-memory.collection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-memory.collection.js","sourceRoot":"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ACrC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;SACvB;QACD,OAAO,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;IAED,MAAM,CAAC,EAAU;QACf,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACvB,CAAC;IAED,GAAG,CAAC,EAAU;QACZ,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC;IACjC,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,MAAM,GAAG,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;QAC7B,MAAM,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;QACpC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAC9C,CAAC;IAED,QAAQ;QACN,OAAO,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;aAC5B,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;aAChE,IAAI,CAAC,KAAK,CAAC,CAAC;IACjB,CAAC;CACF;AA1DD,gCA0DC"}
@@ -0,0 +1,18 @@
1
+ import { Storage } from "./in-memory.storage";
2
+ export type InMemoryTransaction = string;
3
+ export declare class InMemoryDatabase {
4
+ private storage;
5
+ private transactions;
6
+ getStorage(trx?: InMemoryTransaction): Storage;
7
+ clear(collectionName: string): void;
8
+ load(collectionName: string, id: string, trx?: InMemoryTransaction): any;
9
+ delete(collectionName: string, id: string): void;
10
+ loadAll(collectionName: string, trx?: InMemoryTransaction): any[];
11
+ loadLatestSnapshot(id: string): any;
12
+ save(collectionName: string, id: string, data: any, trx?: InMemoryTransaction): void;
13
+ private initiateTransaction;
14
+ transactionally(fn: (trx: InMemoryTransaction) => any): Promise<any>;
15
+ private commit;
16
+ print(): void;
17
+ }
18
+ //# sourceMappingURL=in-memory.database.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-memory.database.d.ts","sourceRoot":"","sources":["../../src/store/in-memory.database.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAE9C,MAAM,MAAM,mBAAmB,GAAG,MAAM,CAAC;AAEzC,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,OAAO,CAAiB;IAChC,OAAO,CAAC,YAAY,CAA2C;IAE/D,UAAU,CAAC,GAAG,CAAC,EAAE,mBAAmB;IAWpC,KAAK,CAAC,cAAc,EAAE,MAAM;IAI5B,IAAI,CAAC,cAAc,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,GAAG;IAIxE,MAAM,CAAC,cAAc,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,IAAI;IAKhD,OAAO,CAAC,cAAc,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,GAAG,EAAE;IAIjE,kBAAkB,CAAC,EAAE,EAAE,MAAM;IAI7B,IAAI,CACF,cAAc,EAAE,MAAM,EACtB,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,GAAG,EACT,GAAG,CAAC,EAAE,mBAAmB,GACxB,IAAI;IAeP,OAAO,CAAC,mBAAmB;IAOrB,eAAe,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,mBAAmB,KAAK,GAAG;IAsB3D,OAAO,CAAC,MAAM;IAUd,KAAK;CAaN"}
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ // type Collection = Map<string, any>;
3
+ // type Storage = Map<string, Collection>;
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.InMemoryDatabase = void 0;
6
+ const in_memory_storage_1 = require("./in-memory.storage");
7
+ class InMemoryDatabase {
8
+ storage = new in_memory_storage_1.Storage();
9
+ transactions = new Map();
10
+ getStorage(trx) {
11
+ if (trx) {
12
+ const storage = this.transactions.get(trx);
13
+ if (!storage) {
14
+ throw new Error(`Transaction "${trx}" not found`);
15
+ }
16
+ return storage;
17
+ }
18
+ return this.storage;
19
+ }
20
+ clear(collectionName) {
21
+ this.storage.getCollection(collectionName).clear();
22
+ }
23
+ load(collectionName, id, trx) {
24
+ return this.getStorage(trx).getCollection(collectionName).get(id);
25
+ }
26
+ delete(collectionName, id) {
27
+ // TODO: implement transactional delete
28
+ this.storage.getCollection(collectionName).delete(id);
29
+ }
30
+ loadAll(collectionName, trx) {
31
+ return this.getStorage(trx).getCollection(collectionName).getAll();
32
+ }
33
+ loadLatestSnapshot(id) {
34
+ return this.storage.getCollection("snapshots").getLatestSnapshot(id);
35
+ }
36
+ save(collectionName, id, data, trx) {
37
+ const globalStorage = this.storage;
38
+ const targetStorage = this.getStorage(trx);
39
+ if (globalStorage.getCollection(collectionName).get(id) &&
40
+ globalStorage.getCollection(collectionName).get(id) !==
41
+ targetStorage.getCollection(collectionName).get(id)) {
42
+ throw new Error(`Write collision detected for key "${id}"`);
43
+ }
44
+ targetStorage.getCollection(collectionName).save(id, data);
45
+ }
46
+ initiateTransaction(trx = Math.random().toString().substring(2)) {
47
+ const snapshot = this.storage.clone();
48
+ this.transactions.set(trx, snapshot);
49
+ return trx;
50
+ }
51
+ async transactionally(fn) {
52
+ const trx = this.initiateTransaction();
53
+ let retry = 5;
54
+ let latestReturnValue = undefined;
55
+ while (retry--) {
56
+ try {
57
+ latestReturnValue = await fn(trx);
58
+ this.commit(trx);
59
+ break;
60
+ }
61
+ catch (error) {
62
+ console.error(error);
63
+ this.initiateTransaction(trx);
64
+ }
65
+ }
66
+ if (retry === -1) {
67
+ throw new Error("failed to execute transaction after 5 retries");
68
+ }
69
+ return latestReturnValue;
70
+ }
71
+ commit(trx) {
72
+ const snapshot = this.transactions.get(trx);
73
+ if (!snapshot) {
74
+ throw new Error(`Transaction "${trx}" not found`);
75
+ }
76
+ this.storage = this.storage.clone().merge(snapshot);
77
+ this.transactions.delete(trx);
78
+ }
79
+ print() {
80
+ console.log([
81
+ "Database:",
82
+ this.storage.toPretty(),
83
+ "",
84
+ "Transactions:",
85
+ ...[...this.transactions.entries()].map(([trx, storage]) => `\t${trx}: ${storage.toPretty()}`),
86
+ ].join("\n"));
87
+ }
88
+ }
89
+ exports.InMemoryDatabase = InMemoryDatabase;
90
+ //# sourceMappingURL=in-memory.database.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-memory.database.js","sourceRoot":"","sources":["../../src/store/in-memory.database.ts"],"names":[],"mappings":";AAAA,sCAAsC;AACtC,0CAA0C;;;AAE1C,2DAA8C;AAI9C,MAAa,gBAAgB;IACnB,OAAO,GAAG,IAAI,2BAAO,EAAE,CAAC;IACxB,YAAY,GAAG,IAAI,GAAG,EAAgC,CAAC;IAE/D,UAAU,CAAC,GAAyB;QAClC,IAAI,GAAG,EAAE;YACP,MAAM,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAC3C,IAAI,CAAC,OAAO,EAAE;gBACZ,MAAM,IAAI,KAAK,CAAC,gBAAgB,GAAG,aAAa,CAAC,CAAC;aACnD;YACD,OAAO,OAAO,CAAC;SAChB;QACD,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,KAAK,CAAC,cAAsB;QAC1B,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,cAAc,CAAC,C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G,CAAC,CAAC;IAChC,CAAC;IAED,KAAK;QACH,OAAO,CAAC,GAAG,CACT;YACE,WAAW;YACX,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE;YACvB,EAAE;YACF,eAAe;YACf,GAAG,CAAC,GAAG,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,CACrC,CAAC,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,KAAK,GAAG,KAAK,OAAO,CAAC,QAAQ,EAAE,EAAE,CACtD;SACF,CAAC,IAAI,CAAC,IAAI,CAAC,CACb,CAAC;IACJ,CAAC;CACF;AA5GD,4CA4GC"}
@@ -0,0 +1,10 @@
1
+ import { Collection } from "./in-memory.collection";
2
+ export declare class Storage {
3
+ private collections;
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
@@ -0,0 +1 @@
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;IACN,OAAO,CAAC,WAAW;gBAAX,WAAW,GAAE,GAAG,CAAC,MAAM,EAAE,UAAU,CAAa;IAEpE,KAAK;IASL,KAAK,CAAC,KAAK,EAAE,OAAO;IAWpB,aAAa,CAAC,cAAc,EAAE,MAAM,GAAG,UAAU;IAMjD,QAAQ;CAUT"}
@@ -0,0 +1,41 @@
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 [
33
+ 'Collection: "' + collectionName + '"',
34
+ collection.toPretty(),
35
+ ].join("\n");
36
+ })
37
+ .join("\n");
38
+ }
39
+ }
40
+ exports.Storage = Storage;
41
+ //# sourceMappingURL=in-memory.storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-memory.storage.js","sourceRoot":"","sources":["../../src/store/in-memory.storage.ts"],"names":[],"mappings":";;;AAAA,iEAAoD;AAEpD,MAAa,OAAO;IACE;IAApB,YAAoB,cAAuC,IAAI,GAAG,EAAE;QAAhD,gBAAW,GAAX,WAAW,CAAqC;IAAG,CAAC;IAExE,KAAK;QACH,MAAM,KAAK,GAAG,IAAI,GAAG,EAAsB,CAAC;QAC5C,KAAK,MAAM,CAAC,cAAc,EAAE,UAAU,CAAC,IAAI,IAAI,CAAC,WAAW,EAAE;YAC3D,KAAK,CAAC,GAAG,CAAC,cAAc,EAAE,UAAU,CAAC,KAAK,EAAE,CAAC,CAAC;SAC/C;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;YAC5D,WAAW,CAAC,GAAG,CACb,cAAc,EACd,IAAI,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,CACrD,CAAC;SACH;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;gBACL,eAAe,GAAG,cAAc,GAAG,GAAG;gBACtC,UAAU,CAAC,QAAQ,EAAE;aACtB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACf,CAAC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,CAAC;IAChB,CAAC;CACF;AAvCD,0BAuCC"}
@@ -0,0 +1,21 @@
1
+ import { Serializer, Store } from "@ddd-ts/model";
2
+ import { InMemoryDatabase, InMemoryTransaction } from "./in-memory.database";
3
+ /**
4
+ * This in memory store is a copy store. It stores a copy of the actual model.
5
+ * It is the recommended inmemory store to use, as it reflects more closely the behaviour of a real store.
6
+ */
7
+ export declare class InMemoryStore<Model, Id extends {
8
+ toString(): string;
9
+ }> implements Store<Model, Id> {
10
+ readonly collection: string;
11
+ readonly database: InMemoryDatabase;
12
+ readonly serializer: Serializer<Model>;
13
+ constructor(collection: string, database: InMemoryDatabase, serializer: Serializer<Model>);
14
+ protected filter(predicate: (model: Model) => boolean): Promise<Model[]>;
15
+ clear(): void;
16
+ save(model: Model, trx?: InMemoryTransaction): Promise<void>;
17
+ load(id: Id, trx?: InMemoryTransaction): Promise<Model | undefined>;
18
+ loadAll(): Promise<Model[]>;
19
+ delete(id: Id): Promise<void>;
20
+ }
21
+ //# sourceMappingURL=in-memory.store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-memory.store.d.ts","sourceRoot":"","sources":["../../src/store/in-memory.store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAC7E;;;GAGG;AACH,qBAAa,aAAa,CAAC,KAAK,EAAE,EAAE,SAAS;IAAE,QAAQ,IAAI,MAAM,CAAA;CAAE,CACjE,YAAW,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC;aAGT,UAAU,EAAE,MAAM;aAClB,QAAQ,EAAE,gBAAgB;aAC1B,UAAU,EAAE,UAAU,CAAC,KAAK,CAAC;gBAF7B,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,gBAAgB,EAC1B,UAAU,EAAE,UAAU,CAAC,KAAK,CAAC;cAG/B,MAAM,CACpB,SAAS,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,GACnC,OAAO,CAAC,KAAK,EAAE,CAAC;IAUnB,KAAK;IAIC,IAAI,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAS5D,IAAI,CAAC,EAAE,EAAE,EAAE,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC;IAczE,OAAO,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;IAMrB,MAAM,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAGpC"}
@@ -0,0 +1,44 @@
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
+ constructor(collection, database, serializer) {
13
+ this.collection = collection;
14
+ this.database = database;
15
+ this.serializer = serializer;
16
+ }
17
+ async filter(predicate) {
18
+ const serialized = await this.database.loadAll(this.collection);
19
+ const all = await Promise.all(serialized.map((s) => this.serializer.deserialize(s)));
20
+ return all.filter(predicate);
21
+ }
22
+ clear() {
23
+ this.database.clear(this.collection);
24
+ }
25
+ async save(model, trx) {
26
+ await this.database.save(this.collection, this.serializer.getIdFromModel(model).toString(), await this.serializer.serialize(model), trx);
27
+ }
28
+ async load(id, trx) {
29
+ const serialized = await this.database.load(this.collection, id.toString(), trx);
30
+ if (!serialized) {
31
+ return undefined;
32
+ }
33
+ return this.serializer.deserialize(serialized);
34
+ }
35
+ loadAll() {
36
+ const serialized = this.database.loadAll(this.collection);
37
+ return Promise.all(serialized.map((s) => this.serializer.deserialize(s)));
38
+ }
39
+ async delete(id) {
40
+ this.database.delete(this.collection, id.toString());
41
+ }
42
+ }
43
+ exports.InMemoryStore = InMemoryStore;
44
+ //# sourceMappingURL=in-memory.store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-memory.store.js","sourceRoot":"","sources":["../../src/store/in-memory.store.ts"],"names":[],"mappings":";;;AAEA;;;GAGG;AACH,MAAa,aAAa;IAIN;IACA;IACA;IAHlB,YACkB,UAAkB,EAClB,QAA0B,EAC1B,UAA6B;QAF7B,eAAU,GAAV,UAAU,CAAQ;QAClB,aAAQ,GAAR,QAAQ,CAAkB;QAC1B,eAAU,GAAV,UAAU,CAAmB;IAC5C,CAAC;IAEM,KAAK,CAAC,MAAM,CACpB,SAAoC;QAEpC,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAEhE,MAAM,GAAG,GAAG,MAAM,OAAO,CAAC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AQ,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC;IACvD,CAAC;CACF;AAzDD,sCAyDC"}
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@ddd-ts/store-inmemory",
3
+ "version": "0.0.0-feat.kraaft.018a0bc",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "license": "MIT",
7
+ "scripts": {
8
+ "build": "builder"
9
+ },
10
+ "peerDependencies": {
11
+ "@ddd-ts/model": "*"
12
+ },
13
+ "devDependencies": {
14
+ "@ddd-ts/dev": "*",
15
+ "@ddd-ts/types": "*"
16
+ }
17
+ }