@lazyapps/eventstore-mongodb 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 +101 -0
- package/package.json +46 -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/eventstore-mongodb
|
|
2
|
+
|
|
3
|
+
MongoDB-backed persistent event store with replay capability. Stores domain events durably and supports event stream replay for aggregate reconstitution.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @lazyapps/eventstore-mongodb
|
|
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,101 @@
|
|
|
1
|
+
import { MongoClient } from 'mongodb';
|
|
2
|
+
import pRetry from 'p-retry';
|
|
3
|
+
|
|
4
|
+
import { getLogger } from '@lazyapps/logger';
|
|
5
|
+
|
|
6
|
+
const replay = (dbContext) => (correlationId) => (cmdProcContext) =>
|
|
7
|
+
Promise.all([
|
|
8
|
+
cmdProcContext.aggregateStore.startReplay(),
|
|
9
|
+
cmdProcContext.eventBus.publishReplayState(correlationId)(true),
|
|
10
|
+
])
|
|
11
|
+
.then(() =>
|
|
12
|
+
dbContext.collection
|
|
13
|
+
.find({}, { sort: { timestamp: 1 } })
|
|
14
|
+
.forEach((event) => {
|
|
15
|
+
if (event) {
|
|
16
|
+
return Promise.all([
|
|
17
|
+
cmdProcContext.aggregateStore.applyAggregateProjection(
|
|
18
|
+
correlationId,
|
|
19
|
+
)(event),
|
|
20
|
+
// Implementation of event replay concept in these samples is currently
|
|
21
|
+
// incomplete, so don't do it.
|
|
22
|
+
// cmdProcContext.eventBus.publishEvent(event),
|
|
23
|
+
]);
|
|
24
|
+
} else return false;
|
|
25
|
+
}),
|
|
26
|
+
)
|
|
27
|
+
.then(() =>
|
|
28
|
+
Promise.all([
|
|
29
|
+
cmdProcContext.aggregateStore.endReplay(),
|
|
30
|
+
cmdProcContext.eventBus.publishReplayState(correlationId)(false),
|
|
31
|
+
]),
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
export const mongodb =
|
|
35
|
+
({
|
|
36
|
+
url,
|
|
37
|
+
user,
|
|
38
|
+
pwd,
|
|
39
|
+
scheme,
|
|
40
|
+
host,
|
|
41
|
+
urlPath,
|
|
42
|
+
database = 'events',
|
|
43
|
+
collection = 'events',
|
|
44
|
+
} = {}) =>
|
|
45
|
+
() => {
|
|
46
|
+
const connectUrl = url
|
|
47
|
+
? url
|
|
48
|
+
: user && pwd && scheme && host
|
|
49
|
+
? (url = `${scheme}://${user}:${pwd}@${host}/${urlPath}`)
|
|
50
|
+
: 'mongodb://127.0.0.1:27017';
|
|
51
|
+
|
|
52
|
+
// Keep location separate for logging, so that the password
|
|
53
|
+
// doesn't get into the log.
|
|
54
|
+
const logLocation = user ? host : connectUrl;
|
|
55
|
+
|
|
56
|
+
const initLog = getLogger('CmdProc/ES', 'INIT');
|
|
57
|
+
|
|
58
|
+
return pRetry(
|
|
59
|
+
() =>
|
|
60
|
+
MongoClient.connect(connectUrl, {
|
|
61
|
+
useNewUrlParser: true,
|
|
62
|
+
useUnifiedTopology: true,
|
|
63
|
+
}),
|
|
64
|
+
{
|
|
65
|
+
onFailedAttempt: (error) => {
|
|
66
|
+
initLog.error(
|
|
67
|
+
`Attempt ${error.attemptNumber} failed connecting to MongoDB at ${logLocation}: '${error}'. Will retry another ${error.retriesLeft} times.`,
|
|
68
|
+
);
|
|
69
|
+
},
|
|
70
|
+
retries: 10,
|
|
71
|
+
},
|
|
72
|
+
)
|
|
73
|
+
.catch((err) => {
|
|
74
|
+
initLog.error(`Can't connect to MongoDB at ${logLocation}: ${err}`);
|
|
75
|
+
})
|
|
76
|
+
.then((client) => ({ client, db: client.db(database) }))
|
|
77
|
+
.then((dbContext) => ({
|
|
78
|
+
...dbContext,
|
|
79
|
+
collection: dbContext.db.collection(collection),
|
|
80
|
+
}))
|
|
81
|
+
.then((dbContext) => ({
|
|
82
|
+
addEvent: (correlationId) => (event) => {
|
|
83
|
+
const log = getLogger('ES/MongoDB', correlationId);
|
|
84
|
+
return dbContext.collection
|
|
85
|
+
.insertOne(event)
|
|
86
|
+
.catch((err) => {
|
|
87
|
+
log.error(`Can't insert event ${event}: ${err}`);
|
|
88
|
+
})
|
|
89
|
+
.then(() => {
|
|
90
|
+
// Questionable mongodb behavior: insertOne mutates
|
|
91
|
+
// the source object to add the mongodb _id property.
|
|
92
|
+
// We want to publish the event object later, so
|
|
93
|
+
// mongodb artefacts are not wanted here.
|
|
94
|
+
delete event._id;
|
|
95
|
+
return event;
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
close: () => dbContext.client.close(),
|
|
99
|
+
replay: replay(dbContext),
|
|
100
|
+
}));
|
|
101
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lazyapps/eventstore-mongodb",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "MongoDB-backed persistent event store with replay capability for LazyApps",
|
|
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
|
+
"event-store",
|
|
17
|
+
"mongodb"
|
|
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/eventstore-mongodb"
|
|
25
|
+
},
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/oliversturm/lazyapps-libs/issues"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/oliversturm/lazyapps-libs/tree/main/packages/eventstore-mongodb#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
|
+
"mongodb": "^5.7.0",
|
|
39
|
+
"p-retry": "^6.1.0",
|
|
40
|
+
"@lazyapps/logger": "^0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"type": "module",
|
|
43
|
+
"scripts": {
|
|
44
|
+
"test": "vitest"
|
|
45
|
+
}
|
|
46
|
+
}
|