sequelize提供了批量插入數據的方法:Model.bulkCreate([…object])
例如:
/**
* @route POST api/detail/bulkCreate
* @desc 批量添加漫畫詳情接口地址
* @access 接口是公開的
*/
router.post("/bulkCreate", async (ctx) => {
// 存儲到數據庫
const { chapterId, chapterImages } = ctx.request.body;
let params = chapterImages.map((imgUrl) => {
return {
chapterId,
imgUrl,
};
});
let data = await Detail.bulkCreate(params);
if (data) {
ctx.status = 200;
ctx.body = {
data,
msg: "保存成功",
};
} else {
ctx.status = 500;
ctx.body = {
data: {},
msg: "保存失敗",
};
}
});
.
