180 lines
5.2 KiB
TypeScript
180 lines
5.2 KiB
TypeScript
/**
|
|
* seed-pre-1977-essays.ts — Push thirteen pre-1977 essays into MongoDB.
|
|
*
|
|
* Uses the native MongoDB driver (same pattern as seed-cc-essays.ts) to avoid
|
|
* Prisma replica-set write requirements.
|
|
*
|
|
* Collections written:
|
|
* - essays → 9 essay docs (upsert by slug)
|
|
*
|
|
* JSON sources:
|
|
* - lib/jsons/essays/man-is-the-architect-of-his-own-fortune.json
|
|
* - lib/jsons/essays/what-is-the-matter-with-the-world.json
|
|
* - lib/jsons/essays/religion-is-godly-life.json
|
|
* - lib/jsons/essays/truth-and-beauty.json
|
|
* - lib/jsons/essays/misuse-of-public-fund-in-the-name-of-religion.json
|
|
* - lib/jsons/essays/perfection-at-home.json
|
|
* - lib/jsons/essays/how-should-one-cultivate-human-spirit.json
|
|
* - lib/jsons/essays/viswa-dharma.json
|
|
* - lib/jsons/essays/glorification-of-srila-bhaktisiddhanta-sarasvati-thakura.json
|
|
* - lib/jsons/essays/who-is-crazy-btg66.json
|
|
* - lib/jsons/essays/manifestation-of-dharma-in-human-being.json
|
|
* - lib/jsons/essays/message-of-krishna-consciousness.json
|
|
* - lib/jsons/essays/a-wrong-civilization.json
|
|
*
|
|
* Usage:
|
|
* npx tsx scripts/seed-pre-1977-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 = [
|
|
"man-is-the-architect-of-his-own-fortune.json",
|
|
"what-is-the-matter-with-the-world.json",
|
|
"religion-is-godly-life.json",
|
|
"truth-and-beauty.json",
|
|
"misuse-of-public-fund-in-the-name-of-religion.json",
|
|
"perfection-at-home.json",
|
|
"how-should-one-cultivate-human-spirit.json",
|
|
"viswa-dharma.json",
|
|
"glorification-of-srila-bhaktisiddhanta-sarasvati-thakura.json",
|
|
"who-is-crazy-btg66.json",
|
|
"manifestation-of-dharma-in-human-being.json",
|
|
"message-of-krishna-consciousness.json",
|
|
"a-wrong-civilization.json",
|
|
];
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Types
|
|
// ---------------------------------------------------------------------------
|
|
|
|
type EssayJson = {
|
|
slug: string;
|
|
title: string;
|
|
author: string;
|
|
year: number | null;
|
|
group: string;
|
|
order: number;
|
|
paragraphs: string[];
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Helpers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function readJson<T>(filePath: string): T {
|
|
return JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Seed function
|
|
// ---------------------------------------------------------------------------
|
|
|
|
async function seedPre1977Essays(db: ReturnType<MongoClient["db"]>) {
|
|
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(" Ensure the JSON file exists in lib/jsons/essays/.");
|
|
process.exit(1);
|
|
}
|
|
|
|
const essay = readJson<EssayJson>(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: "pre-1977-essays",
|
|
});
|
|
console.log(
|
|
`\n ✅ Verified: ${totalDocs} document(s) in essays collection with group "pre-1977-essays"`,
|
|
);
|
|
|
|
// Summary
|
|
console.log(
|
|
`\n Summary: ${inserted} inserted, ${updated} updated, ${unchanged} unchanged`,
|
|
);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Main
|
|
// ---------------------------------------------------------------------------
|
|
|
|
async function main() {
|
|
console.log("🌱 Seeding Pre-1977 Essays (13 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 seedPre1977Essays(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);
|
|
});
|