Giter Site home page Giter Site logo

shawnmclean / ts-cqs Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 136 KB

Lightweight Command and Query Separation framework for typescript

License: MIT License

JavaScript 62.19% TypeScript 37.81%
cqs cqrs-pattern typescript inversifyjs command-query command query

ts-cqs's Introduction

CircleCI Test Coverage Maintainability npm version Known Vulnerabilities

ts-cqs

Light Command and Query Separation framework for typescript that uses inversify for dependency injection.

⚠️ Note, this is not a CQRS library

Motivation

Following the single responsibility principle, the command and query separation pattern works well. This library sets out to provide a framework in to replicate the CQS pattern in typescript that uses the dependency injection framework InversifyJs.

There are CQRS libraries such as NestJS/CQRS that provides a framework for this pattern. However, it adheres more to the CQRS pattern with event sourcing and uses the bus (command/query processor) as a way to wire up the dependency injection. This library allows you to wire up the dependencies outside of the processors.

Installation

npm install ts-cqs inversify reflect-metadata --save

The InversifyJS type definitions are included in the inversify npm package.

⚠️ Important! ts-cqs requires has a peer dependency on InversifyJs, please view their readme for their requirements.

Usage

Commands

// TestCommand.ts

import 'reflect-metadata'
import { injectable } from 'inversify'
import { ICommand, ICommandHandler, CommandHandler } from 'ts-cqs'

export class TestCommand implements ICommand<string> {
  constructor(public readonly val: string) {}
}

@injectable()
@CommandHandler(TestCommand)
export class TestCommandHandler implements ICommandHandler<TestCommand> {
  async handle(command: TestCommand): Promise<string> {
    return command.val
  }
}

ICommandHandler.execute is set to return any. In this case, command handlers can chose to return something or not.

// CompositeRoot.ts

import { Container } from 'inversify'
import { TestCommandHandler } from './TestCommand'

const container = new Container()
container.bind(TestCommandHandler).toSelf()

The inversify binding here uses the handler class as the id. See docs

// SomeConsumerClass.ts

import { CommandProcessor } from 'ts-cqs'
import { TestCommand } from './TestCommand'

export class SomeConsumerClass {
  constructor(private commandProcessor: CommandProcessor) {}

  async use(): Promise<string> {
    const testCommand = new TestCommand('some value')
    const commandResult = await this.commandProcessor.execute(testCommand)

    console.log(commandResult) // logs 'some value'
  }
}

Contributing

Tests

npm run test

ts-cqs's People

Contributors

shawnmclean avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

corsega

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.