-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |