Diffinery

homeflare/celldpublic

90 lines · 4.2 KBRaw History
1# D1
2
3D1 is a SQL database that a Worker reaches through a binding. celld gives each
4database its own SQLite file inside one cell. Read the
5[Cloudflare D1 documentation](https://developers.cloudflare.com/d1/worker-api/d1-database/)
6for the API.
7
8## Example
9
10The [D1 example](../../examples/d1) implements a guestbook with a D1 database.
11
12<!-- celld-example: d1 -->
13
14## API
15
16- `prepare(sql)` returns a statement, and `bind(...values)` returns a new
17 statement that carries the values.
18- `all()` returns `success`, a `results` array of row objects, and `meta`.
19 `run()` is the same method under a second name.
20- `first()` returns the first row or `null`, and `first(column)` returns one
21 column of that row.
22- `raw()` returns the rows as arrays, and `raw({ columnNames: true })` puts the
23 column names in the first array.
24- `batch(statements)` runs prepared statements in one transaction and returns
25 one result for each statement.
26- `exec(sql)` runs one or more statements without parameters, such as a schema.
27
28## Configuration and limits
29
30A `d1_databases` entry gives a `binding`, a `database_name`, and a
31`database_id`. celld uses the `database_id` as the identity, or the
32`database_name` when the entry has no id. Two Workers that name one identity
33reach one database. celld creates a database on first use, so a typo in the
34name opens an empty database and does not cause an error.
35
36`bind()` checks each value. A number and a string pass, a boolean becomes `1`
37or `0`, and an `ArrayBuffer`, a typed array, or an array of byte numbers becomes
38a `BLOB`. Any other value throws `D1_TYPE_ERROR`. celld binds the positional
39parameters `?` and `?NNN`.
40
41celld enforces these
42[D1 limits](https://developers.cloudflare.com/d1/platform/limits/): a statement
43of at most 100,000 bytes, at most 100 bound parameters, at most 100 columns in a
44table, and a string, a `BLOB`, or a row of at most 2,200,000 bytes.
45
46A prepared statement that holds a second statement fails with
47`A prepared SQL statement must contain only one statement.` Use `exec()` or
48`batch()` for more than one statement. Both run inside one transaction, so a
49failure rolls back the whole call.
50
51Every statement goes to the node that owns the database cell, so each call costs
52one cell dispatch. Group related statements in one `batch()`, which costs one
53dispatch for all of them. The 100,000-row result cap does not apply to
54`first()`. An acknowledged write survives the loss of the owner
55node.
56
57`withSession()` always selects the primary. `getBookmark()` returns
58`celld:primary`, `meta.served_by_primary` is always `true`, and
59`meta.served_by_region` is always `local`.
60
61Application SQL cannot use `BEGIN`, `COMMIT`, `ROLLBACK`, `SAVEPOINT`, `ATTACH`,
62`DETACH`, `VACUUM`, `load_extension()`, or a temporary table, index, trigger, or
63view. celld allows the `fts5` and `fts5vocab` virtual tables, and it accepts the
64`PRAGMA` list in the
65[D1 SQL statement reference](https://developers.cloudflare.com/d1/sql-api/sql-statements/).
66
67`celld d1 execute` and `celld d1 migrations apply` run SQL from the command line
68through a live node.
69
70## Differences from Cloudflare
71
72- A binding result can contain at most 100,000 rows or 32 MiB.
73- A SQLite `TEXT` value that is not valid UTF-8 decodes with the replacement
74 character U+FFFD, as workerd decodes it, and the stored bytes do not change.
75 Store arbitrary bytes in a `BLOB`.
76- A database has one writer. Use more databases to increase write capacity.
77- celld has no read replica. `withSession()` always selects the primary, and
78 `getBookmark()` returns `celld:primary`.
79- A read-only statement reports `last_row_id` and `changes` as `0`. Cloudflare
80 reports the values that the previous write left on the connection.
81- `prepare()` accepts exactly one statement.
82- `exec()` counts each statement. Cloudflare counts each input line.
83- `dump()` throws. The database is a SQLite file in the operator's own bucket.
84- Application SQL cannot use `BEGIN`, `SAVEPOINT`, `ATTACH`, `VACUUM`,
85 `load_extension()`, or a temporary table.
86- celld has no D1 REST API and no Time Travel. Use `celld d1` for SQL and for
87 migrations.
88
89The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists the
90runtime APIs and the unsupported services.