Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize configuration to retrieve total count with details

I am working with node sequelize with postgres database. I am loading paginated records to my UI, now I need to get total records count with the same query which I am using to retrieve paginate records. Anyone, please give the sample sequelize configuration to do the same. Please see my expected sample postgres query to clarify my question

SELECT count(*) over() as total ,name FROM students  WHERE gender='male' LIMIT 2 

Thanks in advance

like image 575
Jamsheer Avatar asked Nov 29 '17 06:11

Jamsheer


People also ask

How do you get total count in Sequelize?

Sequelize Model. count() method is used to generate and execute a SELECT COUNT SQL query to your connected database. The method accepts an object of options that you can define to modify the generated query. You need to create a Sequelize Model for the above table and call the count() method from there.

How does include work in Sequelize?

Vanilla Include Sequelize is smart enough to pull in any rows from your Artists model that are associated with your Albums. Include takes an array of objects. These objects have properties like model , as , and where which tell Sequelize how to look for associated rows.

Where is Sequelize?

Sequelize where option accepts an object describing the WHERE clause to add to your generated SQL query. For a WHERE clause with a simple condition, you can add a single property to the where object. The property name will be the column name and the property value will be the value you use to filter the query.


4 Answers

If you want to avoid specifying SQL, you can also use findAndCountAll

like image 93
redgeoff Avatar answered Oct 13 '22 07:10

redgeoff


You can't do that with sequelize, but you can do through 2 separate queries, one to get the data you need and the other one to get the total count.

first one :

await Model.findAll({ where: { columnName: condition }});

second one :

await Model.count({ where: { columnName: condition }});

if you want to do that in one query which is maybe not the best way (because you adding metadata for each result which is not metadata related to the model) you can create a raw query like that:

await sequelize.query('select count(*) over(), name from table where condition', { model: Model });

I hope my explanation will help you :),

Have a nice day!

like image 33
Adrien De Peretti Avatar answered Oct 13 '22 09:10

Adrien De Peretti


You can use findAndCountAll for this purpose.

findAndCountAll - Search for multiple elements in the database, returns both data and total count

Here is an example:

const getStudents = async params => {
  const { count, rows: students } = await Student.findAndCountAll({
    where: {
      gender: 'male',
    },
    limit: DEFAULT_PAGE_SIZE,
    order: [['id', 'ASC']],
    ...params,
  });

  return { count, students };
}

params is an object with limit and offset properties that overwrite default limit and offset.

Note that you may need to pass distinct: true as well in case you include other models in a query.

like image 10
Yuriy Rypka Avatar answered Oct 13 '22 09:10

Yuriy Rypka


You can count data in two ways

  1. With data (findAndCountAll)
  2. Without Data (count)

1: With data

const students = await students.findAndCountAll({
  where: {
     gender = "male"
  }
});
console.log(students)

It will return Count students where gender is male and data of that students

2: Without Data

const students = await students.count({
  where: {
     gender = "male"
  }
});
console.log(students)

It will return only Count students where gender is male

like image 1
Aryan Avatar answered Oct 13 '22 07:10

Aryan