Skip to content
@tde.io/plexis  ·  npm install @tde.io/plexis  ·  npm  ·  GitHub

Plexis

Zero-dependency TypeScript library for modeling business state with domain state machines and finite workflow pipelines.

Install Plexis:

Terminal window
npm install @tde.io/plexis

Define a pipeline and domain, then drive the domain forward:

import { defineDomain, definePipeline, when, on, target, pipeline, node, action, fork, terminal } from '@tde.io/plexis';
type OrderContext = { cardValid: boolean };
// Pipeline: runs once per invocation — validate then route to charge or decline
const payment = definePipeline<OrderContext>('payment', () => {
node('validate', () => {
fork('card-ok', target('charge'), (ctx) => ctx.cardValid);
fork('card-invalid', target('decline'), (ctx) => !ctx.cardValid);
});
node('charge', terminal());
node('decline', terminal());
return { initial: 'validate' };
});
// Domain: durable order state — pending → processing → fulfilled
const order = defineDomain<OrderContext>('order', () => {
when('pending', () => {
on('submit', () => { pipeline(payment); return target('processing'); });
});
when('processing', () => {
on('fulfill', target('fulfilled'));
});
when('fulfilled', terminal());
return { context: { cardValid: true }, initial: 'pending' } as const;
});
// Drive the domain forward
const result = await order.follow('submit');
console.log(result.status); // 'followed'
console.log(result.to); // 'processing'

Plexis separates business logic into two complementary layers:

Domain — durable state that persists across time. A domain has named states, flows triggered by events, optional guards, lifecycle hooks (enter/exit), and can invoke pipelines on flows. Define one with defineDomain() and advance it with domain.follow(event).

Pipeline — a finite, single-run workflow. Execution starts at the initial node and follows the first matching fork at each step until it reaches a terminal node. Define one with definePipeline() and run it with pipeline.run(context).

The two layers compose naturally: a flow on a domain can invoke a pipeline, so workflow logic (validation, enrichment, side effects) lives in the pipeline while the domain tracks the resulting state.

Plexis has no runtime dependencies. It runs in Node.js ≥ 19, modern browsers, and serverless environments without polyfills.