Cloudflare D1
Cloudflare D1-backed keystore adapter.
Overview
- Keystore backed by Cloudflare D1 using the Worker runtime API (
prepare().bind().run()/first()/all()
). - On first use, it creates the required table and indexes. Uses
exec
when available to batch DDL. metadata
is stored as JSON text.
Usage
import { D1KeyStore, usefulkey } from "betterkey";
export default {
async fetch(req: Request, env: { DB: D1Database }) {
const keyStore = new D1KeyStore(env.DB, { tableName: "usefulkey_keys" });
const uk = usefulkey({ adapters: { keyStore } });
// ...
return new Response("OK");
}
};
Client compatibility
- Object exposing
prepare(sql)
returning{ bind(...).run(), first?(), all?() }
. - Optional
exec(sql)
for batch DDL.
Options
Option | Type | Default | Description |
---|---|---|---|
tableName | string | "usefulkey_keys" | Name of the table to store keys. |
Schema used by the adapter (created automatically if missing):
CREATE TABLE IF NOT EXISTS usefulkey_keys (
id TEXT PRIMARY KEY,
user_id TEXT,
prefix TEXT NOT NULL,
key_hash TEXT NOT NULL UNIQUE,
created_at INTEGER NOT NULL,
expires_at INTEGER,
metadata TEXT,
uses_remaining INTEGER,
revoked_at INTEGER
);
CREATE INDEX IF NOT EXISTS idx_usefulkey_keys_user_id ON usefulkey_keys(user_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_usefulkey_keys_key_hash ON usefulkey_keys(key_hash);
Notes
metadata
is stored as JSON string.- Epoch times are stored as milliseconds.