Skip to content

Commit b18ec56

Browse files
authored
Merge pull request #120 from dolittle/fix-samples
Fix samples and upgrade to released contracts
2 parents edb6138 + 8058a1c commit b18ec56

23 files changed

Lines changed: 164 additions & 44 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Dolittle. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
module.exports = {
5+
extends: '../../.eslintrc.js',
6+
rules: {
7+
'no-restricted-globals': 'off',
8+
'@typescript-eslint/naming-convention' : 'off',
9+
'jsdoc/require-jsdoc': 'off',
10+
}
11+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Dolittle. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
import { convertToMongoDB, copyProjectionToMongoDB, on, projection, MongoDBConversion, ProjectionContext } from '@dolittle/sdk.projections';
5+
6+
import { DishPrepared } from './DishPrepared';
7+
8+
@projection('98f9db66-b6ca-4e5f-9fc3-638626c9ecfa')
9+
@copyProjectionToMongoDB()
10+
export class DishCounter {
11+
12+
name: string = 'Unknown';
13+
14+
numberOfTimesPrepared: number = 0;
15+
16+
@convertToMongoDB(MongoDBConversion.Date)
17+
lastPrepared: Date = new Date(0);
18+
19+
@on(DishPrepared, _ => _.keyFromProperty('Dish'))
20+
on(event: DishPrepared, projectionContext: ProjectionContext) {
21+
this.name = event.Dish;
22+
this.numberOfTimesPrepared ++;
23+
this.lastPrepared = projectionContext.eventContext.occurred.toJSDate();
24+
}
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) Dolittle. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
import { eventType } from '@dolittle/sdk.events';
5+
6+
@eventType('1844473f-d714-4327-8b7f-5b3c2bdfc26a')
7+
export class DishPrepared {
8+
constructor(readonly Dish: string, readonly Chef: string) {}
9+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Dolittle. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
// Sample code for the tutorial at https://dolittle.io/docs/tutorials/projections/
5+
6+
import { DolittleClient } from '@dolittle/sdk';
7+
import { TenantId } from '@dolittle/sdk.execution';
8+
import { setTimeout } from 'timers/promises';
9+
import { DateTime } from 'luxon';
10+
11+
import { DishCounter } from './DishCounter';
12+
import { DishPrepared } from './DishPrepared';
13+
14+
(async () => {
15+
const client = await DolittleClient
16+
.setup()
17+
.connect();
18+
19+
const eventStore = client.eventStore.forTenant(TenantId.development);
20+
21+
await eventStore.commit(new DishPrepared('Bean Blaster Taco', 'Mr. Taco'), 'Dolittle Tacos');
22+
await eventStore.commit(new DishPrepared('Bean Blaster Taco', 'Mrs. Tex Mex'), 'Dolittle Tacos');
23+
await eventStore.commit(new DishPrepared('Avocado Artillery Tortilla', 'Mr. Taco'), 'Dolittle Tacos');
24+
await eventStore.commit(new DishPrepared('Chili Canon Wrap', 'Mrs. Tex Mex'), 'Dolittle Tacos');
25+
26+
await setTimeout(1000);
27+
28+
const db = await client.resources.forTenant(TenantId.development).mongoDB.getDatabase();
29+
const dishCounterCollection = db.collection(DishCounter);
30+
31+
for (const { name, numberOfTimesPrepared, lastPrepared } of await dishCounterCollection.find().toArray()) {
32+
client.logger.info(`The kitchen has prepared ${name} ${numberOfTimesPrepared} times. The last time was ${lastPrepared}`);
33+
}
34+
35+
const dishesPreparedToday = await dishCounterCollection.find({ lastPrepared: { $gte: DateTime.utc().startOf('day').toJSDate(), $lte: DateTime.utc().endOf('day').toJSDate() }}).toArray();
36+
for (const { name } of await dishCounterCollection.find().toArray()) {
37+
client.logger.info(`The kitchen has prepared ${name} today`);
38+
}
39+
})();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "mongodb-projections",
3+
"private": true,
4+
"version": "22.2.0-sam.4",
5+
"main": "index.js",
6+
"author": "Dolittle",
7+
"license": "MIT",
8+
"scripts": {
9+
"watch": "nodemon --watch **/*.ts --exec 'ts-node' index.ts",
10+
"start": "ts-node index.ts",
11+
"test": "mocha",
12+
"build": "tsc -p ./tsconfig.json"
13+
},
14+
"dependencies": {
15+
"@dolittle/sdk": "22.2.0-sam.4",
16+
"@dolittle/sdk.artifacts": "22.2.0-sam.4",
17+
"@dolittle/sdk.events": "22.2.0-sam.4",
18+
"@dolittle/sdk.events.handling": "22.2.0-sam.4",
19+
"inversify": "^6.0.1",
20+
"reflect-metadata": "^0.1.13",
21+
"winston": "^3.3.4"
22+
},
23+
"devDependencies": {
24+
"nodemon": "^2.0.4",
25+
"ts-node": "^8.10.1"
26+
}
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"extends": "../../tsconfig.settings.json",
3+
"compilerOptions": {
4+
"sourceRoot": "../",
5+
"inlineSourceMap": true,
6+
"sourceMap": false,
7+
"emitDecoratorMetadata": true,
8+
"experimentalDecorators": true,
9+
"outDir": "Distribution"
10+
},
11+
"include": ["**/*.ts"],
12+
"exclude": ["node_modules", "Distribution"],
13+
"references": [
14+
{ "path": "../../Source/sdk" }
15+
]
16+
}

Samples/Tutorials/Projections/Chef.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
export class Chef {
77
constructor(
88
public name: string = '',
9-
public dishes: string[] = [],
10-
public lastPreparedDish: Date = new Date(0),
9+
public dishes: string[] = []
1110
) { }
1211
}

Samples/Tutorials/Projections/DishCounter.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33

44
// Sample code for the tutorial at https://dolittle.io/tutorials/projections/typescript/
55

6-
import { ProjectionContext, projection, on, copyProjectionToMongoDB } from '@dolittle/sdk.projections';
6+
import { ProjectionContext, projection, on } from '@dolittle/sdk.projections';
77

88
import { DishPrepared } from './DishPrepared';
99

1010
@projection('98f9db66-b6ca-4e5f-9fc3-638626c9ecfa')
11-
@copyProjectionToMongoDB()
1211
export class DishCounter {
13-
1412
name: string = 'Unknown';
1513
numberOfTimesPrepared: number = 0;
1614

Samples/Tutorials/Projections/index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import { DolittleClient } from '@dolittle/sdk';
77
import { TenantId } from '@dolittle/sdk.execution';
8-
import { MongoDBConversion } from '@dolittle/sdk.projections';
98
import { setTimeout } from 'timers/promises';
109

1110
import { Chef } from './Chef';
@@ -18,12 +17,8 @@ import { DishPrepared } from './DishPrepared';
1817
.withProjections(_ => _
1918
.create('0767bc04-bc03-40b8-a0be-5f6c6130f68b')
2019
.forReadModel(Chef)
21-
.copyToMongoDB(_ => _
22-
.withConversion('lastPreparedDish', MongoDBConversion.Date)
23-
)
2420
.on(DishPrepared, _ => _.keyFromProperty('Chef'), (chef, event, projectionContext) => {
2521
chef.name = event.Chef;
26-
chef.lastPreparedDish = new Date();
2722
if (!chef.dishes.includes(event.Dish)) chef.dishes.push(event.Dish);
2823
return chef;
2924
})

Source/artifacts/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
"@dolittle/concepts": "6.0.0",
4848
"@dolittle/rudiments": "6.0.0",
4949
"@dolittle/types": "6.0.0",
50-
"@dolittle/runtime.contracts": "6.6.0-sam.3"
50+
"@dolittle/runtime.contracts": "6.6.0"
5151
},
5252
"devDependencies": {
5353
"@types/is-natural-number": "^4.0.0"
5454
}
55-
}
55+
}

0 commit comments

Comments
 (0)