mirror of
https://github.com/strapi/strapi.git
synced 2025-11-09 14:51:29 +00:00
Declare a new load method in the entity service (#10930)
This commit is contained in:
parent
a0cdf693bb
commit
8e6de48dc6
@ -6,7 +6,6 @@ const {
|
|||||||
webhook: webhookUtils,
|
webhook: webhookUtils,
|
||||||
contentTypes: contentTypesUtils,
|
contentTypes: contentTypesUtils,
|
||||||
relations: relationsUtils,
|
relations: relationsUtils,
|
||||||
pagination: paginationUtils,
|
|
||||||
} = require('@strapi/utils');
|
} = require('@strapi/utils');
|
||||||
const uploadFiles = require('../utils/upload-files');
|
const uploadFiles = require('../utils/upload-files');
|
||||||
|
|
||||||
@ -16,21 +15,30 @@ const {
|
|||||||
updateComponents,
|
updateComponents,
|
||||||
deleteComponents,
|
deleteComponents,
|
||||||
} = require('./components');
|
} = require('./components');
|
||||||
const { transformParamsToQuery, pickSelectionParams } = require('./params');
|
const {
|
||||||
|
chainParamsTransformations,
|
||||||
|
transformCommonParams,
|
||||||
|
transformPaginationParams,
|
||||||
|
transformPublicationStateParams,
|
||||||
|
pickSelectionParams,
|
||||||
|
} = require('./params');
|
||||||
|
|
||||||
|
const transformParamsToQuery = (uid, params) => {
|
||||||
|
return chainParamsTransformations(params, [
|
||||||
|
// _q, _where, filters, etc...
|
||||||
|
transformCommonParams,
|
||||||
|
// page, pageSize, start, limit
|
||||||
|
transformPaginationParams,
|
||||||
|
// publicationState
|
||||||
|
transformPublicationStateParams(uid),
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
const { MANY_RELATIONS } = relationsUtils.constants;
|
const { MANY_RELATIONS } = relationsUtils.constants;
|
||||||
|
|
||||||
// TODO: those should be strapi events used by the webhooks not the other way arround
|
// TODO: those should be strapi events used by the webhooks not the other way arround
|
||||||
const { ENTRY_CREATE, ENTRY_UPDATE, ENTRY_DELETE } = webhookUtils.webhookEvents;
|
const { ENTRY_CREATE, ENTRY_UPDATE, ENTRY_DELETE } = webhookUtils.webhookEvents;
|
||||||
|
|
||||||
const paginateAndTransformToQuery = (uid, opts = {}) => {
|
|
||||||
// Paginate the opts
|
|
||||||
const paginatedOpts = paginationUtils.withDefaultPagination(opts);
|
|
||||||
|
|
||||||
// Transform the opts into a query & return it
|
|
||||||
return transformParamsToQuery(uid, paginatedOpts);
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = ctx => {
|
module.exports = ctx => {
|
||||||
const implementation = createDefaultImplementation(ctx);
|
const implementation = createDefaultImplementation(ctx);
|
||||||
|
|
||||||
@ -76,7 +84,7 @@ const createDefaultImplementation = ({ strapi, db, eventHub, entityValidator })
|
|||||||
|
|
||||||
const { params } = await this.wrapOptions(opts, { uid, action: 'find' });
|
const { params } = await this.wrapOptions(opts, { uid, action: 'find' });
|
||||||
|
|
||||||
const query = paginateAndTransformToQuery(uid, params);
|
const query = transformParamsToQuery(uid, params);
|
||||||
|
|
||||||
if (kind === 'singleType') {
|
if (kind === 'singleType') {
|
||||||
return db.query(uid).findOne(query);
|
return db.query(uid).findOne(query);
|
||||||
@ -88,7 +96,7 @@ const createDefaultImplementation = ({ strapi, db, eventHub, entityValidator })
|
|||||||
async findPage(uid, opts) {
|
async findPage(uid, opts) {
|
||||||
const { params } = await this.wrapOptions(opts, { uid, action: 'findPage' });
|
const { params } = await this.wrapOptions(opts, { uid, action: 'findPage' });
|
||||||
|
|
||||||
const query = paginateAndTransformToQuery(uid, params);
|
const query = transformParamsToQuery(uid, params);
|
||||||
|
|
||||||
return db.query(uid).findPage(query);
|
return db.query(uid).findPage(query);
|
||||||
},
|
},
|
||||||
@ -140,7 +148,7 @@ const createDefaultImplementation = ({ strapi, db, eventHub, entityValidator })
|
|||||||
async count(uid, opts) {
|
async count(uid, opts) {
|
||||||
const { params } = await this.wrapOptions(opts, { uid, action: 'count' });
|
const { params } = await this.wrapOptions(opts, { uid, action: 'count' });
|
||||||
|
|
||||||
const query = paginateAndTransformToQuery(uid, params);
|
const query = transformParamsToQuery(uid, params);
|
||||||
|
|
||||||
return db.query(uid).count(query);
|
return db.query(uid).count(query);
|
||||||
},
|
},
|
||||||
@ -248,4 +256,17 @@ const createDefaultImplementation = ({ strapi, db, eventHub, entityValidator })
|
|||||||
|
|
||||||
return db.query(uid).deleteMany(query);
|
return db.query(uid).deleteMany(query);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
load(uid, entity, field, params) {
|
||||||
|
const { attributes } = strapi.getModel(uid);
|
||||||
|
|
||||||
|
const attribute = attributes[field];
|
||||||
|
|
||||||
|
const loadParams =
|
||||||
|
attribute.type === 'relation'
|
||||||
|
? transformParamsToQuery(attribute.target, params)
|
||||||
|
: chainParamsTransformations(params, [transformCommonParams, transformPaginationParams]);
|
||||||
|
|
||||||
|
return db.query(uid).load(entity, field, loadParams);
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { pick } = require('lodash/fp');
|
const { pick, pipe, isNil } = require('lodash/fp');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
convertSortQueryParams,
|
convertSortQueryParams,
|
||||||
@ -15,47 +15,28 @@ const { contentTypes: contentTypesUtils } = require('@strapi/utils');
|
|||||||
|
|
||||||
const { PUBLISHED_AT_ATTRIBUTE } = contentTypesUtils.constants;
|
const { PUBLISHED_AT_ATTRIBUTE } = contentTypesUtils.constants;
|
||||||
|
|
||||||
// TODO: check invalid values / add defaults ....
|
// TODO: to remove once the front is migrated
|
||||||
const transformParamsToQuery = (uid, params = {}) => {
|
const convertOldQuery = params => {
|
||||||
const model = strapi.getModel(uid);
|
const obj = {};
|
||||||
|
|
||||||
const query = {};
|
Object.keys(params).forEach(key => {
|
||||||
|
if (key.startsWith('_')) {
|
||||||
|
obj[key.slice(1)] = params[key];
|
||||||
|
} else {
|
||||||
|
obj[key] = params[key];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const {
|
return obj;
|
||||||
start,
|
};
|
||||||
page,
|
|
||||||
pageSize,
|
const transformCommonParams = (params = {}) => {
|
||||||
limit,
|
const { _q, sort, filters, _where, fields, populate, ...query } = params;
|
||||||
sort,
|
|
||||||
filters,
|
|
||||||
fields,
|
|
||||||
populate,
|
|
||||||
publicationState,
|
|
||||||
_q,
|
|
||||||
_where,
|
|
||||||
...rest
|
|
||||||
} = params;
|
|
||||||
|
|
||||||
if (_q) {
|
if (_q) {
|
||||||
query._q = _q;
|
query._q = _q;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (page) {
|
|
||||||
query.page = Number(page);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pageSize) {
|
|
||||||
query.pageSize = Number(pageSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (start) {
|
|
||||||
query.offset = convertStartQueryParams(start);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (limit) {
|
|
||||||
query.limit = convertLimitQueryParams(limit);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sort) {
|
if (sort) {
|
||||||
query.orderBy = convertSortQueryParams(sort);
|
query.orderBy = convertSortQueryParams(sort);
|
||||||
}
|
}
|
||||||
@ -78,8 +59,50 @@ const transformParamsToQuery = (uid, params = {}) => {
|
|||||||
query.populate = convertPopulateQueryParams(populate);
|
query.populate = convertPopulateQueryParams(populate);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: move to convert-query-params ?
|
return { ...convertOldQuery(query), ...query };
|
||||||
if (publicationState && contentTypesUtils.hasDraftAndPublish(model)) {
|
};
|
||||||
|
|
||||||
|
const transformPaginationParams = (params = {}) => {
|
||||||
|
const { page, pageSize, start, limit, ...query } = params;
|
||||||
|
|
||||||
|
const isPagePagination = !isNil(page) || !isNil(pageSize);
|
||||||
|
const isOffsetPagination = !isNil(start) || !isNil(limit);
|
||||||
|
|
||||||
|
if (isPagePagination && isOffsetPagination) {
|
||||||
|
throw new Error(
|
||||||
|
'Invalid pagination attributes. You cannot use page and offset pagination in the same query'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (page) {
|
||||||
|
query.page = Number(page);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pageSize) {
|
||||||
|
query.pageSize = Number(pageSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (start) {
|
||||||
|
query.offset = convertStartQueryParams(start);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (limit) {
|
||||||
|
query.limit = convertLimitQueryParams(limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { ...convertOldQuery(query), ...query };
|
||||||
|
};
|
||||||
|
|
||||||
|
const transformPublicationStateParams = uid => (params = {}) => {
|
||||||
|
const contentType = strapi.getModel(uid);
|
||||||
|
|
||||||
|
if (!contentType) {
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { publicationState, ...query } = params;
|
||||||
|
|
||||||
|
if (publicationState && contentTypesUtils.hasDraftAndPublish(contentType)) {
|
||||||
const { publicationState = 'live' } = params;
|
const { publicationState = 'live' } = params;
|
||||||
|
|
||||||
const liveClause = {
|
const liveClause = {
|
||||||
@ -97,32 +120,19 @@ const transformParamsToQuery = (uid, params = {}) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const finalQuery = {
|
return { ...convertOldQuery(query), ...query };
|
||||||
...convertOldQuery(rest),
|
|
||||||
...query,
|
|
||||||
};
|
|
||||||
|
|
||||||
return finalQuery;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: to remove once the front is migrated
|
const chainParamsTransformations = (params, transformFunctions = []) => {
|
||||||
const convertOldQuery = params => {
|
return pipe(...transformFunctions)(params);
|
||||||
const obj = {};
|
|
||||||
|
|
||||||
Object.keys(params).forEach(key => {
|
|
||||||
if (key.startsWith('_')) {
|
|
||||||
obj[key.slice(1)] = params[key];
|
|
||||||
} else {
|
|
||||||
obj[key] = params[key];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return obj;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const pickSelectionParams = pick(['fields', 'populate']);
|
const pickSelectionParams = pick(['fields', 'populate']);
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
transformParamsToQuery,
|
transformCommonParams,
|
||||||
|
transformPublicationStateParams,
|
||||||
|
transformPaginationParams,
|
||||||
|
chainParamsTransformations,
|
||||||
pickSelectionParams,
|
pickSelectionParams,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,15 +1,11 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { omit } = require('lodash/fp');
|
|
||||||
|
|
||||||
module.exports = ({ strapi }) => {
|
module.exports = ({ strapi }) => {
|
||||||
const { isMorphRelation, isMedia } = strapi.plugin('graphql').service('utils').attributes;
|
const { isMorphRelation, isMedia } = strapi.plugin('graphql').service('utils').attributes;
|
||||||
const { transformArgs } = strapi.plugin('graphql').service('builders').utils;
|
const { transformArgs } = strapi.plugin('graphql').service('builders').utils;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
buildAssociationResolver: ({ contentTypeUID, attributeName }) => {
|
buildAssociationResolver: ({ contentTypeUID, attributeName }) => {
|
||||||
const { entityManager } = strapi.db;
|
|
||||||
|
|
||||||
const contentType = strapi.getModel(contentTypeUID);
|
const contentType = strapi.getModel(contentTypeUID);
|
||||||
const attribute = contentType.attributes[attributeName];
|
const attribute = contentType.attributes[attributeName];
|
||||||
|
|
||||||
@ -33,20 +29,11 @@ module.exports = ({ strapi }) => {
|
|||||||
usePagination: true,
|
usePagination: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Since we're using the entity-manager & not the entity-service to load the
|
const data = await strapi.entityService.load(
|
||||||
// association, we need to apply some transformation to the transformed args object
|
|
||||||
const entityManagerArgs = {
|
|
||||||
...omit(['start', 'filters'], transformedArgs),
|
|
||||||
where: transformedArgs.filters,
|
|
||||||
offset: transformedArgs.start,
|
|
||||||
};
|
|
||||||
|
|
||||||
// todo[v4]: should we move the .load to the entity service so we can use the same args everywhere?
|
|
||||||
const data = await entityManager.load(
|
|
||||||
contentTypeUID,
|
contentTypeUID,
|
||||||
parent,
|
parent,
|
||||||
attributeName,
|
attributeName,
|
||||||
entityManagerArgs
|
transformedArgs
|
||||||
);
|
);
|
||||||
|
|
||||||
// If this a polymorphic association, it returns the raw data
|
// If this a polymorphic association, it returns the raw data
|
||||||
|
|||||||
@ -1,7 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { omit } = require('lodash/fp');
|
|
||||||
|
|
||||||
module.exports = ({ strapi }) => ({
|
module.exports = ({ strapi }) => ({
|
||||||
buildComponentResolver: ({ contentTypeUID, attributeName }) => {
|
buildComponentResolver: ({ contentTypeUID, attributeName }) => {
|
||||||
const { transformArgs } = strapi.plugin('graphql').service('builders').utils;
|
const { transformArgs } = strapi.plugin('graphql').service('builders').utils;
|
||||||
@ -10,16 +8,7 @@ module.exports = ({ strapi }) => ({
|
|||||||
const contentType = strapi.contentTypes[contentTypeUID];
|
const contentType = strapi.contentTypes[contentTypeUID];
|
||||||
const transformedArgs = transformArgs(args, { contentType, usePagination: true });
|
const transformedArgs = transformArgs(args, { contentType, usePagination: true });
|
||||||
|
|
||||||
// Since we're using the entity-manager & not the entity-service to load the
|
return strapi.entityService.load(contentTypeUID, parent, attributeName, transformedArgs);
|
||||||
// association, we need to apply some transformation to the transformed args object
|
|
||||||
const entityManagerArgs = {
|
|
||||||
...omit(['start', 'filters'], transformedArgs),
|
|
||||||
where: transformedArgs.filters,
|
|
||||||
offset: transformedArgs.start,
|
|
||||||
};
|
|
||||||
|
|
||||||
// todo[v4]: should we move the .load to the entity service so we can use the same args everywhere?
|
|
||||||
return strapi.db.entityManager.load(contentTypeUID, parent, attributeName, entityManagerArgs);
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@ -2,6 +2,6 @@
|
|||||||
|
|
||||||
module.exports = ({ strapi }) => ({
|
module.exports = ({ strapi }) => ({
|
||||||
buildDynamicZoneResolver: ({ contentTypeUID, attributeName }) => async parent => {
|
buildDynamicZoneResolver: ({ contentTypeUID, attributeName }) => async parent => {
|
||||||
return strapi.db.entityManager.load(contentTypeUID, parent, attributeName);
|
return strapi.entityService.load(contentTypeUID, parent, attributeName);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user