@nattogo/nest-qualifier
TypeScript icon, indicating that this package has built-in type declarations

0.0.13 • Public • Published

Nest Qualifier

How it works

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

How to install

# npm
npm i --save @nattogo/nest-qualifier

#yarn
yarn add @nattogo/nest-qualifier

How to use

IRepository.ts

export interface IRepository {
    findOne: (id: string) => Promise<any>;
}

MongoRepository.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

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

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

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

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

License

MIT License

Readme

Keywords

none

Package Sidebar

Install

npm i @nattogo/nest-qualifier

Weekly Downloads

192

Version

0.0.13

License

MIT

Unpacked Size

11.3 kB

Total Files

26

Last publish

Collaborators

  • nattogo