javascript - 如何使用 knex 查询对

标签 javascript node.js postgresql knex.js

我有一个简单的表 (PostgreSQL),其中包含两个整数列,例如 idxidy。我想使用 knex 来查询它以获得与规定的组合 idx/idy 列表相对应的所有行,例如,生成如下查询:

select * from "datatable" where (idx, idy) IN (('1', '10'), ('2', '20'))

Knex 确实提供 whereIn方法,但是它似乎不支持多列。我设法通过以下方式实现了结果:

const knex_conf = require('./knexfile');
const knex = require('knex')(knex_conf.development);
const pgFormat = require('pg-format');

const pairs = [ [1, 10], [2, 20] ];

var P = knex.table('datatable').whereRaw(`(idx,idy) IN ${pgFormat('(%L)',pairs)}`).toSQL();
console.log(P.sql);

但我想知道是否会有更优雅的解决方案(无需使用 pg-format 或类似的“外部”工具)。

最佳答案

其实knex已经支持这个了:

https://runkit.com/embed/f2wym1fwfrn1

const Knex = require('knex');
const knex = Knex({
  client: 'pg'
});
const pairs = [ [1, 10], [2, 20] ];
knex('datatable').whereIn(['idx','idy'], pairs).toSQL();

关于javascript - 如何使用 knex 查询对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48689667/

相关文章:

javascript - 如何正确实现 Jquery Mobile Router 插件

javascript - highland.js 异步数组到值流

php - 如何在准备好的语句中使用sql函数

json - PostgreSQL:json 中的词汇概述,窗口函数......递归?

javascript - Angular Observables 返回匿名函数

javascript - 尝试通过动态键获取对象值时出现 Flowtype 错误

javascript - 在循环内等待 API 的结果

javascript - HTML Canvas : Two clients writing at the same time

sql - 联合主键中包含的索引列

javascript - Firebase 图像数组的异步加载