···142142 constructor(state, env) {
143143 this.state = state
144144 this.sql = state.storage.sql
145145+ this.env = env
146146+147147+ // Initialize schema
148148+ this.sql.exec(`
149149+ CREATE TABLE IF NOT EXISTS blocks (
150150+ cid TEXT PRIMARY KEY,
151151+ data BLOB NOT NULL
152152+ );
153153+154154+ CREATE TABLE IF NOT EXISTS records (
155155+ uri TEXT PRIMARY KEY,
156156+ cid TEXT NOT NULL,
157157+ collection TEXT NOT NULL,
158158+ rkey TEXT NOT NULL,
159159+ value BLOB NOT NULL
160160+ );
161161+162162+ CREATE TABLE IF NOT EXISTS commits (
163163+ seq INTEGER PRIMARY KEY AUTOINCREMENT,
164164+ cid TEXT NOT NULL,
165165+ rev TEXT NOT NULL,
166166+ prev TEXT
167167+ );
168168+169169+ CREATE TABLE IF NOT EXISTS seq_events (
170170+ seq INTEGER PRIMARY KEY AUTOINCREMENT,
171171+ did TEXT NOT NULL,
172172+ commit_cid TEXT NOT NULL,
173173+ evt BLOB NOT NULL
174174+ );
175175+176176+ CREATE INDEX IF NOT EXISTS idx_records_collection ON records(collection, rkey);
177177+ `)
145178 }
146179147180 async fetch(request) {
···160193 if (url.pathname === '/test/tid') {
161194 const tids = [createTid(), createTid(), createTid()]
162195 return Response.json({ tids })
196196+ }
197197+ if (url.pathname === '/test/schema') {
198198+ const tables = this.sql.exec(`
199199+ SELECT name FROM sqlite_master WHERE type='table' ORDER BY name
200200+ `).toArray()
201201+ return Response.json({ tables: tables.map(t => t.name) })
163202 }
164203 return new Response('pds running', { status: 200 })
165204 }