Skip to content

Commit cc0de6f

Browse files
committed
Actioning PR feedback.
1 parent b7d850d commit cc0de6f

File tree

5 files changed

+37
-36
lines changed

5 files changed

+37
-36
lines changed

src/content/docs/d1/sql-api/sql-statements.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Details, Render } from "~/components";
99

1010
D1 is compatible with most SQLite's SQL convention since it leverages SQLite's query engine. D1 supports a number of database-level statements that allow you to list tables, indexes, and inspect the schema for a given table or index.
1111

12-
You can execute any of these statements via the D1 console in the Cloudflare dashboard, [`wrangler d1 execute`](/workers/wrangler/commands/#d1), or with the [D1 Worker Bindings API](/d1/worker-api/d1-binding).
12+
You can execute any of these statements via the D1 console in the Cloudflare dashboard, [`wrangler d1 execute`](/workers/wrangler/commands/#d1), or with the [D1 Worker Bindings API](/d1/worker-api/d1-database).
1313

1414
## Supported SQLite extensions
1515

src/content/docs/d1/worker-api/d1-binding.mdx renamed to src/content/docs/d1/worker-api/d1-database.mdx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: D1 binding
2+
title: D1 Database
33
pcx_content_type: concept
44
sidebar:
55
order: 1
@@ -19,7 +19,7 @@ A D1 binding has the type `D1Database`, and supports a number of methods, as lis
1919

2020
## Methods
2121

22-
### `db.prepare()`
22+
### `prepare()`
2323

2424
Prepares a query statement to be later executed.
2525

@@ -30,7 +30,7 @@ const stmt = env.DB.prepare("SELECT * FROM Customers WHERE CompanyName = ?").bin
3030

3131
#### Parameters
3232

33-
- <code>sqlQuery</code>: <Type text="String"/> <MetaInfo text="Required"/>
33+
- <code>query</code>: <Type text="String"/> <MetaInfo text="Required"/>
3434
- The SQL query you wish to execute on the database.
3535

3636
#### Return values
@@ -46,7 +46,7 @@ const stmt = env.DB.prepare("SELECT * FROM Customers WHERE CompanyName = ?").bin
4646
| `?NNN` | Ordered | A question mark followed by a number `NNN` holds a spot for the `NNN`-th parameter. `NNN` must be between `1` and `SQLITE_MAX_VARIABLE_NUMBER` |
4747
| `?` | Anonymous | A question mark that is not followed by a number creates a parameter with a number one greater than the largest parameter number already assigned. If this means the parameter number is greater than `SQLITE_MAX_VARIABLE_NUMBER`, it is an error. This parameter format is provided for compatibility with other database engines. But because it is easy to miscount the question marks, the use of this parameter format is discouraged. Programmers are encouraged to use one of the symbolic formats below or the `?NNN` format above instead. |
4848

49-
To bind a parameter, use the `stmt.bind()` method.
49+
To bind a parameter, use the `D1PreparedStatement::bind` method.
5050

5151
Order and anonymous examples:
5252

@@ -91,13 +91,13 @@ const stmt = env.DB.prepare("SELECT * FROM Customers WHERE CompanyName = Bs Beve
9191
// `stmt` is a static statement.
9292
```
9393

94-
### `db.batch()`
94+
### `batch()`
9595

9696
Sends multiple SQL statements inside a single call to the database. This can have a huge performance impact as it reduces latency from network round trips to D1. D1 operates in auto-commit. Our implementation guarantees that each statement in the list will execute and commit, sequentially, non-concurrently.
9797

9898
Batched statements are [SQL transactions](https://www.sqlite.org/lang_transaction.html). If a statement in the sequence fails, then an error is returned for that specific statement, and it aborts or rolls back the entire sequence.
9999

100-
To send batch statements, provide `.batch()` a list of prepared statements and get the results in the same order.
100+
To send batch statements, provide `D1Database::batch` a list of prepared statements and get the results in the same order.
101101

102102
```js
103103
const companyName1 = `Bs Beverages`;
@@ -112,12 +112,12 @@ const batchResult = await env.DB.batch([
112112
#### Parameters
113113

114114
- <code>statements</code>: <Type text="Array"/>
115-
- An array of `db.prepare()` statements.
115+
- An array of [`D1PreparedStatement`](#prepare)s.
116116

117117
#### Return values
118118

119119
- <code>results</code>: <Type text="Array"/>
120-
- An array of `D1Result` objects containing the results of the `.db.prepare()` statements. Each object is in the array position corresponding to the array position of the initial `db.prepare()` statement within the `statementArray`.
120+
- An array of `D1Result` objects containing the results of the `D1Database::prepare` statements. Each object is in the array position corresponding to the array position of the initial `D1Database::prepare` statement within the `statements`.
121121
- Refer to [`D1Result`](/d1/worker-api/return-object/#d1result) for more information about this object.
122122

123123
<Details header="Example of return values" open={false}>
@@ -209,7 +209,7 @@ console.log(stmt[1].results);
209209
return Response.json(batchResult);
210210
```
211211

212-
### `db.exec()`
212+
### `exec()`
213213

214214
Executes one or more queries directly without prepared statements or parameter bindings.
215215

@@ -219,7 +219,7 @@ const returnValue = await env.DB.exec(`SELECT * FROM Customers WHERE CompanyName
219219

220220
#### Parameters
221221

222-
- <code>queryString</code>: <Type text="String"/> <MetaInfo text="Required"/>
222+
- <code>query</code>: <Type text="String"/> <MetaInfo text="Required"/>
223223
- The SQL query statement without parameter binding.
224224

225225
#### Return values
@@ -249,7 +249,7 @@ return Response.json(returnValue);
249249
- Only use this method for maintenance and one-shot tasks (for example, migration jobs).
250250
- The input can be one or multiple queries separated by `\n`.
251251

252-
### `db.dump`
252+
### `dump`
253253

254254
:::caution
255255
This API only works on databases created during D1's alpha period. Check which version your database uses with `wrangler d1 info <DATABASE_NAME>`.

src/content/docs/d1/worker-api/index.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { DirectoryListing, Details, Steps } from "~/components";
99

1010
You can execute SQL queries on your D1 database from a Worker using the Worker Binding API. To do this, you can perform the following steps:
1111

12-
1. [Bind the D1 Database](/d1/worker-api/d1-binding).
13-
2. [Prepare a statement](/d1/worker-api/d1-binding/#dbprepare).
12+
1. [Bind the D1 Database](/d1/worker-api/d1-database).
13+
2. [Prepare a statement](/d1/worker-api/d1-database/#dbprepare).
1414
3. [Run the prepared statement](/d1/worker-api/prepared-statements).
1515
4. Analyze the [return object](/d1/worker-api/return-object) (if necessary).
1616

@@ -20,9 +20,9 @@ Refer to the relevant sections for the API documentation.
2020

2121
D1 Worker Bindings API is fully-typed via the [`@cloudflare/workers-types`](/workers/languages/typescript/#typescript) package, and also supports [generic types](https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-types) as part of its TypeScript API. A generic type allows you to provide an optional `type parameter` so that a function understands the type of the data it is handling.
2222

23-
When using the [query statement methods](/d1/worker-api/prepared-statements) `stmt.run()`, `stmt.raw()` and `stmt.first()`, you can provide a type representing each database row. D1's API will [return the result object](#return-object) with the correct type.
23+
When using the [query statement methods](/d1/worker-api/prepared-statements) `D1PreparedStatement::run`, `D1PreparedStatement::raw` and `D1PreparedStatement::first`, you can provide a type representing each database row. D1's API will [return the result object](#return-object) with the correct type.
2424

25-
For example, providing an `OrderRow` type as a type parameter to `stmt.run()` will return a typed `Array<OrderRow>` object instead of the default `Record<string, unknown>` type:
25+
For example, providing an `OrderRow` type as a type parameter to `D1PreparedStatement::run` will return a typed `Array<OrderRow>` object instead of the default `Record<string, unknown>` type:
2626

2727
```ts
2828
// Row definition

src/content/docs/d1/worker-api/prepared-statements.mdx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ sidebar:
77

88
import { Type, MetaInfo, Details } from "~/components";
99

10-
This chapter documents the various ways you can run and retrieve the results of a query after you have [prepared your statement](/d1/worker-api/d1-binding/#dbprepare).
10+
This chapter documents the various ways you can run and retrieve the results of a query after you have [prepared your statement](/d1/worker-api/d1-database/#dbprepare).
1111

1212
## Methods
1313

14-
### `stmt.run()`
14+
### `run()`
1515

1616
Runs the prepared query (or queries) and returns results. The returned results includes metadata.
1717

@@ -70,8 +70,8 @@ return Response.json(returnValue);
7070
#### Guidance
7171

7272
- `results` is empty for write operations such as `UPDATE`, `DELETE`, or `INSERT`.
73-
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `run()` to return a typed result object.
74-
- `stmt.run()` is functionally equivalent to `stmt.all()`, and can be treated as an alias.
73+
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `D1PreparedStatement::run` to return a typed result object.
74+
- `D1PreparedStatement::run` is functionally equivalent to `D1PreparedStatement::all`, and can be treated as an alias.
7575
- You can choose to extract only the results you expect from the statement by simply returning the `results` property of the return object.
7676

7777
<Details header="Example of returning only the `results`" open={false}>
@@ -94,7 +94,7 @@ return Response.json(returnValue.results);
9494
```
9595
</Details>
9696

97-
### `stmt.raw()`
97+
### `raw()`
9898

9999
Runs the prepared query (or queries), and returns the results as an array of arrays. The returned results do not include metadata.
100100

@@ -106,8 +106,8 @@ const returnValue = await stmt.raw();
106106

107107
#### Parameters
108108

109-
- <code>columnNames</code>: <Type text="Boolean"/> <MetaInfo text="Optional"/>
110-
- A boolean flag which includes column names as the first row of the result array.
109+
- <code>columnNames</code>: <Type text="Object"/> <MetaInfo text="Optional"/>
110+
- A boolean object which includes column names as the first row of the result array.
111111

112112
#### Return values
113113

@@ -118,7 +118,7 @@ const returnValue = await stmt.raw();
118118
```js
119119
const someVariable = `Bs Beverages`;
120120
const stmt = env.DB.prepare("SELECT * FROM Customers WHERE CompanyName = ?").bind(someVariable);
121-
const returnValue = await stmt.raw(); // columnName not specified
121+
const returnValue = await stmt.raw();
122122
return Response.json(returnValue);
123123
```
124124
```js output
@@ -158,9 +158,9 @@ return Response.json(returnValue)
158158

159159
#### Guidance
160160

161-
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `raw()` to return a typed result array.
161+
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `D1PreparedStatement::raw` to return a typed result array.
162162

163-
### `stmt.first()`
163+
### `first()`
164164

165165
Runs the prepared query (or queries), and returns the first row of the query result as an object. This does not return any metadata. Instead, it directly returns the object.
166166

@@ -177,8 +177,9 @@ const values = await stmt.first();
177177

178178
#### Return values
179179

180-
- <code>firstRow</code>: <Type text="Object"/>
180+
- <code>firstRow</code>: <Type text="Object"/> <MetaInfo text="Optional"/>
181181
- An object containing the first row of the query result.
182+
- The return value will be further filtered to a specific attribute if `columnName` was specified.
182183

183184
- `null`: <Type text="null"/>
184185
- If the query returns no rows.
@@ -216,7 +217,7 @@ return Response.json(returnValue)
216217

217218
#### Guidance
218219

219-
- If the query returns rows but `column` does not exist, then `first()` throws the `D1_ERROR` exception.
220-
- `stmt.first()` does not alter the SQL query. To improve performance, consider appending `LIMIT 1` to your statement.
221-
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `first()` to return a typed result object.
220+
- If the query returns rows but `column` does not exist, then `D1PreparedStatement::first` throws the `D1_ERROR` exception.
221+
- `D1PreparedStatement::first` does not alter the SQL query. To improve performance, consider appending `LIMIT 1` to your statement.
222+
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `D1PreparedStatement::first` to return a typed result object.
222223

src/content/docs/d1/worker-api/return-object.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ sidebar:
77

88
Some D1 Worker Binding APIs return a typed object.
99

10-
| D1 Worker Binding API | Return object |
11-
| ------------------------- | ------------- |
12-
| `stmt.run()`, `db.batch()`| `D1Result` |
13-
| `db.exec()` | `D1ExecResult`|
10+
| D1 Worker Binding API | Return object |
11+
| ---------------------------------------------- | ------------- |
12+
| `D1PreparedStatement::run`, `D1Database::batch`| `D1Result` |
13+
| `D1Database::exec` | `D1ExecResult`|
1414

1515
## D1Result
1616

17-
The methods `stmt.run()` and `db.batch()` return a typed `D1Result` object for each query statement. This object contains:
17+
The methods `D1PreparedStatement::run` and `D1Database::batch` return a typed `D1Result` object for each query statement. This object contains:
1818

1919
- The success status
2020
- A meta object with the internal duration of the operation in milliseconds
@@ -75,7 +75,7 @@ return Response.json(result)
7575

7676
## D1ExecResult
7777

78-
The method `db.exec()` returns a typed `D1ExecResult` object for each query statement. This object contains:
78+
The method `D1Database::exec` returns a typed `D1ExecResult` object for each query statement. This object contains:
7979

8080
- The number of executed queries
8181
- The duration of the operation in milliseconds

0 commit comments

Comments
 (0)