mirror of
https://github.com/directus/directus.git
synced 2026-01-30 02:47:59 -05:00
Add batch update
This commit is contained in:
@@ -131,20 +131,40 @@ router.patch(
|
||||
throw new RouteNotFoundException(req.path);
|
||||
}
|
||||
|
||||
const primaryKey = await ItemsService.updateItem(req.collection, req.params.pk, req.body, {
|
||||
role: req.role,
|
||||
admin: req.admin,
|
||||
ip: req.ip,
|
||||
userAgent: req.get('user-agent'),
|
||||
user: req.user,
|
||||
});
|
||||
const primaryKey = req.params.pk;
|
||||
|
||||
const item = await ItemsService.readItem(req.collection, primaryKey, req.sanitizedQuery, {
|
||||
role: req.role,
|
||||
admin: req.admin,
|
||||
});
|
||||
const isBatch = primaryKey.includes(',');
|
||||
|
||||
return res.json({ data: item || null });
|
||||
if (isBatch) {
|
||||
const primaryKeys = primaryKey.split(',');
|
||||
const items = await Promise.all(primaryKeys.map(updateItem));
|
||||
return res.json({ data: items || null });
|
||||
} else {
|
||||
const item = await updateItem(primaryKey);
|
||||
return res.json({ data: item || null });
|
||||
}
|
||||
|
||||
async function updateItem(pk: string | number) {
|
||||
const primaryKey = await ItemsService.updateItem(req.collection, pk, req.body, {
|
||||
role: req.role,
|
||||
admin: req.admin,
|
||||
ip: req.ip,
|
||||
userAgent: req.get('user-agent'),
|
||||
user: req.user,
|
||||
});
|
||||
|
||||
const item = await ItemsService.readItem(
|
||||
req.collection,
|
||||
primaryKey,
|
||||
req.sanitizedQuery,
|
||||
{
|
||||
role: req.role,
|
||||
admin: req.admin,
|
||||
}
|
||||
);
|
||||
|
||||
return item;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
@@ -239,6 +239,8 @@ export const checkAccess = async (
|
||||
|
||||
if (!result) throw '';
|
||||
} catch {
|
||||
throw new ForbiddenException(`You're not allowed to ${operation} this item.`);
|
||||
throw new ForbiddenException(
|
||||
`You're not allowed to ${operation} item "${pk}" in collection "${collection}".`
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user