I'm trying to upgrade my app from Strapi v3 to v4 and it seems there is a breaking change for querying models of a plugin.
This line of code (that works in v3):
const publicRole = await strapi
.query("role", "users-permissions")
.findOne({ type: "public" });
Produces the following error: "Error: Model role not found"
This is the full code:
async function setPublicPermissions(strapi, newPermissions) {
// Find the ID of the public role
const publicRole = await strapi
.query("role", "users-permissions")
.findOne({ type: "public" }); <---- produces Error: Model role not found
// List all available permissions
const publicPermissions = await strapi
.query("permission", "users-permissions")
.find({ type: "application", role: publicRole.id });
// Update permission to match new config
const controllersToUpdate = Object.keys(newPermissions);
console.log({ controllersToUpdate });
const updatePromises = publicPermissions
.filter((permission) => {
// Only update permissions included in newConfig
if (!controllersToUpdate.includes(permission.controller)) {
return false;
}
if (!newPermissions[permission.controller].includes(permission.action)) {
return false;
}
return true;
})
.map((permission) => {
// Enable the selected permissions
return strapi
.query("permission", "users-permissions")
.update({ id: permission.id }, { enabled: true });
});
await Promise.all(updatePromises);
}
Is there a new way to query plugin models in version 4 to make this work?
I found the answer. Querying models in a plugin in Strapi v4 looks like this:
const publicRole = await strapi
.query("plugin::users-permissions.role")
.findOne({
where: {
type: "public",
},
});