import { type Database } from "./database.js"; /** * Site SQL definitions in Postgres (migration 0783): whether the owner turned * a Site's Cloudflare database on, its ordered migrations, its named queries, * its usage counters (site_sql_usage, apart from the database row so Site * page calls never wait on a migration's lock) and its call windows. The rows * themselves live in the Site's Durable Object; everything here is what * rebuilds it. */ export type SiteSqlScope = { accountId: string; workspaceId: string; siteId: string; }; export type SiteSqlStoreErrorCode = "not_found" | "conflict" | "rate_limited" | "quota_exceeded"; export declare class SiteSqlStoreError extends Error { readonly code: SiteSqlStoreErrorCode; readonly details: Record; constructor(code: SiteSqlStoreErrorCode, message: string, details?: Record); } export type SiteSqlDatabase = { siteId: string; enabled: boolean; storageBytes: number; /** First day of the month the row counters are for (YYYY-MM-01). */ usageMonth: string; rowsReadThisMonth: number; rowsWrittenThisMonth: number; migrationCount: number; rebuiltAt: Date | null; createdAt: Date; updatedAt: Date; }; export type SiteSqlStoredMigration = { ordinal: number; name: string; sql: string; checksum: string; createdBy: string; createdAt: Date; }; export type SiteSqlStoredQuery = { name: string; sql: string; mode: "read" | "write"; params: string[]; audience: "everyone" | "signed_in" | "managers"; description: string | null; maxRows: number | null; createdBy: string; updatedBy: string; createdAt: Date; updatedAt: Date; }; export declare function getSiteSqlDatabase(db: Database, scope: SiteSqlScope): Promise; /** Turn a Site's database on or off; the definitions and data are kept either way. */ export declare function setSiteSqlEnabled(db: Database, scope: SiteSqlScope, enabled: boolean, actor: string): Promise; /** The ids of a workspace's Sites that ever turned Site SQL on. */ export declare function listSiteSqlSiteIds(db: Database, scope: { accountId: string; workspaceId: string; }): Promise; /** * Run `fn` holding the Site's database row lock, so migrations, rebuilds and * resets of one Site never interleave. The row must exist (Site SQL turned on * at least once). Site page calls never take this lock. */ export declare function withSiteSqlDatabaseLock(db: Database, scope: SiteSqlScope, fn: (tx: Database, database: SiteSqlDatabase) => Promise): Promise; export declare function listSiteSqlMigrations(db: Database, scope: SiteSqlScope): Promise; /** UTF-8 bytes of SQL a Site's migrations hold together. */ export declare function siteSqlMigrationBytes(tx: Database, scope: SiteSqlScope): Promise; /** The error for a migration that would pass a Site's migration SQL cap. */ export declare function siteSqlMigrationQuotaError(): SiteSqlStoreError; /** * Append a migration under the database row lock `tx` holds (from * withSiteSqlDatabaseLock). A name already used is a conflict; SQL past the * Site's migration cap (SITE_SQL_HARD_LIMITS.migrationBytesPerSite) is * quota_exceeded. */ export declare function appendSiteSqlMigration(tx: Database, scope: SiteSqlScope, migration: { name: string; sql: string; checksum: string; }, actor: string): Promise; /** * Forget every migration and named query (a reset that drops the schema), * under the database row lock `tx` holds. Ordinals start again at 1. */ export declare function clearSiteSqlDefinitions(tx: Database, scope: SiteSqlScope, actor: string): Promise; /** * Record that the Durable Object was rebuilt from Postgres, under the row * lock: its size, which is also the new storage high-water mark. */ export declare function markSiteSqlRebuilt(tx: Database, scope: SiteSqlScope, storageBytes: number): Promise; export declare function listSiteSqlQueries(db: Database, scope: SiteSqlScope): Promise; export declare function getSiteSqlQuery(db: Database, scope: SiteSqlScope, name: string): Promise; /** Create or replace a named query. Returns whether it was created. */ export declare function upsertSiteSqlQuery(db: Database, scope: SiteSqlScope, query: { name: string; sql: string; mode: "read" | "write"; params: string[]; audience: SiteSqlStoredQuery["audience"]; description: string | null; maxRows: number | null; }, actor: string): Promise<{ query: SiteSqlStoredQuery; created: boolean; }>; export declare function deleteSiteSqlQuery(db: Database, scope: SiteSqlScope, name: string): Promise; /** * Count one call against fixed one-minute windows, each `[principal key, * limit, message]`; a null limit only counts. Counted in its own transaction * after the request is checked and before the database call, so calls the * monthly caps or the database refuse still count. Throws rate_limited when * a window is over its limit. */ export declare function consumeSiteSqlRate(db: Database, scope: SiteSqlScope, windows: ReadonlyArray): Promise; /** What one Durable Object call did, as it reported it. */ export type SiteSqlUsageDelta = { rowsRead: number; rowsWritten: number; /** The database size after the call. */ databaseBytes: number; }; /** * Record one Durable Object call's usage: store the database size and, for * Site page calls (`countTowardCaps`, the default), add its rows to this * month's counters the monthly caps read, rolling them over when the month * changed. Storage growth is measured past the high-water mark, so calls * completing out of order never count the same bytes twice. `meter` runs in * the same transaction (usage events). Takes only the Site's usage row lock. * Returns the updated database, or null when Site SQL was never turned on. */ export declare function recordSiteSqlUsage(db: Database, scope: SiteSqlScope, delta: SiteSqlUsageDelta, options?: { countTowardCaps?: boolean; meter?: (tx: Database, change: { storageGrowthBytes: number; }) => Promise; }): Promise;