Skip to content

Commit

Permalink
Added example of MongoDBInlineProjectionSpec in samples
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Dec 31, 2024
1 parent 8d6c549 commit a5fc62b
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 58 deletions.
38 changes: 19 additions & 19 deletions samples/webApi/expressjs-with-esdb/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions samples/webApi/expressjs-with-esdb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
},
"homepage": "https://github.com/event-driven-io/emmett#readme",
"dependencies": {
"@event-driven-io/emmett": "0.23.0-alpha.9",
"@event-driven-io/emmett-esdb": "0.23.0-alpha.9",
"@event-driven-io/emmett-expressjs": "0.23.0-alpha.9",
"@event-driven-io/emmett-testcontainers": "0.23.0-alpha.9"
"@event-driven-io/emmett": "0.23.0-alpha.10",
"@event-driven-io/emmett-esdb": "0.23.0-alpha.10",
"@event-driven-io/emmett-expressjs": "0.23.0-alpha.10",
"@event-driven-io/emmett-testcontainers": "0.23.0-alpha.10"
},
"devDependencies": {
"@types/node": "20.11.30",
Expand Down
28 changes: 14 additions & 14 deletions samples/webApi/expressjs-with-mongodb/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions samples/webApi/expressjs-with-mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
},
"homepage": "https://github.com/event-driven-io/emmett#readme",
"dependencies": {
"@event-driven-io/emmett": "0.23.0-alpha.9",
"@event-driven-io/emmett-expressjs": "0.23.0-alpha.9",
"@event-driven-io/emmett-mongodb": "0.23.0-alpha.9"
"@event-driven-io/emmett": "0.23.0-alpha.10",
"@event-driven-io/emmett-expressjs": "0.23.0-alpha.10",
"@event-driven-io/emmett-mongodb": "0.23.0-alpha.10"
},
"devDependencies": {
"@testcontainers/mongodb": "^10.10.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void describe('ShoppingCart', () => {
productItem,
addedAt: now,
},
metadata: { clientId },
},
]),
]);
Expand All @@ -64,6 +65,7 @@ void describe('ShoppingCart', () => {
productItem,
addedAt: oldTime,
},
metadata: { clientId },
},
]),
)
Expand All @@ -79,6 +81,7 @@ void describe('ShoppingCart', () => {
shoppingCartId,
confirmedAt: now,
},
metadata: { clientId },
},
]),
]);
Expand All @@ -97,10 +100,12 @@ void describe('ShoppingCart', () => {
productItem,
addedAt: oldTime,
},
metadata: { clientId },
},
{
type: 'ShoppingCartConfirmed',
data: { shoppingCartId, confirmedAt: oldTime },
metadata: { clientId },
},
]),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
expectInlineReadModel,
MongoDBInlineProjectionSpec,
} from '@event-driven-io/emmett-mongodb';
import {
MongoDBContainer,
StartedMongoDBContainer,
} from '@testcontainers/mongodb';
import { after, before, beforeEach, describe, it } from 'node:test';
import { v4 as uuid } from 'uuid';
import { shoppingCartDetailsProjection } from '.';
import { ShoppingCartId, type ShoppingCartEvent } from '../shoppingCart';

void describe('Shopping Cart Short Details Projection', () => {
let mongodb: StartedMongoDBContainer;
let connectionString: string;
let given: MongoDBInlineProjectionSpec<ShoppingCartId, ShoppingCartEvent>;
let shoppingCartId: ShoppingCartId;
let clientId: string;
const now = new Date();

before(async () => {
mongodb = await new MongoDBContainer().start();
connectionString = mongodb.getConnectionString();

given = MongoDBInlineProjectionSpec.for({
projection: shoppingCartDetailsProjection,
connectionString,
clientOptions: {
directConnection: true,
},
});
});

beforeEach(() => {
clientId = uuid();
shoppingCartId = ShoppingCartId(clientId);
});

after(async () => {
try {
await mongodb.stop();
} catch (error) {
console.log(error);
}
});

void it('adds product to empty shopping cart', () =>
given({ streamName: shoppingCartId, events: [] })
.when([
{
type: 'ProductItemAddedToShoppingCart',
data: {
shoppingCartId,
clientId,
productItem: { unitPrice: 100, productId: 'shoes', quantity: 100 },
addedAt: now,
},
metadata: {
clientId,
},
},
])
.then(
expectInlineReadModel.toHave({
status: 'Opened',
clientId,
openedAt: now,
totalAmount: 10000,
productItems: [{ quantity: 100, productId: 'shoes', unitPrice: 100 }],
productItemsCount: 100,
}),
));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import {
expectInlineReadModel,
MongoDBInlineProjectionSpec,
} from '@event-driven-io/emmett-mongodb';
import {
MongoDBContainer,
StartedMongoDBContainer,
} from '@testcontainers/mongodb';
import { after, before, beforeEach, describe, it } from 'node:test';
import { v4 as uuid } from 'uuid';
import {
shoppingCartShortInfoProjection,
shoppingCartShortInfoProjectionName,
} from '.';
import { ShoppingCartId, type ShoppingCartEvent } from '../shoppingCart';

void describe('Shopping Cart Short Info Projection', () => {
let mongodb: StartedMongoDBContainer;
let connectionString: string;
let given: MongoDBInlineProjectionSpec<ShoppingCartId, ShoppingCartEvent>;
let shoppingCartId: ShoppingCartId;
let clientId: string;
const now = new Date();

before(async () => {
mongodb = await new MongoDBContainer().start();
connectionString = mongodb.getConnectionString();

given = MongoDBInlineProjectionSpec.for({
projection: shoppingCartShortInfoProjection,
connectionString,
clientOptions: {
directConnection: true,
},
});
});

beforeEach(() => {
clientId = uuid();
shoppingCartId = ShoppingCartId(clientId);
});

after(async () => {
try {
await mongodb.stop();
} catch (error) {
console.log(error);
}
});

void it('adds product to empty shopping cart', () =>
given({ streamName: shoppingCartId, events: [] })
.when([
{
type: 'ProductItemAddedToShoppingCart',
data: {
shoppingCartId,
clientId,
productItem: { unitPrice: 100, productId: 'shoes', quantity: 100 },
addedAt: now,
},
metadata: {
clientId,
},
},
])
.then(
expectInlineReadModel
.withName(shoppingCartShortInfoProjectionName)
.toHave({
productItemsCount: 100,
totalAmount: 10000,
}),
));
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const getShortInfoById = (
});

export const shoppingCartShortInfoProjection = mongoDBInlineProjection({
name: shoppingCartShortInfoProjectionName,
evolve,
canHandle: [
'ProductItemAddedToShoppingCart',
Expand Down
Loading

0 comments on commit a5fc62b

Please sign in to comment.