Use connection pools
This commit is contained in:
parent
2df11c8091
commit
cfec56bcf9
@ -6,15 +6,27 @@ import * as schema from './schema';
|
|||||||
const { DATABASE_DB, DATABASE_HOST, DATABASE_PASS, DATABASE_USER } = env;
|
const { DATABASE_DB, DATABASE_HOST, DATABASE_PASS, DATABASE_USER } = env;
|
||||||
|
|
||||||
export class DB {
|
export class DB {
|
||||||
static mysqlConnection: mysql.Connection;
|
static mysqlConnection: mysql.Pool;
|
||||||
static drizzle: ReturnType<typeof drizzle<typeof schema>>;
|
static drizzle: ReturnType<typeof drizzle<typeof schema>>;
|
||||||
|
|
||||||
static async init() {
|
static async init() {
|
||||||
DB.mysqlConnection = await mysql.createConnection({
|
DB.mysqlConnection = mysql.createPool({
|
||||||
host: DATABASE_HOST,
|
host: DATABASE_HOST,
|
||||||
user: DATABASE_USER,
|
user: DATABASE_USER,
|
||||||
password: DATABASE_PASS,
|
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' });
|
DB.drizzle = drizzle(DB.mysqlConnection, { schema, mode: 'default' });
|
||||||
|
Loading…
Reference in New Issue
Block a user