/** * Platform Portability Types * * Abstracts Cloudflare D1/KV behind platform-agnostic interfaces. * Same API whether running on Cloudflare or local SQLite. * * Philosophy: The infrastructure disappears; only the work remains. */ export type PlatformMode = 'cloudflare' | 'local'; export interface PlatformConfig { mode: PlatformMode; database?: { path?: string; }; cache?: { path?: string; ttl?: number; }; } /** * Prepared statement interface matching D1's API. * Enables method chaining: db.prepare(sql).bind(...).all() */ export interface PreparedStatement { bind(...values: unknown[]): PreparedStatement; all(): Promise>; first(columnName?: string): Promise; run(): Promise; } /** * Query result containing rows and metadata. */ export interface QueryResult { results: T[]; success: boolean; meta: { duration?: number; changes?: number; last_row_id?: number; }; } /** * Result from mutation operations (INSERT, UPDATE, DELETE). */ export interface RunResult { success: boolean; meta: { changes: number; last_row_id: number; duration?: number; }; } /** * Database interface matching D1's API. * Implementations: Cloudflare D1, local SQLite (better-sqlite3) */ export interface Database { prepare(query: string): PreparedStatement; batch(statements: PreparedStatement[]): Promise[]>; exec(query: string): Promise; dump?(): Promise; } export interface CacheGetOptions { type?: 'text' | 'json' | 'arrayBuffer' | 'stream'; cacheTtl?: number; } export interface CachePutOptions { expiration?: number; expirationTtl?: number; metadata?: Record; } export interface CacheListOptions { prefix?: string; limit?: number; cursor?: string; } export interface CacheListResult { keys: Array<{ name: string; expiration?: number; metadata?: Record; }>; list_complete: boolean; cursor?: string; } export interface CacheGetWithMetadataResult { value: T | null; metadata: Record | null; } /** * Cache interface matching Cloudflare KV's API. * Implementations: Cloudflare KV, local file-based cache */ export interface Cache { get(key: string, options?: CacheGetOptions): Promise; get(key: string, options: CacheGetOptions & { type: 'json'; }): Promise; getWithMetadata(key: string, options?: CacheGetOptions): Promise>; put(key: string, value: string | ArrayBuffer | ReadableStream, options?: CachePutOptions): Promise; delete(key: string): Promise; list(options?: CacheListOptions): Promise; } /** * Platform environment bindings. * Available in SvelteKit via `platform.env` */ export interface PlatformEnv { DB: Database; CACHE: Cache; SESSIONS?: Cache; [key: string]: unknown; } /** * Platform object matching SvelteKit's platform type. */ export interface Platform { env: PlatformEnv; }