deno.com

Connect to DuckDB

Edit on Github

Using Deno with DuckDB, you can connect to memory or a persistent database with a filename.

import { DuckDBInstance } from "npm:@duckdb/node-api";
For graceful cleanup of resources
using stack = new DisposableStack();
Create an in-memory database
const instance = await DuckDBInstance.create(":memory:");
Close the instance when `stack` gets out of scope
stack.defer(() => instance.closeSync());
Connect to the database
const connection = await instance.connect();
Close the connection when `stack` gets out of scope
stack.defer(() => connection.closeSync());
Simple select query
const reader = await connection.runAndReadAll("select 10, 'foo'");
const rows = reader.getRows();

console.debug(rows); // [ [ 10, "foo" ] ]
Prepared statement
const prepared = await connection.prepare("select $1, $2");
prepared.bindInteger(1, 20);
prepared.bindVarchar(2, "bar");
const reader2 = await prepared.runAndReadAll();
const rows2 = reader2.getRows();

console.debug(rows2); // [ [ 20, "bar" ] ]

Run this example locally using the Deno CLI:

deno run -R -W -E -N --allow-ffi https://docs.deno.com/examples/scripts/duckdb.ts

Did you find what you needed?

Privacy policy