Files
spsays-training/scripts/seed-va.ts
T
2026-05-03 21:02:41 +05:30

140 lines
3.7 KiB
TypeScript

/**
* seed-va.ts — Push Vaiśiṣṭyāṣṭakam (VA) into MongoDB.
*
* Collections written:
* - book_meta → 1 doc { book: "va", data: <intro.json> }
* - prose_chapters → 8 chapter docs
*
* JSON sources:
* - lib/jsons/books/va/intro.json
* - lib/jsons/books/va/chapters/01.json through 08.json
*
* Usage:
* npx tsx scripts/seed-va.ts
*/
import dotenv from "dotenv";
import fs from "fs";
import path from "path";
import { MongoClient } from "mongodb";
// Load env
dotenv.config({ path: ".env.local" });
dotenv.config({ path: ".env" });
const DATABASE_URL = process.env.DATABASE_URL;
if (!DATABASE_URL) {
console.error("❌ DATABASE_URL is not set in .env");
process.exit(1);
}
const BASE_DIR = path.resolve(__dirname, "..");
const BOOK_DIR = path.join(BASE_DIR, "lib", "jsons", "books", "va");
const BOOK = "va";
const NO_VOLUME = 0;
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
type IntroJson = {
book: string;
title: string;
author: string;
year: number | null;
description: string;
totalChapters: number;
chapters: { number: number; title: string }[];
};
type ChapterJson = {
book: string;
chapter: number;
title: string;
type: string;
paragraphs: string[];
};
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function readJson<T>(filePath: string): T {
return JSON.parse(fs.readFileSync(filePath, "utf-8"));
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
async function main() {
console.log("🌱 Seeding Vaiśiṣṭyāṣṭakam (VA)...\n");
const client = new MongoClient(DATABASE_URL!);
await client.connect();
console.log("📡 Connected to MongoDB\n");
const db = client.db();
const start = Date.now();
// ---- Read intro ----
const introPath = path.join(BOOK_DIR, "intro.json");
if (!fs.existsSync(introPath)) {
console.error(`❌ Missing: ${introPath}`);
process.exit(1);
}
const intro = readJson<IntroJson>(introPath);
// ---- Book meta ----
const metaCol = db.collection("book_meta");
await metaCol.deleteMany({ book: BOOK });
await metaCol.insertOne({ book: BOOK, data: intro });
console.log(`📖 Inserted book_meta for "${intro.title}" (${BOOK})`);
// ---- Chapters ----
const proseCol = db.collection("prose_chapters");
await proseCol.deleteMany({ book: BOOK });
const chaptersDir = path.join(BOOK_DIR, "chapters");
const chapterFiles = fs
.readdirSync(chaptersDir)
.filter((f) => f.endsWith(".json"))
.sort();
let inserted = 0;
for (const file of chapterFiles) {
const ch = readJson<ChapterJson>(path.join(chaptersDir, file));
const chNum = ch.chapter;
await proseCol.insertOne({
book: BOOK,
volume: NO_VOLUME,
chapter: chNum,
title: ch.title,
type: ch.type || "chapter",
slug: String(chNum),
order: 100 + chNum,
paragraphs: ch.paragraphs,
});
console.log(
` 📄 Chapter ${chNum}: "${ch.title}" (${ch.paragraphs.length} paragraphs)`,
);
inserted++;
}
// Verify
const totalDocs = await proseCol.countDocuments({ book: BOOK });
console.log(`\n✅ Verified: ${totalDocs} prose_chapters docs for ${BOOK}`);
const elapsed = ((Date.now() - start) / 1000).toFixed(1);
console.log(`\n🎉 Done in ${elapsed}s (${inserted} chapters inserted)`);
await client.close();
}
main().catch((err) => {
console.error("❌ Seed failed:", err);
process.exit(1);
});