nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇
https://nestjs.com
MIT License
1.92k stars 206 forks source link

How to properly create a custom repository? #418

Closed danvandachevici closed 4 years ago

danvandachevici commented 4 years ago

I'm submitting a question. (subject)


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

Current behavior

I'm having the following situation:


@Module({
    imports: [
        ConfigModule.forRoot({
            expandVariables: true
        }),
        TypeOrmModule.forRootAsync({
            imports: [ConfigModule],
            useFactory: (configService: ConfigService) => ({
                type: 'mysql',
                host: configService.get('DATABASE_HOST'),
                port: configService.get('DATABASE_PORT') || 3306,
                username: configService.get('DATABASE_USER'),
                password: configService.get('DATABASE_PASSWORD'),
                database: configService.get('DATABASE_NAME'),
                entities: ['dist/**/*.entity.{ts,js}'],
                synchronize: true,
            }),
            inject: [ConfigService],
        }),
        UsersModule,
        ...
    ],
    controllers: [],
    providers: [],
})

The Users repository looks as follows:


import {UserEntity} from "../entities/user.entity";
import {EntityRepository, Repository} from "typeorm";
import {InjectRepository} from "@nestjs/typeorm";

@EntityRepository(UserEntity)
export class UserRepository extends Repository {
    getUserById(id: string): Promise {
        return this.findOne(id).then((user) => {
            return user;
        }).catch((err) => {
            return undefined;
        });
    }
}

The bootstrap goes smoothly, but as soon as I make my call that would trigger the above repository function getUserById to be called, I get an error:


[Nest] 92721   - 04/12/2020, 3:44:00 PM   [ExceptionsHandler] Cannot read property 'findOne' of undefined

Expected behavior

I would expect that the app would work, and the findOne would be defined, since the class extends the base Repository class.

Minimal reproduction of the problem with instructions

Code as above

Environment


MacOS Catalina 10.15.4
Node@10.19.0
"@nestjs/common": "^7.0.0",
    "@nestjs/config": "^0.4.0",
    "@nestjs/core": "^7.0.0",
    "@nestjs/jwt": "^7.0.0",
    "@nestjs/passport": "^7.0.0",
    "@nestjs/platform-express": "^7.0.0",
    "@nestjs/typeorm": "^7.0.0",

I bypassed the problem in a stupid (I think its stupid, but correct me if I'm wrong) way, by injecting in the repository the Repository, and using the this.userRepo for the base functionalities.

I say it feels stupid, because in this case I wouldn't need to extend from the Repository, because I could do this injection in any other service, and don't see the need to have a repo anymore.

I found a lot of similar issues online, people either fixed their issues with a provider (Doesn't work for me), or bypassed the issue in some way, or figured they're doing something stupid. I expect that my problem is also generated by some stupid thing I did, that I'm missing right now.

danvandachevici commented 4 years ago

I posted my question to stack overflow: https://stackoverflow.com/questions/61175529/how-to-properly-create-a-custom-repository