我正在尝试弄清Nest中TypeOrm的“存储库模式”如何工作。我想要一个resources
文件,其中一个local
和一个远程文件包含不同的实体,控制器,模块等。请参见屏幕截图
在构建应用程序时,出现以下错误:
[Nest] 3186 - 11/19/2018, 10:44:43 PM [ExceptionHandler] No repository for "Project" was found. Looks like this entity is not registered in current "default" connection? +1ms
从Nest和TypeORM文档中,我可以进行三角测量,我必须告诉应用程序可以在哪里找到实体,或者至少这是我认为错误试图告诉我的内容。
我正在使用.env
将TypeORM的配置放入以下位置:
TYPEORM_CONNECTION = postgres
TYPEORM_HOST = localhost
TYPEORM_USERNAME = xyz
TYPEORM_PASSWORD = xyz
TYPEORM_DATABASE = xyz
TYPEORM_PORT = 5432
TYPEORM_SYNCHRONIZE = true
TYPEORM_LOGGING = true
TYPEORM_ENTITIES = src/server/**/**.entity{.ts,.js}
app.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AngularUniversalModule } from './modules/angular-universal/angular-universal.module';
import { JiraService } from './services/jira.service'
// modules
import { ProjectModule } from './resources/local/project/project.module'
// sync
import {ProjectsSync} from './sync/projects.sync'
@Module({
imports: [
ProjectModule,
TypeOrmModule.forRoot(),
AngularUniversalModule.forRoot(),
],
controllers: [],
providers:[JiraService, ProjectsSync],
})
export class ApplicationModule {}
project.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ProjectService } from './project.service';
import { ProjectController } from './project.controller';
import { Project } from './project.entity';
@Module({
imports: [TypeOrmModule.forFeature([Project])],
providers: [ProjectService],
controllers: [ProjectController],
})
export class ProjectModule {}
project.service.ts
import { Injectable, Inject } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Project } from './project.entity';
@Injectable()
export class ProjectService {
constructor(
@InjectRepository(Project)
private readonly projectRepository: Repository<Project>,
) {}
async findAll(): Promise<Project[]> {
return await this.projectRepository.find();
}
}
project.entity.ts
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class Project {
@PrimaryGeneratedColumn()
id: number;
@Column({ length: 500 })
name: string;
}
project.controller.ts
import { Controller, Get } from '@nestjs/common';
import { ProjectService } from './project.service';
import { Project } from './project.entity';
@Controller('project')
export class ProjectController {
constructor(private readonly projectService: ProjectService) {}
@Get()
findAll(): Promise<Project[]> {
return this.projectService.findAll();
}
}
答案 0 :(得分:0)
一个疯狂的猜测,您是否尝试过:
TYPEORM_ENTITIES = ./server/**/**.entity{.ts,.js}
和/或切换app.module.ts中的导入,将TypeOrmModule放在首位:
@Module({
imports: [
TypeOrmModule.forRoot(),
ProjectModule,
AngularUniversalModule.forRoot(),
],
controllers: [],
providers:[JiraService, ProjectsSync],
})
export class ApplicationModule {}
答案 1 :(得分:0)
在.env文件更改中
TYPEORM_ENTITIES = src/server/**/**.entity{.ts,.js}
到
TYPEORM_ENTITIES = /../**/*.entity.{js,ts}
在您的app.module中使用:
const typeOrmConfig: TypeOrmModuleOptions = {
type: process.env.TYPEORM_CONNECTION,
host: process.env.TYPEORM_HOST,
port: process.env.TYPEORM_PORT,
username: process.env.TYPEORM_USERNAME,
password: process.env.TYPEORM_PASSWORD,
database: process.env.TYPEORM_DATABASE,
synchronize: process.env.TYPEORM_SYNCHRONIZE,
logging: process.env.TYPEORM_LOGGING,
entities: [__dirname + process.env.TYPEORM_ENTITIES]
}
@Module({
imports: [
TypeOrmModule.forRoot(typeOrmConfig),
ProjectModule,
AngularUniversalModule.forRoot(),
],
controllers: [],
providers:[JiraService, ProjectsSync],
})
export class ApplicationModule {}