How to make pg-promise return rows as arrays?

↘锁芯ラ 提交于 2019-11-29 08:43:16

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=>{});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!