Skip to content

Commit

Permalink
Added decorator EventVersion (#64)
Browse files Browse the repository at this point in the history
* Added decorator EventVersion

* Added new version

* Bug fixed

* 2.3.0
  • Loading branch information
Jacob Dharandas Méndez authored Jun 7, 2021
1 parent fce2a2a commit 07db186
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions examples/src/heroes/events/impl/hero-killed-dragon.event.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { EventStoreAcknowledgeableEvent } from '../../../../../src';
import { EventVersion } from '../../../../../src/decorators/event-version.decorator';

// This is the second version of this event
@EventVersion(2)
export class HeroKilledDragonEvent extends EventStoreAcknowledgeableEvent {
public declare readonly data: {
heroId: string;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-geteventstore",
"version": "2.2.1",
"version": "2.3.0",
"description": "Event Store connector for NestJS-Cqrs",
"author": "Vincent Vermersch <vincent.vermersch@prestashop.com >",
"contributors": [
Expand Down
18 changes: 18 additions & 0 deletions src/decorators/event-version.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IBaseEvent } from '../interfaces';

export const EventVersion = (version: number) => <
T extends { new (...args: any[]): IBaseEvent }
>(
BaseEvent: T,
) => {
const newClass = class extends BaseEvent implements IBaseEvent {
constructor(...args: any[]) {
super(...args);
this.metadata.version = version;
}
};
Object.defineProperty(newClass, 'name', {
value: BaseEvent.name,
});
return newClass;
};

0 comments on commit 07db186

Please sign in to comment.