TypeORM 不支持实体装饰器上的数据库设置 [英] TypeORM doesn't support the database setting on the entity decorator
问题描述
我正在尝试将我的 TypeORM 项目分离到多个数据库上,因为它的规模越来越大,而且它的组件非常离散(但相互关联,所以我需要能够跨数据库建立关系).
I'm trying to seperate my TypeORM project over multiple databases as it is growing in size, and its components are very discrete(yet interlinked, so i need to be able to have relations cross-database).
我正在尝试使用 @Entity
装饰器上的 database
设置来做到这一点,如下所述:https://typeorm.io/#multiple-connections/using-multiple-databases-in-a-single-连接
I am trying to do that using the database
setting on the @Entity
decorator, as described here: https://typeorm.io/#multiple-connections/using-multiple-databases-in-a-single-connection
我为此制作了一个最小的可重现示例,其中两个实体理论上应该放在不同的数据库中:
I made a minimal reproducable example for this, with two entities that should in theory be put in different databases:
@Entity({ database: 'test' })
export default class Entity1 {
@PrimaryGeneratedColumn()
id?: number
@Column()
name?: string
@Column()
address?: string
}
和
@Entity({ database: 'database2' })
export default class Entity2 {
@PrimaryGeneratedColumn()
id?: number
@Column()
name?: string
@Column()
address?: string
}
连接代码:
import {createConnections} from "typeorm";
async function doDbExample() {
const connections = await createConnections([{
name: "db1Connection",
type: "postgres",
host: "db",
port: 5432,
username: "test",
password: "testPassword",
database: "test",
entities: [__dirname + "/entity/*{.js,.ts}"],
synchronize: true
}]);
console.log("Created connections")
}
doDbExample()
然而,发生的情况是两个实体的表都放入了连接的数据库中.我做错了什么,还是 TypeORM 中的错误?在我看来,它不再尊重 database
设置.
However, what happens is that both entities' table is put in the database of the connection. Am i doing something wrong, or is this a bug in TypeORM? It looks to me like it is not respecting the database
setting any more.
我正在使用 ts-node-dev
我在 github 上做了一个完整的最小可重现示例,完成了数据库环境的 dockerized 设置:https://github.com/petterroea/TypeOrmBug-MRE
I made a full minimal reproducable example, complete with dockerized setup of the database environment, on github: https://github.com/petterroea/TypeOrmBug-MRE
推荐答案
这是一个设置问题.我是这样解决的:
This is a setup issue. I solved it like this:
- 修改数组
entities
,以便每个连接/数据库都有自己的实体文件文件夹,并将您最常用的实体命名为default
:
- Modify the array
entities
so each connection/database has its own folder with entity files and name the entity you use the most asdefault
:
// src/index.ts
await createConnections([
{
name: 'default',
host: 'SERVER1',
username: 'bob',
password: 'kiwi,
type: 'mssql',
database: 'db1',
...
"synchronize": true,
"entities": ["src/db1/entity/**/*.ts"],
},
{
name: 'connection2,
host: 'SERVER2',
username: 'Mike',
password: 'carrot',
type: 'mssql',
database: 'db2,
...
"synchronize": true,
"entities": ["src/db2/entity/**/*.ts"],
])
- 在各自的文件夹中为每个数据库创建实体文件:
src/db1/entity/Fruit.ts
> db1 中的表src/db2/entity/Vegetables.ts
> db2 中的表
- Create entity files for each database in its respective folder:
src/db1/entity/Fruit.ts
> table in db1src/db2/entity/Vegetables.ts
> table in db2
使用 "synchronize": true
每个表都将在正确的数据库中自动创建
With "synchronize": true
each table will be created automatically in the correct database
- 访问表中的数据:
- 对于
default
连接::
- 对于
import { Fruit} from 'src/db1/entity/Fruit.ts'
fruits() {
return Fruit.find()
}
- 对于非默认连接:
import { getRepository } from 'typeorm'
import { Vegetable} from 'src/db2/entity/Vegetable.ts'
vegetables() {
return async () => await getRepository(Vegetable).find()
}
或
async vegetables() {
return await getRepository(vegetables, 'connection2').find()
}
我希望这能帮助其他与你和我有同样问题的人.
I hope this helps someone else struggling with the same issues as you and me.
这篇关于TypeORM 不支持实体装饰器上的数据库设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!