NODEJS - Perform INNER JOIN sequelize - mysql

I want to perform an INNER JOIN using sequelize and Node. The actual SQL query looks like this:
SELECT b.id, b.title, b.author, b.image_url, s.novel_status AS status, g.genre
FROM novels b
INNER JOIN genres g
ON b.genre = g.id
INNER JOIN novel_statuses s
ON b.status = s.id
Which gives the following response:
Using sequelize as my ORM, I have tried to accomplish the above like this:
getNovel: (req, res) => {
novelModel.hasMany(statusModel, {
foreignKey: 'id'
});
statusModel.belongsTo(novelModel, {
foreignKey: 'id'
});
novelModel.hasMany(genreModel, {
foreignKey: 'id'
});
genreModel.belongsTo(novelModel, {
foreignKey: 'id'
});
novelModel
.findAll({
include: [
{
model: genreModel,
required: true,
attributes: ['genre']
},
{
model: statusModel,
required: true,
attributes: ['novel_status']
}
]
})
.then(result => res.json(result))
.catch(err => res.json(err));
},
But then the response is different, i.e. as below:
My DB:
I have tried to search for answers, but without any luck.

You should give the name of the Model Framework you use.

Related

Sequelize: find count of 3 level associated model entries

I have a products database structure as follows:
Categories > Sub categories > Child categories > Products
the respective table names in mySql database are:
categories, subCategories, childCategories and products
My model definitions are as follows:
////////////////// DEFINING ASSOCIATIONS /////////////
/******* CATEGORY - SUBCATEGORY ASSOCIATION *********/
db.category.hasMany(db.subCategory, {
foreignKey: 'categoryId',
sourceKey: 'id',
});
db.subCategory.belongsTo(db.category, {
foreignKey: 'categoryId',
targetKey: 'id',
});
/******* SUBCATEGORY - CHILDCATEGORY ASSOCIATION *********/
db.subCategory.hasMany(db.childCategory, {
foreignKey: 'subCategoryId',
sourceKey: 'id',
});
db.childCategory.belongsTo(db.subCategory, {
foreignKey: 'subCategoryId',
targetKey: 'id',
});
/******* CHILDCATEGORY - PRODUCT ASSOCIATION *********/
db.childCategory.hasMany(db.product, {
foreignKey: 'childCategoryId',
sourceKey: 'id',
});
db.product.belongsTo(db.childCategory, {
foreignKey: 'childCategoryId',
targetKey: 'id',
});
In my summary result, I want to share a table as follows:
For this, I tried multiple variations of the below code but am unable to produce the required result.
My current code to achieve the above result:
exports.getAllCategoriesSummary = async (req, res) => {
const { page, size, search } = req.query;
console.log(page, size, search);
try {
const totalCategories = await Category.count();
// get categories and count of included subCategories, childCategories and products
const [rows, fields] = await mysqlConnPool.promise().execute(
`SELECT c.id, c.name, COUNT(DISTINCT(s.id)) AS subCategories,
COUNT(DISTINCT(ch.id)) AS childCategories, COUNT(p.id) AS products
FROM categories c
LEFT JOIN subCategories s ON c.id = s.categoryId
LEFT JOIN childCategories ch ON s.id = ch.subCategoryId
LEFT JOIN products p ON ch.id = p.childCategoryId GROUP BY c.id
ORDER BY c.id ASC LIMIT ${size} OFFSET ${page * size}
`
);
return res.status(200).json({
status: 'success',
categories: rows,
totalCategories: totalCategories,
});
} catch (error) {
return res.status(500).json({
status: 'error',
message: error.message,
});
}
};
Requesting help to produce the needed result using sequelize.

How to write an Inner Join query using sequelize ORM?

My inner join query looks as below
SELECT list.id, list.name,
sa.createdBy
FROM list
INNER JOIN data sa
ON sa.listId = list.id
WHERE sa.type = 'type1'
and sa.data = 'data1'
I am trying to write the above query using sequelize ORM.
I have written the following query but it is not giving desired result.
list.findAll({
include: [{
model: data,
required: true
where: {type: 'type1'}
}]
}).then(list => {
/* ... */
});
Your where clause doesn't fully contain the query you wrote
list.findAll({
include: [{
model: data,
required: true,
where: {
type: 'type1', // sa.type = 'type1'
data: 'data1', // sa.data = 'data1'
},
}]
}).then(lists => { // renamed to lists to prevent shadowing the "list" model variable
/* ... */
});

Query records that does not have an entry in another table using Sequelize include clause

Given Users table and Ratings table
How do I query all user records from Users table that does not have any rating record in Ratings table using Sequelize include clause
Note: Sequelize version 5.x
Thanks in advance
You can do this in two ways depending on how your models are defined.
1. Get all Users along with Ratings by using Sequelize Eager Loading. Then filter where user does not have any ratings.
const users = Users.findAll({
include: [Ratings]
});
const filteredUsers = users.filter(user => user.ratings.length === 0);
2. Get all userIds from the Ratings table and then pass these userIds to the where clause using the notIn Sequelize operator
const ratings = Ratings.findAll({
attributes: ["userId"],
group: ["userId"]
});
const userIds = ratings.map(rating => rating.userId);
const filteredUsers = Users.findAll({
where: {
userId: { [Op.notIn]: userIds }
}
});
Try incorporating a sequelize literal in the where clause:
const ratings = Ratings.findAll({
attributes: ["userId"],
group: ["userId"],
where: {
$and: [
sequelize.literal(`NOT EXISTS (
SELECT 1 FROM Ratings r
WHERE r.userId = User.id
)`),
],
},
});
Assuming you have a relationship between Users and Ratings in your models, this can be accomplished in a single query by using a left outer join followed by a filter on the client side.
In your model definition:
Users.hasMany(Ratings, { foreignKey: 'user_id' });
Ratings.belongsTo(Users, { foreignKey: 'user_id' });
In your query:
const users = await Users.findAll({
include: [
{
model: Ratings,
required: false // left outer join
}
]
});
const usersWithoutRatings = users.filter(u => u.user_ratings.length === 0);

Convert SQL query to Sequelize Query Format

I'm new to Sequelize ORM. I would like to convert SQL query to Sequelize Query.
This is my SQL query, I want to convert this query to sequelize query:
SELECT * FROM `Posts` AS `Posts`
WHERE `Posts`.user_id IN
(SELECT `Follows`.receiver_id FROM `follows` AS `Follows`
WHERE `Follows`.user_id = user_id and `Follows`.status = "accept");
I have tried this but it does not return any data:
Posts
.findAll({ where: {
user_id: { [Op.in]: [{
include: [{
model: Follows,
attributes: ['receiver_id'],
where: {
user_id: user_id,
status:status
}
}]
}]
}
}})
.then(users => { res.send(users); })
After Executing above code it gives error in console
SELECT `event_id`, `user_id`, `event_message`, `e_imagepath`,
`createdAt`, `updatedAt`, `receiver_id`
FROM `Posts` AS `Posts`
WHERE `Posts`.`user_id` IN ('[object Object]');
I would like to convert SQL query to Sequelize Query.
You put your incude in the wrong position. Sequelize does not have a subquery feature as far I am aware of.
So you could do instead:
Posts
.findAll({ where: { user_id: user_id},
include: [{
model: Follows,
attributes: ['receiver_id'],
where: {
user_id: user_id,
status:status
}
}]
})
.then(users => { res.send(users); })
If the example above does not suits your need. You can also try to use a subquery by mixing raw SQL with Sequelize as the link below describes:
stackoverflow.com/questions/28286811/sequelize-subquery-as-field
This works fine.
router.get('/posts', function(req, res)
{
const user_id = req.session.user_id;
const status = "accept";
Posts.findAndCountAll({include:[{ model: Likes},{ model: Comments},{ model: Users}],
where:{user_id:{[Op.in]:[sequelize.literal('SELECT `Follows`.receiver_id FROM `follows` AS `Follows` WHERE `Follows`.user_id=1 and `Follows`.status="accept')]}}
})
.then((postdata)=>
{
Users.findAll({where:{user_id:user_id}})
.then((userdata)=>
{
res.send(postdata.rows)
// res.render('home',{title:'home',items:postdata.rows,user:userdata});
})
.catch((err)=>
{
})
})
.catch((err)=>
{
})
});

rewrite left outer join for sub queries in bookshelf.js

Note : I have not shared database schema as I am mainly looking for a help only w.r.t. last step which is 'left outer join' on 2 sub-queries.
select *
from
(select id
from Action
where id = 3) AS act1
left Outer Join
(select Action.name,
completed_At as completedAt,
deadline, notes,
ActionAssignedTo.action_Id as actionId,
from Action
inner join Employee
on Action.created_By_Id = Employee.id
and Employee.vendor_Id = 2
inner join ActionAssignedTo
on Action.id = ActionAssignedTo.action_Id
and ActionAssignedTo.action_Id = 3
where Action.created_By_Id = 7
group by Action.id
limit 2) AS act2
on act1.id = act2.actionId
I need to write this above query using Bookshelf
let options = {columns: [ 'Action.name', 'completed_At as completedAt',
'deadline', 'notes',
'ActionAssignedTo.action_Id as actionId',
]};
let action2 = new Action();
action2.query().innerJoin('Employee', function () {
this.on('Action.created_By_Id', 'Employee.id')
.andOn('Employee.vendor_Id', bookshelf.knex.raw(1));
});
action2.query().innerJoin('ActionAssignedTo', function () {
this.on('Action.id', 'ActionAssignedTo.action_Id')
.andOn('ActionAssignedTo.action_Id', bookshelf.knex.raw(5));
});
action2.query().where(function() {
this.where('Action.created_By_Id', empId)
});
action2.query().groupBy('Action.id');
action2.query().limit(2);
action2.query().columns(options.columns);
let action1;
action1 = Action.where('id', actionId);
action1.query().columns('id');
return bookshelf.knex.raw('select * from '
+ '(' + action1.query().toString() + ') AS act1'
+ ' left Outer Join '
+ '(' + action2.query().toString() + ') AS act2'
+ ' on act1.id = act2.actionId');
I am not keen on using bookshelf.knex.raw for using the left Outer Join as the output given by knex.raw and bookshelf differ.
Is there a way I can do the 'left Outer Join' directly using bookshelf library.
I looked into the code but it seems leftOuterJoin only takes table name as the first parameter and what I need is a query.
I think your main problem is that you're using Bookshelf like you would be using knex. Bookshelf is meant to be used with models you would define and then query on them.
Here is an example of what you should have as model
// Adding registry to avoid circular references
// Adding camelcase to get your columns names converted to camelCase
bookshelf.plugin(['bookshelf-camelcase', 'registry']);
// Reference: https://github.com/brianc/node-pg-types
// These two lines convert all bigint values coming from Postgres from JS string to JS integer.
// Removing these lines will mess up with Bookshelf count() methods and bigserial values
pg.types.setTypeParser(20, 'text', parseInt);
const Action = db.bookshelf.Model.extend({
tableName: 'Action',
createdBy: function createdBy() {
return this.belongsTo(Employee, 'id', 'created_By_Id');
},
assignedTo: function assignedTo() {
return this.hasMany(ActionAssignedTo, 'action_id');
},
});
const Employee = db.bookshelf.Model.extend({
tableName: 'Employee',
createdActions: function createdActions() {
return this.hasMany(Action, 'created_By_Id');
},
});
const ActionAssignedTo = db.bookshelf.Model.extend({
tableName: 'ActionAssignedTo',
action: function action() {
return this.belongsTo(Action, 'id', 'action_Id');
},
employee: function employee() {
return this.belongsTo(Employee, 'id', 'employee_Id');
},
});
module.exports = {
Action: db.bookshelf.model('Action', Action),
Employee: db.bookshelf.model('Employee', Employee),
ActionAssignedTo: db.bookshelf.model('ActionAssignedTo', ActionAssignedTo),
db,
};
You would then be able to fetch your results with a query like this
const Model = require('model.js');
Model.Action
.where({ id: 3 })
.fetchAll({ withRelated: ['createdBy', 'assignedTo', 'assignedTo.employee'] })
.then(data => {
// Do what you have to do
});
What your want to achieve is not possible with only one query in Bookshelf. You probably need to do a first query using knex to get a list of Action ids and then give them to Bookshelf.js
db.bookshelf.knex.raw(`
select ActionAssignedTo.action_Id as actionId,
from Action
inner join Employee
on Action.created_By_Id = Employee.id
and Employee.vendor_Id = ?
inner join ActionAssignedTo
on Action.id = ActionAssignedTo.action_Id
and ActionAssignedTo.action_Id = ?
where Action.created_By_Id = ?
group by Action.id
limit ?`,
[2, 3, 7, 2]
)
.then(result => {
const rows = result.rows;
// Do what you have to do
})
And then use the recovered Ids to get your Bookshelf query like this
Model.Action
.query(qb => {
qb.whereIn('id', rows);
})
.fetchAll({
withRelated: [{
'createdBy': qb => {
qb.columns(['id', 'firstname', 'lastname']);
},
'assignedTo': qb => {
qb.columns(['action_Id', 'employee_Id']);
},
'assignedTo.employee': qb => {
qb.columns(['id', 'firstname', 'lastname']);
},
}],
columns: ['id', 'name', 'completed_At', 'deadline', 'notes']
})
.fetchAll(data => {
// Do what you have to do
});
Note that the columns used for joins MUST BE in the columns list for each table. If you omit the columns, all the columns will be selected.
By default, Bookshelf will retrieve all columns and all root objects. The default is kind of LEFT OUTER JOIN.