在electron开发客户端的时候,使用typeorm作为数据库处理框架。在开发的时候,是没有问题的 ,但是打包之后,运行会出现如下错误
No repository for "xxx" was found. Looks like this entity is not registered in current "default" connection
原因是,开发环境是使用ormconfig.json作为typeorm配置文件的,配置如下
{
"type": "mysql",
"synchronize": true,
"logging": true,
"host": "xxx.xxx.xxx.xxx",
"port": 3306,
"username": "xxx",
"password": "xxx",
"database": "xxx",
"entities": [
"build/**/*.entity{.ts,.js}"
]
}
这个配置本身是没有问题的,关键就傻子啊entities这里,这里用的是相对路径,但是打包之后相对路径就找不到了。
解决办法:
不使用配置文件,直接在代码中写,另外,entities这里,直接用具体对象。例如
import {Order} from "../domain/entity/order.entity";
await createConnection({
"type": "mysql",
"synchronize": true,
"logging": true,
"host": "xxx.xxx.xxx.xxx",
"port": 3306,
"username": "xxx",
"password": "xxx",
"database": "xxx",
"entities": [
Order
]
})