40 lines
1.1 KiB
JavaScript
40 lines
1.1 KiB
JavaScript
const fs = require("fs");
|
|
const Database = require("better-sqlite3");
|
|
const path = require("path");
|
|
const { open } = require('sqlite');
|
|
const filepath = "./data/minzbot.db";
|
|
|
|
async function createDbConnection() {
|
|
const dir = path.dirname(filepath);
|
|
if (!fs.existsSync(dir)) {
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
}
|
|
|
|
const db = new Database(filepath, { verbose: console.log });
|
|
|
|
initDB(db);
|
|
|
|
console.log("[DATABASE] Connection with better-sqlite3 established");
|
|
return db;
|
|
}
|
|
|
|
async function initDB(db) {
|
|
await db.exec(`
|
|
CREATE TABLE IF NOT EXISTS anniversaries (
|
|
ID INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name VARCHAR(50) NOT NULL,
|
|
guild_id TEXT NOT NULL,
|
|
discord_id VARCHAR(50) NOT NULL,
|
|
last_anniversary_notification TEXT
|
|
);
|
|
`);
|
|
await db.exec(`
|
|
CREATE TABLE IF NOT EXISTS lastfm (
|
|
discord_id TEXT PRIMARY KEY NOT NULL,
|
|
lastfm_name TEXT
|
|
);
|
|
`);
|
|
console.log('[DATABASE] Created new DB table');
|
|
}
|
|
|
|
module.exports = createDbConnection; |