-
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 24 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
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
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: 10 | ||
|
|
||
| --- | ||
|
|
||
|
|
||
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
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: 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 |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| pcx_content_type: navigation | ||
| title: Reference | ||
| sidebar: | ||
| order: 9 | ||
| order: 11 | ||
| 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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| title: SQL API | ||
| pcx_content_type: navigation | ||
| sidebar: | ||
| order: 5 | ||
| 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
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: 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| --- | ||
| pcx_content_type: navigation | ||
| title: D1 Worker Binding API | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| sidebar: | ||
| order: 4 | ||
| --- | ||
|
|
||
| import { DirectoryListing, Details, Steps } from "~/components"; | ||
|
|
||
| You can execute queries on your D1 database through SQL query statements. To do this, you need to perform the following steps: | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 1. [Prepare a statement](/d1/worker-api/prepare-a-statement). | ||
| 2. [Run the prepared statement](/d1/worker-api/run-a-statement). | ||
| 3. Analyze the [return object](/d1/worker-api/return-object) (if necessary). | ||
|
|
||
| Refer to the relevant sections for the API documentation. | ||
|
|
||
| ## Typescript support | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| D1 Worker Bindings API is fully-typed via the `@cloudflare/workers-types` 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. | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| When using the [query statement methods](#query-statement-methods) `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. | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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: | ||
|
|
||
| ```ts | ||
| // Row definition | ||
| type OrderRow = { | ||
| Id: string; | ||
| CustomerName: string; | ||
| OrderDate: number; | ||
| }; | ||
|
|
||
| // Elsewhere in your application | ||
| const result = await env.MY_DB.prepare( | ||
| "SELECT Id, CustomerName, OrderDate FROM [Order] ORDER BY ShippedDate DESC LIMIT 100", | ||
| ).run<OrderRow>(); | ||
| ``` | ||
|
|
||
| ## API playground | ||
|
|
||
| The D1 Worker Binding API playground is an `index.js` file where you can test each of the documented Worker Binding APIs for D1. The file builds from the end-state of the [Get started](/d1/get-started/#write-queries-within-your-worker) code. | ||
|
|
||
| You can use this alongside the API documentation to better understand how each API works. | ||
|
|
||
| Follow the steps to setup your API playground. | ||
|
|
||
| ### 1. Complete the Get started tutorial | ||
|
|
||
| Complete the [Get started](/d1/get-started/#write-queries-within-your-worker) tutorial. Ensure you use JavaScript instead of TypeScript. | ||
|
|
||
| ### 2. Modify the content of `index.js` | ||
|
|
||
| Replace the contents of your `index.js` file with the below to view the effect of each API. | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| <Details header="index.js" open={false}> | ||
| ```js | ||
| export default { | ||
| async fetch(request, env) { | ||
| const { pathname } = new URL(request.url); | ||
|
|
||
| const companyName1 = `Bs Beverages`; | ||
| const companyName2 = `Around the Horn`; | ||
| const stmt = env.DB.prepare(`SELECT * FROM Customers WHERE CompanyName = ?`); | ||
|
|
||
| if (pathname === `/RUN`){ | ||
| const returnValue = await stmt.bind(companyName1).run() | ||
| return Response.json(returnValue); | ||
|
|
||
| } else if (pathname === `/RAW`){ | ||
| const returnValue = await stmt.bind(companyName1).raw(); | ||
| return Response.json(returnValue); | ||
|
|
||
| } else if (pathname === `/FIRST`){ | ||
| const returnValue = await stmt.bind(companyName1).first(); | ||
| return Response.json(returnValue); | ||
|
|
||
| } else if (pathname === `/BATCH`) { | ||
| const batchResult = await env.DB.batch([ | ||
| stmt.bind(companyName1), | ||
| stmt.bind(companyName2) | ||
| ]); | ||
| // const returnValue = await stmt.run(); | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return Response.json(batchResult); | ||
| } | ||
|
|
||
| return new Response( | ||
| `Welcome to the D1 API Playground! | ||
| \nChange the URL to test the methods inside your index.js file.`, | ||
| ); | ||
| }, | ||
| }; | ||
|
|
||
| ``` | ||
| </Details> | ||
|
|
||
| ### 3. Deploy the Worker | ||
|
|
||
| <Steps> | ||
| 1. Navigate to your tutorial directory you created by following step 1. | ||
| 2. Run `npx wrangler dev`. | ||
| ```sh | ||
| npx wrangler dev | ||
| ``` | ||
| ```sh output | ||
| ⛅️ wrangler 3.85.0 (update available 3.86.1) | ||
| ------------------------------------------------------- | ||
|
|
||
| Your worker has access to the following bindings: | ||
| - D1 Databases: | ||
| - DB: <DATABASE_NAME> (DATABASE_ID) (local) | ||
| ⎔ Starting local server... | ||
| [wrangler:inf] Ready on http://localhost:8787 | ||
| ╭───────────────────────────╮ | ||
| │ [b] open a browser │ | ||
| │ [d] open devtools │ | ||
| │ [l] turn off local mode │ | ||
| │ [c] clear console │ | ||
| │ [x] to exit │ | ||
| ╰───────────────────────────╯ | ||
| ``` | ||
| 3. Open a browser at the specified address. | ||
| </Steps> | ||
|
|
||
| ### 4. Test the APIs | ||
|
|
||
| Change the URL to test the various D1 Worker Binding APIs. | ||
|
|
||
Oops, something went wrong.
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.