Created
May 23, 2019 00:44
-
-
Save khaosdoctor/135dccd3beb0302938c94207c897b2c3 to your computer and use it in GitHub Desktop.
Oracle database async iterator to iterate over result sets using for await of
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
class Cursor { | |
constructor (resultSet) { | |
this.resultSet = resultSet | |
} | |
getIterator () { | |
return { | |
[Symbol.asyncIterator]: () => this._buildIterator() | |
} | |
} | |
_buildIterator () { | |
return { | |
next: () => this.resultSet.getRow().then((row) => ({ value: row, done: row === undefined })) | |
} | |
} | |
} | |
module.exports = Cursor | |
/* USAGE | |
const { resultSet } = await conn.execute('query', [], { outFormat: oracle.OBJECT, resultSet: true }) | |
const cursor = new Cursor(resultSet) | |
for await (const row of cursor.getIterator()) { | |
console.log(row) | |
} | |
cursor.resultSet.close() | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment