Use connection pools

This commit is contained in:
Evert Prants 2024-06-13 18:05:27 +03:00
parent 2df11c8091
commit cfec56bcf9
Signed by: evert
GPG Key ID: 1688DA83D222D0B5

View File

@ -6,15 +6,27 @@ import * as schema from './schema';
const { DATABASE_DB, DATABASE_HOST, DATABASE_PASS, DATABASE_USER } = env;
export class DB {
static mysqlConnection: mysql.Connection;
static mysqlConnection: mysql.Pool;
static drizzle: ReturnType<typeof drizzle<typeof schema>>;
static async init() {
DB.mysqlConnection = await mysql.createConnection({
DB.mysqlConnection = mysql.createPool({
host: DATABASE_HOST,
user: DATABASE_USER,
password: DATABASE_PASS,
database: DATABASE_DB
database: DATABASE_DB,
keepAliveInitialDelay: 10 * 1000, // 10 seconds
enableKeepAlive: true,
maxIdle: 0,
idleTimeout: 5 * 60 * 1000 // 5 minutes
});
DB.mysqlConnection.on('connection', (connection) => {
connection.on('error', (error) => {
// We log warning on connection error, this is not a critical error
console.warn('Error received on connection. Will destroy.', { error });
connection.destroy();
});
});
DB.drizzle = drizzle(DB.mysqlConnection, { schema, mode: 'default' });