|
| 1 | +/** |
| 2 | + * Populate BotanicalSynonym from USDA CropSource data. |
| 3 | + * |
| 4 | + * USDA rawData has "Synonym Symbol" and "Accepted Symbol" fields. |
| 5 | + * When a crop's Symbol != Accepted Symbol, the crop's Scientific Name is |
| 6 | + * a synonym and the accepted crop is the canonical one. This script creates |
| 7 | + * a BotanicalSynonym row on the accepted crop pointing at the synonym name. |
| 8 | + * |
| 9 | + * Also mines the "Scientific Name" field to catch cases where the stored |
| 10 | + * botanicalName differs from the USDA name (import normalisation artifacts). |
| 11 | + * |
| 12 | + * Usage: |
| 13 | + * pnpm enrich:populate-botanical-synonyms |
| 14 | + * pnpm enrich:populate-botanical-synonyms --dry-run |
| 15 | + */ |
| 16 | + |
| 17 | +import { PrismaClient } from '@prisma/client' |
| 18 | + |
| 19 | +const prisma = new PrismaClient() |
| 20 | + |
| 21 | +interface UsdaRawData { |
| 22 | + Symbol?: string |
| 23 | + 'Synonym Symbol'?: string |
| 24 | + 'Accepted Symbol'?: string |
| 25 | + 'Scientific Name'?: string |
| 26 | +} |
| 27 | + |
| 28 | +async function main() { |
| 29 | + const dryRun = process.argv.includes('--dry-run') |
| 30 | + |
| 31 | + const sources = await prisma.cropSource.findMany({ |
| 32 | + where: { source: 'USDA' }, |
| 33 | + select: { cropId: true, rawData: true }, |
| 34 | + }) |
| 35 | + |
| 36 | + // Build symbol → cropId map from all USDA sources |
| 37 | + const symbolToCropId = new Map<string, string>() |
| 38 | + for (const s of sources) { |
| 39 | + const d = s.rawData as UsdaRawData |
| 40 | + if (d.Symbol) symbolToCropId.set(d.Symbol, s.cropId) |
| 41 | + } |
| 42 | + |
| 43 | + let created = 0 |
| 44 | + let skipped = 0 |
| 45 | + |
| 46 | + for (const s of sources) { |
| 47 | + const d = s.rawData as UsdaRawData |
| 48 | + const symbol = d.Symbol ?? '' |
| 49 | + const acceptedSymbol = d['Accepted Symbol'] ?? '' |
| 50 | + const scientificName = d['Scientific Name'] |
| 51 | + |
| 52 | + if (!scientificName) continue |
| 53 | + |
| 54 | + // This entry IS a synonym: point at the accepted crop |
| 55 | + if (acceptedSymbol && acceptedSymbol !== symbol) { |
| 56 | + const acceptedCropId = symbolToCropId.get(acceptedSymbol) |
| 57 | + if (!acceptedCropId) continue |
| 58 | + |
| 59 | + if (dryRun) { |
| 60 | + console.log(` synonym: "${scientificName}" → accepted symbol ${acceptedSymbol} (cropId ${acceptedCropId})`) |
| 61 | + } else { |
| 62 | + await prisma.botanicalSynonym.upsert({ |
| 63 | + where: { cropId_name: { cropId: acceptedCropId, name: scientificName } }, |
| 64 | + create: { cropId: acceptedCropId, name: scientificName, source: 'usda' }, |
| 65 | + update: {}, |
| 66 | + }) |
| 67 | + } |
| 68 | + created++ |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + // Same symbol accepted — check if USDA Scientific Name differs from stored botanicalName |
| 73 | + const crop = await prisma.crop.findUnique({ |
| 74 | + where: { id: s.cropId }, |
| 75 | + select: { botanicalName: true }, |
| 76 | + }) |
| 77 | + if (!crop) continue |
| 78 | + if (scientificName === crop.botanicalName) { skipped++; continue } |
| 79 | + |
| 80 | + if (dryRun) { |
| 81 | + console.log(` alt name: crop ${s.cropId}: "${scientificName}" vs stored "${crop.botanicalName}"`) |
| 82 | + } else { |
| 83 | + await prisma.botanicalSynonym.upsert({ |
| 84 | + where: { cropId_name: { cropId: s.cropId, name: scientificName } }, |
| 85 | + create: { cropId: s.cropId, name: scientificName, source: 'usda' }, |
| 86 | + update: {}, |
| 87 | + }) |
| 88 | + } |
| 89 | + created++ |
| 90 | + } |
| 91 | + |
| 92 | + await prisma.$disconnect() |
| 93 | + if (dryRun) { |
| 94 | + console.log(`\n[dry-run] Would create ${created} synonym rows (${skipped} no difference).`) |
| 95 | + } else { |
| 96 | + console.log(`Done. Created/verified ${created} BotanicalSynonym rows, ${skipped} no difference.`) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +main().catch(err => { console.error(err); process.exit(1) }) |
0 commit comments