-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[D1] Creating proper API documentation #17923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 b59f2a8
Initializing await stmt API chapter.
Oxyjun 192fa36
Reordering the sidebar. Adding intro to stmt.methods.
Oxyjun 3f7dee4
Setting up Worker Bindings API chapters.
Oxyjun d1dcb91
Settling on naming convention, fleshing out .db chapter.
Oxyjun ce19b83
Setting up the database API section.
Oxyjun 1684e9c
Fleshing out API chapters.
Oxyjun f5d8282
Updating D1 API docs.
Oxyjun 5aa097a
Adding in more information to be complete.
Oxyjun 6ab8172
Apply suggestions from code review
Oxyjun 3d5cb99
Update src/content/docs/d1/worker-api/query.mdx
Oxyjun c89fc37
Updating the Return Object chapter. Testing more APIs.
Oxyjun 4880d8f
Fleshing out the API docs in more detail.
Oxyjun 66182a0
Fixing broken link.
Oxyjun a0fb41f
Changing reference to mention Worker Binding APIs.
Oxyjun e1c2357
Small rename.
Oxyjun 93f6674
Small rename.
Oxyjun e3741dd
Updating the chapters
Oxyjun 0a349be
Merge branch 'jun/d1/API-reshuffle' of github.com:cloudflare/cloudfla…
Oxyjun bf9ced4
Modifying the structure to align with discussion.
Oxyjun 8817761
Renaming extensions header for better clarity.
Oxyjun 696a82a
Adding changelog entry for including multiple queries in a single pre…
Oxyjun 1d3637f
Updating wording in Get started to match new folders.
Oxyjun c3023ab
Fixing broken link
Oxyjun 73083c0
Implementing feedback.
Oxyjun 4f2a119
Removing references to multi-statement prepare().
Oxyjun 7dbf844
Fixing broken links
Oxyjun 9efd9e9
Grammar fixes and deleting redundant comment block.
Oxyjun caf3890
Apply suggestions from code review
Oxyjun d3cdce6
Minor rename of the chapter
Oxyjun ba5fbde
Cleaning up the structure by method hierarchy.
Oxyjun b7d850d
Adding a note for those who are following the tutorial with an existi…
Oxyjun cc0de6f
Actioning PR feedback.
Oxyjun 236aad9
Turning all method names into links to the relevant header.
Oxyjun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| pcx_content_type: navigation | ||
| title: Demos and architectures | ||
| sidebar: | ||
| order: 8 | ||
| order: 9 | ||
|
|
||
| --- | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| pcx_content_type: navigation | ||
| title: Platform | ||
| sidebar: | ||
| order: 8 | ||
| order: 9 | ||
| group: | ||
| hideIndex: true | ||
| --- | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| pcx_content_type: navigation | ||
| title: Reference | ||
| sidebar: | ||
| order: 9 | ||
| order: 10 | ||
| group: | ||
| hideIndex: true | ||
| --- | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ pcx_content_type: navigation | |
| title: Tutorials | ||
| hideChildren: true | ||
| sidebar: | ||
| order: 7 | ||
| order: 8 | ||
|
|
||
| --- | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ### 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. | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ### 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. | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ### 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. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.