From c1c37c6c70beef547eaeb82e0de2f643e9dfc451 Mon Sep 17 00:00:00 2001 From: Yan Kuang Date: Mon, 9 Dec 2024 23:50:05 -0800 Subject: [PATCH] Add DuckDB Example (#1232) --- examples/duckdb.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 examples/duckdb.ts diff --git a/examples/duckdb.ts b/examples/duckdb.ts new file mode 100644 index 000000000..9599a8177 --- /dev/null +++ b/examples/duckdb.ts @@ -0,0 +1,36 @@ +/** + * @title Connect to DuckDB + * @difficulty intermediate + * @tags cli, deploy + * @run --allow-read --allow-write --allow-env --allow-net --allow-ffi + * @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();