You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/d1/get-started.mdx
+65-55Lines changed: 65 additions & 55 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: Getting started
3
3
pcx_content_type: get-started
4
-
reviewed: 2024-08-28
4
+
reviewed: 2025-09-02
5
5
sidebar:
6
6
order: 2
7
7
---
@@ -132,13 +132,14 @@ To create your first D1 database:
132
132
{
133
133
"d1_databases": [
134
134
{
135
-
"binding": "DB",
135
+
"binding": "prod_d1_tutorial",
136
136
"database_name": "prod-d1-tutorial",
137
137
"database_id": "<unique-ID-for-your-database>"
138
138
}
139
139
]
140
140
}
141
141
```
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.
142
143
143
144
</Steps>
144
145
@@ -176,18 +177,20 @@ To bind your D1 database to your Worker:
176
177
177
178
<TabssyncKey='CLIvDash'> <TabItemlabel='CLI'>
178
179
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:
180
183
181
184
<Steps>
182
185
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.
184
187
2. Add them to the end of your Wrangler file.
185
188
186
189
<WranglerConfig>
187
190
188
191
```toml
189
192
[[d1_databases]]
190
-
binding = "DB"# available in your Worker on env.DB
193
+
binding = "prod_d1_tutorial"# available in your Worker on env.DB
191
194
database_name = "prod-d1-tutorial"
192
195
database_id = "<unique-ID-for-your-database>"
193
196
```
@@ -196,7 +199,7 @@ You create bindings by updating your Wrangler file.
196
199
197
200
Specifically:
198
201
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`.
200
203
- 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.
201
204
- 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.
202
205
@@ -218,7 +221,7 @@ You create bindings by adding them to the Worker you have created.
218
221
3. Select **Settings**.
219
222
4. Scroll to **Bindings**, then select **Add**.
220
223
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`.
222
225
7. Select **Deploy** to deploy your binding. When deploying, there are two options:
223
226
-**Deploy:** Immediately deploy the binding to 100% of your audience.
224
227
-**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
272
275
```
273
276
274
277
```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:
@@ -364,7 +368,7 @@ After you have set up your database, run an SQL query from within your Worker.
364
368
pathname = urlparse(request.url).path
365
369
if pathname =="/api/beverages":
366
370
query = (
367
-
awaitself.env.DB.prepare(
371
+
awaitself.env.prod_d1_tutorial.prepare(
368
372
"SELECT * FROM Customers WHERE CompanyName = ?",
369
373
)
370
374
.bind("Bs Beverages")
@@ -381,7 +385,7 @@ After you have set up your database, run an SQL query from within your Worker.
381
385
In the code above, you:
382
386
383
387
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).
385
389
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.
386
390
4. Execute the query by calling [`run()`](/d1/worker-api/prepared-statements/#run) to return all rows (or none, if the query returns none).
387
391
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.
407
411
408
412
if (pathname ==="/api/beverages") {
409
413
// If you did not use `DB` as your binding name, change it here
0 commit comments