Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Mimic node-postgres "Row Mode" #318

TGlock started this conversation in Ideas
Apr 14, 2022 · 3 comments · 5 replies
Discussion options

See - https://node-postgres.com/features/queries Row Mode

The concept is to be able create a result with similar shape as:

let res = await sql`select first_name, last_name, email from tbl_users`
res would have the shape
{
 cols: { first_name: { type : 1043 }, last_name: { type : 1043 }, email: { type : 1043 } },
 rows: ['John', 'Smith', 'js@domain.com']
}

This can significantly reduce http response size since repeated attribute names are omitted.

cols can simply utilize the statement.columns already provided.

If there is a means to use raw() please provide an example.

Alternatively perhaps consider, ".rowmode()" but it is not clear if that could be combined with existing .unsafe()

There may be existing codebases seeking to migrate from node-postgres and in short-term utilizing .unsafe()

You must be logged in to vote

Replies: 3 comments 5 replies

Comment options

Have you seen .raw() ?

We could allow providing an options object eg .raw({ parse: true }) to parse the Buffer values?

You must be logged in to vote
5 replies
Comment options

I will explore .raw() further and revert back.

I like your option idea {parse : true}

Thanks for quick reply!

Comment options

Great :) Looking forward to hear what you find. The docs could be improved for .raw() so suggestions are welcome.

This can significantly reduce http response size since repeated attribute names are omitted.

Keep in mind that simply using compression for your http responses will remove almost all overhead of sending a long array of full objects. I would highly recommend you test that out in practice before assuming arrays will be much smaller than arrays. For any case I have had the difference was so negligible that the overhead in handling array indexes in code would never be worth it.

Comment options

I'm thinking adding this feature as it's own method instead so you'd do eg sql`select generate_series(1, 5)`.values(). I've got it ready, but have not decided on the naming yet. I'm currently juggling .values(), .array() and .flat(), but leaning on .values() - any ideas?

Comment options

Thanks for this - I would also lean toward values(). Apologies for delay!

Comment options

Perfect :) It's added.. Will be included in next release

Comment options

Regrettably compression is not always in play. For results of ~500+ rows significant content-length reduction is seen. Bear in mind, database column names can be legacy, poorly chosen and long.

On client side, I have found processing to be slightly easier with array of arrays vs array of objects but codebase is almost identical.

I will add a single array of objects could have smaller memory impact than "array of arrays" but I've yet to take time to measure and ultimately it will vary depending on client (or server) implementation.

Again I will explore .raw() and see what I might learn.

You must be logged in to vote
0 replies
Comment options

Thank you for this - gave it a try and thus far is perfect.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /