Getting started
A first run
Section titled “A first run”In an empty directory, install the pipeline, the validation behavior and Zod:
Save this as create-order.mjs:
Run it:
Standard output shows the logged call, the result of the valid order and the rejection of the invalid one. The correlation id and the duration differ on every run:
logging() also reports the rejected call on standard error, as createOrder failed with
the ZodValidationError and its stack. That is the log of the failure the example
provokes; the process exits with code 0. The function returns an object and stores
nothing: there is no database, no framework and no build step.
The rest of this page takes the same pieces one at a time.
Install
Section titled “Install”The packages require Node.js 22.12 or later; @cqrs-ddd/mikro-orm requires 22.17 or
later, as MikroORM 7 does. The release check also loads every entry
point in Bun; the test suites run on Node.js only, and no other runtime is tested. The
packages are published as ES modules; a CommonJS application loads them with require().
Create a pipeline
Section titled “Create a pipeline”A pipeline holds the behavior instances and the behaviors that apply to every operation. Create it once, in a module of its own:
behaviors are the instances, one per behavior class; a behavior placed without an
instance is constructed with no arguments. globalBehaviors places behaviors around every wrapped
operation.
Wrap a function
Section titled “Wrap a function”pipeline.wrap(options, ...entries) returns a wrapper. Applied to a function, it returns a
new async function that runs the behaviors around it:
A plain function needs a name, which keys and logs use to tell operations apart, and a
kind: a query reads, a command changes state, an event reacts. Behaviors use the
kind to decide what applies; idempotency, for example, applies to commands.
Decorate a method
Section titled “Decorate a method”The same wrapper decorates a method, with TypeScript’s standard decorators or with
experimentalDecorators:
A method is named Class.method (here Prices.find) unless the options name it.
Add a behavior
Section titled “Add a behavior”Each concern is a package. Install it, give the pipeline an instance, and add its entry where it applies:
- How an operation is wrapped: names, kinds and requests.
- The plain Node.js guide: a complete, tested example.
- Packages: every behavior and its options.
- CQRS without NestJS: an application of commands, queries and
events on
@cqrs-ddd/cqrs, wired withcreateCqrs(). - NestJS & @cqrs-ddd: integrating with NestJS using
@cqrs-ddd/nestjs. - The repository’s applications: small applications that each use one package
family (the pipeline alone, the domain alone, the repositories alone), and
api/, which uses them all.