nq
@nattogo/nest-qualifier
npm i @nattogo/nest-qualifier
nq

@nattogo/nest-qualifier

Dynamic provider for Nest JS

by Than The Van

0.0.13 (see all)License:MITTypeScript:Built-In
npm i @nattogo/nest-qualifier
Readme

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

No alternatives found
No tutorials found
Add a tutorial
No dependencies found

Rate & Review

100
No reviews found
Be the first to rate