Skip to content

Commit 57bd74f

Browse files
[D1] Cleaning up Build with D1 (#18321)
* Moving Error section away from "Query D1". * Redirecting Query D1 to Workers Binding API section. * Replacing all interlinks to Query D1 chapter, to the new structure. * Update src/content/docs/d1/worker-api/prepared-statements.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Fixing grammar. * Moving Wrangler commands out of Reference. Reshuffling chapters. * Resolving merge conflict, adding global commands once at the bottom. * Moving "global commands" out of the partial. * Introducing APIs in Query D1. Introducing Wrangler commands properly. * Setting up redirects, fixing broken link. * Adding missing `/` * Fixing redirects. * Resolving two conflicting redirects. * Fixing redirect * Reordering API pages, moving Remote dev below Local dev. * Editing chapter contents to action feedback. * Renaming "Build with D1" to "Best practices". * Moving foreign keys and query json into SQL API. * Moving foreign keys and query json into sql api. * Removing "D1" from REST and Wrangler sidebar. * Introducing query JSON and foreign keys in Query databases chapter. * Restructuring "Query database", changing the example of * Editing Wrangler example. Restructuring to better explain how SQL fits into D1. --------- Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com>
1 parent 704e3a6 commit 57bd74f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+635
-944
lines changed

public/_redirects

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -258,42 +258,52 @@
258258
/constellation/ /workers-ai/ 301
259259

260260
# D1
261-
/d1/client-api/ /d1/build-with-d1/d1-client-api/ 301
261+
/d1/client-api/ /d1/worker-api/ 301
262+
/d1/build-with-d1/d1-client-api/ /d1/worker-api/ 301
263+
/d1/build-with-d1/import-data/ /d1/best-practices/import-export-data/ 301
264+
/d1/build-with-d1/ /d1/best-practices/ 301
265+
/d1/build-with-d1/import-export-data/ /d1/best-practices/import-export-data/ 301
266+
/d1/build-with-d1/use-indexes/ /d1/best-practices/use-indexes/ 301
267+
/d1/build-with-d1/remote-development/ /d1/best-practices/remote-development/ 301
268+
/d1/build-with-d1/local-development/ /d1/best-practices/local-development/ 301
269+
/d1/build-with-d1/foreign-keys/ /d1/sql-api/foreign-keys/ 301
270+
/d1/build-with-d1/query-json/ /d1/sql-api/query-json/ 301
271+
/d1/build-with-d1/use-d1-from-pages/ /d1/best-practices/use-d1-from-pages/ 301
262272
/d1/learning/using-d1-from-pages/ /pages/functions/bindings/#d1-databases 301
263273
/d1/learning/debug-d1/ /d1/observability/debug-d1/ 301
264-
/d1/learning/using-indexes/ /d1/build-with-d1/use-indexes/ 301
265-
/d1/learning/querying-json/ /d1/build-with-d1/query-json/ 301
266-
/d1/learning/importing-data/ /d1/build-with-d1/import-export-data/ 301
274+
/d1/learning/using-indexes/ /d1/best-practices/use-indexes/ 301
275+
/d1/learning/querying-json/ /d1/sql-api/query-json/ 301
276+
/d1/learning/importing-data/ /d1/best-practices/import-export-data/ 301
267277
/d1/learning/generated-columns/ /d1/reference/generated-columns/ 301
268-
/d1/learning/local-development/ /d1/build-with-d1/local-development/ 301
269-
/d1/learning/remote-development/ /d1/build-with-d1/remote-development/ 301
278+
/d1/learning/local-development/ /d1/best-practices/local-development/ 301
279+
/d1/learning/remote-development/ /d1/best-practices/remote-development/ 301
270280
/d1/learning/data-location/ /d1/configuration/data-location/ 301
271281
/d1/migrations/ /d1/reference/migrations/ 301
272282
/d1/platform/wrangler-commands/ /workers/wrangler/commands/#d1 301
273283
/d1/platform/community-projects/ /d1/reference/community-projects/ 301
274-
/d1/platform/client-api/ /d1/build-with-d1/d1-client-api/ 301
284+
/d1/platform/client-api/ /d1/worker-api/ 301
275285
/d1/platform/data-security/ /d1/reference/data-security/ 301
276286
/d1/platform/environments/ /d1/configuration/environments/ 301
277287
/d1/platform/metrics-analytics/ /d1/observability/metrics-analytics/ 301
278288
/d1/platform/migrations/ /d1/reference/migrations/ 301
279-
/d1/reference/client-api/ /d1/build-with-d1/d1-client-api/ 301
289+
/d1/reference/client-api/ /d1/worker-api/ 301
280290
/d1/reference/environments/ /d1/configuration/environments/ 301
281291
/d1/reference/metrics-analytics/ /d1/observability/metrics-analytics/ 301
282-
/d1/how-to/ /d1/build-with-d1/ 301
283-
/d1/how-to/query-databases/ /d1/build-with-d1/d1-client-api/ 301
284-
/d1/how-to/using-indexes/ /d1/build-with-d1/use-indexes/ 301
285-
/d1/how-to/querying-json/ /d1/build-with-d1/query-json/ 301
286-
/d1/how-to/importing-data/ /d1/build-with-d1/import-export-data/ 301
292+
/d1/reference/wrangler-commands/ / /d1/wrangler-commands/ 301
293+
/d1/how-to/ /d1/best-practices/ 301
294+
/d1/how-to/query-databases/ /d1/best-practices/query-d1/ 301
295+
/d1/how-to/using-indexes/ /d1/best-practices/use-indexes/ 301
296+
/d1/how-to/querying-json/ /d1/sql-api/query-json/ 301
297+
/d1/how-to/importing-data/ /d1/best-practices/import-export-data/ 301
287298
/d1/how-to/generated-columns/ /d1/reference/generated-columns/ 301
288-
/d1/build-databases/ /d1/build-with-d1/ 301
289-
/d1/build-databases/query-databases/ /d1/build-with-d1/d1-client-api/ 301
290-
/d1/build-databases/use-indexes/ /d1/build-with-d1/use-indexes/ 301
291-
/d1/build-databases/import-data/ /d1/build-with-d1/import-export-data/ 301
292-
/d1/build-databases/client-api/ /d1/build-with-d1/d1-client-api/ 301
293-
/d1/reference/query-json/ /d1/build-with-d1/query-json/ 301
294-
/d1/configuration/local-development/ /d1/build-with-d1/local-development/ 301
295-
/d1/configuration/remote-development/ /d1/build-with-d1/remote-development/ 301
296-
/d1/build-with-d1/import-data/ /d1/build-with-d1/import-export-data/ 301
299+
/d1/build-databases/ /d1/best-practices/ 301
300+
/d1/build-databases/query-databases/ /d1/best-practices/query-d1/ 301
301+
/d1/build-databases/use-indexes/ /d1/best-practices/use-indexes/ 301
302+
/d1/build-databases/import-data/ /d1/best-practices/import-export-data/ 301
303+
/d1/build-databases/client-api/ /d1/worker-api/ 301
304+
/d1/reference/query-json/ /d1/sql-api/query-json/ 301
305+
/d1/configuration/local-development/ /d1/best-practices/local-development/ 301
306+
/d1/configuration/remote-development/ /d1/best-practices/remote-development/ 301
297307
/d1/reference/database-commands/ /d1/reference/sql-statements/ 301
298308
/d1/reference/sql-statements/ /d1/sql-api/sql-statements/ 301
299309

src/content/changelogs/d1.yaml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ entries:
1919
- publish_date: "2024-07-26"
2020
title: Fixed bug in TypeScript typings for run() API
2121
description: |-
22-
The `run()` method as part of the [D1 Client API](/d1/build-with-d1/d1-client-api/) had an incorrect (outdated) type definition, which has now been addressed as of [`@cloudflare/workers-types`](https://www.npmjs.com/package/@cloudflare/workers-types) version `4.20240725.0`.
22+
The `run()` method as part of the [D1 Client API](/d1/worker-api/) had an incorrect (outdated) type definition, which has now been addressed as of [`@cloudflare/workers-types`](https://www.npmjs.com/package/@cloudflare/workers-types) version `4.20240725.0`.
2323
2424
The correct type definition is `stmt.run<T>(): D1Result`, as `run()` returns the result rows of the query. The previously _incorrect_ type definition was `stmt.run(): D1Response`, which only returns query metadata and no results.
2525
@@ -28,7 +28,7 @@ entries:
2828
description: |-
2929
Previously, D1's [HTTP API](/api/operations/cloudflare-d1-query-database) returned a HTTP `500 Internal Server` error for queries that came in while a D1 database was overloaded. These requests now correctly return a `HTTP 429 Too Many Requests` error.
3030
31-
D1's [Workers API](/d1/build-with-d1/d1-client-api/) is unaffected by this change.
31+
D1's [Workers API](/d1/worker-api/) is unaffected by this change.
3232
3333
- publish_date: "2024-04-30"
3434
title: D1 alpha databases will stop accepting live SQL queries on August 15, 2024
@@ -42,7 +42,7 @@ entries:
4242
description: |-
4343
Previously, D1's [HTTP API](/api/operations/cloudflare-d1-query-database) returned a HTTP `500 Internal Server` error for an invalid query. An invalid SQL query now correctly returns a `HTTP 400 Bad Request` error.
4444
45-
D1's [Workers API](/d1/build-with-d1/d1-client-api/) is unaffected by this change.
45+
D1's [Workers API](/d1/worker-api/) is unaffected by this change.
4646
4747
- publish_date: "2024-04-05"
4848
title: D1 alpha databases are deprecated
@@ -58,7 +58,7 @@ entries:
5858
5959
* Developers with a Workers Paid plan now have a 10GB GB per-database limit (up from 2GB), which can be combined with existing limit of 50,000 databases per account.
6060
* Developers with a Workers Free plan retain the 500 MB per-database limit and can create up to 10 databases per account.
61-
* D1 databases can be [exported](/d1/build-with-d1/import-export-data/#export-an-existing-d1-database) as a SQL file.
61+
* D1 databases can be [exported](/d1/best-practices/import-export-data/#export-an-existing-d1-database) as a SQL file.
6262
6363
- publish_date: "2024-03-12"
6464
title: Change in `wrangler d1 execute` default
@@ -81,24 +81,24 @@ entries:
8181
- publish_date: "2024-02-16"
8282
title: API changes to `run()`
8383
description: |-
84-
A previous change (made on 2024-02-13) to the `run()` [query statement method](/d1/build-with-d1/d1-client-api/#await-stmtrun) has been reverted.
84+
A previous change (made on 2024-02-13) to the `run()` [query statement method](/d1/worker-api/prepared-statements/#run) has been reverted.
8585
86-
`run()` now returns a `D1Result`, including the result rows, matching its original behaviour prior to the change on 2024-02-13.
86+
`run()` now returns a `D1Result`, including the result rows, matching its original behavior prior to the change on 2024-02-13.
8787
88-
Future change to `run()` to return a [`D1ExecResult`](/d1/build-with-d1/d1-client-api/#return-object), as originally intended and documented, will be gated behind a [compatibility date](/workers/configuration/compatibility-dates/) as to avoid breaking existing Workers relying on the way `run()` currently works.
88+
Future change to `run()` to return a [`D1ExecResult`](/d1/worker-api/return-object/#d1execresult), as originally intended and documented, will be gated behind a [compatibility date](/workers/configuration/compatibility-dates/) as to avoid breaking existing Workers relying on the way `run()` currently works.
8989
9090
- publish_date: "2024-02-13"
9191
title: API changes to `raw()`, `all()` and `run()`
9292
description: |-
93-
D1's `raw()`, `all()` and `run()` [query statement methods](/d1/build-with-d1/d1-client-api/#query-statement-methods) have been updated to reflect their intended behaviour and improve compatibility with ORM libraries.
93+
D1's `raw()`, `all()` and `run()` [query statement methods](/d1/worker-api/prepared-statements/) have been updated to reflect their intended behavior and improve compatibility with ORM libraries.
9494
9595
`raw()` now correctly returns results as an array of arrays, allowing the correct handling of duplicate column names (such as when joining tables), as compared to `all()`, which is unchanged and returns an array of objects. To include an array of column names in the results when using `raw()`, use `raw({columnNames: true})`.
9696
97-
`run()` no longer incorrectly returns a `D1Result` and instead returns a [`D1ExecResult`](/d1/build-with-d1/d1-client-api/#return-object) as originally intended and documented.
97+
`run()` no longer incorrectly returns a `D1Result` and instead returns a [`D1ExecResult`](/d1/worker-api/return-object/#d1execresult) as originally intended and documented.
9898
9999
This may be a breaking change for some applications that expected `raw()` to return an array of objects.
100100
101-
Refer to [D1 client API](/d1/build-with-d1/d1-client-api/) to review D1's query methods, return types and TypeScript support in detail.
101+
Refer to [D1 client API](/d1/worker-api/) to review D1's query methods, return types and TypeScript support in detail.
102102
103103
- publish_date: "2024-01-18"
104104
title: Support for LIMIT on UPDATE and DELETE statements
@@ -138,9 +138,9 @@ entries:
138138
- publish_date: "2023-08-19"
139139
title: Row count now returned per query
140140
description: |-
141-
D1 now returns a count of `rows_written` and `rows_read` for every query executed, allowing you to assess the cost of query for both [pricing](/d1/platform/pricing/) and [index optimization](/d1/build-with-d1/use-indexes/) purposes.
141+
D1 now returns a count of `rows_written` and `rows_read` for every query executed, allowing you to assess the cost of query for both [pricing](/d1/platform/pricing/) and [index optimization](/d1/best-practices/use-indexes/) purposes.
142142
143-
The `meta` object returned in [D1's Client API](/d1/build-with-d1/d1-client-api/) contains a total count of the rows read (`rows_read`) and rows written (`rows_written`) by that query. For example, a query that performs a full table scan (for example, `SELECT * FROM users`) from a table with 5000 rows would return a `rows_read` value of `5000`:
143+
The `meta` object returned in [D1's Client API](/d1/worker-api/return-object/#d1result) contains a total count of the rows read (`rows_read`) and rows written (`rows_written`) by that query. For example, a query that performs a full table scan (for example, `SELECT * FROM users`) from a table with 5000 rows would return a `rows_read` value of `5000`:
144144
```json
145145
"meta": {
146146
"duration": 0.20472300052642825,
@@ -195,12 +195,12 @@ entries:
195195
New documentation has been published on how to use D1's support for
196196
[generated columns](/d1/reference/generated-columns/) to define columns that are
197197
dynamically generated on write (or read). Generated columns allow you to extract
198-
data from [JSON objects](/d1/build-with-d1/query-json/) or use the output of other
198+
data from [JSON objects](/d1/sql-api/query-json/) or use the output of other
199199
SQL functions.
200200
- publish_date: "2023-06-12"
201201
title: Deprecating Error.cause
202202
description: |-
203-
As of [`wrangler v3.1.1`](https://github.com/cloudflare/workers-sdk/releases/tag/wrangler%403.1.1) the [D1 client API](/d1/build-with-d1/d1-client-api/) now returns [detailed error messages](/d1/build-with-d1/d1-client-api/#errors) within the top-level `Error.message` property, and no longer requires developers to inspect the `Error.cause.message` property.
203+
As of [`wrangler v3.1.1`](https://github.com/cloudflare/workers-sdk/releases/tag/wrangler%403.1.1) the [D1 client API](/d1/worker-api/) now returns [detailed error messages](/d1/observability/debug-d1/) within the top-level `Error.message` property, and no longer requires developers to inspect the `Error.cause.message` property.
204204
205205
To facilitate a transition from the previous `Error.cause` behaviour, detailed error messages will continue to be populated within `Error.cause` as well as the top-level `Error` object until approximately July 14th, 2023. Future versions of both `wrangler` and the D1 client API will no longer populate `Error.cause` after this date.
206206
- publish_date: "2023-05-19"
@@ -223,7 +223,7 @@ entries:
223223
- publish_date: "2023-05-17"
224224
title: Query JSON
225225
description:
226-
"[New documentation](/d1/build-with-d1/query-json/) has been published
226+
"[New documentation](/d1/sql-api/query-json/) has been published
227227
that covers D1's extensive JSON function support. JSON functions allow you to
228228
parse, query and modify JSON directly from your SQL queries, reducing the number
229229
of round trips to your database, or data queried."

src/content/docs/d1/build-with-d1/import-export-data.mdx renamed to src/content/docs/d1/best-practices/import-export-data.mdx

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

88
D1 allows you to import existing SQLite tables and their data directly, enabling you to migrate existing data into D1 quickly and easily. This can be useful when migrating applications to use Workers and D1, or when you want to prototype a schema locally before importing it to your D1 database(s).
99

10-
D1 also allows you to export a database. This can be useful for [local development](/d1/build-with-d1/local-development/) or testing.
10+
D1 also allows you to export a database. This can be useful for [local development](/d1/best-practices/local-development/) or testing.
1111

1212
## Import an existing database
1313

@@ -69,7 +69,7 @@ The `_cf_KV` table is a reserved table used by D1's underlying storage system. I
6969

7070
:::
7171

72-
From here, you can now query our new table from our Worker [using the D1 client API](/d1/build-with-d1/d1-client-api/).
72+
From here, you can now query our new table from our Worker [using the D1 Workers Binding API](/d1/worker-api/).
7373

7474
:::caution[Known limitations]
7575

@@ -105,12 +105,6 @@ Once you have run the above command, you will need to edit the output SQL file t
105105

106106
You can then follow the steps to [import an existing database](#import-an-existing-database) into D1 by using the `.sql` file you generated from the database dump as the input to `wrangler d1 execute`.
107107

108-
## Foreign key constraints
109-
110-
When importing data, you may need to temporarily disable [foreign key constraints](/d1/build-with-d1/foreign-keys/). To do so, call `PRAGMA defer_foreign_keys = true` before making changes that would violate foreign keys.
111-
112-
Refer to the [foreign key documentation](/d1/build-with-d1/foreign-keys/) to learn more about how to work with foreign keys and D1.
113-
114108
## Export an existing D1 database
115109

116110
In addition to importing existing SQLite databases, you might want to export a D1 database for local development or testing. You can export a D1 database to a `.sql` file using [wrangler d1 export](/workers/wrangler/commands/#export) and then execute (import) with `d1 execute --file`.
@@ -198,8 +192,14 @@ VALUES
198192
('1000', 'Boris Pewter', '2022-12-15 22:16:15');
199193
```
200194

195+
## Foreign key constraints
196+
197+
When importing data, you may need to temporarily disable [foreign key constraints](/d1/sql-api/foreign-keys/). To do so, call `PRAGMA defer_foreign_keys = true` before making changes that would violate foreign keys.
198+
199+
Refer to the [foreign key documentation](/d1/sql-api/foreign-keys/) to learn more about how to work with foreign keys and D1.
200+
201201
## Next Steps
202202

203203
- Read the SQLite [`CREATE TABLE`](https://www.sqlite.org/lang_createtable.html) documentation.
204-
- Learn how to [use the D1 client API](/d1/build-with-d1/d1-client-api/) from within a Worker.
204+
- Learn how to [use the D1 Workers Binding API](/d1/worker-api/) from within a Worker.
205205
- Understand how [database migrations work](/d1/reference/migrations/) with D1.

src/content/docs/d1/build-with-d1/index.mdx renamed to src/content/docs/d1/best-practices/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Build with D1
2+
title: Best practices
33
pcx_content_type: navigation
44
sidebar:
55
order: 3
File renamed without changes.

0 commit comments

Comments
 (0)