Skip to content

Instantly share code, notes, and snippets.

@kianaditya
Created July 6, 2020 13:11
Show Gist options
  • Save kianaditya/bd4dbcc0b6cc31ddac937f4a111c9faa to your computer and use it in GitHub Desktop.
Save kianaditya/bd4dbcc0b6cc31ddac937f4a111c9faa to your computer and use it in GitHub Desktop.
const db = require('../index')
const getAllPosts = async () => {
const allPosts = await db.Post.findAll({
attributes: ['id', 'title', 'content'],
include: [
{
model: db.User,
as: 'Author',
attributes: ['firstName', 'lastName'],
},
],
})
return allPosts
}
const getSpecificPost = async (postId) => {
const specificPost = await db.Post.findOne({
where: {
id: postId,
},
attributes: ['id', 'title', 'content'],
include: [
{
model: db.User,
as: 'Author',
attributes: ['firstName', 'lastName'],
},
{
model: db.User,
as: 'savedBy',
attributes: ['firstName', 'lastName'],
through: {
attributes: [],
},
},
],
})
return specificPost
}
module.exports = {
getAllPosts,
getSpecificPost,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment