diff --git a/src/sql/init.sql b/src/sql/init.sql new file mode 100644 index 0000000..abe3b62 --- /dev/null +++ b/src/sql/init.sql @@ -0,0 +1,13 @@ +CREATE TABLE IF NOT EXISTS + reminder ( + id INTEGER PRIMARY KEY, + data TEXT, + expiration_date TEXT, + option_id INTEGER, + channel_id TEXT, + creation_date TEXT, + user_id TEXT, + guild_id TEXT, + locale TEXT, + timeout_id TEXT + ); diff --git a/src/utils/client.ts b/src/utils/client.ts index 14a3874..2ab5276 100644 --- a/src/utils/client.ts +++ b/src/utils/client.ts @@ -5,6 +5,7 @@ import { Database } from "sqlite3"; import "../modules/client"; import { loadLocales } from "./locales"; import { YoutubeiExtractor } from "discord-player-youtubei"; +import { readSQL } from "./db"; /** Creation of the client and definition of its properties */ export default async (isDev: boolean) => { @@ -59,7 +60,7 @@ export default async (isDev: boolean) => { client.db = new Database(`${process.env.DOCKERIZED === "1" ? "/config" : "./config"}/db.sqlite3`); - initDatabase(client.db); + client.db.run(readSQL("init")); return client; }; @@ -75,25 +76,3 @@ export const quit = (client: Client) => { // Close client client.destroy(); }; - -/** - * Initalize the database - * @param db Database - */ -const initDatabase = (db: Database) => { - // Table for reminders - db.run( - "CREATE TABLE IF NOT EXISTS reminder ( \ - id INTEGER PRIMARY KEY, \ - data TEXT, \ - expiration_date TEXT, \ - option_id INTEGER, \ - channel_id TEXT, \ - creation_date TEXT, \ - user_id TEXT, \ - guild_id TEXT, \ - locale TEXT, \ - timeout_id TEXT \ - );", - ); -}; diff --git a/src/utils/db.ts b/src/utils/db.ts new file mode 100644 index 0000000..5d3aa4b --- /dev/null +++ b/src/utils/db.ts @@ -0,0 +1,15 @@ +import fs from "node:fs"; + +export const readSQL = (path: string) => { + const dir = "./src/sql/"; + if (!path.startsWith(dir)) { + path = dir + path; + } + + const ext = ".sql"; + if (!path.endsWith(ext)) { + path += ext; + } + + return fs.readFileSync(path, "utf8"); +};