34 lines · 1.1 KB
import { randomId } from "./crypto";
import { stmt } from "./scoped-db";
import type { ScopedStatement } from "./scoped-db";
import { locate, registerLocator } from "./scoped-db/locators";
import type { LocatorKind } from "./scoped-db/locators";

/** The `activity` row for a repository event, for batching with the repo-scoped write it describes. */
export function repoActivityStatement(
  actorId: string | null,
  repoId: string,
  kind: string,
  data: Record<string, unknown>,
): ScopedStatement {
  return stmt(
    "INSERT INTO activity (id, actor_id, repo_id, kind, data, created_at) VALUES (?, ?, ?, ?, ?, ?)",
    randomId(),
    actorId,
    repoId,
    kind,
    JSON.stringify(data),
    Date.now(),
  );
}

/** The repository whose object holds the issue (or pull request) with this id. */
export async function issueRepoId(issueId: string): Promise<string | null> {
  return locate("issue", issueId);
}

/** Registers several rows of one repository at once. */
export async function registerRepoRows(kind: LocatorKind, ids: string[], repoId: string): Promise<void> {
  await Promise.all(ids.map((id) => registerLocator(kind, id, repoId)));
}