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

@zudojs/cqrs

v1.0.0

Published

Command Query Responsibility Segregation (CQRS) primitives for separating read and write operations.

Downloads

349

Readme

@zudojs/cqrs

Command Query Responsibility Segregation (CQRS) primitives for separating read and write operations.

Installation

npm install @zudojs/cqrs

Quick Start

Commands and queries are plain objects with a type discriminator. Give the bus explicit generics (or a typed handler) so the payload and result types flow through register and execute.

import {
  createCommandBus,
  createQueryBus,
  createQueryHandler,
  type CommandOf,
  type QueryOf,
} from "@zudojs/cqrs";

interface User {
  id: string;
  name: string;
}

type CreateUser = CommandOf<"CreateUser", { name: string }>;
type GetUser = QueryOf<"GetUser", { id: string }>;

const commandBus = createCommandBus();
const queryBus = createQueryBus();

// Function handler with explicit generics
commandBus.register<CreateUser, User>("CreateUser", async (command) => {
  return userRepository.create({ name: command.name });
});

// Typed handler object created with a factory
queryBus.register(
  "GetUser",
  createQueryHandler<GetUser, User | undefined>("GetUser", async (query) =>
    userRepository.findById(query.id),
  ),
);

const user = await commandBus.execute<CreateUser, User>({
  type: "CreateUser",
  name: "Alice",
});

const found = await queryBus.execute<GetUser, User | undefined>({
  type: "GetUser",
  id: user.id,
});

Class-based handlers extend CommandHandler / QueryHandler; any object with an execute() method is accepted as well.

import { CommandHandler, createCommandBus } from "@zudojs/cqrs";

class CreateUserHandler extends CommandHandler<CreateUser, User> {
  readonly commandType = "CreateUser";

  async execute(command: CreateUser): Promise<User> {
    return userRepository.create({ name: command.name });
  }
}

createCommandBus().register("CreateUser", new CreateUserHandler());

Middleware

import {
  createCommandBus,
  timingMiddleware,
  errorMiddleware,
  isCqrsError,
} from "@zudojs/cqrs";

const timing = timingMiddleware({
  onTiming: ({ request, durationMs }) =>
    console.log(`${request.type} took ${durationMs.toFixed(1)}ms`),
});

const bus = createCommandBus({ middleware: [timing, errorMiddleware()] });

try {
  await bus.execute({ type: "Unknown" });
} catch (error) {
  isCqrsError(error); // true — CommandHandlerNotFoundError
}

Validation and handler resolution run at the end of the middleware pipeline, so middleware observes InvalidCommandError / CommandHandlerNotFoundError like any other failure. Each middleware may call next() at most once.

Features

  • Command bus for write operations
  • Query bus for read operations
  • Middleware pipeline for both (timing, error normalisation, validation, locking, context enrichment)
  • Function, object and class-based handlers
  • Class decorators (CommandHandlerFor, QueryHandlerFor, CqrsHandler) for handler discovery
  • Dedicated error classes (isCqrsError)
  • Result types for explicit returns
  • Execution contexts with correlation chains

Use Cases

  • Complex domain logic
  • Event-sourced systems
  • Read/write model separation
  • Audit trails and logging