Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Referencing primaryKey of a model as opposed to the entire object #305

Open
nojibe opened this issue Oct 4, 2024 · 0 comments

Comments

@nojibe
Copy link

nojibe commented Oct 4, 2024

When referencing model(s), an object might be designed to only hold the primaryKey(s) of the referenced model(s). For example, Mirage when relating the models blogPost and comments,...

import { createServer, Model, hasMany } from "miragejs"

createServer({
  models: {
    blogPost: Model.extend({
      comments: hasMany(), // relation to `comments` model
    }),

    comment: Model,
  },
})

...creates several helpful keys for the association. In addition, if I wanted the association to be named differently, I could create the association like so: hasMany("different name for association here").

blogPost.commentIds // [1, 2, 3]
blogPost.commentIds = [2, 3] // updates the relationship
blogPost.comments // array of related comments
blogPost.comments = [comment1, comment2] // updates the relationship
blogPost.newComment(attrs) // new unsaved comment
blogPost.createComment(attrs) // new saved comment (comment.blogPostId is set)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant