mirror of
https://github.com/strapi/strapi.git
synced 2025-11-14 01:02:04 +00:00
(tmp) init morph populate
This commit is contained in:
parent
933f646a5a
commit
4dcfe4ab40
@ -470,6 +470,8 @@ const morphToMany = 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];
|
||||||
|
|
||||||
@ -480,10 +482,14 @@ 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(populateValue)
|
.init(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 });
|
||||||
@ -579,7 +585,16 @@ const morphToOne = async (input, ctx) => {
|
|||||||
|
|
||||||
// TODO: Omit limit & offset to avoid needing a query per result to avoid making too many queries
|
// TODO: Omit limit & offset to avoid needing a query per result to avoid making too many queries
|
||||||
const pickPopulateParams = (populate) => {
|
const pickPopulateParams = (populate) => {
|
||||||
const fieldsToPick = ['select', 'count', 'where', 'populate', 'orderBy', 'filters', 'ordering'];
|
const fieldsToPick = [
|
||||||
|
'select',
|
||||||
|
'count',
|
||||||
|
'where',
|
||||||
|
'populate',
|
||||||
|
'orderBy',
|
||||||
|
'filters',
|
||||||
|
'ordering',
|
||||||
|
'on',
|
||||||
|
];
|
||||||
|
|
||||||
if (populate.count !== true) {
|
if (populate.count !== true) {
|
||||||
fieldsToPick.push('limit', 'offset');
|
fieldsToPick.push('limit', 'offset');
|
||||||
|
|||||||
@ -6,17 +6,17 @@
|
|||||||
* Converts the standard Strapi REST query params to a more usable format for querying
|
* Converts the standard Strapi REST query params to a more usable format for querying
|
||||||
* You can read more here: https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html#filters
|
* You can read more here: https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html#filters
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
isNil,
|
||||||
|
toNumber,
|
||||||
|
isInteger,
|
||||||
has,
|
has,
|
||||||
isEmpty,
|
isEmpty,
|
||||||
isObject,
|
isObject,
|
||||||
isPlainObject,
|
isPlainObject,
|
||||||
cloneDeep,
|
cloneDeep,
|
||||||
get,
|
get,
|
||||||
mergeAll,
|
|
||||||
isNil,
|
|
||||||
toNumber,
|
|
||||||
isInteger,
|
|
||||||
} = require('lodash/fp');
|
} = require('lodash/fp');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const parseType = require('./parse-type');
|
const parseType = require('./parse-type');
|
||||||
@ -185,22 +185,19 @@ const convertPopulateObject = (populate, schema) => {
|
|||||||
return acc;
|
return acc;
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: This is a temporary solution for dynamic zones that should be
|
if (subPopulate && subPopulate.on) {
|
||||||
// fixed when we'll implement a more accurate way to query them
|
|
||||||
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 {
|
return {
|
||||||
...acc,
|
...acc,
|
||||||
[key]: mergeAll(populates),
|
[key]: {
|
||||||
|
...subPopulate,
|
||||||
|
on: Object.entries(subPopulate.on).reduce(
|
||||||
|
(newTypeSubPopulate, [type, typeSubPopulate]) => ({
|
||||||
|
...newTypeSubPopulate,
|
||||||
|
[type]: convertNestedPopulate(typeSubPopulate, strapi.getModel(type)),
|
||||||
|
}),
|
||||||
|
{}
|
||||||
|
),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user