Handle relationships except many-to-many

This commit is contained in:
Aurélien Georget 2016-08-05 15:51:55 +02:00
parent 4e6ca004b0
commit c79f55b0d9

View File

@ -7,6 +7,7 @@
// Public node modules. // Public node modules.
const _ = require('lodash'); const _ = require('lodash');
const mongoose = require('mongoose'); const mongoose = require('mongoose');
const mongooseUtils = require('mongoose/lib/utils');
const pluralize = require('pluralize'); const pluralize = require('pluralize');
// Local helpers. // Local helpers.
@ -58,6 +59,7 @@ module.exports = function (strapi) {
_.set(strapi, 'mongoose.collections', {}); _.set(strapi, 'mongoose.collections', {});
const loadedAttributes = _.after(_.size(strapi.models), function () { const loadedAttributes = _.after(_.size(strapi.models), function () {
console.log(strapi.mongoose.collections);
_.forEach(strapi.models, function (definition, model) { _.forEach(strapi.models, function (definition, model) {
try { try {
// Initialize lifecycle callbacks. // Initialize lifecycle callbacks.
@ -82,9 +84,13 @@ module.exports = function (strapi) {
// } // }
// }); // });
// }; // };
console.log(model);
// Generate schema without virtual populate console.log("NO VIRTUAL", _.omitBy(definition.loadedModel, model => {
const schema = mongoose.Schema(_.omitBy(definition.loadedModel, model => { return model.type === 'virtual';
}));
console.log("VIRTUAL", _.pickBy(definition.loadedModel, model => {
return model.type === 'virtual'; return model.type === 'virtual';
})); }));
@ -92,22 +98,25 @@ module.exports = function (strapi) {
_.forEach(_.pickBy(definition.loadedModel, model => { _.forEach(_.pickBy(definition.loadedModel, model => {
return model.type === 'virtual' return model.type === 'virtual'
}), (value, key) => { }), (value, key) => {
schema.virtual(key, { strapi.mongoose.collections[mongooseUtils.toCollectionName(definition.globalName)].schema.virtual(key.replace('_v', ''), {
ref: value.ref, ref: value.ref,
localField: '_id', localField: '_id',
foreignField: value.via foreignField: value.via,
justOne: value.justOne || false
}); });
}); });
schema.set('toObject', { console.log('------------------');
strapi.mongoose.collections[mongooseUtils.toCollectionName(definition.globalName)].schema.set('toObject', {
virtuals: true virtuals: true
}); });
schema.set('toJSON', { strapi.mongoose.collections[mongooseUtils.toCollectionName(definition.globalName)].schema.set('toJSON', {
virtuals: true virtuals: true
}); });
global[definition.globalName] = mongoose.model(definition.globalName, schema);; global[definition.globalName] = mongoose.model(definition.globalName, strapi.mongoose.collections[mongooseUtils.toCollectionName(definition.globalName)].schema);
// Push model to strapi global variables. // Push model to strapi global variables.
strapi.mongoose.collections[mongooseUtils.toCollectionName(definition.globalName)] = global[definition.globalName]; strapi.mongoose.collections[mongooseUtils.toCollectionName(definition.globalName)] = global[definition.globalName];
@ -164,6 +173,12 @@ module.exports = function (strapi) {
// Call this callback function after we are done parsing // Call this callback function after we are done parsing
// all attributes for relationships-- see below. // all attributes for relationships-- see below.
const done = _.after(_.size(definition.attributes), function () { const done = _.after(_.size(definition.attributes), function () {
// Generate schema without virtual populate
_.set(strapi.mongoose.collections, mongooseUtils.toCollectionName(definition.globalName) + '.schema', mongoose.Schema(_.omitBy(definition.loadedModel, model => {
return model.type === 'virtual';
})));
console.log('##################');
loadedAttributes(); loadedAttributes();
}); });
@ -181,6 +196,8 @@ module.exports = function (strapi) {
let FK; let FK;
console.log(model, verbose)
switch (verbose) { switch (verbose) {
case 'hasOne': case 'hasOne':
definition.loadedModel[name] = { definition.loadedModel[name] = {
@ -207,22 +224,33 @@ module.exports = function (strapi) {
break; break;
case 'belongsTo': case 'belongsTo':
definition.loadedModel[name] = { FK = _.find(definition.associations, { alias : name});
type: mongoose.Schema.Types.ObjectId,
ref: _.capitalize(details.model) if (FK && FK.nature === 'oneToOne') {
}; definition.loadedModel[name] = {
type: 'virtual',
ref: _.capitalize(details.model),
via: FK.via,
justOne: true
};
} else {
definition.loadedModel[name] = {
type: mongoose.Schema.Types.ObjectId,
ref: _.capitalize(details.model)
};
}
break; break;
case 'belongsToMany': case 'belongsToMany':
FK = _.find(definition.associations, { alias : name}); FK = _.find(definition.associations, { alias : name});
console.log(FK);
if (FK) { if (FK) {
definition.loadedModel[name] = { definition.loadedModel[name + '_v'] = {
type: 'virtual', type: 'virtual',
ref: _.capitalize(details.collection), ref: _.capitalize(details.collection),
via: FK via: FK.via
}; };
} else {
definition.loadedModel[name] = [{ definition.loadedModel[name] = [{
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
ref: _.capitalize(details.collection) ref: _.capitalize(details.collection)