mirror of
https://github.com/strapi/strapi.git
synced 2026-01-03 18:53:34 +00:00
Merge pull request #9705 from strapi/i18n/populate-custom-fields
I18n/populate custom fields
This commit is contained in:
commit
f8186505b1
@ -592,6 +592,15 @@ module.exports = async ({ models, target }, ctx, { selfFinalize = false } = {})
|
||||
if (relation) {
|
||||
// Extract raw JSON data.
|
||||
attrs[association.alias] = relation.toJSON ? relation.toJSON(options) : relation;
|
||||
|
||||
if (_.isArray(association.populate)) {
|
||||
const { alias, populate } = association;
|
||||
const pickPopulate = entry => _.pick(entry, populate);
|
||||
|
||||
attrs[alias] = _.isArray(attrs[alias])
|
||||
? _.map(attrs[alias], pickPopulate)
|
||||
: pickPopulate(attrs[alias]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@ -72,52 +72,29 @@ const populateBareAssociations = (definition, options = {}) => {
|
||||
.reduce((acc, val) => acc.concat(val), []);
|
||||
};
|
||||
|
||||
const hasCustomPopulate = assoc => _.isArray(assoc.populate);
|
||||
|
||||
const formatAssociationPopulate = ({ assoc }, options = {}) => {
|
||||
const { prefix = '', ...queryOptions } = options;
|
||||
|
||||
const path = `${prefix}${assoc.alias}`;
|
||||
const assocModel = strapi.db.getModelByAssoc(assoc);
|
||||
|
||||
const polyAssocs = hasCustomPopulate(assoc)
|
||||
? []
|
||||
: assocModel.associations
|
||||
.filter(polyAssoc => isPolymorphic({ assoc: polyAssoc }))
|
||||
.map(polyAssoc => {
|
||||
return formatPolymorphicPopulate(
|
||||
{ assoc: polyAssoc },
|
||||
{ prefix: `${path}.`, ...queryOptions }
|
||||
);
|
||||
});
|
||||
const polyAssocs = assocModel.associations
|
||||
.filter(polyAssoc => isPolymorphic({ assoc: polyAssoc }))
|
||||
.map(polyAssoc => {
|
||||
return formatPolymorphicPopulate(
|
||||
{ assoc: polyAssoc },
|
||||
{ prefix: `${path}.`, ...queryOptions }
|
||||
);
|
||||
});
|
||||
|
||||
const components = hasCustomPopulate(assoc)
|
||||
? []
|
||||
: populateComponents(assocModel, { prefix: `${path}.`, ...queryOptions });
|
||||
const components = populateComponents(assocModel, { prefix: `${path}.`, ...queryOptions });
|
||||
|
||||
const populateOpts = bindPopulateQueries(
|
||||
[path],
|
||||
queryOptionsToQueryMap(queryOptions, { model: assocModel })
|
||||
);
|
||||
|
||||
return [selectPopulateFields({ assoc, assocModel }, populateOpts), ...polyAssocs, ...components];
|
||||
};
|
||||
|
||||
const selectPopulateFields = ({ assoc, assocModel }, populateOpts) => {
|
||||
if (!hasCustomPopulate(assoc)) return populateOpts;
|
||||
|
||||
const columns = assoc.populate
|
||||
.filter(name => _.has(assocModel.attributes, name))
|
||||
.concat('id')
|
||||
.map(name => `${assocModel.collectionName}.${name}`);
|
||||
|
||||
return Object.keys(populateOpts).reduce((opts, key) => {
|
||||
opts[key] = qb => {
|
||||
qb.column(columns);
|
||||
populateOpts[key](qb);
|
||||
};
|
||||
return opts;
|
||||
}, {});
|
||||
return [populateOpts, ...polyAssocs, ...components];
|
||||
};
|
||||
|
||||
const populateComponents = (definition, options = {}) => {
|
||||
|
||||
@ -192,6 +192,10 @@ module.exports = async ({ models, target }, ctx) => {
|
||||
};
|
||||
};
|
||||
|
||||
const associations = definition.associations.filter(
|
||||
association => !isPolymorphicAssoc(association)
|
||||
);
|
||||
|
||||
schema.options.toObject = schema.options.toJSON = {
|
||||
virtuals: true,
|
||||
transform: function(doc, returned) {
|
||||
@ -255,6 +259,24 @@ module.exports = async ({ models, target }, ctx) => {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
associations.forEach(association => {
|
||||
const relation = returned[association.alias];
|
||||
|
||||
if (relation) {
|
||||
// Extract raw JSON data.
|
||||
returned[association.alias] = relation.toJSON ? relation.toJSON() : relation;
|
||||
|
||||
if (_.isArray(association.populate)) {
|
||||
const { alias, populate } = association;
|
||||
const pickPopulate = entry => _.pick(entry, populate);
|
||||
|
||||
returned[alias] = _.isArray(returned[alias])
|
||||
? _.map(returned[alias], pickPopulate)
|
||||
: pickPopulate(returned[alias]);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@ -43,7 +43,6 @@ module.exports = ({ model, strapi }) => {
|
||||
const populate = {
|
||||
path: ast.alias,
|
||||
options: populateOptions,
|
||||
select: _.isArray(ast.populate) ? ast.populate.join(' ') : null,
|
||||
};
|
||||
|
||||
if (
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user