11import { setTimeout } from "node:timers/promises" ;
2- import { type Collection , type Db , type Document , MongoClient } from "mongodb" ;
2+ import { type AnyBulkWriteOperation , type Collection , type Db , type Document , MongoClient } from "mongodb" ;
33import makeTestLogger from "../tests/helpers/makeTestLogger" ;
44import getCollectionName from "./getCollectionName" ;
55import type MongoDBModel from "./model" ;
66import getBulkOperations from "./optimistic/getBulkOperations" ;
77import { Signal , clearModelSlots , runWithModel } from "./read" ;
8- import type { Changes , JourneyCommittedEvent , MongoDBStore , MongoDBStoreConfig , MongoOps } from "./types" ;
8+ import type { Changes , JourneyCommittedEvent , MongoDBStore , MongoDBStoreConfig } from "./types" ;
99
1010interface SnapshotDocument {
1111 __v : number ;
@@ -294,19 +294,12 @@ export default async function makeMongoDBStore(config: MongoDBStoreConfig): Prom
294294
295295 // output is an array of changes per event.
296296 // however for efficiency, we need to handle event per model
297- type VersionedChange = {
298- change : MongoOps < Document > ;
299- __op : number ;
300- __v : number ;
301- } ;
302-
303- const changesPerModel : VersionedChange [ ] [ ] = [ ] ;
297+ // so we need to group changes per model
298+ const bulkWritesPerModel : AnyBulkWriteOperation < Document > [ ] [ ] = [ ] ;
304299
305300 for ( const allChangesForAnEvent of outputs ) {
306301 let modelIndex = 0 ;
307302 for ( const changesForAModel of allChangesForAnEvent ) {
308- let __op = 0 ;
309-
310303 if ( changesForAModel === undefined || changesForAModel . length === 0 ) {
311304 modelIndex ++ ;
312305 continue ;
@@ -315,37 +308,37 @@ export default async function makeMongoDBStore(config: MongoDBStoreConfig): Prom
315308 const changesWithOp = changesForAModel . map ( ( change ) => {
316309 return {
317310 change,
318- __op : __op ++ ,
319311 __v : events [ eventIndex ] . id ,
320312 } ;
321313 } ) ;
322314
323- // add changes to changesPerModel
324- if ( ! changesPerModel [ modelIndex ] ) {
325- changesPerModel [ modelIndex ] = [ ] ;
315+ const operations = getBulkOperations ( changesWithOp ) ;
316+
317+ // add operations to the bulkWritesPerModel
318+ if ( ! bulkWritesPerModel [ modelIndex ] ) {
319+ bulkWritesPerModel [ modelIndex ] = [ ] ;
326320 }
327- changesPerModel [ modelIndex ] . push ( ...changesWithOp ) ;
321+ bulkWritesPerModel [ modelIndex ] . push ( ...operations ) ;
328322 modelIndex ++ ;
329323 }
330324 eventIndex ++ ;
331325 }
332326
333327 await Promise . all (
334- changesPerModel . map ( async ( changesForAModel , modelIndex ) => {
328+ bulkWritesPerModel . map ( async ( bulkWritesForAModel , modelIndex ) => {
335329 const model = models [ modelIndex ] ;
336330 const outputForThisModel = changes [ modelIndex ] ;
337- if ( changesForAModel === undefined || changesForAModel . length === 0 ) {
331+ if ( bulkWritesForAModel === undefined || bulkWritesForAModel . length === 0 ) {
338332 return ;
339333 }
340334
341335 const collection = db . collection ( getCollectionName ( model ) ) ;
342- const bulkWriteOperations = getBulkOperations ( changesForAModel ) ;
343336
344337 if ( abortSignal . aborted ) {
345338 return ;
346339 }
347340
348- const res = await collection . bulkWrite ( bulkWriteOperations , { ordered : true , writeConcern : { w : "majority" } } ) ;
341+ const res = await collection . bulkWrite ( bulkWritesForAModel , { ordered : true , writeConcern : { w : "majority" } } ) ;
349342
350343 skipByModel [ modelIndex ] = Math . max ( skipByModel [ modelIndex ] , events [ events . length - 1 ] . id ) ;
351344
0 commit comments