import { all } from "../db"; import { repoDb, type ScopedDb } from "../scoped-db"; import { forgetLocator, locate, locatedScopes, registerLocator } from "../scoped-db/locators"; import type { SiteRow } from "./config"; /** * Where a site's rows live. A repository's site sits in that repository's RepoState; a repo-less site (an upload * scratch site, claimed or not) is its own scope, `site:`, in a RepoState of its own. */ export function siteScope(site: Pick): string { return site.repo_id ?? `site:${site.id}`; } export function siteDb(site: Pick): ScopedDb { return repoDb(siteScope(site)); } /** The store holding `siteId`, from its `site` locator; null when the site is unknown. */ export async function siteDbById(siteId: string): Promise { const scope = await locate("site", siteId); return scope ? repoDb(scope) : null; } export async function loadSiteRow(siteId: string): Promise { const db = await siteDbById(siteId); return db ? db.first("SELECT * FROM sites WHERE id = ?", siteId) : null; } /** The store holding a deployment, from its `site_deployment` locator. */ export async function deploymentDb(deploymentId: string): Promise { const scope = await locate("site_deployment", deploymentId); return scope ? repoDb(scope) : null; } export function hostKey(host: string): string { return host.toLowerCase(); } /** Registers the site so it can be found by id and by its production label. */ export async function registerSite(site: Pick): Promise { await registerLocator("site", site.id, siteScope(site)); await claimHost(site.label, site.id); } /** Hosts (labels and custom domains) map to the site id that holds them. */ export async function claimHost(host: string, siteId: string): Promise { await registerLocator("site_host", hostKey(host), siteId); } /** Releases a host only while `siteId` still holds it, so a newer holder keeps its claim. */ export async function releaseHost(host: string, siteId: string): Promise { if ((await locate("site_host", hostKey(host))) === siteId) await forgetLocator("site_host", hostKey(host)); } export async function hostHolder(host: string): Promise { return locate("site_host", hostKey(host)); } /** Every scope (repository or `site:`) that holds a site, for platform-wide sweeps. */ export async function siteScopes(): Promise { return locatedScopes("site"); } /** Every site row, walking the site scopes. Data is small; bounded by `limit` when given. */ export async function allSites(where = "1 = 1", ...params: Array): Promise { const out: SiteRow[] = []; for (const scope of await siteScopes()) { out.push(...(await repoDb(scope).all(`SELECT * FROM sites WHERE ${where}`, ...params))); } return out; } /** * Scopes that may hold sites (or site usage) of `ownerId`: site scopes of repositories it owns plus every * repo-less site scope (claimed upload sites are few). Callers still filter by owner or account id. */ export async function ownerSiteScopes(ownerId: string): Promise { const repos = new Set( (await all<{ id: string }>("SELECT id FROM repos WHERE owner_id = ?", ownerId)).map((repo) => repo.id), ); return (await siteScopes()).filter((scope) => scope.startsWith("site:") || repos.has(scope)); } /** Sites billed to `ownerId`: those in repositories it owns plus repo-less sites it claimed. */ export async function sitesOwnedBy(ownerId: string): Promise { const out: SiteRow[] = []; for (const scope of await ownerSiteScopes(ownerId)) { out.push(...(await repoDb(scope).all("SELECT * FROM sites WHERE owner_id = ?", ownerId))); } return out; } /** Drops every locator of a deleted site (its scope rows go with the repository or are deleted by the caller). */ export async function forgetSite(site: Pick): Promise { await releaseHost(site.label, site.id); if (site.custom_domain) await releaseHost(site.custom_domain, site.id); await forgetLocator("site", site.id); }