Once again I am very surprised by the power of Sequelize!
Let’s say I want to retrieve all the « locations » with their associated user.
I do:
I get:
But what’s interesting is not that! It’s how Sequelize does this under the hood.
Some ORMs in this kind of situation make X requests:
- A
SELECT *to fetch all the rows - Then a
SELECTper foreign key to fetch the nested attribute
Here, Sequelize makes a single request
SELECT `t_location`.`id`, `t_location`.`user_id`, `t_location`.`latitude`, `t_location`.`longitude`, `t_location`.`altitude`, `t_location`.`accuracy`, `t_location`.`created_at`, `t_location`.`updated_at`,
`user`.`id` AS `user.id`, `user`.`firstname` AS `user.firstname`, `user`.`lastname` AS `user.lastname`, `user`.`email` AS `user.email`, `user`.`birthdate` AS `user.birthdate`, `user`.`language` AS `user.language`, `user`.`password_hash` AS `user.password_hash`, `user`.`role` AS `user.role`, `user`.`created_at` AS `user.created_at`, `user`.`updated_at` AS `user.updated_at`
FROM `t_location` AS `t_location`
LEFT OUTER JOIN `t_user` AS `user` ON `t_location`.`user_id` = `user`.`id`;
Then it reassembles the nested object using its « table.attribute » format in the « AS »
It’s super performant!















