I am developing a web app with Docker using Express and Javascript. I am using a Three-layered architecture and using Sequelize to send queries using Mysql. I have blogpost as a resource which has a relation to the accounts table. This is how the relation looks like:
const Account = sequelize.define('accounts', {
personId: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
username: {
type: Sequelize.STRING(50),
allowNull: false,
unique: true
},
email: {
type: Sequelize.STRING(50),
allowNull: false,
unique: true
},
userPassword: Sequelize.TEXT
}, {
timestamps: false
})
const Blogpost = sequelize.define('blogposts', {
blogId: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
title: Sequelize.TEXT,
content: Sequelize.TEXT,
posted: Sequelize.TEXT,
imageFile: Sequelize.TEXT
}, {
timestamps: false
})
Blogpost.belongsTo(Account, {foreignKey: "userId", foreignKeyConstraint: true})
This is how I retrieve all blogposts in the presentation layer:
router.get("/", function(request, response){
blogManager.getAllBlogposts(function(errors, blogposts){
if(errors.includes("databaseError")){
response.send("<h1>Something went wrong!</h1>")
}
else if(errors.length > 0){
const model = {
errors
}
console.log("blogpostsModel:", { model })
response.render("blogposts.hbs", { model })
}else{
const model = {
blogposts
}
response.render("blogposts.hbs", { model })
}
})
})
And this is how the model looks like:
blogposts {
dataValues: {
blogId: 2,
title: 'fsdhkjfsdahflhs',
content: 'fhjkdsfkbmngfhyuxgc',
posted: '275760-07-09',
imageFile: 'module-6.jpg',
userId: 1
},
_previousDataValues: {
blogId: 2,
title: 'fsdhkjfsdahflhs',
content: 'fhjkdsfkbmngfhyuxgc',
posted: '275760-07-09',
imageFile: 'module-6.jpg',
userId: 1
},
blogposts {
dataValues: {
blogId: 3,
title: 'fjhhkjfsa',
content: 'gfhjdsakdasdsa',
posted: '8989-08-09',
imageFile: 'module-6.jpg',
userId: 2
},
_previousDataValues: {
blogId: 3,
title: 'fjhhkjfsa',
content: 'gfhjdsakdasdsa',
posted: '8989-08-09',
imageFile: 'module-6.jpg',
userId: 2
},
Now I want to retrieve the usernames with the userids. I have a working function that retrieves the a username by userid but I do not know how I can use that to get all the usernames from the userids. Any ideas how to solve this?
Thanks in advance!
you can use the include
option while querying blogposts
, like this
Blogpost.findAll({
include: [{
model:Account,
}]
});
it will use the join query to fetch the account
but if you want use a separate query then you can also do something like this
router.get("/", function(request, response){
blogManager.getAllBlogposts(function(errors, blogposts){
if(errors.includes("databaseError")){
response.send("<h1>Something went wrong!</h1>")
}
else if(errors.length > 0){
const model = {
errors
}
console.log("blogpostsModel:", { model })
response.render("blogposts.hbs", { model })
}else{
const userIds = blogposts.map(blogpost =>
blogpost.userId);
// query the Account with the userIds;
Account.find({ where : { userId : userIds}})
.then((accounts) => {
const blogpostsWithUserNames =
blogposts.map(blogpost => {
const findAccount =
accounts.filter(acc =>
acc.userId === blogpost.userId);
return {
...blogpost,
userName : findAccount.username,
}
})
const model = {
blogposts : blogpostsWithUserNames
}
response.render("blogposts.hbs",
{ model })
});
}
})
})