Hi @Sahiba_K I have a few recommendations.
TL;DR here’s a possible solution to your requirements. Refer to this guide for more information on model lifecycles.
// api/presentation/models/presentation.js
module.exports = {
lifecycles: {
async afterCreate(result, data) {
// since items or in your case 'presentations' is an array, you have to loop through it
data.owners.forEach(async (id)=>{
// find the user so that you can fetch the current number of items/presentations
const user = strapi.query("user", "users-permissions").findOne({id});
// get the number of presentations and increment it
// awaiting because you might want to.
await strapi.query("user", "users-permissions").update({id}, {number_of_items: user.number_of_items || 0 + 1});
});
},
// make one for afterUpdate
// make one for afterDelete
}
};
The relation you have outlined is not a many-to-many relation. It is a one-to-many relation. You should consider using a many-to-many relation which then would allow you to have the ability to aggregate your data. Relational databases are good at counting (to a certain extent) so if you’re not building a large data-set, your data model will benefit from taking advantage of that.
Your requirements will give you an unnecessary (for a simple relational data model) amount of maintenance work to do to keep the data consistent. If two ‘presentations’ are created at the same time, for instance, they could both pull the same value for ‘numberOfPresentations’. This will offset the accuracy of this value. You will then have to implement eventual consistency by way of a CRON job to correct that value over time.