sequelize / sequelize/sequelize

Please Add Support for Scopes on Model Create or on Model Instance Reload

Open
#17,456 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending-approval
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:

  1. create the object, but don't care about the created object,
  2. 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
}

See https://github.com/icefoganalytics/wrap/blob/c329afc0ce279622d812d389c7d46481d7fc65b1/api/src/services/users/ensure-from-auth0-token-service.ts

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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.