npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

machin

v1.1.0

Published

TypeScript state machine library with first-class persistence support

Readme

machin


State machines are great for modeling complex workflows. But persisting them usually means gluing together a state machine library, a database layer, and custom sync logic.

machin handles both. Define your machine, pick a storage adapter, and your state transitions are automatically persisted. No manual saves, no sync bugs.

Features

  • Awaitable by design
  • Postgres, SQLite, and Redis adapters included
  • Full TypeScript inference for states, events, and context

Installation

npm install machin
pnpm add machin
yarn add machin

Quick example

import { machine } from "machin";
import { withDrizzlePg } from "machin/drizzle/pg";

type Context = { customerId: string | null };

// Define your machine
const orderMachine = machine<Context>().define({
  initial: "pending",
  states: {
    pending: {
      on: { confirm: { target: "processing" } },
    },
    processing: {
      entry: async (ctx, event: { customerId: string }) => {
        // Do async work here
        return { ...ctx, customerId: event.customerId };
      },
      onSuccess: { target: "completed" },
      onError: { target: "failed" },
    },
    completed: {},
    failed: {
      on: { retry: { target: "processing" } },
    },
  },
});

// Bind to storage
const boundMachine = withDrizzlePg(orderMachine, { db, table: ordersTable });

// Create an actor and send events
const actor = await boundMachine.createActor("order-123", { customerId: null });
await actor.send("confirm", { customerId: "customer-456" });

console.log(actor.state); // "completed"

State is persisted automatically after each transition.

Storage adapters

Postgres

import { withDrizzlePg } from "machin/drizzle/pg";

const machine = withDrizzlePg(machineConfig, { db, table: myTable });

SQLite

import { withDrizzleSQLite } from "machin/drizzle/sqlite";

const machine = withDrizzleSQLite(machineConfig, { db, table: myTable });

Redis

import { createClient } from "redis";
import { withRedis } from "machin/redis";

const client = await createClient({ url: "redis://localhost:6379" }).connect();
const machine = withRedis(machineConfig, { client });

Table schema

Your table needs these columns:

  • id (text, primary key)
  • state (text)
  • createdAt (timestamp)
  • updatedAt (timestamp)

Plus any columns for your context fields.

Type inference utilities

machin provides type inference utilities to extract types from your machine definitions. This is useful for typing database columns, API responses, or any other code that needs to work with machine states, events, or context.

import { machine, InferStates, InferEvents, InferContext } from "machin";

const myMachine = machine<{ count: number }>().define({
  initial: "idle",
  states: {
    idle: { on: { start: { target: "running" } } },
    running: { on: { stop: { target: "idle" } } },
  },
});

// Infer types from the machine
type States = InferStates<typeof myMachine>;   // "idle" | "running"
type Events = InferEvents<typeof myMachine>;   // "start" | "stop"
type Context = InferContext<typeof myMachine>; // { count: number }

Using with Drizzle schemas

The inference utilities are particularly useful for typing your database schema columns:

import { pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
import { InferStates } from "machin";
import { orderMachine } from "./order-machine";

// Infer the state type from your machine
type OrderState = InferStates<typeof orderMachine>;
// → "pending" | "processing" | "completed" | "failed"

export const ordersTable = pgTable("orders", {
  id: uuid().primaryKey(),
  state: text().$type<OrderState>().notNull(),
  createdAt: timestamp().notNull(),
  updatedAt: timestamp().notNull(),
});

This ensures your database schema stays in sync with your machine definition - if you add or remove states from your machine, TypeScript will catch any mismatches.

License

MIT