SPB Git

Repository Public

seed.ts 6.2 KB 103 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Sheet8 * File             : scripts/seed.ts9 * Module           : scripts10 * Repository       : https://git.spboucher.ai/sheet11 * Project Website  : https://sheet.spboucher.ai12 * Created          : 2026-11-1713 * Last Modified    : 2026-11-1714 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import "./_env";44import { sql } from "../src/lib/db";45import { buildTemplate } from "../src/templates";46import { createSheet, publish, getSheetRow } from "../src/server/sheets";47import { qualityReport } from "../src/server/quality";4849/**50 * Idempotent seed: the topic list (French and English names) and the demonstration sheets.51 *   tsx scripts/seed.ts                     → topics + demonstration sheets as drafts52 *   tsx scripts/seed.ts --publish-examples  → also publishes them (they carry the "example, not yet reviewed" banner)53 * Demonstration sheets are built from the tested templates; each exists in French and English, linked together.54 */55const TOPICS: [slug: string, fr: string, en: string, descFr: string, descEn: string, tone: string][] = [56  ["ia", "IA", "AI", "Modèles de langage, architectures et systèmes d’IA.", "Language models, architectures and AI systems.", "violet"],57  ["apprentissage-profond", "Apprentissage profond", "Deep Learning", "Réseaux de neurones, entraînement et inférence.", "Neural networks, training and inference.", "blue"],58  ["econometrie", "Économétrie", "Econometrics", "Séries temporelles, identification et inférence.", "Time series, identification and inference.", "teal"],59  ["finance", "Finance", "Finance", "Marchés, volatilité et évaluation.", "Markets, volatility and valuation.", "green"],60  ["statistique", "Statistique", "Statistics", "Estimation, tests et modèles.", "Estimation, testing and models.", "amber"],61  ["infrastructure", "Infrastructure", "Infrastructure", "Grappes de calcul, réseaux et déploiement.", "Compute clusters, networks and deployment.", "neutral"],62  ["donnees", "Données", "Data", "Pipelines, qualité et stockage des données.", "Data pipelines, quality and storage.", "pink"],63  ["developpement-logiciel", "Développement logiciel", "Software", "Conception et pratique du logiciel.", "Software design and practice.", "blue"],64  ["blockchain", "Blockchain", "Blockchain", "Registres distribués et cryptographie appliquée.", "Distributed ledgers and applied cryptography.", "amber"],65  ["sciences", "Sciences", "Science", "Méthodes et résultats scientifiques.", "Scientific methods and results.", "red"],66];67const EXAMPLES = ["mixture-of-experts", "garch-volatility", "rag-pipeline"];6869async function main() {70  for (const [i, [slug, fr, en, dfr, den, tone]] of TOPICS.entries()) {71    await sql`INSERT INTO topics (slug, name_fr, name_en, description_fr, description_en, tone, position) VALUES (${slug}, ${fr}, ${en}, ${dfr}, ${den}, ${tone}, ${i})72              ON CONFLICT (slug) DO UPDATE SET name_fr = EXCLUDED.name_fr, name_en = EXCLUDED.name_en, description_fr = EXCLUDED.description_fr, description_en = EXCLUDED.description_en`;73  }74  console.log(`  ${TOPICS.length} topics`);75  const publishThem = process.argv.includes("--publish-examples");76  const [admin] = await sql<{ id: string }[]>`SELECT id FROM users ORDER BY created_at LIMIT 1`;77  for (const [n, key] of EXAMPLES.entries()) {78    const marker = `seed:${key}`;79    const existing = await sql<{ sheet_id: string }[]>`SELECT sheet_id FROM sheet_revisions WHERE origin = 'template' AND summary LIKE ${marker + "%"} AND number = 1`;80    const ids: string[] = existing.map((e) => e.sheet_id);81    if (!ids.length) {82      const fr = buildTemplate(key, "fr"), en = buildTemplate(key, "en");83      const q = qualityReport(fr);84      if (!q.publishable) throw new Error(`Template ${key} has quality errors: ${q.items.filter((i) => i.severity === "error").map((i) => i.en).join("; ")}`);85      const a = await createSheet({ doc: fr, origin: "template", userId: admin?.id ?? null, summary: `${marker}:fr` });86      const { localization_id } = await getSheetRow(a.id);87      const b = await createSheet({ doc: en, origin: "template", userId: admin?.id ?? null, summary: `${marker}:en`, localizationId: localization_id });88      ids.push(a.id, b.id);89      console.log(`  example created: ${key} (fr: /s/${a.slug}, en: /s/${b.slug})`);90    }91    if (publishThem) for (const id of ids) {92      const row = await getSheetRow(id).catch(() => null);93      if (!row || row.status !== "draft" || row.first_published_at) continue; // never re-publish something the author unpublished94      if (!admin) throw new Error("Publishing the examples needs the administrator account (run bootstrap first)");95      await publish(id, row.version, admin.id);96      await sql`UPDATE sheets SET featured = true, featured_position = ${n} WHERE id = ${id}`;97      console.log(`  example published: /s/${row.slug}`);98    }99  }100  await sql.end();101}102main().catch((e) => { console.error(e.message); process.exit(1); });103
Files main
Menu

Theme