svelte5 migration, formatting cleanup
This commit is contained in:
@@ -10,11 +10,11 @@ const schema = fs.readFileSync('schema.sql', 'utf8');
|
||||
const db = new Database(dbName);
|
||||
|
||||
try {
|
||||
console.log(`Connected to the ${dbName} database.`);
|
||||
db.exec(schema);
|
||||
console.log('Schema initialized successfully.');
|
||||
console.log(`Connected to the ${dbName} database.`);
|
||||
db.exec(schema);
|
||||
console.log('Schema initialized successfully.');
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
console.error(err.message);
|
||||
} finally {
|
||||
db.close();
|
||||
db.close();
|
||||
}
|
||||
|
||||
@@ -11,15 +11,15 @@ const db = new Database(dbName);
|
||||
// Map JSON attribute names to database column names
|
||||
// Not needed today, but makes schema changes easier
|
||||
const lookup = {
|
||||
id: 'id',
|
||||
date: 'stream_date',
|
||||
filename: 'filename',
|
||||
format: 'format',
|
||||
title: 'title',
|
||||
tags: 'tags',
|
||||
description: 'description',
|
||||
length_seconds: 'length_seconds',
|
||||
tracks: 'tracks'
|
||||
id: 'id',
|
||||
date: 'stream_date',
|
||||
filename: 'filename',
|
||||
format: 'format',
|
||||
title: 'title',
|
||||
tags: 'tags',
|
||||
description: 'description',
|
||||
length_seconds: 'length_seconds',
|
||||
tracks: 'tracks'
|
||||
};
|
||||
|
||||
// Retrieve existing IDs from Stream
|
||||
@@ -32,40 +32,39 @@ const shouldOverwrite = process.argv.includes('--overwrite');
|
||||
|
||||
// Process JSON files and insert data into Stream
|
||||
fs.readdir(jsonFolder, (err, files) => {
|
||||
if (err) throw err;
|
||||
console.log(`Found ${files.length} JSON file(s) in ${jsonFolder}.`);
|
||||
if (err) throw err;
|
||||
console.log(`Found ${files.length} JSON file(s) in ${jsonFolder}.`);
|
||||
|
||||
files.forEach(file => {
|
||||
if (!file.endsWith('.json')) return;
|
||||
if (file.endsWith('.sideload.json')) return;
|
||||
files.forEach((file) => {
|
||||
if (!file.endsWith('.json')) return;
|
||||
if (file.endsWith('.sideload.json')) return;
|
||||
|
||||
const jsonString = fs.readFileSync(path.join(jsonFolder, file), 'utf8');
|
||||
let jsonData = JSON.parse(jsonString);
|
||||
const jsonString = fs.readFileSync(path.join(jsonFolder, file), 'utf8');
|
||||
let jsonData = JSON.parse(jsonString);
|
||||
|
||||
const sideloadPath = path.join(jsonFolder, file.slice(0, -5) + ".sideload.json");
|
||||
if (fs.existsSync(sideloadPath, 'utf8')) {
|
||||
const sideloadData = JSON.parse(fs.readFileSync(sideloadPath));
|
||||
jsonData = { ...jsonData, ...sideloadData };
|
||||
}
|
||||
// Skip if ID already exists in Stream
|
||||
if (idSet.has(jsonData.id)) {
|
||||
if (!shouldOverwrite) {
|
||||
console.log(`Skipped data for ID ${jsonData.id} (already exists).`);
|
||||
return;
|
||||
}
|
||||
console.log(`Overwriting data for ID ${jsonData.id}.`);
|
||||
}
|
||||
const sideloadPath = path.join(jsonFolder, file.slice(0, -5) + '.sideload.json');
|
||||
if (fs.existsSync(sideloadPath, 'utf8')) {
|
||||
const sideloadData = JSON.parse(fs.readFileSync(sideloadPath));
|
||||
jsonData = { ...jsonData, ...sideloadData };
|
||||
}
|
||||
// Skip if ID already exists in Stream
|
||||
if (idSet.has(jsonData.id)) {
|
||||
if (!shouldOverwrite) {
|
||||
console.log(`Skipped data for ID ${jsonData.id} (already exists).`);
|
||||
return;
|
||||
}
|
||||
console.log(`Overwriting data for ID ${jsonData.id}.`);
|
||||
}
|
||||
|
||||
// Prepare attributes for insertion
|
||||
const values = Object.keys(jsonData).map(
|
||||
// Serialize value if array
|
||||
key => Array.isArray(jsonData[key]) ? JSON.stringify(jsonData[key]) : jsonData[key]
|
||||
);
|
||||
const columns = Object.keys(jsonData).map(key => lookup[key]);
|
||||
// Prepare attributes for insertion
|
||||
const values = Object.keys(jsonData).map(
|
||||
// Serialize value if array
|
||||
(key) => (Array.isArray(jsonData[key]) ? JSON.stringify(jsonData[key]) : jsonData[key])
|
||||
);
|
||||
const columns = Object.keys(jsonData).map((key) => lookup[key]);
|
||||
|
||||
|
||||
const sql = `INSERT OR REPLACE INTO Stream (${columns.join(', ')}) VALUES (${columns.map(() => '?').join(', ')})`;
|
||||
db.prepare(sql).run(...values);
|
||||
console.log(`Inserted data for ID ${jsonData.id}.`);
|
||||
});
|
||||
const sql = `INSERT OR REPLACE INTO Stream (${columns.join(', ')}) VALUES (${columns.map(() => '?').join(', ')})`;
|
||||
db.prepare(sql).run(...values);
|
||||
console.log(`Inserted data for ID ${jsonData.id}.`);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user