hanlde morphToOne populate & re-add legacy way of querying DZs

This commit is contained in:
Convly 2022-11-15 15:14:28 +01:00
parent 605e5b2009
commit 8c2fb155b8
2 changed files with 32 additions and 10 deletions

View File

@ -446,6 +446,11 @@ const morphToMany = async (input, ctx) => {
.where({ .where({
[joinColumn.name]: referencedValues, [joinColumn.name]: referencedValues,
...(joinTable.on || {}), ...(joinTable.on || {}),
// If the populateValue contains an "on" property,
// only populate the types defined in it
...('on' in populateValue
? { [morphColumn.typeColumn.name]: Object.keys(populateValue.on) }
: {}),
}) })
.orderBy([joinColumn.name, 'order']) .orderBy([joinColumn.name, 'order'])
.execute({ mapResults: false }); .execute({ mapResults: false });
@ -482,14 +487,10 @@ const morphToMany = async (input, ctx) => {
continue; continue;
} }
if (on && on[type]) {
Object.assign(typePopulate, on[type]);
}
const qb = db.entityManager.createQueryBuilder(type); const qb = db.entityManager.createQueryBuilder(type);
const rows = await qb const rows = await qb
.init(typePopulate) .init(on && type in on ? on[type] : typePopulate)
.addSelect(`${qb.alias}.${idColumn.referencedColumn}`) .addSelect(`${qb.alias}.${idColumn.referencedColumn}`)
.where({ [idColumn.referencedColumn]: ids }) .where({ [idColumn.referencedColumn]: ids })
.execute({ mapResults: false }); .execute({ mapResults: false });
@ -546,6 +547,8 @@ const morphToOne = async (input, ctx) => {
}, {}); }, {});
const map = {}; const map = {};
const { on, ...typePopulate } = populateValue;
for (const type of Object.keys(idsByType)) { for (const type of Object.keys(idsByType)) {
const ids = idsByType[type]; const ids = idsByType[type];
@ -558,7 +561,7 @@ const morphToOne = async (input, ctx) => {
const qb = db.entityManager.createQueryBuilder(type); const qb = db.entityManager.createQueryBuilder(type);
const rows = await qb const rows = await qb
.init(populateValue) .init(on && type in on ? on[type] : typePopulate)
.addSelect(`${qb.alias}.${idColumn.referencedColumn}`) .addSelect(`${qb.alias}.${idColumn.referencedColumn}`)
.where({ [idColumn.referencedColumn]: ids }) .where({ [idColumn.referencedColumn]: ids })
.execute({ mapResults: false }); .execute({ mapResults: false });

View File

@ -17,6 +17,7 @@ const {
isPlainObject, isPlainObject,
cloneDeep, cloneDeep,
get, get,
mergeAll,
} = require('lodash/fp'); } = require('lodash/fp');
const _ = require('lodash'); const _ = require('lodash');
const parseType = require('./parse-type'); const parseType = require('./parse-type');
@ -185,14 +186,13 @@ const convertPopulateObject = (populate, schema) => {
return acc; return acc;
} }
if (subPopulate && subPopulate.on) { if (subPopulate && 'on' in subPopulate) {
return { return {
...acc, ...acc,
[key]: { [key]: {
...subPopulate,
on: Object.entries(subPopulate.on).reduce( on: Object.entries(subPopulate.on).reduce(
(newTypeSubPopulate, [type, typeSubPopulate]) => ({ (acc, [type, typeSubPopulate]) => ({
...newTypeSubPopulate, ...acc,
[type]: convertNestedPopulate(typeSubPopulate, strapi.getModel(type)), [type]: convertNestedPopulate(typeSubPopulate, strapi.getModel(type)),
}), }),
{} {}
@ -201,6 +201,25 @@ const convertPopulateObject = (populate, schema) => {
}; };
} }
// TODO: Deprecated way of handling dynamic zone populate queries. It's kept as is,
// as removing it could break existing user queries but should be removed in V5.
if (attribute.type === 'dynamiczone') {
const populates = attribute.components
.map((uid) => strapi.getModel(uid))
.map((schema) => convertNestedPopulate(subPopulate, schema))
.map((populate) => (populate === true ? {} : populate)) // cast boolean to empty object to avoid merging issues
.filter((populate) => populate !== false);
if (isEmpty(populates)) {
return acc;
}
return {
...acc,
[key]: mergeAll(populates),
};
}
// NOTE: Retrieve the target schema UID. // NOTE: Retrieve the target schema UID.
// Only handles basic relations, medias and component since it's not possible // Only handles basic relations, medias and component since it's not possible
// to populate with options for a dynamic zone or a polymorphic relation // to populate with options for a dynamic zone or a polymorphic relation