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

ddd-kit

v0.0.28

Published

A toolkit for Domain-Driven Design in TypeScript.

Downloads

208

Readme

ddd-kit: A Pragmatic DDD Toolkit

A small collection of sharp, focused tools for taming complex business logic in TypeScript. This isn't a framework; it's a set of patterns, inspired by the Domain-Driven Design (DDD) philosophy, to help you build robust, maintainable, and testable applications.

Philosophy

This kit is built on a few core beliefs: your domain logic is your most valuable asset, persistence is a detail, and testability is a core feature, not an afterthought.

In small, fast-moving teams, our code should directly model the business domain, using the same terminology our experts use. This creates a Ubiquitous Language that bridges the gap between technical implementation and business reality.


Core Building Blocks

This kit provides a set of composable primitives to structure your application:

  • AggregateRoot: A base class for your domain models that guards business rules (invariants) and manages state changes.
  • ICommand & CommandHandler: A pattern to encapsulate every business operation into a clean, transactional pipeline (load -> execute -> save).
  • Result<T, E>: A simple ok or err wrapper to handle outcomes without throwing exceptions.
  • UnitOfWork: An abstraction to ensure that all database operations for a command succeed or fail together (atomicity).
  • AggregateRepository: A generic port for loading and saving your aggregates, decoupling your domain from the database.
  • withIdempotency: A wrapper to make your command handlers safe to retry, preventing duplicate operations.
  • HTTP Helpers: Utilities (makeRequestHandler, respond) to standardize auth, validation, and response formatting at the API edge.

A 30-Second Tour

Here's a glimpse of how the pieces fit together.

1. Define a rule and a state transition in your Aggregate:

class Order extends AggregateRoot {
  // The command method makes a decision. 
  cancel(reason: string) {
    if (this.status === 'SHIPPED') {
      throw new DomainInvariantError('Cannot cancel a shipped order.');
    } 
    // It raises an event, but doesn't change state itself.
    this.raise({ type: 'OrderCancelled', data: { orderId: this.id, reason } });
  }

  // The apply method enacts the state change.
  protected apply(event: DomainEvent): void {
    if (event.type === 'OrderCancelled') {
      this.status = 'CANCELLED';
    }
  }
}

2. Create a Command for the use case:

class CancelOrderCommand implements ICommand<{ reason: string }, { ok: boolean }, Order> {
****  execute(payload: { reason: string }, aggregate: Order) {
    // The command calls the aggregate's public method.
    aggregate.cancel(payload.reason);
    return ok({ aggregate, response: { ok: true }, events: aggregate.pullEvents() });
  }
}

3. The CommandHandler runs the operation in a transaction:

const handler = new OrderCommandHandler(/* ...dependencies... */);

// This entire operation is atomic and safe.
const result = await handler.execute('cancel-order', {
  aggregateId: 'order_123',
  payload: { reason: 'Customer changed their mind.' },
}); 

A Unified Model for Any Persistence

ddd-kit uses a "Raise/Apply" pattern within its AggregateRoot to keep your business logic pure and persistence-agnostic.

  • Command Methods raise Events: A public method like cancel() validates the request and raises an event describing what should happen. It doesn't change the state itself.
  • The apply() Method Changes State: A single, protected apply() method is the only place state can be mutated. It acts like a reducer, changing state based on the event that was raised.

This simple but powerful pattern means the same domain model works seamlessly whether you're using:

  • CRUD: Rehydrating from a database row and saving the full, updated state.
  • Event Sourcing: Rehydrating by replaying an event history and saving only the new event.

Your core business logic remains identical, completely decoupled from the infrastructure.


Dive Deeper: The Guides

This toolkit is small, but the concepts are powerful. To get the most out of it, please read through the guides.

What's Next for ddd-kit?

This toolkit is actively under development and will continue to evolve. Here are some of the improvements on the horizon:

Event Sourcing Support: While the AggregateRoot is already capable of producing domain events, we will be introducing an AbstractEsRepository. This will provide a clear pattern for teams who want to adopt an event-sourcing persistence strategy without changing their domain logic.

Read-Side (CQRS) Helpers: So far, we've focused heavily on the "Command" side of CQRS—making sure writes are safe, transactional, and clear. The next major focus will be on the "Query" side. We plan to add helpers and patterns for building efficient read models through projections, materialized views, and dedicated query handlers.