jsonapi-suite / jsorm

Javascript ORM for JSONAPI
MIT License
68 stars 15 forks source link

Allow polymorphic relationships #71

Open jannishuebl opened 6 years ago

jannishuebl commented 6 years ago

Hello,

In my current application I would like to have a Model, which has an polymorphic association. Is is covered by json:api spec. At least I could not find something that prohibits us to do something like that.

Could jsorm support something like that?

eg:

class Book extends ApplicationRecord {
  static jsonapiType = 'books';

  title = hasOne(['sub-title', 'main-title']);
}

for sure the example does not make to much sense, but it should point out what I mean.

Cheers,

Jannis

richmolj commented 6 years ago

At some point I think we should have built-in support, but right now there are workarounds. Something like:

class Person extends ApplicationRecord {
  static jsonapiType = 'people'

  visas = hasMany()
  mastercards = hasMany()

  get creditCards() {
    return this.visas.concat(this.mastercards)
  }
}

// response will include credit_cards association containing types 'visa' and 'mastercard'
let { data } = await Person.includes('credit_cards')
data.visas // just visa credit cards
data.creditCards // all types of credit cards

Would love to see more built-in support in the future.

uday160386 commented 6 years ago

Hi,

I am using JSORM to construct a test script. I want to check on the roadmap of built in support for polymorphic resource fix.

richmolj commented 6 years ago

@uday160386 possibly in Q4 this year, but the snippet above should get you running right now. Nothing really prevents polymorphic relationships, we can just add better sugar.