1 min read

Model Querying in Sequelize ORM

Sequelization is the process of connecting a Node.js application to an Object Relational Mapper for better database synchronization.These are some basic queries used in sequelize to work with relational databases more easily.
Model Querying in Sequelize ORM

Sequelize provides various methods to assist in querying your database for data.

The Model.create() the method is a shorthand for building an unsaved instance with Model.build() and saving the instance with instance.save().

const user =await User.create({  username:'alice123',  isAdmin:true},

{ fields:['username']});// let's assume the default of isAdmin is false

console.log(user.username);// 'alice123'

console.log(user.isAdmin);// false

Simple SELECT queries

You can read the whole table from the database with the findAll method:

// Find all users

const users =await User.findAll();

Specifying attributes for SELECT queries

To select only some attributes, you can use the attributes option:

Model.findAll({
  attributes: ['foo', 'bar']
});
SELECT foo, bar FROM ...

Attributes can be renamed using a nested array:

Model.findAll({
  attributes: ['foo', ['bar', 'baz'], 'qux']
});
SELECT foo, bar AS baz, qux FROM ...

Read more...

Sequelize
An easy-to-use multi SQL dialect ORM for Node.js
https://sequelize.org/master/manual/model-querying-basics.html

#probyto #probytoai #datascience #machinelearning #python #artificialintelligence #ai #dataanalytics #data #bigdata #deeplearning #programming #datascientist #technology #coding #datavisualization #computerscience #pythonprogramming #analytics #tech #dataanalysis #iot #programmer #statistics #developer #ml #business #innovation #coder #dataanalyst #sql #sequelize

Subscribe and follow us for latest news in Data Science and Machine learning and stay updated!
Facebook: https://facebook.com/probyto
Twitter: https://twitter.com/probyto
LinkedIn: https://linkedin.com/company/probyto
Instagram: https://instagram.com/probyto