javascript - 使用带有 pg-promise 的游标
问题描述
我正在努力寻找一个将光标与 pg-promise 一起使用的示例。node-postgres 支持其 pg-cursor 扩展。有没有办法将该扩展与 pg-promise 一起使用?我正在尝试实现一个异步生成器(以支持 for-await-of)。pg-query-stream 似乎不适合这个用例(我需要“拉”,而不是“推”)。
例如,我使用 SQLite 进行单元测试,我的(删节的)生成器看起来像这样......
async function* () {
const stmt = await db.prepare(...);
try {
while (true) {
const record = await stmt.get();
if (isUndefined(record)) {
break;
}
yield value;
}
}
finally {
stmt.finalize();
}
}
使用 pg-cursor,分配 tostmt
会变成类似client.query(new Cursor(...))
,stmt.get
会变成stmt.read(1)
并且stmt.finalize
会变成stmt.close
.
谢谢
解决方案
按照原始示例,我们可以修改它们以与pg-promise一起使用:
const pgp = require('pg-promise')(/* initialization options */);
const db = pgp(/* connection details */);
const Cursor = require('pg-cursor');
const c = await db.connect(); // manually managed connection
const text = 'SELECT * FROM my_large_table WHERE something > $1';
const values = [10];
const cursor = c.client.query(new Cursor(text, values));
cursor.read(100, (err, rows) => {
cursor.close(() => {
c.done(); // releasing connection
});
// or you can just do: cursor.close(c.done);
});
由于pg-promise不明确支持pg-cursor,因此必须手动获取连接对象并直接使用它,如上例所示。
pg-query-stream 似乎不适合这个用例(我需要
pull
,而不是push
)。
实际上,在这些库的上下文中,流和游标都仅用于提取数据。所以你也可以使用流媒体。
推荐阅读
- matlab - Label the actual data points in y-axis [MATLAB]
- typescript - Typescript replaceAll 未在 es5 中转译
- javascript - 日历函数调用不起作用无效的挂钩调用
- react-native - 到达标记坐标时显示到达位置消息反应原生地图
- nlp - 变压器的输出是否有最大序列长度?
- javascript - 为什么我在使用 Passport 和会话时没有保持登录状态?Passport 的“isAuthenticated()”总是返回 false
- r - 如何将具有逗号分隔字符串的数据框列转换为具有相应 TRUE/FALSE 值的列名,以确定字符串是否出现在行中
- google-sheets - Google表格:检查每一行的另一列中是否存在任何值
- c++ - 如何在 C++20 范围内将投影与变换结合起来
- visual-studio-code - VS Code 使用 AWS SAM template.yml 显示错误