question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return objects rather than arrays

See original GitHub issue

The node.js project node-postgres or pg returns objects rather than rows of arrays. It was elegant to work with. Is there a reason this project only returns an array of arrays? Am I missing something?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
haydcommented, May 22, 2020

IMO we should copy what node-postgres does.

1reaction
ebebbingtoncommented, Jul 8, 2020

What about something like:

// deno-postgres inernal code
let returnedResult = []
const queryResult = query(...)
const rows = queryResult.rows
const columns = queryResult.rowDescription.columns
const columnNames: string[] = columns.map((column) => {
  return column.name;
});
rows.forEach((row, rowIndex) => {
  let rowData: any = {};
  row.forEach((rVal, rIndex) => {
    const columnName: string = columnNames[rIndex];
    rowData[columnName] = row[rIndex];
  });
  returnedResult.push(rowData);
});
returnedResult = userDefinedQueryData.limit === 1 ? returnedResult[0] :  returnedResult
return {
  query: ...
  ...
  rows: returnedResult
}

// user code
const result = client.query(...)
const rows = result.rows // [{ name: "Edward"}, {name: "Hayd"} }]

(Could be improved but you get the gist)

I’ve actually implemented something similar in a Deno project that uses deno-postgres but that was obviously manual work, and each db query calls that method to get the data in key value pairs

Read more comments on GitHub >

github_iconTop Results From Across the Web

php - Why return object instead of array? - Stack Overflow
An array is just an index of values. Whereas an object contains methods which can generate the result for you.
Read more >
Objects vs. Arrays - Medium
Both objects and arrays are considered “special” in JavaScript. Objects represent a special data type that is mutable and can be used to...
Read more >
Data Structures: Objects and Arrays - Eloquent JavaScript
Many types of information require more than one atom, though. ... You give it an object, and it returns an array of strings—the...
Read more >
BrandonSavage.net Stop returning arrays (use objects instead)
The reason is simple: when one object makes a request to another object, it should generally get back an object unless the response...
Read more >
Practical JavaScript: Arrays vs. Objects - Towards Data Science
Think about what your particular data represents: If it's a single entity with named properties, you want an object. If it's a group...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found