Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
702c550
Adding placeholder for new API section.
Oxyjun Oct 31, 2024
b59f2a8
Initializing await stmt API chapter.
Oxyjun Nov 1, 2024
192fa36
Reordering the sidebar. Adding intro to stmt.methods.
Oxyjun Nov 4, 2024
3f7dee4
Setting up Worker Bindings API chapters.
Oxyjun Nov 4, 2024
d1dcb91
Settling on naming convention, fleshing out .db chapter.
Oxyjun Nov 5, 2024
ce19b83
Setting up the database API section.
Oxyjun Nov 6, 2024
1684e9c
Fleshing out API chapters.
Oxyjun Nov 6, 2024
f5d8282
Updating D1 API docs.
Oxyjun Nov 7, 2024
5aa097a
Adding in more information to be complete.
Oxyjun Nov 7, 2024
6ab8172
Apply suggestions from code review
Oxyjun Nov 7, 2024
3d5cb99
Update src/content/docs/d1/worker-api/query.mdx
Oxyjun Nov 7, 2024
c89fc37
Updating the Return Object chapter. Testing more APIs.
Oxyjun Nov 8, 2024
4880d8f
Fleshing out the API docs in more detail.
Oxyjun Nov 11, 2024
66182a0
Fixing broken link.
Oxyjun Nov 11, 2024
a0fb41f
Changing reference to mention Worker Binding APIs.
Oxyjun Nov 11, 2024
e1c2357
Small rename.
Oxyjun Nov 12, 2024
93f6674
Small rename.
Oxyjun Nov 12, 2024
e3741dd
Updating the chapters
Oxyjun Nov 12, 2024
0a349be
Merge branch 'jun/d1/API-reshuffle' of github.com:cloudflare/cloudfla…
Oxyjun Nov 12, 2024
bf9ced4
Modifying the structure to align with discussion.
Oxyjun Nov 12, 2024
8817761
Renaming extensions header for better clarity.
Oxyjun Nov 13, 2024
696a82a
Adding changelog entry for including multiple queries in a single pre…
Oxyjun Nov 13, 2024
1d3637f
Updating wording in Get started to match new folders.
Oxyjun Nov 13, 2024
c3023ab
Fixing broken link
Oxyjun Nov 13, 2024
73083c0
Implementing feedback.
Oxyjun Nov 15, 2024
4f2a119
Removing references to multi-statement prepare().
Oxyjun Nov 15, 2024
7dbf844
Fixing broken links
Oxyjun Nov 15, 2024
9efd9e9
Grammar fixes and deleting redundant comment block.
Oxyjun Nov 15, 2024
caf3890
Apply suggestions from code review
Oxyjun Nov 15, 2024
d3cdce6
Minor rename of the chapter
Oxyjun Nov 15, 2024
ba5fbde
Cleaning up the structure by method hierarchy.
Oxyjun Nov 19, 2024
b7d850d
Adding a note for those who are following the tutorial with an existi…
Oxyjun Nov 19, 2024
cc0de6f
Actioning PR feedback.
Oxyjun Nov 19, 2024
236aad9
Turning all method names into links to the relevant header.
Oxyjun Nov 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/content/docs/d1/configuration/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Configuration
pcx_content_type: navigation
sidebar:
order: 4
order: 5
group:
hideIndex: true
---
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/d1/demos.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pcx_content_type: navigation
title: Demos and architectures
sidebar:
order: 8
order: 9

---

Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/d1/examples/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ hideChildren: false
pcx_content_type: navigation
title: Examples
sidebar:
order: 6
order: 7
group:
hideIndex: true
---
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/d1/observability/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Observability
pcx_content_type: navigation
sidebar:
order: 5
order: 6
group:
hideIndex: true
---
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/d1/platform/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pcx_content_type: navigation
title: Platform
sidebar:
order: 8
order: 9
group:
hideIndex: true
---
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/d1/reference/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pcx_content_type: navigation
title: Reference
sidebar:
order: 9
order: 10
group:
hideIndex: true
---
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/d1/tutorials/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pcx_content_type: navigation
title: Tutorials
hideChildren: true
sidebar:
order: 7
order: 8

---

Expand Down
12 changes: 12 additions & 0 deletions src/content/docs/d1/worker-api/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
pcx_content_type: navigation
title: Worker API
sidebar:
order: 4
group:
hideIndex: true
---

import { DirectoryListing } from "~/components";

<DirectoryListing />
282 changes: 282 additions & 0 deletions src/content/docs/d1/worker-api/query.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
---
title: D1 Query SQL Statements
pcx_content_type: concept
sidebar:
order: 4
---

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

## Description

## Methods

### await stmt.all()

Returns all rows as an array of objects, with each result row represented as an object on the `results` property of the `D1Result` type.

```js
const { results } = await stmt.all();
```

#### Parameters

- None.

#### Return values

- <code>Array</code>: <Type text="Array"/>
- An array of objects.
- Each row represents a row.
- Each object is created on the `results` property of the `D1Result` type.

<Details header="Example of return values" open = {false}>
```js
const stmt = db.prepare("SELECT name, age FROM users LIMIT 3");
const { results } = await stmt.all();
console.log(results);
```

```js output
[
{
name: "John",
age: 42,
},
{
name: "Anthony",
age: 37,
},
{
name: "Dave",
age: 29,
},
]
```
</Details>

#### Guidance

- When joining tables with identical column names, only the leftmost column will be included in the row object. Use [`stmt.raw()`](#await-stmtraw) to return all rows as an array of arrays.
- When using TypeScript, you can pass a [type parameter](/d1/build-with-d1/d1-client-api/#typescript-support) to `all()` to return a typed result object.

### await stmt.raw()

Returns results as an array of arrays, with each row represented by an array. The return type is an array of arrays, and does not include query metadata.

Column names are not included in the result set by default. To include column names as the first row of the result array, set `.raw({columnNames: true})`.

```js
const rows = await stmt.raw();
```

#### Parameters

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

#### Return values

- <code>Array</code>: <Type text="Array"/>
- An array of arrays.
- Each array represents a row.

<Details header="Example of return values" open = {false}>
```js
const stmt = db.prepare("SELECT name, age FROM users LIMIT 3");
const rows = await stmt.raw();
console.log(rows);
```
```js output
[
[ "John", 42 ],
[ "Anthony", 37 ],
[ "Dave", 29 ],
]
```

With parameter `columnNames: true`:
```js
const stmt = db.prepare("SELECT name, age FROM users LIMIT 3");
const [columns, ...rows] = await stmt.raw({ columnNames: true });
console.log(columns);
```
```js output
[ "name", age ], // The first result array includes the column names
```
</Details>

#### Guidance

- 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.

### await stmt.first()

Returns the first row of the query result. This does not return metadata like the other methods. Instead, it directly returns the object.

```js
const values = await stmt.first();
const total = await stmt.first("columnName");
```

#### Parameters

- <code>columnName</code>: <Type text="String"/> <MetaInfo text="Optional"/>
- Specify a `columnName` to return the value from a specific column in the first row of the query result.
- None.
- Do not pass a parameter to obtain all columns from the first row.

#### Return values

- <code>firstRow</code>: <Type text="Object"/>
- An object containing the first row of the query result.

- `null`: <Type text="null"/>
- If the query returns no rows.

<Details header ="Example of return values" open = {false}>

Get all the columns from the first row:

```js
const stmt = db.prepare("SELECT COUNT(*) AS total FROM users");
const values = await stmt.first();
console.log(values);
```
```js output
{ total: 50 }
```

Get a specific column from the first row:

```js
const stmt = db.prepare("SELECT COUNT(*) AS total FROM users");
const total = await stmt.first("total");
console.log(total);
```
```js output
50 // NEED CONFIRMING
```
</Details>

#### Guidance

- If the query returns rows but `column` does not exist, then `first()` throws the `D1_ERROR` exception.
- `stmt.first()` does not alter the SQL query. To improve performance, consider appending `LIMIT 1` to your statement.
- 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.

### await stmt.run()

Runs the query (or queries) and returns results.

```js
const { results } = await stmt.run();
```

#### Parameter

- None.

#### Return value

- <code>results</code>: <Type text="Array"/>
- An array of objects, where each object represents a row of the query result.
- Each object is created on the `results` property of the `D1Result` type. {/*What does it return when `results` is actually empty?*/}

<Details header="Example of return values" open = {false}>
```js
const stmt = await db.prepare("SELECT name, age FROM users LIMIT 3");
const { results } = await stmt.run();
console.log(results);
```
```js output
[
{
name: "John",
age: 42,
},
{
name: "Anthony",
age: 37,
},
{
name: "Dave",
age: 29,
},
]
```
</Details>

#### Guidance

- `results` is empty for write operations such as UPDATE, DELETE, or INSERT.
- Run is functionally equivalent to `stmt.all()` and can be treated as an alias.
- 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.

### await db.dump()

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

Dumps the entire D1 database to an SQLite compatible file inside an ArrayBuffer.

```js
const dump = await db.dump();
return new Response(dump, {
status: 200,
headers: {
"Content-Type": "application/octet-stream",
},
});
```

#### Parameters

- None.

#### Return values

- ???

#### Guidance

- ???

### await db.exec()

Executes one or more queries directly without prepared statements or parameters binding.

```js
const out = await db.exec();
```

#### Parameters

- ???

#### Return values

- ???

<Details header="Example of return values" open={false}>
```js
const migration = await fetch("/migration.sql");
const out = await db.exec(migration.text());
console.log(out);
```
```js output
{
count: 80,
duration: 76
}
```
</Details>

#### Guidance

- This method can have poorer performance (prepared statements can be reused in some cases) and, more importantly, is less safe.
- Only use this method for maintenance and one-shot tasks (for example, migration jobs).
- The input can be one or multiple queries separated by `\n`.
- If an error occurs, an exception is thrown with the query and error messages, execution stops and further statements are not executed. Refer to [Errors](/d1/build-with-d1/d1-client-api/#errors) to learn more.

Loading