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
+50-49Lines changed: 50 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -132,7 +132,7 @@ 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
}
@@ -190,7 +190,7 @@ But if you wish to add the binding manually, follow the steps below:
190
190
191
191
```toml
192
192
[[d1_databases]]
193
-
binding = "DB"# available in your Worker on env.DB
193
+
binding = "prod_d1_tutorial"# available in your Worker on env.DB
194
194
database_name = "prod-d1-tutorial"
195
195
database_id = "<unique-ID-for-your-database>"
196
196
```
@@ -199,7 +199,7 @@ But if you wish to add the binding manually, follow the steps below:
199
199
200
200
Specifically:
201
201
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`.
203
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.
204
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.
205
205
@@ -221,7 +221,7 @@ You create bindings by adding them to the Worker you have created.
221
221
3. Select **Settings**.
222
222
4. Scroll to **Bindings**, then select **Add**.
223
223
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`.
225
225
7. Select **Deploy** to deploy your binding. When deploying, there are two options:
226
226
-**Deploy:** Immediately deploy the binding to 100% of your audience.
227
227
-**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
275
275
```
276
276
277
277
```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:
@@ -367,7 +368,7 @@ After you have set up your database, run an SQL query from within your Worker.
367
368
pathname = urlparse(request.url).path
368
369
if pathname =="/api/beverages":
369
370
query = (
370
-
awaitself.env.DB.prepare(
371
+
awaitself.env.prod_d1_tutorial.prepare(
371
372
"SELECT * FROM Customers WHERE CompanyName = ?",
372
373
)
373
374
.bind("Bs Beverages")
@@ -384,7 +385,7 @@ After you have set up your database, run an SQL query from within your Worker.
384
385
In the code above, you:
385
386
386
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`.
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).
388
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.
389
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).
390
391
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.
410
411
411
412
if (pathname ==="/api/beverages") {
412
413
// If you did not use `DB` as your binding name, change it here
0 commit comments