@decohere/data-memory 0.0.1-dev.20261122.343 → 0.0.1-dev.5
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/package.json +3 -3
- package/src/memory-entity.ts +150 -0
- package/src/memory.plugin.ts +63 -0
- package/src/memory.row-store.ts +87 -0
- package/src/memory.store.ts +508 -0
- package/tests/memory.plugin.spec.ts +100 -0
- package/tests/memory.store.spec.ts +157 -0
- package/tests/model.ts +39 -0
- package/LICENSE +0 -15
- package/es6/src/memory-entity.js +0 -91
- package/es6/src/memory.plugin.js +0 -119
- package/es6/src/memory.row-store.js +0 -62
- package/es6/src/memory.store.js +0 -447
- package/types/src/index.d.ts +0 -4
- package/types/src/memory-entity.d.ts +0 -52
- package/types/src/memory.plugin.d.ts +0 -20
- package/types/src/memory.row-store.d.ts +0 -27
- package/types/src/memory.store.d.ts +0 -65
- /package/{es6/src/index.js → src/index.ts} +0 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// What the memory store does that the shared contract cases can't show: the database's own rules
|
|
2
|
+
// (foreign keys, row locks, deadlocks, SQL's nulls) and rows copied in and out.
|
|
3
|
+
import { OptimisticLockError, TrackingUnitOfWork } from "@decohere/data";
|
|
4
|
+
import { describe, expect, it } from "vitest";
|
|
5
|
+
import { MemoryRowStore, MemoryStore } from "../src/index.js";
|
|
6
|
+
import { Book, book, entities, Shelf, shelf } from "./model.js";
|
|
7
|
+
|
|
8
|
+
function open() {
|
|
9
|
+
const store = new MemoryStore(entities);
|
|
10
|
+
return () => new TrackingUnitOfWork(new MemoryRowStore(store));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async function seeded() {
|
|
14
|
+
const begin = open();
|
|
15
|
+
await using uow = begin();
|
|
16
|
+
const s = shelf("s1", "fiction");
|
|
17
|
+
s.books.add(book("b1", "Dune", s, { rating: 5, meta: { tags: ["sand"] } }));
|
|
18
|
+
s.books.add(book("b2", "Emma", s));
|
|
19
|
+
uow.add(s);
|
|
20
|
+
await uow.commit();
|
|
21
|
+
return begin;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Settles every pending promise callback, so a waiting unit of work shows it is still waiting. */
|
|
25
|
+
const settle = () => new Promise((resolve) => setImmediate(resolve));
|
|
26
|
+
|
|
27
|
+
describe("memory store", () => {
|
|
28
|
+
it("refuses to delete a row another row still references, and keeps both", async () => {
|
|
29
|
+
const begin = await seeded();
|
|
30
|
+
{
|
|
31
|
+
await using uow = begin();
|
|
32
|
+
uow.remove(await uow.findById(Shelf, "s1"));
|
|
33
|
+
await expect(uow.commit()).rejects.toThrow(
|
|
34
|
+
"Can't delete Shelf s1: Book.shelf still references it",
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
await using uow = begin();
|
|
38
|
+
expect(await uow.findById(Shelf, "s1", ["books"])).toMatchObject({ label: "fiction" });
|
|
39
|
+
expect(await uow.find(Book)).toHaveLength(2);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("refuses a reference to a row that doesn't exist, or none where one is required", async () => {
|
|
43
|
+
// The unit of work inserts what an entity references, so this goes to the rows directly.
|
|
44
|
+
const rows = new MemoryRowStore(new MemoryStore(entities));
|
|
45
|
+
await expect(
|
|
46
|
+
rows.insert(Book, [{ id: "b9", title: "Lost", shelf: "nowhere" }]),
|
|
47
|
+
).rejects.toThrow("Book.shelf references Shelf nowhere, which doesn't exist");
|
|
48
|
+
await expect(
|
|
49
|
+
rows.insert(Book, [{ id: "b9", title: "Lost", shelf: null }]),
|
|
50
|
+
).rejects.toThrow("Book.shelf can't be empty");
|
|
51
|
+
expect(await rows.select(Book, {})).toEqual([]);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("refuses a second row with the same key", async () => {
|
|
55
|
+
const begin = await seeded();
|
|
56
|
+
await using uow = begin();
|
|
57
|
+
uow.add(shelf("s1", "again"));
|
|
58
|
+
await expect(uow.commit()).rejects.toThrow("Shelf s1 already exists");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("stores copies: changing an object after commit, or a loaded one without commit, changes nothing stored", async () => {
|
|
62
|
+
const begin = open();
|
|
63
|
+
const s = shelf("s1", "fiction");
|
|
64
|
+
const meta = { tags: ["sand"] };
|
|
65
|
+
const published = new Date("1965-08-01T00:00:00Z");
|
|
66
|
+
{
|
|
67
|
+
await using uow = begin();
|
|
68
|
+
uow.add(s);
|
|
69
|
+
uow.add(book("b1", "Dune", s, { meta, published }));
|
|
70
|
+
await uow.commit();
|
|
71
|
+
}
|
|
72
|
+
meta.tags.push("worms");
|
|
73
|
+
published.setUTCFullYear(2000);
|
|
74
|
+
{
|
|
75
|
+
await using uow = begin();
|
|
76
|
+
const loaded = await uow.findById(Book, "b1");
|
|
77
|
+
loaded.meta.tags.push("spice");
|
|
78
|
+
}
|
|
79
|
+
await using uow = begin();
|
|
80
|
+
const stored = await uow.findById(Book, "b1");
|
|
81
|
+
expect(stored.meta).toEqual({ tags: ["sand"] });
|
|
82
|
+
expect(stored.published).toEqual(new Date("1965-08-01T00:00:00Z"));
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("compares and orders empty values as SQL does", async () => {
|
|
86
|
+
const begin = await seeded();
|
|
87
|
+
await using uow = begin();
|
|
88
|
+
expect((await uow.find(Book, { where: { rating: { ne: 4 } } })).map((b) => b.id)).toEqual([
|
|
89
|
+
"b1",
|
|
90
|
+
]);
|
|
91
|
+
expect((await uow.find(Book, { where: { rating: { lt: 9 } } })).map((b) => b.id)).toEqual([
|
|
92
|
+
"b1",
|
|
93
|
+
]);
|
|
94
|
+
const asc = await uow.find(Book, { orderBy: { rating: "asc" } });
|
|
95
|
+
expect(asc.map((b) => b.id)).toEqual(["b1", "b2"]);
|
|
96
|
+
const desc = await uow.find(Book, { orderBy: { rating: "desc" } });
|
|
97
|
+
expect(desc.map((b) => b.id)).toEqual(["b2", "b1"]);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('serialises lock: "update" on a row: the second waits for the first to commit, then reads its write', async () => {
|
|
101
|
+
const begin = await seeded();
|
|
102
|
+
await using first = begin();
|
|
103
|
+
await using second = begin();
|
|
104
|
+
const [mine] = await first.find(Shelf, { where: { id: "s1" }, lock: "update" });
|
|
105
|
+
let theirs: Shelf | undefined;
|
|
106
|
+
const waiting = second
|
|
107
|
+
.find(Shelf, { where: { id: "s1" }, lock: "update" })
|
|
108
|
+
.then(([found]) => (theirs = found));
|
|
109
|
+
await settle();
|
|
110
|
+
expect(theirs).toBeUndefined();
|
|
111
|
+
mine.label = "classics";
|
|
112
|
+
await first.commit();
|
|
113
|
+
await waiting;
|
|
114
|
+
expect(theirs).toMatchObject({ label: "classics", version: 2 });
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("makes two commits updating one row at once take turns: the later one loses on its version", async () => {
|
|
118
|
+
const begin = await seeded();
|
|
119
|
+
await using first = begin();
|
|
120
|
+
await using second = begin();
|
|
121
|
+
(await first.findById(Shelf, "s1")).label = "first";
|
|
122
|
+
(await second.findById(Shelf, "s1")).label = "second";
|
|
123
|
+
const [a, b] = await Promise.allSettled([first.commit(), second.commit()]);
|
|
124
|
+
expect(a.status).toBe("fulfilled");
|
|
125
|
+
expect(b).toMatchObject({ status: "rejected", reason: expect.any(OptimisticLockError) });
|
|
126
|
+
await using check = begin();
|
|
127
|
+
expect(await check.findById(Shelf, "s1")).toMatchObject({ label: "first", version: 2 });
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("ends a deadlock by failing the unit of work that would wait for itself", async () => {
|
|
131
|
+
const begin = await seeded();
|
|
132
|
+
{
|
|
133
|
+
await using uow = begin();
|
|
134
|
+
uow.add(shelf("s2", "poetry"));
|
|
135
|
+
await uow.commit();
|
|
136
|
+
}
|
|
137
|
+
await using first = begin();
|
|
138
|
+
await using second = begin();
|
|
139
|
+
await first.find(Shelf, { where: { id: "s1" }, lock: "update" });
|
|
140
|
+
await second.find(Shelf, { where: { id: "s2" }, lock: "update" });
|
|
141
|
+
const firstWaits = first.find(Shelf, { where: { id: "s2" }, lock: "update" });
|
|
142
|
+
await settle();
|
|
143
|
+
await expect(second.find(Shelf, { where: { id: "s1" }, lock: "update" })).rejects.toThrow(
|
|
144
|
+
/Deadlock/,
|
|
145
|
+
);
|
|
146
|
+
await second[Symbol.asyncDispose]();
|
|
147
|
+
await expect(firstWaits).resolves.toHaveLength(1);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("rejects a query on a property that isn't mapped", async () => {
|
|
151
|
+
const begin = await seeded();
|
|
152
|
+
await using uow = begin();
|
|
153
|
+
await expect(uow.find(Book, { where: { nope: 1 } as never })).rejects.toThrow(
|
|
154
|
+
'Book has no field "nope"',
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
});
|
package/tests/model.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// A small domain for the memory provider's own tests: a shelf of books, each with notes.
|
|
2
|
+
import { type Children, ChildList } from "@decohere/data";
|
|
3
|
+
import { mapEntity } from "../src/index.js";
|
|
4
|
+
|
|
5
|
+
export class Shelf {
|
|
6
|
+
public id!: string;
|
|
7
|
+
public label!: string;
|
|
8
|
+
public version!: number;
|
|
9
|
+
public books: Children<Book> = new ChildList();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class Book {
|
|
13
|
+
public id!: string;
|
|
14
|
+
public title!: string;
|
|
15
|
+
public shelf!: Shelf;
|
|
16
|
+
public rating: number | null = null;
|
|
17
|
+
public published: Date | null = null;
|
|
18
|
+
public meta: { tags: string[] } | null = null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function shelf(id: string, label: string) {
|
|
22
|
+
return Object.assign(new Shelf(), { id, label });
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function book(id: string, title: string, s: Shelf, more: Partial<Book> = {}) {
|
|
26
|
+
return Object.assign(new Book(), { id, title, shelf: s, ...more });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const entities = [
|
|
30
|
+
mapEntity(Shelf, {
|
|
31
|
+
fields: ["label"],
|
|
32
|
+
version: "version",
|
|
33
|
+
children: [{ prop: "books", target: Book, parentRef: "shelf" }],
|
|
34
|
+
}),
|
|
35
|
+
mapEntity(Book, {
|
|
36
|
+
fields: ["title", "rating", "published", "meta"],
|
|
37
|
+
references: [{ prop: "shelf", target: Shelf }],
|
|
38
|
+
}),
|
|
39
|
+
];
|
package/LICENSE
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
ISC License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025-2026 Typesmiths
|
|
4
|
-
|
|
5
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
-
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
-
copyright notice and this permission notice appear in all copies.
|
|
8
|
-
|
|
9
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
-
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/es6/src/memory-entity.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { DataConfigurationError } from "@decohere/data";
|
|
2
|
-
/**
|
|
3
|
-
* Map a class for the memory provider. With no ORM to read metadata from, the mapping is written
|
|
4
|
-
* out: which properties are stored, which reference another class, and which hold children.
|
|
5
|
-
*/ export function mapEntity(type, options) {
|
|
6
|
-
const key = options.key ?? [
|
|
7
|
-
"id"
|
|
8
|
-
];
|
|
9
|
-
const references = (options.references ?? []).map((r)=>({
|
|
10
|
-
prop: r.prop,
|
|
11
|
-
target: r.target,
|
|
12
|
-
nullable: r.nullable ?? false,
|
|
13
|
-
onDelete: r.onDelete
|
|
14
|
-
}));
|
|
15
|
-
const isReference = (prop)=>references.some((r)=>r.prop === prop);
|
|
16
|
-
const fields = new Set(options.fields);
|
|
17
|
-
for (const prop of key)if (!isReference(prop)) fields.add(prop);
|
|
18
|
-
if (options.version) fields.add(options.version);
|
|
19
|
-
if (options.generated) fields.add(options.generated);
|
|
20
|
-
return {
|
|
21
|
-
type,
|
|
22
|
-
name: type.name,
|
|
23
|
-
key,
|
|
24
|
-
fields: [
|
|
25
|
-
...fields
|
|
26
|
-
],
|
|
27
|
-
references,
|
|
28
|
-
children: (options.children ?? []).map((c)=>({
|
|
29
|
-
prop: c.prop,
|
|
30
|
-
target: c.target,
|
|
31
|
-
parentRef: c.parentRef,
|
|
32
|
-
orderBy: c.orderBy
|
|
33
|
-
})),
|
|
34
|
-
version: options.version,
|
|
35
|
-
generated: options.generated
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Check the mappings fit together, so a mistake fails when the app is built rather than at the
|
|
40
|
-
* first query: every key, reference and child names something mapped.
|
|
41
|
-
*/ function check(entities) {
|
|
42
|
-
const byType = new Map();
|
|
43
|
-
for (const entity of entities){
|
|
44
|
-
if (byType.has(entity.type)) {
|
|
45
|
-
throw new DataConfigurationError(`${entity.name} is mapped twice`);
|
|
46
|
-
}
|
|
47
|
-
byType.set(entity.type, entity);
|
|
48
|
-
}
|
|
49
|
-
const mapped = (owner, prop, target)=>{
|
|
50
|
-
const found = byType.get(target);
|
|
51
|
-
if (found == null) {
|
|
52
|
-
throw new DataConfigurationError(`${owner.name}.${prop} points at ${target.name}, which isn't mapped: add it to entities`);
|
|
53
|
-
}
|
|
54
|
-
return found;
|
|
55
|
-
};
|
|
56
|
-
for (const entity of entities){
|
|
57
|
-
const { name } = entity;
|
|
58
|
-
const isReference = (prop)=>entity.references.some((r)=>r.prop === prop);
|
|
59
|
-
if (entity.key.length === 0) {
|
|
60
|
-
throw new DataConfigurationError(`${name} has no key`);
|
|
61
|
-
}
|
|
62
|
-
if (entity.generated && (entity.key.length !== 1 || entity.key[0] !== entity.generated)) {
|
|
63
|
-
throw new DataConfigurationError(`${name}.${entity.generated} is generated, so it must be the whole key`);
|
|
64
|
-
}
|
|
65
|
-
for (const ref of entity.references){
|
|
66
|
-
if (entity.fields.includes(ref.prop)) {
|
|
67
|
-
throw new DataConfigurationError(`${name}.${ref.prop} is both a field and a reference`);
|
|
68
|
-
}
|
|
69
|
-
const target = mapped(entity, ref.prop, ref.target);
|
|
70
|
-
if (target.key.length !== 1) {
|
|
71
|
-
throw new DataConfigurationError(`${name}.${ref.prop} references ${target.name}, whose key is composite: a reference must point at a single key`);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
for (const spec of entity.children){
|
|
75
|
-
const child = mapped(entity, spec.prop, spec.target);
|
|
76
|
-
const parentRef = child.references.find((r)=>r.prop === spec.parentRef);
|
|
77
|
-
if (parentRef == null || parentRef.target !== entity.type) {
|
|
78
|
-
throw new DataConfigurationError(`${name}.${spec.prop}: ${child.name}.${spec.parentRef} must be a reference to ${name}`);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
for (const prop of entity.key){
|
|
82
|
-
if (!entity.fields.includes(prop) && !isReference(prop)) {
|
|
83
|
-
throw new DataConfigurationError(`${name}'s key ${prop} isn't mapped`);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return byType;
|
|
88
|
-
}
|
|
89
|
-
export const memoryEntities = {
|
|
90
|
-
check
|
|
91
|
-
};
|
package/es6/src/memory.plugin.js
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
function _ts_add_disposable_resource(env, value, async) {
|
|
2
|
-
if (value !== null && value !== void 0) {
|
|
3
|
-
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
4
|
-
var dispose, inner;
|
|
5
|
-
if (async) {
|
|
6
|
-
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
7
|
-
dispose = value[Symbol.asyncDispose];
|
|
8
|
-
}
|
|
9
|
-
if (dispose === void 0) {
|
|
10
|
-
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
11
|
-
dispose = value[Symbol.dispose];
|
|
12
|
-
if (async) inner = dispose;
|
|
13
|
-
}
|
|
14
|
-
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
15
|
-
if (inner) dispose = function() {
|
|
16
|
-
try {
|
|
17
|
-
inner.call(this);
|
|
18
|
-
} catch (e) {
|
|
19
|
-
return Promise.reject(e);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
env.stack.push({
|
|
23
|
-
value: value,
|
|
24
|
-
dispose: dispose,
|
|
25
|
-
async: async
|
|
26
|
-
});
|
|
27
|
-
} else if (async) {
|
|
28
|
-
env.stack.push({
|
|
29
|
-
async: true
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
return value;
|
|
33
|
-
}
|
|
34
|
-
function _ts_dispose_resources(env) {
|
|
35
|
-
var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
36
|
-
var e = new Error(message);
|
|
37
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
38
|
-
};
|
|
39
|
-
return (_ts_dispose_resources = function _ts_dispose_resources(env) {
|
|
40
|
-
function fail(e) {
|
|
41
|
-
env.error = env.hasError ? new _SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
42
|
-
env.hasError = true;
|
|
43
|
-
}
|
|
44
|
-
var r, s = 0;
|
|
45
|
-
function next() {
|
|
46
|
-
while(r = env.stack.pop()){
|
|
47
|
-
try {
|
|
48
|
-
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
49
|
-
if (r.dispose) {
|
|
50
|
-
var result = r.dispose.call(r.value);
|
|
51
|
-
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
|
|
52
|
-
fail(e);
|
|
53
|
-
return next();
|
|
54
|
-
});
|
|
55
|
-
} else s |= 1;
|
|
56
|
-
} catch (e) {
|
|
57
|
-
fail(e);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
61
|
-
if (env.hasError) throw env.error;
|
|
62
|
-
}
|
|
63
|
-
return next();
|
|
64
|
-
})(env);
|
|
65
|
-
}
|
|
66
|
-
import { DataConfigurationError, DEFAULT_DATA_SOURCE, TrackingUnitOfWork } from "@decohere/data";
|
|
67
|
-
import { MemoryRowStore } from "./memory.row-store.js";
|
|
68
|
-
import { MemoryStore } from "./memory.store.js";
|
|
69
|
-
/**
|
|
70
|
-
* Keep the data in the process, with no database: for tests and local runs. Each build of the app
|
|
71
|
-
* gets empty stores of its own. Needs no `database` config.
|
|
72
|
-
*/ export function memory(config) {
|
|
73
|
-
const modules = Array.isArray(config) ? config : [
|
|
74
|
-
config
|
|
75
|
-
];
|
|
76
|
-
if (modules.length > 1 && modules.some((m)=>m.name == null)) {
|
|
77
|
-
throw new DataConfigurationError("Name is required if there is more than one datasource");
|
|
78
|
-
}
|
|
79
|
-
return (builder)=>{
|
|
80
|
-
const stores = new Map();
|
|
81
|
-
builder.withServices((containerBuilder)=>{
|
|
82
|
-
for (const module of modules){
|
|
83
|
-
const store = new MemoryStore(module.entities);
|
|
84
|
-
stores.set(module, store);
|
|
85
|
-
containerBuilder.registerRequestScoped(module.name ?? DEFAULT_DATA_SOURCE, ()=>new TrackingUnitOfWork(new MemoryRowStore(store), {
|
|
86
|
-
outbox: module.outbox
|
|
87
|
-
}));
|
|
88
|
-
for (const repository of module.repositories ?? []){
|
|
89
|
-
containerBuilder.registerRequestScoped(repository);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
if (modules.every((m)=>m.seed == null)) return;
|
|
94
|
-
builder.registerStartStep(async ()=>{
|
|
95
|
-
for (const module of modules){
|
|
96
|
-
const env = {
|
|
97
|
-
stack: [],
|
|
98
|
-
error: void 0,
|
|
99
|
-
hasError: false
|
|
100
|
-
};
|
|
101
|
-
try {
|
|
102
|
-
const store = stores.get(module);
|
|
103
|
-
if (module.seed == null || store == null) continue;
|
|
104
|
-
const unitOfWork = _ts_add_disposable_resource(env, new TrackingUnitOfWork(new MemoryRowStore(store), {
|
|
105
|
-
outbox: module.outbox
|
|
106
|
-
}), true);
|
|
107
|
-
await module.seed(unitOfWork);
|
|
108
|
-
await unitOfWork.commit();
|
|
109
|
-
} catch (e) {
|
|
110
|
-
env.error = e;
|
|
111
|
-
env.hasError = true;
|
|
112
|
-
} finally{
|
|
113
|
-
const result = _ts_dispose_resources(env);
|
|
114
|
-
if (result) await result;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}, "memory data sources");
|
|
118
|
-
};
|
|
119
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { DataError } from "@decohere/data";
|
|
2
|
-
/**
|
|
3
|
-
* The memory provider's share of the unit of work: one transaction at a time on a
|
|
4
|
-
* {@link MemoryStore}. Outside a transaction each operation runs in one of its own, as a database
|
|
5
|
-
* runs a statement on its own.
|
|
6
|
-
*/ export class MemoryRowStore {
|
|
7
|
-
store;
|
|
8
|
-
transaction;
|
|
9
|
-
constructor(store){
|
|
10
|
-
this.store = store;
|
|
11
|
-
}
|
|
12
|
-
model(type) {
|
|
13
|
-
return this.store.model(type);
|
|
14
|
-
}
|
|
15
|
-
async begin() {
|
|
16
|
-
if (this.transaction) throw new DataError("A transaction is already open");
|
|
17
|
-
this.transaction = this.store.begin();
|
|
18
|
-
}
|
|
19
|
-
async commit() {
|
|
20
|
-
this.store.commit(this.end());
|
|
21
|
-
}
|
|
22
|
-
async rollback() {
|
|
23
|
-
this.store.rollback(this.end());
|
|
24
|
-
}
|
|
25
|
-
async release() {
|
|
26
|
-
if (this.transaction) await this.rollback();
|
|
27
|
-
}
|
|
28
|
-
inTransaction() {
|
|
29
|
-
return this.transaction != null;
|
|
30
|
-
}
|
|
31
|
-
select(type, query) {
|
|
32
|
-
return this.run((tx)=>this.store.select(tx, type, query));
|
|
33
|
-
}
|
|
34
|
-
insert(type, rows) {
|
|
35
|
-
return this.run((tx)=>this.store.insert(tx, type, rows));
|
|
36
|
-
}
|
|
37
|
-
update(type, key, changes, version) {
|
|
38
|
-
return this.run((tx)=>this.store.update(tx, type, key, changes, version));
|
|
39
|
-
}
|
|
40
|
-
delete(type, keys) {
|
|
41
|
-
return this.run((tx)=>this.store.delete(tx, type, keys));
|
|
42
|
-
}
|
|
43
|
-
end() {
|
|
44
|
-
const tx = this.transaction;
|
|
45
|
-
if (tx == null) throw new DataError("No transaction is open");
|
|
46
|
-
this.transaction = undefined;
|
|
47
|
-
return tx;
|
|
48
|
-
}
|
|
49
|
-
async run(operation) {
|
|
50
|
-
if (this.transaction) return operation(this.transaction);
|
|
51
|
-
const tx = this.store.begin();
|
|
52
|
-
let result;
|
|
53
|
-
try {
|
|
54
|
-
result = await operation(tx);
|
|
55
|
-
} catch (err) {
|
|
56
|
-
this.store.rollback(tx);
|
|
57
|
-
throw err;
|
|
58
|
-
}
|
|
59
|
-
this.store.commit(tx);
|
|
60
|
-
return result;
|
|
61
|
-
}
|
|
62
|
-
}
|