/** * seed-cc-essays.ts — Push all five "Essays on Śrī Caitanya-caritāmṛta" into MongoDB. * * Uses the native MongoDB driver (same pattern as seed-te.ts) to avoid * Prisma replica-set write requirements. * * Collections written: * - essays → 5 essay docs (upsert by slug) * * JSON sources: * - lib/jsons/essays/the-spiritual-masters.json * - lib/jsons/essays/the-absolute-truth.json * - lib/jsons/essays/causes-of-lord-caitanyas-incarnation.json * - lib/jsons/essays/confidential-reasons-for-lord-caitanyas-appearance.json * - lib/jsons/essays/glories-of-lord-nityananda.json * * Usage: * npx tsx scripts/seed-cc-essays.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 ESSAYS_DIR = path.join(BASE_DIR, "lib", "jsons", "essays"); const ESSAY_FILES = [ "the-spiritual-masters.json", "the-absolute-truth.json", "causes-of-lord-caitanyas-incarnation.json", "confidential-reasons-for-lord-caitanyas-appearance.json", "glories-of-lord-nityananda.json", ]; // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- type EssayJson = { slug: string; title: string; author: string; year: number; group: string; order: number; paragraphs: string[]; }; // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- function readJson(filePath: string): T { return JSON.parse(fs.readFileSync(filePath, "utf-8")); } // --------------------------------------------------------------------------- // Seed function // --------------------------------------------------------------------------- async function seedCcEssays(db: ReturnType) { const essaysCol = db.collection("essays"); let inserted = 0; let updated = 0; let unchanged = 0; for (const filename of ESSAY_FILES) { const jsonPath = path.join(ESSAYS_DIR, filename); if (!fs.existsSync(jsonPath)) { console.error(` ❌ Missing: ${jsonPath}`); console.error(" Run `python3 scripts/cc_essays_extract.py` first."); process.exit(1); } const essay = readJson(jsonPath); const result = await essaysCol.updateOne( { slug: essay.slug }, { $set: { slug: essay.slug, title: essay.title, author: essay.author, year: essay.year, group: essay.group, order: essay.order, paragraphs: essay.paragraphs, }, }, { upsert: true }, ); if (result.upsertedCount > 0) { console.log( ` 📝 Inserted "${essay.title}" (${essay.paragraphs.length} paragraphs)`, ); inserted++; } else if (result.modifiedCount > 0) { console.log( ` 📝 Updated "${essay.title}" (${essay.paragraphs.length} paragraphs)`, ); updated++; } else { console.log(` 📝 "${essay.title}" already up to date`); unchanged++; } } // Verify const totalDocs = await essaysCol.countDocuments({ group: "cc-essays", }); console.log( `\n ✅ Verified: ${totalDocs} document(s) in essays collection with group "cc-essays"`, ); // Summary console.log( `\n Summary: ${inserted} inserted, ${updated} updated, ${unchanged} unchanged`, ); } // --------------------------------------------------------------------------- // Main // --------------------------------------------------------------------------- async function main() { console.log("🌱 Seeding CC Essays (5 essays)...\n"); const client = new MongoClient(DATABASE_URL!); await client.connect(); console.log("📡 Connected to MongoDB\n"); const db = client.db(); const start = Date.now(); await seedCcEssays(db); const elapsed = ((Date.now() - start) / 1000).toFixed(1); console.log(`\n🎉 Done in ${elapsed}s`); await client.close(); } main().catch((err) => { console.error("❌ Seed failed:", err); process.exit(1); });