Skip to content

Commit b4a2bd9

Browse files
Oxyjunnevikashah
authored andcommitted
[D1] Updating CLI instruction for Getting started (#24844)
* Updating D1 get started CLI instructions * Updating last reviewed date * Updating binding name convention * Updating CLI return block
1 parent 41981e1 commit b4a2bd9

File tree

1 file changed

+65
-55
lines changed

1 file changed

+65
-55
lines changed

src/content/docs/d1/get-started.mdx

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Getting started
33
pcx_content_type: get-started
4-
reviewed: 2024-08-28
4+
reviewed: 2025-09-02
55
sidebar:
66
order: 2
77
---
@@ -132,13 +132,14 @@ To create your first D1 database:
132132
{
133133
"d1_databases": [
134134
{
135-
"binding": "DB",
135+
"binding": "prod_d1_tutorial",
136136
"database_name": "prod-d1-tutorial",
137137
"database_id": "<unique-ID-for-your-database>"
138138
}
139139
]
140140
}
141141
```
142+
3. When prompted: `Would you like Wrangler to add it on your behalf?`, select `Yes`. This will automatically add the binding to your Wrangler configuration file.
142143

143144
</Steps>
144145

@@ -176,18 +177,20 @@ To bind your D1 database to your Worker:
176177

177178
<Tabs syncKey='CLIvDash'> <TabItem label='CLI'>
178179

179-
You create bindings by updating your Wrangler file.
180+
You can automatically add the binding to your Wrangler configuration file when you run the `wrangler d1 create` command (step 3 of [2. Create a database](/d1/get-started/#2-create-a-database)).
181+
182+
But if you wish to add the binding manually, follow the steps below:
180183

181184
<Steps>
182185

183-
1. Copy the lines obtained from [step 2](/d1/get-started/#2-create-a-database) from your terminal.
186+
1. Copy the lines obtained from step 2 of [2. Create a database](/d1/get-started/#2-create-a-database) from your terminal.
184187
2. Add them to the end of your Wrangler file.
185188

186189
<WranglerConfig>
187190

188191
```toml
189192
[[d1_databases]]
190-
binding = "DB" # available in your Worker on env.DB
193+
binding = "prod_d1_tutorial" # available in your Worker on env.DB
191194
database_name = "prod-d1-tutorial"
192195
database_id = "<unique-ID-for-your-database>"
193196
```
@@ -196,7 +199,7 @@ You create bindings by updating your Wrangler file.
196199

197200
Specifically:
198201

199-
- The value (string) you set for `binding` is the **binding name**, and is used to reference this database in your Worker. In this tutorial, name your binding `DB`.
202+
- The value (string) you set for `binding` is the **binding name**, and is used to reference this database in your Worker. In this tutorial, name your binding `prod_d1_tutorial`.
200203
- The binding name must be [a valid JavaScript variable name](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#variables). For example, `binding = "MY_DB"` or `binding = "productionDB"` would both be valid names for the binding.
201204
- Your binding is available in your Worker at `env.<BINDING_NAME>` and the D1 [Workers Binding API](/d1/worker-api/) is exposed on this binding.
202205

@@ -218,7 +221,7 @@ You create bindings by adding them to the Worker you have created.
218221
3. Select **Settings**.
219222
4. Scroll to **Bindings**, then select **Add**.
220223
5. Select **D1 database**.
221-
6. Name your binding in **Variable name**, then select the `prod-d1-tutorial` D1 database you created in [step 2](/d1/get-started/#2-create-a-database) from the dropdown menu. For this tutorial, name your binding `DB`.
224+
6. Name your binding in **Variable name**, then select the `prod-d1-tutorial` D1 database you created in [step 2](/d1/get-started/#2-create-a-database) from the dropdown menu. For this tutorial, name your binding `prod_d1_tutorial`.
222225
7. Select **Deploy** to deploy your binding. When deploying, there are two options:
223226
- **Deploy:** Immediately deploy the binding to 100% of your audience.
224227
- **Save version:** Save a version of the binding which you can deploy in the future.
@@ -272,19 +275,20 @@ After correctly preparing your [Wrangler configuration file](/workers/wrangler/c
272275
```
273276

274277
```txt output
275-
🌀 Mapping SQL input into an array of statements
276-
🌀 Executing on local database production-db-backend (<DATABASE_ID>) from .wrangler/state/v3/d1:
277-
┌────────────┬─────────────────────┬───────────────────┐
278-
│ CustomerId │ CompanyName │ ContactName │
279-
├────────────┼─────────────────────┼───────────────────┤
280-
│ 1 │ Alfreds Futterkiste │ Maria Anders │
281-
├────────────┼─────────────────────┼───────────────────┤
282-
│ 4 │ Around the Horn │ Thomas Hardy │
283-
├────────────┼─────────────────────┼───────────────────┤
284-
│ 11 │ Bs Beverages │ Victoria Ashworth │
285-
├────────────┼─────────────────────┼───────────────────┤
286-
│ 13 │ Bs Beverages │ Random Name │
287-
└────────────┴─────────────────────┴───────────────────┘
278+
🌀 Executing on local database jun-d1-db-gs-2025 (cf91ec5c-fa77-4d49-ad8e-e22921b996b2) from .wrangler/state/v3/d1:
279+
🌀 To execute on your remote database, add a --remote flag to your wrangler command.
280+
🚣 1 command executed successfully.
281+
┌────────────┬─────────────────────┬───────────────────┐
282+
│ CustomerId │ CompanyName │ ContactName │
283+
├────────────┼─────────────────────┼───────────────────┤
284+
│ 1 │ Alfreds Futterkiste │ Maria Anders │
285+
├────────────┼─────────────────────┼───────────────────┤
286+
│ 4 │ Around the Horn │ Thomas Hardy │
287+
├────────────┼─────────────────────┼───────────────────┤
288+
│ 11 │ Bs Beverages │ Victoria Ashworth │
289+
├────────────┼─────────────────────┼───────────────────┤
290+
│ 13 │ Bs Beverages │ Random Name │
291+
└────────────┴─────────────────────┴───────────────────┘
288292
```
289293

290294
</Steps>
@@ -330,7 +334,7 @@ After you have set up your database, run an SQL query from within your Worker.
330334
export interface Env {
331335
// If you set another name in the Wrangler config file for the value for 'binding',
332336
// replace "DB" with the variable name you defined.
333-
DB: D1Database;
337+
prod_d1_tutorial: D1Database;
334338
}
335339

336340
export default {
@@ -339,7 +343,7 @@ After you have set up your database, run an SQL query from within your Worker.
339343

340344
if (pathname === "/api/beverages") {
341345
// If you did not use `DB` as your binding name, change it here
342-
const { results } = await env.DB.prepare(
346+
const { results } = await env.prod_d1_tutorial.prepare(
343347
"SELECT * FROM Customers WHERE CompanyName = ?",
344348
)
345349
.bind("Bs Beverages")
@@ -364,7 +368,7 @@ After you have set up your database, run an SQL query from within your Worker.
364368
pathname = urlparse(request.url).path
365369
if pathname == "/api/beverages":
366370
query = (
367-
await self.env.DB.prepare(
371+
await self.env.prod_d1_tutorial.prepare(
368372
"SELECT * FROM Customers WHERE CompanyName = ?",
369373
)
370374
.bind("Bs Beverages")
@@ -381,7 +385,7 @@ After you have set up your database, run an SQL query from within your Worker.
381385
In the code above, you:
382386

383387
1. Define a binding to your D1 database in your code. This binding matches the `binding` value you set in the [Wrangler configuration file](/workers/wrangler/configuration/) under `d1_databases`.
384-
2. Query your database using `env.DB.prepare` to issue a [prepared query](/d1/worker-api/d1-database/#prepare) with a placeholder (the `?` in the query).
388+
2. Query your database using `env.prod_d1_tutorial.prepare` to issue a [prepared query](/d1/worker-api/d1-database/#prepare) with a placeholder (the `?` in the query).
385389
3. Call `bind()` to safely and securely bind a value to that placeholder. In a real application, you would allow a user to pass the `CompanyName` they want to list results for. Using `bind()` prevents users from executing arbitrary SQL (known as "SQL injection") against your application and deleting or otherwise modifying your database.
386390
4. Execute the query by calling [`run()`](/d1/worker-api/prepared-statements/#run) to return all rows (or none, if the query returns none).
387391
5. Return your query results, if any, in JSON format with `Response.json(results)`.
@@ -407,7 +411,7 @@ You can query your D1 database using your Worker.
407411

408412
if (pathname === "/api/beverages") {
409413
// If you did not use `DB` as your binding name, change it here
410-
const { results } = await env.DB.prepare(
414+
const { results } = await env.prod_d1_tutorial.prepare(
411415
"SELECT * FROM Customers WHERE CompanyName = ?"
412416
)
413417
.bind("Bs Beverages")
@@ -445,10 +449,16 @@ To deploy your Worker to production using Wrangler, you must first repeat the [d
445449
npx wrangler d1 execute prod-d1-tutorial --remote --file=./schema.sql
446450
```
447451
```txt output
448-
✔ ⚠️ This process may take some time, during which your D1 database will be unavailable to serve queries.
449-
Ok to proceed? y
452+
🌀 Executing on remote database prod-d1-tutorial (<DATABASE_ID>):
453+
🌀 To execute on your local development database, remove the --remote flag from your wrangler command.
454+
Note: if the execution fails to complete, your DB will return to its original state and you can safely retry.
455+
├ 🌀 Uploading <DATABASE_ID>.a7f10c4651cc3a26.sql
456+
│ 🌀 Uploading complete.
457+
458+
🌀 Starting import...
459+
🌀 Processed 3 queries.
450460
🚣 Executed 3 queries in 0.00 seconds (5 rows read, 6 rows written)
451-
Database is currently at bookmark 00000002-00000004-00004ef1-ad4a06967970ee3b20860c86188a4b31.
461+
Database is currently at bookmark 00000000-0000000a-00004f6d-b85c16a3dbcf077cb8f258b4d4eb965e.
452462
┌────────────────────────┬───────────┬──────────────┬────────────────────┐
453463
│ Total queries executed │ Rows read │ Rows written │ Database size (MB) │
454464
├────────────────────────┼───────────┼──────────────┼────────────────────┤
@@ -462,23 +472,23 @@ To deploy your Worker to production using Wrangler, you must first repeat the [d
462472
npx wrangler d1 execute prod-d1-tutorial --remote --command="SELECT * FROM Customers"
463473
```
464474
```txt output
465-
⛅️ wrangler 4.13.2
466-
-------------------
467475
468-
🌀 Executing on remote database prod-d1-tutorial (<DATABASE_ID>):
469-
🌀 To execute on your local development database, remove the --remote flag from your wrangler command.
470-
🚣 Executed 1 command in 0.4069ms
471-
┌────────────┬─────────────────────┬───────────────────┐
472-
│ CustomerId │ CompanyName │ ContactName │
473-
├────────────┼─────────────────────┼───────────────────┤
474-
│ 1 │ Alfreds Futterkiste │ Maria Anders │
475-
├────────────┼─────────────────────┼───────────────────┤
476-
│ 4 │ Around the Horn │ Thomas Hardy │
477-
├────────────┼─────────────────────┼───────────────────┤
478-
│ 11 │ Bs Beverages │ Victoria Ashworth │
479-
├────────────┼─────────────────────┼───────────────────┤
480-
│ 13 │ Bs Beverages │ Random Name │
481-
└────────────┴─────────────────────┴───────────────────┘
476+
⛅️ wrangler 4.33.1
477+
───────────────────
478+
🌀 Executing on remote database jun-d1-db-gs-2025 (cf91ec5c-fa77-4d49-ad8e-e22921b996b2):
479+
🌀 To execute on your local development database, remove the --remote flag from your wrangler command.
480+
🚣 Executed 1 command in 0.1797ms
481+
┌────────────┬─────────────────────┬───────────────────┐
482+
│ CustomerId │ CompanyName │ ContactName │
483+
├────────────┼─────────────────────┼───────────────────┤
484+
│ 1 │ Alfreds Futterkiste │ Maria Anders │
485+
├────────────┼─────────────────────┼───────────────────┤
486+
│ 4 │ Around the Horn │ Thomas Hardy │
487+
├────────────┼─────────────────────┼───────────────────┤
488+
│ 11 │ Bs Beverages │ Victoria Ashworth │
489+
├────────────┼─────────────────────┼───────────────────┤
490+
│ 13 │ Bs Beverages │ Random Name │
491+
└────────────┴─────────────────────┴───────────────────┘
482492
```
483493

484494
3. Deploy your Worker to make your project accessible on the Internet. Run:
@@ -487,17 +497,17 @@ To deploy your Worker to production using Wrangler, you must first repeat the [d
487497
npx wrangler deploy
488498
```
489499
```txt output
490-
⛅️ wrangler 4.13.2
491-
-------------------
492-
493-
Total Upload: 0.19 KiB / gzip: 0.16 KiB
494-
Your worker has access to the following bindings:
495-
- D1 Databases:
496-
- DB: prod-d1-tutorial (<DATABASE_ID>)
497-
Uploaded d1-tutorial (3.76 sec)
498-
Deployed d1-tutorial triggers (2.77 sec)
499-
https://d1-tutorial.<YOUR_SUBDOMAIN>.workers.dev
500-
Current Version ID: <VERSION_ID>
500+
⛅️ wrangler 4.33.1
501+
────────────────────
502+
Total Upload: 0.52 KiB / gzip: 0.33 KiB
503+
Your Worker has access to the following bindings:
504+
Binding Resource
505+
env.prod_d1_tutorial (prod-d1-tutorial) D1 Database
506+
507+
Uploaded prod-d1-tutorial (4.17 sec)
508+
Deployed prod-d1-tutorial triggers (3.49 sec)
509+
https://prod-d1-tutorial.pcx-team.workers.dev
510+
Current Version ID: 42c82f1c-ff2b-4dce-9ea2-265adcccd0d5
501511
```
502512

503513
You can now visit the URL for your newly created project to query your live database.

0 commit comments

Comments
 (0)