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
@@ -12,14 +12,24 @@ This guide instructs you through:
12
12
13
13
- Creating your first database using D1, Cloudflare's native serverless SQL database.
14
14
- 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.
16
16
17
17
You can perform these tasks through the CLI or through the Cloudflare dashboard.
18
18
19
19
:::note
20
20
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).
21
21
:::
22
22
23
+
## Quick start
24
+
25
+
If you want to skip the steps and get started quickly, click on the button below.
26
+
27
+
[](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.
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**.
84
94
3. Select **Create**.
85
-
4.Select**Create Worker**.
95
+
4.Under**Start from a template**, select **Hello world**.
86
96
5. Name your Worker. For this tutorial, name your Worker `d1-tutorial`.
87
97
6. Select **Deploy**.
88
98
</Steps>
89
99
</TabItem> </Tabs>
90
100
91
101
## 2. Create a database
92
102
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).
94
104
95
105
To create your first D1 database:
96
106
@@ -103,37 +113,46 @@ To create your first D1 database:
103
113
cd d1-tutorial
104
114
```
105
115
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
+
:::
107
123
108
124
```sh
109
-
npx wrangler d1 create prod-d1-tutorial
125
+
npx wrangler@latest d1 create prod-d1-tutorial
110
126
```
111
127
112
128
```sh output
113
129
114
-
✅ Successfully created DB 'prod-d1-tutorial'
130
+
✅ Successfully created DB 'prod-d1-tutorial'in region WEUR
131
+
Created your new D1 database.
115
132
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
+
}
121
142
122
-
</Steps>
143
+
```
123
144
124
-
This creates a new D1 database and outputs the [binding](/workers/runtime-apis/bindings/) configuration needed in the next step.
125
145
126
-
:::note
127
146
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>
129
148
130
-
:::
149
+
This creates a new D1 database and outputs the [binding](/workers/runtime-apis/bindings/) configuration needed in the next step.
131
150
132
151
</TabItem> <TabItemlabel='Dashboard'>
133
152
134
153
<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**.
137
156
3. Name your database. For this tutorial, name your D1 database `prod-d1-tutorial`.
138
157
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.
139
158
5. Select **Create**.
@@ -198,7 +217,7 @@ You can also bind your D1 database to a [Pages Function](/pages/functions/). For
198
217
You create bindings by adding them to the Worker you have created.
199
218
200
219
<Steps>
201
-
1. Go to **Workers & Pages** > **Overview**.
220
+
1. Go to **Compute (Workers)** > **Workers & Pages**.
202
221
2. Select the `d1-tutorial` Worker you created in [step 1](/d1/get-started/#1-create-a-worker).
203
222
3. Select **Settings**.
204
223
4. Scroll to **Bindings**, then select **Add**.
@@ -217,11 +236,11 @@ You create bindings by adding them to the Worker you have created.
217
236
218
237
## 4. Run a query against your D1 database
219
238
220
-
### Configure your D1 database
239
+
### Populate your D1 database
221
240
222
241
<TabssyncKey='CLIvDash'> <TabItemlabel='CLI'>
223
242
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.
225
244
226
245
<Steps>
227
246
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
@@ -267,7 +298,7 @@ After correctly preparing your [Wrangler configuration file](/workers/wrangler/c
267
298
Use the Dashboard to create a table and populate it with data.
268
299
269
300
<Steps>
270
-
1. Go to **Storage & Databases** > **D1**.
301
+
1. Go to **Storage & Databases** > **D1 SQL Database**.
271
302
2. Select the `prod-d1-tutorial` database you created in [step 2](/d1/get-started/#2-create-a-database).
272
303
3. Select **Console**.
273
304
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.
329
360
330
361
In the code above, you:
331
362
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`.
333
364
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.
335
366
4. Execute the query by calling `all()` to return all rows (or none, if the query returns none).
336
367
5. Return your query results, if any, in JSON format with `Response.json(results)`.
337
368
@@ -344,9 +375,9 @@ After configuring your Worker, you can test your project locally before you depl
344
375
You can query your D1 database using your Worker.
345
376
346
377
<Steps>
347
-
1. Go to **Workers & Pages** > **Overview**.
378
+
1. Go to **Compute (Workers)** > **Workers & Pages**.
348
379
2. Select the `d1-tutorial` Worker you created.
349
-
3. Select **Edit Code**.
380
+
3. Select the **Edit code** icon (**\<\/\>**).
350
381
4. Clear the contents of the `worker.js` file, then paste the following code:
351
382
352
383
```js
@@ -379,36 +410,75 @@ You can query your D1 database using your Worker.
379
410
</TabItem>
380
411
</Tabs>
381
412
382
-
## 5. Deploy your database
413
+
## 5. Deploy your application
383
414
384
-
Deploy your database on Cloudflare's global network.
415
+
Deploy your application on Cloudflare's global network.
385
416
386
417
<TabssyncKey='CLIvDash'> <TabItemlabel='CLI'>
387
418
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.
389
420
390
421
<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.
0 commit comments