@lazyapps/aggregatestore-inmemory 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 +15 -0
- package/README.md +13 -0
- package/index.js +64 -0
- package/package.json +43 -0
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/aggregatestore-inmemory
|
|
2
|
+
|
|
3
|
+
In-memory aggregate state cache for the LazyApps command processing pipeline. Caches aggregate state to avoid replaying all events on every command.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @lazyapps/aggregatestore-inmemory
|
|
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.
|
package/index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { getLogger } from '@lazyapps/logger';
|
|
2
|
+
|
|
3
|
+
const initLogger = getLogger('CmdProc/AS', 'INIT');
|
|
4
|
+
|
|
5
|
+
export const inmemory = () => (aggregates) => {
|
|
6
|
+
const store = {};
|
|
7
|
+
let lastProjectedEventTimestamp = 0;
|
|
8
|
+
let inReplay = false;
|
|
9
|
+
|
|
10
|
+
const getAggregateState = (name, id) =>
|
|
11
|
+
(store[name] && store[name][id]) || aggregates[name].initial();
|
|
12
|
+
|
|
13
|
+
const setAggregateState = (name, id, state) => {
|
|
14
|
+
if (!store[name]) store[name] = {};
|
|
15
|
+
store[name][id] = state;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const applyAggregateProjection = (correlationId) => (event) => {
|
|
19
|
+
const { aggregateName, aggregateId, type, timestamp } = event;
|
|
20
|
+
const projection =
|
|
21
|
+
aggregates[aggregateName].projections &&
|
|
22
|
+
aggregates[aggregateName].projections[type];
|
|
23
|
+
const log = getLogger('CmdProc/AS', correlationId);
|
|
24
|
+
if (projection) {
|
|
25
|
+
const state = getAggregateState(aggregateName, aggregateId);
|
|
26
|
+
const projected = projection(state, event);
|
|
27
|
+
setAggregateState(aggregateName, aggregateId, projected);
|
|
28
|
+
log.debug(
|
|
29
|
+
`Applied aggregate projection for event timestamp ${timestamp}`,
|
|
30
|
+
);
|
|
31
|
+
} else {
|
|
32
|
+
log.debug(
|
|
33
|
+
`No aggregate projection for type in event ${JSON.stringify(event)}`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (!inReplay && timestamp < lastProjectedEventTimestamp)
|
|
38
|
+
log.debug(
|
|
39
|
+
`Noticing event out of sequence (lastPET=${lastProjectedEventTimestamp}, ts=${timestamp}): ${JSON.stringify(
|
|
40
|
+
event,
|
|
41
|
+
)}`,
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
lastProjectedEventTimestamp = timestamp;
|
|
45
|
+
|
|
46
|
+
return event;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const startReplay = () => {
|
|
50
|
+
initLogger.debug('Starting replay state for aggregate store');
|
|
51
|
+
inReplay = true;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const endReplay = () => {
|
|
55
|
+
initLogger.debug('Ending replay state for aggregate store');
|
|
56
|
+
inReplay = false;
|
|
57
|
+
};
|
|
58
|
+
return {
|
|
59
|
+
getAggregateState,
|
|
60
|
+
applyAggregateProjection,
|
|
61
|
+
startReplay,
|
|
62
|
+
endReplay,
|
|
63
|
+
};
|
|
64
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lazyapps/aggregatestore-inmemory",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "In-memory aggregate state cache for the LazyApps command processing pipeline",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"index.js"
|
|
11
|
+
],
|
|
12
|
+
"keywords": [
|
|
13
|
+
"event-sourcing",
|
|
14
|
+
"cqrs",
|
|
15
|
+
"lazyapps",
|
|
16
|
+
"aggregate-store"
|
|
17
|
+
],
|
|
18
|
+
"author": "Oliver Sturm",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/oliversturm/lazyapps-libs.git",
|
|
23
|
+
"directory": "packages/aggregatestore-inmemory"
|
|
24
|
+
},
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/oliversturm/lazyapps-libs/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/oliversturm/lazyapps-libs/tree/main/packages/aggregatestore-inmemory#readme",
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=18.20.3 || >=20.18.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"eslint": "^8.46.0",
|
|
34
|
+
"vitest": "^4.0.18"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@lazyapps/logger": "^0.1.0"
|
|
38
|
+
},
|
|
39
|
+
"type": "module",
|
|
40
|
+
"scripts": {
|
|
41
|
+
"test": "vitest"
|
|
42
|
+
}
|
|
43
|
+
}
|