# depot-command-bus

> A light weight Command Bus for TypeScript apps.

Latest version **1.0.1** (published 2021-12-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install depot-command-bus
pnpm add depot-command-bus
yarn add depot-command-bus
bun add depot-command-bus
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2021-12-20 |
| First published | 2021-11-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Nathan King |
| Maintainers | vherus |
| Keywords | command bus, util |

## Links

- npm: https://www.npmjs.com/package/depot-command-bus
- Repository: https://github.com/vherus/depot-ts
- Homepage: https://github.com/vherus/depot-ts#readme
- Issues: https://github.com/vherus/depot-ts/issues
- npm.io page: https://npm.io/package/depot-command-bus

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2021-12-20
- 1.0.0 — 2021-11-12

## README

# Depot

**Depot** is a command bus that resolves commands to a relevant handler and then executes logic. This is very useful for decoupling your application.

Let's say you have an app where you can create Users in a database. You can create a command for that:

```ts
import { Command, Handler, Registry, Depot } from 'depot-command-bus';

class CreateUser implements Command {
    private _username: string;
    private _email: string;

    constructor(username: string, email: string) {
        this._username = username;
        this._email = email;
    }

    get username(): string { return this._username }
    get email(): string { return this._email }

    getName(): string {
        return 'CreateUser';
    }
}
```

Next, you'll create the handler that does the creation:

```ts
class CreateUserHandler implements Handler {
    private dbClient: SomeDbClient;

    async handle(command: CreateUser): Promise<any> {
        return await this.dbClient.create({
            user: {
                username: command.username,
                email: command.email
            }
        })
    }
}
```

Depot uses a `Registry` to map commands to handlers. The `Registry` takes an array of tuples, using the command name as [0] and the handler as [1]:

```ts
const registry = new Registry([
    ['CreateUser', new CreateUserHandler(yourDbClient)]
]);

const commandBus = new Depot(registry);

const cmd = new CreateUser('nathan', 'nathan@nathan.com');

const createdUser = await commandBus.dispatch(cmd);
```

---
_Source: https://npm.io/package/depot-command-bus · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
