I have a legacy system I am writting a node intigration for, and there are a few columns that have module_name and module_id
module_name basicly tells which table it will join ( ie order, user, contact, task.... )
module_id is the id of the above entity,
So for orders, to get what tasks the mysql looks like
SELECT * FROM orders LEFT JOIN tasks ON orders.id = tasks.module_id AND tasks.module_name = 'ORDER'
In typeorm, I have looked into JoinTable, and JoinColumn but both require all joined fields to be a reference of the other ( no static text, so I can't add the AND tasks.module_name = 'ORDER' )
UPDATE:
I do have a work around, that is ok for what I need now ( but a bit hacky ) and if there is a better way Please let me know, Thank you
@OneToMany(
type => Task,
task => task.order,
)
@JoinColumn({
name: 'order_id',
})
protected tasks: Task[];
@AfterLoad()
aLoad() {
if (Array.isArray(this.tasks)) {
this.tasks = this.tasks.filter(t => t.moduleName === 'order');
}
}
question from:
https://stackoverflow.com/questions/65920845/typeorm-joining-table-with-an-additional-and-table-column-static 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…