How to use Sequelize findAll() method

How to use Sequelize findAll() method

The findAll method of the Sequelize Model class is used to retrieve all records from a table in your database. It returns an array of instances of the model, which can be used to access the data of the retrieved records.

Here is an example of how to use the findAll method to retrieve all users from a users table:

const { User } = require('./models');

async function getUsers() {
  const users = await User.findAll();
  return users;
}

The findAll method accepts an options object that can be used to specify criteria for filtering the results and control the data that is returned. Here are a few examples of options that you can use with findAll:

  • where: A where clause to filter the results.
  • order: An order clause to specify the sort order of the results.
  • attributes: An array of attributes to include or exclude in the returned results.
  • offset: The number of records to skip before returning the results.
  • limit: The maximum number of records to return.

Here is an example of using the findAll method with some options:

const { User } = require('./models');

async function getUsers() {
  const users = await User.findAll({
    where: { active: true },
    order: [['name', 'ASC']],
    attributes: ['name', 'email'],
    offset: 10,
    limit: 10
  });
  return users;
}

Let me know if you have any more questions about using the findAll method with Sequelize.

Here are a few more things to keep in mind when using the findAll method with Sequelize:

  • Pagination: The offset and limit options can be used to implement pagination in your application. By specifying an offset and a limit, you can retrieve a specific "page" of results from your database.

  • Eager Loading: By default, the findAll method does not load associations for the returned records. If you want to include associations in the results, you can use the include option to specify the associations that you want to load. This is known as "eager loading" and can be useful for reducing the number of database queries that your application needs to perform.

const { User } = require('./models');

async function getUsers() {
  const users = await User.findAll({
    include: [{ model: Task, as: 'tasks' }]
  });
  return users;
}
  • Raw Queries: If you want to execute a raw SQL query that is not supported by the Sequelize API, you can use the sequelize.query method to execute the query directly. This method returns the raw results of the query, which can then be processed as needed.
const { sequelize } = require('./models');

async function getUsers() {
  const [results, metadata] = await sequelize.query('SELECT * FROM users WHERE active = :active', {
    replacements: { active: true },
    type: sequelize.QueryTypes.SELECT
  });
  return results;
}
  • Asynchronous Execution: The findAll method is asynchronous and returns a Promise, which means that you need to use the await keyword or the then method to handle the results of the query.
const { User } = require('./models');

async function getUsers() {
  const users = await User.findAll();
  console.log(users);
}

getUsers();
const { User } = require('./models');

function getUsers() {
  User.findAll()
    .then(users => console.log(users))
    .catch(error => console.error(error));
}

getUsers();
  • Error Handling: If there is an error executing the findAll query, the returned Promise will be rejected and you will need to handle the error using the catch method or a try/catch block.
const { User } = require('./models');

async function getUsers() {
  try {
    const users = await User.findAll();
    console.log(users);
  } catch (error) {
    console.error(error);
  }
}

getUsers();
  • Caching: If you want to cache the results of the findAll query to improve performance, you can use a caching library like redis or memcached. You can then store the results of the query in the cache and retrieve them from the cache on subsequent requests, rather than executing the query against the database each time.

I hope this gives you a better understanding of how to use the findAll method with Sequelize.


  • Date: