@ddd-ts/store-inmemory 0.0.36 → 0.0.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/in-memory.transaction.js +35 -35
- package/dist/in-memory.transaction.mjs +34 -0
- package/dist/index.js +13 -15
- package/dist/index.mjs +5 -0
- package/dist/store/in-memory.collection.js +61 -71
- package/dist/store/in-memory.collection.mjs +64 -0
- package/dist/store/in-memory.database.js +153 -175
- package/dist/store/in-memory.database.mjs +152 -0
- package/dist/store/in-memory.storage.js +31 -38
- package/dist/store/in-memory.storage.mjs +31 -0
- package/dist/store/in-memory.store.d.ts +1 -1
- package/dist/store/in-memory.store.d.ts.map +1 -1
- package/dist/store/in-memory.store.js +79 -83
- package/dist/store/in-memory.store.mjs +80 -0
- package/package.json +32 -21
- package/dist/in-memory.transaction.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/store/in-memory.collection.js.map +0 -1
- package/dist/store/in-memory.database.js.map +0 -1
- package/dist/store/in-memory.storage.js.map +0 -1
- package/dist/store/in-memory.store.js.map +0 -1
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.
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
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
|
|
35
34
|
exports.FakeInMemoryTransactionPerformer = FakeInMemoryTransactionPerformer;
|
|
36
|
-
|
|
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.js
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
Object.defineProperty(exports, "InMemoryStore", { enumerable: true, get: function () { return in_memory_store_1.InMemoryStore; } });
|
|
15
|
-
//# sourceMappingURL=index.js.map
|
|
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 };
|
|
@@ -1,75 +1,65 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.Collection = void 0;
|
|
1
|
+
|
|
2
|
+
//#region src/store/in-memory.collection.ts
|
|
4
3
|
function now() {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
return BigInt(Date.now() * 1_000);
|
|
4
|
+
if (typeof process === "object" && typeof process.hrtime === "function") return process.hrtime.bigint() / BigInt(1e3);
|
|
5
|
+
return BigInt(Date.now() * 1e3);
|
|
9
6
|
}
|
|
10
|
-
class Collection {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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;
|
|
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
|
+
};
|
|
69
59
|
function replaceBigInt(key, value) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
return value;
|
|
60
|
+
if (typeof value === "bigint") return value.toString();
|
|
61
|
+
return value;
|
|
74
62
|
}
|
|
75
|
-
|
|
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 };
|