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

@ceoro9/ioc-ts

v1.0.6

Published

Inversion of control container on TypeScript

Downloads

3

Readme

ioc-ts

Build Status Coverage Status

Inversion of control container on TypeScript

Installing

npm install @ceoro9/ioc-ts reflect-metadata --save

About

ioc-ts is an implementation of inversion on control (IoC) container on TypeScript, that encourages declarational approarch, using decorators and reflection to comfortably and transperrently manage entities and their dependencies.

Usage

This short example just shows the basics, what ioc-ts container is really capable of. Please check out documetation, that will be avaiable soon, to find out more about more sophisticated functionality ioc-ts can offer you.

Step 0: Configure TypeScript compiler

{
  "compilerOptions": {
    ...
    "types": ["reflect-metadata"],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Step 1: Imports

import 'reflect-metadata';
import { Injectable, Inject, Container } from '@ceoro9/ioc-ts';

Step 2: Declare your data types and service interfaces

interface IPerson {
  id:        string;
  firstName: string;
  lastName:  string;
}

interface IPersonService {
  createPerson(person: Omit<IPerson, 'id'>): IPerson;
  getPersonById(personId: string): IPerson | undefined;
}

Step 3: Declare your injectable entities, which implement the above service interface

In our example we have two services, which implement IPersonService interface. The first one is PostgreSQL service and second one is MongoDB. Each one corresponds to the database, that it uses under the hood.

const PERSON_POSTGRES_SERVICE_NAME = 'PERSON_POSTGRES_SERVICE';
const PERSON_MONGO_SERVICE_NAME    = 'PERSON_MONGO_SERVICE';

@Injectable(PERSON_POSTGRES_SERVICE_NAME)
class PersonPostgresService implements IPersonService {

  private readonly db: { [id: string]: IPerson | undefined };

  public constructor() {
    this.db = {};
  }

  public createPerson(person: IPerson) {
    const personId = Math.random().toString(36).substring(7);
    this.db[personId] = person;
    return {
      ...person,
      id: personId,
    };
  }

  public getPersonById(personId: string) {
    return this.db[personId];
  }
}

@Injectable(PERSON_MONGO_SERVICE_NAME)
class PersonMongoService implements IPersonService {

  private readonly db: Map<string, IPerson>;

  public constructor() {
    this.db = new Map();
  }

  public createPerson(person: IPerson) {
    const personId = Math.random().toString(36).substring(10);
    this.db.set(personId, person);
    return {
      ...person,
      id: personId,
    };
  }

  public getPersonById(personId: string) {
    return this.db.get(personId);
  }
}

Step 4: Make an injection

And now we have a person controller, which wants to use some service to persist data, but don't want to know implementation details of how this data is persisted and stored. In other words, our person controller should depend upon service interface, but not its concrete implementation. That's why the type of personService property is IPersonService interface. But anyway, we should specify which concrete implementation of service interface personService should take from container. This is where we should make an injection by specifying entity name, which our controller should use. In our case this is property injection by PERSON_POSTGRES_SERVICE_NAME identifier. So in fact, our service will use PostgreSQL service to persist data, but it has no idea about this.

@Injectable()
class PersonController {

  @Inject(PERSON_POSTGRES_SERVICE_NAME)
  private personService: IPersonService;
  
  public get(personId: string) {
    const person = this.personService.getPersonById(personId);
    return (
      person
      ? { status: 200, data: person }
      : { status: 404, data: null }
    );
  }

  public post(personData: Omit<IPerson, 'id'>) {
    const person = this.personService.createPerson(personData);
    return {
      status: 201,
      data: person,
    };
  }
}

Step 5: Obtain reference to global container, where all your injectable entities are stored by default

const container = Container.getGlobal();

Step 6: Get your entities with their resolved dependencies

So you're done. Now you can safely and comfortably work with all of your entities. IoC container will take care of resolving and initializing of their dependencies.

const personController = container.get(PersonController);

const { data: { id: personId } } = personController.post({
  firstName: 'firstName0',
  lastName: 'lastName0',
}); // { status: 201, data: { ... } }
personController.get(personId); // { status: 200, data: { ... } }

License

This project is licensed under the MIT license.