mirror of
https://github.com/strapi/strapi.git
synced 2025-07-23 09:00:19 +00:00
refactor relation findNew route
This commit is contained in:
parent
c010656521
commit
7f521c9348
@ -1,64 +1,85 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { prop, pick } = require('lodash/fp');
|
const { prop, isEmpty } = require('lodash/fp');
|
||||||
const { PUBLISHED_AT_ATTRIBUTE } = require('@strapi/utils').contentTypes.constants;
|
const { PUBLISHED_AT_ATTRIBUTE } = require('@strapi/utils').contentTypes.constants;
|
||||||
|
|
||||||
const { getService } = require('../utils');
|
const { getService } = require('../utils');
|
||||||
|
const { validateFindNew } = require('./validation/relations');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
async find(ctx) {
|
async findNew(ctx) {
|
||||||
const { model, targetField } = ctx.params;
|
const { model, targetField } = ctx.params;
|
||||||
const { _component, ...query } = ctx.request.query;
|
|
||||||
const { idsToOmit } = ctx.request.body;
|
|
||||||
|
|
||||||
if (!targetField) {
|
await validateFindNew(ctx.request.query);
|
||||||
return ctx.badRequest();
|
|
||||||
}
|
|
||||||
|
|
||||||
const modelDef = _component ? strapi.getModel(_component) : strapi.getModel(model);
|
const { component, entityId, idsToOmit, page = 1, pageSize = 10, q } = ctx.request.query;
|
||||||
|
|
||||||
|
const sourceModel = component || model;
|
||||||
|
|
||||||
|
const modelDef = strapi.getModel(sourceModel);
|
||||||
if (!modelDef) {
|
if (!modelDef) {
|
||||||
return ctx.notFound('model.notFound');
|
return ctx.badRequest("The model doesn't exist");
|
||||||
}
|
}
|
||||||
|
|
||||||
const attribute = modelDef.attributes[targetField];
|
const attribute = modelDef.attributes[targetField];
|
||||||
if (!attribute || attribute.type !== 'relation') {
|
if (!attribute || attribute.type !== 'relation') {
|
||||||
return ctx.badRequest('targetField.invalid');
|
return ctx.badRequest("This relational field doesn't exist");
|
||||||
}
|
}
|
||||||
|
|
||||||
const target = strapi.getModel(attribute.target);
|
const targetedModel = strapi.getModel(attribute.target);
|
||||||
|
|
||||||
if (!target) {
|
const offset = Math.max(page - 1, 0) * pageSize;
|
||||||
return ctx.notFound('target.notFound');
|
const limit = Number(pageSize);
|
||||||
}
|
|
||||||
|
|
||||||
if (idsToOmit && Array.isArray(idsToOmit)) {
|
const modelConfig = component
|
||||||
query.filters = {
|
|
||||||
$and: [
|
|
||||||
{
|
|
||||||
id: {
|
|
||||||
$notIn: idsToOmit,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
].concat(query.filters || []),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const entityManager = getService('entity-manager');
|
|
||||||
|
|
||||||
const entities = await entityManager.find(query, target.uid, []);
|
|
||||||
|
|
||||||
if (!entities) {
|
|
||||||
return ctx.notFound();
|
|
||||||
}
|
|
||||||
|
|
||||||
const modelConfig = _component
|
|
||||||
? await getService('components').findConfiguration(modelDef)
|
? await getService('components').findConfiguration(modelDef)
|
||||||
: await getService('content-types').findConfiguration(modelDef);
|
: await getService('content-types').findConfiguration(modelDef);
|
||||||
|
const mainField = prop(`metadatas.${targetField}.edit.mainField`, modelConfig) || 'id';
|
||||||
|
|
||||||
const field = prop(`metadatas.${targetField}.edit.mainField`, modelConfig) || 'id';
|
const query = strapi.db.queryBuilder(targetedModel.uid);
|
||||||
const pickFields = [field, 'id', target.primaryKey, PUBLISHED_AT_ATTRIBUTE];
|
|
||||||
|
|
||||||
ctx.body = entities.map(pick(pickFields));
|
if (q) {
|
||||||
|
query.search(q);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isEmpty(idsToOmit)) {
|
||||||
|
query.where({ id: { $notIn: idsToOmit } });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entityId) {
|
||||||
|
const joinTable = strapi.db.metadata.get(sourceModel).attributes[targetField].joinTable;
|
||||||
|
const sourceColumn = component ? joinTable.joinColumn.name : joinTable.inverseJoinColumn.name;
|
||||||
|
const targetColumn = component ? joinTable.inverseJoinColumn.name : joinTable.joinColumn.name;
|
||||||
|
|
||||||
|
// Select ids of targeted entities already having a relation with _entityId
|
||||||
|
const knexSubQuery = strapi.db
|
||||||
|
.queryBuilder(joinTable.name)
|
||||||
|
.select([targetColumn])
|
||||||
|
.where({ [sourceColumn]: entityId })
|
||||||
|
.getKnexQuery();
|
||||||
|
|
||||||
|
query.where({ id: { $notIn: knexSubQuery } });
|
||||||
|
}
|
||||||
|
|
||||||
|
const { count } = await query
|
||||||
|
.clone()
|
||||||
|
.count()
|
||||||
|
.first()
|
||||||
|
.execute();
|
||||||
|
const entities = await query
|
||||||
|
.select([mainField, 'id', PUBLISHED_AT_ATTRIBUTE])
|
||||||
|
.orderBy(mainField)
|
||||||
|
.offset(offset)
|
||||||
|
.limit(limit)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
ctx.body = {
|
||||||
|
results: entities,
|
||||||
|
pagination: {
|
||||||
|
page: Number(page),
|
||||||
|
pageSize: Number(pageSize),
|
||||||
|
total: count,
|
||||||
|
},
|
||||||
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const { yup, validateYupSchema } = require('@strapi/utils');
|
||||||
|
|
||||||
|
const validateFindNewSchema = yup
|
||||||
|
.object()
|
||||||
|
.shape({
|
||||||
|
component: yup.string(),
|
||||||
|
entityId: yup.strapiID(),
|
||||||
|
q: yup.string(),
|
||||||
|
omitIds: yup.array().of(yup.strapiID()),
|
||||||
|
page: yup
|
||||||
|
.number()
|
||||||
|
.integer()
|
||||||
|
.min(1),
|
||||||
|
pageSize: yup
|
||||||
|
.number()
|
||||||
|
.integer()
|
||||||
|
.min(1)
|
||||||
|
.max(100),
|
||||||
|
})
|
||||||
|
.noUnknown()
|
||||||
|
.required();
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
validateFindNew: validateYupSchema(validateFindNewSchema, { strict: false }),
|
||||||
|
};
|
@ -80,9 +80,9 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: 'POST',
|
method: 'GET',
|
||||||
path: '/relations/:model/:targetField',
|
path: '/relations/:model/:targetField',
|
||||||
handler: 'relations.find',
|
handler: 'relations.findNew',
|
||||||
config: {
|
config: {
|
||||||
policies: [
|
policies: [
|
||||||
'admin::isAuthenticatedAdmin',
|
'admin::isAuthenticatedAdmin',
|
||||||
|
@ -4,36 +4,43 @@ const _ = require('lodash/fp');
|
|||||||
|
|
||||||
const helpers = require('./helpers');
|
const helpers = require('./helpers');
|
||||||
|
|
||||||
const createQueryBuilder = (uid, db) => {
|
const createQueryBuilder = (uid, db, initialState = {}) => {
|
||||||
const meta = db.metadata.get(uid);
|
const meta = db.metadata.get(uid);
|
||||||
const { tableName } = meta;
|
const { tableName } = meta;
|
||||||
|
|
||||||
const state = {
|
const state = _.defaults(
|
||||||
type: 'select',
|
{
|
||||||
select: [],
|
type: 'select',
|
||||||
count: null,
|
select: [],
|
||||||
max: null,
|
count: null,
|
||||||
first: false,
|
max: null,
|
||||||
data: null,
|
first: false,
|
||||||
where: [],
|
data: null,
|
||||||
joins: [],
|
where: [],
|
||||||
populate: null,
|
joins: [],
|
||||||
limit: null,
|
populate: null,
|
||||||
offset: null,
|
limit: null,
|
||||||
transaction: null,
|
offset: null,
|
||||||
forUpdate: false,
|
transaction: null,
|
||||||
orderBy: [],
|
forUpdate: false,
|
||||||
groupBy: [],
|
orderBy: [],
|
||||||
};
|
groupBy: [],
|
||||||
|
aliasCounter: 0,
|
||||||
|
},
|
||||||
|
initialState
|
||||||
|
);
|
||||||
|
|
||||||
let counter = 0;
|
const getAlias = () => `t${state.aliasCounter++}`;
|
||||||
const getAlias = () => `t${counter++}`;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
alias: getAlias(),
|
alias: getAlias(),
|
||||||
getAlias,
|
getAlias,
|
||||||
state,
|
state,
|
||||||
|
|
||||||
|
clone() {
|
||||||
|
return createQueryBuilder(uid, db, state);
|
||||||
|
},
|
||||||
|
|
||||||
select(args) {
|
select(args) {
|
||||||
state.type = 'select';
|
state.type = 'select';
|
||||||
state.select = _.uniq(_.castArray(args));
|
state.select = _.uniq(_.castArray(args));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user