Ports and the functional core
Make I/O explicit so the logic stays pure and testable.
package @smitejs/domain
The functional core / ports pattern keeps business logic pure by moving every side effect — a database, a queue, an API client — behind a port. The port describes a capability; the usecase depends on the shape, not on any specific implementation.
domain.port({ name, methods }) declares a contract:
ts
const orderRepo = port({
name: "OrderRepository",
methods: ["findById", "save"],
});
const loadOrder: ReadPort<{ id: number; total: number }, number> = {
findById: async () => Promise.resolve(null),
};ts
"domain.port"tested in packages/domain/src/index.test.ts:297
A working store implements that shape and is injected at the edge:
- The usecase does not construct or import the store — it receives the implementation through its
deps. - Swapping the in-memory Map for a real database changes the wiring, not the usecase code.
- Emitting the tests inject a fake store, exercising every rule without a running service.
Repository / ReadPort / WritePort give you the standard read/write shapes when your aggregate fits the default: findById, save, add, update, remove. For anything richer, declare a custom port.
ts
const notes = { save: async () => Result.ok(undefined) };
const saveNote = usecase({
name: "saveNote",
deps: ["notes"],
handle: (deps, input) => deps.notes.save(input),
});
const runner = saveNote.with({ notes });
const result = await runner({ text: "hi" }).run();ts
truetested in packages/domain/src/index.test.ts:482