mirror of
https://github.com/strapi/strapi.git
synced 2025-08-28 10:45:51 +00:00
Support relations inside of group creation
This commit is contained in:
parent
9245c32c70
commit
f757d4d952
@ -55,23 +55,25 @@ module.exports = ({ model, modelKey, strapi }) => {
|
||||
if (repeatable === true) {
|
||||
validateRepeatableInput(groupValue, { key, ...attr });
|
||||
const groups = await Promise.all(
|
||||
groupValue.map(value => groupModel.create(value))
|
||||
groupValue.map(value => {
|
||||
return strapi.query(group).create(value);
|
||||
})
|
||||
);
|
||||
|
||||
const groupsArr = groups.map(group => ({
|
||||
const groupsArr = groups.map(groupEntry => ({
|
||||
kind: groupModel.globalId,
|
||||
ref: group,
|
||||
ref: groupEntry,
|
||||
}));
|
||||
|
||||
entry[key] = groupsArr;
|
||||
await entry.save();
|
||||
} else {
|
||||
validateNonRepeatableInput(groupValue, { key, ...attr });
|
||||
const group = await groupModel.create(groupValue);
|
||||
const groupEntry = await strapi.query(group).create(groupValue);
|
||||
entry[key] = [
|
||||
{
|
||||
kind: groupModel.globalId,
|
||||
ref: group,
|
||||
ref: groupEntry,
|
||||
},
|
||||
];
|
||||
await entry.save();
|
||||
@ -95,15 +97,14 @@ module.exports = ({ model, modelKey, strapi }) => {
|
||||
const updateOrCreateGroup = async value => {
|
||||
// check if value has an id then update else create
|
||||
if (hasPK(value)) {
|
||||
return groupModel.findOneAndUpdate(
|
||||
return strapi.query(group).update(
|
||||
{
|
||||
[model.primaryKey]: getPK(value),
|
||||
},
|
||||
value,
|
||||
{ new: true }
|
||||
value
|
||||
);
|
||||
}
|
||||
return groupModel.create(value);
|
||||
return strapi.query(group).create(value);
|
||||
};
|
||||
|
||||
if (repeatable === true) {
|
||||
@ -162,7 +163,9 @@ module.exports = ({ model, modelKey, strapi }) => {
|
||||
}, []);
|
||||
|
||||
if (idsToDelete.length > 0) {
|
||||
await groupModel.deleteMany({ [model.primaryKey]: { $in: idsToDelete } });
|
||||
await strapi
|
||||
.query(groupModel.uid)
|
||||
.delete({ [`${model.primaryKey}_in`]: idsToDelete });
|
||||
}
|
||||
}
|
||||
|
||||
@ -175,9 +178,10 @@ module.exports = ({ model, modelKey, strapi }) => {
|
||||
const groupModel = strapi.groups[group];
|
||||
|
||||
if (Array.isArray(entry[key]) && entry[key].length > 0) {
|
||||
await groupModel.deleteMany({
|
||||
[model.primaryKey]: { $in: entry[key].map(el => el.ref) },
|
||||
});
|
||||
const idsToDelete = entry[key].map(el => el.ref);
|
||||
await strapi
|
||||
.query(groupModel.uid)
|
||||
.delete({ [`${model.primaryKey}_in`]: idsToDelete });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -208,17 +208,9 @@ module.exports = {
|
||||
case 'manyMorphToOne':
|
||||
// Update the relational array.
|
||||
acc[current] = property.map(obj => {
|
||||
const globalId =
|
||||
obj.source && obj.source !== 'content-manager'
|
||||
? strapi.plugins[obj.source].models[_.toLower(obj.ref)]
|
||||
.globalId
|
||||
: strapi.models[_.toLower(obj.ref)].globalId;
|
||||
|
||||
// Define the object stored in database.
|
||||
// The shape is this object is defined by the strapi-hook-mongoose connector.
|
||||
return {
|
||||
ref: obj.refId,
|
||||
kind: globalId,
|
||||
kind: obj.ref,
|
||||
[association.filter]: obj.field,
|
||||
};
|
||||
});
|
||||
|
@ -188,7 +188,7 @@ module.exports = {
|
||||
.findOne({ id: roleID }, ['users', 'permissions']);
|
||||
|
||||
if (!role) {
|
||||
throw new Error('Cannot found this role');
|
||||
throw new Error('Cannot find this role');
|
||||
}
|
||||
|
||||
// Group by `type`.
|
||||
|
Loading…
x
Reference in New Issue
Block a user