Created
July 21, 2025 17:23
-
-
Save jessekelly881/4a27613b0c476793259386a1c885bae8 to your computer and use it in GitHub Desktop.
Example of Domain Driven Design using Effect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Context, DateTime, Effect, Schema, PubSub } from "effect"; | |
| export const OrderId = Schema.String.pipe(Schema.brand("OrderId")); | |
| export type OrderId = typeof OrderId.Type; | |
| export class Order extends Schema.Class<Order>("Order")({ | |
| id: OrderId, | |
| }) {} | |
| export class OrderPlacedEvent extends Schema.Class<OrderPlacedEvent>( | |
| "OrderPlacedEvent", | |
| )({ | |
| id: OrderId, | |
| placedAt: Schema.DateTimeUtc, | |
| }) {} | |
| export const OrderEvent = Schema.Union(OrderPlacedEvent); | |
| export type OrderEvent = typeof OrderEvent.Type; | |
| export class OrderPersistence extends Context.Tag("OrderPersistence")< | |
| OrderPersistence, | |
| { | |
| save: (order: Order) => Effect.Effect<void>; | |
| } | |
| >() {} | |
| export class OrderService extends Effect.Service<OrderService>()( | |
| "@/OrderService", | |
| { | |
| effect: Effect.gen(function* () { | |
| const orderPersistence = yield* OrderPersistence; | |
| const eventPubSub = yield* PubSub.unbounded<OrderEvent>(); | |
| return { | |
| subscribe: eventPubSub.subscribe, | |
| placeOrder: (order: Order) => | |
| Effect.gen(function* () { | |
| const now = yield* DateTime.now; | |
| yield* orderPersistence.save(order); | |
| const event = new OrderPlacedEvent({ | |
| id: order.id, | |
| placedAt: now, | |
| }); | |
| yield* eventPubSub.publish(event); | |
| return event; | |
| }), | |
| }; | |
| }), | |
| }, | |
| ) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment