Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1.1 KB
Raw Blame
import { getDbInstance } from "./core";

type DbInstance = ReturnType<typeof getDbInstance>;

const ENCRYPTED_COLUMNS = ["api_key", "access_token", "refresh_token", "id_token"] as const;

const ENCRYPTED_PATTERN = "enc:v1:%";

function buildWhereClause(): string {
  return ENCRYPTED_COLUMNS.map((col) => `${col} LIKE '${ENCRYPTED_PATTERN}'`).join(" OR ");
}

export function countEncryptedCredentials(db: DbInstance = getDbInstance()): number {
  const where = buildWhereClause();
  const row = db
    .prepare(`SELECT COUNT(*) AS cnt FROM provider_connections WHERE ${where}`)
    .get() as { cnt: number } | undefined;
  return row?.cnt ?? 0;
}

export function resetEncryptedColumns(
  { dryRun }: { dryRun: boolean },
  db: DbInstance = getDbInstance()
): { affected: number } {
  const affected = countEncryptedCredentials(db);
  if (dryRun || affected === 0) return { affected };

  const nullCols = ENCRYPTED_COLUMNS.map((col) => `${col} = NULL`).join(", ");
  const where = buildWhereClause();
  db.prepare(`UPDATE provider_connections SET ${nullCols} WHERE ${where}`).run();

  return { affected };
}