vuex-orm icon indicating copy to clipboard operation
vuex-orm copied to clipboard

define a morph map for morphTo relations?

Open JeffBeltran opened this issue 4 years ago • 3 comments

Is your feature request related to a problem? Please describe...

morphTo relies on the Polymorphic type to be the same name that is defined as the model's entity. if we use the documentation example

commentable: this.morphTo('commentable_id', 'commentable_type')

the function expects commentable_type to match an entity name. (either videos or posts). But if an api returns a different name, say video or post, the function would fail.

Describe the solution you'd like

I think the best experience would be something similar to

commentable: this.morphTo('commentable_id', 'commentable_type', {'foo': 'videos', 'bar': 'posts'))

Where you pass in an object,where the key is an expected value from api and value is the vuex-orm entity name for the matching value. You could also retain the current default behavior by having it use the value listed if no matching value is found in the "morphMap" object

Describe alternatives you've considered

the workaround for now is 1) update entity to match the type returned from api (however not sure if this will cause me issues as i just started using this package) 2) update the api type to return value (not a good option but if you control api why not...)

JeffBeltran avatar Jul 01 '20 16:07 JeffBeltran

Note that with some APIs such as Django Rest Framework the 'content type' is an ID which can vary between installations.

I ran into the same issue as you but since I had control over the API I added a string field for the content type and an endpoint for the mapping (to help with post/put requests, which require the dynamic ID based on the entity name).

[
  {
    "id": 39,
    "content_type_name": "logs"
  }
]

yardley avatar Aug 19 '20 23:08 yardley

I also have the same problem with ruby on rails, because commentable_type should store Video(not videos) in db by default!

wuyuedefeng avatar Oct 30 '20 02:10 wuyuedefeng

I have that problem too. Is there any update regarding this?

leonceaklin avatar Mar 21 '21 14:03 leonceaklin