|
| 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 | +})(); |
0 commit comments