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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@brainhubeu/sqrs-inversify

v1.0.3

Published

InversifyJS helpers for sqrs package

Downloads

40

Readme

sqrs-inversify

This package provides helpers for using sqrs with InversifyJS library.

Usage

SQRS Module

SQRS module registers all handler providers and default implementation of the buses.

import { Container } from 'inversify';
import { commandBusId, CommandBus } from '@brainhubeu/sqrs';
import { SQRSModule } from '@brainhubeu/sqrs-inversify';

const container = new Container();

container.load(SQRSModule);

// Will resolve to DefaultCommandBus
const commandBus = container.get<CommandBus>(commandBusId);

ContainerBuilder

ContainerBuilder is a helper class that allows you to use InversifyJS without decorators in type safe manner.

Example usage of ContainerBuilder:

import {
  CommandHandler,
  eventBusId,
  EventBus,
  Dependencies,
} from '@brainhubeu/sqrs';
import { ContainerBuilder } from '@brainhubeu/sqrs-inversify';

// Assuming CreateNoteCommand, NoteRepository are declared
class CreateNoteCommandHandler implements CommandHandler<CreateNoteCommand> {
  static type = CommandTypes.CreateNote;

  static dependencies: Dependencies<typeof CreateNoteCommandHandler> = [
    eventBusId,
    noteRepositoryId,
  ]

  constructor (
    private readonly eventBus: EventBus,
    private readonly noteRepository: NoteRepository,
  ) {
  }

  async handle (command: CreateNoteCommand) {
    const note = new Note(command.text);

    await this.noteRepository.save(note);

    this.eventBus.raise(
      noteCreatedEvent(note)
    )
  }
}

const container = new Container();
const builder = new ContainerBuilder(container);

builder.registerCommandHandler(CreateNoteCommandHandler);

// Or using a module
const module = new ContainerModule(bind => {
  const builder = new ContainerBuilder(bind);

  builder.registerCommandHandler(CreateNoteCommandHandler);
})

It can be used with classes that have static field called dependencies, e.g.:

class Foo {
  static dependencies = [];
}

The type of dependencies should be Dependencies imported from sqrs module. This type reflects arguments of the constructor and requires correct order and number of identifiers of type DependencyIdentifier<T>, e.g.:

import {
  Dependencies,
  depId,
} from '@brainhubeu/sqrs';

type Foo = {};
type Baz = {};

const bazId = depId<Baz>('Baz')
const fooId = depId<Foo>('Foo')

class Bar {
  static dependencies: Dependencies<typeof Bar> = [
    fooId,
    bazId,
  ]

  constructor (
    foo: Foo,
    baz: Baz,
  ) {
  }
}

When you change the order of constructor arguments or their types, dependencies will cause an error guarding the types that are injected.

To register a type that has dependencies static field use registerType method.

import { Container } from 'inversify';
import { depId } from '@brainhubeu/sqrs';
import { ContainerBuilder } from '@brainhubeu/sqrs-inversify';

const container = new Container();
const builder = new ContainerBuilder(container);

const barId = depId<Bar>('Bar');

builder.registerType(barId, Bar);

ContainerBuilder exposes methods that allow for easy registration of:

  • command handlers
  • query handlers
  • event handlers

To register a CommandHandler implementation add static fields dependencies and type to the class. The type field represents the type of command that this command handler handles, e.g.

class CreateNoteCommandHandler implements CommandHandler<CreateNoteCommand> {
  static type = CommandTypes.CreateNote;
  static dependencies: Dependencies<typeof CreateNoteCommandHandler> = [

  ];

  constructor () {
  }

  ...
}

/*
 * same as 
 * containerBuilder.registerType(
 *   commandHandlerId(CommandTypes.CreateNote),
 *   CreateNoteCommandHandler
 * )
 */
containerBuilder.registerCommandHandler(CreateNoteCommandHandler)

To register a QueryHandler implementation add static fields dependencies and queryName to the class. The queryName field represents the name of query that this handler handles, e.g.

class GetNotesQueryHandler implements QueryHandler<GetNotesQuery> {
  static queryName = QueryNames.GetNote;
  static dependencies: Dependencies<typeof GetNotesQueryHandler> = [

  ];

  constructor () {
  }

  ...
}

/*
 * same as 
 * containerBuilder.registerType(
 *   queryHandlerId(QueryNames.GetNotes),
 *   GetNotesQueryHandler
 * )
 */
containerBuilder.registerQueryHandler(GetNotesQueryHandler)

To register an EventHandler implementation add static fields dependencies and type to the class. The type field represents the type of event that this handler handles or an array of types that this handler handles, e.g.

class NoteCreatedEventHandler implements EventHandler<NoteCreatedEvent> {
  // Could also be [ EventTypes.NoteCreated ];
  static type = EventTypes.NoteCreated;
  static dependencies: Dependencies<typeof GetNotesQueryHandler> = [

  ];

  constructor () {
  }

  ...
}

/*
 * same as calling following for each event type
 * containerBuilder.registerType(
 *   eventHandlerId(eventType),
 *   NoteCreatedEventHandler
 * )
 */
containerBuilder.registerEventHandler(NoteCreatedEventHandler)