mirror of
https://github.com/IT4Change/gradido.git
synced 2026-03-01 12:44:43 +00:00
use connection pool for drizzle orm
This commit is contained in:
parent
c03f1ecda7
commit
b0e4981463
@ -1,7 +1,7 @@
|
|||||||
import { drizzle, MySql2Database } from 'drizzle-orm/mysql2'
|
import { drizzle, MySql2Database } from 'drizzle-orm/mysql2'
|
||||||
import Redis from 'ioredis'
|
import Redis from 'ioredis'
|
||||||
import { getLogger } from 'log4js'
|
import { getLogger } from 'log4js'
|
||||||
import { Connection, createConnection } from 'mysql2/promise'
|
import { Connection, createConnection, createPool, Pool } from 'mysql2/promise'
|
||||||
import { DataSource as DBDataSource, FileLogger } from 'typeorm'
|
import { DataSource as DBDataSource, FileLogger } from 'typeorm'
|
||||||
import { latestDbVersion } from '.'
|
import { latestDbVersion } from '.'
|
||||||
import { CONFIG } from './config'
|
import { CONFIG } from './config'
|
||||||
@ -14,7 +14,7 @@ export class AppDatabase {
|
|||||||
private static instance: AppDatabase
|
private static instance: AppDatabase
|
||||||
private dataSource: DBDataSource | undefined
|
private dataSource: DBDataSource | undefined
|
||||||
private drizzleDataSource: MySql2Database | undefined
|
private drizzleDataSource: MySql2Database | undefined
|
||||||
private drizzleConnection: Connection | undefined
|
private drizzlePool: Pool | undefined
|
||||||
private redisClient: Redis | undefined
|
private redisClient: Redis | undefined
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -48,10 +48,10 @@ export class AppDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public getDrizzleDataSource(): MySql2Database {
|
public getDrizzleDataSource(): MySql2Database {
|
||||||
if (!this.drizzleDataSource) {
|
if (!this.drizzlePool) {
|
||||||
throw new Error('Drizzle connection not initialized')
|
throw new Error('Drizzle connection pool not initialized')
|
||||||
}
|
}
|
||||||
return this.drizzleDataSource
|
return drizzle({ client: this.drizzlePool })
|
||||||
}
|
}
|
||||||
|
|
||||||
// create database connection, initialize with automatic retry and check for correct database version
|
// create database connection, initialize with automatic retry and check for correct database version
|
||||||
@ -106,22 +106,25 @@ export class AppDatabase {
|
|||||||
logger.info('Redis status=', this.redisClient.status)
|
logger.info('Redis status=', this.redisClient.status)
|
||||||
|
|
||||||
if (!this.drizzleDataSource) {
|
if (!this.drizzleDataSource) {
|
||||||
this.drizzleConnection = await createConnection({
|
this.drizzlePool = createPool({
|
||||||
host: CONFIG.DB_HOST,
|
host: CONFIG.DB_HOST,
|
||||||
user: CONFIG.DB_USER,
|
user: CONFIG.DB_USER,
|
||||||
password: CONFIG.DB_PASSWORD,
|
password: CONFIG.DB_PASSWORD,
|
||||||
database: CONFIG.DB_DATABASE,
|
database: CONFIG.DB_DATABASE,
|
||||||
port: CONFIG.DB_PORT,
|
port: CONFIG.DB_PORT,
|
||||||
|
waitForConnections: true,
|
||||||
|
connectionLimit: 20,
|
||||||
|
queueLimit: 100,
|
||||||
|
enableKeepAlive: true,
|
||||||
|
keepAliveInitialDelay: 10000,
|
||||||
})
|
})
|
||||||
this.drizzleDataSource = drizzle({ client: this.drizzleConnection })
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async destroy(): Promise<void> {
|
public async destroy(): Promise<void> {
|
||||||
await Promise.all([this.dataSource?.destroy(), this.drizzleConnection?.end()])
|
await Promise.all([this.dataSource?.destroy(), this.drizzlePool?.end()])
|
||||||
this.dataSource = undefined
|
this.dataSource = undefined
|
||||||
this.drizzleConnection = undefined
|
this.drizzlePool = undefined
|
||||||
this.drizzleDataSource = undefined
|
|
||||||
if (this.redisClient) {
|
if (this.redisClient) {
|
||||||
await this.redisClient.quit()
|
await this.redisClient.quit()
|
||||||
this.redisClient = undefined
|
this.redisClient = undefined
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user