@ddd-ts/store-inmemory 0.0.3-43 → 0.0.3-45
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.
|
@@ -1,97 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
3
|
+
const tests_1 = require("@ddd-ts/tests");
|
|
4
4
|
const __1 = require("..");
|
|
5
5
|
const in_memory_database_1 = require("./in-memory.database");
|
|
6
|
-
class MyElement {
|
|
7
|
-
id;
|
|
8
|
-
name;
|
|
9
|
-
constructor(id, name) {
|
|
10
|
-
this.id = id;
|
|
11
|
-
this.name = name;
|
|
12
|
-
}
|
|
13
|
-
setName(name) {
|
|
14
|
-
this.name = name;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
class MyElementSerializer extends (0, serialization_1.Serializer)(MyElement)(1n) {
|
|
18
|
-
serialize(value) {
|
|
19
|
-
return { version: this.version, id: value.id, name: value.name };
|
|
20
|
-
}
|
|
21
|
-
deserialize(value) {
|
|
22
|
-
return new MyElement(value.id, value.name);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
6
|
class MyElementStore extends __1.InMemoryStore {
|
|
26
7
|
constructor(database) {
|
|
27
|
-
super("my_collection", database, new MyElementSerializer());
|
|
8
|
+
super("my_collection", database, new tests_1.MyElementSerializer());
|
|
28
9
|
}
|
|
29
10
|
loadEven() {
|
|
30
|
-
return this.filter((e) =>
|
|
11
|
+
return this.filter((e) => e.even);
|
|
31
12
|
}
|
|
32
13
|
}
|
|
33
14
|
describe("InMemoryStore", () => {
|
|
34
15
|
function getStore() {
|
|
35
16
|
const database = new __1.InMemoryDatabase();
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
17
|
+
return {
|
|
18
|
+
store: new MyElementStore(database),
|
|
19
|
+
transactionPerformer: new __1.InMemoryTransactionPerformer(database),
|
|
20
|
+
};
|
|
39
21
|
}
|
|
40
|
-
|
|
41
|
-
const { store } = getStore();
|
|
42
|
-
const element = new MyElement("myid", "timothee");
|
|
43
|
-
await store.save(element);
|
|
44
|
-
});
|
|
45
|
-
it("loads", async () => {
|
|
46
|
-
const { store } = getStore();
|
|
47
|
-
const element = new MyElement("myid", "timothee");
|
|
48
|
-
await store.save(element);
|
|
49
|
-
const pristine = await store.load(element.id);
|
|
50
|
-
expect(pristine).toEqual(element);
|
|
51
|
-
});
|
|
52
|
-
it("filters", async () => {
|
|
53
|
-
const { store } = getStore();
|
|
54
|
-
const elements = [...Array.from({ length: 100 }).keys()].map((e) => new MyElement(e.toString(), `name-${e.toString()}`));
|
|
55
|
-
await Promise.all(elements.map((e) => store.save(e)));
|
|
56
|
-
const onlyEven = await store.loadEven();
|
|
57
|
-
expect(onlyEven.length).toBe(50);
|
|
58
|
-
expect(onlyEven.map((e) => e.name)).toEqual([...Array.from({ length: 50 }).keys()].map((e) => `name-${(e * 2).toString()}`));
|
|
59
|
-
});
|
|
60
|
-
it("deletes", async () => {
|
|
61
|
-
const { store } = getStore();
|
|
62
|
-
const element = new MyElement("myid", "timothee");
|
|
63
|
-
await store.save(element);
|
|
64
|
-
const pristine = await store.delete(element.id);
|
|
65
|
-
expect(pristine).toEqual(undefined);
|
|
66
|
-
});
|
|
67
|
-
it("transactionally writes", async () => {
|
|
68
|
-
const { store, transactionPerformer } = getStore();
|
|
69
|
-
const element = new MyElement("myid", "timothee");
|
|
70
|
-
await transactionPerformer.perform(async (transaction) => {
|
|
71
|
-
await store.save(element, transaction);
|
|
72
|
-
});
|
|
73
|
-
const pristine = await store.load(element.id);
|
|
74
|
-
expect(pristine).toEqual(element);
|
|
75
|
-
});
|
|
76
|
-
it("transactionally deletes", async () => {
|
|
77
|
-
const { store, transactionPerformer } = getStore();
|
|
78
|
-
const element = new MyElement("myid", "timothee");
|
|
79
|
-
await store.save(element);
|
|
80
|
-
await transactionPerformer.perform(async (transaction) => {
|
|
81
|
-
const pristine = await store.load(element.id, transaction);
|
|
82
|
-
expect(pristine).toBeTruthy();
|
|
83
|
-
if (!pristine) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
await store.delete(pristine.id, transaction);
|
|
87
|
-
});
|
|
88
|
-
const pristine = await store.load(element.id);
|
|
89
|
-
expect(pristine).toEqual(undefined);
|
|
90
|
-
});
|
|
22
|
+
(0, tests_1.StoreSuite)(getStore);
|
|
91
23
|
it("fails transactionally read after write", async () => {
|
|
92
24
|
const { store, transactionPerformer } = getStore();
|
|
93
25
|
await expect(transactionPerformer.perform(async (transaction) => {
|
|
94
|
-
const element = new MyElement("myid", "timothee");
|
|
26
|
+
const element = new tests_1.MyElement("myid", "timothee", false);
|
|
95
27
|
await store.save(element, transaction);
|
|
96
28
|
await store.load(element.id, transaction);
|
|
97
29
|
})).rejects.toThrow(in_memory_database_1.CannotReadAfterWrites);
|
|
@@ -99,7 +31,7 @@ describe("InMemoryStore", () => {
|
|
|
99
31
|
it("fails transactionally trying 5 times to write", async () => {
|
|
100
32
|
let uniqueId = 0;
|
|
101
33
|
const { store, transactionPerformer } = getStore();
|
|
102
|
-
const element = new MyElement("myid", "timothee");
|
|
34
|
+
const element = new tests_1.MyElement("myid", "timothee", false);
|
|
103
35
|
await store.save(element);
|
|
104
36
|
let effectCalled = 0;
|
|
105
37
|
await expect(transactionPerformer.perform(async (transaction) => {
|
|
@@ -107,7 +39,7 @@ describe("InMemoryStore", () => {
|
|
|
107
39
|
await store.load(element.id, transaction);
|
|
108
40
|
element.setName("elies");
|
|
109
41
|
uniqueId += 1;
|
|
110
|
-
await store.save(new MyElement(element.id, uniqueId.toString()));
|
|
42
|
+
await store.save(new tests_1.MyElement(element.id, uniqueId.toString(), false));
|
|
111
43
|
await store.save(element, transaction);
|
|
112
44
|
})).rejects.toThrow(in_memory_database_1.TransactionCollidedTooManyTimes);
|
|
113
45
|
expect(effectCalled).toBe(5);
|
|
@@ -115,7 +47,7 @@ describe("InMemoryStore", () => {
|
|
|
115
47
|
it("transactionally writes after 1 fail", async () => {
|
|
116
48
|
let uniqueId = 0;
|
|
117
49
|
const { store, transactionPerformer } = getStore();
|
|
118
|
-
const element = new MyElement("myid", "timothee");
|
|
50
|
+
const element = new tests_1.MyElement("myid", "timothee", false);
|
|
119
51
|
await store.save(element);
|
|
120
52
|
let effectCalled = 0;
|
|
121
53
|
await transactionPerformer.perform(async (transaction) => {
|
|
@@ -127,7 +59,7 @@ describe("InMemoryStore", () => {
|
|
|
127
59
|
}
|
|
128
60
|
loadedElement.setName("elies");
|
|
129
61
|
if (uniqueId === 0) {
|
|
130
|
-
await store.save(new MyElement(element.id, uniqueId.toString()));
|
|
62
|
+
await store.save(new tests_1.MyElement(element.id, uniqueId.toString(), false));
|
|
131
63
|
uniqueId += 1;
|
|
132
64
|
}
|
|
133
65
|
await store.save(loadedElement, transaction);
|
|
@@ -137,7 +69,7 @@ describe("InMemoryStore", () => {
|
|
|
137
69
|
it("transactionally writes after 1 fail and calls onCommit only once", async () => {
|
|
138
70
|
let uniqueId = 0;
|
|
139
71
|
const { store, transactionPerformer } = getStore();
|
|
140
|
-
const element = new MyElement("myid", "timothee");
|
|
72
|
+
const element = new tests_1.MyElement("myid", "timothee", false);
|
|
141
73
|
await store.save(element);
|
|
142
74
|
let onCommitCalled = 0;
|
|
143
75
|
await transactionPerformer.perform(async (transaction) => {
|
|
@@ -151,7 +83,7 @@ describe("InMemoryStore", () => {
|
|
|
151
83
|
}
|
|
152
84
|
loadedElement.setName("elies");
|
|
153
85
|
if (uniqueId === 0) {
|
|
154
|
-
await store.save(new MyElement(element.id, uniqueId.toString()));
|
|
86
|
+
await store.save(new tests_1.MyElement(element.id, uniqueId.toString(), false));
|
|
155
87
|
uniqueId += 1;
|
|
156
88
|
}
|
|
157
89
|
await store.save(loadedElement, transaction);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.store.spec.js","sourceRoot":"","sources":["../../src/store/in-memory.store.spec.ts"],"names":[],"mappings":";;AAA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AAC,QAAQ,CAAC,GAAG,EAAE;gBACxB,cAAc,IAAI,CAAC,CAAC;YACtB,CAAC,CAAC,CAAC;YAEH,MAAM,aAAa,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,EAAE,WAAW,CAAC,CAAC;YAEhE,MAAM,CAAC,aAAa,CAAC,CAAC,UAAU,EAAE,CAAC;YACnC,IAAI,CAAC,aAAa,EAAE;gBAClB,OAAO;aACR;YAED,aAAa,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;YAE/B,IAAI,QAAQ,KAAK,CAAC,EAAE;gBAClB,MAAM,KAAK,CAAC,IAAI,CAAC,IAAI,iBAAS,CAAC,OAAO,CAAC,EAAE,EAAE,QAAQ,CAAC,QAAQ,EAAE,EAAE,KAAK,CAAC,CAAC,CAAC;gBACxE,QAAQ,IAAI,CAAC,CAAC;aACf;YAED,MAAM,KAAK,CAAC,IAAI,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC;QAC/C,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACjC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ddd-ts/store-inmemory",
|
|
3
|
-
"version": "0.0.3-
|
|
3
|
+
"version": "0.0.3-45",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@ddd-ts/dev": "*",
|
|
17
|
+
"@ddd-ts/tests": "*",
|
|
17
18
|
"@ddd-ts/types": "*"
|
|
18
19
|
}
|
|
19
20
|
}
|