How can we make pg-promise return an array of rows from a query, as opposed to array of row objects?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Version 4.0.5 of pg-promise started to support advanced parameters for Prepared Statements
and Parameterized Queries
, exactly as they are in node-postgres. One such parameter - rowMode, can be set to array
to make the driver return rows as arrays.
// Prepared Statement:
db.query({
name: 'my-prep-statement',
text: 'select ...', // a query or a QueryFile object (see PreparedStatement)
values: [],
rowMode: 'array'
}).then(data=>{}).catch(reason=>{});
// Parameterized Query:
db.query({
text: 'select ...', // a query or a QueryFile object (see ParameterizedQuery)
values: [],
rowMode: 'array'
}).then(data=>{}).catch(reason=>{});
See also types that wrap them: PreparedStatement and ParameterizedQuery.
The code below is equivalent to the one above, but offers better performance + re-usability + flexibility of setting values separately.
var ps = new pgp.PreparedStatement({
name: 'my-prep-statement',
text: 'select ...', // a query or a QueryFile object (see PreparedStatement)
values: [], // alternatively, can be set when calling a query method
rowMode: 'array'
});
db.query(ps).then(data=>{}).catch(reason=>{});
var pq = new pgp.ParameterizedQuery({
text: 'select ...', // a query or a QueryFile object (see ParameterizedQuery)
values: [], // alternatively, can be set when calling a query method
rowMode: 'array'
});
db.query(pq).then(data=>{}).catch(reason=>{});