ResultSet
- class ResultSet()
Represents the row-returning result of a
Connection()query (seeselect()and its shorthands). Not constructed directly by applications – it’s what those methods resolve with.Despite the name, this isn’t a real streaming implementation yet: all rows are fetched up front and held in memory. The restricted
rowsAPI below (rather than a plain Array) is forward-looking, so that a future streaming implementation won’t change how consumers iterate over results.
Properties
rowCount
- ResultSet.rowCount
- Type:
number
The number of rows in this result set.
totalRowCount
- ResultSet.totalRowCount
- Type:
number
Present only when the query used limit/skip: the total number of rows that matched, before pagination was applied.
rows
- ResultSet.rows
- Type:
object
An Array-like object over the result rows, deliberately restricted – see below.
Note
rows only supports rows.forEach(fn), rows.map(fn) (both async, and both iterate every row despite the name “map”/“forEach” suggesting sync usage), and rows[0] (the first row). rows.length is undefined, and accessing any other numeric index (rows[1], etc.) throws 'random access into result sets is not supported'. Use rowCount, above, for the count, and getFirst()/getNext(), below, for cursor-style access.
Methods
getFirst
- getFirst()
Resets the cursor to the beginning of the result set and returns the first row (or false if there are none). This is an async method that returns a Promise.
- Return type:
Promise<object|false>
getNext
- getNext()
Advances the cursor and returns the next row, or false once the cursor has passed the last row. The first call to getNext() (without a prior getFirst()) returns the first row. This is an async method that returns a Promise.
- Return type:
Promise<object|false>
Example
const db = await require('dcp/jnu').connect('scheduler');
const result = await db.select({ table: 'jobs', limit: 10 });
let row;
while ((row = await result.getNext()) !== false) console.log(row);