problem description
how does koa+mysql, implement batch update and batch insert,
realized batch deletions
sql
// id
let cartDelectById = (id) => {
let sql = `delete from cart where cart_id = ${id};`
return query(sql)
}
// id
let cartDelectByIds = (ids) => {
let sql = `delete from cart where cart_id in (${ids});`
return query(sql)
}
koa
//
const cartDelectById = async(ctx) => {
let id = 1;
await cartModel.cartDelectById([id])
.then(result => {
ctx.body = {
state: 200,
msg: "",
data: result
}
})
.catch(error => {
console.log(error);
ctx.body = false;
})
}
//
const cartDelectByIds = async(ctx) => {
let ids = [1, 2, 3];
await cartModel.cartDelectByIds(ids)
.then(result => {
ctx.body = {
state: 200,
msg: "",
data: result
}
})
.catch(error => {
console.log(error);
ctx.body = false;
})
}
A single sql is successfully inserted and updated. How to achieve batch update and batch insert
//
let cartInsertInto = (values) => {
let sql = `insert into cart values(default,?,?,?,?,?);`
return query(sql, values)
}
//
let cartUpdate = (values) => {
let sql = `update cart set user_id=?,goods_id=?,cart_goods_number=? where cart_id=?;`
return query(sql)
}
insert
const cartInsertInto = async(ctx) => {
console.log(ctx.request.body)
let goodsId = ctx.request.body.goodsId;
let cartGoodsNumber = ctx.request.body.goodsNumber;
let userId = ctx.request.body.userId;
await cartModel.cartInsertInto([userId, goodsId, cartGoodsNumber, new Date(), new Date()])
.then(result => {
ctx.body = {
state: 200,
msg: "",
data: result
}
})
.catch(error => {
console.log(error);
ctx.body = false;
})
}