So, what I need is a following json.
var plan = [{
id: 11,
title: 'give a title',
actions: [
{id: 1,
planId: 11,
title: 'give action name'},
{
id: 3,
planId: 11,
title: 'give another action name'
}
]},
{
id: 13,
title: 'thirteen a title',
actions: [
{id: 1,
planId: 13,
title: 'thirteen action name'},
{
id: 3,
planId: 13,
title: 'thirteen another action name'
}
]}
]
SO I have 2 tables, plan and actions. The relation between two table is Plan has many actions. Plan(id, titile) Action(id, titile, planId)
SELECT
*,
ARRAY (
SELECT
jsonb_build_object ('id',
m.id,
'title',
m.title)
FROM
actions a
INNER JOIN plan p ON p.id = a.planid
) AS actions
FROM
plan
I'm not sure how can I get the related actions under each plan.