d1-sql-tag 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +65 -0
  2. package/package.json +2 -1
package/README.md CHANGED
@@ -10,6 +10,14 @@ database.
10
10
  If you have created a D1 database and configured it with the binding name `DB`,
11
11
  in `wrangler.toml`, you can create a template literal tag with `createD1SqlTag()`.
12
12
 
13
+ We also set up a callback to log stats for each query, like so:
14
+
15
+ ```
16
+ D1 batch: 286ms · 1 queries
17
+ 1: SELECT ?1 AS message
18
+ ↳ 0.3053ms · 0 changed · 0 read · 0 written
19
+ ```
20
+
13
21
  ```ts
14
22
  import { createD1SqlTag, logQueryResults } from "d1-sql-tag";
15
23
 
@@ -40,6 +48,63 @@ export default {
40
48
  };
41
49
  ```
42
50
 
51
+ ## Usage with Hono on Cloudflare Workers
52
+
53
+ If you have created a D1 database and configured it with the binding name `DB`,
54
+ in `wrangler.toml`, you can create a template literal tag with `createD1SqlTag()`.
55
+
56
+ We also set up a callback to log stats for each query, like so:
57
+
58
+ ```
59
+ D1 batch: 286ms · 1 queries
60
+ 1: SELECT ?1 AS message
61
+ ↳ 0.3053ms · 0 changed · 0 read · 0 written
62
+ ```
63
+
64
+ Additionally, we use [`hono/timing`](https://hono.dev/middleware/builtin/timing)
65
+ to send [`Server-Timing`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing) headers for the total response time, how long we wait for each batch, and how
66
+ long each query takes. Open the network tab in your browser's devtools, select
67
+ the request and look at the "Timing" tab.
68
+
69
+ ```ts
70
+ import { createD1SqlTag, logQueryResults } from "d1-sql-tag";
71
+ import { Hono, type Context } from "hono";
72
+ import { endTime, setMetric, startTime, timing } from "hono/timing";
73
+
74
+ type Bindings = {
75
+ DB: D1Database;
76
+ };
77
+
78
+ const app = new Hono<{ Bindings: Bindings }>();
79
+
80
+ function createSqlTag(c: Context<{ Bindings: Bindings }>) {
81
+ return createD1SqlTag(c.env.DB, {
82
+ beforeQuery(batchId, queries) {
83
+ startTime(c, `db-${batchId}`);
84
+ },
85
+ afterQuery(batchId, queries, results, duration) {
86
+ endTime(c, `db-${batchId}`);
87
+ results.forEach((result, i) => {
88
+ setMetric(c, `db-${batchId}-query-${i + 1}`, result.meta.duration);
89
+ });
90
+ logQueryResults(queries, results, duration);
91
+ },
92
+ });
93
+ }
94
+
95
+ app.use("*", timing());
96
+
97
+ app.get("/", async (c) => {
98
+ const sql = createSqlTag(c);
99
+ const result = await sql`SELECT ${"hello world"} AS message`.all<{
100
+ message: string;
101
+ }>();
102
+ return c.text(`Message: ${result.results[0].message}`);
103
+ });
104
+
105
+ export default app;
106
+ ```
107
+
43
108
  ## License
44
109
 
45
110
  [MIT](./LICENSE.txt)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "d1-sql-tag",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "A template literal for working with Cloudflare D1 database",
5
5
  "keywords": [
6
6
  "d1",
@@ -19,6 +19,7 @@
19
19
  },
20
20
  "scripts": {
21
21
  "build": "tsup src/index.ts --format esm --sourcemap --dts",
22
+ "changeset": "changeset",
22
23
  "prettier:check": "prettier --check .",
23
24
  "prettier:fix": "prettier --write .",
24
25
  "release": "npm run build && npm run test && changeset publish",