36 lines · 1.1 KB
import { env } from "cloudflare:workers";
import { all, first } from "./db";

type D1Value = string | number | null;

function session(): D1DatabaseSession | null {
  const db = (env as { DB?: D1Database }).DB;
  return typeof db?.withSession === "function" ? db.withSession("first-unconstrained") : null;
}

/**
 * Directory reads that tolerate a few seconds of replica lag (listings, search, name lookups) go through the
 * D1 Sessions API so read replication can serve them. Anything that gates access or follows a write uses
 * `first`/`all` from ./db, which always hit the primary. Falls back to the primary where sessions are unavailable.
 */
export const replica = {
  async first<T>(sql: string, ...params: D1Value[]): Promise<T | null> {
    const live = session();
    if (!live) return first<T>(sql, ...params);
    return live
      .prepare(sql)
      .bind(...params)
      .first<T>();
  },
  async all<T>(sql: string, ...params: D1Value[]): Promise<T[]> {
    const live = session();
    if (!live) return all<T>(sql, ...params);
    return (
      await live
        .prepare(sql)
        .bind(...params)
        .all<T>()
    ).results;
  },
};