24 lines
796 B
JavaScript
24 lines
796 B
JavaScript
import initSqlJs from 'sql.js';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
const dbPath = process.env.DATABASE_PATH || path.join(__dirname, '..', 'data', 'configurateur.db');
|
|
|
|
if (!fs.existsSync(dbPath)) {
|
|
console.error('Database file not found. Run npm run db:init first.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const SQL = await initSqlJs({
|
|
locateFile: (file) => path.join(__dirname, '..', 'node_modules', 'sql.js', 'dist', file)
|
|
});
|
|
const buffer = fs.readFileSync(dbPath);
|
|
const db = new SQL.Database(buffer);
|
|
const seedSql = fs.readFileSync(path.join(__dirname, 'seeds.sql'), 'utf8');
|
|
db.exec(seedSql);
|
|
fs.writeFileSync(dbPath, Buffer.from(db.export()));
|
|
db.close();
|
|
console.log('Seeds applied.');
|