Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

TypeORM joining table with an additional AND table.column = "STATIC"

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...