diff --git a/src/Lucid/Model/Base.js b/src/Lucid/Model/Base.js index e861b1e3..d6b835f2 100644 --- a/src/Lucid/Model/Base.js +++ b/src/Lucid/Model/Base.js @@ -12,6 +12,7 @@ const _ = require('lodash') const moment = require('moment') const VanillaSerializer = require('../Serializers/Vanilla') +const { ioc } = require('../../../lib/iocResolver') const DATE_FORMAT = 'YYYY-MM-DD HH:mm:ss' /** @@ -120,6 +121,21 @@ class BaseModel { return moment(value).format(DATE_FORMAT) } + /** + * Resolves the serializer for the current model. + * + * If serializer is a string, then it is resolved using + * the Ioc container, otherwise it is assumed that + * a `class` is returned. + * + * @method resolveSerializer + * + * @returns {Class} + */ + static resolveSerializer () { + return typeof (this.Serializer) === 'string' ? ioc.use(this.Serializer) : this.Serializer + } + /** * This method is executed when toJSON is called on a * model or collection of models. The value received @@ -264,7 +280,8 @@ class BaseModel { * @return {Object} */ toJSON () { - return new this.constructor.Serializer(this, null, true).toJSON() + const Serializer = this.constructor.resolveSerializer() + return new Serializer(this, null, true).toJSON() } } diff --git a/src/Lucid/QueryBuilder/index.js b/src/Lucid/QueryBuilder/index.js index c694e2e0..b9c83c8c 100644 --- a/src/Lucid/QueryBuilder/index.js +++ b/src/Lucid/QueryBuilder/index.js @@ -355,10 +355,8 @@ class QueryBuilder { await this.Model.$hooks.after.exec('fetch', modelInstances) } - /** - * Return an instance of active model serializer - */ - return new this.Model.Serializer(modelInstances) + const Serializer = this.Model.resolveSerializer() + return new Serializer(modelInstances) } /** @@ -490,7 +488,8 @@ class QueryBuilder { /** * Return an instance of active model serializer */ - return new this.Model.Serializer(modelInstances, pages) + const Serializer = this.Model.resolveSerializer() + return new Serializer(modelInstances, pages) } /** diff --git a/src/Lucid/Relations/BelongsToMany.js b/src/Lucid/Relations/BelongsToMany.js index 6f7d409c..742b4a15 100644 --- a/src/Lucid/Relations/BelongsToMany.js +++ b/src/Lucid/Relations/BelongsToMany.js @@ -637,7 +637,7 @@ class BelongsToMany extends BaseRelation { * @return {Object} @multiple([key=String, values=Array, defaultValue=Null]) */ group (relatedInstances) { - const Serializer = this.RelatedModel.Serializer + const Serializer = this.RelatedModel.resolveSerializer() const transformedValues = _.transform(relatedInstances, (result, relatedInstance) => { const foreignKeyValue = relatedInstance.$sideLoaded[`pivot_${this.foreignKey}`] diff --git a/src/Lucid/Relations/HasMany.js b/src/Lucid/Relations/HasMany.js index 6615ccde..6701811d 100644 --- a/src/Lucid/Relations/HasMany.js +++ b/src/Lucid/Relations/HasMany.js @@ -71,7 +71,7 @@ class HasMany extends BaseRelation { * @return {Object} @multiple([key=String, values=Array, defaultValue=Null]) */ group (relatedInstances) { - const Serializer = this.RelatedModel.Serializer + const Serializer = this.RelatedModel.resolveSerializer() const transformedValues = _.transform(relatedInstances, (result, relatedInstance) => { const foreignKeyValue = relatedInstance[this.foreignKey] diff --git a/src/Lucid/Relations/HasManyThrough.js b/src/Lucid/Relations/HasManyThrough.js index 3d97f7b6..f7c1238f 100644 --- a/src/Lucid/Relations/HasManyThrough.js +++ b/src/Lucid/Relations/HasManyThrough.js @@ -217,7 +217,7 @@ class HasManyThrough extends BaseRelation { * @return {Object} @multiple([key=String, values=Array, defaultValue=Null]) */ group (relatedInstances) { - const Serializer = this.RelatedModel.Serializer + const Serializer = this.RelatedModel.resolveSerializer() const transformedValues = _.transform(relatedInstances, (result, relatedInstance) => { const foreignKeyValue = relatedInstance.$sideLoaded[`through_${this.foreignKey}`]