我尝试了很多方法将实体导入到脚本中的connection中,但是没有任何东西正确运行。这样,我就没有现成的存储库可以在代码中使用我的实体。
处理TypeORM库的文件:
import * as fs from 'fs'
import * as typeorm from 'typeorm'
import {Public} from './../entity/Public'
export default class ConnectionManager
{
async createConnection()
{
this.connection = await this.connectionManager.create({
type: 'sqlite',
database: './src/data/mydb.sql',
synchronize: true,
entities: [
Public,
'./../entity/Public.js',
'./../entity/Public.ts',
'./../entity/Public',
'./../entity/*.js',
'./../entity/*.ts',
"./../entity/Public.js",
"./../entity/Public.ts",
"./../entity/Public",
"./../entity/*.js",
"./../entity/*.ts",
"src/entity/*.ts",
"src/entity/*.js",
'src/entity/*.ts',
'src/entity/*.js',
],
})
}
为什么以及如何正确地将实体声明为TypeORM连接创建?
发布于 2019-03-28 04:58:29
实体似乎不是在typeorm的最新版本中创建的。因此,请使用下面的命令使用“typeorm-model-生成器”包生成实体。
npx typeorm-model-generator -d "Database path or Host" -e DatabaseName -o "Path of Entity folder"
这将帮助您生成实体架构。谢谢。
发布于 2019-12-11 06:26:27
您应该在./entity/中创建一个index.ts文件,然后像这样导入所有实体类:
import {Public1} from "./public1";
import {Public2} from "./public2";
export default [
Public1,
Public2
]
并导入创建连接的文件,如下所示:
import * as fs from 'fs';
import * as typeorm from 'typeorm';
import entities from './../entity';
export default class ConnectionManager
{
async createConnection()
{
this.connection = await this.connectionManager.create({
type: 'sqlite',
database: './src/data/mydb.sql',
synchronize: true,
entities,
})
}
阅读和组织会更好。
https://stackoverflow.com/questions/54053661
复制相似问题