使用 Typeorm 连接到 Nestjs 中的 mssql,但 Nest 无法解析 EmployeeRepository 的依赖关系
Posted
技术标签:
【中文标题】使用 Typeorm 连接到 Nestjs 中的 mssql,但 Nest 无法解析 EmployeeRepository 的依赖关系【英文标题】:connect to mssql in Nestjs with Typeorm, but Nest can't resolve dependencies of the EmployeeRepository 【发布时间】:2021-08-27 04:48:36 【问题描述】:我将 Nestjs 中的 mssql 与 Typeorm 连接并得到错误 Nest can't resolve dependencies of the EmployeeRepository
我的 app.module.ts 文件:
import Module from '@nestjs/common';
import AppController from './app.controller';
import AppService from './app.service';
import config from '../ormconfig';
import TypeOrmModule from '@nestjs/typeorm';
import EmployeesModule from './employees/employees.module';
@Module(
imports: [
TypeOrmModule.forRoot( ...config, autoLoadEntities: true ),
EmployeesModule,
],
controllers: [AppController],
providers: [AppService],
)
export class AppModule
ormconfig.ts 文件:
const config: SqlServerConnectionOptions =
type: 'mssql',
host: 'localhost',
port: 1433,
username: 'sa',
password: 'sa',
database: 'db1',
synchronize: false,
;
export default config;
employees.module.ts 文件:
import Module from '@nestjs/common';
import TypeOrmModule from '@nestjs/typeorm';
import Employee from './employee.entity';
import EmployeesController from './employees.controller';
import EmployeesService from './employees.service';
@Module(
imports: [TypeOrmModule.forFeature([Employee])],
providers: [EmployeesService],
controllers: [EmployeesController],
)
export class EmployeesModule
employees.service.ts 文件:
import Injectable from '@nestjs/common';
import InjectRepository from '@nestjs/typeorm';
import Repository from 'typeorm';
import Employee from './employee.entity';
@Injectable()
export class EmployeesService
constructor(
@InjectRepository(Employee)
private employeesRepository: Repository<Employee>,
)
findAll(): Promise<Employee[]>
return this.employeesRepository.find();
employee.entity.ts 文件:
import
Entity,
PrimaryGeneratedColumn,
Column,
BaseEntity,
from 'typeorm';
@Entity('Employees')
export class Employee extends BaseEntity
@PrimaryGeneratedColumn( type: 'int' )
EmployeeID!: number;
@Column( type: 'nvarchar', length: 50 )
FirstName!: string;
@Column( type: 'nvarchar', length: 50 )
LastName!: string;
...
employees.controller.ts 文件只做 CLI 创建的准系统。在 mssql 的 db1 中有一个名为 Employees
的表。我只是无法弄清楚为什么会这样。我什至尝试删除 dist 和 node_modules,但没有运气。
我的 package.json 的主要部分
"scripts":
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"src,apps,libs,test/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
,
"dependencies":
"@nestjs/common": "^7.6.15",
"@nestjs/core": "^7.6.15",
"@nestjs/platform-express": "^7.6.15",
"@nestjs/typeorm": "^7.1.5",
"mssql": "^7.1.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.6",
"typeorm": "^0.2.34"
,
"devDependencies":
"@nestjs/cli": "^7.6.0",
"@nestjs/schematics": "^7.3.0",
"@nestjs/testing": "^7.6.15",
"@types/express": "^4.17.11",
"@types/jest": "^26.0.22",
"@types/node": "^14.14.36",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^4.19.0",
"@typescript-eslint/parser": "^4.19.0",
"eslint": "^7.22.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"supertest": "^6.1.3",
"ts-jest": "^26.5.4",
"ts-loader": "^8.0.18",
"ts-node": "^9.1.1",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.2.3"
,
【问题讨论】:
【参考方案1】:我建议在您的配置中添加实体路径,以便它可以找到文件:
type: 'mssql',
host: 'localhost',
port: 1433,
username: 'sa',
password: 'sa',
database: 'db1',
entities: ["dist/**/*.entity.ts,.js"],
synchronize: false,
;
文档:https://docs.nestjs.com/techniques/database#repository-pattern
【讨论】:
您好,我试过了,但遇到了同样的错误。我什至添加了 autoLoadEntities: true ,如我的 app.module 文件中所示,该文件受到 Nestjs 示例代码的启发。 抱歉无法重现您的问题,只是尝试了同样的方法,一切正常(即使没有实体选项)...您的库是最新的吗? 感谢您的回复,我在上面的问题中添加了package.json。我一周前开始了这个项目。所有的包都应该没问题。 你说得对,我从头开始。有用。我一定做了一些愚蠢的事情,我仍然不知道错误的原因。还是谢谢!以上是关于使用 Typeorm 连接到 Nestjs 中的 mssql,但 Nest 无法解析 EmployeeRepository 的依赖关系的主要内容,如果未能解决你的问题,请参考以下文章
使用 NestJS 和 TypeOrm,在我运行 NestJS 应用程序后不会自动创建表
NestJS/TypeORM:无法读取未定义的属性“createQueryBuilder”
NestJS + MySQL:如何在不设置实体的情况下连接多个数据库