const db = require('../db');
module.exports = {
orders: {
post: (userId, orders, totalPrice, callback) => {
let orderSql = `
insert into orders (user_id, total_price) values (${userId}, ${totalPrice})`;
let itemSql = `insert into order_items (order_id, item_id, order_quantity) values ?`;
db.query(orderSql, (error, result) => {
if (result) {
let params = orders.map(order => [result.insertId, order.itemId, order.quantity]);
db.query(itemSql, [params], (error, result) => {
callback(error, result);
});
}
});
};
insert into
실행 후, 삽입된 데이터의 id를 가져올 때
result
객체의 insertId
의 값을 가져오면 됨
{
fieldCount: 0,
affectedRows: 14,
insertId: 0,
serverStatus: 2,
warningCount: 0,
message: '\'Records:14 Duplicated: 0 Warnings: 0',
protocol41: true,
changedRows: 0
}