The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | --- |
| 2 | title: Domain |
| 3 | description: A domain-driven design framework for Effect. Model your business as commands, aggregates, events, views, projections and policies, test it in memory, and run it on Cloudflare Durable Objects. |
| 4 | --- |
| 5 | |
| 6 | import DAG from "../../../components/DAG.astro"; |
| 7 | |
| 8 | `@alchemy.run/domain` models your business in its own terms. Commands ask for changes, aggregates decide them, and events record what happened. Views, projections and policies then answer questions, feed your own stores, and apply the business's rules about what to do next. |
| 9 | |
| 10 | The same model runs in memory for tests and on Cloudflare Durable Objects in production, with one ordered partition per key. |
| 11 | |
| 12 | <DAG |
| 13 | nodes={[ |
| 14 | { id: "commands", label: "Commands", type: "change something", kind: "command", col: 0, row: 0 }, |
| 15 | { id: "aggregates", label: "Aggregates", type: "decide", kind: "aggregate", col: 1, row: 0 }, |
| 16 | { id: "events", label: "Events", type: "the record", kind: "event", col: 2, row: 0 }, |
| 17 | { id: "views", label: "Views", type: "answer reads", kind: "view", col: 1, row: 1 }, |
| 18 | { id: "queries", label: "Queries", type: "read the views", kind: "query", col: 0, row: 1 }, |
| 19 | ]} |
| 20 | edges={[ |
| 21 | { from: "commands", to: "aggregates" }, |
| 22 | { from: "aggregates", to: "events" }, |
| 23 | { from: "events", to: "views", dashed: true }, |
| 24 | { from: "views", to: "queries" }, |
| 25 | ]} |
| 26 | /> |
| 27 | |
| 28 | ## A taste |
| 29 | |
| 30 | A command declares what it asks for and every way it can end: |
| 31 | |
| 32 | ```typescript title="src/DepositMoney.command.ts" |
| 33 | export class MoneyDeposited extends Domain.Event("MoneyDeposited", { |
| 34 | amount: Schema.Int, |
| 35 | balanceAfter: Schema.Int, |
| 36 | }) {} |
| 37 | |
| 38 | export class DepositMoney extends Domain.Command("DepositMoney", { |
| 39 | target: AccountId, |
| 40 | input: { amount: Schema.Int }, |
| 41 | events: [MoneyDeposited], |
| 42 | rejects: [NotOpen], |
| 43 | reply: { balance: Schema.Int }, |
| 44 | }) {} |
| 45 | ``` |
| 46 | |
| 47 | The account aggregate decides it against its current state: |
| 48 | |
| 49 | ```typescript title="src/Account.aggregate.ts" |
| 50 | DepositMoney: (s, cmd) => |
| 51 | s._tag !== "Open" |
| 52 | ? new NotOpen() |
| 53 | : [new MoneyDeposited({ amount: cmd.amount, balanceAfter: s.balance + cmd.amount })], |
| 54 | ``` |
| 55 | |
| 56 | And a story tests it in memory, in the business's own words: given this history, when this command, then these events. |
| 57 | |
| 58 | ```typescript title="test/Account.test.ts" |
| 59 | it.effect("deposits into an open account and replies with the balance", () => |
| 60 | story( |
| 61 | Story.given(account, new AccountOpened({ owner: "sam" })), |
| 62 | Story.when(account, new DepositMoney({ amount: 100 })), |
| 63 | Story.then(account, new MoneyDeposited({ amount: 100, balanceAfter: 100 })), |
| 64 | Story.replied({ balance: 100 }), |
| 65 | ), |
| 66 | ); |
| 67 | ``` |
| 68 | |
| 69 | ## Build it with your coding agent |
| 70 | |
| 71 | The `alchemy-domain` skill teaches a coding agent how to turn requirements into a model, lay out the project, test it with stories, host it on Cloudflare and drive it from foldkit. |
| 72 | |
| 73 | Ship it with your dependencies, so the skill always matches the alchemy version you have installed: |
| 74 | |
| 75 | ```sh |
| 76 | bun add -d skills-npm |
| 77 | bunx skills-npm setup |
| 78 | ``` |
| 79 | |
| 80 | `setup` adds a `prepare` script. Every install then links the skills your packages ship into each coding agent's skills folder. |
| 81 | |
| 82 | Or install it from GitHub, for any agent: |
| 83 | |
| 84 | ```sh |
| 85 | npx skills add alchemy-run/alchemy --skill alchemy-domain |
| 86 | ``` |
| 87 | |
| 88 | In Claude Code, it's also a plugin: |
| 89 | |
| 90 | ```text |
| 91 | /plugin install alchemy --marketplace alchemy-run/alchemy |
| 92 | ``` |
| 93 | |
| 94 | A skill installed from GitHub follows the repository's main branch, so update it when you upgrade alchemy. |
| 95 | |
| 96 | ## Where to start |
| 97 | |
| 98 | **New to domain-driven design?** [What is Domain-Driven Design?](/domain/what-is-ddd) introduces the ideas one at a time, with the bank as the example. |
| 99 | |
| 100 | **Want to build one?** The [tutorial](/domain/tutorial/part-1) builds a bank step by step, from a single account on your machine to an Api deployed on Cloudflare with a browser app. |
| 101 | |
| 102 | **Want the whole picture?** The concepts read as a manual, one building block per chapter, starting with [Domain Model](/domain/model) and ending with [Story](/domain/story). Each chapter explains the idea, how to find it in your business, and how to write it: [Command](/domain/command), [Event](/domain/event), [Aggregate](/domain/aggregate), [View](/domain/view), [Projection](/domain/projection), [Stream](/domain/stream), [Policy](/domain/policy), [Saga](/domain/saga), [External System](/domain/external-system) and [Api](/domain/api). |