Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to inject TypeORM repositories (and other generic classes and interfaces) #627

Closed
LoicPoullain opened this issue Feb 7, 2020 · 2 comments

Comments

@LoicPoullain
Copy link
Member

LoicPoullain commented Feb 7, 2020

Issue

Version 1.6 of FoalTS will give the possibility to inject manually class instances (#599). This can be useful for example to inject TypeORM connections.

class Controller {
  @dependency
  connection: Connection;
}

const connection = await createConnection();
const serviceManager = new ServiceManager();
serviceManager.set(Connection, connection);

const app = createApp(AppController, { serviceManager });

However, this is not sufficient to inject TypeORM repositories as they are generic classes.

// This cannot work. After compilation only the class `Repository` remains.
class Controller {
  @dependency
  users: Repository<User>;

  @dependency
  products: Repository<Product>;
}

Solution

Add a new @Dependency decorator to allow to inject services with string keys and values.

class Controller {
  @Dependency('User')
  users: Repository<User>;

  @Dependency('Product')
  products: Repository<Product>;
}

const serviceManager = new ServiceManager()
  .set('User', getRepository(User));
  .set('Product', getRepository(Product));

const app = createApp(AppController, { serviceManager });

TODO: update also createService

Related issues

#538 #598

@anonimusprogramus
Copy link
Contributor

Thank you. I had a wish that @dependency would be capitalized. Now it is. 👍

@LoicPoullain LoicPoullain mentioned this issue Feb 28, 2020
5 tasks
@LoicPoullain LoicPoullain changed the title Allow to inject TypeORM repositories (and other generic classes) Allow to inject TypeORM repositories (and other generic classes and interfaces) Apr 16, 2020
@LoicPoullain
Copy link
Member Author

Feature added in v1.8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

2 participants