@xtuc/d1
v1.1.0
Published
Temporary lib that implements the binding.query() D1 API. In the future the Workers runtime will support this natively.
Readme
D1js library
Temporary lib that implements the binding.query() D1 API. In the future the Workers runtime will support this natively.
This lib allows you to send queries to the D1 objects and get the results back. It supports:
- Simple one-statement queries
- Multiple SQL queries in the the binding.query() / fetch, for better performance (avoids HTTP roundtrips)
- Prepared statements
- Returns the results and some profiling information
Examples
Simple query
await query(env.D1, "SELECT * FROM my_table WHERE employees > 1000")Same query using prepared statements
await query(env.D1, "SELECT * FROM my_table WHERE employees > ?1", [ 1000 ])Two SQL statements in the same query
await query(env.D1, [
"DROP TABLE IF EXISTS my_table",
"CREATE TABLE my_table (cid INTEGER PRIMARY KEY, name TEXT NOT NULL, employees INTEGER)"
]);Multiple prepared statements
Warning: please don't use multiple prepared statements. We have a bug (it's been fixed in staging, but not deployed yet) in the returning results object.
await query(env.D1, [
"SELECT * FROM my_table WHERE employees > ?1",
"SELECT * FROM my_table WHERE employees > ?1"
], [
[1000],
[2000]
])Sample worker
Simplest code snippet:
import { query } from "@xtuc/d1";
export default {
async fetch(request: Request, env: any): Promise<Response> {
const data = await query(env.D1, "PRAGMA table_list");
return new Response(JSON.stringify(data));
},
};Here's a complete "Hello World" example.
