Skip to content

Commit 265784c

Browse files
lambrospetrouOxyjun
authored andcommitted
Extend D1 docs with clarifications for one-way type conversions (#22093)
* Extend D1 docs with clarifications for one-way type conversions Clarify the type conversion happening in D1 to avoid confusion by users. Example: cloudflare/workers-sdk#8642 * PCX Review --------- Co-authored-by: Jun Lee <[email protected]>
1 parent d47321d commit 265784c

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

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

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,44 @@ const result = await env.MY_DB.prepare(
4040

4141
## Type conversion
4242

43-
D1 automatically converts supported JavaScript (including TypeScript) types passed as parameters via the Workers Binding API to their associated D1 types. The type conversion is as follows:
44-
45-
| JavaScript | D1 |
46-
| -------------------- | ---------------------------------------------------------------------------- |
47-
| null | `NULL` |
48-
| Number | `REAL` |
49-
| Number <sup>1</sup> | `INTEGER` |
50-
| String | `TEXT` |
51-
| Boolean <sup>2</sup> | `INTEGER` |
52-
| ArrayBuffer | `BLOB` |
53-
| undefined | Not supported. Queries with `undefined` values will return a `D1_TYPE_ERROR` |
54-
55-
<sup>1</sup> D1 supports 64-bit signed `INTEGER` values internally, however
43+
D1 automatically converts supported JavaScript (including TypeScript) types passed as parameters via the Workers Binding API to their associated D1 types <sup>1</sup>.
44+
This conversion is permanent and one-way only. This means that when reading the written values back in your code, you will get the converted values rather than the originally inserted values.
45+
46+
:::note
47+
We recommend using [STRICT tables](https://www.sqlite.org/stricttables.html) in your SQL schema to avoid issues with mismatched types between values that are actually stored in your database compared to values defined by your schema.
48+
:::
49+
50+
The type conversion during writes is as follows:
51+
52+
| JavaScript (write) | D1 | JavaScript (read) |
53+
| -------------------- | --------------------------- | ------------------ |
54+
| null | `NULL` | null |
55+
| Number | `REAL` | Number |
56+
| Number <sup>2</sup> | `INTEGER` | Number |
57+
| String | `TEXT` | String |
58+
| Boolean <sup>3</sup> | `INTEGER` | Number (`0`,`1`) |
59+
| ArrayBuffer | `BLOB` | Array <sup>4</sup> |
60+
| ArrayBuffer View | `BLOB` | Array <sup>4</sup> |
61+
| undefined | Not supported. <sup>5</sup> | - |
62+
63+
<sup>1</sup> D1 types correspond to the underlying [SQLite
64+
types](https://www.sqlite.org/datatype3.html).
65+
66+
<sup>2</sup> D1 supports 64-bit signed `INTEGER` values internally, however
5667
[BigInts](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)
5768
are not currently supported in the API yet. JavaScript integers are safe up to
5869
[`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER).
5970

60-
<sup>2</sup> Booleans will be cast to an `INTEGER` type where `1` is `TRUE` and
71+
<sup>3</sup> Booleans will be cast to an `INTEGER` type where `1` is `TRUE` and
6172
`0` is `FALSE`.
6273

74+
<sup>4</sup> `ArrayBuffer` and [`ArrayBuffer`
75+
views](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView)
76+
are converted using
77+
[`Array.from`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from).
78+
79+
<sup>5</sup> Queries with `undefined` values will return a `D1_TYPE_ERROR`.
80+
6381
## API playground
6482

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

0 commit comments

Comments
 (0)