Skip to content

Commit dc9c68e

Browse files
committed
Updating binding name convention
1 parent 655bc75 commit dc9c68e

File tree

1 file changed

+50
-49
lines changed

1 file changed

+50
-49
lines changed

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

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ 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
}
@@ -190,7 +190,7 @@ But if you wish to add the binding manually, follow the steps below:
190190

191191
```toml
192192
[[d1_databases]]
193-
binding = "DB" # available in your Worker on env.DB
193+
binding = "prod_d1_tutorial" # available in your Worker on env.DB
194194
database_name = "prod-d1-tutorial"
195195
database_id = "<unique-ID-for-your-database>"
196196
```
@@ -199,7 +199,7 @@ But if you wish to add the binding manually, follow the steps below:
199199

200200
Specifically:
201201

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 `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`.
203203
- 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.
204204
- 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.
205205

@@ -221,7 +221,7 @@ You create bindings by adding them to the Worker you have created.
221221
3. Select **Settings**.
222222
4. Scroll to **Bindings**, then select **Add**.
223223
5. Select **D1 database**.
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 `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`.
225225
7. Select **Deploy** to deploy your binding. When deploying, there are two options:
226226
- **Deploy:** Immediately deploy the binding to 100% of your audience.
227227
- **Save version:** Save a version of the binding which you can deploy in the future.
@@ -275,19 +275,20 @@ After correctly preparing your [Wrangler configuration file](/workers/wrangler/c
275275
```
276276

277277
```txt output
278-
🌀 Mapping SQL input into an array of statements
279-
🌀 Executing on local database production-db-backend (<DATABASE_ID>) from .wrangler/state/v3/d1:
280-
┌────────────┬─────────────────────┬───────────────────┐
281-
│ CustomerId │ CompanyName │ ContactName │
282-
├────────────┼─────────────────────┼───────────────────┤
283-
│ 1 │ Alfreds Futterkiste │ Maria Anders │
284-
├────────────┼─────────────────────┼───────────────────┤
285-
│ 4 │ Around the Horn │ Thomas Hardy │
286-
├────────────┼─────────────────────┼───────────────────┤
287-
│ 11 │ Bs Beverages │ Victoria Ashworth │
288-
├────────────┼─────────────────────┼───────────────────┤
289-
│ 13 │ Bs Beverages │ Random Name │
290-
└────────────┴─────────────────────┴───────────────────┘
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+
└────────────┴─────────────────────┴───────────────────┘
291292
```
292293

293294
</Steps>
@@ -333,7 +334,7 @@ After you have set up your database, run an SQL query from within your Worker.
333334
export interface Env {
334335
// If you set another name in the Wrangler config file for the value for 'binding',
335336
// replace "DB" with the variable name you defined.
336-
DB: D1Database;
337+
prod_d1_tutorial: D1Database;
337338
}
338339

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

343344
if (pathname === "/api/beverages") {
344345
// If you did not use `DB` as your binding name, change it here
345-
const { results } = await env.DB.prepare(
346+
const { results } = await env.prod_d1_tutorial.prepare(
346347
"SELECT * FROM Customers WHERE CompanyName = ?",
347348
)
348349
.bind("Bs Beverages")
@@ -367,7 +368,7 @@ After you have set up your database, run an SQL query from within your Worker.
367368
pathname = urlparse(request.url).path
368369
if pathname == "/api/beverages":
369370
query = (
370-
await self.env.DB.prepare(
371+
await self.env.prod_d1_tutorial.prepare(
371372
"SELECT * FROM Customers WHERE CompanyName = ?",
372373
)
373374
.bind("Bs Beverages")
@@ -384,7 +385,7 @@ After you have set up your database, run an SQL query from within your Worker.
384385
In the code above, you:
385386

386387
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`.
387-
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).
388389
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.
389390
4. Execute the query by calling [`run()`](/d1/worker-api/prepared-statements/#run) to return all rows (or none, if the query returns none).
390391
5. Return your query results, if any, in JSON format with `Response.json(results)`.
@@ -410,7 +411,7 @@ You can query your D1 database using your Worker.
410411

411412
if (pathname === "/api/beverages") {
412413
// If you did not use `DB` as your binding name, change it here
413-
const { results } = await env.DB.prepare(
414+
const { results } = await env.prod_d1_tutorial.prepare(
414415
"SELECT * FROM Customers WHERE CompanyName = ?"
415416
)
416417
.bind("Bs Beverages")
@@ -465,23 +466,23 @@ To deploy your Worker to production using Wrangler, you must first repeat the [d
465466
npx wrangler d1 execute prod-d1-tutorial --remote --command="SELECT * FROM Customers"
466467
```
467468
```txt output
468-
⛅️ wrangler 4.13.2
469-
-------------------
470469
471-
🌀 Executing on remote database prod-d1-tutorial (<DATABASE_ID>):
472-
🌀 To execute on your local development database, remove the --remote flag from your wrangler command.
473-
🚣 Executed 1 command in 0.4069ms
474-
┌────────────┬─────────────────────┬───────────────────┐
475-
│ CustomerId │ CompanyName │ ContactName │
476-
├────────────┼─────────────────────┼───────────────────┤
477-
│ 1 │ Alfreds Futterkiste │ Maria Anders │
478-
├────────────┼─────────────────────┼───────────────────┤
479-
│ 4 │ Around the Horn │ Thomas Hardy │
480-
├────────────┼─────────────────────┼───────────────────┤
481-
│ 11 │ Bs Beverages │ Victoria Ashworth │
482-
├────────────┼─────────────────────┼───────────────────┤
483-
│ 13 │ Bs Beverages │ Random Name │
484-
└────────────┴─────────────────────┴───────────────────┘
470+
⛅️ wrangler 4.33.1
471+
───────────────────
472+
🌀 Executing on remote database jun-d1-db-gs-2025 (cf91ec5c-fa77-4d49-ad8e-e22921b996b2):
473+
🌀 To execute on your local development database, remove the --remote flag from your wrangler command.
474+
🚣 Executed 1 command in 0.1797ms
475+
┌────────────┬─────────────────────┬───────────────────┐
476+
│ CustomerId │ CompanyName │ ContactName │
477+
├────────────┼─────────────────────┼───────────────────┤
478+
│ 1 │ Alfreds Futterkiste │ Maria Anders │
479+
├────────────┼─────────────────────┼───────────────────┤
480+
│ 4 │ Around the Horn │ Thomas Hardy │
481+
├────────────┼─────────────────────┼───────────────────┤
482+
│ 11 │ Bs Beverages │ Victoria Ashworth │
483+
├────────────┼─────────────────────┼───────────────────┤
484+
│ 13 │ Bs Beverages │ Random Name │
485+
└────────────┴─────────────────────┴───────────────────┘
485486
```
486487

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

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

0 commit comments

Comments
 (0)