0

I'm using node.js pg-promise module to access a postgres database. It's all working great, except that the results always come back as an array of rows, each of which is a json object with keys and values. It seems wasteful of bandwidth - more than half of the data goes to the keys on each field.

What I'm getting is an array of hashes:

[{
  "ID":110744,
  "Name":"Mann,Julie",
  "Firstname":"Julie",
  "Surname":"Mann",
  "ShortName":null,
  "Date":0,
  "Email":"julie_simmo_68@xyz.com",
  "Mobile":"0410038xxx",
  "Phone":"42615xxx"
}
,{
}
,{}....]

What I want is an array of arrays:

[
 [110744,"Mann,Julie","Julie","Mann",null,0,"julie_simm@xyz.com","0410038xxx","4261 5xxx"]
,
[...]
,
[...]
]

Is there any way to extract the data as an array of arrays? An array of rows, with each row being an ordered list of field values, in the same order as they appear in the SELECT statement. It will help with the speed of queries, and with unpacking the resulting data if they are just bare data in strict column order. I've been searching all day and can't find anything.

Arthur
  • 110
  • 3
  • 13
Zanderman
  • 1
  • 2

1 Answers1

0

How to make pg-promise return rows as arrays?

I think this might be the solution, I'm just installing it now to see if it works.

Community
  • 1
  • 1
Zanderman
  • 1
  • 2
  • I found this: [link]https://github.com/vitaly-t/pg-promise/issues/129 Which mentioned an option "rowMode" that implements exactly the behaviour I was after. I think it was added in May 2016, pg-promise 4.05. `var query=db.query({text:'select...', rowMode: 'array'})` , and the data comes back as bare unlabelled arrays, in the same order as the object versions. Simples! – Zanderman Sep 14 '16 at 07:23
  • Where you used to use **db.any(SQL)** you replace it with: **db.any({text:SQL,rowMode:'array'})** – Zanderman Sep 14 '16 at 07:34
  • 1
    not quite, as you won't be able to use Named Parameters or any query-formatting feature of `pg-promise`, as Parameterized Queries are formatted by the server. – vitaly-t Sep 14 '16 at 08:09