最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Using a cursor with pg-promise - Stack Overflow

matteradmin10PV0评论

I'm struggling to find an example of using a cursor with pg-promise. node-postgres supports its pg-cursor extension. Is there a way to use that extension with pg-promise? I'm attempting to implement an asynchronous generator (to support for-await-of). pg-query-stream doesn't seem to be appropriate for this use case (I need "pull", rather than "push").

As an example, I use SQLite for my unit tests and my (abridged) generator looks something like this...

async function* () {

    const stmt = await db.prepare(...);

    try {

        while (true) {

            const record = await stmt.get();

            if (isUndefined(record)) {

                break;
            }

            yield value;
        }
    }
    finally {

        stmt.finalize();
    }
}

Using pg-cursor, the assignment to stmt would bee something like client.query(new Cursor(...)), stmt.get would bee stmt.read(1) and stmt.finalize would bee stmt.close.

Thanks

I'm struggling to find an example of using a cursor with pg-promise. node-postgres supports its pg-cursor extension. Is there a way to use that extension with pg-promise? I'm attempting to implement an asynchronous generator (to support for-await-of). pg-query-stream doesn't seem to be appropriate for this use case (I need "pull", rather than "push").

As an example, I use SQLite for my unit tests and my (abridged) generator looks something like this...

async function* () {

    const stmt = await db.prepare(...);

    try {

        while (true) {

            const record = await stmt.get();

            if (isUndefined(record)) {

                break;
            }

            yield value;
        }
    }
    finally {

        stmt.finalize();
    }
}

Using pg-cursor, the assignment to stmt would bee something like client.query(new Cursor(...)), stmt.get would bee stmt.read(1) and stmt.finalize would bee stmt.close.

Thanks

Share Improve this question edited Jun 5, 2018 at 11:20 vitaly-t 26k17 gold badges127 silver badges150 bronze badges asked Jun 5, 2018 at 7:52 sdc395sdc395 2851 gold badge3 silver badges12 bronze badges 1
  • Cursor examples for the base driver are here, so you just have to use manual connect to access Client and use it in the same way as in the examples. – vitaly-t Commented Jun 5, 2018 at 11:09
Add a ment  | 

1 Answer 1

Reset to default 5

Following the original examples, we can modify them for use with pg-promise:

const pgp = require('pg-promise')(/* initialization options */);
const db = pgp(/* connection details */);

const Cursor = require('pg-cursor');

const c = await db.connect(); // manually managed connection

const text = 'SELECT * FROM my_large_table WHERE something > $1';
const values = [10];

const cursor = c.client.query(new Cursor(text, values));

cursor.read(100, (err, rows) => {
  cursor.close(() => {
    c.done(); // releasing connection
  });
  // or you can just do: cursor.close(c.done);
});

Since pg-promise doesn't support pg-cursor explicitly, one has to manually acquire the connection object and use it directly, as shown in the example above.

pg-query-stream doesn't seem to be appropriate for this use case (I need pull, rather than push).

Actually, in the context of these libraries, both streams and cursors are only for pulling data. So it would be ok for you to use streaming also.

Post a comment

comment list (0)

  1. No comments so far