-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell.js
More file actions
348 lines (324 loc) · 13.8 KB
/
Copy pathcell.js
File metadata and controls
348 lines (324 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* js-jsonb-demo — showing off SQLite JSONB in tissue c3.
*
* c3 is backed by libSQL (SQLite), so it has the full JSON1 + JSONB toolkit.
* JSONB (SQLite >= 3.45) stores JSON as a parsed BINARY tree instead of text:
* - smaller on disk (no whitespace, compact varint headers)
* - json_extract / ->> read the binary tree directly (no re-parse per call)
* - jsonb_set / jsonb_insert / jsonb_remove mutate a nested field in place,
* returning a new blob — no read-modify-write of the whole doc in JS
* - -> returns JSONB (chainable); ->> returns the SQL scalar
*
* This cell stores the SAME product catalog two ways in one table — a TEXT
* `doc_text` column and a BINARY `doc` JSONB column — then runs live queries
* that highlight where JSONB pulls ahead of hand-rolled JSON-in-TEXT.
*
* Routes:
* GET / — HTML report (runs every demo below and renders it)
* GET /api/report — the same results as JSON
* GET /api/seed — (re)seed the catalog
*/
const SCHEMA = `
CREATE TABLE IF NOT EXISTS products (
id INTEGER PRIMARY KEY,
-- exact same document, stored two ways:
doc_text TEXT NOT NULL, -- plain JSON text (what you'd do without JSONB)
doc BLOB NOT NULL -- JSONB binary tree
);
`;
// A generated expression index over a nested JSONB field. Filtering products
// by category no longer scans+reparses every row — SQLite reads the binary
// tree and probes this B-tree.
const INDEX = `
CREATE INDEX IF NOT EXISTS idx_products_category
ON products ( doc ->> '$.category' );
`;
// Rich, nested catalog documents — arrays, nested objects, mixed types.
const CATALOG = [
{
id: 1, name: "Aurora Keyboard", category: "peripherals", price: 129.0,
inStock: true, tags: ["mechanical", "wireless", "rgb"],
specs: { switch: "brown", layout: "75%", battery_mah: 4000, connectivity: ["bt", "usb-c", "2.4ghz"] },
reviews: [{ user: "kim", stars: 5 }, { user: "lee", stars: 4 }, { user: "ada", stars: 5 }],
},
{
id: 2, name: "Nimbus Mouse", category: "peripherals", price: 59.0,
inStock: false, tags: ["wireless", "ergonomic"],
specs: { dpi: 26000, buttons: 8, battery_mah: 500, connectivity: ["bt", "2.4ghz"] },
reviews: [{ user: "ravi", stars: 4 }, { user: "sol", stars: 3 }],
},
{
id: 3, name: "Halo 27 Monitor", category: "displays", price: 449.0,
inStock: true, tags: ["4k", "hdr", "usb-c"],
specs: { panel: "ips", hz: 144, nits: 400, ports: { hdmi: 2, dp: 1, usbc: 1 } },
reviews: [{ user: "min", stars: 5 }, { user: "jo", stars: 5 }],
},
{
id: 4, name: "Slate Desk Mat", category: "accessories", price: 24.5,
inStock: true, tags: ["felt", "large"],
specs: { material: "wool-felt", size: "900x400mm" },
reviews: [{ user: "eve", stars: 4 }],
},
{
id: 5, name: "Beacon Webcam", category: "peripherals", price: 89.0,
inStock: true, tags: ["1080p", "usb-c", "privacy-shutter"],
specs: { resolution: "1080p", fps: 60, mic: true, connectivity: ["usb-c"] },
reviews: [{ user: "tom", stars: 3 }, { user: "ana", stars: 4 }, { user: "rex", stars: 2 }],
},
];
async function seed(env) {
await env.DB.exec(SCHEMA);
await env.DB.exec("DELETE FROM products;");
// Insert once per row: the TEXT column gets JSON.stringify (with indenting,
// like a human-authored blob), the JSONB column gets jsonb(?) of the compact
// form. SQLite parses the text into its binary tree at write time.
for (const p of CATALOG) {
const pretty = JSON.stringify(p, null, 2); // "wasteful" but realistic text JSON
const compact = JSON.stringify(p);
await env.DB
.prepare("INSERT INTO products (id, doc_text, doc) VALUES (?, ?, jsonb(?))")
.bind(p.id, pretty, compact)
.run();
}
await env.DB.exec(INDEX);
}
// Each demo returns { title, blurb, sql, rows }. The SQL is shown to the reader
// so the JSONB features are visible, not hidden behind the app.
async function runDemos(env) {
const demos = [];
const q = async (title, blurb, sql) => {
const { results } = await env.DB.prepare(sql).all();
demos.push({ title, blurb, sql: sql.trim(), rows: results });
};
// 1. Storage footprint — binary JSONB vs the text it came from.
await q(
"1 · Storage footprint",
"The identical document as TEXT vs JSONB. JSONB drops whitespace and " +
"stores a compact binary tree, so it is smaller on disk — and that is the " +
"same bytes SQLite reads at query time.",
`SELECT
json_extract(doc, '$.name') AS product,
length(doc_text) AS text_bytes,
length(doc) AS jsonb_bytes,
length(doc_text) - length(doc) AS saved_bytes,
printf('%.0f%%',
100.0 * (length(doc_text) - length(doc)) / length(doc_text)) AS saved
FROM products
ORDER BY id`
);
// 2. Extraction without re-parsing — ->> reads the binary tree directly.
await q(
"2 · Nested extraction (no re-parse)",
"With TEXT JSON every json_extract() re-parses the whole string. Over JSONB, " +
"-> / ->> walk the binary tree directly. Here we pull scalars from three " +
"nesting levels in one pass.",
`SELECT
doc ->> '$.name' AS product,
doc ->> '$.specs.battery_mah' AS battery,
doc ->> '$.specs.connectivity[0]' AS primary_link,
doc ->> '$.reviews[0].user' AS first_reviewer
FROM products
WHERE doc ->> '$.specs.battery_mah' IS NOT NULL
ORDER BY battery DESC`
);
// 3. Indexed filter on a nested field — uses idx_products_category.
await q(
"3 · Indexed filter on a nested key",
"The expression index over doc->>'$.category' means this filter is a B-tree " +
"probe, not a full scan that reparses every document.",
`SELECT doc ->> '$.name' AS product, doc ->> '$.price' AS price
FROM products
WHERE doc ->> '$.category' = 'peripherals'
ORDER BY price DESC`
);
// 3b. Prove the index is actually used.
await q(
"3b · Query plan (index in use)",
"EXPLAIN QUERY PLAN confirms SQLite uses idx_products_category rather than " +
"scanning + reparsing every row.",
`EXPLAIN QUERY PLAN
SELECT doc ->> '$.name' FROM products
WHERE doc ->> '$.category' = 'peripherals'`
);
// 4. Unnest arrays with json_each over a JSONB path — tag popularity.
await q(
"4 · Unnest arrays (json_each over JSONB)",
"json_each explodes the tags array of every product into rows so we can " +
"GROUP BY across the whole catalog — a join/aggregate you cannot express " +
"against opaque JSON text.",
`SELECT tag.value AS tag, COUNT(*) AS products
FROM products, json_each(products.doc, '$.tags') AS tag
GROUP BY tag.value
ORDER BY products DESC, tag`
);
// 5. Aggregate over nested arrays — average star rating per product.
await q(
"5 · Aggregate over nested objects",
"json_each walks each product's reviews array; avg() rolls the nested star " +
"ratings up per product. The nesting stays queryable.",
`SELECT
p.doc ->> '$.name' AS product,
COUNT(*) AS reviews,
printf('%.2f', avg(r.value ->> '$.stars')) AS avg_stars
FROM products p, json_each(p.doc, '$.reviews') AS r
GROUP BY p.id
ORDER BY avg_stars DESC`
);
// 6. In-place partial mutation — no read-modify-write in JS.
// Restock #2, bump its price 10%, and append a tag, all in one UPDATE.
await q(
"6 · In-place partial mutation",
"jsonb_set / jsonb_insert edit nested fields inside the stored blob in a " +
"single UPDATE — no SELECT, JSON.parse, mutate, JSON.stringify, UPDATE " +
"round-trip in application code.",
`UPDATE products
SET doc = jsonb_insert(
jsonb_set(
jsonb_set(doc, '$.inStock', json('true')),
'$.price', round((doc ->> '$.price') * 1.10, 2)),
'$.tags[#]', 'restocked')
WHERE id = 2
RETURNING
doc ->> '$.name' AS product,
doc ->> '$.inStock' AS in_stock,
doc ->> '$.price' AS new_price,
json(doc -> '$.tags') AS tags`
);
// 7. Reshape a JSONB doc into a new JSON object — projection.
await q(
"7 · Reshape into a new object",
"json_object + JSONB accessors project a trimmed view — pick nested fields, " +
"compute a derived one, and hand back clean JSON to the client.",
`SELECT json_object(
'sku', 'P' || printf('%03d', id),
'title', doc ->> '$.name',
'link', doc ->> '$.specs.connectivity[0]',
'cheap', json(iif(doc ->> '$.price' < 100, 'true', 'false'))
) AS card
FROM products
ORDER BY id`
);
return demos;
}
export default {
async fetch(request, env) {
const url = new URL(request.url);
const path = url.pathname;
// Seed on demand (and lazily on first hit if the table is empty).
if (path === "/api/seed") {
await seed(env);
return Response.json({ ok: true, seeded: CATALOG.length });
}
// Reseed on every render so the report is deterministic and idempotent —
// demo 6 performs a persistent mutation, so a fresh catalog each request
// keeps the shown numbers stable across reloads.
await seed(env);
const demos = await runDemos(env);
if (path === "/api/report") {
return Response.json({ demos }, { headers: { "cache-control": "no-store" } });
}
return new Response(renderHtml(demos), {
headers: { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" },
});
},
};
/* ---------- rendering ---------- */
function esc(s) {
return String(s).replace(/[&<>"]/g, c => ({ "&": "&", "<": "<", ">": ">", '"': """ }[c]));
}
function renderTable(rows) {
if (!rows.length) return `<p class="empty">no rows</p>`;
const cols = Object.keys(rows[0]);
const head = cols.map(c => `<th>${esc(c)}</th>`).join("");
const body = rows.map(r =>
`<tr>${cols.map(c => {
let v = r[c];
if (v && typeof v === "object") v = JSON.stringify(v);
return `<td>${esc(v ?? "")}</td>`;
}).join("")}</tr>`
).join("");
return `<table><thead><tr>${head}</tr></thead><tbody>${body}</tbody></table>`;
}
function renderHtml(demos) {
const sections = demos.map(d => `
<section class="demo">
<h2>${esc(d.title)}</h2>
<p class="blurb">${esc(d.blurb)}</p>
<pre class="sql">${esc(d.sql)}</pre>
<div class="scroll">${renderTable(d.rows)}</div>
</section>`).join("");
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>JSONB in tissue c3</title>
<style>
:root{
--bg:#0f1117; --panel:#171a22; --line:#262b36; --ink:#e7ebf2;
--muted:#9aa4b6; --accent:#7cd1f7; --green:#addcb9; --ring:#00b57e;
--code:#0b0d12;
}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
body{font-family:system-ui,-apple-system,sans-serif;background:var(--bg);
color:var(--ink);line-height:1.5;padding:2.5rem 1.25rem 4rem}
.wrap{max-width:920px;margin:0 auto}
header{margin-bottom:2.5rem}
.mark{display:flex;gap:.55rem;align-items:center;margin-bottom:1.1rem}
.mark svg{width:34px;height:34px}
.mark b{font-size:1.05rem;letter-spacing:.02em}
h1{font-size:1.9rem;line-height:1.15;margin-bottom:.6rem}
h1 em{font-style:normal;color:var(--accent)}
.lede{color:var(--muted);max-width:70ch}
.lede code{background:var(--code);padding:.1em .4em;border-radius:4px;
font-size:.85em;color:var(--green)}
.demo{background:var(--panel);border:1px solid var(--line);border-radius:12px;
padding:1.4rem 1.4rem 1.1rem;margin-top:1.5rem}
h2{font-size:1.15rem;margin-bottom:.5rem;color:var(--green)}
.blurb{color:var(--muted);margin-bottom:1rem;max-width:74ch;font-size:.95rem}
pre.sql{background:var(--code);border:1px solid var(--line);border-radius:8px;
padding:.9rem 1rem;overflow-x:auto;font:0.82rem/1.5 ui-monospace,Menlo,monospace;
color:#cbd5e6;margin-bottom:1rem;white-space:pre}
.scroll{overflow-x:auto}
table{border-collapse:collapse;width:100%;font-size:.86rem}
th,td{text-align:left;padding:.5rem .7rem;border-bottom:1px solid var(--line);
white-space:nowrap}
th{color:var(--accent);font-weight:600;font-size:.78rem;text-transform:uppercase;
letter-spacing:.04em}
td{color:#dbe2ee;font-variant-numeric:tabular-nums}
tbody tr:last-child td{border-bottom:none}
.empty{color:var(--muted);font-style:italic}
footer{margin-top:2.5rem;color:var(--muted);font-size:.85rem;text-align:center}
footer code{background:var(--code);padding:.1em .4em;border-radius:4px;color:var(--green)}
a{color:var(--accent)}
</style>
</head>
<body>
<div class="wrap">
<header>
<div class="mark">
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
<circle cx="22" cy="22" r="22" fill="#7cd1f7"/>
<circle cx="78" cy="22" r="22" fill="#addcb9"/>
<circle cx="22" cy="78" r="22" fill="#addcb9"/>
<circle cx="78" cy="78" r="20" fill="none" stroke="#00b57e" stroke-width="4"/>
</svg>
<b>tissue · c3</b>
</div>
<h1>The full power of <em>JSONB</em></h1>
<p class="lede">c3 is a SQL API backed by SQLite (libSQL), so it ships the whole
JSON1 + <code>JSONB</code> toolkit. Every product below is stored twice in one
table — once as plain JSON <code>TEXT</code>, once as a binary <code>JSONB</code>
blob — and every query here runs live against c3. JSONB keeps documents smaller,
lets <code>->></code> read nested fields without re-parsing, indexes nested
keys, and mutates deep fields in place with <code>jsonb_set</code>.</p>
</header>
${sections}
<footer>
Live on <a href="/api/report">/api/report</a> (JSON) ·
reseed at <code>/api/seed</code> · powered by tissue c3
</footer>
</div>
</body>
</html>`;
}