Skip to content

Latest commit

 

History

History
59 lines (44 loc) · 2.27 KB

node-duckdb.connection.executeiterator.md

File metadata and controls

59 lines (44 loc) · 2.27 KB

Node-DuckDB API > Connection > executeIterator

Connection.executeIterator() method

Asynchronously executes the query and returns an iterator that points to the first result in the result set.

Signature:

executeIterator<T>(command: string, options?: IExecuteOptions): Promise<ResultIterator<T>>;

Parameters

Parameter Type Description
command string SQL command to execute
options IExecuteOptions optional options object of type IExecuteOptions

Returns:

Promise<ResultIterator<T>>

Example 1

Printing rows:

import { Connection, DuckDB, RowResultFormat } from "node-duckdb";
async function queryDatabaseWithIterator() {
  const db = new DuckDB();
  const connection = new Connection(db);
  await connection.executeIterator("CREATE TABLE people(id INTEGER, name VARCHAR);");
  await connection.executeIterator("INSERT INTO people VALUES (1, 'Mark'), (2, 'Hannes'), (3, 'Bob');");
  const result = await connection.executeIterator("SELECT * FROM people;");
  // print the first row
  console.log(result.fetchRow());
  // print the rest of the rows
  console.log(result.fetchAllRows());
  const result2 = await connection.executeIterator("SELECT * FROM people;", { rowResultFormat: RowResultFormat.Array });
  console.log(result2.fetchAllRows());
  connection.close();
  db.close();
}
queryDatabaseWithIterator();

Example 2

Providing generics type:

const result = await connection.executeIterator<number[]>(`SELECT CAST(1 AS TINYINT)`, {
  rowResultFormat: RowResultFormat.Array,
});
expect(result.fetchRow()).toMatchObject([1]);