0

How can I get the whole just inserted data without another query, is that possible?

I know the ID is there in the response payload: data.insertId

My issue is that when I insert the values there is an auto timestamp and I need it, but I don't want to query the DB again because of performance

db.query(`INSERT INTO PRODUCTS(prd_name, prd_price) \
  VALUES('${this.prd_name}',${this.prd_price})`, (err, data) => {
  res.status(200).json({
    message: 'Product added.',
    newProductData: data.WHOLE_ROW?????,
  });
});
lito
  • 3,105
  • 11
  • 43
  • 71
  • 2
    Bad news for you: you have to query the database again. But I would not fret over an extra query, that's what databases are designed for. – Shadow Feb 02 '20 at 08:24
  • @Shadow: ok!... – lito Feb 02 '20 at 08:38
  • 1
    Of course, if you generated your auto timestamp in your application code, then you would know it beforehand – Strawberry Feb 02 '20 at 10:10
  • You might also consider adding Knex which offers many convenience methods. In your case the `.returning()` function would be handy. [http://knexjs.org/#Builder-returning] – dusthaines Mar 24 '20 at 16:55

0 Answers0