@ddd-ts/store-inmemory 0.0.37 → 0.0.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/in-memory.transaction.d.ts +18 -0
- package/dist/in-memory.transaction.d.ts.map +1 -0
- package/dist/in-memory.transaction.js +36 -0
- package/dist/in-memory.transaction.mjs +34 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.mjs +5 -0
- package/dist/store/in-memory.collection.d.ts +29 -0
- package/dist/store/in-memory.collection.d.ts.map +1 -0
- package/dist/store/in-memory.collection.js +65 -0
- package/dist/store/in-memory.collection.mjs +64 -0
- package/dist/store/in-memory.database.d.ts +73 -0
- package/dist/store/in-memory.database.d.ts.map +1 -0
- package/dist/store/in-memory.database.js +155 -0
- package/dist/store/in-memory.database.mjs +152 -0
- package/dist/store/in-memory.storage.d.ts +10 -0
- package/dist/store/in-memory.storage.d.ts.map +1 -0
- package/dist/store/in-memory.storage.js +31 -0
- package/dist/store/in-memory.storage.mjs +31 -0
- package/dist/store/in-memory.store.d.ts +26 -0
- package/dist/store/in-memory.store.d.ts.map +1 -0
- package/dist/store/in-memory.store.js +80 -0
- package/dist/store/in-memory.store.mjs +80 -0
- package/package.json +32 -30
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Aetherall
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
|
@@ -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,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"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require('./store/in-memory.database.js');
|
|
2
|
+
let _ddd_ts_core = require("@ddd-ts/core");
|
|
3
|
+
|
|
4
|
+
//#region src/in-memory.transaction.ts
|
|
5
|
+
var InMemoryTransaction = class {
|
|
6
|
+
commitListeners = [];
|
|
7
|
+
constructor(transaction) {
|
|
8
|
+
this.transaction = transaction;
|
|
9
|
+
}
|
|
10
|
+
counter = -1;
|
|
11
|
+
increment() {
|
|
12
|
+
this.counter++;
|
|
13
|
+
return this.counter;
|
|
14
|
+
}
|
|
15
|
+
onCommit(callback) {
|
|
16
|
+
this.commitListeners.push(callback);
|
|
17
|
+
}
|
|
18
|
+
async executeCommitListeners() {
|
|
19
|
+
await Promise.all(this.commitListeners.map((cb) => cb()));
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
var InMemoryTransactionPerformer = class extends _ddd_ts_core.TransactionPerformer {
|
|
23
|
+
constructor(db) {
|
|
24
|
+
super((effect) => db.transactionally((trx) => effect(trx)));
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var FakeInMemoryTransactionPerformer = class extends _ddd_ts_core.TransactionPerformer {
|
|
28
|
+
constructor() {
|
|
29
|
+
super((effect) => effect(new InMemoryTransaction(null)));
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
exports.FakeInMemoryTransactionPerformer = FakeInMemoryTransactionPerformer;
|
|
35
|
+
exports.InMemoryTransaction = InMemoryTransaction;
|
|
36
|
+
exports.InMemoryTransactionPerformer = InMemoryTransactionPerformer;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import "./store/in-memory.database.mjs";
|
|
2
|
+
import { TransactionPerformer } from "@ddd-ts/core";
|
|
3
|
+
|
|
4
|
+
//#region src/in-memory.transaction.ts
|
|
5
|
+
var InMemoryTransaction = class {
|
|
6
|
+
commitListeners = [];
|
|
7
|
+
constructor(transaction) {
|
|
8
|
+
this.transaction = transaction;
|
|
9
|
+
}
|
|
10
|
+
counter = -1;
|
|
11
|
+
increment() {
|
|
12
|
+
this.counter++;
|
|
13
|
+
return this.counter;
|
|
14
|
+
}
|
|
15
|
+
onCommit(callback) {
|
|
16
|
+
this.commitListeners.push(callback);
|
|
17
|
+
}
|
|
18
|
+
async executeCommitListeners() {
|
|
19
|
+
await Promise.all(this.commitListeners.map((cb) => cb()));
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
var InMemoryTransactionPerformer = class extends TransactionPerformer {
|
|
23
|
+
constructor(db) {
|
|
24
|
+
super((effect) => db.transactionally((trx) => effect(trx)));
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var FakeInMemoryTransactionPerformer = class extends TransactionPerformer {
|
|
28
|
+
constructor() {
|
|
29
|
+
super((effect) => effect(new InMemoryTransaction(null)));
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
export { FakeInMemoryTransactionPerformer, InMemoryTransaction, InMemoryTransactionPerformer };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_in_memory_database = require('./store/in-memory.database.js');
|
|
3
|
+
const require_in_memory_transaction = require('./in-memory.transaction.js');
|
|
4
|
+
const require_in_memory_store = require('./store/in-memory.store.js');
|
|
5
|
+
|
|
6
|
+
exports.CannotReadAfterWrites = require_in_memory_database.CannotReadAfterWrites;
|
|
7
|
+
exports.FakeInMemoryTransactionPerformer = require_in_memory_transaction.FakeInMemoryTransactionPerformer;
|
|
8
|
+
exports.InMemoryDatabase = require_in_memory_database.InMemoryDatabase;
|
|
9
|
+
exports.InMemoryStore = require_in_memory_store.InMemoryStore;
|
|
10
|
+
exports.InMemoryTransaction = require_in_memory_transaction.InMemoryTransaction;
|
|
11
|
+
exports.InMemoryTransactionPerformer = require_in_memory_transaction.InMemoryTransactionPerformer;
|
|
12
|
+
exports.InMemoryUnderlyingTransaction = require_in_memory_database.InMemoryUnderlyingTransaction;
|
|
13
|
+
exports.TransactionCollidedTooManyTimes = require_in_memory_database.TransactionCollidedTooManyTimes;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { CannotReadAfterWrites, InMemoryDatabase, InMemoryUnderlyingTransaction, TransactionCollidedTooManyTimes } from "./store/in-memory.database.mjs";
|
|
2
|
+
import { FakeInMemoryTransactionPerformer, InMemoryTransaction, InMemoryTransactionPerformer } from "./in-memory.transaction.mjs";
|
|
3
|
+
import { InMemoryStore } from "./store/in-memory.store.mjs";
|
|
4
|
+
|
|
5
|
+
export { CannotReadAfterWrites, FakeInMemoryTransactionPerformer, InMemoryDatabase, InMemoryStore, InMemoryTransaction, InMemoryTransactionPerformer, InMemoryUnderlyingTransaction, TransactionCollidedTooManyTimes };
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/store/in-memory.collection.ts
|
|
3
|
+
function now() {
|
|
4
|
+
if (typeof process === "object" && typeof process.hrtime === "function") return process.hrtime.bigint() / BigInt(1e3);
|
|
5
|
+
return BigInt(Date.now() * 1e3);
|
|
6
|
+
}
|
|
7
|
+
var Collection = class Collection {
|
|
8
|
+
constructor(data = /* @__PURE__ */ new Map()) {
|
|
9
|
+
this.data = data;
|
|
10
|
+
}
|
|
11
|
+
clear() {
|
|
12
|
+
this.data.clear();
|
|
13
|
+
}
|
|
14
|
+
getLatestSnapshot(id) {
|
|
15
|
+
return [...this.data.values()].filter((d) => d.data.id === id).sort((a, b) => b.savedAt > a.savedAt ? 1 : -1)[0]?.data;
|
|
16
|
+
}
|
|
17
|
+
clone() {
|
|
18
|
+
const clone = /* @__PURE__ */ new Map();
|
|
19
|
+
for (const [key, value] of this.data) clone.set(key, value);
|
|
20
|
+
return new Collection(clone);
|
|
21
|
+
}
|
|
22
|
+
merge(other) {
|
|
23
|
+
const merge = /* @__PURE__ */ new Map();
|
|
24
|
+
for (const [key, value] of this.data) merge.set(key, value);
|
|
25
|
+
for (const [key, value] of other.data) merge.set(key, value);
|
|
26
|
+
return new Collection(merge);
|
|
27
|
+
}
|
|
28
|
+
delete(id) {
|
|
29
|
+
this.data.delete(id);
|
|
30
|
+
}
|
|
31
|
+
getRaw(id) {
|
|
32
|
+
return this.data.get(id);
|
|
33
|
+
}
|
|
34
|
+
countAll() {
|
|
35
|
+
return this.data.size;
|
|
36
|
+
}
|
|
37
|
+
get(id) {
|
|
38
|
+
return this.data.get(id)?.data;
|
|
39
|
+
}
|
|
40
|
+
getAllRaw() {
|
|
41
|
+
return [...this.data.entries()].map(([id, data]) => ({
|
|
42
|
+
id,
|
|
43
|
+
data
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
getAll() {
|
|
47
|
+
return [...this.data.entries()].map(([id, data]) => data.data);
|
|
48
|
+
}
|
|
49
|
+
save(id, data) {
|
|
50
|
+
this.data.set(id, {
|
|
51
|
+
savedAt: now(),
|
|
52
|
+
data
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
toPretty() {
|
|
56
|
+
return [...this.data.entries()].map(([id, data]) => `\t\t"${id}": ${JSON.stringify(data.data, replaceBigInt)}`).join(",\n");
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
function replaceBigInt(key, value) {
|
|
60
|
+
if (typeof value === "bigint") return value.toString();
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
exports.Collection = Collection;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
//#region src/store/in-memory.collection.ts
|
|
2
|
+
function now() {
|
|
3
|
+
if (typeof process === "object" && typeof process.hrtime === "function") return process.hrtime.bigint() / BigInt(1e3);
|
|
4
|
+
return BigInt(Date.now() * 1e3);
|
|
5
|
+
}
|
|
6
|
+
var Collection = class Collection {
|
|
7
|
+
constructor(data = /* @__PURE__ */ new Map()) {
|
|
8
|
+
this.data = data;
|
|
9
|
+
}
|
|
10
|
+
clear() {
|
|
11
|
+
this.data.clear();
|
|
12
|
+
}
|
|
13
|
+
getLatestSnapshot(id) {
|
|
14
|
+
return [...this.data.values()].filter((d) => d.data.id === id).sort((a, b) => b.savedAt > a.savedAt ? 1 : -1)[0]?.data;
|
|
15
|
+
}
|
|
16
|
+
clone() {
|
|
17
|
+
const clone = /* @__PURE__ */ new Map();
|
|
18
|
+
for (const [key, value] of this.data) clone.set(key, value);
|
|
19
|
+
return new Collection(clone);
|
|
20
|
+
}
|
|
21
|
+
merge(other) {
|
|
22
|
+
const merge = /* @__PURE__ */ new Map();
|
|
23
|
+
for (const [key, value] of this.data) merge.set(key, value);
|
|
24
|
+
for (const [key, value] of other.data) merge.set(key, value);
|
|
25
|
+
return new Collection(merge);
|
|
26
|
+
}
|
|
27
|
+
delete(id) {
|
|
28
|
+
this.data.delete(id);
|
|
29
|
+
}
|
|
30
|
+
getRaw(id) {
|
|
31
|
+
return this.data.get(id);
|
|
32
|
+
}
|
|
33
|
+
countAll() {
|
|
34
|
+
return this.data.size;
|
|
35
|
+
}
|
|
36
|
+
get(id) {
|
|
37
|
+
return this.data.get(id)?.data;
|
|
38
|
+
}
|
|
39
|
+
getAllRaw() {
|
|
40
|
+
return [...this.data.entries()].map(([id, data]) => ({
|
|
41
|
+
id,
|
|
42
|
+
data
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
getAll() {
|
|
46
|
+
return [...this.data.entries()].map(([id, data]) => data.data);
|
|
47
|
+
}
|
|
48
|
+
save(id, data) {
|
|
49
|
+
this.data.set(id, {
|
|
50
|
+
savedAt: now(),
|
|
51
|
+
data
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
toPretty() {
|
|
55
|
+
return [...this.data.entries()].map(([id, data]) => `\t\t"${id}": ${JSON.stringify(data.data, replaceBigInt)}`).join(",\n");
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
function replaceBigInt(key, value) {
|
|
59
|
+
if (typeof value === "bigint") return value.toString();
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
export { Collection };
|
|
@@ -0,0 +1,73 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"in-memory.database.d.ts","sourceRoot":"","sources":["../../src/store/in-memory.database.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
const require_in_memory_storage = require('./in-memory.storage.js');
|
|
2
|
+
const require_in_memory_transaction = require('../in-memory.transaction.js');
|
|
3
|
+
require('../index.js');
|
|
4
|
+
let _ddd_ts_core = require("@ddd-ts/core");
|
|
5
|
+
|
|
6
|
+
//#region src/store/in-memory.database.ts
|
|
7
|
+
var CannotReadAfterWrites = class extends Error {
|
|
8
|
+
constructor() {
|
|
9
|
+
super("Cannot read after having written into a transaction");
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var TransactionCollision = class extends Error {
|
|
13
|
+
constructor() {
|
|
14
|
+
super("Transaction has collided with other extern writes");
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var DocumentAlreadyExists = class extends _ddd_ts_core.ConcurrencyError {
|
|
18
|
+
constructor() {
|
|
19
|
+
super("Document already exists");
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
var TransactionCollidedTooManyTimes = class extends Error {
|
|
23
|
+
constructor(tries) {
|
|
24
|
+
super(`Transaction collided too many times (${tries})`);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var InMemoryUnderlyingTransaction = class {
|
|
28
|
+
operations = [];
|
|
29
|
+
id = Math.random().toString().substring(2);
|
|
30
|
+
ensureNoWrites() {
|
|
31
|
+
if (this.operations.some((operation) => operation.type === "write" || operation.type === "create" || operation.type === "delete")) throw new CannotReadAfterWrites();
|
|
32
|
+
}
|
|
33
|
+
markRead(collectionName, id, savedAt) {
|
|
34
|
+
this.ensureNoWrites();
|
|
35
|
+
this.operations.push({
|
|
36
|
+
type: "read",
|
|
37
|
+
collectionName,
|
|
38
|
+
id,
|
|
39
|
+
savedAt
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
markWritten(collectionName, id, data) {
|
|
43
|
+
this.operations.push({
|
|
44
|
+
type: "write",
|
|
45
|
+
collectionName,
|
|
46
|
+
id,
|
|
47
|
+
data
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
markCreated(collectionName, id, data) {
|
|
51
|
+
this.operations.push({
|
|
52
|
+
type: "create",
|
|
53
|
+
collectionName,
|
|
54
|
+
id,
|
|
55
|
+
data
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
markDeleted(collectionName, id, savedAt) {
|
|
59
|
+
this.operations.push({
|
|
60
|
+
type: "delete",
|
|
61
|
+
collectionName,
|
|
62
|
+
id,
|
|
63
|
+
savedAt
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
checkConsistency(storage) {
|
|
67
|
+
for (const operation of this.operations) {
|
|
68
|
+
if (operation.type === "read") {
|
|
69
|
+
const collection = storage.getCollection(operation.collectionName);
|
|
70
|
+
if (!collection) return false;
|
|
71
|
+
if (operation.savedAt !== collection.getRaw(operation.id)?.savedAt) return false;
|
|
72
|
+
}
|
|
73
|
+
if (operation.type === "create") {
|
|
74
|
+
const collection = storage.getCollection(operation.collectionName);
|
|
75
|
+
if (collection && collection.getRaw(operation.id)?.savedAt !== void 0) throw new DocumentAlreadyExists();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
var InMemoryDatabase = class InMemoryDatabase {
|
|
82
|
+
storage = new require_in_memory_storage.Storage();
|
|
83
|
+
clear(collectionName) {
|
|
84
|
+
this.storage.getCollection(collectionName).clear();
|
|
85
|
+
}
|
|
86
|
+
load(collectionName, id, trx) {
|
|
87
|
+
const collection = this.storage.getCollection(collectionName);
|
|
88
|
+
const data = collection.get(id);
|
|
89
|
+
if (trx) {
|
|
90
|
+
const doc = collection.getRaw(id);
|
|
91
|
+
trx.markRead(collectionName, id, doc?.savedAt);
|
|
92
|
+
}
|
|
93
|
+
return data;
|
|
94
|
+
}
|
|
95
|
+
delete(collectionName, id, trx) {
|
|
96
|
+
if (trx) {
|
|
97
|
+
const doc = this.storage.getCollection(collectionName).getRaw(id);
|
|
98
|
+
trx.markDeleted(collectionName, id, doc?.savedAt);
|
|
99
|
+
} else this.storage.getCollection(collectionName).delete(id);
|
|
100
|
+
}
|
|
101
|
+
countAll(collectionName) {
|
|
102
|
+
return this.storage.getCollection(collectionName).countAll();
|
|
103
|
+
}
|
|
104
|
+
loadAll(collectionName) {
|
|
105
|
+
return this.storage.getCollection(collectionName).getAllRaw();
|
|
106
|
+
}
|
|
107
|
+
loadLatestSnapshot(id) {
|
|
108
|
+
return this.storage.getCollection("snapshots").getLatestSnapshot(id);
|
|
109
|
+
}
|
|
110
|
+
save(collectionName, id, data, trx) {
|
|
111
|
+
if (trx) trx.markWritten(collectionName, id, data);
|
|
112
|
+
else this.storage.getCollection(collectionName).save(id, data);
|
|
113
|
+
}
|
|
114
|
+
create(collectionName, id, data, trx) {
|
|
115
|
+
if (trx) trx.markCreated(collectionName, id, data);
|
|
116
|
+
else {
|
|
117
|
+
if (this.storage.getCollection(collectionName).get(id)) throw new Error(`Document with id ${id} already exists`);
|
|
118
|
+
this.storage.getCollection(collectionName).save(id, data);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
static transactionTries = 5;
|
|
122
|
+
async transactionally(fn) {
|
|
123
|
+
let trx = new require_in_memory_transaction.InMemoryTransaction(new InMemoryUnderlyingTransaction());
|
|
124
|
+
let retry = InMemoryDatabase.transactionTries;
|
|
125
|
+
let latestReturnValue = void 0;
|
|
126
|
+
while (retry--) try {
|
|
127
|
+
latestReturnValue = await fn(trx);
|
|
128
|
+
this.streamId(trx);
|
|
129
|
+
break;
|
|
130
|
+
} catch (error) {
|
|
131
|
+
if (error instanceof TransactionCollision) trx = new require_in_memory_transaction.InMemoryTransaction(new InMemoryUnderlyingTransaction());
|
|
132
|
+
else throw error;
|
|
133
|
+
}
|
|
134
|
+
if (retry === -1) throw new TransactionCollidedTooManyTimes(InMemoryDatabase.transactionTries);
|
|
135
|
+
return latestReturnValue;
|
|
136
|
+
}
|
|
137
|
+
streamId(trx) {
|
|
138
|
+
if (!trx.transaction.checkConsistency(this.storage)) throw new TransactionCollision();
|
|
139
|
+
for (const operation of trx.transaction.operations) {
|
|
140
|
+
if (operation.type === "read") continue;
|
|
141
|
+
if (operation.type === "write") this.save(operation.collectionName, operation.id, operation.data);
|
|
142
|
+
if (operation.type === "delete") this.delete(operation.collectionName, operation.id);
|
|
143
|
+
if (operation.type === "create") this.create(operation.collectionName, operation.id, operation.data);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
print() {
|
|
147
|
+
console.log(["Database:", this.storage.toPretty()].join("\n"));
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
152
|
+
exports.CannotReadAfterWrites = CannotReadAfterWrites;
|
|
153
|
+
exports.InMemoryDatabase = InMemoryDatabase;
|
|
154
|
+
exports.InMemoryUnderlyingTransaction = InMemoryUnderlyingTransaction;
|
|
155
|
+
exports.TransactionCollidedTooManyTimes = TransactionCollidedTooManyTimes;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { Storage } from "./in-memory.storage.mjs";
|
|
2
|
+
import { InMemoryTransaction } from "../in-memory.transaction.mjs";
|
|
3
|
+
import "../index.mjs";
|
|
4
|
+
import { ConcurrencyError } from "@ddd-ts/core";
|
|
5
|
+
|
|
6
|
+
//#region src/store/in-memory.database.ts
|
|
7
|
+
var CannotReadAfterWrites = class extends Error {
|
|
8
|
+
constructor() {
|
|
9
|
+
super("Cannot read after having written into a transaction");
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var TransactionCollision = class extends Error {
|
|
13
|
+
constructor() {
|
|
14
|
+
super("Transaction has collided with other extern writes");
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var DocumentAlreadyExists = class extends ConcurrencyError {
|
|
18
|
+
constructor() {
|
|
19
|
+
super("Document already exists");
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
var TransactionCollidedTooManyTimes = class extends Error {
|
|
23
|
+
constructor(tries) {
|
|
24
|
+
super(`Transaction collided too many times (${tries})`);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var InMemoryUnderlyingTransaction = class {
|
|
28
|
+
operations = [];
|
|
29
|
+
id = Math.random().toString().substring(2);
|
|
30
|
+
ensureNoWrites() {
|
|
31
|
+
if (this.operations.some((operation) => operation.type === "write" || operation.type === "create" || operation.type === "delete")) throw new CannotReadAfterWrites();
|
|
32
|
+
}
|
|
33
|
+
markRead(collectionName, id, savedAt) {
|
|
34
|
+
this.ensureNoWrites();
|
|
35
|
+
this.operations.push({
|
|
36
|
+
type: "read",
|
|
37
|
+
collectionName,
|
|
38
|
+
id,
|
|
39
|
+
savedAt
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
markWritten(collectionName, id, data) {
|
|
43
|
+
this.operations.push({
|
|
44
|
+
type: "write",
|
|
45
|
+
collectionName,
|
|
46
|
+
id,
|
|
47
|
+
data
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
markCreated(collectionName, id, data) {
|
|
51
|
+
this.operations.push({
|
|
52
|
+
type: "create",
|
|
53
|
+
collectionName,
|
|
54
|
+
id,
|
|
55
|
+
data
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
markDeleted(collectionName, id, savedAt) {
|
|
59
|
+
this.operations.push({
|
|
60
|
+
type: "delete",
|
|
61
|
+
collectionName,
|
|
62
|
+
id,
|
|
63
|
+
savedAt
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
checkConsistency(storage) {
|
|
67
|
+
for (const operation of this.operations) {
|
|
68
|
+
if (operation.type === "read") {
|
|
69
|
+
const collection = storage.getCollection(operation.collectionName);
|
|
70
|
+
if (!collection) return false;
|
|
71
|
+
if (operation.savedAt !== collection.getRaw(operation.id)?.savedAt) return false;
|
|
72
|
+
}
|
|
73
|
+
if (operation.type === "create") {
|
|
74
|
+
const collection = storage.getCollection(operation.collectionName);
|
|
75
|
+
if (collection && collection.getRaw(operation.id)?.savedAt !== void 0) throw new DocumentAlreadyExists();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
var InMemoryDatabase = class InMemoryDatabase {
|
|
82
|
+
storage = new Storage();
|
|
83
|
+
clear(collectionName) {
|
|
84
|
+
this.storage.getCollection(collectionName).clear();
|
|
85
|
+
}
|
|
86
|
+
load(collectionName, id, trx) {
|
|
87
|
+
const collection = this.storage.getCollection(collectionName);
|
|
88
|
+
const data = collection.get(id);
|
|
89
|
+
if (trx) {
|
|
90
|
+
const doc = collection.getRaw(id);
|
|
91
|
+
trx.markRead(collectionName, id, doc?.savedAt);
|
|
92
|
+
}
|
|
93
|
+
return data;
|
|
94
|
+
}
|
|
95
|
+
delete(collectionName, id, trx) {
|
|
96
|
+
if (trx) {
|
|
97
|
+
const doc = this.storage.getCollection(collectionName).getRaw(id);
|
|
98
|
+
trx.markDeleted(collectionName, id, doc?.savedAt);
|
|
99
|
+
} else this.storage.getCollection(collectionName).delete(id);
|
|
100
|
+
}
|
|
101
|
+
countAll(collectionName) {
|
|
102
|
+
return this.storage.getCollection(collectionName).countAll();
|
|
103
|
+
}
|
|
104
|
+
loadAll(collectionName) {
|
|
105
|
+
return this.storage.getCollection(collectionName).getAllRaw();
|
|
106
|
+
}
|
|
107
|
+
loadLatestSnapshot(id) {
|
|
108
|
+
return this.storage.getCollection("snapshots").getLatestSnapshot(id);
|
|
109
|
+
}
|
|
110
|
+
save(collectionName, id, data, trx) {
|
|
111
|
+
if (trx) trx.markWritten(collectionName, id, data);
|
|
112
|
+
else this.storage.getCollection(collectionName).save(id, data);
|
|
113
|
+
}
|
|
114
|
+
create(collectionName, id, data, trx) {
|
|
115
|
+
if (trx) trx.markCreated(collectionName, id, data);
|
|
116
|
+
else {
|
|
117
|
+
if (this.storage.getCollection(collectionName).get(id)) throw new Error(`Document with id ${id} already exists`);
|
|
118
|
+
this.storage.getCollection(collectionName).save(id, data);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
static transactionTries = 5;
|
|
122
|
+
async transactionally(fn) {
|
|
123
|
+
let trx = new InMemoryTransaction(new InMemoryUnderlyingTransaction());
|
|
124
|
+
let retry = InMemoryDatabase.transactionTries;
|
|
125
|
+
let latestReturnValue = void 0;
|
|
126
|
+
while (retry--) try {
|
|
127
|
+
latestReturnValue = await fn(trx);
|
|
128
|
+
this.streamId(trx);
|
|
129
|
+
break;
|
|
130
|
+
} catch (error) {
|
|
131
|
+
if (error instanceof TransactionCollision) trx = new InMemoryTransaction(new InMemoryUnderlyingTransaction());
|
|
132
|
+
else throw error;
|
|
133
|
+
}
|
|
134
|
+
if (retry === -1) throw new TransactionCollidedTooManyTimes(InMemoryDatabase.transactionTries);
|
|
135
|
+
return latestReturnValue;
|
|
136
|
+
}
|
|
137
|
+
streamId(trx) {
|
|
138
|
+
if (!trx.transaction.checkConsistency(this.storage)) throw new TransactionCollision();
|
|
139
|
+
for (const operation of trx.transaction.operations) {
|
|
140
|
+
if (operation.type === "read") continue;
|
|
141
|
+
if (operation.type === "write") this.save(operation.collectionName, operation.id, operation.data);
|
|
142
|
+
if (operation.type === "delete") this.delete(operation.collectionName, operation.id);
|
|
143
|
+
if (operation.type === "create") this.create(operation.collectionName, operation.id, operation.data);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
print() {
|
|
147
|
+
console.log(["Database:", this.storage.toPretty()].join("\n"));
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
152
|
+
export { CannotReadAfterWrites, InMemoryDatabase, InMemoryUnderlyingTransaction, TransactionCollidedTooManyTimes };
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
|
@@ -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;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"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const require_in_memory_collection = require('./in-memory.collection.js');
|
|
2
|
+
|
|
3
|
+
//#region src/store/in-memory.storage.ts
|
|
4
|
+
var Storage = class Storage {
|
|
5
|
+
constructor(collections = /* @__PURE__ */ new Map()) {
|
|
6
|
+
this.collections = collections;
|
|
7
|
+
}
|
|
8
|
+
clone() {
|
|
9
|
+
const clone = /* @__PURE__ */ new Map();
|
|
10
|
+
for (const [collectionName, collection] of this.collections) clone.set(collectionName, collection.clone());
|
|
11
|
+
return new Storage(clone);
|
|
12
|
+
}
|
|
13
|
+
merge(other) {
|
|
14
|
+
const collections = /* @__PURE__ */ new Map();
|
|
15
|
+
for (const [collectionName, collection] of other.collections) collections.set(collectionName, this.getCollection(collectionName).merge(collection));
|
|
16
|
+
return new Storage(collections);
|
|
17
|
+
}
|
|
18
|
+
getCollection(collectionName) {
|
|
19
|
+
const collection = this.collections.get(collectionName) || new require_in_memory_collection.Collection();
|
|
20
|
+
this.collections.set(collectionName, collection);
|
|
21
|
+
return collection;
|
|
22
|
+
}
|
|
23
|
+
toPretty() {
|
|
24
|
+
return [...this.collections.entries()].map(([collectionName, collection]) => {
|
|
25
|
+
return [`Collection: "${collectionName}"`, collection.toPretty()].join("\n");
|
|
26
|
+
}).join("\n");
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
exports.Storage = Storage;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Collection } from "./in-memory.collection.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/store/in-memory.storage.ts
|
|
4
|
+
var Storage = class Storage {
|
|
5
|
+
constructor(collections = /* @__PURE__ */ new Map()) {
|
|
6
|
+
this.collections = collections;
|
|
7
|
+
}
|
|
8
|
+
clone() {
|
|
9
|
+
const clone = /* @__PURE__ */ new Map();
|
|
10
|
+
for (const [collectionName, collection] of this.collections) clone.set(collectionName, collection.clone());
|
|
11
|
+
return new Storage(clone);
|
|
12
|
+
}
|
|
13
|
+
merge(other) {
|
|
14
|
+
const collections = /* @__PURE__ */ new Map();
|
|
15
|
+
for (const [collectionName, collection] of other.collections) collections.set(collectionName, this.getCollection(collectionName).merge(collection));
|
|
16
|
+
return new Storage(collections);
|
|
17
|
+
}
|
|
18
|
+
getCollection(collectionName) {
|
|
19
|
+
const collection = this.collections.get(collectionName) || new Collection();
|
|
20
|
+
this.collections.set(collectionName, collection);
|
|
21
|
+
return collection;
|
|
22
|
+
}
|
|
23
|
+
toPretty() {
|
|
24
|
+
return [...this.collections.entries()].map(([collectionName, collection]) => {
|
|
25
|
+
return [`Collection: "${collectionName}"`, collection.toPretty()].join("\n");
|
|
26
|
+
}).join("\n");
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
export { Storage };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ISerializer, Store, 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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"in-memory.store.d.ts","sourceRoot":"","sources":["../../src/store/in-memory.store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AACtE,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D;;;GAGG;AACH,qBAAa,aAAa,CAAC,CAAC,SAAS,aAAa,CAAE,YAAW,KAAK,CAAC,CAAC,CAAC;aAEnD,UAAU,EAAE,MAAM;aAClB,QAAQ,EAAE,gBAAgB;aAC1B,UAAU,EAAE,WAAW,CAAC,CAAC,CAAC;aAC1B,KAAK,CAAC,EAAE,MAAM;gBAHd,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,gBAAgB,EAC1B,UAAU,EAAE,WAAW,CAAC,CAAC,CAAC,EAC1B,KAAK,CAAC,EAAE,MAAM,YAAA;IAG1B,MAAM,CACV,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,EAChC,GAAG,CAAC,EAAE,mBAAmB,GACxB,OAAO,CAAC,CAAC,EAAE,CAAC;IAiBf,KAAK;IAIC,MAAM,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAU1D,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAUxD,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAI9D,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAiB1E,OAAO,CAAC,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC;IAc1C,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC;IAKjE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAI7D,QAAQ;IAIP,SAAS,IAAI,aAAa,CAAC,CAAC,CAAC;CAKrC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
require('./in-memory.database.js');
|
|
2
|
+
require('../in-memory.transaction.js');
|
|
3
|
+
|
|
4
|
+
//#region src/store/in-memory.store.ts
|
|
5
|
+
/**
|
|
6
|
+
* This in memory store is a copy store. It stores a copy of the actual model.
|
|
7
|
+
* It is the recommended inmemory store to use, as it reflects more closely the behaviour of a real store.
|
|
8
|
+
*/
|
|
9
|
+
var InMemoryStore = class {
|
|
10
|
+
constructor(collection, database, serializer, $name) {
|
|
11
|
+
this.collection = collection;
|
|
12
|
+
this.database = database;
|
|
13
|
+
this.serializer = serializer;
|
|
14
|
+
this.$name = $name;
|
|
15
|
+
}
|
|
16
|
+
async filter(predicate, trx) {
|
|
17
|
+
return (await Promise.all(this.database.loadAll(this.collection).map(async (e) => {
|
|
18
|
+
const deserialized = await this.serializer.deserialize({
|
|
19
|
+
...this.$name ? { $name: this.$name } : {},
|
|
20
|
+
...e.data.data
|
|
21
|
+
});
|
|
22
|
+
if (!predicate(deserialized)) return;
|
|
23
|
+
trx?.transaction.markRead(this.collection, e.id, e.data.savedAt);
|
|
24
|
+
return deserialized;
|
|
25
|
+
}))).filter((e) => Boolean(e));
|
|
26
|
+
}
|
|
27
|
+
clear() {
|
|
28
|
+
this.database.clear(this.collection);
|
|
29
|
+
}
|
|
30
|
+
async create(model, trx) {
|
|
31
|
+
const serialized = await this.serializer.serialize(model);
|
|
32
|
+
await this.database.create(this.collection, model.id.serialize(), {
|
|
33
|
+
...this.$name ? { $name: this.$name } : {},
|
|
34
|
+
...serialized
|
|
35
|
+
}, trx?.transaction);
|
|
36
|
+
}
|
|
37
|
+
async save(model, trx) {
|
|
38
|
+
const serialized = await this.serializer.serialize(model);
|
|
39
|
+
await this.database.save(this.collection, model.id.serialize(), {
|
|
40
|
+
...this.$name ? { $name: this.$name } : {},
|
|
41
|
+
...serialized
|
|
42
|
+
}, 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) return;
|
|
50
|
+
return this.serializer.deserialize({
|
|
51
|
+
...this.$name ? { $name: this.$name } : {},
|
|
52
|
+
...serialized
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
loadAll(trx) {
|
|
56
|
+
const serialized = this.database.loadAll(this.collection);
|
|
57
|
+
return Promise.all(serialized.map(async (s) => {
|
|
58
|
+
trx?.transaction.markRead(this.collection, s.id, s.data.savedAt);
|
|
59
|
+
return this.serializer.deserialize({
|
|
60
|
+
...this.$name ? { $name: this.$name } : {},
|
|
61
|
+
...s.data.data
|
|
62
|
+
});
|
|
63
|
+
}));
|
|
64
|
+
}
|
|
65
|
+
async loadMany(ids, trx) {
|
|
66
|
+
return (await Promise.all(ids.map((id) => this.load(id, trx)))).filter((m) => m !== void 0);
|
|
67
|
+
}
|
|
68
|
+
async delete(id, trx) {
|
|
69
|
+
this.database.delete(this.collection, id.serialize(), trx?.transaction);
|
|
70
|
+
}
|
|
71
|
+
async countAll() {
|
|
72
|
+
return this.database.countAll(this.collection);
|
|
73
|
+
}
|
|
74
|
+
async *streamAll() {
|
|
75
|
+
for (const item of await this.filter(() => true)) yield item;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
exports.InMemoryStore = InMemoryStore;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import "./in-memory.database.mjs";
|
|
2
|
+
import "../in-memory.transaction.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/store/in-memory.store.ts
|
|
5
|
+
/**
|
|
6
|
+
* This in memory store is a copy store. It stores a copy of the actual model.
|
|
7
|
+
* It is the recommended inmemory store to use, as it reflects more closely the behaviour of a real store.
|
|
8
|
+
*/
|
|
9
|
+
var InMemoryStore = class {
|
|
10
|
+
constructor(collection, database, serializer, $name) {
|
|
11
|
+
this.collection = collection;
|
|
12
|
+
this.database = database;
|
|
13
|
+
this.serializer = serializer;
|
|
14
|
+
this.$name = $name;
|
|
15
|
+
}
|
|
16
|
+
async filter(predicate, trx) {
|
|
17
|
+
return (await Promise.all(this.database.loadAll(this.collection).map(async (e) => {
|
|
18
|
+
const deserialized = await this.serializer.deserialize({
|
|
19
|
+
...this.$name ? { $name: this.$name } : {},
|
|
20
|
+
...e.data.data
|
|
21
|
+
});
|
|
22
|
+
if (!predicate(deserialized)) return;
|
|
23
|
+
trx?.transaction.markRead(this.collection, e.id, e.data.savedAt);
|
|
24
|
+
return deserialized;
|
|
25
|
+
}))).filter((e) => Boolean(e));
|
|
26
|
+
}
|
|
27
|
+
clear() {
|
|
28
|
+
this.database.clear(this.collection);
|
|
29
|
+
}
|
|
30
|
+
async create(model, trx) {
|
|
31
|
+
const serialized = await this.serializer.serialize(model);
|
|
32
|
+
await this.database.create(this.collection, model.id.serialize(), {
|
|
33
|
+
...this.$name ? { $name: this.$name } : {},
|
|
34
|
+
...serialized
|
|
35
|
+
}, trx?.transaction);
|
|
36
|
+
}
|
|
37
|
+
async save(model, trx) {
|
|
38
|
+
const serialized = await this.serializer.serialize(model);
|
|
39
|
+
await this.database.save(this.collection, model.id.serialize(), {
|
|
40
|
+
...this.$name ? { $name: this.$name } : {},
|
|
41
|
+
...serialized
|
|
42
|
+
}, 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) return;
|
|
50
|
+
return this.serializer.deserialize({
|
|
51
|
+
...this.$name ? { $name: this.$name } : {},
|
|
52
|
+
...serialized
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
loadAll(trx) {
|
|
56
|
+
const serialized = this.database.loadAll(this.collection);
|
|
57
|
+
return Promise.all(serialized.map(async (s) => {
|
|
58
|
+
trx?.transaction.markRead(this.collection, s.id, s.data.savedAt);
|
|
59
|
+
return this.serializer.deserialize({
|
|
60
|
+
...this.$name ? { $name: this.$name } : {},
|
|
61
|
+
...s.data.data
|
|
62
|
+
});
|
|
63
|
+
}));
|
|
64
|
+
}
|
|
65
|
+
async loadMany(ids, trx) {
|
|
66
|
+
return (await Promise.all(ids.map((id) => this.load(id, trx)))).filter((m) => m !== void 0);
|
|
67
|
+
}
|
|
68
|
+
async delete(id, trx) {
|
|
69
|
+
this.database.delete(this.collection, id.serialize(), trx?.transaction);
|
|
70
|
+
}
|
|
71
|
+
async countAll() {
|
|
72
|
+
return this.database.countAll(this.collection);
|
|
73
|
+
}
|
|
74
|
+
async *streamAll() {
|
|
75
|
+
for (const item of await this.filter(() => true)) yield item;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
export { InMemoryStore };
|
package/package.json
CHANGED
|
@@ -1,31 +1,33 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
2
|
+
"name": "@ddd-ts/store-inmemory",
|
|
3
|
+
"version": "0.0.38",
|
|
4
|
+
"types": "dist/index.d.ts",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"url": "git+https://github.com/ddd-ts/monorepo"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@ddd-ts/core": "0.0.38"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@ddd-ts/tools": "0.0.38",
|
|
17
|
+
"@ddd-ts/types": "0.0.38",
|
|
18
|
+
"@types/jest": "^29.5.1",
|
|
19
|
+
"@types/node": "^20.12.4"
|
|
20
|
+
},
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": "./dist/index.mjs",
|
|
24
|
+
"require": "./dist/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"main": "./dist/index.js",
|
|
29
|
+
"module": "./dist/index.mjs",
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsdown --config node_modules/@ddd-ts/tools/tsdown.config.js"
|
|
32
|
+
}
|
|
33
|
+
}
|