来自knex中的多个联接(连续表)的嵌套Json

时间:2020-07-08 12:10:59

标签: javascript node.js postgresql knex.js

我是knex的新手,试图弄清楚如何在knex中实现嵌套联接。我在此答案中找到了想做的事情-Create nested json from sql query postgres 9.4

尽管我了解上述答案中发生的事情,但由于对knex的经验有限,所以我无法在knex中实现类似的功能。

我的表格结构是这样的- equality_plan_action 具有 equality_Plan_action_indicator (第二个表中的键),并且 equality_Plan_action_indicator 具有 equality_Plan_action_indicator_activity (第二个键)

喜欢- A-> B(A.id = B.a_id), B-> C(B.id = C.b_id)

我的代码结构是这样的-

const actionFields = [
  `${actionTable}.id`,
  `${actionTable}.created_at`,
  `${actionTable}.updated_at`,
  `${actionTable}.equality_plan_id`,
  `${actionTable}.scope_of_action`,
  `${actionTable}.name`,
  `${actionTable}.start_date`,
  `${actionTable}.end_date`,
  `${actionTable}.details`,
  `${actionTable}.cancel_reason`,
  `objectives`,
];

const actionAggregateFields = [
  db.raw(`
    COALESCE(
      JSON_AGG(
        JSON_BUILD_OBJECT(
          'id', ${actionIndicatorTable}.id,
          'name', ${actionIndicatorTable}.name,
          'start_date', ${actionIndicatorTable}.start_date,
          'end_date', ${actionIndicatorTable}.end_date,
          'initial_value', ${actionIndicatorTable}.initial_value,
          'current_value', ${actionIndicatorTable}.current_value,
          'target_value', ${actionIndicatorTable}.target_value,
          'periodicity', ${actionIndicatorTable}.periodicity,
          'information_tool', ${actionIndicatorTable}.information_tool,
          'activity', JSON_AGG(
            JSON_BUILD_OBJECT(
              'id', ${actionIndicatorActivityTable}.id,
              'created_at', ${actionIndicatorActivityTable}.created_at,
              'updated_at', ${actionIndicatorActivityTable}.updated_at,
              'previous_value', ${actionIndicatorActivityTable}.previous_value,
              'value', ${actionIndicatorActivityTable}.value,
              'comment', ${actionIndicatorActivityTable}.comment
              'member_id', ${actionIndicatorActivityTable}.membert_id,
            )
          )
          FILTER (WHERE ${actionIndicatorActivityTable}.id IS NOT NULL), '[]'
        )
      )
      FILTER (WHERE ${actionIndicatorTable}.id IS NOT NULL), '[]'
    ) as indicators
  `)
];

const rawActionObjectivesJoinQuery = `
  JOIN LATERAL (
    SELECT ARRAY(SELECT ${objectiveActionTable}.equality_plan_objective_id
    FROM ${objectiveActionTable}
    WHERE ${actionTable}.id = ${objectiveActionTable}.equality_plan_action_id)
    as objectives
  ) objective_arr ON true`

await db.select([...actionFields, ...actionAggregateFields]).from(actionTable)
    .leftJoin(
      actionIndicatorTable,
      `${actionTable}.id`,
      `${actionIndicatorTable}.equality_plan_action_id`,
    )
    .leftJoin(
      actionIndicatorActivityTable,
      `${actionIndicatorTable}.id`,
      `${actionIndicatorActivityTable}.equality_plan_action_indicator_id`,
    )
    .joinRaw(rawActionObjectivesJoinQuery)
    .where(actionFilter)
    .groupBy(`${actionTable}.id`, 'objective_arr.objectives')
    .orderBy(`${actionTable}.created_at`);
};

执行此操作会给我错误-

error: select "equality_plan_action"."id", "equality_plan_action"."created_at", "equality_plan_action"."updated_at", "equality_plan_action"."equality_plan_id", "equality_plan_action"."scope_of_action", "equality_plan_action"."name", "equality_plan_action"."start_date", "equality_plan_action"."end_date", "equality_plan_action"."details", "equality_plan_action"."cancel_reason", "objectives", 
[32]     COALESCE(
[32]       JSON_AGG(
[32]         JSON_BUILD_OBJECT(
[32]           'id', equality_plan_action_indicator.id,
[32]           'name', equality_plan_action_indicator.name,
[32]           'start_date', equality_plan_action_indicator.start_date,
[32]           'end_date', equality_plan_action_indicator.end_date,
[32]           'initial_value', equality_plan_action_indicator.initial_value,
[32]           'current_value', equality_plan_action_indicator.current_value,
[32]           'target_value', equality_plan_action_indicator.target_value,
[32]           'periodicity', equality_plan_action_indicator.periodicity,
[32]           'information_tool', equality_plan_action_indicator.information_tool,
[32]           'activity', (
[32]             CASE WHEN equality_plan_action_indicator.id IS NOT NULL
[32]             THEN JSON_AGG(
[32]               JSON_BUILD_OBJECT(
[32]                 'id', equality_plan_action_indicator_activity.id,
[32]                 'created_at', equality_plan_action_indicator_activity.created_at,
[32]                 'updated_at', equality_plan_action_indicator_activity.updated_at,
[32]                 'previous_value', equality_plan_action_indicator_activity.previous_value,
[32]                 'value', equality_plan_action_indicator_activity.value,
[32]                 'comment', equality_plan_action_indicator_activity.comment
[32]                 'member_id', equality_plan_action_indicator_activity.membert_id,
[32]               )
[32]             )
[32]             END
[32]           )
[32]         )
[32]       )
[32]       FILTER (WHERE equality_plan_action_indicator.id IS NOT NULL), '[]'
[32]     ) as indicators
[32]    from "equality_plan_action" left join "equality_plan_action_indicator" on "equality_plan_action"."id" = "equality_plan_action_indicator"."equality_plan_action_id" left join "equality_plan_action_indicator_activity" on "equality_plan_action_indicator"."id" = "equality_plan_action_indicator_activity"."equality_plan_action_indicator_id" 
[32]   JOIN LATERAL (
[32]     SELECT ARRAY(SELECT equality_plan_objective_action.equality_plan_objective_id
[32]     FROM equality_plan_objective_action
[32]     WHERE equality_plan_action.id = equality_plan_objective_action.equality_plan_action_id)
[32]     as objectives
[32]   ) objective_arr ON true where "equality_plan_action"."equality_plan_id" = $1 group by "equality_plan_action"."id", "objective_arr"."objectives" order by "equality_plan_action"."created_at" asc - syntax error at or near ")"

我已经坚持了几个小时,并尝试了多种变化。我知道我做错了,但不知道怎么办。

我的预期结果是这样的-

{
    ...actionColumns,
    indicators: [
        {
            ...indicatorColumns,
            activity: [
                {...activityColumns},
                {...activityColumns}
            ]
        },
        {
            ...indicatorColumns,
            activity: [
                {...activityColumns},
                {...activityColumns}
            ]
        }
    ]
}

0 个答案:

没有答案