nest-eventsource-kit 1.0.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 +21 -0
- package/README.md +40 -0
- package/dist/event-sourcing.module.d.ts +4 -0
- package/dist/event-sourcing.module.js +69 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/dist/interfaces/event.interface.d.ts +11 -0
- package/dist/interfaces/event.interface.js +2 -0
- package/dist/services/event-store.service.d.ts +6 -0
- package/dist/services/event-store.service.js +69 -0
- package/dist/services/event-store.service.spec.d.ts +1 -0
- package/dist/services/event-store.service.spec.js +26 -0
- package/jest.config.js +7 -0
- package/package.json +45 -0
- package/src/event-sourcing.module.ts +14 -0
- package/src/index.ts +3 -0
- package/src/interfaces/event.interface.ts +12 -0
- package/src/services/event-store.service.spec.ts +30 -0
- package/src/services/event-store.service.ts +15 -0
- package/tsconfig.json +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alireza Aminzadeh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# nest-eventsource-kit
|
|
2
|
+
|
|
3
|
+
Event Sourcing and CQRS toolkit for NestJS applications.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Event Sourcing** - Store state as sequence of events
|
|
8
|
+
- **CQRS** - Separate read and write models
|
|
9
|
+
- **Saga Pattern** - Coordinate complex transactions
|
|
10
|
+
- **Event Store** - Persist and replay events
|
|
11
|
+
- **Domain-Driven Design** - DDD building blocks
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install nest-eventsource-kit
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { EventSourcingModule } from 'nest-eventsource-kit';
|
|
23
|
+
|
|
24
|
+
@Module({
|
|
25
|
+
imports: [
|
|
26
|
+
EventSourcingModule.forRoot({
|
|
27
|
+
eventStore: InMemoryEventStore,
|
|
28
|
+
}),
|
|
29
|
+
],
|
|
30
|
+
})
|
|
31
|
+
export class AppModule {}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Author
|
|
35
|
+
|
|
36
|
+
Alireza Aminzadeh - alireza.aminzadeh@hotmail.com
|
|
37
|
+
|
|
38
|
+
## License
|
|
39
|
+
|
|
40
|
+
MIT
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
3
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
4
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
5
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
6
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
7
|
+
var _, done = false;
|
|
8
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
9
|
+
var context = {};
|
|
10
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
11
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
12
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
13
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
14
|
+
if (kind === "accessor") {
|
|
15
|
+
if (result === void 0) continue;
|
|
16
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
17
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
18
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
19
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
20
|
+
}
|
|
21
|
+
else if (_ = accept(result)) {
|
|
22
|
+
if (kind === "field") initializers.unshift(_);
|
|
23
|
+
else descriptor[key] = _;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
27
|
+
done = true;
|
|
28
|
+
};
|
|
29
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
30
|
+
var useValue = arguments.length > 2;
|
|
31
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
32
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
33
|
+
}
|
|
34
|
+
return useValue ? value : void 0;
|
|
35
|
+
};
|
|
36
|
+
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
|
|
37
|
+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
|
|
38
|
+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
|
|
39
|
+
};
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.EventSourcingModule = void 0;
|
|
42
|
+
const common_1 = require("@nestjs/common");
|
|
43
|
+
const event_store_service_1 = require("./services/event-store.service");
|
|
44
|
+
let EventSourcingModule = (() => {
|
|
45
|
+
let _classDecorators = [(0, common_1.Module)({})];
|
|
46
|
+
let _classDescriptor;
|
|
47
|
+
let _classExtraInitializers = [];
|
|
48
|
+
let _classThis;
|
|
49
|
+
var EventSourcingModule = _classThis = class {
|
|
50
|
+
static forRoot() {
|
|
51
|
+
return {
|
|
52
|
+
module: EventSourcingModule,
|
|
53
|
+
providers: [event_store_service_1.EventStoreService],
|
|
54
|
+
exports: [event_store_service_1.EventStoreService],
|
|
55
|
+
global: true,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
__setFunctionName(_classThis, "EventSourcingModule");
|
|
60
|
+
(() => {
|
|
61
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
62
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
63
|
+
EventSourcingModule = _classThis = _classDescriptor.value;
|
|
64
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
65
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
66
|
+
})();
|
|
67
|
+
return EventSourcingModule = _classThis;
|
|
68
|
+
})();
|
|
69
|
+
exports.EventSourcingModule = EventSourcingModule;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./event-sourcing.module"), exports);
|
|
18
|
+
__exportStar(require("./services/event-store.service"), exports);
|
|
19
|
+
__exportStar(require("./interfaces/event.interface"), exports);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface DomainEvent {
|
|
2
|
+
aggregateId: string;
|
|
3
|
+
eventType: string;
|
|
4
|
+
data: any;
|
|
5
|
+
timestamp: Date;
|
|
6
|
+
version: number;
|
|
7
|
+
}
|
|
8
|
+
export interface EventStore {
|
|
9
|
+
append(event: DomainEvent): Promise<void>;
|
|
10
|
+
getEvents(aggregateId: string): Promise<DomainEvent[]>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
3
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
4
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
5
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
6
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
7
|
+
var _, done = false;
|
|
8
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
9
|
+
var context = {};
|
|
10
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
11
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
12
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
13
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
14
|
+
if (kind === "accessor") {
|
|
15
|
+
if (result === void 0) continue;
|
|
16
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
17
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
18
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
19
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
20
|
+
}
|
|
21
|
+
else if (_ = accept(result)) {
|
|
22
|
+
if (kind === "field") initializers.unshift(_);
|
|
23
|
+
else descriptor[key] = _;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
27
|
+
done = true;
|
|
28
|
+
};
|
|
29
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
30
|
+
var useValue = arguments.length > 2;
|
|
31
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
32
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
33
|
+
}
|
|
34
|
+
return useValue ? value : void 0;
|
|
35
|
+
};
|
|
36
|
+
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
|
|
37
|
+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
|
|
38
|
+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
|
|
39
|
+
};
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.EventStoreService = void 0;
|
|
42
|
+
const common_1 = require("@nestjs/common");
|
|
43
|
+
let EventStoreService = (() => {
|
|
44
|
+
let _classDecorators = [(0, common_1.Injectable)()];
|
|
45
|
+
let _classDescriptor;
|
|
46
|
+
let _classExtraInitializers = [];
|
|
47
|
+
let _classThis;
|
|
48
|
+
var EventStoreService = _classThis = class {
|
|
49
|
+
constructor() {
|
|
50
|
+
this.events = [];
|
|
51
|
+
}
|
|
52
|
+
async append(event) {
|
|
53
|
+
this.events.push(event);
|
|
54
|
+
}
|
|
55
|
+
async getEvents(aggregateId) {
|
|
56
|
+
return this.events.filter(e => e.aggregateId === aggregateId);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
__setFunctionName(_classThis, "EventStoreService");
|
|
60
|
+
(() => {
|
|
61
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
62
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
63
|
+
EventStoreService = _classThis = _classDescriptor.value;
|
|
64
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
65
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
66
|
+
})();
|
|
67
|
+
return EventStoreService = _classThis;
|
|
68
|
+
})();
|
|
69
|
+
exports.EventStoreService = EventStoreService;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const testing_1 = require("@nestjs/testing");
|
|
4
|
+
const event_store_service_1 = require("./event-store.service");
|
|
5
|
+
describe('EventStoreService', () => {
|
|
6
|
+
let service;
|
|
7
|
+
beforeEach(async () => {
|
|
8
|
+
const module = await testing_1.Test.createTestingModule({
|
|
9
|
+
providers: [event_store_service_1.EventStoreService],
|
|
10
|
+
}).compile();
|
|
11
|
+
service = module.get(event_store_service_1.EventStoreService);
|
|
12
|
+
});
|
|
13
|
+
it('should append and retrieve events', async () => {
|
|
14
|
+
const event = {
|
|
15
|
+
aggregateId: 'test-1',
|
|
16
|
+
eventType: 'TestEvent',
|
|
17
|
+
data: { value: 42 },
|
|
18
|
+
timestamp: new Date(),
|
|
19
|
+
version: 1,
|
|
20
|
+
};
|
|
21
|
+
await service.append(event);
|
|
22
|
+
const events = await service.getEvents('test-1');
|
|
23
|
+
expect(events).toHaveLength(1);
|
|
24
|
+
expect(events[0]).toEqual(event);
|
|
25
|
+
});
|
|
26
|
+
});
|
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nest-eventsource-kit",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Event Sourcing and CQRS toolkit for NestJS with support for event store, command/query separation, and saga patterns",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "jest",
|
|
10
|
+
"prepublishOnly": "npm run build"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"nestjs",
|
|
14
|
+
"event-sourcing",
|
|
15
|
+
"cqrs",
|
|
16
|
+
"ddd",
|
|
17
|
+
"domain-driven-design",
|
|
18
|
+
"event-store",
|
|
19
|
+
"saga",
|
|
20
|
+
"command-query",
|
|
21
|
+
"typescript"
|
|
22
|
+
],
|
|
23
|
+
"author": {
|
|
24
|
+
"name": "Alireza Aminzadeh",
|
|
25
|
+
"email": "syeedalireza@yahoo.com",
|
|
26
|
+
"url": "https://github.com/syeedalireza"
|
|
27
|
+
},
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@nestjs/common": "^10.0.0",
|
|
31
|
+
"@nestjs/core": "^10.0.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@nestjs/common": "^10.3.0",
|
|
35
|
+
"@nestjs/core": "^10.3.0",
|
|
36
|
+
"@nestjs/testing": "^10.3.0",
|
|
37
|
+
"@types/jest": "^29.5.11",
|
|
38
|
+
"@types/node": "^20.11.0",
|
|
39
|
+
"jest": "^29.7.0",
|
|
40
|
+
"reflect-metadata": "^0.2.1",
|
|
41
|
+
"rxjs": "^7.8.1",
|
|
42
|
+
"ts-jest": "^29.1.1",
|
|
43
|
+
"typescript": "^5.3.3"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Module, DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { EventStoreService } from './services/event-store.service';
|
|
3
|
+
|
|
4
|
+
@Module({})
|
|
5
|
+
export class EventSourcingModule {
|
|
6
|
+
static forRoot(): DynamicModule {
|
|
7
|
+
return {
|
|
8
|
+
module: EventSourcingModule,
|
|
9
|
+
providers: [EventStoreService],
|
|
10
|
+
exports: [EventStoreService],
|
|
11
|
+
global: true,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface DomainEvent {
|
|
2
|
+
aggregateId: string;
|
|
3
|
+
eventType: string;
|
|
4
|
+
data: any;
|
|
5
|
+
timestamp: Date;
|
|
6
|
+
version: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface EventStore {
|
|
10
|
+
append(event: DomainEvent): Promise<void>;
|
|
11
|
+
getEvents(aggregateId: string): Promise<DomainEvent[]>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Test } from '@nestjs/testing';
|
|
2
|
+
import { EventStoreService } from './event-store.service';
|
|
3
|
+
|
|
4
|
+
describe('EventStoreService', () => {
|
|
5
|
+
let service: EventStoreService;
|
|
6
|
+
|
|
7
|
+
beforeEach(async () => {
|
|
8
|
+
const module = await Test.createTestingModule({
|
|
9
|
+
providers: [EventStoreService],
|
|
10
|
+
}).compile();
|
|
11
|
+
|
|
12
|
+
service = module.get<EventStoreService>(EventStoreService);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should append and retrieve events', async () => {
|
|
16
|
+
const event = {
|
|
17
|
+
aggregateId: 'test-1',
|
|
18
|
+
eventType: 'TestEvent',
|
|
19
|
+
data: { value: 42 },
|
|
20
|
+
timestamp: new Date(),
|
|
21
|
+
version: 1,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
await service.append(event);
|
|
25
|
+
const events = await service.getEvents('test-1');
|
|
26
|
+
|
|
27
|
+
expect(events).toHaveLength(1);
|
|
28
|
+
expect(events[0]).toEqual(event);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
|
+
import { DomainEvent, EventStore } from '../interfaces/event.interface';
|
|
3
|
+
|
|
4
|
+
@Injectable()
|
|
5
|
+
export class EventStoreService implements EventStore {
|
|
6
|
+
private events: DomainEvent[] = [];
|
|
7
|
+
|
|
8
|
+
async append(event: DomainEvent): Promise<void> {
|
|
9
|
+
this.events.push(event);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async getEvents(aggregateId: string): Promise<DomainEvent[]> {
|
|
13
|
+
return this.events.filter(e => e.aggregateId === aggregateId);
|
|
14
|
+
}
|
|
15
|
+
}
|