@ossy/event-store 3.4.0 → 3.7.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/README.md +9 -2
- package/package.json +2 -2
- package/src/aggregate-rebuild.js +19 -12
- package/src/aggregate-rebuild.spec.js +43 -2
- package/src/ensure-indexes.js +6 -0
package/README.md
CHANGED
|
@@ -155,13 +155,20 @@ import { Mongo } from '@ossy/event-store'
|
|
|
155
155
|
await Mongo.connect(process.env.DB_URL)
|
|
156
156
|
```
|
|
157
157
|
|
|
158
|
-
Recommended indexes
|
|
158
|
+
Recommended indexes (created at startup by `ensureEventStoreIndexes`):
|
|
159
159
|
|
|
160
160
|
```js
|
|
161
|
+
// eventstore
|
|
161
162
|
{ resourceId: 1, version: 1 } // stream queries
|
|
162
|
-
|
|
163
|
+
|
|
164
|
+
// aggregates
|
|
165
|
+
{ id: 1 } // snapshot get-by-id
|
|
166
|
+
{ type: 1, 'state.email': 1 } // user lookup (sparse)
|
|
167
|
+
{ 'state.belongsTo': 1, 'state.location': 1 } // folder list/search (sparse)
|
|
163
168
|
```
|
|
164
169
|
|
|
170
|
+
`{ type: 1, event: 1 }` on `eventstore` is still useful for projection replay / task triggers but is not created automatically.
|
|
171
|
+
|
|
165
172
|
---
|
|
166
173
|
|
|
167
174
|
## Testing
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ossy/event-store",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.7.0",
|
|
4
4
|
"description": "Ossy Event Store — Aggregate, EventStore, and MongoDB client",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -37,5 +37,5 @@
|
|
|
37
37
|
"files": [
|
|
38
38
|
"src"
|
|
39
39
|
],
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "622d97535abad08415963405ace2f5aaaecd7ca4"
|
|
41
41
|
}
|
package/src/aggregate-rebuild.js
CHANGED
|
@@ -95,18 +95,25 @@ export class AggregateRebuild {
|
|
|
95
95
|
return
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
98
|
+
// Fan-out of every stream at once OOMs 1–2 GB Fargate tasks (website-ossy
|
|
99
|
+
// deploy 2026-08-20). Rebuild in batches so Mongo load and RSS stay bounded.
|
|
100
|
+
const streams = await EventStore.GetResourceStreams()
|
|
101
|
+
const results = []
|
|
102
|
+
for (let offset = 0; offset < streams.length; offset += 8) {
|
|
103
|
+
const batchResults = await Promise.allSettled(
|
|
104
|
+
streams.slice(offset, offset + 8).map(({ resourceId, type }) =>
|
|
105
|
+
AggregateRebuild.BuildAndSaveResource(resourceId, type),
|
|
106
|
+
),
|
|
107
|
+
)
|
|
108
|
+
results.push(...batchResults)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
await ProjectionRebuild.rebuildAll()
|
|
112
|
+
const failed = results.filter(r => r.status === 'rejected')
|
|
113
|
+
const success = results.filter(r => r.status === 'fulfilled')
|
|
114
|
+
log.info('[AggregateRebuild][BuildAndSaveAll] Finished building aggregates')
|
|
115
|
+
log.info(`[AggregateRebuild][BuildAndSaveAll] Failed: ${failed.length}`)
|
|
116
|
+
log.info(`[AggregateRebuild][BuildAndSaveAll] Success: ${success.length}`)
|
|
110
117
|
}
|
|
111
118
|
}
|
|
112
119
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { describe, expect, it } from '@jest/globals'
|
|
2
|
-
import { selectTailEventsForRebuild } from './aggregate-rebuild.js'
|
|
1
|
+
import { afterEach, describe, expect, it, jest } from '@jest/globals'
|
|
2
|
+
import { AggregateRebuild, selectTailEventsForRebuild } from './aggregate-rebuild.js'
|
|
3
|
+
import { EventStore } from './event-store.js'
|
|
4
|
+
import { ProjectionRebuild } from './projection-rebuild.js'
|
|
3
5
|
|
|
4
6
|
describe('selectTailEventsForRebuild', () => {
|
|
5
7
|
const resourceId = 'res_1'
|
|
@@ -40,6 +42,45 @@ describe('selectTailEventsForRebuild', () => {
|
|
|
40
42
|
})
|
|
41
43
|
})
|
|
42
44
|
|
|
45
|
+
describe('BuildAndSaveAll', () => {
|
|
46
|
+
afterEach(() => {
|
|
47
|
+
AggregateRebuild._schemaMap = {}
|
|
48
|
+
AggregateRebuild._aggregateMap = {}
|
|
49
|
+
jest.restoreAllMocks()
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('rebuilds resource streams in batches of 8', async () => {
|
|
53
|
+
AggregateRebuild.registerAggregate({
|
|
54
|
+
id: '@ossy/test/aggregate',
|
|
55
|
+
Aggregate: class {
|
|
56
|
+
static SchemaId = 'test.schema'
|
|
57
|
+
},
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
const streams = Array.from({ length: 10 }, (_, i) => ({
|
|
61
|
+
resourceId: `res_${i}`,
|
|
62
|
+
type: 'test.schema',
|
|
63
|
+
}))
|
|
64
|
+
jest.spyOn(EventStore, 'GetResourceStreams').mockResolvedValue(streams)
|
|
65
|
+
|
|
66
|
+
let inFlight = 0
|
|
67
|
+
let maxInFlight = 0
|
|
68
|
+
jest.spyOn(AggregateRebuild, 'BuildAndSaveResource').mockImplementation(async () => {
|
|
69
|
+
inFlight += 1
|
|
70
|
+
maxInFlight = Math.max(maxInFlight, inFlight)
|
|
71
|
+
await new Promise(resolve => setTimeout(resolve, 15))
|
|
72
|
+
inFlight -= 1
|
|
73
|
+
})
|
|
74
|
+
jest.spyOn(ProjectionRebuild, 'rebuildAll').mockResolvedValue()
|
|
75
|
+
|
|
76
|
+
await AggregateRebuild.BuildAndSaveAll()
|
|
77
|
+
|
|
78
|
+
expect(AggregateRebuild.BuildAndSaveResource).toHaveBeenCalledTimes(10)
|
|
79
|
+
expect(maxInFlight).toBeLessThanOrEqual(8)
|
|
80
|
+
expect(ProjectionRebuild.rebuildAll).toHaveBeenCalledTimes(1)
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
|
|
43
84
|
describe('incremental rebuild semantics', () => {
|
|
44
85
|
it('folds only tail events onto snapshot state', () => {
|
|
45
86
|
const View = (events, state = {}) =>
|
package/src/ensure-indexes.js
CHANGED
|
@@ -9,6 +9,8 @@ let ensured = false
|
|
|
9
9
|
* Ensure MongoDB indexes required for aggregate/event-stream reads.
|
|
10
10
|
* Without `{ resourceId, version }` on `eventstore`, every `getResourceStream`
|
|
11
11
|
* call scans the full collection — sign-up and changestream rebuilds deadlock under load.
|
|
12
|
+
* Without `{ state.belongsTo, state.location }` on `aggregates`, folder list/search
|
|
13
|
+
* examines the whole collection, including audit snapshots that are not in any folder.
|
|
12
14
|
*/
|
|
13
15
|
export async function ensureEventStoreIndexes () {
|
|
14
16
|
if (ensured) return
|
|
@@ -29,6 +31,10 @@ export async function ensureEventStoreIndexes () {
|
|
|
29
31
|
{ type: 1, 'state.email': 1 },
|
|
30
32
|
{ name: 'type_state_email', background: true, sparse: true },
|
|
31
33
|
)
|
|
34
|
+
await aggregates.createIndex(
|
|
35
|
+
{ 'state.belongsTo': 1, 'state.location': 1 },
|
|
36
|
+
{ name: 'state_belongsTo_location', background: true, sparse: true },
|
|
37
|
+
)
|
|
32
38
|
})
|
|
33
39
|
|
|
34
40
|
ensured = true
|