I have an external library (Objection.js) that I'm using in my Node.js app. I created a base model class that extends Objection's Model
class for my entity models:
const { Model } = require('objection')
class ModelBase extends Model {
// implementation not important for this question
}
In my model classes that extend my base, there are times, specifically when coding the relationMappings
, where I have to access properties/enumerations on the Model
base class. I could do this in my extended models like this:
const ModelBase = require('./model-base')
class SomeModel extends ModelBase {
static get relationMappings () {
const SomeOtherModel = require('./some-other-model')
return {
someOtherModel: {
relation: ModelBase.BelongsToOneRelation,
modelClass: SomeOtherModel,
// etc.
}
}
}
}
Notice the relation: ModelBase.BelongsToOneRelation
line. This works, but I think it is misleading, as BelongsToOneRelation
is NOT a member of ModelBase
. What seems more explicit and correct to me is to import/require the Model
from Objection so I can access the BelongsToOneRelation
from there, like:
const { Model } = require('objection')
// other code just like above until the relationMappings...
relation: Model.BelongsToOneRelation
I prefer this approach. Will it cause problems, like require loops or the JavaScript version of a circular dependency, if I import/require a class that is already in scope in the inheritance chain?