You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.3 KiB
48 lines
1.3 KiB
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<MysqlDB> {
|
|
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<MysqlDB> {
|
|
const connection = await createPool({
|
|
host, user, database, password, port,
|
|
waitForConnections: true,
|
|
connectionLimit: 20,
|
|
queueLimit: 0,
|
|
});
|
|
|
|
return new MysqlDB("", connection);
|
|
}
|
|
}
|