182 lines · 7.3 KB
import { backfillDone, backfillOnce } from "../backfill";
import { all } from "../db";
import { repoDb, type ScopedDb } from "../scoped-db";
import {
  forgetLocator,
  locate,
  locatedByPrefix,
  locatedScopes,
  locatedScopesAmong,
  locatedScopesFrom,
  registerLocator,
} from "../scoped-db/locators";
import type { SiteRow } from "./config";
import { forgetSiteDirectory } from "./directory";

/**
 * 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> {