Skip to content

Commit 5aa097a

Browse files
committed
Adding in more information to be complete.
1 parent f5d8282 commit 5aa097a

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

src/content/docs/d1/sql-api/sql-statements.mdx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ D1 supports a subset of SQLite extensions for added functionality, including:
1818
- Default SQLite extensions.
1919
- [FTS5 module](https://www.sqlite.org/fts5.html) for full-text search.
2020

21-
## PRAGMA statements
21+
## Compatible PRAGMA statements
2222

23-
D1 supports [SQLite PRAGMA](https://www.sqlite.org/pragma.html) statements. The PRAGMA statement is an SQL extension for SQLite. PRAGMA commands can be used to:
23+
D1 supports some [SQLite PRAGMA](https://www.sqlite.org/pragma.html) statements. The PRAGMA statement is an SQL extension for SQLite. PRAGMA commands can be used to:
2424

2525
- Modify the behavior of certain SQLite operations.
2626
- Query the SQLite library for internal data about schemas or tables (but note that PRAGMA statements cannot query the contents of a table).
@@ -55,6 +55,22 @@ SELECT name, sql FROM sqlite_master
5555
}
5656
```
5757

58+
## Search with LIKE
59+
60+
You can perform a search using SQL's `LIKE` operator:
61+
62+
```js
63+
const { results } = await env.DB.prepare(
64+
"SELECT * FROM Customers WHERE CompanyName LIKE ?",
65+
)
66+
.bind("%eve%")
67+
.all();
68+
console.log("results: ", results);
69+
```
70+
```js output
71+
results: [...]
72+
```
73+
5874
## Related resources
5975

6076
- Learn [how to create indexes](/d1/build-with-d1/use-indexes/#list-indexes) in D1.

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,38 @@ import { Type, MetaInfo, Details } from "~/components";
1111

1212
You can execute queries on your D1 database through SQL statements.
1313

14+
### TypeScript support
15+
16+
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.
17+
18+
When using the [query statement methods](#query-statement-methods) `stmt.all()`, `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.
19+
20+
For example, providing an `OrderRow` type as a type parameter to `stmt.all()` will return a typed `Array<OrderRow>` object instead of the default `Record<string, unknown>` type:
21+
22+
```ts
23+
// Row definition
24+
type OrderRow = {
25+
Id: string;
26+
CustomerName: string;
27+
OrderDate: number;
28+
};
29+
30+
// Elsewhere in your application
31+
const result = await env.MY_DB.prepare(
32+
"SELECT Id, CustomerName, OrderDate FROM [Order] ORDER BY ShippedDate DESC LIMIT 100",
33+
).all<OrderRow>();
34+
```
35+
1436
## Methods
1537

1638
### `db.prepare()`
1739

18-
D1 API supports both prepared and static statements. The recommended approach is to use prepared statements (which are precompiled objects used by the database) to run the SQL. Prepared statements lead to faster overall execution and prevent SQL injection attacks.
40+
D1 API supports both prepared and static statements.
41+
42+
- Prepared statements are SQL statements where the variables are dynamically determined. When writing a prepared statement, you insert variables into placeholders within the statement string.
43+
- Static statements are SQL statements where the variables have been hard coded. When writing a static statement, you manually type the variable within the statement string.
44+
45+
The recommended approach is to use prepared statements (which are precompiled objects used by the database) to run the SQL. Prepared statements lead to faster overall execution and prevent SQL injection attacks.
1946

2047
Example of a prepared statement:
2148

src/content/docs/d1/worker-api/return-object.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ sidebar:
55
order: 3
66
---
77

8-
The methods `stmt.run()` and `db.batch()` return a typed `D1Result` object that contains the results (if applicable), the success status, and a meta object with the internal duration of the operation in milliseconds.
8+
The methods `stmt.run()` and `db.batch()` return a typed `D1Result` object for each query statement. This object contains:
9+
10+
- The results (if applicable) as an array
11+
- The success status
12+
- A meta object with the internal duration of the operation in milliseconds
913

1014
```js
1115
{

0 commit comments

Comments
 (0)