Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pirumu/nest-qualifier

Dynamic provider for Nest JS
https://github.com/pirumu/nest-qualifier

Last synced: 29 days ago
JSON representation

Dynamic provider for Nest JS

Awesome Lists containing this project

README

        

# Nest Qualifier

### How it works
Add alias metadata to the class and use it as token injection.

### How to install
```shell
# npm
npm i --save @nattogo/nest-qualifier

#yarn
yarn add @nattogo/nest-qualifier
```
### How to use
####
IRepository.ts
```ts
export interface IRepository {
findOne: (id: string) => Promise;
}
```

MongoRepository.ts
```ts

import {Injectable} from "@nattogo/nest-qualifier";

@Injectable({
alias: 'MongoRepository'
})
export class MongoRepositoryImlp implements IRepository {
findOne(id: string): Promise<{ id: string }> {
return Promise.resolve({
id
});
}

}
```

MySQLRepository.ts
```ts

import {Injectable} from "@nattogo/nest-qualifier";

@Injectable({
alias: 'MySQLRepository'
})
export class MySQLRepositoryImpl implements IRepository {
findOne(id: string): Promise<{ id: string }> {
return Promise.resolve({
id
});
}

}
```
ProductService.ts
```ts
import {Injectable} from "@nestjs/common";
import {Qualifier} from "@nattogo/nest-qualifier";

@Injectable()
export class ProductService {
public constructor(
@Qualifier('MongoRepository') mongoRepository: IRepository,
@Qualifier('MySQLRepository') mysqlRepository: IRepository
) {}
}
```

ProductModule.ts
```ts
import {Module} from "@nattogo/nest-qualifier";

@Module({
providers: [
MongoRepository,
MySQLRepository,
],
})
export class ProductModule {
}
```

### License
MIT License