@gbapis/qs
TypeScript icon, indicating that this package has built-in type declarations

2.0.4 • Public • Published

Dependency injection

https://github.com/microsoft/tsyringe

register.service.ts

@singleton()
export class RegistryService {
    private registry: IApiBase[];
    constructor() {
      this.registry = [];
    }

    public init() {
      // resolve all instances of controllers here
      this.registry = [
        container.resolve(Controller1Api),
        container.resolve(Controller2Api),
      ];
    }

    public registerModels() {
      // register mongoose models
      container.register<Model<Territory>>('Territory', { useValue: TerritoryModel })
      container.register<Model<Industry>>('Industry', { useValue: IndustryModel })
    }

    public connect(app: Express) {
      for (const controller of this.registry) {
        app.use(controller.baseRoute, controller.router);
        logger.info('[RegistryService]', `Registered router ${controller.baseRoute}`);
      }
    }
}

server.ts

    ...
    import { container, injectable } from "tsyringe";
    ...
    @injectable()
    class Server {
    ...
    this.registry.registerModels()
    this.registry.init();
    //this.app = express();
    ...
    // this.bindPOSIXSignals();
    this.registry.connect(this.app)

index.ts

    import "reflect-metadata";
    import { container } from 'tsyringe';
    import { Server } from './server';
    //
    // Run service with default configuration
    const app = container.resolve(Server);
    exports.default = app.up();

Readme

Keywords

none

Package Sidebar

Install

npm i @gbapis/qs

Weekly Downloads

2

Version

2.0.4

License

ISC

Unpacked Size

18.6 kB

Total Files

30

Last publish

Collaborators

  • dmariano001
  • fabriciog09
  • dpresno
  • jgcolo