typescript 如果未在配置文件中设置实体目录,则 TypeORM 无法找到实体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52155315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeORM cannot find entities if entity directory was not set in configuration files
提问by Daniel Santos
I'm using TypeORM with the fallowing configuration file: ormconfig.json
我正在使用带有空闲配置文件的 TypeORM:ormconfig.json
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "my-secret-pw",
"database": "mytestdb",
}
My Entities files are stored on the ./src/bar/entity directory. I always get the following error:
我的实体文件存储在 ./src/bar/entity 目录中。我总是收到以下错误:
RepositoryNotFoundError: No repository for "myTable" was found. Looks like this entity is not registered in current "default" connection?
RepositoryNotFoundError:找不到“myTable”的存储库。看起来这个实体没有在当前的“默认”连接中注册?
The Entity is found when I manually add the directory to the configuration file:
当我手动将目录添加到配置文件时发现实体:
{
...
"entities": ["src/bar/entity/**/*.ts"]
}
My Entity is defined like:
我的实体定义如下:
@Entity('myTable')
export default class MyTable {
@PrimaryGeneratedColumn()
public id: number;
...
How can I allow the TypeORM to find those entities without setting manually in the configuration file for each directory?
如何让 TypeORM 无需在每个目录的配置文件中手动设置即可找到这些实体?
回答by Risto Novik
The most common case you described is to have separate entities
directory which consists only of Entity declarations.
您描述的最常见的情况是拥有entities
仅包含实体声明的单独目录。
{
...
"entities": ["src/bar/entities/**/*.ts"]
}
Another approach would be importing each entity separately:
另一种方法是分别导入每个实体:
import {User} from "./payment/entity/User";
import {Post} from "./blog/entity/Post";
{
...
"entities": [User, Post]
}
回答by Victor
For me the answer was
{
...
entities: [join(__dirname, '/../**/**.entity{.ts,.js}')],
}
对我来说答案是
{
...
entities: [join(__dirname, '/../**/**.entity{.ts,.js}')],
}
I've found the exapmle here https://github.com/nestjs/nest/blob/master/sample/05-sql-typeorm/src/app.module.ts
我在这里找到了示例 https://github.com/nestjs/nest/blob/master/sample/05-sql-typeorm/src/app.module.ts
回答by michal.jakubeczy
For me it helped to include also src
directory to ormconfig.json
:
对我来说,它也有助于将src
目录包含到ormconfig.json
:
"entities": [
"dist/**/*.entity{.ts,.js}",
"src/**/*.entity{.ts,.js}"
],
回答by ryan0
If you don't want to put all entities in the same place (ie if you have module folders and want to put entities in their associated module folders), and if you are using a file naming convention like foo.entity.ts
, foo.service.ts
, etc. then you can do the following and it will find all entities wherever they are in your source tree:
如果你不想把所有的实体在同一个地方(即如果你有模块的文件夹,并希望把实体及其关联的模块文件夹),如果你正在使用的文件命名约定一样foo.entity.ts
,foo.service.ts
等等,那么你可以执行以下操作,它将在源树中的任何位置找到所有实体:
{
...
"entities": ["src/**/*{.entity.ts}"],
}
回答by renatogbp
In order to work for both development and production environment, I had to do like this:
为了同时适用于开发和生产环境,我必须这样做:
entities: [
this.isProduction() ?
path.join(__dirname, '../**/**.entity{.ts,.js}') : '**/*.entity{.ts,.js}',
],
// ....
private isProduction(): boolean {
const mode = this.configService.get('NODE_ENV');
return mode !== 'development';
}