import {createPool} from "mysql2/promise" import type {Pool} from "mysql2/promise"; import type { Database, ScopeRepo, StatRepo } from "../interfaces"; import MysqlDBScopes from "./scopes"; export default class MysqlDB implements Database { connection: Pool userId: string; private constructor(userId: string, connection: Pool) { this.userId = userId; this.connection = connection; } scopes(): ScopeRepo { return new MysqlDBScopes(this.connection, this.userId); } stats(scopeId: number): StatRepo { throw new Error("Method not implemented."); } withUser(userId: string): Database { return new MysqlDB(userId, this.connection); } static async connectEnv(): Promise { return this.connect( process.env.STUFFLOG3_MYSQL_HOST, parseInt(process.env.STUFFLOG3_MYSQL_PORT), process.env.STUFFLOG3_MYSQL_USERNAME, process.env.STUFFLOG3_MYSQL_PASSWORD, process.env.STUFFLOG3_MYSQL_SCHEMA, ) } static async connect(host: string, port: number, user: string, password: string, database: string): Promise { const connection = await createPool({ host, user, database, password, port, waitForConnections: true, connectionLimit: 20, queueLimit: 0, }); return new MysqlDB("", connection); } }