自动为连接的表格列添加前缀

时间:2016-09-28 16:57:21

标签: javascript postgresql knex.js

我正在寻找更简单的方法来让两行相互关联。

这有点疯狂,看看是否还有其他选择。

我正在使用knex

  async function main ({knex, table, token}) {
    let res = await knex(`${table} AS t1`)
      .select(
        't1.every as t1.every',
        't1.dog as t1.dog',
        't1.has as t1.has',
        't1.its as t1.its',
        't1.day as t1.day',
        't2.every as t2.every',
        't2.dog as t2.dog',
        't2.has as t2.has',
        't2.its as t2.its',
        't2.day as t2.day',
      )
      .where('t1.every', token)
      .innerJoin(`${table} AS t2`, 't1.day', 't2.id')
      let flat = res[0]
    let result = {}
    each(flat, (value, key) => {
      set(result, key, value)
    })
    return {
      main: result.t2,
      associated: result.t1
    }
  }

0 个答案:

没有答案