How to get all user list group by location in postgres sequelize?

Solution for How to get all user list group by location in postgres sequelize?
is Given Below:

username location
user1 L1
user2 L2
user3 L2
user4 L3
user5 L4

I need the final output

L1 – [‘user1’]

L2 – [‘user2’, ‘user3’]

L3 – [‘user4’]

L4 – [‘user5’]

you can use array_agg in postgres:

select location , array_agg(username)
from table
group by location

alternatively you can use string_agg(username,',') if you want comma separated string instead of list

db<>fiddle here