SPB Git

spb/cancerindex

Public
2.4 KB · 50 lines typescript
1import { fileURLToPath } from 'node:url';2import path from 'node:path';3import { eq, sql } from 'drizzle-orm';4import { getDb, closeDb } from './client.js';5import { geographies, metricDefinitions } from './schema/index.js';6import { mintId } from './ids.js';7import { METRIC_CATALOG } from './seed-data/metrics.js';8import { GEOGRAPHY_SEED } from './seed-data/geographies.js';910/**11 * Seeds only system data (CLAUDE.md §358): metric definitions and canonical geographies.12 * Scientific data is never seeded — it comes from connectors. Sources are synced from connector13 * manifests by `pnpm cix sources:sync` (lives in the root CLI to avoid a package cycle).14 */15export async function seed(): Promise<void> {16  const db = getDb({ max: 2 });17  for (const m of METRIC_CATALOG) {18    const [existing] = await db.select({ id: metricDefinitions.id }).from(metricDefinitions).where(eq(metricDefinitions.slug, m.slug)).limit(1);19    const values = { ...m, updatedAt: new Date() };20    if (existing) await db.update(metricDefinitions).set(values).where(eq(metricDefinitions.id, existing.id));21    else await db.insert(metricDefinitions).values({ id: await mintId(db, 'METRIC'), ...m });22  }23  const bySlug = new Map<string, string>();24  for (const g of GEOGRAPHY_SEED) {25    const [existing] = await db.select({ id: geographies.id }).from(geographies).where(eq(geographies.slug, g.slug)).limit(1);26    const parentId = g.parentSlug ? bySlug.get(g.parentSlug) ?? null : null;27    if (existing) {28      await db.update(geographies).set({ name: g.name, kind: g.kind, iso2: g.iso2 ?? null, iso3: g.iso3 ?? null, parentId, whoRegion: g.whoRegion ?? null }).where(eq(geographies.id, existing.id));29      bySlug.set(g.slug, existing.id);30    } else {31      const id = await mintId(db, 'GEO');32      await db.insert(geographies).values({ id, slug: g.slug, name: g.name, kind: g.kind, iso2: g.iso2 ?? null, iso3: g.iso3 ?? null, parentId, whoRegion: g.whoRegion ?? null });33      bySlug.set(g.slug, id);34    }35  }36  const [{ n }] = (await db.execute<{ n: string }>(sql`SELECT count(*)::text AS n FROM metric_definitions`)) as unknown as [{ n: string }];37  console.log(`[seed] metrics=${n} geographies=${GEOGRAPHY_SEED.length}`);38}3940const isMain = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);41if (isMain) {42  seed()43    .then(() => closeDb())44    .catch(async (e) => {45      console.error('[seed] failed', e);46      await closeDb();47      process.exit(1);48    });49}50
Files · 2663b65737c5409bbe90300247714e1d2633efef
Menu

Theme