/** * A `@gramio/storage` adapter over a Cloudflare D1 table — the per-user * session blobs (language pick, consent, menu position, payments cache). * Follows gramio's own write-your-adapter recipe (4 methods over JSON). * * Battle-tested in production (xtldrbot) before moving here. * * `flush()` exists because `@gramio/session` calls `storage.set()` without * awaiting it, and a Worker freezes the isolate the instant `fetch()` * returns, killing in-flight writes (consent wouldn't stick). Every write * is tracked; hand `flush()` to `ctx.waitUntil()` — `bot/worker` does this * automatically when you pass it a `flush`. * * Table schema (create it in your migrations): * * CREATE TABLE session ( * key TEXT PRIMARY KEY, * value TEXT NOT NULL, * expires INTEGER -- reserved for future TTL use * ); * * No `@cloudflare/workers-types` dependency: the db is typed structurally * (prepare → bind → first/run), so any D1-shaped binding satisfies it. */ import type { Storage } from "@gramio/storage"; /** The slice of D1's API this adapter touches — satisfied by a real D1Database binding. */ export type D1Like = { prepare(sql: string): { bind(...values: unknown[]): { first(): Promise; run(): Promise<{ meta: { changes?: number; }; }>; }; }; }; /** A {@link Storage} that also lets the Worker await its in-flight writes. */ export interface FlushableStorage extends Storage { /** Resolves once every write issued so far has settled. Never rejects. */ flush(): Promise; } export type D1StorageOptions = { db: D1Like; /** Table name (default `"session"`). Identifier-validated — SQL can't be injected through it. */ table?: string; }; export declare function d1Storage(opts: D1StorageOptions): FlushableStorage; //# sourceMappingURL=storage-d1.d.ts.map