Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for firstRow/allRows convenience methods #1534

Closed
aloisbarreras opened this issue Dec 8, 2017 · 1 comment
Closed

Request for firstRow/allRows convenience methods #1534

aloisbarreras opened this issue Dec 8, 2017 · 1 comment

Comments

@aloisbarreras
Copy link

When executing queries with this library like so:
db.query('select * from users').then(res => res.rows[0]);

I very often just want to get the first row from the result, and it's tedious to retype the .then(res => res.rows[0]) every time.

It would be great to add some convenience methods like firstRow() or allRows() that would save a lot of typing. For example:

db.query('select * from users').firstRow()
db.query('select * from users').allRows()
db.query('select * from users')

Omitting the methods would maintain the current functionality.

Let me know if you think this is a worthwhile addition.

@charmander
Copy link
Collaborator

query returns a promise, so you could include those on the configurable promise implementation for pools:

class QueryPromise extends Promise {
    firstRow() {
        return this.then(result => result.rows[0]);
    }

    allRows() {
        return this.then(result => result.rows);
    }
}

const db = new pg.Pool({,
    Promise: QueryPromise,
});

#1518 adds the same functionality to clients.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants