Skip to content

Commit

Permalink
fix metadata properties and added test
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-laycalvert authored and oskardudycz committed Dec 6, 2024
1 parent 63a1a85 commit 1cb7bb7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,34 @@ void describe('MongoDBEventStore', () => {
}
});

void it('should create a new stream with metadata with appendToStream', async () => {
const productItem: PricedProductItem = {
productId: '123',
quantity: 10,
price: 3,
};
const shoppingCartId = uuid();
const streamType = 'shopping_cart';
const streamName = toStreamName(streamType, shoppingCartId);

await eventStore.appendToStream<ShoppingCartEvent>(
streamName,
[{ type: 'ProductItemAdded', data: { productItem } }],
{ expectedStreamVersion: STREAM_DOES_NOT_EXIST },
);

const stream = await collection.findOne(
{ streamName },
{ useBigInt64: true },
);
assertIsNotNull(stream);
assertEqual(1n, stream.metadata.streamPosition);
assertEqual(shoppingCartId, stream.metadata.streamId);
assertEqual(streamType, stream.metadata.streamType);
assertTrue(stream.metadata.createdAt instanceof Date);
assertTrue(stream.metadata.updatedAt instanceof Date);
});

void it('should append events correctly using appendEvent function', async () => {
const productItem: PricedProductItem = {
productId: '123',
Expand All @@ -78,7 +106,8 @@ void describe('MongoDBEventStore', () => {
};
const discount = 10;
const shoppingCartId = uuid();
const streamName = toStreamName('shopping_cart', shoppingCartId);
const streamType = 'shopping_cart';
const streamName = toStreamName(streamType, shoppingCartId);

await eventStore.appendToStream<ShoppingCartEvent>(
streamName,
Expand All @@ -98,7 +127,7 @@ void describe('MongoDBEventStore', () => {
{ useBigInt64: true },
);
assertIsNotNull(stream);
assertEqual('3', stream.metadata.streamPosition.toString());
assertEqual(3n, stream.metadata.streamPosition);
assertDeepEqual(stream.projections[SHOPPING_CART_PROJECTION_NAME], {
productItemsCount: 20,
totalAmount: 54,
Expand Down
10 changes: 8 additions & 2 deletions src/packages/emmett-mongodb/src/eventStore/mongoDBEventStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ export class MongoDBEventStore implements EventStore<MongoDBReadEventMetadata> {
Exclude<ReadStreamResult<EventType, MongoDBReadEventMetadata>, null>
> {
const expectedStreamVersion = options?.expectedStreamVersion;
// TODO: use `to` and `from`

const filter = {
streamName: { $eq: streamName },
Expand Down Expand Up @@ -229,10 +228,17 @@ export class MongoDBEventStore implements EventStore<MongoDBReadEventMetadata> {
>;
});

const { streamId, streamType } = fromStreamName(streamName);
const now = new Date();
const updates: UpdateFilter<EventStream> = {
$push: { events: { $each: eventsToAppend } },
$set: { 'metadata.updatedAt': new Date() },
$set: { 'metadata.updatedAt': now },
$inc: { 'metadata.streamPosition': BigInt(events.length) },
$setOnInsert: {
'metadata.streamId': streamId,
'metadata.streamType': streamType,
'metadata.createdAt': now,
},
};

if (this.inlineProjections) {
Expand Down

0 comments on commit 1cb7bb7

Please sign in to comment.