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

M2m accessors #279

Merged
merged 3 commits into from
Sep 12, 2019
Merged

M2m accessors #279

merged 3 commits into from
Sep 12, 2019

Conversation

jawadst
Copy link
Collaborator

@jawadst jawadst commented Sep 12, 2019

This PR for #232 allows using an accessor (as) when defining a many-to-many relationship while keeping the value in the original field to behave the same as ForeignKey with as.

Example definition:

User.fields = {
    id: attr(),
    projects_id: many({ to: 'Project', relatedName: 'users', as: 'projects' }),
};

which allows accessing the related projects with:

session.User.create({ id: 'u0', projects_id: ['p0', 'p1'] });

session.User.withId('u0').projects // QuerySet<Project>
session.User.withId('u0').projects_id // Array of IDs (['p0', 'p1'])

The PR contains the following modifications:

  • Pass as to the virtual field of the M2M relationship so that we can populate the right field when refreshing the M2M values
  • Update the QuerySet on the field named as instead of the original field name if configured
  • Keep the original field from props as is in create/update when an accessor is configured
  • Update to the README to add an example of a fk and many with an accessor

@codecov-io
Copy link

codecov-io commented Sep 12, 2019

Codecov Report

Merging #279 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #279      +/-   ##
==========================================
+ Coverage   99.81%   99.81%   +<.01%     
==========================================
  Files          14       14              
  Lines        1080     1082       +2     
  Branches      214      218       +4     
==========================================
+ Hits         1078     1080       +2     
  Misses          2        2
Impacted Files Coverage Δ
src/fields.js 100% <ø> (ø) ⬆️
src/Model.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5848e90...85f0b21. Read the comment docs.

@haveyaseen
Copy link
Member

Wow, this is a perfect PR! Thank you so much for considering even the edge cases. I'll also add a test for the update check and add a little whitespace.

@haveyaseen haveyaseen merged commit 6a2c914 into redux-orm:master Sep 12, 2019
@jawadst
Copy link
Collaborator Author

jawadst commented Sep 12, 2019

@haveyaseen Thanks for reviewing and completing my PR!

@jawadst jawadst deleted the m2m-accessors branch September 12, 2019 16:22
@haveyaseen haveyaseen added this to Done in Version 1.0 Sep 12, 2019
@haveyaseen haveyaseen added the type: Enhancement This intends to enhance the project. label Sep 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: Enhancement This intends to enhance the project.
Projects
Version 1.0
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants