Files
radiola-backend/prisma/seed.ts

65 lines
1.5 KiB
TypeScript

import { PrismaClient } from '@prisma/client';
import * as fs from 'fs';
import * as path from 'path';
const prisma = new PrismaClient();
async function main() {
const stationsPath = path.join(__dirname, 'stations.json');
if (!fs.existsSync(stationsPath)) {
console.warn(`Stations file not found at ${stationsPath}. Skipping seed.`);
return;
}
const raw = fs.readFileSync(stationsPath, 'utf-8');
const data = JSON.parse(raw);
const groups: Record<number, string> = {};
for (const group of data.groups || []) {
groups[group.id] = group.name || 'Unknown';
}
const stations = data.stations || [];
console.log(`Seeding ${stations.length} stations...`);
let count = 0;
for (const s of stations) {
const groupName = groups[s.groupId] || 'Unknown';
await prisma.station.upsert({
where: { stationId: s.id },
update: {
name: s.name,
streamUrl: s.stream,
genre: groupName,
tags: [groupName],
sortOrder: count,
source: 'local',
},
create: {
stationId: s.id,
name: s.name,
prefix: '',
streamUrl: s.stream,
coverUrl: null,
genre: groupName,
tags: [groupName],
sortOrder: count,
source: 'local',
},
});
count++;
}
console.log(`Seeded ${count} stations.`);
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});