npm.io
0.2.0 • Published 6d ago

@stateledger/memory

Licence
MIT
Version
0.2.0
Deps
1
Size
40 kB
Vulns
0
Weekly
11
Stars
1

@stateledger/memory

npm

In-memory adapter for stateledger. Use it for tests, hello-world demos, and prototyping — not production. State is lost on process exit.

Install

pnpm add @stateledger/core @stateledger/memory

Use

import { defineMachine } from "@stateledger/core";
import { InMemoryAdapter } from "@stateledger/memory";

const PaymentMachine = defineMachine({
  name: "payment",
  states: ["pending", "authorized", "captured", "settled"],
  initialState: "pending",
  transitions: [
    { from: "pending",    to: "authorized" },
    { from: "authorized", to: "captured" },
    { from: "captured",   to: "settled" },
  ],
} as const);

const adapter = new InMemoryAdapter();
const machine = PaymentMachine.for("payment-1", { adapter });

await machine.transitionTo("pending");      // bootstrap
await machine.transitionTo("authorized");
await machine.transitionTo("captured");

console.log(await machine.history());
// [
//   { fromState: null,         toState: "pending",    sortKey: 1, ... },
//   { fromState: "pending",    toState: "authorized", sortKey: 2, ... },
//   { fromState: "authorized", toState: "captured",   sortKey: 3, ... },
// ]

When to use this

  • Unit tests in user code. Spin up a fresh adapter per test, no DB setup.
  • Hello-world demos in documentation or tutorials.
  • Prototyping an API design before wiring up real persistence.

When NOT to use it

  • Anything where you'd be sad if a server restart wiped the state.

For production, use @stateledger/prisma (Postgres-backed) or another persistent adapter.

License

MIT

Keywords