How can we make pg-promise return an array of rows from a query, as opposed to array of row objects?
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=>{});
来源:https://stackoverflow.com/questions/36020663/how-to-make-pg-promise-return-rows-as-arrays