rads-db 3.0.37 → 3.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.
@@ -23,82 +23,94 @@ 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
- effects[eventEntityName].push({
27
- async beforePut(context, docs, ctx) {
28
- const docsByAggId = {};
29
- const docsById = {};
30
- for (const d of docs) {
31
- if (!d.doc.id || docsById[d.doc.id]) continue;
32
- docsById[d.doc.id] = d.doc;
33
- const aggId = d.doc[aggregateRelationField].id;
34
- if (!aggId) throw new Error(`Missing ${entityName}.${aggregateRelationField}.id`);
35
- if (d.oldDoc && aggId !== d.oldDoc[aggregateRelationField].id) {
36
- throw new Error(`Field ${entityName}.${aggregateRelationField}.id cannot be changed`);
37
- }
38
- if (!docsByAggId[aggId]) docsByAggId[aggId] = [];
39
- docsByAggId[aggId].push(d.doc);
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
- const existingEvents = await context.drivers[eventEntityName].getAll({
42
- where: {
43
- [aggregateRelationField]: {
44
- id_in: _lodash.default.keys(docsByAggId)
45
- },
46
- _not: {
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
- await (0, _radsDb.handlePrecomputed)(
83
- // @ts-expect-error TODO wrong types
84
- {
85
- ...context,
86
- typeName: entityName
87
- }, result.map(v => ({
88
- doc: v.aggDoc,
89
- events: v.events,
90
- oldDoc: existingAggregatesById[v.aggDoc.id]
91
- })), ctx);
92
- return result;
93
- },
94
- async afterPut(context, docs, beforePutResult, ctx) {
95
- const aggs = beforePutResult.map(d => d.aggDoc);
96
- await context.drivers[entityName].putMany(aggs, ctx);
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
+ await beforePut(hookItems, ctx, context);
108
+ await context.drivers[entityName].putMany(aggs, ctx);
109
+ await afterPut(hookItems, ctx, context);
110
+ }
111
+ };
100
112
  }
101
- function keepHistory(keepHistoryFields, originalChange, changeEvent) {
113
+ function handleKeepHistory(keepHistoryFields, originalChange, changeEvent) {
102
114
  if (keepHistoryFields && originalChange) {
103
115
  keepHistoryFields.forEach(prop => {
104
116
  if (!changeEvent.change[prop] && originalChange[prop] !== void 0) {
@@ -163,4 +175,14 @@ function validateSchemaField(fieldName, eventEntityName, expectedFields, schema)
163
175
  const expectedRequired = expectedField.isRequired ? `required` : "optional";
164
176
  throw new Error(`"${eventEntityName}.${fieldName}" must be ${expectedRequired}`);
165
177
  }
178
+ }
179
+ async function beforePut(items, ctx, computedContext) {
180
+ for (const f of computedContext.options.features) {
181
+ await f.beforePut?.(items, ctx, computedContext);
182
+ }
183
+ }
184
+ async function afterPut(items, ctx, computedContext) {
185
+ for (const f of computedContext.options.features) {
186
+ await f.afterPut?.(items, ctx, computedContext);
187
+ }
166
188
  }
@@ -17,76 +17,83 @@ function verifyEventSourcingSetup(schema, effects) {
17
17
  const eventEntityName = `${entityName}Event`;
18
18
  const aggregateRelationField = _.lowerFirst(entityName);
19
19
  validateEventSourcingSetup(schema, entityName, eventEntityName, aggregateRelationField);
20
- effects[eventEntityName].push({
21
- async beforePut(context, docs, ctx) {
22
- const docsByAggId = {};
23
- const docsById = {};
24
- for (const d of docs) {
25
- if (!d.doc.id || docsById[d.doc.id])
26
- continue;
27
- docsById[d.doc.id] = d.doc;
28
- const aggId = d.doc[aggregateRelationField].id;
29
- if (!aggId)
30
- throw new Error(`Missing ${entityName}.${aggregateRelationField}.id`);
31
- if (d.oldDoc && aggId !== d.oldDoc[aggregateRelationField].id) {
32
- throw new Error(`Field ${entityName}.${aggregateRelationField}.id cannot be changed`);
33
- }
34
- if (!docsByAggId[aggId])
35
- docsByAggId[aggId] = [];
36
- docsByAggId[aggId].push(d.doc);
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
- const existingEvents = await context.drivers[eventEntityName].getAll(
39
- {
40
- where: {
41
- [aggregateRelationField]: { id_in: _.keys(docsByAggId) },
42
- _not: { id_in: docs.map((d) => d.doc.id) }
43
- }
44
- },
45
- ctx
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
- result.push({ aggDoc, events });
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
- await handlePrecomputed(
75
- // @ts-expect-error TODO wrong types
76
- { ...context, typeName: entityName },
77
- result.map((v) => ({ doc: v.aggDoc, events: v.events, oldDoc: existingAggregatesById[v.aggDoc.id] })),
78
- ctx
79
- );
80
- return result;
81
- },
82
- async afterPut(context, docs, beforePutResult, ctx) {
83
- const aggs = beforePutResult.map((d) => d.aggDoc);
84
- await context.drivers[entityName].putMany(aggs, ctx);
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
+ await beforePut(hookItems, ctx, context);
91
+ await context.drivers[entityName].putMany(aggs, ctx);
92
+ await afterPut(hookItems, ctx, context);
93
+ }
94
+ };
88
95
  }
89
- function keepHistory(keepHistoryFields, originalChange, changeEvent) {
96
+ function handleKeepHistory(keepHistoryFields, originalChange, changeEvent) {
90
97
  if (keepHistoryFields && originalChange) {
91
98
  keepHistoryFields.forEach((prop) => {
92
99
  if (!changeEvent.change[prop] && originalChange[prop] !== void 0) {
@@ -141,3 +148,13 @@ function validateSchemaField(fieldName, eventEntityName, expectedFields, schema)
141
148
  throw new Error(`"${eventEntityName}.${fieldName}" must be ${expectedRequired}`);
142
149
  }
143
150
  }
151
+ async function beforePut(items, ctx, computedContext) {
152
+ for (const f of computedContext.options.features) {
153
+ await f.beforePut?.(items, ctx, computedContext);
154
+ }
155
+ }
156
+ async function afterPut(items, ctx, computedContext) {
157
+ for (const f of computedContext.options.features) {
158
+ await f.afterPut?.(items, ctx, computedContext);
159
+ }
160
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rads-db",
3
- "version": "3.0.37",
3
+ "version": "3.0.38",
4
4
  "files": [
5
5
  "dist",
6
6
  "drivers",