Code Monkey home page Code Monkey logo

Comments (5)

remojansen avatar remojansen commented on May 16, 2024 10

Hi guys. I'm the author of InversifyJS. Inversify allows you to inject an asynchronous factory (AKA provider). Providers are designed to inject objects with an asynchronous nature (e.g db clients).

A Provider is just a function that returns a promise:

() => Promise<T>

In the next release (InversifyJS 3.0) we are going to improve the way providers work: inversify/InversifyJS#418

I will try my best to document the integration with TypeORM 😉

from typeorm.

pleerock avatar pleerock commented on May 16, 2024

its actually better to ask inversify what to do in cases when you integrate with other libraries which does not have their annotation set. Maybe you do something wrong? May because you are setting kernel.bind<ConnectionManager>(ConnectionManager).to(ConnectionManager); it causes such error and you should not set it? As I mentioned if you are missing some feature in typedi you can propose it there and lets discuss if this can be implemented there.

from typeorm.

dreamersp avatar dreamersp commented on May 16, 2024

At WonderBits we made a skeleton TS backend( with users, roles, permissions and OAuth tokens) using typeorm and Inversify and ended up using the singleton of a DB provider class that checks if a local db connection on itself is set or not to create a new connection or just return the previously set one. This is a kind of Factory/Provider pattern into an IoC injection pattern.
We were messing up with createConnection() returning a Promise and not being sure how to handle an unique connection with Inversify's Provider pattern so we ended up doing the aforementioned.

import ConfigProvider from './ConfigProvider';
import { injectable, inject } from 'inversify';
import { Connection, createConnection } from 'typeorm';

@injectable()
export default class DatabaseProvider {
  public db: Connection;

  constructor(@inject(ConfigProvider) private configProvider: ConfigProvider) {
  }

  getDBConnection(): Promise<Connection> {
    return Promise.resolve()
      .then(()=> {
        if (!this.db) {
          let config: any = this.configProvider.config;
          return createConnection({
            driver: {
              type: config.dialect,
              host: config.host,
              port: config.port,
              database: config.database,
              username: config.username,
              password: config.password
            },
            entities: [
              __dirname + '/../modules/**/*.model.ts'
            ],
            autoSchemaSync: true,
          });
        }
      })
      .then((db) => {
        if (!this.db && db) {
          this.db = db;
        }
        return this.db;
      })
      .catch((e) => {
        console.log('ERROR: ' + JSON.stringify(e));
        throw e; // Propagate the error
      })
  }
}

And then in the main server class we wait for getDBConnection() before instantiating the context class that depends on the DB provider. It isn't the cleaner way but we were forced by the asynchronous nature of createConnection() and our still incomplete control of Inversify and wanted to get a fast glimpse of typeorm working with our structure.
Anyway I agree with @pleerock that this shall be a question to make in Inversify context.

from typeorm.

Shepless avatar Shepless commented on May 16, 2024

@dreamersp Thanks for sharing. Interestingly, I have basically ended up with the same approach.

I think there are two issues here then:

  1. The async nature of createConnection() doesn't appear to play nice with DI in general, is it possible for a synchronous version?

  2. Asking the Inversify people how to hook up TypeOrm correctly to their container implementation.

from typeorm.

pleerock avatar pleerock commented on May 16, 2024

here @remojansen gave answer how to use typeorm with inversify

from typeorm.

Related Issues (20)

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.