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:<id>`, in a RepoState of its own.
*/
export function siteScope(site: Pick<SiteRow, "id" | "repo_id">): string {
return site.repo_id ?? `site:${site.id}`;
}
export function siteDb(site: Pick<SiteRow, "id" | "repo_id">): 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<ScopedDb | null> {
const scope = await locate("site", siteId);
return scope ? repoDb(scope) : null;
}
export async function loadSiteRow(siteId: string): Promise<SiteRow | null> {
const db = await siteDbById(siteId);
return db ? db.first<SiteRow>("SELECT * FROM sites WHERE id = ?", siteId) : null;
}
/** The store holding a deployment, from its `site_deployment` locator. */
export async function deploymentDb(deploymentId: string): Promise<ScopedDb | null> {
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<SiteRow, "id" | "repo_id" | "label">): Promise<void> {