-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Using async/await with pg-cursor? #1839
Comments
You should be able to promisify pg-cursor manually: const { promisify } = require('util')
const Cursor = require('pg-cursor')
Cursor.prototype.readAsync = promisify(Cursor.prototype.read)
⋮
let rows = await cursor.readAsync(100)
while (rows.length) {
// do something with rows
rows = await cursor.readAsync(100)
} But that seems like it would make a good built-in pg-cursor feature. |
Hi @charmander. I agree that this would be nice to include in the I've used the Mongo Node.js client before and if I remember correctly they support either callback or promise style dynamically. I was pointed to code HERE that achieves this (the Do you have any pointers/preferences for a good way to incorporate promise support into the the |
@zachsa See https://github.com/brianc/node-pg-cursor/blob/1cdad4d8d20000dc6fb9a783394249b727106b84/index.js#L170-L185 and Lines 265 to 280 in 2c7be86
|
Thank you. That actually looks more straightforward than I was expecting. |
Hi @charmander - I had a quick look at this over the weekend and will hopefully have a bit of time this month to look more into this (I would really like to contribute to this if I am able). Previously I have used this library without paying too much attention to how it works. Looking at In a node app, my
Does this mean that at app start I'm loading 2 separate instantiations of the If so, why? I am using it like this:
So to me it seems that the cursor wouldn't need to include a dependency on the pg library itself? |
@zachsa pg-cursor has |
Ah thanks. I should have picked that up |
Hi @charmander, would it be welcome if I were to update the package.json dependency versions? For example, using the
Updating
The test output is this:
I'm not sure what the expected behaviour should be? I can see that the test suite isn't expecting the constructor name for each object in the row. |
What is actually happening here?
I see from the documentation that a cursor is an instance of |
Also - is this the same issue (as in, promisifying the cursor) as discussed here? brianc/node-pg-cursor#36 |
Another question: The I assume this function is what is called in this scenario?
|
It would be amazing if |
@charmander Great solution! Although, I tried your solution above but it doesn't seem to work with calling the client again inside the loop when the size of rows is smaller than the actual size. So for example I have 10 users in the users_table and I tried getting two at a time to double check if it reads the next bit, it doesn't seem to go along with another client call inside. Do you know the explanation for it? Here's a sample code of what I am trying to achieve:
|
@tienzochi You can’t make new queries on a client while one is already in progress (the one with the cursor). SELECT DISTINCT ON (user_id) user_id, username, sales.[…]
FROM user_table LEFT JOIN sales USING (user_id)
-- ORDER BY probably; if not, maybe EXISTS or equivalent join would be more appropriate |
For this you should probably use |
I'm using this guy to sugarize cursors const pg = require('pg');
const Cursor = require('pg-cursor');
const client = new pg.Client(/* ... */);
client.cursor = async function* cursor(query, values, chunkSize = 100) {
const cursor = this.query(new Cursor(query, values));
try {
while (true) {
const rows = await new Promise((resolve, reject) => {
cursor.read(chunkSize, (error, rows) => error ? reject(error) : resolve(rows));
});
if (rows.length === 0) break;
for (const row of rows) {
yield row;
}
}
} finally {
cursor.close();
}
};
// usage
try {
const rows = client.cursor(`SELECT ...`, [], 20);
for await (const row of rows) {
use(row);
}
} catch (error) {
handle(error); // should perfectly catch all errors here
} |
@brianc It is not clear to me that if using an |
@brianc could this be included, please? It would enable the example given by @charmander , out of the box, and close this issue. Cursor.prototype.readAsync = promisify(Cursor.prototype.read) // should be built-in
let rows = await cursor.readAsync(100)
while (rows.length) {
// do something with rows
rows = await cursor.readAsync(100)
} |
pg: 8.7.3 |
Looks so. I'll be able to test in 2 weeks. Thanks |
This is correct, works fine. This issue can be closed. |
Hi - thanks for a great library! I have a question regarding the
pg-cursor
API.In the example on using a cursor from start to finish, this is shown in the docs HERE:
It seems like I would need to wrap this in a promise if I wanted to be able to cycle through rows returned via the cursor?
Is it possible to use
async/await
here? I.e. something along the lines of the following:Apologies if I've missed something in the documentation!
The text was updated successfully, but these errors were encountered: