From 1e8272b80f5ec728d0c574012c0e13edfb089ff9 Mon Sep 17 00:00:00 2001 From: Aurelsicoko Date: Thu, 22 Feb 2018 16:08:11 +0100 Subject: [PATCH] Handle new syntax for polymorphic in strapi-mongoose --- packages/strapi-mongoose/lib/index.js | 18 ++++++++++++++---- packages/strapi-utils/lib/models.js | 4 ++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/strapi-mongoose/lib/index.js b/packages/strapi-mongoose/lib/index.js index de3b248275..4b7ccf1bf1 100755 --- a/packages/strapi-mongoose/lib/index.js +++ b/packages/strapi-mongoose/lib/index.js @@ -104,16 +104,15 @@ module.exports = function (strapi) { .forEach(key => { collection.schema.pre(key, function (next) { if (this._mongooseOptions.populate && this._mongooseOptions.populate[association.alias]) { - if (association.nature === 'oneToMorph' || association.nature === 'manyToMorph') { + if (association.nature === 'oneToManyMorph' || association.nature === 'manyToManyMorph') { this._mongooseOptions.populate[association.alias].match = { [`${association.via}.${association.filter}`]: association.alias, [`${association.via}.kind`]: definition.globalId } } else { - this._mongooseOptions.populate[association.alias].path = `${association.alias}.${association.filter}`; + this._mongooseOptions.populate[association.alias].path = `${association.alias}.ref`; } } - next(); }); }); @@ -163,7 +162,18 @@ module.exports = function (strapi) { transform: function (doc, returned, opts) { morphAssociations.forEach(association => { if (Array.isArray(returned[association.alias]) && returned[association.alias].length > 0) { - returned[association.alias] = returned[association.alias].map(o => o[association.filter]); + // Reformat data by bypassing the many-to-many relationship. + switch (association.nature) { + case 'oneMorphToOne': + returned[association.alias] = returned[association.alias][0].ref; + break; + case 'manyMorphToOne': + returned[association.alias] = returned[association.alias].map(obj => obj.ref); + break; + default: + + } + } }); } diff --git a/packages/strapi-utils/lib/models.js b/packages/strapi-utils/lib/models.js index 0fec435c4b..a073fb2596 100755 --- a/packages/strapi-utils/lib/models.js +++ b/packages/strapi-utils/lib/models.js @@ -339,7 +339,7 @@ module.exports = { autoPopulate: _.get(association, 'autoPopulate', true), dominant: details.dominant !== true, plugin: association.plugin || undefined, - where: details.where, + filter: details.filter, }); } else if (association.hasOwnProperty('model') && association.model !== '*') { definition.associations.push({ @@ -351,7 +351,7 @@ module.exports = { autoPopulate: _.get(association, 'autoPopulate', true), dominant: details.dominant !== true, plugin: association.plugin || undefined, - where: details.where, + filter: details.filter, }); } else if (association.hasOwnProperty('collection') || association.hasOwnProperty('model')) { const pluginsModels = Object.keys(strapi.plugins).reduce((acc, current) => {