|
| 1 | +import type { Event } from '@event-driven-io/emmett'; |
| 2 | +import { |
| 3 | + PostgreSqlContainer, |
| 4 | + StartedPostgreSqlContainer, |
| 5 | +} from '@testcontainers/postgresql'; |
| 6 | +import { after, before, beforeEach, describe, it } from 'node:test'; |
| 7 | +import { v4 as uuid } from 'uuid'; |
| 8 | +import { |
| 9 | + expectPongoDocuments, |
| 10 | + pongoSingleStreamProjection, |
| 11 | + PostgreSQLProjectionSpec, |
| 12 | +} from '.'; |
| 13 | +import type { |
| 14 | + DiscountApplied, |
| 15 | + PricedProductItem, |
| 16 | +} from '../../testing/shoppingCart.domain'; |
| 17 | + |
| 18 | +export type ProductItemAdded = Event< |
| 19 | + 'ProductItemAdded', |
| 20 | + { productItem: PricedProductItem; shoppingCartId: string } |
| 21 | +>; |
| 22 | + |
| 23 | +void describe('Postgres Projections', () => { |
| 24 | + let postgres: StartedPostgreSqlContainer; |
| 25 | + let connectionString: string; |
| 26 | + let given: PostgreSQLProjectionSpec<ProductItemAdded | DiscountApplied>; |
| 27 | + let shoppingCartId: string; |
| 28 | + let streamName: string; |
| 29 | + |
| 30 | + before(async () => { |
| 31 | + postgres = await new PostgreSqlContainer().start(); |
| 32 | + connectionString = postgres.getConnectionUri(); |
| 33 | + |
| 34 | + given = PostgreSQLProjectionSpec.for({ |
| 35 | + projection: shoppingCartShortInfoProjection, |
| 36 | + connectionString, |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + shoppingCartId = uuid(); |
| 42 | + streamName = `shoppingCart:${shoppingCartId}`; |
| 43 | + }); |
| 44 | + |
| 45 | + after(async () => { |
| 46 | + try { |
| 47 | + await postgres.stop(); |
| 48 | + } catch (error) { |
| 49 | + console.log(error); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + void it('uses custom document id instead of stream name assigned in projection evolve', () => |
| 54 | + given([]) |
| 55 | + .when([ |
| 56 | + { |
| 57 | + type: 'ProductItemAdded', |
| 58 | + data: { |
| 59 | + productItem: { price: 100, productId: 'shoes', quantity: 100 }, |
| 60 | + shoppingCartId, |
| 61 | + }, |
| 62 | + metadata: { |
| 63 | + streamName, |
| 64 | + }, |
| 65 | + }, |
| 66 | + ]) |
| 67 | + .then( |
| 68 | + expectPongoDocuments |
| 69 | + .fromCollection<ShoppingCartShortInfo>( |
| 70 | + shoppingCartShortInfoCollectionName, |
| 71 | + ) |
| 72 | + .withId(shoppingCartId) |
| 73 | + .toBeEqual({ |
| 74 | + _id: shoppingCartId, |
| 75 | + productItemsCount: 100, |
| 76 | + totalAmount: 10000, |
| 77 | + }), |
| 78 | + )); |
| 79 | +}); |
| 80 | + |
| 81 | +type ShoppingCartShortInfo = { |
| 82 | + _id?: string; |
| 83 | + productItemsCount: number; |
| 84 | + totalAmount: number; |
| 85 | +}; |
| 86 | + |
| 87 | +const shoppingCartShortInfoCollectionName = 'shoppingCartShortInfo'; |
| 88 | + |
| 89 | +const evolve = ( |
| 90 | + document: ShoppingCartShortInfo, |
| 91 | + { type, data: event }: ProductItemAdded, |
| 92 | +): ShoppingCartShortInfo => { |
| 93 | + switch (type) { |
| 94 | + case 'ProductItemAdded': |
| 95 | + return { |
| 96 | + ...document, |
| 97 | + _id: event.shoppingCartId, |
| 98 | + totalAmount: |
| 99 | + document.totalAmount + |
| 100 | + event.productItem.price * event.productItem.quantity, |
| 101 | + productItemsCount: |
| 102 | + document.productItemsCount + event.productItem.quantity, |
| 103 | + }; |
| 104 | + default: |
| 105 | + return document; |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +const shoppingCartShortInfoProjection = pongoSingleStreamProjection({ |
| 110 | + collectionName: shoppingCartShortInfoCollectionName, |
| 111 | + evolve, |
| 112 | + getDocumentId: (event) => event.data.shoppingCartId, |
| 113 | + canHandle: ['ProductItemAdded'], |
| 114 | + initialState: () => ({ |
| 115 | + productItemsCount: 0, |
| 116 | + totalAmount: 0, |
| 117 | + }), |
| 118 | +}); |
0 commit comments