initial commit
This commit is contained in:
73
backend/bootstrap.js
vendored
Normal file
73
backend/bootstrap.js
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { parse } = require('csv-parse/sync');
|
||||
const db = require('./database');
|
||||
|
||||
function bootstrapGames() {
|
||||
// Check if games already exist
|
||||
const count = db.prepare('SELECT COUNT(*) as count FROM games').get();
|
||||
|
||||
if (count.count > 0) {
|
||||
console.log(`Database already has ${count.count} games. Skipping bootstrap.`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Read the CSV file
|
||||
const csvPath = path.join(__dirname, '..', 'games-list.csv');
|
||||
|
||||
if (!fs.existsSync(csvPath)) {
|
||||
console.log('games-list.csv not found. Skipping bootstrap.');
|
||||
return;
|
||||
}
|
||||
|
||||
const csvContent = fs.readFileSync(csvPath, 'utf-8');
|
||||
const records = parse(csvContent, {
|
||||
columns: true,
|
||||
skip_empty_lines: true,
|
||||
trim: true
|
||||
});
|
||||
|
||||
const insert = db.prepare(`
|
||||
INSERT INTO games (
|
||||
pack_name, title, min_players, max_players, length_minutes,
|
||||
has_audience, family_friendly, game_type, secondary_type, enabled
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 1)
|
||||
`);
|
||||
|
||||
const insertMany = db.transaction((games) => {
|
||||
for (const game of games) {
|
||||
insert.run(
|
||||
game['Game Pack'],
|
||||
game['Game Title'],
|
||||
parseInt(game['Min. Players']) || 1,
|
||||
parseInt(game['Max. Players']) || 8,
|
||||
parseLengthMinutes(game['Length']),
|
||||
parseBoolean(game['Audience']),
|
||||
parseBoolean(game['Family Friendly?']),
|
||||
game['Game Type'] || null,
|
||||
game['Secondary Type'] || null
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
insertMany(records);
|
||||
console.log(`Successfully imported ${records.length} games from CSV`);
|
||||
}
|
||||
|
||||
function parseLengthMinutes(lengthStr) {
|
||||
if (!lengthStr || lengthStr === '????' || lengthStr === '?') {
|
||||
return null;
|
||||
}
|
||||
const match = lengthStr.match(/(\d+)/);
|
||||
return match ? parseInt(match[1]) : null;
|
||||
}
|
||||
|
||||
function parseBoolean(value) {
|
||||
if (!value || value === '?' || value === '????') {
|
||||
return 0;
|
||||
}
|
||||
return value.toLowerCase() === 'yes' ? 1 : 0;
|
||||
}
|
||||
|
||||
module.exports = { bootstrapGames };
|
||||
|
||||
Reference in New Issue
Block a user