Last active
June 23, 2021 20:38
-
-
Save zachsa/0c91bfd4ab435ef6e8ac4f85d541bd8b to your computer and use it in GitHub Desktop.
Example of how to create a row iterator using pg-cursor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createIterator = async sql => { | |
const client = await pool.connect() | |
const cursor = client.query(new Cursor(sql)) | |
const batchSize = 100 | |
return (async function getRows(client, cursor, batchSize) { | |
let done = false | |
// Get next rows | |
const rows = await new Promise((resolve, reject) => | |
cursor.read( | |
batchSize, | |
(err, rows) => err ? reject(err) : resolve(rows) | |
) | |
) | |
// Check if iteration is finished | |
if (rows.length < 1) { | |
done = true | |
cursor.close(() => client.release()) | |
} | |
// Return the iterator | |
return { | |
done, | |
rows, | |
next: async () => await getRows(client, cursor, batchSize) | |
} | |
})(client, cursor, batchSize) | |
} | |
// Used like so: | |
let iterator = await createIterator ('select ... from ...') | |
while (!iterator.done) { | |
let rows = iterator.rows | |
// do stuff with rows | |
iterator = await rowIterator.next() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment