@lazyapps/readmodels 0.1.0

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 ADDED
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2026, Oliver Sturm
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 WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # @lazyapps/readmodels
2
+
3
+ Event projection and query resolution engine. Core of the read side in the LazyApps CQRS architecture, projecting events into queryable read models.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @lazyapps/readmodels
9
+ ```
10
+
11
+ ## Part of LazyApps
12
+
13
+ This package is part of the [LazyApps](https://github.com/oliversturm/lazyapps-libs) event-sourcing and CQRS framework.
@@ -0,0 +1,60 @@
1
+ import Queue from 'promise-queue';
2
+
3
+ import { getLogger } from '@lazyapps/logger';
4
+
5
+ const createChangeNotificationHandler =
6
+ (changeNotificationSender) => (correlationId) => {
7
+ const queue = new Queue(1, Infinity);
8
+
9
+ const log = getLogger('RM/Chng', correlationId);
10
+
11
+ return {
12
+ sendChangeNotification: (changeInfo) =>
13
+ queue.add(() =>
14
+ new Promise((resolve) => {
15
+ log.debug(
16
+ `Sending change notification ${JSON.stringify(changeInfo)}'`,
17
+ );
18
+ resolve();
19
+ })
20
+ .then(
21
+ changeNotificationSender.sendChangeNotification(
22
+ correlationId,
23
+ changeInfo,
24
+ ),
25
+ )
26
+ .catch((err) => {
27
+ log.error(
28
+ `Can't send change notification ${JSON.stringify(
29
+ changeInfo,
30
+ )}: ${err}`,
31
+ );
32
+ }),
33
+ ),
34
+ createChangeInfo: (
35
+ endpointName,
36
+ readModelName,
37
+ resolverName,
38
+ changeKind,
39
+ details,
40
+ ) => {
41
+ // I used to check these kinds specifically, but that seems
42
+ // counter-productive. These kinds might be handled on a
43
+ // client using standard mechanisms, but it's perfectly fine
44
+ // to pass any custom name as the changeKind and handle
45
+ // the details field accordingly.
46
+ //
47
+ // if (!['all', 'addRow', 'updateRow', 'deleteRow'].includes(changeKind))
48
+ // throw new Error(`Invalid changeKind ${changeKind}`);
49
+ return {
50
+ endpointName,
51
+ readModelName,
52
+ resolverName,
53
+ changeKind,
54
+ details,
55
+ };
56
+ },
57
+ };
58
+ };
59
+
60
+ export { createChangeNotificationHandler };
package/commands.js ADDED
@@ -0,0 +1,21 @@
1
+ import { getLogger } from '@lazyapps/logger';
2
+
3
+ const createCommandHandler =
4
+ ({ commandSender }) =>
5
+ (correlationId) => {
6
+ const log = getLogger('RM/Cmd', correlationId);
7
+ return {
8
+ // accept the command parameters and then return a promise generator
9
+ // which will be evaluated lazily depending on replay state
10
+ execute: (cmd) => () =>
11
+ new Promise((resolve) => {
12
+ log.debug(`Executing command ${JSON.stringify(cmd)}`);
13
+ resolve();
14
+ })
15
+ .then(() => commandSender.sendCommand(correlationId, cmd))
16
+ .catch((err) => {
17
+ log.error(`Can't execute command ${JSON.stringify(cmd)}: ${err}`);
18
+ }),
19
+ };
20
+ };
21
+ export { createCommandHandler };
package/context.js ADDED
@@ -0,0 +1,37 @@
1
+ import { createProjectionHandler } from './projections.js';
2
+ import { createSideEffectsHandler } from './sideEffects.js';
3
+ import { createChangeNotificationHandler } from './changeNotification.js';
4
+ import { createCommandHandler } from './commands.js';
5
+
6
+ export const initializeContext = (
7
+ correlationConfig,
8
+ { readModels, storage, eventBus, changeNotificationSender, commandSender },
9
+ ) =>
10
+ storage()
11
+ .then((storage) => ({ storage, readModels, correlationConfig }))
12
+ .then((context) =>
13
+ context.storage
14
+ .readLastProjectedEventTimestamps(readModels)
15
+ .then(() => context),
16
+ )
17
+ .then((context) => ({
18
+ ...context,
19
+ commands: createCommandHandler({ commandSender }),
20
+ }))
21
+ .then((context) =>
22
+ createSideEffectsHandler().then((sideEffects) => ({
23
+ ...context,
24
+ sideEffects,
25
+ })),
26
+ )
27
+ .then((context) => ({
28
+ ...context,
29
+ changeNotification: createChangeNotificationHandler(
30
+ changeNotificationSender,
31
+ ),
32
+ }))
33
+ .then((context) => ({
34
+ ...context,
35
+ projectionHandler: createProjectionHandler(context),
36
+ }))
37
+ .then((context) => eventBus(context).then(() => context));
package/index.js ADDED
@@ -0,0 +1,6 @@
1
+ import { initializeContext } from './context.js';
2
+
3
+ export const startReadModels = (correlationConfig, config) =>
4
+ initializeContext(correlationConfig, config).then((context) =>
5
+ config.listener(context),
6
+ );
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@lazyapps/readmodels",
3
+ "version": "0.1.0",
4
+ "description": "Event projection and query resolution engine for LazyApps",
5
+ "main": "index.js",
6
+ "exports": {
7
+ ".": "./index.js"
8
+ },
9
+ "files": [
10
+ "*.js"
11
+ ],
12
+ "keywords": [
13
+ "event-sourcing",
14
+ "cqrs",
15
+ "lazyapps",
16
+ "read-models",
17
+ "projections"
18
+ ],
19
+ "author": "Oliver Sturm",
20
+ "license": "ISC",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/oliversturm/lazyapps-libs.git",
24
+ "directory": "packages/readmodels"
25
+ },
26
+ "bugs": {
27
+ "url": "https://github.com/oliversturm/lazyapps-libs/issues"
28
+ },
29
+ "homepage": "https://github.com/oliversturm/lazyapps-libs/tree/main/packages/readmodels#readme",
30
+ "engines": {
31
+ "node": ">=18.20.3 || >=20.18.0"
32
+ },
33
+ "devDependencies": {
34
+ "eslint": "^8.46.0",
35
+ "vitest": "^4.0.18"
36
+ },
37
+ "dependencies": {
38
+ "lodash": "^4.17.21",
39
+ "promise-queue": "^2.2.5",
40
+ "@lazyapps/logger": "^0.1.0"
41
+ },
42
+ "type": "module",
43
+ "scripts": {
44
+ "test": "vitest"
45
+ }
46
+ }
package/projections.js ADDED
@@ -0,0 +1,113 @@
1
+ import Queue from 'promise-queue';
2
+ import { getLogger } from '@lazyapps/logger';
3
+
4
+ const collectProjections = (readModels, event) =>
5
+ Promise.resolve(
6
+ Object.keys(readModels)
7
+ .map((rmName) => {
8
+ const rm = readModels[rmName];
9
+ const projection = rm.projections && rm.projections[event.type];
10
+ if (projection) {
11
+ return [rmName, projection];
12
+ } else return null;
13
+ })
14
+ .filter((el) => !!el)
15
+ .filter(([, f]) => !!f),
16
+ );
17
+
18
+ const logProjections = (log, inReplay) => (rmProjections) => {
19
+ if (rmProjections.length)
20
+ log.debug(
21
+ `Projecting event for read models: ${JSON.stringify(
22
+ rmProjections.map(([rmName]) => rmName),
23
+ )} (inReplay=${inReplay})`,
24
+ );
25
+ return rmProjections;
26
+ };
27
+
28
+ const updateInternalReadModelTimestamps =
29
+ (event, readModels) => (rmProjections) =>
30
+ Promise.all(
31
+ rmProjections.map(([rmName]) => {
32
+ readModels[rmName].lastProjectedEventTimestamp = event.timestamp;
33
+ }),
34
+ ).then(() => rmProjections);
35
+
36
+ const updateTimestamp = (correlationId, storage, rmName, timestamp) =>
37
+ storage.updateLastProjectedEventTimestamps(
38
+ correlationId,
39
+ [rmName],
40
+ timestamp,
41
+ );
42
+
43
+ const handleProjections =
44
+ (correlationId, log, context, getProjectionContext, inReplay, event) =>
45
+ (rmProjections) =>
46
+ Promise.all(
47
+ rmProjections.map(([rmName, f]) =>
48
+ f(getProjectionContext(correlationId)(rmName)(inReplay), event)
49
+ .then(() =>
50
+ updateTimestamp(
51
+ correlationId,
52
+ context.storage,
53
+ rmName,
54
+ event.timestamp,
55
+ ),
56
+ )
57
+ .catch((err) => {
58
+ log.error(
59
+ `Error occurred projecting event ${JSON.stringify(
60
+ event,
61
+ )} for read model ${rmName}, or during read model timestamp update: ${err}`,
62
+ );
63
+ }),
64
+ ),
65
+ );
66
+
67
+ const projectEvent =
68
+ (context, eventQueue, getProjectionContext) => (correlationId) => {
69
+ const log = getLogger(`RM/ProjEv`, correlationId);
70
+ return (event, inReplay) =>
71
+ eventQueue.add(() =>
72
+ collectProjections(context.readModels, event)
73
+ .then(logProjections(log, inReplay))
74
+ .then(updateInternalReadModelTimestamps(event, context.readModels))
75
+ .then(
76
+ handleProjections(
77
+ correlationId,
78
+ log,
79
+ context,
80
+ getProjectionContext,
81
+ inReplay,
82
+ event,
83
+ ),
84
+ ),
85
+ );
86
+ };
87
+
88
+ export const createProjectionHandler = (context) => {
89
+ const eventQueue = new Queue(1, Infinity);
90
+ const getProjectionContext = (correlationId) => (rmName) => (inReplay) => ({
91
+ storage: context.storage.perRequest(correlationId),
92
+ commands: context.commands(correlationId),
93
+ changeNotification: context.changeNotification(correlationId),
94
+ log: getLogger(`RM/${rmName}`, correlationId),
95
+ sideEffects: context.sideEffects.getSideEffectsHandler(
96
+ correlationId,
97
+ inReplay,
98
+ ),
99
+ });
100
+
101
+ return {
102
+ projectEvent: projectEvent(context, eventQueue, getProjectionContext),
103
+ };
104
+ };
105
+
106
+ export const testing = {
107
+ collectProjections,
108
+ logProjections,
109
+ updateInternalReadModelTimestamps,
110
+ updateTimestamp,
111
+ handleProjections,
112
+ projectEvent,
113
+ };
package/sideEffects.js ADDED
@@ -0,0 +1,53 @@
1
+ import Queue from 'promise-queue';
2
+ import { getLogger } from '@lazyapps/logger';
3
+
4
+ const createSideEffectsHandler = () => {
5
+ const queue = new Queue(1, Infinity);
6
+
7
+ const schedule =
8
+ (correlationId, inReplay) =>
9
+ (promiseGenerator, options = {}) => {
10
+ const defaultOptions = { name: 'unnamed', execution: 'liveOnly' };
11
+ const actualOptions = { ...defaultOptions, ...options };
12
+
13
+ const log = getLogger('ReadMod/Side', correlationId);
14
+ return (inReplay &&
15
+ ['replayOnly', 'always'].includes(actualOptions.execution)) ||
16
+ (!inReplay && ['liveOnly', 'always'].includes(actualOptions.execution))
17
+ ? queue.add(() =>
18
+ new Promise((resolve) => {
19
+ log.debug(
20
+ `Running side-effect '${actualOptions.name}' (inReplay=${inReplay}, execution=${actualOptions.execution})`,
21
+ );
22
+ resolve();
23
+ })
24
+ .then(promiseGenerator())
25
+ .then(
26
+ () =>
27
+ new Promise((resolve) => {
28
+ log.debug(`Side-effect '${actualOptions.name}' done`);
29
+ resolve();
30
+ }),
31
+ )
32
+ .catch((err) => {
33
+ log.error(
34
+ `Can't execute side-effect '${actualOptions.name}': ${err}`,
35
+ );
36
+ }),
37
+ )
38
+ : new Promise((resolve) => {
39
+ log.debug(
40
+ `Skipping side-effect '${actualOptions.name} (inReplay=${inReplay}, execution=${actualOptions.execution})`,
41
+ );
42
+ resolve();
43
+ });
44
+ };
45
+
46
+ return Promise.resolve({
47
+ getSideEffectsHandler: (correlationId, inReplay) => ({
48
+ schedule: schedule(correlationId, inReplay),
49
+ }),
50
+ });
51
+ };
52
+
53
+ export { createSideEffectsHandler };
package/validation.js ADDED
@@ -0,0 +1,13 @@
1
+ export class ValidationError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = this.constructor.name;
5
+ }
6
+ }
7
+
8
+ export class AuthorizationError extends Error {
9
+ constructor(message) {
10
+ super(message);
11
+ this.name = this.constructor.name;
12
+ }
13
+ }