-
Notifications
You must be signed in to change notification settings - Fork 362
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
RelationDefinition applyScope/applyMapping #172
Conversation
Add the ability to set static/dynamic filtering (where, order, limit, skip, fields …) and property mapping/transfer (de-normalization) for hasMany/hasOne.
Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test." |
The path looks great. I wonder if we can have a better name than Meanwhile, I think we should decouple the |
Thanks @raymondfeng you are right, they can be seen as foreign/composite keys. However, you can also use them to transfer properties to de-normalize for example. Since the mapping will only be applied during creation, you should obviously limit this to more or less immutable properties/data. Because you can handle the mapping dynamically, it can also be used to have related items 'inherit' certain (default) attributes from a parent, unless the item already defines it. In short, it will come into play in all kinds of situations. So mapping denotes a more broad concept - what about My app definitely needs the dynamic scoping, so I hope we can merge in something like this soon. |
What about |
I updated my comment. Yeah, I'm fine with that - relatedProperties perhaps? However, I always try to keep option keys short and sweet. |
I guess I'm cool with |
Sounds good, thanks! Any thoughts on a public method to set the scope dynamically? For this to work, it should be picked up by applyScope too. Here's what I'm doing in a beforeRemote handler at the moment:
And then look for __dynamicScope in my scope function. It would be even better if we could standardize this. |
I see what you mean. So you would like to inject values from the context into the query. That's pretty common. Would the instance level dynamic scope only apply to relations? What about other scopes? |
Yes, that is indeed common. Another issue #168 refers to a more generalized approach for other scopes. On the other hand, handling the exact behavior is hard to really generalize. For example, to which relations would the dynamic scope pertain? Inside the dynamic scope function 'this' refers to the current relation definition, so that makes it easy to inspect and act accordingly. If we were to go for a more general approach for relations, we'd need something like:
Where scope can be an object or function. Not sure how to handle #168 but I think it might be similar:
|
@raymondfeng no matter how the public API will turn out, I think it's safe to apply this pull request as a low-level means. What do you think? |
Sure |
@fabien Did you have chance to rename 'mapping' to 'properties'? |
@raymondfeng yes, just committed. |
@fabien Thank you so much. One last thing, would you please sign off the commits per https://github.com/strongloop/loopback/blob/master/CONTRIBUTING.md#signing-patches? |
Signed-off-by: Fabien Franzen <[email protected]>
@raymondfeng just signed off - will do it immediately next time! |
@raymondfeng I deliberately didn't implement belongsTo scope/property, however, I'm working on that right now. Unfortunately, I've hit a bug it seems. Using the MongoDB connector, a hasManyThrough setup implicitly coerces the foreignKeys to type 'string', instead of ObjectID - even though I explicitly set: Looking into it further, it appears this definition that causes the implicit to-string coercion:
When creating the modelTo-instance at |
Add the ability to set static/dynamic filtering (where, order, limit, skip, fields …) and property mapping/transfer (de-normalization) for hasMany/hasOne. This enables LoopBack beforeRemote handlers to dynamically change the relation scope. See the comment in the tests for the rationale.
Based upon this, it should be easy to create a public method to set this scope on the instance-level (
setScope({ ... })
).