38 lines
1.5 KiB
JavaScript
38 lines
1.5 KiB
JavaScript
import { pool } from '../db.js';
|
|
|
|
export async function seedRegions() {
|
|
console.log('📍 Seeding global regions (States/Provinces)...');
|
|
|
|
const regions = [
|
|
// Brazil
|
|
{ name: 'São Paulo', country: 'BR', code: 'SP' },
|
|
{ name: 'Rio de Janeiro', country: 'BR', code: 'RJ' },
|
|
{ name: 'Minas Gerais', country: 'BR', code: 'MG' },
|
|
// USA
|
|
{ name: 'California', country: 'US', code: 'CA' },
|
|
{ name: 'New York', country: 'US', code: 'NY' },
|
|
{ name: 'Texas', country: 'US', code: 'TX' },
|
|
// Japan (keep some for legacy compat if needed, but renamed)
|
|
{ name: 'Tokyo', country: 'JP', code: '13' }, // maintaining code '13' for existing logic if any
|
|
{ name: 'Osaka', country: 'JP', code: '27' }
|
|
];
|
|
|
|
try {
|
|
for (const reg of regions) {
|
|
await pool.query(`
|
|
INSERT INTO regions (name, country_code, code)
|
|
VALUES ($1, $2, $3)
|
|
ON CONFLICT (id) DO NOTHING -- ID is serial, conflict usually on unique constraint if added.
|
|
-- For now, just insert. If code usage is heavy, unique index on code or country+code.
|
|
`, [reg.name, reg.country, reg.code]);
|
|
}
|
|
|
|
// Note: Without unique constraint on code in new schema, we might dup if ran twice without reset.
|
|
// But reset handles it.
|
|
|
|
console.log(` ✓ ${regions.length} regions seeded`);
|
|
} catch (error) {
|
|
console.error(' ❌ Error seeding regions:', error.message);
|
|
throw error;
|
|
}
|
|
}
|