first commit
This commit is contained in:
82
scripts/seed_mappings.js
Normal file
82
scripts/seed_mappings.js
Normal file
@@ -0,0 +1,82 @@
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const Papa = require('papaparse');
|
||||
const { PrismaClient } = require('../app/generated/prisma');
|
||||
|
||||
async function seedContentHandle(prisma) {
|
||||
const csvPath = path.resolve(process.cwd(), 'datas/ch2.csv');
|
||||
if (!fs.existsSync(csvPath)) {
|
||||
console.warn('content_handle.csv not found, skip');
|
||||
return;
|
||||
}
|
||||
const csv = fs.readFileSync(csvPath, 'utf8');
|
||||
const parsed = Papa.parse(csv, { header: true, skipEmptyLines: true });
|
||||
if (parsed.errors && parsed.errors.length) {
|
||||
console.error('CSV parse errors (content_handle):', parsed.errors);
|
||||
process.exit(1);
|
||||
}
|
||||
let ok = 0, skip = 0;
|
||||
for (const r of parsed.data) {
|
||||
// Keep raw Id to match previously seeded content ids (some have spaces)
|
||||
const contentId = String(r.Id || '');
|
||||
const handle = (r.Handle || '').trim();
|
||||
if (!contentId || !handle) { skip++; continue; }
|
||||
|
||||
// Ensure user handle exists; if not, skip (email is required to create)
|
||||
const existsUser = await prisma.userHandle.findUnique({ where: { handle } });
|
||||
if (!existsUser) { skip++; continue; }
|
||||
|
||||
await prisma.contentHandle.upsert({
|
||||
where: { contentId },
|
||||
update: { handle },
|
||||
create: { contentId, handle },
|
||||
});
|
||||
ok++;
|
||||
}
|
||||
console.log(`ContentHandle seeded: ok=${ok}, skip=${skip}`);
|
||||
}
|
||||
|
||||
async function seedViewPerDay(prisma) {
|
||||
const csvPath = path.resolve(process.cwd(), 'datas/viewperdate.csv');
|
||||
if (!fs.existsSync(csvPath)) {
|
||||
console.warn('viewperdate.csv not found, skip');
|
||||
return;
|
||||
}
|
||||
const csv = fs.readFileSync(csvPath, 'utf8');
|
||||
const parsed = Papa.parse(csv, { header: true, skipEmptyLines: true });
|
||||
if (parsed.errors && parsed.errors.length) {
|
||||
console.error('CSV parse errors (viewperdate):', parsed.errors);
|
||||
process.exit(1);
|
||||
}
|
||||
let ok = 0, bad = 0;
|
||||
for (const r of parsed.data) {
|
||||
const dateStr = (r.Date || '').trim();
|
||||
const contented = (r.Contented || '').trim();
|
||||
const v = Number(r.Validviewday ?? 0);
|
||||
if (!dateStr || !contented) { bad++; continue; }
|
||||
const date = new Date(dateStr);
|
||||
if (isNaN(date.getTime())) { bad++; continue; }
|
||||
|
||||
await prisma.viewPerDay.upsert({
|
||||
where: { date_contented: { date, contented } },
|
||||
update: { validViewDay: v },
|
||||
create: { date, contented, validViewDay: v },
|
||||
});
|
||||
ok++;
|
||||
}
|
||||
console.log(`ViewPerDay seeded: ok=${ok}, bad=${bad}`);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const prisma = new PrismaClient();
|
||||
await seedContentHandle(prisma);
|
||||
await prisma.$disconnect();
|
||||
console.log('Done.');
|
||||
}
|
||||
|
||||
main().catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user