- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将 pg-promise
库与 bluebird
一起用于进行相关查询。我有两个表,a 和 b,如下所示:
| a | | b |
|-------| |-------|
| a_id | | b_id |
| prop1 | | prop2 |
| b_a |
其中 b.b_a
是对 a.a_id
的引用。我想选择与给定 prop1
匹配的所有条目,结果应包含所有匹配的 a
-行加上每个 b
的相应行 一个
。这应该可以通过两个相关查询来实现。两个查询都可能返回多个结果。
如果表 a
只返回一行,我可以这样做:
function getResult(prop1) {
return db.task(function (t) {
return t.one("select * from a where prop1=$1", prop1)
.then(function (a) {
return t.batch([a, t.any("select * from b where b_a=$1", a.a_id)]);
})
.then(function (data) {
var a = data[0];
var bs = data[1];
bs.forEach(function (b) {
b.a = a;
});
return bs;
});
});
}
而且我还能够为多个 a
- 结果获取所有匹配的 b
条目,如下所示:
function getResult(prop1) {
return db.task(function (t) {
return t.many("select * from a where prop1=$1", prop1)
.then(function (as) {
var queries = [];
as.forEach(function (a) {
queries.push(t.any("select * from b where b_a=$1", a.id));
});
return t.batch(queries); // could concat queries with as here, but there wouldn't be a reference which b row belongs to which a row
})
.then(function (data) {
// data[n] contains all matching b rows
});
});
}
但是如何将这两者结合起来呢?
最佳答案
我是 pg-promise 的作者.
当您有 2 个表:Parent
-> Child
具有一对多关系,并且您希望获得匹配的 Parent
数组code> 行,每行扩展属性 children
设置为表 Child
...
有几种方法可以实现这一点,如 pg-promise 的组合并且 promises 通常是非常灵活的。这是最短的版本:
db.task(t => {
return t.map('SELECT * FROM Parent WHERE prop1 = $1', [prop1], parent => {
return t.any('SELECT * FROM Child WHERE parentId = $1', parent.id)
.then(children => {
parent.children = children;
return parent;
});
}).then(a => t.batch(a))
})
.then(data => {
/* data = the complete tree */
});
这就是我们在那里所做的:
首先,我们查询 Parent
项,然后将每一行映射到对相应 Child
项的查询,然后将其行设置到 Parent
并返回它。然后我们使用方法batch解析从方法 map 返回的 Child
查询数组.
这里和上面一样,但是使用了ES7的async
/await
语法:
await db.task(async t => {
const parents = await t.any('SELECT * FROM Parent WHERE prop1 = $1', [prop1]);
for(const p of parents) {
p.children = await t.any('SELECT * FROM Child WHERE parentId = $1', [p.id]);
}
return parents;
});
// the task resolves with the correct data tree
任务将使用这样的数组来解决:
[
{
"parent1-prop1", "parent1-prop2",
"children": [
{"child1-prop1", "child1-prop2"},
{"child2-prop1", "child2-prop2"}
]
},
{
"parent2-prop1", "parent2-prop2",
"children": [
{"child3-prop1", "child3-prop2"},
{"child4-prop1", "child4-prop2"}
]
}
]
更新
查看更好的答案:JOIN table as array of results with PostgreSQL/NodeJS .
关于javascript - 使用 pg-promise 获取父子树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37664258/
运行马里奥游戏时此文件出现错误当我尝试运行文件 super_mario.py 时,我从以下链接( https://github.com/Ja1r0/DQN-play-SuperMario )下载了代码
来自pg module doc ,似乎连接到 PG 数据库的正确方法是使用: conn = PG::Connection.open(dbname: 'test') 但是,我发现 other examp
嘿,所以我试图让 Nodejs Koa 使用 Koa-pg 模块与 postgres 对话,但我一直收到“找不到模块 pg”错误。我已尝试遵循 koa-pg 示例,但效果不佳……因此,我们将不胜感激任
我需要一些关于 pg npm 的帮助。 我已经阅读了许多文章和示例,并且对以正确的方式使用 pg 池感到非常困惑。许多文章都是我读过的旧文章。 我想向您展示我的一些代码以及我是如何围绕 db 构建所有
我有一个 postgresql 表,每天都有数千条时间序列数据。我有一个应用程序允许用户检索这些数据。查询可能需要 200 毫秒到 30 秒,具体取决于时间范围,因此这些查询必须是可取消的,以避免对生
我不确定我的 heroku postgres 发生了什么,但是当我尝试使用命令行访问它时 pg:info和 pg:psql ,我得到“未找到”。 我什至单击 heroku 上的按钮也为我的应用程序创建
给定一个包含 jsonb[] 类型列的表,如何将 json 数组插入到该列中? 使用提供的格式化程序 :array、:json 在这种情况下将不起作用 - 除非我缺少正确的组合或其他内容。 const
我是 node 和 pg-promise 的新手,一直无法弄清楚如何将三个相关查询的结果合并到一个 json 结果中。 给定三个相关表: 父实体 create table parent ( id bi
在开发时,我需要拉取最新的数据库,以便我知道我正在使用最新的数据。但是,我们保留了一张表,里面装满了我不需要费心下载的文件,因为它是一个非常大的表。 我知道 pg_dump 允许自定义参数,这些参数可
当我运行时(作为 root) gem install pg -v '0.12.0' -- --with-pg-config=/usr/pgsql-9.1/bin/pg_config 我得到以下输出:
我在尝试使用annotation@type(type=“pg uuid”)时遇到了一些问题。它抛出此异常: Caused by: java.lang.ClassNotFoundException: C
我按照这个线程 (Cannot install pg gem in Mavericks with Postgres.app) 获得了 'pg' gem 成功地与 Mavericks 上的 Postgr
正在获取 pg:找不到模型 id="," 的 dst 值 我定义了以下模型 // omitting fields which don't seem relevant to the issue // c
如何在Ruby pg gem 中使用PG::Connection 类的公共(public)实例方法send_query? 它可以帮助加快这样的程序的执行时间吗? a = [1,2,3,4,5,6,..
大家好,我一直在尝试使用 capistrano 部署我的应用程序。但是,我始终收到以下错误,并且不知道如何解决它。 * executing "cd /var/www/html/mysite.com/r
在我的 TestLogger 类中,require 'pg'(版本 0.16.0)返回 true,但 PGconn 未注册。 到目前为止,只有服务器有这个问题,而且只在TestLogger。其他确实需
使用 VSCode。为 Windows 安装了最新的 Node.js(首次用户)以及所需的模块,其中一个是“pg”(npm install pg) 在我的脚本顶部有这样的内容:const pg = r
我写了一个简单的脚本:。This.db对应于一个Pool对象。所以我在数据库中选择了一个特定的对象,在它的旁边我试图取回相同的对象,但这次使用了creation_date字段。。我尝试了许多结果变量更
我做了一个简单的脚本:。This.db对应于Pool对象。因此,我使用他的ID在我的数据库中选择了一个特定的对象。在它旁边,我试图取回相同的对象,但这次使用的是Creation_Date字段。。我尝试
我希望执行以下操作。 使用 group by 查询查询大型表以执行值汇总。 通过例程运行这些记录以添加一些附加数据 将它们高效地插入数据库。 我尝试使用 pg-query-stream 将数据作为流读
我是一名优秀的程序员,十分优秀!