sequelize / sequelize/sequelize
Please Add Support for Scopes on Model Create or on Model Instance Reload
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 30.4k
- Forks
- 4.3k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 68
Description
Issue Creation Checklist
- I understand that my issue will be automatically closed if I don't fill in the requested information
- I have read the contribution guidelines
Feature Description
Describe the feature you'd like to see implemented
I really like scopes, but I'm finding them hard to use in certain cases.
I want to create a model instance and apply some scopes to the initially created object.
I have to do it in multiple steps, and it feels kind of confusing since I have to:
- create the object, but don't care about the created object,
- have to look it up right after creating it, but handle the possibility that it no longer exists.
e.g.
import { auth0Integration } from "@/integrations"
import { User } from "@/models"
async function ensureUserFromAuth0Token(token: string) {
const { auth0Subject, email, firstName, lastName } = await auth0Integration.getUserInfo(token)
const existingUser = await User.withScope(["withPositions", "withTeams"]).findOne({
where: { authSubject: auth0Subject },
})
if (existingUser) return existingUser
await User.create({
authSubject: auth0Subject,
email,
firstName,
lastName,
})
const newUser = await User.withScope(["withPositions", "withTeams"]).findOne({
where: { authSubject: auth0Subject },
rejectOnEmpty: true,
})
return newUser
}
I'd expect to be able to do either:
a)
return User.withScope(["withPositions", "withTeams"]).create({
authSubject: auth0Subject,
email,
firstName,
lastName,
})
or b)
const newUser = User.create({
authSubject: auth0Subject,
email,
firstName,
lastName,
})
return newUser.reload({ scopes: ["withPositions", "withTeams"] })
Describe why you would like this feature to be added to Sequelize
This would make using Sequelize a bit more intuitive and convenient.
Is this feature dialect-specific?
- No. This feature is relevant to Sequelize as a whole.
- Yes. This feature only applies to the following dialect(s):
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 will need guidance.
- No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
- No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in implementing my feature.
Indicate your interest in the addition of this feature by adding the 👍 reaction. Comments such as "+1" will be removed.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the model APIs shown in the issue: withScope, create, and reload. Compare the two proposed usage forms and determine the intended scope behavior after creation or reload. Done means one supported approach is documented and verified for the requested scoped associations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- node.js, typescript
- Domain
- backend, database
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100