1 min read

Model Querying - Finders in sequelize

Sequelize automatically wraps everything in proper instance objects after the database returns results. In a few cases, when there are too many results, this wrapping can be inefficient.
Model Querying - Finders in sequelize

Finder methods are the ones that generate SELECT queries.

findAll:

The findAll method is already known from the previous tutorial. It generates a standard  SELECT query that will retrieve all entries from the table (unless restricted by something like a where clause, for example).

findByPk:

The findByPk method obtains only a single entry from the table, using the provided primary key.

const project =awaitProject.findByPk(123);

if(project ===null){

 console.log('Not found!');

}else

{

console.log(project instanceofProject);

// true// Its primary key is 123}

findOne

The findOne method obtains the first entry it finds (that fulfills the optional query options, if provided).

const project =await Project.findOne({where:{ title:'My Title'}});

if(project ===null){

console.log('Not found!');

}else

{  

console.log(project instanceofProject);// true  

console.log(project.title);// 'My Title'}

Read more..

Sequelize
An easy-to-use multi SQL dialect ORM for Node.js
https://sequelize.org/master/manual/model-querying-finders.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 #sequelize #queryFinders

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