Skip to content

Commit 0af7176

Browse files
Oxyjundaisyfaithauma
authored andcommitted
[D1] Adding "Deploy to Cloudflare" button into D1 Getting started. (#22140)
* Actioning first round of feedback. * Implementing feedback round 2 * Updating dashboard instructions.
1 parent a99c9fa commit 0af7176

File tree

1 file changed

+116
-42
lines changed

1 file changed

+116
-42
lines changed

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

Lines changed: 116 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,24 @@ This guide instructs you through:
1212

1313
- Creating your first database using D1, Cloudflare's native serverless SQL database.
1414
- Creating a schema and querying your database via the command-line.
15-
- Connecting a [Cloudflare Worker](/workers/) to your D1 database to query your D1 database programmatically.
15+
- Connecting a [Cloudflare Worker](/workers/) to your D1 database using bindings, and querying your D1 database programmatically.
1616

1717
You can perform these tasks through the CLI or through the Cloudflare dashboard.
1818

1919
:::note
2020
If you already have an existing Worker and an existing D1 database, follow this tutorial from [3. Bind your Worker to your D1 database](/d1/get-started/#3-bind-your-worker-to-your-d1-database).
2121
:::
2222

23+
## Quick start
24+
25+
If you want to skip the steps and get started quickly, click on the button below.
26+
27+
[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/docs-examples/tree/d1-get-started/d1/d1-get-started)
28+
29+
This creates a repository in your GitHub account and deploys the application to Cloudflare Workers. Use this option if you are familiar with Cloudflare Workers, and wish to skip the step-by-step guidance.
30+
31+
You may wish to manually follow the steps if you are new to Cloudflare Workers.
32+
2333
## Prerequisites
2434

2535
<Render file="prereqs" product="workers" />
@@ -80,17 +90,17 @@ For example: `CI=true npm create cloudflare@latest d1-tutorial --type=simple --g
8090

8191
<Steps>
8292
1. Log in to your [Cloudflare dashboard](https://dash.cloudflare.com/) and select your account.
83-
2. Go to your account > **Workers & Pages** > **Overview**.
93+
2. Go to your account > **Compute (Workers)** > **Workers & Pages**.
8494
3. Select **Create**.
85-
4. Select **Create Worker**.
95+
4. Under **Start from a template**, select **Hello world**.
8696
5. Name your Worker. For this tutorial, name your Worker `d1-tutorial`.
8797
6. Select **Deploy**.
8898
</Steps>
8999
</TabItem> </Tabs>
90100

91101
## 2. Create a database
92102

93-
A D1 database is conceptually similar to many other databases: a database may contain one or more tables, the ability to query those tables, and optional indexes. D1 uses the familiar [SQL query language](https://www.sqlite.org/lang.html) (as used by SQLite).
103+
A D1 database is conceptually similar to many other SQL databases: a database may contain one or more tables, the ability to query those tables, and optional indexes. D1 uses the familiar [SQL query language](https://www.sqlite.org/lang.html) (as used by SQLite).
94104

95105
To create your first D1 database:
96106

@@ -103,37 +113,46 @@ To create your first D1 database:
103113
cd d1-tutorial
104114
```
105115

106-
2. Run the following `wrangler d1` command and give your database a name. In this tutorial, the database is named `prod-d1-tutorial`:
116+
2. Run the following `wrangler@latest d1` command and give your database a name. In this tutorial, the database is named `prod-d1-tutorial`:
117+
118+
:::note
119+
The [Wrangler command-line interface](/workers/wrangler/) is Cloudflare's tool for managing and deploying Workers applications and D1 databases in your terminal. It was installed when you used `npm create cloudflare@latest` to initialize your new project.
120+
121+
While Wrangler gets installed locally to your project, you can use it outside the project by using the command `npx wrangler`.
122+
:::
107123

108124
```sh
109-
npx wrangler d1 create prod-d1-tutorial
125+
npx wrangler@latest d1 create prod-d1-tutorial
110126
```
111127

112128
```sh output
113129

114-
✅ Successfully created DB 'prod-d1-tutorial'
130+
✅ Successfully created DB 'prod-d1-tutorial' in region WEUR
131+
Created your new D1 database.
115132

116-
[[d1_databases]]
117-
binding = "DB" # available in your Worker on env.DB
118-
database_name = "prod-d1-tutorial"
119-
database_id = "<unique-ID-for-your-database>"
120-
```
133+
{
134+
"d1_databases": [
135+
{
136+
"binding": "DB",
137+
"database_name": "prod-d1-tutorial",
138+
"database_id": "<unique-ID-for-your-database>"
139+
}
140+
]
141+
}
121142

122-
</Steps>
143+
```
123144

124-
This creates a new D1 database and outputs the [binding](/workers/runtime-apis/bindings/) configuration needed in the next step.
125145

126-
:::note
127146

128-
The `wrangler` command-line interface is Cloudflare's tool for managing and deploying Workers applications and D1 databases in your terminal. It was installed when you used `npm create cloudflare@latest` to initialize your new project.
147+
</Steps>
129148

130-
:::
149+
This creates a new D1 database and outputs the [binding](/workers/runtime-apis/bindings/) configuration needed in the next step.
131150

132151
</TabItem> <TabItem label='Dashboard'>
133152

134153
<Steps>
135-
1. Go to **Storage & Databases** > **D1**.
136-
2. Select **Create**.
154+
1. Go to **Storage & Databases** > **D1 SQL Database**.
155+
2. Select **Create Database**.
137156
3. Name your database. For this tutorial, name your D1 database `prod-d1-tutorial`.
138157
4. (Optional) Provide a location hint. Location hint is an optional parameter you can provide to indicate your desired geographical location for your database. Refer to [Provide a location hint](/d1/configuration/data-location/#provide-a-location-hint) for more information.
139158
5. Select **Create**.
@@ -198,7 +217,7 @@ You can also bind your D1 database to a [Pages Function](/pages/functions/). For
198217
You create bindings by adding them to the Worker you have created.
199218

200219
<Steps>
201-
1. Go to **Workers & Pages** > **Overview**.
220+
1. Go to **Compute (Workers)** > **Workers & Pages**.
202221
2. Select the `d1-tutorial` Worker you created in [step 1](/d1/get-started/#1-create-a-worker).
203222
3. Select **Settings**.
204223
4. Scroll to **Bindings**, then select **Add**.
@@ -217,11 +236,11 @@ You create bindings by adding them to the Worker you have created.
217236

218237
## 4. Run a query against your D1 database
219238

220-
### Configure your D1 database
239+
### Populate your D1 database
221240

222241
<Tabs syncKey='CLIvDash'> <TabItem label='CLI'>
223242

224-
After correctly preparing your [Wrangler configuration file](/workers/wrangler/configuration/), set up your database. Use the example `schema.sql` file below to initialize your database.
243+
After correctly preparing your [Wrangler configuration file](/workers/wrangler/configuration/), set up your database. Create a `schema.sql` file using the SQL syntax below to initialize your database.
225244

226245
<Steps>
227246
1. Copy the following code and save it as a `schema.sql` file in the `d1-tutorial` Worker directory you created in step 1:
@@ -237,16 +256,28 @@ After correctly preparing your [Wrangler configuration file](/workers/wrangler/c
237256
```sh
238257
npx wrangler d1 execute prod-d1-tutorial --local --file=./schema.sql
239258
```
259+
```output
260+
⛅️ wrangler 4.13.2
261+
-------------------
262+
263+
🌀 Executing on local database prod-d1-tutorial (<DATABASE_ID>) from .wrangler/state/v3/d1:
264+
🌀 To execute on your remote database, add a --remote flag to your wrangler command.
265+
🚣 3 commands executed successfully.
266+
```
267+
268+
:::note
269+
The command `npx wrangler d1 execute` initializes your database locally, not on the remote database.
270+
:::
240271

241-
3. Validate your data is in your database by running:
272+
3. Validate that your data is in the database by running:
242273

243274
```sh
244275
npx wrangler d1 execute prod-d1-tutorial --local --command="SELECT * FROM Customers"
245276
```
246277

247278
```sh output
248279
🌀 Mapping SQL input into an array of statements
249-
🌀 Executing on local database production-db-backend (5f092302-3fbd-4247-a873-bf1afc5150b) from .wrangler/state/v3/d1:
280+
🌀 Executing on local database production-db-backend (<DATABASE_ID>) from .wrangler/state/v3/d1:
250281
┌────────────┬─────────────────────┬───────────────────┐
251282
│ CustomerId │ CompanyName │ ContactName │
252283
├────────────┼─────────────────────┼───────────────────┤
@@ -267,7 +298,7 @@ After correctly preparing your [Wrangler configuration file](/workers/wrangler/c
267298
Use the Dashboard to create a table and populate it with data.
268299

269300
<Steps>
270-
1. Go to **Storage & Databases** > **D1**.
301+
1. Go to **Storage & Databases** > **D1 SQL Database**.
271302
2. Select the `prod-d1-tutorial` database you created in [step 2](/d1/get-started/#2-create-a-database).
272303
3. Select **Console**.
273304
4. Paste the following SQL snippet.
@@ -329,9 +360,9 @@ After you have set up your database, run an SQL query from within your Worker.
329360

330361
In the code above, you:
331362

332-
1. Define a binding to your D1 database in your TypeScript code. This binding matches the `binding` value you set in the [Wrangler configuration file](/workers/wrangler/configuration/) under `[[d1_databases]]`.
363+
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`.
333364
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).
334-
3. Call `bind()` to safely and securely bind a value to that placeholder. In a real application, you would allow a user to define 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.
365+
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.
335366
4. Execute the query by calling `all()` to return all rows (or none, if the query returns none).
336367
5. Return your query results, if any, in JSON format with `Response.json(results)`.
337368

@@ -344,9 +375,9 @@ After configuring your Worker, you can test your project locally before you depl
344375
You can query your D1 database using your Worker.
345376

346377
<Steps>
347-
1. Go to **Workers & Pages** > **Overview**.
378+
1. Go to **Compute (Workers)** > **Workers & Pages**.
348379
2. Select the `d1-tutorial` Worker you created.
349-
3. Select **Edit Code**.
380+
3. Select the **Edit code** icon (**\<\/\>**).
350381
4. Clear the contents of the `worker.js` file, then paste the following code:
351382

352383
```js
@@ -379,36 +410,75 @@ You can query your D1 database using your Worker.
379410
</TabItem>
380411
</Tabs>
381412

382-
## 5. Deploy your database
413+
## 5. Deploy your application
383414

384-
Deploy your database on Cloudflare's global network.
415+
Deploy your application on Cloudflare's global network.
385416

386417
<Tabs syncKey='CLIvDash'> <TabItem label='CLI'>
387418

388-
To deploy your Worker to production using Wrangler, you must first repeat the [database configuration](/d1/get-started/#configure-your-d1-database) steps after replacing the `--local` flag with the `--remote` flag to give your Worker data to read. This creates the database tables and imports the data into the production version of your database.
419+
To deploy your Worker to production using Wrangler, you must first repeat the [database configuration](/d1/get-started/#populate-your-d1-database) steps after replacing the `--local` flag with the `--remote` flag to give your Worker data to read. This creates the database tables and imports the data into the production version of your database.
389420

390421
<Steps>
391-
1. Bootstrap your database with the `schema.sql` file you created in step 4:
422+
1. Create tables and add entries to your remote database with the `schema.sql` file you created in step 4. Enter `y` to confirm your decision.
392423

393424
```sh
394425
npx wrangler d1 execute prod-d1-tutorial --remote --file=./schema.sql
395426
```
427+
```sh output
428+
✔ ⚠️ This process may take some time, during which your D1 database will be unavailable to serve queries.
429+
Ok to proceed? y
430+
🚣 Executed 3 queries in 0.00 seconds (5 rows read, 6 rows written)
431+
Database is currently at bookmark 00000002-00000004-00004ef1-ad4a06967970ee3b20860c86188a4b31.
432+
┌────────────────────────┬───────────┬──────────────┬────────────────────┐
433+
│ Total queries executed │ Rows read │ Rows written │ Database size (MB) │
434+
├────────────────────────┼───────────┼──────────────┼────────────────────┤
435+
│ 3 │ 5 │ 6 │ 0.02 │
436+
└────────────────────────┴───────────┴──────────────┴────────────────────┘
437+
```
396438

397439
2. Validate the data is in production by running:
398440

399-
```sh
400-
npx wrangler d1 execute prod-d1-tutorial --remote --command="SELECT * FROM Customers"
401-
```
441+
```sh
442+
npx wrangler d1 execute prod-d1-tutorial --remote --command="SELECT * FROM Customers"
443+
```
444+
```sh output
445+
⛅️ wrangler 4.13.2
446+
-------------------
447+
448+
🌀 Executing on remote database prod-d1-tutorial (<DATABASE_ID>):
449+
🌀 To execute on your local development database, remove the --remote flag from your wrangler command.
450+
🚣 Executed 1 command in 0.4069ms
451+
┌────────────┬─────────────────────┬───────────────────┐
452+
│ CustomerId │ CompanyName │ ContactName │
453+
├────────────┼─────────────────────┼───────────────────┤
454+
│ 1 │ Alfreds Futterkiste │ Maria Anders │
455+
├────────────┼─────────────────────┼───────────────────┤
456+
│ 4 │ Around the Horn │ Thomas Hardy │
457+
├────────────┼─────────────────────┼───────────────────┤
458+
│ 11 │ Bs Beverages │ Victoria Ashworth │
459+
├────────────┼─────────────────────┼───────────────────┤
460+
│ 13 │ Bs Beverages │ Random Name │
461+
└────────────┴─────────────────────┴───────────────────┘
462+
```
402463

403464
3. Deploy your Worker to make your project accessible on the Internet. Run:
404465

405466
```sh
406467
npx wrangler deploy
407468
```
408-
409469
```sh output
410-
Outputs: https://d1-tutorial.<YOUR_SUBDOMAIN>.workers.dev
411-
```
470+
⛅️ wrangler 4.13.2
471+
-------------------
472+
473+
Total Upload: 0.19 KiB / gzip: 0.16 KiB
474+
Your worker has access to the following bindings:
475+
- D1 Databases:
476+
- DB: prod-d1-tutorial (<DATABASE_ID>)
477+
Uploaded d1-tutorial (3.76 sec)
478+
Deployed d1-tutorial triggers (2.77 sec)
479+
https://d1-tutorial.<YOUR_SUBDOMAIN>.workers.dev
480+
Current Version ID: <VERSION_ID>
481+
```
412482

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

@@ -421,7 +491,7 @@ To deploy your Worker to production using Wrangler, you must first repeat the [d
421491
</TabItem> <TabItem label='Dashboard'>
422492
<Steps>
423493

424-
1. Go to **Workers & Pages** > **Overview**.
494+
1. Go to **Compute (Workers)** > **Workers & Pages**.
425495
2. Select your `d1-tutorial` Worker.
426496
3. Select **Deployments**.
427497
4. From the **Version History** table, select **Deploy version**.
@@ -475,7 +545,7 @@ npx wrangler d1 delete prod-d1-tutorial
475545
</TabItem><TabItem label='Dashboard'>
476546

477547
<Steps>
478-
1. Go to **Storages & Databases** > **D1**.
548+
1. Go to **Storages & Databases** > **D1 SQL Database**.
479549

480550
2. Select your `prod-d1-tutorial` D1 database.
481551

@@ -489,6 +559,10 @@ npx wrangler d1 delete prod-d1-tutorial
489559

490560
</TabItem> </Tabs>
491561

562+
:::caution
563+
Note that deleting your D1 database will stop your application from functioning as before.
564+
:::
565+
492566
If you want to delete your Worker:
493567

494568
<Tabs syncKey='CLIvDash'> <TabItem label='CLI'>
@@ -502,7 +576,7 @@ npx wrangler delete d1-tutorial
502576
</TabItem> <TabItem label='Dashboard'>
503577

504578
<Steps>
505-
1. Go to **Workers & Pages** > **Overview**.
579+
1. Go to **Compute (Workers)** > **Workers & Pages**.
506580

507581
2. Select your `d1-tutorial` Worker.
508582

0 commit comments

Comments
 (0)