93 lines
3.4 KiB
TypeScript
93 lines
3.4 KiB
TypeScript
import 'dotenv/config';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import db from '../src/database/models/index.js';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const { Zone, State, Location } = db;
|
|
|
|
async function run() {
|
|
console.log('--- Seeding Real Geo Data (Denormalized Model) ---');
|
|
try {
|
|
await db.sequelize.authenticate();
|
|
|
|
// Read the source seeder file
|
|
const seederPath = path.join(__dirname, '../seeders/20240127-seed-geo-data.js');
|
|
const content = fs.readFileSync(seederPath, 'utf8');
|
|
|
|
// Extract arrays using regex
|
|
const zonesMatch = content.match(/const ZONES_DATA = \[([\s\S]*?)\];/);
|
|
const statesMatch = content.match(/const STATES_DATA = \[([\s\S]*?)\];/);
|
|
const citiesMatch = content.match(/const CITIES_DATA = \[([\s\S]*?)\];/);
|
|
|
|
if (!zonesMatch || !statesMatch || !citiesMatch) {
|
|
throw new Error('Could not parse geo data arrays from seeder file!');
|
|
}
|
|
|
|
// Eval helper (data is trusted since it's our own seeder)
|
|
const ZONES_DATA = eval(`[${zonesMatch[1]}]`);
|
|
const STATES_DATA = eval(`[${statesMatch[1]}]`);
|
|
const CITIES_DATA = eval(`[${citiesMatch[1]}]`);
|
|
|
|
console.log(`Extracted ${ZONES_DATA.length} Zones, ${STATES_DATA.length} States, and ${CITIES_DATA.length} Districts.`);
|
|
|
|
// 1. Seed Zones
|
|
const zoneIdMap = new Map(); // Name -> UUID
|
|
for (const z of ZONES_DATA) {
|
|
const [zoneRecord] = await Zone.findOrCreate({
|
|
where: { name: z.name },
|
|
defaults: { name: z.name, code: z.code }
|
|
});
|
|
zoneIdMap.set(z.name, zoneRecord.id);
|
|
// Attach states list for later lookup
|
|
z._dbId = zoneRecord.id;
|
|
}
|
|
console.log('Zones seeded.');
|
|
|
|
// 2. Seed States and link to Zones
|
|
const stateIdMap = new Map(); // Legacy ID -> { id, zoneId }
|
|
for (const s of STATES_DATA) {
|
|
// Find parent zone by checking which zone's states array contains this state name
|
|
const parentZoneData = ZONES_DATA.find((z: any) => z.states.includes(s.name));
|
|
const zoneId = parentZoneData ? zoneIdMap.get(parentZoneData.name) : null;
|
|
|
|
const [stateRecord] = await State.findOrCreate({
|
|
where: { name: s.name },
|
|
defaults: { name: s.name, zoneId: zoneId }
|
|
});
|
|
|
|
stateIdMap.set(s.id, { id: stateRecord.id, zoneId: zoneId });
|
|
}
|
|
console.log('States seeded.');
|
|
|
|
// 3. Seed Districts (Locations)
|
|
let districtCount = 0;
|
|
for (const c of CITIES_DATA) {
|
|
const parentStateData = stateIdMap.get(c.state_id);
|
|
if (parentStateData) {
|
|
await Location.findOrCreate({
|
|
where: { name: c.name, stateId: parentStateData.id },
|
|
defaults: {
|
|
name: c.name,
|
|
stateId: parentStateData.id,
|
|
zoneId: parentStateData.zoneId
|
|
}
|
|
});
|
|
districtCount++;
|
|
}
|
|
}
|
|
|
|
console.log(`✅ Successfully seeded Real Geo Data! Created/Verified ${districtCount} districts.`);
|
|
process.exit(0);
|
|
|
|
} catch (e: any) {
|
|
console.error('❌ Failed:', e.message);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
run();
|