11import {
22 ExpectedVersionConflictError ,
3- STREAM_DOES_NOT_EXIST ,
43 assertExpectedVersionMatchesCurrent ,
54 type AggregateStreamOptions ,
65 type AggregateStreamResult ,
98 type Event ,
109 type EventMetaDataOf ,
1110 type EventStore ,
12- type ExpectedStreamVersion ,
1311 type ProjectionRegistration ,
1412 type ReadEvent ,
1513 type ReadEventMetadataWithoutGlobalPosition ,
@@ -85,6 +83,24 @@ export type MongoDBReadEvent<EventType extends Event = Event> = ReadEvent<
8583 MongoDBReadEventMetadata
8684> ;
8785
86+ export type MongoDBSingleCollectionEventStoreOptions = {
87+ storage : 'SINGLE_COLLECTION' ;
88+ collection : string ;
89+ projections ?: ProjectionRegistration <
90+ 'inline' ,
91+ MongoDBReadEventMetadata ,
92+ MongoDBProjectionInlineHandlerContext
93+ > [ ] ;
94+ } & (
95+ | {
96+ client : MongoClient ;
97+ }
98+ | {
99+ connectionString : string ;
100+ clientOptions ?: MongoClientOptions ;
101+ }
102+ ) ;
103+
88104export type MongoDBEventStoreOptions = {
89105 database ?: string ;
90106 collection ?: string ;
@@ -270,9 +286,12 @@ class MongoDBEventStoreImplementation implements MongoDBEventStore {
270286 const now = new Date ( ) ;
271287 const updates : UpdateFilter < EventStream > = {
272288 $push : { messages : { $each : eventsToAppend } } ,
273- $set : { 'metadata.updatedAt' : now } ,
274- $inc : { 'metadata.streamPosition' : BigInt ( events . length ) } ,
289+ $set : {
290+ 'metadata.updatedAt' : now ,
291+ 'metadata.streamPosition' : currentStreamVersion + BigInt ( events . length ) ,
292+ } ,
275293 $setOnInsert : {
294+ streamName,
276295 'metadata.streamId' : streamId ,
277296 'metadata.streamType' : streamType ,
278297 'metadata.createdAt' : now ,
@@ -293,9 +312,7 @@ class MongoDBEventStoreImplementation implements MongoDBEventStore {
293312 const updatedStream = await collection . updateOne (
294313 {
295314 streamName : { $eq : streamName } ,
296- 'metadata.streamPosition' : toExpectedVersion (
297- options ?. expectedStreamVersion ,
298- ) ,
315+ 'metadata.streamPosition' : currentStreamVersion ,
299316 } ,
300317 updates ,
301318 { useBigInt64 : true , upsert : true } ,
@@ -367,23 +384,6 @@ export const getMongoDBEventStore = (
367384 options : MongoDBEventStoreOptions ,
368385) : MongoDBEventStore => new MongoDBEventStoreImplementation ( options ) ;
369386
370- function toExpectedVersion (
371- expectedStreamVersion ?: ExpectedStreamVersion ,
372- ) : bigint | undefined {
373- if ( ! expectedStreamVersion ) return undefined ;
374-
375- if ( typeof expectedStreamVersion === 'string' ) {
376- switch ( expectedStreamVersion ) {
377- case STREAM_DOES_NOT_EXIST :
378- return BigInt ( 0 ) ;
379- default :
380- return undefined ;
381- }
382- }
383-
384- return expectedStreamVersion ;
385- }
386-
387387/**
388388 * Accepts a `streamType` (the type/category of the event stream) and an `streamId`
389389 * (the individual entity/object or aggregate ID) and combines them to a singular
0 commit comments