@event-driven-io/emmett-mongodb 0.22.1 → 0.23.0-alpha.1

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/dist/index.js CHANGED
@@ -260,7 +260,9 @@ var streamTransformations = {
260
260
  var { retry: retry2 } = streamTransformations;
261
261
 
262
262
  // src/eventStore/mongoDBEventStore.ts
263
- import "mongodb";
263
+ import {
264
+ MongoClient
265
+ } from "mongodb";
264
266
  import { v4 as uuid4 } from "uuid";
265
267
 
266
268
  // src/eventStore/projections/index.ts
@@ -314,17 +316,29 @@ var mongoDBInlineProjection = (options) => {
314
316
 
315
317
  // src/eventStore/mongoDBEventStore.ts
316
318
  var MongoDBEventStoreDefaultStreamVersion = 0n;
317
- var MongoDBEventStore = class {
318
- collection;
319
+ var MongoDBEventStoreImplementation = class {
320
+ client;
321
+ defaultOptions;
322
+ shouldManageClientLifetime;
323
+ db;
324
+ streamCollections = /* @__PURE__ */ new Map();
319
325
  inlineProjections;
326
+ isClosed = false;
320
327
  constructor(options) {
321
- this.collection = options.collection;
328
+ this.client = "client" in options ? options.client : new MongoClient(options.connectionString, options.clientOptions);
329
+ this.shouldManageClientLifetime = !("client" in options);
330
+ this.defaultOptions = {
331
+ database: options.database,
332
+ collection: options.collection
333
+ };
322
334
  this.inlineProjections = (options.projections ?? []).filter(({ type }) => type === "inline").map(
323
335
  ({ projection }) => projection
324
336
  );
325
337
  }
326
338
  async readStream(streamName, options) {
339
+ const { streamType } = fromStreamName(streamName);
327
340
  const expectedStreamVersion = options?.expectedStreamVersion;
341
+ const collection = await this.collectionFor(streamType);
328
342
  const filter2 = {
329
343
  streamName: { $eq: streamName }
330
344
  };
@@ -338,11 +352,11 @@ var MongoDBEventStore = class {
338
352
  eventsSliceArr.push(Number(options.to));
339
353
  }
340
354
  const eventsSlice = eventsSliceArr.length > 1 ? { $slice: eventsSliceArr } : 1;
341
- const stream = await this.collection.findOne(filter2, {
355
+ const stream = await collection.findOne(filter2, {
342
356
  useBigInt64: true,
343
357
  projection: {
344
358
  metadata: 1,
345
- events: eventsSlice
359
+ messages: eventsSlice
346
360
  }
347
361
  });
348
362
  if (!stream) {
@@ -358,7 +372,7 @@ var MongoDBEventStore = class {
358
372
  MongoDBEventStoreDefaultStreamVersion
359
373
  );
360
374
  return {
361
- events: stream.events,
375
+ events: stream.messages,
362
376
  currentStreamVersion: stream.metadata.streamPosition,
363
377
  streamExists: true
364
378
  };
@@ -373,8 +387,10 @@ var MongoDBEventStore = class {
373
387
  };
374
388
  }
375
389
  async appendToStream(streamName, events, options) {
390
+ const { streamId, streamType } = fromStreamName(streamName);
376
391
  const expectedStreamVersion = options?.expectedStreamVersion;
377
- const stream = await this.collection.findOne(
392
+ const collection = await this.collectionFor(streamType);
393
+ const stream = await collection.findOne(
378
394
  { streamName: { $eq: streamName } },
379
395
  {
380
396
  useBigInt64: true,
@@ -406,10 +422,9 @@ var MongoDBEventStore = class {
406
422
  }
407
423
  };
408
424
  });
409
- const { streamId, streamType } = fromStreamName(streamName);
410
425
  const now = /* @__PURE__ */ new Date();
411
426
  const updates = {
412
- $push: { events: { $each: eventsToAppend } },
427
+ $push: { messages: { $each: eventsToAppend } },
413
428
  $set: { "metadata.updatedAt": now },
414
429
  $inc: { "metadata.streamPosition": BigInt(events.length) },
415
430
  $setOnInsert: {
@@ -423,12 +438,12 @@ var MongoDBEventStore = class {
423
438
  readModels: stream?.projections ?? {},
424
439
  events: eventsToAppend,
425
440
  projections: this.inlineProjections,
426
- collection: this.collection,
441
+ collection,
427
442
  updates,
428
443
  client: {}
429
444
  });
430
445
  }
431
- const updatedStream = await this.collection.updateOne(
446
+ const updatedStream = await collection.updateOne(
432
447
  {
433
448
  streamName: { $eq: streamName },
434
449
  "metadata.streamPosition": toExpectedVersion(
@@ -449,11 +464,33 @@ var MongoDBEventStore = class {
449
464
  createdNewStream: currentStreamVersion === MongoDBEventStoreDefaultStreamVersion
450
465
  };
451
466
  }
467
+ close() {
468
+ if (this.isClosed) return Promise.resolve();
469
+ this.isClosed = true;
470
+ if (!this.shouldManageClientLifetime) return Promise.resolve();
471
+ return this.client.close();
472
+ }
473
+ getDB = async () => {
474
+ if (!this.db) {
475
+ if (!this.isClosed) await this.client.connect();
476
+ this.db = this.client.db(this.defaultOptions.database);
477
+ }
478
+ return this.db;
479
+ };
480
+ collectionFor = async (streamType) => {
481
+ const collectionName = this.defaultOptions?.collection ?? toStreamCollectionName(streamType);
482
+ let collection = this.streamCollections.get(collectionName);
483
+ if (collection) return collection;
484
+ const db = await this.getDB();
485
+ collection = db.collection(collectionName);
486
+ this.streamCollections.set(
487
+ collectionName,
488
+ collection
489
+ );
490
+ return collection;
491
+ };
452
492
  };
453
- var getMongoDBEventStore = (options) => {
454
- const eventStore = new MongoDBEventStore(options);
455
- return eventStore;
456
- };
493
+ var getMongoDBEventStore = (options) => new MongoDBEventStoreImplementation(options);
457
494
  function toExpectedVersion(expectedStreamVersion) {
458
495
  if (!expectedStreamVersion) return void 0;
459
496
  if (typeof expectedStreamVersion === "string") {
@@ -476,13 +513,23 @@ function fromStreamName(streamName) {
476
513
  streamId: parts[1]
477
514
  };
478
515
  }
516
+ function toStreamCollectionName(streamType) {
517
+ return `emt:${streamType}`;
518
+ }
519
+ function fromStreamCollectionName(streamCollectionName) {
520
+ const parts = streamCollectionName.split(":");
521
+ return {
522
+ streamType: parts[1]
523
+ };
524
+ }
479
525
  export {
480
- MongoDBEventStore,
481
526
  MongoDBEventStoreDefaultStreamVersion,
527
+ fromStreamCollectionName,
482
528
  fromStreamName,
483
529
  getMongoDBEventStore,
484
530
  handleInlineProjections,
485
531
  mongoDBInlineProjection,
532
+ toStreamCollectionName,
486
533
  toStreamName
487
534
  };
488
535
  //# sourceMappingURL=index.js.map