如何在 pg-promise 中将单独的 pg-promise 查询合并为一个结果

How to combine separate pg-promise queries into one result in pg-promise

我是节点和 pg-promise 的新手,一直无法弄清楚如何将三个相关查询的结果合并为一个 json 结果。

给出三个相关的tables:

一个parent实体

create table parent ( 
id bigint,
name character varying
);

一个child实体

create table entity_2 ( 
id bigint,
parent_id bigint,
name character varying
);

多对多table介于child和关联

之间
create table entity_2_entity_3 (
id bigint,
child_id bigint, 
associate_id bigint
);

table 到 child table

create associate (
id bigint,
name character varying
);

我的服务 url 是 /api/family/1(其中 1 是 child id)

第一个查询是(return一个结果):

SELECT * 
FROM child 
WHERE id =  

(使用 child id 参数)

第二个查询是(returns 0 到很多结果):

SELECT a.* 
FROM associate a 
JOIN child_associate ca ON ca.associate_id = a.id 
JOIN child c ON c.id = b.child_id 
WHERE c.id =  

(使用 id 参数)

第三个查询是 (returns the parent for the child

SELECT *
FROM parent
where id =  

(使用先前查询中 child 记录中的 parent_id)

生成的 JSON 应包含 'parent' 的一项、'child' 的一项以及 'associates'.

数组的一项

最好的方法是什么?我已经接近了,但没能做到正确。

在此先感谢您的帮助。顺便说一句-爱pg-promise!很高兴我决定在节点和 pg-promise.

中编写整个 back-en 应用程序

====== 更新 ======

基于另一个 post,我决定尝试一种不同的方法 - 使用单个查询并使用 Postgres return JSON。这是函数:

    var serverFamilyQuery = `SELECT json_build_object( 'id', s.id, 'name', s.name, 'server_environment', ( 
                SELECT json_agg(json_build_object('id', se.id, 'name', se.name)) 
                FROM salt.server_environment se 
                WHERE se.id = s.id ), 'applications', (
                  SELECT json_agg(json_build_object('id', ap.id, 'name', ap.name)) 
                  FROM salt.application ap
                  JOIN salt.server_application sa ON sa.application_id = ap.id
                  WHERE sa.server_id = s.id )
               ) result
              FROM salt.server s
              WHERE s.id = `

function getServerFamily(req, res, next) {
  var serverID = parseInt(req.params.id);
  db.one(serverFamilyQuery, [serverID])
    .then(data => {
      debug('data: %s', data);
      res.status(200)
        .json({
          status: 'success',
          data: data,
          message: 'Retrieved Application Successfully'
        });
    })
    .catch(function (err) {
      return next(err);
    });
};

结果如下:

{
  "status": "success",
  "data": {
    "result": {
      "id": 1,
      "name": "app01",
      "server_environment": [
        {
          "id": 1,
          "name": "Via West"
        }
      ],
      "applications": [
        {
          "id": 1,
          "name": "SnapApp"
        }
      ]
    }
  },
  "message": "Retrieved Application Successfully"
}

正如我之前提到的,我希望 server_environment、服务器和应用程序在 json 中是单独的条目,但至少这是可行的。

这么简单的事情:

db.task(async t => {
    const child = await t.one('SELECT * FROM child WHERE id = ', 123);
    const parent = await t.one('SELECT * FROM parent WHERE id = ', child.id);
    const associates = await t.any('SELECT * FROM associate...');
    return {child, parent, associates};
})
    .then(data => {
        // success, data = {child, parent, associates}
    })
    .catch(error => {
        // error
    });