mirror of
https://github.com/strapi/strapi.git
synced 2025-09-22 14:59:07 +00:00
Support creation
This commit is contained in:
parent
a34f117a4a
commit
f82b05dc94
@ -364,7 +364,9 @@ const createOnFetchPopulateFn = ({
|
|||||||
// TODO: handle Dynamic zone
|
// TODO: handle Dynamic zone
|
||||||
componentAttributes.forEach(name => {
|
componentAttributes.forEach(name => {
|
||||||
const attr = definition.attributes[name];
|
const attr = definition.attributes[name];
|
||||||
|
const { type } = attr;
|
||||||
|
|
||||||
|
if (type === 'component') {
|
||||||
const component = strapi.components[attr.component];
|
const component = strapi.components[attr.component];
|
||||||
|
|
||||||
const assocs = (component.associations || []).filter(
|
const assocs = (component.associations || []).filter(
|
||||||
@ -416,6 +418,7 @@ const createOnFetchPopulateFn = ({
|
|||||||
_docs: {},
|
_docs: {},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
next();
|
next();
|
||||||
|
@ -16,9 +16,10 @@ module.exports = ({ model, modelKey, strapi }) => {
|
|||||||
_.has(obj, model.primaryKey) ? obj[model.primaryKey] : obj.id;
|
_.has(obj, model.primaryKey) ? obj[model.primaryKey] : obj.id;
|
||||||
|
|
||||||
const assocKeys = model.associations.map(ast => ast.alias);
|
const assocKeys = model.associations.map(ast => ast.alias);
|
||||||
const componentKeys = Object.keys(model.attributes).filter(key => {
|
const componentKeys = Object.keys(model.attributes).filter(key =>
|
||||||
return model.attributes[key].type === 'component';
|
['component', 'dynamiczone'].includes(model.attributes[key].type)
|
||||||
});
|
);
|
||||||
|
|
||||||
const excludedKeys = assocKeys.concat(componentKeys);
|
const excludedKeys = assocKeys.concat(componentKeys);
|
||||||
|
|
||||||
const defaultPopulate = model.associations
|
const defaultPopulate = model.associations
|
||||||
@ -38,6 +39,9 @@ module.exports = ({ model, modelKey, strapi }) => {
|
|||||||
|
|
||||||
for (let key of componentKeys) {
|
for (let key of componentKeys) {
|
||||||
const attr = model.attributes[key];
|
const attr = model.attributes[key];
|
||||||
|
const { type } = attr;
|
||||||
|
|
||||||
|
if (type === 'component') {
|
||||||
const { component, required = false, repeatable = false } = attr;
|
const { component, required = false, repeatable = false } = attr;
|
||||||
|
|
||||||
const componentModel = strapi.components[component];
|
const componentModel = strapi.components[component];
|
||||||
@ -83,6 +87,50 @@ module.exports = ({ model, modelKey, strapi }) => {
|
|||||||
await entry.save();
|
await entry.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type === 'dynamiczone') {
|
||||||
|
const { required = false } = attr;
|
||||||
|
|
||||||
|
if (required === true && !_.has(values, key)) {
|
||||||
|
const err = new Error(`Dynamiczone ${key} is required`);
|
||||||
|
err.status = 400;
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_.has(values, key)) continue;
|
||||||
|
|
||||||
|
const dynamiczoneValues = values[key];
|
||||||
|
|
||||||
|
validateDynamiczoneInput(dynamiczoneValues, { key, ...attr });
|
||||||
|
|
||||||
|
const dynamiczones = await Promise.all(
|
||||||
|
dynamiczoneValues.map(value => {
|
||||||
|
const component = value.__component;
|
||||||
|
return strapi
|
||||||
|
.query(component)
|
||||||
|
.create(value)
|
||||||
|
.then(entity => {
|
||||||
|
return {
|
||||||
|
__component: value.__component,
|
||||||
|
entity,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const componentsArr = dynamiczones.map(({ __component, entity }) => {
|
||||||
|
const componentModel = strapi.components[__component];
|
||||||
|
|
||||||
|
return {
|
||||||
|
kind: componentModel.globalId,
|
||||||
|
ref: entity,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
entry[key] = componentsArr;
|
||||||
|
await entry.save();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateComponents(entry, values) {
|
async function updateComponents(entry, values) {
|
||||||
@ -454,3 +502,56 @@ function validateNonRepeatableInput(value, { key, required }) {
|
|||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function validateDynamiczoneInput(
|
||||||
|
value,
|
||||||
|
{ key, min, max, components, required }
|
||||||
|
) {
|
||||||
|
if (!Array.isArray(value)) {
|
||||||
|
const err = new Error(`Dynamiczone ${key} is invalid. Expected an array`);
|
||||||
|
err.status = 400;
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
value.forEach(val => {
|
||||||
|
if (typeof val !== 'object' || Array.isArray(val) || val === null) {
|
||||||
|
const err = new Error(
|
||||||
|
`Dynamiczone ${key} has invalid items. Expected each items to be objects`
|
||||||
|
);
|
||||||
|
err.status = 400;
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_.has(val, '__component')) {
|
||||||
|
const err = new Error(
|
||||||
|
`Dynamiczone ${key} has invalid items. Expected each items to have a valid __component key`
|
||||||
|
);
|
||||||
|
err.status = 400;
|
||||||
|
throw err;
|
||||||
|
} else if (!components.includes(val.__component)) {
|
||||||
|
const err = new Error(
|
||||||
|
`Dynamiczone ${key} has invalid items. Each item must have a __component key that is present in the attribute definition`
|
||||||
|
);
|
||||||
|
err.status = 400;
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (
|
||||||
|
(required === true || (required !== true && value.length > 0)) &&
|
||||||
|
(min && value.length < min)
|
||||||
|
) {
|
||||||
|
const err = new Error(
|
||||||
|
`Dynamiczone ${key} must contain at least ${min} items`
|
||||||
|
);
|
||||||
|
err.status = 400;
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
if (max && value.length > max) {
|
||||||
|
const err = new Error(
|
||||||
|
`Dynamiczone ${key} must contain at most ${max} items`
|
||||||
|
);
|
||||||
|
err.status = 400;
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user