dataSource.createEntityManager is not a function typeorm nest.js

Viewed 21

Used "@nestjs/typeorm": "^9.0.1", "typeorm": "^0.3.9"

when using the withRepository method, an exception is thrown

ERROR [ExceptionsHandler] dataSource.createEntityManager is not a function TypeError: dataSource.createEntityManager is not a function

import { Injectable } from "@nestjs/common";
import { DataSource } from "typeorm";

@Injectable()
export class BarService {
  constructor(
    private readonly barRepository: BarRepository,
    private readonly dataSource: DataSource
  ) {}

  async foo() {
    await this.dataSource.manager.transaction((manager) => {
      const barRepository = manager.withRepository(this.barRepository);

      return;
    });
  }
}


import { Injectable } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm';

import { BarEntity } from '../entities/bar.entity';

@Injectable()
export class BarRepository extends Repository<BarEntity> {
  constructor(dataSource: DataSource) {
    super(BarEntity, dataSource.createEntityManager());
  }
}


import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BarEntity } from './entities/bar.entity';
import { BarService } from './services/bar.service';
import { BarRepository } from './repositories/bar.repository';

@Module({
  imports: [TypeOrmModule.forFeature([BarEntity])],
  providers: [
    BarService,
    BarRepository
  ],
  exports: [BarService],
})
export class BarModule {}

error at debbuger

0 Answers
Related