Multiple conditional counts on one relation

System Information
  • Strapi Version: 4.4.1
  • Operating System: Ubuntu
  • Database: PGSQL
  • Node Version: 16.17.0
  • Yarn Version: 1.22.19

Hi.

I have an Article content type which has a one to many relationship with a Reaction content type, i.e. article.reactions. The reactions can have multiple different types, i.e. funny, helpful, informative etc.

I’m wondering if it’s at all possible using either the entity service or the query api to have multiple, aliased counts on the article object. So for instance a response that looks something like

{
    reactions: {
        funny: 13,
        helfpul: 3,
        informative: 43,
    }
}

Would I need to write a raw query to get these counts?

Not a raw query, but probably a custom controller or custom middleware which transforms the reactions to your desired target format.

1 Like