initial commit
This commit is contained in:
2
db/.gitignore
vendored
Normal file
2
db/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
strimserve.db
|
||||
stream_json
|
||||
20
db/init_db.js
Normal file
20
db/init_db.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import fs from 'fs';
|
||||
import Database from 'better-sqlite3';
|
||||
|
||||
const dbName = 'strimserve.db';
|
||||
|
||||
// Read the schema file
|
||||
const schema = fs.readFileSync('schema.sql', 'utf8');
|
||||
|
||||
// Create a new database object and initialize it with the schema
|
||||
const db = new Database(dbName);
|
||||
|
||||
try {
|
||||
console.log(`Connected to the ${dbName} database.`);
|
||||
db.exec(schema);
|
||||
console.log('Schema initialized successfully.');
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
12
db/schema.sql
Normal file
12
db/schema.sql
Normal file
@@ -0,0 +1,12 @@
|
||||
CREATE TABLE Stream (
|
||||
id TEXT PRIMARY KEY NOT NULL UNIQUE,
|
||||
stream_date TEXT NOT NULL,
|
||||
filename TEXT NOT NULL UNIQUE,
|
||||
format TEXT NOT NULL,
|
||||
title TEXT,
|
||||
description TEXT,
|
||||
tags TEXT,
|
||||
length_seconds INT NOT NULL,
|
||||
tracks TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
|
||||
);
|
||||
68
db/update_db.js
Normal file
68
db/update_db.js
Normal file
@@ -0,0 +1,68 @@
|
||||
import fs from 'fs';
|
||||
import Database from 'better-sqlite3';
|
||||
|
||||
const jsonFolder = './stream_json/';
|
||||
const dbName = 'strimserve.db';
|
||||
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'
|
||||
};
|
||||
|
||||
// Retrieve existing IDs from Stream
|
||||
const existingIds = db.prepare('SELECT id FROM Stream').pluck().all();
|
||||
// Create a set of existing IDs for efficient lookup
|
||||
const idSet = new Set(existingIds);
|
||||
|
||||
// Check if --overwrite flag is set
|
||||
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}.`);
|
||||
|
||||
files.forEach(file => {
|
||||
if (!file.endsWith('.json')) return;
|
||||
if (file.endsWith('.sideload.json')) return;
|
||||
|
||||
const jsonString = fs.readFileSync(jsonFolder + file, 'utf8');
|
||||
let jsonData = JSON.parse(jsonString);
|
||||
|
||||
const sideloadPath = 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]);
|
||||
|
||||
|
||||
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