Read Replica Health Check

Is there a way to health check read replica or not needed because it switches to primary?

also is there a better way to check if connection is working properly? at the moment I query a table.

import { db } from "@/server/db";

interface DatabaseError {
  code?: string;
  message?: string;
}

// Type guard to check if the error is a DatabaseError
function isDatabaseError(error: unknown): error is DatabaseError {
  return (
    typeof error === "object" &&
    error !== null &&
    "code" in error &&
    typeof (error as { code?: unknown }).code === "string"
  );
}

export async function GET(): Promise<Response> {
  try {
    await db.$primary.query.users.findFirst();
  } catch (error) {
    console.error(error);

    if (isDatabaseError(error)) {
      return new Response(error.code || "Internal Server Error", {
        status: 500,
      });
    }

    return new Response("Internal Server Error", { status: 500 });
  }

  return new Response("Ok", { status: 200 });
}
Was this page helpful?