establish db
This commit is contained in:
90
apps/web/scripts/seed-existing.ts
Normal file
90
apps/web/scripts/seed-existing.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Seed Existing JSON Data into Turso
|
||||
*
|
||||
* Reads the existing plants.json and diseases.json files and inserts them
|
||||
* into the Turso database via Drizzle ORM.
|
||||
*
|
||||
* Usage:
|
||||
* cd apps/web && npx tsx scripts/seed-existing.ts
|
||||
*
|
||||
* Environment: DATABASE_URL and DATABASE_TOKEN from .env.development
|
||||
*/
|
||||
|
||||
import "dotenv/config";
|
||||
import { readFileSync } from "fs";
|
||||
import { resolve } from "path";
|
||||
import { sql } from "drizzle-orm";
|
||||
import { getDb, closeDb } from "../src/lib/db/index";
|
||||
import { plants, diseases } from "../src/lib/db/schema";
|
||||
import type { Plant, Disease } from "../src/lib/types";
|
||||
|
||||
// ─── Load JSON data ──────────────────────────────────────────────────────────
|
||||
|
||||
const __dirname = resolve(new URL(".", import.meta.url).pathname);
|
||||
|
||||
const plantsPath = resolve(__dirname, "../src/data/plants.json");
|
||||
const diseasesPath = resolve(__dirname, "../src/data/diseases.json");
|
||||
|
||||
const rawPlants = JSON.parse(readFileSync(plantsPath, "utf-8")) as Plant[];
|
||||
const rawDiseases = JSON.parse(readFileSync(diseasesPath, "utf-8")) as Disease[];
|
||||
|
||||
// ─── Seed ────────────────────────────────────────────────────────────────────
|
||||
|
||||
async function main() {
|
||||
const db = getDb();
|
||||
|
||||
console.log(`Seeding ${rawPlants.length} plants...`);
|
||||
for (const p of rawPlants) {
|
||||
await db
|
||||
.insert(plants)
|
||||
.values({
|
||||
id: p.id,
|
||||
commonName: p.commonName,
|
||||
scientificName: p.scientificName,
|
||||
family: p.family,
|
||||
category: p.category,
|
||||
careSummary: p.careSummary,
|
||||
imageUrl: p.imageUrl,
|
||||
})
|
||||
.onConflictDoNothing();
|
||||
}
|
||||
console.log(`✅ ${rawPlants.length} plants inserted`);
|
||||
|
||||
console.log(`Seeding ${rawDiseases.length} diseases...`);
|
||||
for (const d of rawDiseases) {
|
||||
await db
|
||||
.insert(diseases)
|
||||
.values({
|
||||
id: d.id,
|
||||
plantId: d.plantId,
|
||||
name: d.name,
|
||||
scientificName: d.scientificName,
|
||||
causalAgentType: d.causalAgentType,
|
||||
description: d.description,
|
||||
symptoms: d.symptoms,
|
||||
causes: d.causes,
|
||||
treatment: d.treatment,
|
||||
prevention: d.prevention,
|
||||
lookalikeIds: d.lookalikeDiseaseIds,
|
||||
severity: d.severity,
|
||||
sourceUrl: "",
|
||||
})
|
||||
.onConflictDoNothing();
|
||||
}
|
||||
console.log(`✅ ${rawDiseases.length} diseases inserted`);
|
||||
|
||||
// Verify
|
||||
const [plantCount] = await db.select({ count: sql<number>`COUNT(*)` }).from(plants);
|
||||
const [diseaseCount] = await db.select({ count: sql<number>`COUNT(*)` }).from(diseases);
|
||||
console.log(`\n📊 Database now has:`);
|
||||
console.log(` ${plantCount.count} plants`);
|
||||
console.log(` ${diseaseCount.count} diseases`);
|
||||
|
||||
closeDb();
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error("❌ Seed failed:", err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user