Eseguire query con D1 SQL all’edge
Usate Cloudflare D1, un database SQLite serverless, per eseguire query relazionali dai vostri Worker all’edge.
Eseguire query con D1 SQL all’edge è una lezione Edge Computing with Cloudflare Workers & Deno gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Edge Computing with Cloudflare Workers & Deno, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Edge Computing with Cloudflare Workers & Deno include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
What is D1?
Cloudflare D1 is a serverless SQL database built on SQLite. It brings relational queries to the edge, complementing key-value (KV) and Durable Objects.
KV vs D1
When to use which:
- KV: simple key-value, eventual consistency
- D1: structured data, joins, transactions, SQL
Creating a Database
Create a D1 database from the CLI, then bind it to your Worker.
wrangler d1 create my-dbBinding D1
Add the binding to wrangler.toml so the database appears on the env object.
# wrangler.toml
[[d1_databases]]
binding = 'DB'
database_name = 'my-db'
database_id = 'xxxx'Creating a Schema
Define tables with standard SQL, executed via the CLI or a migration file.
CREATE TABLE users (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL UNIQUE
);Prepared Statements
Query D1 from your Worker using prepared statements with bound parameters to prevent SQL injection.
const row = await env.DB
.prepare('SELECT * FROM users WHERE id = ?')
.bind(userId)
.first();Reading Multiple Rows
Use .all() to fetch every matching row as an array of objects.
const { results } = await env.DB
.prepare('SELECT email FROM users LIMIT 10')
.all();Writing Data
Inserts and updates use the same prepared-statement API with .run().
await env.DB
.prepare('INSERT INTO users (email) VALUES (?)')
.bind('a@b.com')
.run();Batching Statements
Run several statements atomically in a single round trip with batch, reducing latency.
await env.DB.batch([
env.DB.prepare('INSERT INTO users (email) VALUES (?)').bind('x@y.com'),
env.DB.prepare('INSERT INTO users (email) VALUES (?)').bind('p@q.com')
]);Consistency & Replication
D1 offers strong consistency on its primary, with read replication for low-latency reads near users. Plan writes around the primary location.
When to Choose D1
Pick D1 when you need relational structure, transactions, and SQL at the edge; pick KV for fast simple lookups and Durable Objects for coordinated state.
Quick Check
Test your D1 knowledge.
Recap
You learned to create and bind a D1 database, define schemas, run safe parameterized queries, batch statements, and choose D1 over KV or Durable Objects when you need relational data at the edge.
Domande Frequenti
La lezione «Eseguire query con D1 SQL all’edge» è gratuita?
Sì — il testo completo di «Eseguire query con D1 SQL all’edge» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Edge Computing with Cloudflare Workers & Deno, passa a CoddyKit PRO. Il corso Edge Computing with Cloudflare Workers & Deno include 4 lezioni in totale.
Cosa imparerò in «Eseguire query con D1 SQL all’edge»?
Usate Cloudflare D1, un database SQLite serverless, per eseguire query relazionali dai vostri Worker all’edge. Eserciti Edge Computing with Cloudflare Workers & Deno con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Edge Computing with Cloudflare Workers & Deno?
Non è richiesta alcuna esperienza precedente. Edge Computing with Cloudflare Workers & Deno su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Eseguire query con D1 SQL all’edge»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Edge Computing with Cloudflare Workers & Deno?
Sì. Ogni lezione Edge Computing with Cloudflare Workers & Deno include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Cloudflare KV Store
- Durable Objects spiegati
- Integrazione di Deno con lo storage edge
- Eseguire query con D1 SQL all’edge