Handle new syntax for polymorphic in strapi-mongoose

This commit is contained in:
Aurelsicoko 2018-02-22 16:08:11 +01:00
parent 946f0f17b3
commit 1e8272b80f
2 changed files with 16 additions and 6 deletions

View File

@ -104,16 +104,15 @@ module.exports = function (strapi) {
.forEach(key => { .forEach(key => {
collection.schema.pre(key, function (next) { collection.schema.pre(key, function (next) {
if (this._mongooseOptions.populate && this._mongooseOptions.populate[association.alias]) { 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 = { this._mongooseOptions.populate[association.alias].match = {
[`${association.via}.${association.filter}`]: association.alias, [`${association.via}.${association.filter}`]: association.alias,
[`${association.via}.kind`]: definition.globalId [`${association.via}.kind`]: definition.globalId
} }
} else { } else {
this._mongooseOptions.populate[association.alias].path = `${association.alias}.${association.filter}`; this._mongooseOptions.populate[association.alias].path = `${association.alias}.ref`;
} }
} }
next(); next();
}); });
}); });
@ -163,7 +162,18 @@ module.exports = function (strapi) {
transform: function (doc, returned, opts) { transform: function (doc, returned, opts) {
morphAssociations.forEach(association => { morphAssociations.forEach(association => {
if (Array.isArray(returned[association.alias]) && returned[association.alias].length > 0) { 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:
}
} }
}); });
} }

View File

@ -339,7 +339,7 @@ module.exports = {
autoPopulate: _.get(association, 'autoPopulate', true), autoPopulate: _.get(association, 'autoPopulate', true),
dominant: details.dominant !== true, dominant: details.dominant !== true,
plugin: association.plugin || undefined, plugin: association.plugin || undefined,
where: details.where, filter: details.filter,
}); });
} else if (association.hasOwnProperty('model') && association.model !== '*') { } else if (association.hasOwnProperty('model') && association.model !== '*') {
definition.associations.push({ definition.associations.push({
@ -351,7 +351,7 @@ module.exports = {
autoPopulate: _.get(association, 'autoPopulate', true), autoPopulate: _.get(association, 'autoPopulate', true),
dominant: details.dominant !== true, dominant: details.dominant !== true,
plugin: association.plugin || undefined, plugin: association.plugin || undefined,
where: details.where, filter: details.filter,
}); });
} else if (association.hasOwnProperty('collection') || association.hasOwnProperty('model')) { } else if (association.hasOwnProperty('collection') || association.hasOwnProperty('model')) {
const pluginsModels = Object.keys(strapi.plugins).reduce((acc, current) => { const pluginsModels = Object.keys(strapi.plugins).reduce((acc, current) => {