@ddd-ts/store-inmemory 0.0.0-feat.kraaft.b1feeab

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,6 @@
1
+ import { TransactionPerformer } from "@ddd-ts/model";
2
+ import { InMemoryDatabase } from "./store/in-memory.database";
3
+ export declare class InMemoryTransactionPerformer extends TransactionPerformer {
4
+ constructor(db: InMemoryDatabase);
5
+ }
6
+ //# 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,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAE9D,qBAAa,4BAA6B,SAAQ,oBAAoB;gBACxD,EAAE,EAAE,gBAAgB;CAGjC"}
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ 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
+ //# 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;AAGrD,MAAa,4BAA6B,SAAQ,4BAAoB;IACpE,YAAY,EAAoB;QAC9B,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC;IAChD,CAAC;CACF;AAJD,oEAIC"}
@@ -0,0 +1,4 @@
1
+ export { InMemoryTransactionPerformer } 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,EAAE,4BAA4B,EAAE,MAAM,yBAAyB,CAAC;AACvE,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,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InMemoryStore = exports.InMemoryDatabase = 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
+ var in_memory_database_1 = require("./store/in-memory.database");
7
+ Object.defineProperty(exports, "InMemoryDatabase", { enumerable: true, get: function () { return in_memory_database_1.InMemoryDatabase; } });
8
+ var in_memory_store_1 = require("./store/in-memory.store");
9
+ Object.defineProperty(exports, "InMemoryStore", { enumerable: true, get: function () { return in_memory_store_1.InMemoryStore; } });
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iEAAuE;AAA9D,qIAAA,4BAA4B,OAAA;AACrC,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;CAOT"}
@@ -0,0 +1,58 @@
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(function collectionStringifier([id, data]) {
52
+ return `\t\t"${id}": ${JSON.stringify(data.data)}`;
53
+ })
54
+ .join(",\n");
55
+ }
56
+ }
57
+ exports.Collection = Collection;
58
+ //# sourceMappingURL=in-memory.collection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-memory.collection.js","sourceRoot":"","sources":["../../src/store/in-memory.collection.ts"],"names":[],"mappings":";;;AAAA,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,CAAC,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC;QAC5D,OAAO,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACxB,C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AG,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,SAAS,qBAAqB,CAAC,CAAC,EAAE,EAAE,IAAI,CAAC;YAC5C,OAAO,QAAQ,EAAE,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACrD,CAAC,CAAC;aACD,IAAI,CAAC,KAAK,CAAC,CAAC;IACjB,CAAC;CACF;AA5DD,gCA4DC"}
@@ -0,0 +1,17 @@
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
+ transactionally(fn: (trx: InMemoryTransaction) => any): Promise<any>;
14
+ private commit;
15
+ print(): void;
16
+ }
17
+ //# 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;IAcD,eAAe,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,mBAAmB,KAAK,GAAG;IA0B3D,OAAO,CAAC,MAAM;IAUd,KAAK;CAaN"}
@@ -0,0 +1,86 @@
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
+ targetStorage.getCollection(collectionName).get(id)) {
41
+ throw new Error(`Write collision detected for key "${id}"`);
42
+ }
43
+ targetStorage.getCollection(collectionName).save(id, data);
44
+ }
45
+ async transactionally(fn) {
46
+ let trx = Math.random().toString().substring(2);
47
+ const snapshot = this.storage.clone();
48
+ this.transactions.set(trx, snapshot);
49
+ let retry = 5;
50
+ let latestReturnValue = undefined;
51
+ while (retry--) {
52
+ try {
53
+ latestReturnValue = await fn(trx);
54
+ this.commit(trx);
55
+ break;
56
+ }
57
+ catch (error) {
58
+ console.error(error);
59
+ this.transactions.set(trx, this.storage.clone());
60
+ }
61
+ }
62
+ if (retry === -1) {
63
+ throw new Error("failed to execute transaction after 5 retries");
64
+ }
65
+ return latestReturnValue;
66
+ }
67
+ commit(trx) {
68
+ const snapshot = this.transactions.get(trx);
69
+ if (!snapshot) {
70
+ throw new Error(`Transaction "${trx}" not found`);
71
+ }
72
+ this.storage = this.storage.clone().merge(snapshot);
73
+ this.transactions.delete(trx);
74
+ }
75
+ print() {
76
+ console.log([
77
+ "Database:",
78
+ this.storage.toPretty(),
79
+ "",
80
+ "Transactions:",
81
+ ...[...this.transactions.entries()].map(([trx, storage]) => `\t${trx}: ${storage.toPretty()}`),
82
+ ].join("\n"));
83
+ }
84
+ }
85
+ exports.InMemoryDatabase = InMemoryDatabase;
86
+ //# 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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EO,MAAM,CAAC,GAAwB;QACrC,MAAM,QAAQ,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC5C,IAAI,CAAC,QAAQ,EAAE;YACb,MAAM,IAAI,KAAK,CAAC,gBAAgB,GAAG,aAAa,CAAC,CAAC;SACnD;QAED,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QACpD,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,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;AAxGD,4CAwGC"}
@@ -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,14 @@
1
+ import { Serializer, Store } from "@ddd-ts/model";
2
+ import { InMemoryDatabase } from "./in-memory.database";
3
+ import { AbstractConstructor } from "@ddd-ts/types";
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 function InMemoryStore<Model, Id extends {
9
+ toString(): string;
10
+ }>(collection: string): AbstractConstructor<Store<Model, Id>, [
11
+ InMemoryDatabase,
12
+ Serializer<Model>
13
+ ]>;
14
+ //# 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,EAAuB,MAAM,sBAAsB,CAAC;AAC7E,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD;;;GAGG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,EAAE,SAAS;IAAE,QAAQ,IAAI,MAAM,CAAA;CAAE,EACpE,UAAU,EAAE,MAAM,GACjB,mBAAmB,CACpB,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC,EAChB;IAAC,gBAAgB;IAAE,UAAU,CAAC,KAAK,CAAC;CAAC,CACtC,CAiDA"}
@@ -0,0 +1,41 @@
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
+ function InMemoryStore(collection) {
9
+ class InMemoryStore {
10
+ database;
11
+ serializer;
12
+ constructor(database, serializer) {
13
+ this.database = database;
14
+ this.serializer = serializer;
15
+ }
16
+ clear() {
17
+ this.database.clear(collection);
18
+ }
19
+ async save(model, trx) {
20
+ const serialized = this.serializer.serialize(model);
21
+ await this.database.save(collection, serialized.id, this.serializer.serialize(model), trx);
22
+ }
23
+ async load(id, trx) {
24
+ const serialized = await this.database.load(collection, id.toString(), trx);
25
+ if (!serialized) {
26
+ return undefined;
27
+ }
28
+ return this.serializer.deserialize(serialized);
29
+ }
30
+ loadAll() {
31
+ const serialized = this.database.loadAll(collection);
32
+ return Promise.resolve(serialized.map((s) => this.serializer.deserialize(s)));
33
+ }
34
+ async delete(id) {
35
+ this.database.delete(collection, id.toString());
36
+ }
37
+ }
38
+ return InMemoryStore;
39
+ }
40
+ exports.InMemoryStore = InMemoryStore;
41
+ //# 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":";;;AAGA;;;GAGG;AACH,SAAgB,aAAa,CAC3B,UAAkB;IAKlB,MAAe,aAAa;QAER;QACC;QAFnB,YACkB,QAA0B,EACzB,UAA6B;YAD9B,aAAQ,GAAR,QAAQ,CAAkB;YACzB,eAAU,GAAV,UAAU,CAAmB;QAC7C,CAAC;QAEJ,KAAK;YACH,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;QAClC,CAAC;QAED,KAAK,CAAC,IAAI,CAAC,KAAY,EAAE,GAAyB;YAChD,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YACpD,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,CACtB,UAAU,EACV,UAAU,CAAC,EAAE,EACb,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,KAAK,CAAC,EAChC,GAAG,CACJ,CAAC;QACJ,CAAC;QAED,KAAK,CAAC,IAAI,CAAC,EAAM,EAAE,GAAyB;YAC1C,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,CACzC,UAAU,EACV,EAAE,CAAC,QAAQ,EAAE,EACb,GAAG,CACJ,CAAC;YAEF,IAAI,CAAC,UAAU,EAAE;gBACf,OAAO,SAAS,CAAC;aAClB;YAED,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;QACjD,CAAC;QAED,OAAO;YACL,MAAM,UAAU,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;YAErD,OAAO,OAAO,CAAC,OAAO,CACpB,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CACtD,CAAC;QACJ,CAAC;QAED,KAAK,CAAC,MAAM,CAAC,EAAM;YACjB,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,UAAU,EAAE,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC;QAClD,CAAC;KACF;IAED,OAAO,aAAa,CAAC;AACvB,CAAC;AAtDD,sCAsDC"}
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@ddd-ts/store-inmemory",
3
+ "version": "0.0.0-feat.kraaft.b1feeab",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "license": "MIT",
7
+ "scripts": {
8
+ "test": "test-runner",
9
+ "build": "builder"
10
+ },
11
+ "dependencies": {
12
+ "@ddd-ts/model": "*"
13
+ },
14
+ "devDependencies": {
15
+ "@ddd-ts/dev": "*",
16
+ "@ddd-ts/types": "*"
17
+ }
18
+ }