module.exports = (app, db) => {
app.get( "/posts", (req, res) =>
db.post.findAll().then( (result) => res.json(result) )
);
app.get( "/post/:id", (req, res) =>
db.post.findByPk(req.params.id).then( (result) => res.json(result))
);
app.post("/post", (req, res) =>
db.post.create({
title: req.body.title,
content: req.body.content
}).then( (result) => res.json(result) )
);
app.put( "/post/:id", (req, res) =>
db.post.update({
title: req.body.title,
content: req.body.content
},
{
where: {
id: req.params.id
}
}).then( (result) => res.json(result) )
);
app.delete( "/post/:id", (req, res) =>
db.post.destroy({
where: {
id: req.params.id
}
}).then( (result) => res.json(result) )
);
}
Thursday, June 4, 2020
Sequelize API for CRUD operation
更多:
https://dwatow.github.io/2018/09-24-sequelize/sequelize-R-of-CRUD/
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment