spb/cancerindex
Public
1import 'server-only';2import { getDb, getSql, closeDb, schema } from '@cancerindex/database';34/** Server-side database handle for server components, route handlers and server actions. */5export const db = () => getDb();6export { getDb, getSql, closeDb, schema };7export type { Database } from '@cancerindex/database';8export { sql, eq, and, or, desc, asc, gte, lte, lt, gt, inArray, isNull, isNotNull, ilike, count, ne } from 'drizzle-orm';9import type { SQL } from 'drizzle-orm';1011// Tables re-exported explicitly (webpack handles `export *` chains, but explicit names keep the web12// bundle's dependency graph obvious and typecheck failures local).13export const {14 cancers,15 cancerAliases,16 cancerHierarchy,17 cancerCodes,18 anatomicalSites,19 cancerAnatomy,20 geographies,21 sources,22 ingestRuns,23 connectorCursors,24 sourceRecords,25 provenance,26 unresolvedLabels,27 changeEvents,28 auditLog,29 genes,30 geneAliases,31 variants,32 variantClinicalSignificance,33 genomicCohorts,34 cancerGeneFrequencies,35 drugs,36 drugAliases,37 drugApprovals,38 clinicalTrials,39 trialConditions,40 trialInterventions,41 trialLocations,42 publications,43 publicationEntityEdges,44 literatureCounts,45 civicEvidenceItems,46 knowledgeEdges,47 epidemiologyObservations,48 survivalObservations,49 metricDefinitions,50 rankingSnapshots,51 rankings,52 entityCounters,53} = schema;5455/**56 * Run a query and swallow "relation does not exist" style errors so pages render an EmptyState57 * instead of crashing when a table has not been created on this environment yet.58 */59export async function safe<T>(fn: () => Promise<T>, fallback: T): Promise<T> {60 try {61 return await fn();62 } catch (err) {63 if (process.env.NODE_ENV !== 'production') console.error('[cancerindex/web] query failed:', (err as Error).message);64 return fallback;65 }66}6768/** Execute a raw SQL query and return plain typed rows (drops the RowList metadata wrapper). */69export async function run<T>(query: SQL): Promise<T[]> {70 const res = await getDb().execute(query);71 return Array.from(res as unknown as Iterable<T>);72}73