sequelize / sequelize/sequelize

Support throwing when accessing attribute not requested

Open
#12,108 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

type: feature
Dominant language
TypeScript
Stars
30.4k
Forks
4.3k
Avg merge
1d 6h
Merged PRs (30d)
68

Description

Issue Description

Is your feature request related to a problem? Please describe.

When selecting specific fields of a module for performance reasons it is really easy to introduce bugs if you use a field you didn't request. For example:

const model = await MyModel.findOne({where: ...}, attributes: ['field1', 'field2]);

// other code

if (model.field3 > 3) {
  // do something important
}
Describe the solution you'd like

It would be great to have an option on the model that would throw an error if attempting to access an field that wasn't requested

const MyModel = sequelize.define(
    'my_model',
    {
        field1: {},
        field2: {},
        field3: {},
    },
    {
        throwOnMissingAttribute: true
    }
);

const model = await MyModel.findOne({where: ...}, attributes: ['field1', 'field2]);

// other code

if (model.field3 > 3) { // this now throws
  // do something important
}

Why should this be in Sequelize

This is a feature in ActiveRecord

We use Sequelize heavily and can count a number of important bugs that would have been prevented if we had this in place

Describe alternatives/workarounds you've considered

We could monkey patch this in or just not specify attributes - just difficult to do both of those

Additional context

Issue Template Checklist

Is this issue dialect-specific?
  • No. This issue is relevant to Sequelize as a whole.
  • Yes. This issue only applies to the following dialect(s): XXX, YYY, ZZZ
Would you be willing to resolve this issue by submitting a Pull Request?
  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files or tests. Start by locating Sequelize model attribute access and the findOne attributes option, then determine how the proposed model setting should behave for unrequested fields. Done means the behavior is specified, covered by tests, and documented for users.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs, typescript
Domain
backend, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.