@serialized/serialized-client
Advanced tools
Comparing version 3.8.0 to 3.9.0
@@ -32,3 +32,4 @@ import { BaseClient, DomainEvent, EventEnvelope } from './'; | ||
export interface Commit { | ||
events: DomainEvent[]; | ||
events: EventEnvelope<DomainEvent>[]; | ||
expectedVersion?: number; | ||
encryptedData?: string; | ||
@@ -43,6 +44,7 @@ } | ||
checkExists(request: CheckAggregateExistsRequest): Promise<any>; | ||
update(aggregateId: string, commandHandler: (s: A) => Commit): Promise<void>; | ||
create(aggregateId: string, commandHandler: (s: A) => Commit): Promise<void>; | ||
storeEvent(aggregateId: string, event: DomainEvent, options?: StoreEventsOptions): Promise<void>; | ||
storeEvents(aggregateId: string, commit: Commit, options?: StoreEventsOptions): Promise<void>; | ||
update(aggregateId: string, commandHandler: (s: A) => DomainEvent[]): Promise<void>; | ||
create(aggregateId: string, commandHandler: (s: A) => DomainEvent[]): Promise<void>; | ||
commit(aggregateId: string, commandHandler: (s: A) => Commit): Promise<void>; | ||
recordEvent(aggregateId: string, event: DomainEvent): Promise<void>; | ||
recordEvents(aggregateId: string, events: DomainEvent[]): Promise<void>; | ||
load<T extends A>(aggregateId: string): Promise<T>; | ||
@@ -49,0 +51,0 @@ private loadInternal; |
@@ -80,3 +80,3 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var response, eventsToSave; | ||
var response, currentVersion, domainEvents, eventsToSave; | ||
return __generator(this, function (_a) { | ||
@@ -87,4 +87,6 @@ switch (_a.label) { | ||
response = _a.sent(); | ||
eventsToSave = commandHandler(response.aggregate); | ||
return [4 /*yield*/, this.saveInternal(aggregateId, eventsToSave, { expectedVersion: response.metadata.version })]; | ||
currentVersion = response.metadata.version; | ||
domainEvents = commandHandler(response.aggregate); | ||
eventsToSave = domainEvents.map(function (e) { return (_1.EventEnvelope.fromDomainEvent(e)); }); | ||
return [4 /*yield*/, this.saveInternal(aggregateId, { events: eventsToSave, expectedVersion: currentVersion })]; | ||
case 2: | ||
@@ -99,3 +101,3 @@ _a.sent(); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var aggregate, eventsToSave; | ||
var aggregate, domainEvents, eventsToSave; | ||
return __generator(this, function (_a) { | ||
@@ -105,4 +107,5 @@ switch (_a.label) { | ||
aggregate = new this.aggregateTypeConstructor.prototype.constructor(this.initialState); | ||
eventsToSave = commandHandler(aggregate); | ||
return [4 /*yield*/, this.saveInternal(aggregateId, eventsToSave, { expectedVersion: 0 })]; | ||
domainEvents = commandHandler(aggregate); | ||
eventsToSave = domainEvents.map(function (e) { return (_1.EventEnvelope.fromDomainEvent(e)); }); | ||
return [4 /*yield*/, this.saveInternal(aggregateId, { events: eventsToSave, expectedVersion: 0 })]; | ||
case 1: | ||
@@ -115,7 +118,11 @@ _a.sent(); | ||
}; | ||
AggregatesClient.prototype.storeEvent = function (aggregateId, event, options) { | ||
AggregatesClient.prototype.commit = function (aggregateId, commandHandler) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var aggregate, commit; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.storeEvents(aggregateId, { events: [event] }, options)]; | ||
case 0: | ||
aggregate = new this.aggregateTypeConstructor.prototype.constructor(this.initialState); | ||
commit = commandHandler(aggregate); | ||
return [4 /*yield*/, this.saveInternal(aggregateId, commit)]; | ||
case 1: | ||
@@ -128,7 +135,17 @@ _a.sent(); | ||
}; | ||
AggregatesClient.prototype.storeEvents = function (aggregateId, commit, options) { | ||
AggregatesClient.prototype.recordEvent = function (aggregateId, event) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.saveInternal(aggregateId, commit, options)]; | ||
case 0: return [4 /*yield*/, this.recordEvents(aggregateId, [event])]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
} | ||
}); | ||
}); | ||
}; | ||
AggregatesClient.prototype.recordEvents = function (aggregateId, events) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.saveInternal(aggregateId, { events: events.map(_1.EventEnvelope.fromDomainEvent) })]; | ||
case 1: | ||
@@ -205,25 +222,10 @@ _a.sent(); | ||
}; | ||
AggregatesClient.prototype.saveInternal = function (aggregateId, commit, options) { | ||
if (options === void 0) { options = { expectedVersion: undefined }; } | ||
AggregatesClient.prototype.saveInternal = function (aggregateId, commit) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var eventsToSave, payload, url; | ||
var url; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
eventsToSave = commit.events.map(function (e) { return (_1.EventEnvelope.fromDomainEvent(e)); }); | ||
if (options.expectedVersion) { | ||
payload = { | ||
events: eventsToSave, | ||
expectedVersion: options.expectedVersion, | ||
encryptedData: commit.encryptedData | ||
}; | ||
} | ||
else { | ||
payload = { | ||
events: eventsToSave, | ||
encryptedData: commit.encryptedData | ||
}; | ||
} | ||
url = AggregatesClient.aggregateUrlPath(this.aggregateType, aggregateId) + "/events"; | ||
return [4 /*yield*/, this.axiosClient.post(url, payload, this.axiosConfig())]; | ||
return [4 /*yield*/, this.axiosClient.post(url, commit, this.axiosConfig())]; | ||
case 1: | ||
@@ -230,0 +232,0 @@ _a.sent(); |
@@ -6,3 +6,3 @@ { | ||
"author": "Mattias Holmqvist", | ||
"version": "3.8.0", | ||
"version": "3.9.0", | ||
"main": "dist/index.js", | ||
@@ -9,0 +9,0 @@ "types": "dist/index.d.ts", |
61532
1257