Skip to content

Commit 55a9385

Browse files
committed
Restructuring "Query database", changing the example of
1 parent 05a130b commit 55a9385

File tree

1 file changed

+52
-50
lines changed

1 file changed

+52
-50
lines changed

src/content/docs/d1/best-practices/query-d1.mdx

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,15 @@ sidebar:
55
order: 1
66
---
77

8+
D1 is compatible with most SQLite's SQL convention since it leverages SQLite's query engine. You can use SQL commands to query D1.
9+
810
There are a number of ways you can interact with a D1 database:
911

1012
1. Using [D1 Workers Binding API](/d1/worker-api/) in your code.
11-
2. Using [SQL API](/d1/sql-api/sql-statements/).
12-
3. Using [D1 REST API](/api/operations/cloudflare-d1-create-database).
13-
4. Using [D1 Wrangler commands](/d1/wrangler-commands/).
14-
15-
:::note
16-
D1 is compatible with most SQLite's SQL convention since it leverages SQLite's query engine.
17-
:::
18-
19-
## Query D1 with Workers Binding API
20-
21-
Workers Binding API primarily interacts with the data plane, and allows you to query your D1 database from your Worker.
22-
23-
This requires you to:
24-
25-
1. Bind your D1 database to your Worker.
26-
2. Prepare a statement.
27-
3. Run the statement.
28-
29-
```js title="index.js"
30-
export default {
31-
async fetch(request, env) {
32-
const {pathname} = new URL(request.url);
33-
const companyName1 = `Bs Beverages`;
34-
const companyName2 = `Around the Horn`;
35-
const stmt = env.DB.prepare(`SELECT * FROM Customers WHERE CompanyName = ?`);
36-
37-
if (pathname === `/RUN`) {
38-
const returnValue = await stmt.bind(companyName1).run();
39-
return Response.json(returnValue);
40-
}
41-
42-
return new Response(
43-
`Welcome to the D1 API Playground!
44-
\nChange the URL to test the various methods inside your index.js file.`,
45-
);
46-
},
47-
};
48-
```
49-
50-
Refer to [Workers Binding API](/d1/worker-api/) for more information.
13+
2. Using [D1 REST API](/api/operations/cloudflare-d1-create-database).
14+
3. Using [D1 Wrangler commands](/d1/wrangler-commands/).
5115

52-
## Query D1 with SQL API
16+
## Use SQL to query D1
5317

5418
D1 understands SQLite semantics, which allows you to query a database using SQL statements via Workers BindingAPI or REST API (including Wrangler commands). Refer to [D1 SQL API](/d1/sql-api/sql-statements/) to learn more about supported SQL statements.
5519

@@ -101,6 +65,39 @@ SELECT json_extract(sensor_reading, '$.measurement.temp_f')-- returns "77.4" as
10165

10266
Refer to [Query JSON](/d1/sql-api/query-json/) to learn more about querying JSON objects.
10367

68+
## Query D1 with Workers Binding API
69+
70+
Workers Binding API primarily interacts with the data plane, and allows you to query your D1 database from your Worker.
71+
72+
This requires you to:
73+
74+
1. Bind your D1 database to your Worker.
75+
2. Prepare a statement.
76+
3. Run the statement.
77+
78+
```js title="index.js"
79+
export default {
80+
async fetch(request, env) {
81+
const {pathname} = new URL(request.url);
82+
const companyName1 = `Bs Beverages`;
83+
const companyName2 = `Around the Horn`;
84+
const stmt = env.DB.prepare(`SELECT * FROM Customers WHERE CompanyName = ?`);
85+
86+
if (pathname === `/RUN`) {
87+
const returnValue = await stmt.bind(companyName1).run();
88+
return Response.json(returnValue);
89+
}
90+
91+
return new Response(
92+
`Welcome to the D1 API Playground!
93+
\nChange the URL to test the various methods inside your index.js file.`,
94+
);
95+
},
96+
};
97+
```
98+
99+
Refer to [Workers Binding API](/d1/worker-api/) for more information.
100+
104101
## Query D1 with REST API
105102

106103
REST API primarily interacts with the control plane, and allows you to create/manage your D1 database.
@@ -112,15 +109,20 @@ Refer to [D1 REST API](/api/operations/cloudflare-d1-create-database) for D1 RES
112109
You can use Wrangler commands to interact with the control plane. Note that Wrangler commands use REST APIs to perform its operations.
113110

114111
```sh
115-
npx wrangler d1 create prod-d1-tutorial
112+
npx wrangler d1 execute prod-d1-tutorial --command="SELECT * FROM Customers"
116113
```
117-
118114
```sh output
119-
120-
✅ Successfully created DB 'prod-d1-tutorial'
121-
122-
[[d1_databases]]
123-
binding = "DB" # available in your Worker on env.DB
124-
database_name = "prod-d1-tutorial"
125-
database_id = "<unique-ID-for-your-database>"
115+
🌀 Mapping SQL input into an array of statements
116+
🌀 Executing on local database production-db-backend (<DATABASE_ID>) from .wrangler/state/v3/d1:
117+
┌────────────┬─────────────────────┬───────────────────┐
118+
│ CustomerId │ CompanyName │ ContactName │
119+
├────────────┼─────────────────────┼───────────────────┤
120+
│ 1 │ Alfreds Futterkiste │ Maria Anders │
121+
├────────────┼─────────────────────┼───────────────────┤
122+
│ 4 │ Around the Horn │ Thomas Hardy │
123+
├────────────┼─────────────────────┼───────────────────┤
124+
│ 11 │ Bs Beverages │ Victoria Ashworth │
125+
├────────────┼─────────────────────┼───────────────────┤
126+
│ 13 │ Bs Beverages │ Random Name │
127+
└────────────┴─────────────────────┴───────────────────┘
126128
```

0 commit comments

Comments
 (0)