Navigate back to the homepage

Node.js sequelize query examples

Infinitbility
Node
May 5th, 2021 · 1 min read
Node.js sequelize query examples

Hello Friends,

Welcome To Infinitbility!

This article help you to write query in node with sequelize. here you get examples query of insert, update, select, delete, and join query with code snippest.

Let’s start today topic Node.js sequelize query examples Or how to write query in node sequelize

Table of content

  1. Insert query
  2. Update query
  3. Select query
  4. Delete query
  5. Join or Eager query

Insert query

Node sequelize provide create() method to create new record or row in table also return row id and row details.

Node sequelize insert query example

1const jane = await User.create({ firstName: "Jane", lastName: "Doe" });
2console.log("Jane's auto-generated ID:", jane.id);

Update query

Node sequelize provide update() method to update details of row or record.

Node sequelize update query example

1// Change everyone without a last name to "Doe"
2await User.update({ lastName: "Doe" }, {
3 where: {
4 lastName: null
5 }
6});

Select query

Node sequelize provide findAll() method to select or retrive records from table.

Node sequelize select query example

1const users = User.findAll({
2 attributes: ['first_name', 'last_name'],
3 where: {
4 id: 2
5 }
6});

Delete query

Node sequelize provide destroy() method to delete records from table.

Node sequelize delete or destroy query example

1// Delete everyone named "Jane"
2await User.destroy({
3 where: {
4 first_name: "Jane"
5 }
6});

Join or Eager query

When you want use join or eager query in node sequelize then first you have to declare relation or association between tables.

Sequelize relation or association

Sequelize provides four types of associations that should be combined to create them:

  • The HasOne association
  • The BelongsTo association
  • The HasMany association
  • The BelongsToMany association

relation or association in node sequelize example.

1const A = sequelize.define('A', /* ... */);
2const B = sequelize.define('B', /* ... */);
3
4A.hasOne(B); // A HasOne B
5A.belongsTo(B); // A BelongsTo B
6A.hasMany(B); // A HasMany B
7A.belongsToMany(B, { through: 'C' }); // A BelongsToMany B through the junction table C

Inner query example

Node sequelize provide include or required key to do inner join.

Node sequelize inner query example

1await User.findAll({
2 include: [{
3 model: Task,
4 required: true,
5 }]
6});

Left query example

Node sequelize provide include key to do left join.

Node sequelize left query example

1await User.findAll({
2 include: [{
3 model: Task // will create a left join
4 }]
5});

Right query example

Node sequelize provide include and right key to do right join.

Node sequelize right query example

1await User.findAll({
2 include: [{
3 model: Task,
4 right: true // will create a right join
5 }]
6});

Outer query example

Node sequelize provide include and required key to do outer join. default query is outer join but if mantion required then it’s change to inner join.

Node sequelize outer query example

1await User.findAll({
2 include: {
3 model: Task,
4 required: false
5 }
6});

Thanks for reading…

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Tutorials

React Native

Categories

More articles from Infinitbility

Laravel change string case

Laravel change string case

how to change string case in laravel

May 4th, 2021 · 1 min read
Laravel query examples

Laravel query examples

Laravel CRUD ( create, retrieve, update, and delete ), and join query examples

May 3rd, 2021 · 1 min read
© 2020–2021 Infinitbility
Disclaimer
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]