discord-media-server
Version:
Self-hosted Discord bot for scanning and serving information on local media files.
77 lines (71 loc) • 2.31 kB
JavaScript
// lib/setupDatabase.js
import fs from 'fs';
import path from 'path';
export async function createTables(config) {
if (config.dbType === 'mysql') {
const mysql = await import('mysql2/promise');
try {
const conn = await mysql.createConnection({
host: config.dbConfig.host,
user: config.dbConfig.user,
password: config.dbConfig.password
});
await conn.query(`CREATE DATABASE IF NOT EXISTS \`${config.dbConfig.database}\``);
await conn.query(`USE \`${config.dbConfig.database}\``);
await conn.query(`
CREATE TABLE IF NOT EXISTS Movie_Info (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
year VARCHAR(10),
filename TEXT NOT NULL,
poster TEXT,
poster_fallback TEXT,
filepath TEXT,
filesize BIGINT,
imdb VARCHAR(20),
format VARCHAR(20),
runtime VARCHAR(50),
rating VARCHAR(10),
overview TEXT,
added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(title, year)
)
`);
await conn.end();
console.log(`MySQL tables created.`);
} catch (err) {
console.error('MySQL setup failed:', err.message);
throw err;
}
} else {
const dbFile = config.dbConfig.filename;
console.log('Attempting to create DB at:', dbFile);
console.log('Directory exists?', fs.existsSync(path.dirname(dbFile)));
const dir = path.dirname(dbFile);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
const sqlite3 = (await import('better-sqlite3')).default;
const db = new sqlite3(dbFile);
db.prepare(`
CREATE TABLE IF NOT EXISTS Movie_Info (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
year TEXT,
filename TEXT NOT NULL,
poster TEXT,
poster_fallback TEXT,
filepath TEXT,
filesize INTEGER,
imdb TEXT,
format TEXT,
runtime TEXT,
rating TEXT,
overview TEXT,
added_at DATETIME DEFAULT CURRENT_TIMESTAMP,
UNIQUE(title, year)
)
`).run();
console.log(`SQLite tables created.`);
}
}