Skip to content

Commit

Permalink
Add DuckDB Example (#1232)
Browse files Browse the repository at this point in the history
  • Loading branch information
YTKme authored Dec 10, 2024
1 parent ef1c98c commit c1c37c6
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/duckdb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @title Connect to DuckDB
* @difficulty intermediate
* @tags cli, deploy
* @run --allow-read --allow-write --allow-env --allow-net --allow-ffi <url>
* @resource {https://deno.land/x/duckdb} Deno DuckDB on deno.land/x
* @resource {https://duckdb.org/} DuckDB - An in-process SQL OLAP database management system
* @resource {https://github.com/suketa/ruby-duckdb?tab=readme-ov-file#pre-requisite-setup-linux} DuckDB Pre-requisite setup (Linux)
* @group Databases
*
* Using Deno with DuckDB, you can connect to memory or a persistent
* database with a filename.
*/

import { open } from "https://deno.land/x/duckdb/mod.ts";

// const db = open("./example.db");
const db = open(":memory:");

const connection = db.connect();

for (const row of connection.stream("select 42 as number")) {
console.debug(`Row Number: ${row.number}`); // -> { number: 42 }
}

const prepared = connection.prepare(
"SELECT ?::INTEGER AS number, ?::VARCHAR AS text;",
);

const result = prepared.query(1337, "foo"); // [{ number: 1337, text: 'foo' }]

console.debug(`Number: ${result[0].number}`);
console.debug(`Text: ${result[0].text}`);

connection.close();
db.close();

0 comments on commit c1c37c6

Please sign in to comment.