List of posts with reaction GROUP BY reaction type

Viewed 35

I need Sql query for this case here is my code here is my code

here is my controller query

my query is:

Post.query().preload('reactions')

here is my model relation

@hasMany(() => Post,{
    localKey :'id',
    foreignKey : 'reaction_id',
   
    onQuery: (query) => {
      query.select('id','reaction','chat_id',
         Database.raw('Count(*) as total_likes')
       )
       .groupBy('reaction')
      
    }
  })

  public reactions : HasMany<typeof Post>

posts table columns

id,
post

reactions table columns

id , 
user_id , 
reaction, 
post_id 

For my case I need the data in the below format

Any one please give me raw query for this case ?


post_lists=[
           {
            id:1,
            post:'Hello everyone',
            reactions:[
                {
                    reaction:'like',
                    total_count:2
                },
                {
                    reaction:'love',
                    total_count:2
                }
            ]
            
          },
          {
            id:1,
            post:'Hello everyone',
            reactions:[
                {
                    reaction:'like',
                    total_count:2
                },
                {
                    reaction:'angry',
                    total_count:2
                }
            ]
            
          },
          .........
      ]

In my case my query return wrong result

0 Answers
Related