22 lines · 967 B
import { env } from "cloudflare:workers";
import { scopedDb, type ScopedDb, type ScopedSqlHost } from "./types";

export { stmt, type ScopedDb, type ScopedStatement, type ScopedValue } from "./types";

/**
 * The repository's own database, inside its RepoState object. Same API as `~/server/db` (first / all / run /
 * batch); tables are listed in scripts/gen-scoped-schema.ts. Directory tables (accounts, users, repos) are not
 * here: look names up in D1 separately and join in code.
 */
export function repoDb(repoId: string): ScopedDb {
  return scopedDb(repoId, () => env.REPO_STATE.get(env.REPO_STATE.idFromName(repoId)) as unknown as ScopedSqlHost);
}

/** The account's own database, inside its AccountState object (sessions, notifications, billing, ...). */
export function accountDb(accountId: string): ScopedDb {
  return scopedDb(
    accountId,
    () => env.ACCOUNT_STATE.get(env.ACCOUNT_STATE.idFromName(accountId)) as unknown as ScopedSqlHost,
  );
}