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 🙏

© 2025 – Pkg Stats / Ryan Hefner

clean-cqrs

v0.0.4

Published

Simple and clean Command Query Responsibility Segregation for NodeJS ecosystem

Readme

Build Status

Simple CQRS package for NodeJS ecosystem

Command Query Responsibility Segregation for NodeJS ecosystem.
If you do not have any idea what is CQRS please read nice article created by Martin Fowler.

This package provided nothing more than simple CQRS implemented with MessageBus and possible to observe with RxJS.

Installation

npm install clean-cqrs --save

Usage

First of all you have to create own command. Command is nothing more like data transfer object without logic. For example command to register new account with provided id and name can look like below:

import { Command } from 'clean-cqrs';
class RegisterUserCommand implements Command {
    public constructor(public id: number,
                       public name: string) {
    }
}

for command must exists command handler (this should be relation one-to-one). For example registration process can look like this:

import { CommandHandler } from 'clean-cqrs';
class RegisterUserCommandHandler implements CommandHandler {
    public constructor(private userService: any) {
    }

    public async handle(command: RegisterUserCommand): Promise<boolean> {
        try {
            await userService.registerAccount({id: command.id, username: command.name});
            await userService.sendNotificationToAdmins(command.id);
            return true;
        } catch (e) {
            return false;
        }
    }
}

now we have to connect Command with CommandHandler. All connections should be created one in process lifetime and before command goes to execution

import { Bus, Command, CommandHandler } from 'clean-cqrs';
const bus = new Bus<Command, CommandHandler>();
bus.bindHandler(new RegisterUserCommandHandler(new UserService()), RegisterUserCommand);

bus.execute( new RegisterNewUserCommand(1, 'Agnieszka'))
    .then(() => console.log('Registered!'))
    .catch((e) => console.warn('Registration failed!'));

Possibilities

Command / Query interfaces

By default two interfaces group are provided and can be used:

import { Command, CommandHandler } from 'clean-cqrs'; // for commands
import { Query, QueryHandler } from 'clean-cqrs'; // for queries

Technically there is no differences between them but this separation approach simplify and organize flow in application.

Both of them extends same abstraction layer:

import { Message, MessageHandler } from 'clean-cqrs'; // for commands

Command / Query buses

You should use two different bus instances for commands and queries.

const commandBus = new Bus<Command, CommandHandler>();
const queryBus = new Bus<Query, QueryHandler>();

Observable Bus with RxJS

There is possibility to create observable Bus using RxJS.

// create directly
const observableBus = new BusRx<Command, CommandHandler>();

// or use your existing instance (types must fit)
const commandBus = new Bus<Command, CommandHandler>();
const observableBus = new BusRx<Command, CommandHandler>(commandBus);

// subscribe to bus and listen for any command flowing through the message bus
observableBus.subscribe(
    (command) => console.log(command),
    (error) => console.warn(error),
    () => console.log('stream closed')
);

Test

npm test
npm run lint

ISC License