Handle many-to-many relationships

This commit is contained in:
Aurélien Georget 2016-08-08 11:12:09 +02:00
parent c79f55b0d9
commit 18c3a51fe9
2 changed files with 48 additions and 26 deletions

View File

@ -59,7 +59,6 @@ module.exports = function (strapi) {
_.set(strapi, 'mongoose.collections', {});
const loadedAttributes = _.after(_.size(strapi.models), function () {
console.log(strapi.mongoose.collections);
_.forEach(strapi.models, function (definition, model) {
try {
// Initialize lifecycle callbacks.
@ -84,15 +83,6 @@ module.exports = function (strapi) {
// }
// });
// };
console.log(model);
console.log("NO VIRTUAL", _.omitBy(definition.loadedModel, model => {
return model.type === 'virtual';
}));
console.log("VIRTUAL", _.pickBy(definition.loadedModel, model => {
return model.type === 'virtual';
}));
// Add virtual key to provide populate and reverse populate
_.forEach(_.pickBy(definition.loadedModel, model => {
@ -106,8 +96,6 @@ module.exports = function (strapi) {
});
});
console.log('------------------');
strapi.mongoose.collections[mongooseUtils.toCollectionName(definition.globalName)].schema.set('toObject', {
virtuals: true
});
@ -178,7 +166,6 @@ module.exports = function (strapi) {
return model.type === 'virtual';
})));
console.log('##################');
loadedAttributes();
});
@ -196,8 +183,6 @@ module.exports = function (strapi) {
let FK;
console.log(model, verbose)
switch (verbose) {
case 'hasOne':
definition.loadedModel[name] = {
@ -207,7 +192,7 @@ module.exports = function (strapi) {
break;
case 'hasMany':
FK = _.find(definition.associations, { alias : name});
FK = _.find(definition.associations, { alias : name });
if (FK) {
definition.loadedModel[name] = {
@ -224,7 +209,7 @@ module.exports = function (strapi) {
break;
case 'belongsTo':
FK = _.find(definition.associations, { alias : name});
FK = _.find(definition.associations, { alias : name });
if (FK && FK.nature === 'oneToOne') {
definition.loadedModel[name] = {
@ -242,15 +227,15 @@ module.exports = function (strapi) {
break;
case 'belongsToMany':
FK = _.find(definition.associations, { alias : name});
console.log(FK);
if (FK) {
definition.loadedModel[name + '_v'] = {
type: 'virtual',
ref: _.capitalize(details.collection),
via: FK.via
};
FK = _.find(definition.associations, { alias : name });
if (FK && _.isUndefined(details.via)) {
definition.loadedModel[name] = {
type: 'virtual',
ref: _.capitalize(FK.collection),
via: utilsModels.getVia(name, details)
};
} else {
definition.loadedModel[name] = [{
type: mongoose.Schema.Types.ObjectId,
ref: _.capitalize(details.collection)

View File

@ -89,8 +89,10 @@ module.exports = {
types.current = 'collection';
if (relatedAttribute.hasOwnProperty('collection')) {
if (relatedAttribute.hasOwnProperty('collection') && relatedAttribute.hasOwnProperty('via')) {
types.other = 'collection';
} else if (relatedAttribute.hasOwnProperty('collection') && !relatedAttribute.hasOwnProperty('via')) {
types.other = 'collectionD';
} else if (relatedAttribute.hasOwnProperty('model')) {
types.other = 'model';
}
@ -116,6 +118,27 @@ module.exports = {
} else if (association.hasOwnProperty('model')) {
types.current = 'model';
// We have to find if they are a model linked to this key
_.forIn(models, model => {
_.forIn(model.attributes, attribute => {
if (attribute.hasOwnProperty('via') && attribute.via === key) {
if (attribute.hasOwnProperty('collection')) {
types.other = 'collection';
// Break loop
return false;
} else if (attribute.hasOwnProperty('model')) {
types.other = 'modelD';
// Break loop
return false;
}
}
});
});
} else if (association.hasOwnProperty('collection')) {
types.current = 'collectionD';
// We have to find if they are a model linked to this key
_.forIn(models, model => {
_.forIn(model.attributes, attribute => {
@ -161,6 +184,16 @@ module.exports = {
nature: 'manyToMany',
verbose: 'belongsToMany'
};
} else if (types.current === 'collectionD' && types.other === 'collection' || types.current === 'collection' && types.other === 'collectionD') {
return {
nature: 'manyToMany',
verbose: 'belongsToMany'
};
} else if (types.current === 'collectionD' && types.other === '') {
return {
nature: 'manyWay',
verbose: 'belongsToMany'
};
} else if (types.current === 'model' && types.other === '') {
return {
nature: 'oneWay',
@ -215,5 +248,9 @@ module.exports = {
autoPopulate: (_.get(association, 'autoPopulate') || _.get(strapi.config, 'jsonapi.enabled')) === true
});
}
},
getVia: function (attribute, association) {
return _.findKey(strapi.models[association.model || association.collection].attributes, { via: attribute });
}
};