I have a postgres database and am using Knex with Objection.js. This can either be postgres related or objection.js/knex related problem.
I have two models, Account and Stat. Account has field "accountId" and Stat also has field "accountId". One account can have multiple stats.
Account:
╔════════════╦═════════════╗
║ accountId ║ title ║
╠════════════╬═════════════╣
║ a ║ Account 1 ║
║ b ║ Account 2 ║
╚════════════╩═════════════╝
Stat:
╔════════════╦════════════╦═════════════╗
║ accountId ║ subs ║ month ║
╠════════════╬════════════╬═════════════╣
║ a ║ 313 ║ 2019/01 ║
║ b ║ 30 ║ 2019/01 ║
║ a ║ 909 ║ 2019/02 ║
║ a ║ 100 ║ 2019/03 ║
║ b ║ 3 ║ 2019/02 ║
╚════════════╩════════════╩═════════════╝
I would like to fetch a single account with it's stats [so that the stats could be limited to certain months for example].
I have Objection.js query:
Account.query()
.select(['Accounts.*', 'stats.*'])
.where('Accounts.accountId', accountId)
.joinRelation('stats')
which prints sql like so:
select "Accounts".*, "stats".* from "Accounts" inner join "Stats" as "stats" on "stats"."accountlId" = "Accounts"."accountId" where "Accounts"."accountId" = 'a'
which results in:
[{
accountId: a,
title: 'Account 1',
subs: 313,
month: '2019/01',
},{
accountId: a,
title: 'Account 1',
subs: 909,
month: '2019/02',
},{
accountId: a,
title: 'Account 1',
subs: 100,
month: '2019/03',
}]
whereas I would like to have the results like so:
{
accountId: a,
title: 'Account 1',
stats: [{
subs: 313,
month: '2019/01'
},{
subs: 909,
month: '2019/02'
},{
subs: 100,
month: '2019/03'
}]
}
I tried using postgres array_agg
but I'm not sure if I understood the principal correctly. Is such thing possible from the postgres level or should Objection.js handle this somehow or is the only option to do the mapping manually in javascript after the query?