Knex.js更新语句从多个表返回数据

时间:2019-07-12 16:30:02

标签: javascript node.js knex.js

如何使用knex.js进行更新语句,其中returning数据来自多个表。例如,如何用knex.js编写此更新查询?

update notes 
set note = 'This is an updated1 note', timestamp = now() 
from(select
    id, 
    note, 
    user_id,
    customer_id
    from notes
    where id = '46' AND user_id = 7) a
    left join customers as b on a.customer_id = b.id
where notes.id = a.id
returning a.id, a.note, a.user_id, b.id;

当只使用一个表时,我可以将其写为返回数据:

knex('notes')
    .returning([
      'notes.id',
      'notes.note',
      'notes.user_id AS userId',
      'notes.customer_id AS customerId',
      'notes.product_id AS productId',
      'notes.timestamp',
    ])
    .where('notes.id', noteId)
    .update({
        timestamp: new Date(),
        note: req.body.note,
    });

当我尝试介绍customers时,我的问题就开始了。

我也尝试过:

const [note] = await db.sequelize.knex.raw(
        'UPDATE notes '
        + 'SET note = ?, '
        + 'timestamp = NOW() '
        + 'FROM(SELECT id, '
        + 'note, '
        + 'user_id, '
        + 'customer_id '
        + 'FROM notes '
        + 'WHERE id = ? '
        + 'AND user_id = 7) AS a '
        + 'LEFT JOIN customers AS b ON a.customer_id = b.id '
        + 'WHERE notes.id = a.id '
        + 'RETURNING a.id, a.note, a.user_id, b.id', ['This is an updated1 note', 46]
      );

但是它给了我TypeError: (intermediate value) is not iterable

更新

我现在有了这个:

let note = await db.sequelize.knex.raw(
        'UPDATE notes '
        + 'SET note = ?, '
        + 'timestamp = NOW() '
        + 'FROM(SELECT id, '
        + 'note, '
        + 'user_id, '
        + 'customer_id, '
        + 'product_id, '
        + 'timestamp '
        + 'FROM notes '
        + 'WHERE id = ? '
        + 'AND user_id = 7) AS a '
        + 'LEFT JOIN customers AS b ON a.customer_id = b.id '
        + 'LEFT JOIN products AS c ON a.product_id = c.id '
        + 'WHERE notes.id = a.id '
        + 'RETURNING a.id, a.note, a.user_id AS userId, a.customer_id AS customerId, a.product_id AS productId, a.timestamp, b.title AS customerName, b.code AS customerCode, c.title AS productName, c.code AS productCode', [req.body.note, noteId]
      );

      /*const [note] = await db.sequelize.knex('notes')
        .returning([
          'notes.id',
          'notes.note',
          'notes.user_id AS userId',
          'notes.customer_id AS customerId',
          'notes.product_id AS productId',
          'notes.timestamp',
          //'customers.title AS customerName',
          //'customers.code AS customerCode',
          //'products.title AS productName',
          //'products.code AS productCode',
        ])
        //.leftJoin('customers', 'notes.customer_id', 'customers.id')
        //.leftJoin('products', 'notes.product_id', 'products.id')
        .where('notes.id', noteId)
        .update({
          timestamp: new Date(),
          note: req.body.note,
        });*/

        console.log('PC NOTE', note.rows);
      [note] = note.rows;

      return res.json({ note });

它返回以下内容:

{
    "note": {
        "id": 46,
        "note": "This is an updated2 note",
        "userid": 7,
        "customerid": 111781,
        "productid": null,
        "timestamp": "2019-07-12T19:37:23.996Z",
        "customername": "PC CUSTOMER3",
        "customercode": "PC003",
        "productname": null,
        "productcode": null
    }
}

这几乎是我所需要的,除了返回的键都是小写字母。我如何使useridcustomeridproductidcustomernamecustomercodeproductnameproductcode出现在骆驼的情况下就像我在knex语句的RETURN部分中所说的那样?

2 个答案:

答案 0 :(得分:1)

Knex对更新+联接没有任何特殊支持

https://github.com/tgriesser/knex/issues/2796

因此编写原始查询是您唯一的选择。

答案 1 :(得分:0)

这对我有用:

let note = await db.sequelize.knex.raw(
        'UPDATE notes '
        + 'SET note = ?, '
        + 'timestamp = NOW() '
        + 'FROM(SELECT id, '
        + 'note, '
        + 'user_id, '
        + 'customer_id, '
        + 'product_id, '
        + 'timestamp '
        + 'FROM notes '
        + 'WHERE id = ? '
        + 'AND user_id = 7) AS a '
        + 'LEFT JOIN customers AS b ON a.customer_id = b.id '
        + 'LEFT JOIN products AS c ON a.product_id = c.id '
        + 'WHERE notes.id = a.id '
        + 'RETURNING a.id, '
        + 'notes.note, '
        + 'notes.user_id AS "userId", '
        + 'notes.customer_id AS "customerId", '
        + 'notes.product_id AS "productId", '
        + 'notes.timestamp, '
        + 'b.title AS "customerName", '
        + 'b.code AS "customerCode", '
        + 'c.title AS "productName", '
        + 'c.code AS "productCode"', [newNote, noteId],
      );

      [note] = note.rows;

      return res.json({ note });