rads-db 3.0.37 → 3.0.39
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/features/eventSourcing.cjs +97 -70
- package/features/eventSourcing.mjs +83 -65
- package/package.json +1 -1
|
@@ -23,82 +23,99 @@ function verifyEventSourcingSetup(schema, effects) {
|
|
|
23
23
|
const eventEntityName = `${entityName}Event`;
|
|
24
24
|
const aggregateRelationField = _lodash.default.lowerFirst(entityName);
|
|
25
25
|
validateEventSourcingSetup(schema, entityName, eventEntityName, aggregateRelationField);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
26
|
+
const effect = getEffectFor(entityName, aggregateRelationField, eventEntityName, schema);
|
|
27
|
+
effects[eventEntityName].push(effect);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function getEffectFor(entityName, aggregateRelationField, eventEntityName, schema) {
|
|
31
|
+
return {
|
|
32
|
+
async beforePut(context, docs, ctx) {
|
|
33
|
+
const docsByAggId = {};
|
|
34
|
+
const docsById = {};
|
|
35
|
+
for (const d of docs) {
|
|
36
|
+
if (!d.doc.id || docsById[d.doc.id]) continue;
|
|
37
|
+
docsById[d.doc.id] = d.doc;
|
|
38
|
+
const aggId = d.doc[aggregateRelationField].id;
|
|
39
|
+
if (!aggId) throw new Error(`Missing ${entityName}.${aggregateRelationField}.id`);
|
|
40
|
+
if (d.oldDoc && aggId !== d.oldDoc[aggregateRelationField].id) {
|
|
41
|
+
throw new Error(`Field ${entityName}.${aggregateRelationField}.id cannot be changed`);
|
|
40
42
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
id_in: docs.map(d => d.doc.id)
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}, ctx);
|
|
51
|
-
const existingEventsByAggId = _lodash.default.groupBy(existingEvents, ev => ev[aggregateRelationField].id);
|
|
52
|
-
const existingAggregates = await context.drivers[entityName].getAll({
|
|
53
|
-
where: {
|
|
43
|
+
if (!docsByAggId[aggId]) docsByAggId[aggId] = [];
|
|
44
|
+
docsByAggId[aggId].push(d.doc);
|
|
45
|
+
}
|
|
46
|
+
const existingEvents = await context.drivers[eventEntityName].getAll({
|
|
47
|
+
where: {
|
|
48
|
+
[aggregateRelationField]: {
|
|
54
49
|
id_in: _lodash.default.keys(docsByAggId)
|
|
50
|
+
},
|
|
51
|
+
_not: {
|
|
52
|
+
id_in: docs.map(d => d.doc.id)
|
|
55
53
|
}
|
|
56
|
-
}, ctx);
|
|
57
|
-
const existingAggregatesById = _lodash.default.keyBy(existingAggregates, "id");
|
|
58
|
-
const result = [];
|
|
59
|
-
for (const aggId in docsByAggId) {
|
|
60
|
-
const events = _lodash.default.orderBy([...docsByAggId[aggId], ...(existingEventsByAggId[aggId] || [])], ["date"], "asc");
|
|
61
|
-
if (events[0].type !== "creation") throw new Error(`First event must have type = "creation". (type: ${events[0].type}, id: ${events[0].id})`);
|
|
62
|
-
if (events.slice(1).some(ev => ev.type === "creation")) {
|
|
63
|
-
throw new Error(`Only first event may have type = "creation"`);
|
|
64
|
-
}
|
|
65
|
-
let aggDoc;
|
|
66
|
-
for (const ev of events) {
|
|
67
|
-
const newAggDoc = context.validators[entityName]((0, _radsDb.merge)(aggDoc || {
|
|
68
|
-
id: aggId
|
|
69
|
-
}, ev.change));
|
|
70
|
-
ev.beforeChange = aggDoc;
|
|
71
|
-
const originalChange = ev.change;
|
|
72
|
-
ev.change = (0, _radsDb.diff)(newAggDoc, aggDoc);
|
|
73
|
-
keepHistory(schema[entityName].keepHistoryFields, originalChange, ev);
|
|
74
|
-
aggDoc = newAggDoc;
|
|
75
|
-
if (!context.options.keepNulls) (0, _radsDb.cleanUndefinedAndNull)(aggDoc);
|
|
76
|
-
}
|
|
77
|
-
result.push({
|
|
78
|
-
aggDoc,
|
|
79
|
-
events
|
|
80
|
-
});
|
|
81
54
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
55
|
+
}, ctx);
|
|
56
|
+
const existingEventsByAggId = _lodash.default.groupBy(existingEvents, ev => ev[aggregateRelationField].id);
|
|
57
|
+
const existingAggregates = await context.drivers[entityName].getAll({
|
|
58
|
+
where: {
|
|
59
|
+
id_in: _lodash.default.keys(docsByAggId)
|
|
60
|
+
}
|
|
61
|
+
}, ctx);
|
|
62
|
+
const existingAggregatesById = _lodash.default.keyBy(existingAggregates, "id");
|
|
63
|
+
const result = [];
|
|
64
|
+
for (const aggId in docsByAggId) {
|
|
65
|
+
const events = _lodash.default.orderBy([...docsByAggId[aggId], ...(existingEventsByAggId[aggId] || [])], ["date"], "asc");
|
|
66
|
+
if (events[0].type !== "creation") throw new Error(`First event must have type = "creation". (type: ${events[0].type}, id: ${events[0].id})`);
|
|
67
|
+
if (events.slice(1).some(ev => ev.type === "creation")) {
|
|
68
|
+
throw new Error(`Only first event may have type = "creation"`);
|
|
69
|
+
}
|
|
70
|
+
let aggDoc;
|
|
71
|
+
for (const ev of events) {
|
|
72
|
+
const newAggDoc = context.validators[entityName]((0, _radsDb.merge)(aggDoc || {
|
|
73
|
+
id: aggId
|
|
74
|
+
}, ev.change));
|
|
75
|
+
ev.beforeChange = aggDoc;
|
|
76
|
+
const originalChange = ev.change;
|
|
77
|
+
ev.change = (0, _radsDb.diff)(newAggDoc, aggDoc);
|
|
78
|
+
handleKeepHistory(schema[entityName].keepHistoryFields, originalChange, ev);
|
|
79
|
+
aggDoc = newAggDoc;
|
|
80
|
+
if (!context.options.keepNulls) (0, _radsDb.cleanUndefinedAndNull)(aggDoc);
|
|
81
|
+
}
|
|
82
|
+
result.push({
|
|
83
|
+
aggDoc,
|
|
84
|
+
events,
|
|
85
|
+
oldAggDoc: existingAggregatesById[aggDoc.id]
|
|
86
|
+
});
|
|
97
87
|
}
|
|
98
|
-
|
|
99
|
-
|
|
88
|
+
await (0, _radsDb.handlePrecomputed)(
|
|
89
|
+
// @ts-expect-error TODO wrong types
|
|
90
|
+
{
|
|
91
|
+
...context,
|
|
92
|
+
typeName: entityName
|
|
93
|
+
}, result.map(v => ({
|
|
94
|
+
doc: v.aggDoc,
|
|
95
|
+
events: v.events,
|
|
96
|
+
oldDoc: v.oldAggDoc
|
|
97
|
+
})), ctx);
|
|
98
|
+
return result;
|
|
99
|
+
},
|
|
100
|
+
async afterPut(context, docs, beforePutResult, ctx) {
|
|
101
|
+
const aggs = beforePutResult.map(d => d.aggDoc);
|
|
102
|
+
const hookItems = beforePutResult.map(v => ({
|
|
103
|
+
doc: v.aggDoc,
|
|
104
|
+
events: v.events,
|
|
105
|
+
oldDoc: v.oldAggDoc
|
|
106
|
+
}));
|
|
107
|
+
const aggregateEffectContext = {
|
|
108
|
+
...context,
|
|
109
|
+
typeName: entityName,
|
|
110
|
+
handle: schema[entityName].handle
|
|
111
|
+
};
|
|
112
|
+
await beforePut(hookItems, ctx, aggregateEffectContext);
|
|
113
|
+
await context.drivers[entityName].putMany(aggs, ctx);
|
|
114
|
+
await afterPut(hookItems, ctx, aggregateEffectContext);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
100
117
|
}
|
|
101
|
-
function
|
|
118
|
+
function handleKeepHistory(keepHistoryFields, originalChange, changeEvent) {
|
|
102
119
|
if (keepHistoryFields && originalChange) {
|
|
103
120
|
keepHistoryFields.forEach(prop => {
|
|
104
121
|
if (!changeEvent.change[prop] && originalChange[prop] !== void 0) {
|
|
@@ -163,4 +180,14 @@ function validateSchemaField(fieldName, eventEntityName, expectedFields, schema)
|
|
|
163
180
|
const expectedRequired = expectedField.isRequired ? `required` : "optional";
|
|
164
181
|
throw new Error(`"${eventEntityName}.${fieldName}" must be ${expectedRequired}`);
|
|
165
182
|
}
|
|
183
|
+
}
|
|
184
|
+
async function beforePut(items, ctx, computedContext) {
|
|
185
|
+
for (const f of computedContext.options.features) {
|
|
186
|
+
await f.beforePut?.(items, ctx, computedContext);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
async function afterPut(items, ctx, computedContext) {
|
|
190
|
+
for (const f of computedContext.options.features) {
|
|
191
|
+
await f.afterPut?.(items, ctx, computedContext);
|
|
192
|
+
}
|
|
166
193
|
}
|
|
@@ -17,76 +17,84 @@ function verifyEventSourcingSetup(schema, effects) {
|
|
|
17
17
|
const eventEntityName = `${entityName}Event`;
|
|
18
18
|
const aggregateRelationField = _.lowerFirst(entityName);
|
|
19
19
|
validateEventSourcingSetup(schema, entityName, eventEntityName, aggregateRelationField);
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
20
|
+
const effect = getEffectFor(entityName, aggregateRelationField, eventEntityName, schema);
|
|
21
|
+
effects[eventEntityName].push(effect);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function getEffectFor(entityName, aggregateRelationField, eventEntityName, schema) {
|
|
25
|
+
return {
|
|
26
|
+
async beforePut(context, docs, ctx) {
|
|
27
|
+
const docsByAggId = {};
|
|
28
|
+
const docsById = {};
|
|
29
|
+
for (const d of docs) {
|
|
30
|
+
if (!d.doc.id || docsById[d.doc.id])
|
|
31
|
+
continue;
|
|
32
|
+
docsById[d.doc.id] = d.doc;
|
|
33
|
+
const aggId = d.doc[aggregateRelationField].id;
|
|
34
|
+
if (!aggId)
|
|
35
|
+
throw new Error(`Missing ${entityName}.${aggregateRelationField}.id`);
|
|
36
|
+
if (d.oldDoc && aggId !== d.oldDoc[aggregateRelationField].id) {
|
|
37
|
+
throw new Error(`Field ${entityName}.${aggregateRelationField}.id cannot be changed`);
|
|
37
38
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const existingEventsByAggId = _.groupBy(existingEvents, (ev) => ev[aggregateRelationField].id);
|
|
48
|
-
const existingAggregates = await context.drivers[entityName].getAll(
|
|
49
|
-
{ where: { id_in: _.keys(docsByAggId) } },
|
|
50
|
-
ctx
|
|
51
|
-
);
|
|
52
|
-
const existingAggregatesById = _.keyBy(existingAggregates, "id");
|
|
53
|
-
const result = [];
|
|
54
|
-
for (const aggId in docsByAggId) {
|
|
55
|
-
const events = _.orderBy([...docsByAggId[aggId], ...existingEventsByAggId[aggId] || []], ["date"], "asc");
|
|
56
|
-
if (events[0].type !== "creation")
|
|
57
|
-
throw new Error(`First event must have type = "creation". (type: ${events[0].type}, id: ${events[0].id})`);
|
|
58
|
-
if (events.slice(1).some((ev) => ev.type === "creation")) {
|
|
59
|
-
throw new Error(`Only first event may have type = "creation"`);
|
|
60
|
-
}
|
|
61
|
-
let aggDoc;
|
|
62
|
-
for (const ev of events) {
|
|
63
|
-
const newAggDoc = context.validators[entityName](merge(aggDoc || { id: aggId }, ev.change));
|
|
64
|
-
ev.beforeChange = aggDoc;
|
|
65
|
-
const originalChange = ev.change;
|
|
66
|
-
ev.change = diff(newAggDoc, aggDoc);
|
|
67
|
-
keepHistory(schema[entityName].keepHistoryFields, originalChange, ev);
|
|
68
|
-
aggDoc = newAggDoc;
|
|
69
|
-
if (!context.options.keepNulls)
|
|
70
|
-
cleanUndefinedAndNull(aggDoc);
|
|
39
|
+
if (!docsByAggId[aggId])
|
|
40
|
+
docsByAggId[aggId] = [];
|
|
41
|
+
docsByAggId[aggId].push(d.doc);
|
|
42
|
+
}
|
|
43
|
+
const existingEvents = await context.drivers[eventEntityName].getAll(
|
|
44
|
+
{
|
|
45
|
+
where: {
|
|
46
|
+
[aggregateRelationField]: { id_in: _.keys(docsByAggId) },
|
|
47
|
+
_not: { id_in: docs.map((d) => d.doc.id) }
|
|
71
48
|
}
|
|
72
|
-
|
|
49
|
+
},
|
|
50
|
+
ctx
|
|
51
|
+
);
|
|
52
|
+
const existingEventsByAggId = _.groupBy(existingEvents, (ev) => ev[aggregateRelationField].id);
|
|
53
|
+
const existingAggregates = await context.drivers[entityName].getAll(
|
|
54
|
+
{ where: { id_in: _.keys(docsByAggId) } },
|
|
55
|
+
ctx
|
|
56
|
+
);
|
|
57
|
+
const existingAggregatesById = _.keyBy(existingAggregates, "id");
|
|
58
|
+
const result = [];
|
|
59
|
+
for (const aggId in docsByAggId) {
|
|
60
|
+
const events = _.orderBy([...docsByAggId[aggId], ...existingEventsByAggId[aggId] || []], ["date"], "asc");
|
|
61
|
+
if (events[0].type !== "creation")
|
|
62
|
+
throw new Error(`First event must have type = "creation". (type: ${events[0].type}, id: ${events[0].id})`);
|
|
63
|
+
if (events.slice(1).some((ev) => ev.type === "creation")) {
|
|
64
|
+
throw new Error(`Only first event may have type = "creation"`);
|
|
73
65
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
66
|
+
let aggDoc;
|
|
67
|
+
for (const ev of events) {
|
|
68
|
+
const newAggDoc = context.validators[entityName](merge(aggDoc || { id: aggId }, ev.change));
|
|
69
|
+
ev.beforeChange = aggDoc;
|
|
70
|
+
const originalChange = ev.change;
|
|
71
|
+
ev.change = diff(newAggDoc, aggDoc);
|
|
72
|
+
handleKeepHistory(schema[entityName].keepHistoryFields, originalChange, ev);
|
|
73
|
+
aggDoc = newAggDoc;
|
|
74
|
+
if (!context.options.keepNulls)
|
|
75
|
+
cleanUndefinedAndNull(aggDoc);
|
|
76
|
+
}
|
|
77
|
+
result.push({ aggDoc, events, oldAggDoc: existingAggregatesById[aggDoc.id] });
|
|
85
78
|
}
|
|
86
|
-
|
|
87
|
-
|
|
79
|
+
await handlePrecomputed(
|
|
80
|
+
// @ts-expect-error TODO wrong types
|
|
81
|
+
{ ...context, typeName: entityName },
|
|
82
|
+
result.map((v) => ({ doc: v.aggDoc, events: v.events, oldDoc: v.oldAggDoc })),
|
|
83
|
+
ctx
|
|
84
|
+
);
|
|
85
|
+
return result;
|
|
86
|
+
},
|
|
87
|
+
async afterPut(context, docs, beforePutResult, ctx) {
|
|
88
|
+
const aggs = beforePutResult.map((d) => d.aggDoc);
|
|
89
|
+
const hookItems = beforePutResult.map((v) => ({ doc: v.aggDoc, events: v.events, oldDoc: v.oldAggDoc }));
|
|
90
|
+
const aggregateEffectContext = { ...context, typeName: entityName, handle: schema[entityName].handle };
|
|
91
|
+
await beforePut(hookItems, ctx, aggregateEffectContext);
|
|
92
|
+
await context.drivers[entityName].putMany(aggs, ctx);
|
|
93
|
+
await afterPut(hookItems, ctx, aggregateEffectContext);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
88
96
|
}
|
|
89
|
-
function
|
|
97
|
+
function handleKeepHistory(keepHistoryFields, originalChange, changeEvent) {
|
|
90
98
|
if (keepHistoryFields && originalChange) {
|
|
91
99
|
keepHistoryFields.forEach((prop) => {
|
|
92
100
|
if (!changeEvent.change[prop] && originalChange[prop] !== void 0) {
|
|
@@ -141,3 +149,13 @@ function validateSchemaField(fieldName, eventEntityName, expectedFields, schema)
|
|
|
141
149
|
throw new Error(`"${eventEntityName}.${fieldName}" must be ${expectedRequired}`);
|
|
142
150
|
}
|
|
143
151
|
}
|
|
152
|
+
async function beforePut(items, ctx, computedContext) {
|
|
153
|
+
for (const f of computedContext.options.features) {
|
|
154
|
+
await f.beforePut?.(items, ctx, computedContext);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async function afterPut(items, ctx, computedContext) {
|
|
158
|
+
for (const f of computedContext.options.features) {
|
|
159
|
+
await f.afterPut?.(items, ctx, computedContext);
|
|
160
|
+
}
|
|
161
|
+
}
|