nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.69k stars 7.63k forks source link

Custom providers error #739

Closed santaz1995 closed 6 years ago

santaz1995 commented 6 years ago

I'm submitting a...


[x] Regression 
[ ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

It is not possible to create a custom provider use (providers: {provide: 'Interface', useClass: implements this interface''}). There is an interface that describes the functions


import { User } from './user';

export interface UserCommandRepository {

    /**
     * @param {number} id
     * @returns {Promise}
     */
    byId(id: number): Promise

    /**
     * @param {User} user
     */
    store(user: User): Promise;
}

Next, I created a class that implements this interface


@EntityRepository()
export class TypeOrmUserCommandRepository extends TypeOrmCommandRepository implements UserCommandRepository {

    /**
     * @param {number} id
     * @returns {Promise}
     */
    public async byId(id: number): Promise {
        return this.createQueryBuilder().andWhere('u.id = :id').setParameter('id', id).getOne();
    }

    /**
     * @param {User} user
     * @returns {Promise}
     */
    public async store(user: User): Promise {
        return this.entityManager.save(user);
    }

    /**
     * @param {ObjectType} entityClass
     * @param {string} alias
     *
     * @returns {SelectQueryBuilder}
     */
    protected createQueryBuilder(entityClass: ObjectType = User, alias: string = 'u'): SelectQueryBuilder {

        return this.entityManager.createQueryBuilder(entityClass, alias)
            .select(alias)
            .where(alias + '.deletedAt IS NULL');
    }
}

Then I created the provider in the module


@Module({
    imports: [CQRSModule],
    controllers: [AuthorizationController],
    providers: [
        {
            provide: 'UserCommandRepository',
            useClass: TypeOrmUserCommandRepository
        },
        {
            provide: 'UserQueryRepository',
            useClass: TypeOrmUserQueryRepository
        },
    ],
})

And i have error

 
TS2345: Argument of type '{ imports: (typeof CQRSModule)[]; controllers: (typeof AuthorizationController)[]; providers: { p...' is not assignable to parameter of type 'ModuleMetadata'.   Types of property 'providers' are incompatible.     Type '{ provide: string; useClass: typeof TypeOrmUserCommandRepository; }[]' is not assignable to type 'Provider[]'.       Type '{ provide: string; useClass: typeof TypeOrmUserCommandRepository; }' is not assignable to type 'Provider'.         Type '{ provide: string; useClass: typeof TypeOrmUserCommandRepository; }' is not assignable to type 'FactoryProvider'.           Property 'useFactory' is missing in type '{ provide: string; useClass: typeof TypeOrmUserCommandRepository; }'
 

Expected behavior

The provider must associate the interface and implementations for further use of the interface. Example:


@CommandHandler(SignUpCommand)
export class SignUpExecute implements ICommandHandler {

    constructor(
        @Inject('UserCommandRepository') private userRepository: UserCommandRepository,
        private readonly publisher: EventPublisher) {
    }

Minimal reproduction of the problem with instructions

In the repository (https://github.com/santaz1995/nestjs-cqrs-starter) , you can look at any module that is on the path 'src/http/modules/'

Environment


Nest version: 5.0.1

For Tooling issues:
- Node version: v9.11.1
- Platform:  Windows 10
lock[bot] commented 5 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.