itsfadnis/jsonapi-client

Possible circular reference when defining models?

Closed this issue · 1 comments

Hi, I'm interested in your library, but quick glance over sample models code urges question if it's possible to define them in separate js files for cleaner code (after all, you can easily have dozens of models with lots of business logic in non-trivial application). I.e. DriversLicense references Person constant in it's belongsTo declaration, but Person class definition references DriversLicense class constant in it's hasMany definition, which will likely give circular reference error when those models are in separate files and use import to import constants to use them.
Maybe it's possible to create separate models registry file like that:

import Person from './Person'
import DriversLicense from './DriversLicense'
...etc

export default const Models = {
  Person: Person,
  DriversLicense: DriversLicense,
  ...
}

this way any single model file can just import Models from './Models' and avoid those errors?

P.S. The issue and solution are described in more detail i.e. in this great blog post

Hi @pineapplethief, thanks for your interest.

And yes you are right. The sample models (Person, Address & DriversLicense) if defined in separate files would certainly cause circular references.

I am using this library in a very large project which has more than 50+ models, which I have defined in separate files. I personally avoid defining circular dependencies between modules because it just complicates things.

This library however isn't opinionated when it comes to structuring your models so it is completely upto you to decide what suits you best.

Also thank you for sharing the blog post, a good read 👍