Sling Academy
Home/Node.js/Sequelize.js: How to exclude some fields from the result

Sequelize.js: How to exclude some fields from the result

Last updated: December 29, 2023

Introduction

Sequelize is an Object-Relational Mapping (ORM) library for Node.js that allows developers to write robust and scalable database code with ease. One common requirement when working with databases is the ability to control the selection of fields retrieved from a query. This capability is essential for optimizing data load, increasing the efficiency of data transfer, and securing sensitive information. In this guide, we’ll explore how to use Sequelize to exclude specific fields from the results of a query.

Basic Usage with the attributes Option

const users = await User.findAll({
  attributes: { exclude: ['password'] }
});

The code above is an illustrative example of how to exclude a field (in this case, ‘password’) when retrieving all user records from the database.

Excluding Fields in Associations

const usersWithPosts = await User.findAll({
  include: [{
    model: Post,
    attributes: { exclude: ['content'] }
  }]
});

Here we demonstrate how to exclude fields from associated models, such as excluding the ‘content’ field from posts when retrieving users.

Dynamic Field Exclusion Based on Conditions

const users = await User.findAll({
  attributes: (user) => user.isAdmin ? { exclude: ['password'] } : { exclude: ['password', 'email'] }
});

In this advanced example, we conditionally exclude fields based on properties of the user instance. This showcases the flexibility of Sequelize in adjusting query results dynamically.

Utilizing Raw SQLQueries for Field Exclusion

const [results, metadata] = await sequelize.query('SELECT id, username FROM Users');

For scenarios where more control is required, executing raw SQL queries through Sequelize allows for full customization of the selected fields.

Conclusion

This tutorial covered several ways to exclude fields from query results in Sequelize.js, ranging from straightforward options for simple use cases to more sophisticated techniques for complex scenarios. Mastering these methods can greatly optimize your application’s database interactions and enhance data security.

Next Article: How to use Sequelize CLI

Previous Article: Sequelize: How to Update Record and Return the Result

Series: Sequelize.js Tutorials

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates