finetune code, move drizzle init after type orm init

This commit is contained in:
einhornimmond 2025-12-06 15:23:02 +01:00
parent 4745de4f58
commit 29bbecfe44
4 changed files with 15 additions and 21 deletions

View File

@ -83,17 +83,6 @@ export class AppDatabase {
})
}
if (!this.drizzleDataSource) {
this.drizzleConnection = await createConnection({
host: CONFIG.DB_HOST,
user: CONFIG.DB_USER,
password: CONFIG.DB_PASSWORD,
database: CONFIG.DB_DATABASE,
port: CONFIG.DB_PORT,
})
await this.drizzleConnection.ping()
this.drizzleDataSource = drizzle({ client: this.drizzleConnection })
}
// retry connection on failure some times to allow database to catch up
for (let attempt = 1; attempt <= CONFIG.DB_CONNECT_RETRY_COUNT; attempt++) {
try {
@ -115,6 +104,17 @@ export class AppDatabase {
this.redisClient = new Redis(CONFIG.REDIS_URL)
logger.info('Redis status=', this.redisClient.status)
if (!this.drizzleDataSource) {
this.drizzleConnection = await createConnection({
host: CONFIG.DB_HOST,
user: CONFIG.DB_USER,
password: CONFIG.DB_PASSWORD,
database: CONFIG.DB_DATABASE,
port: CONFIG.DB_PORT,
})
this.drizzleDataSource = drizzle({ client: this.drizzleConnection })
}
}
public async destroy(): Promise<void> {
@ -151,3 +151,4 @@ export class AppDatabase {
}
export const getDataSource = () => AppDatabase.getInstance().getDataSource()
export const drizzleDb = () => AppDatabase.getInstance().getDrizzleDataSource()

View File

@ -1,5 +1,3 @@
import { MySql2Database } from 'drizzle-orm/mysql2'
import { AppDatabase } from '../AppDatabase'
import { LOG4JS_BASE_CATEGORY_NAME } from '../config/const'
export * from './communities'
@ -11,8 +9,4 @@ export * from './transactionLinks'
export * from './transactions'
export * from './user'
export function drizzleDb(): MySql2Database {
return AppDatabase.getInstance().getDrizzleDataSource()
}
export const LOG4JS_QUERIES_CATEGORY_NAME = `${LOG4JS_BASE_CATEGORY_NAME}.queries`

View File

@ -1,7 +1,6 @@
import { beforeEach } from 'node:test'
import { eq } from 'drizzle-orm'
import { MySql2Database } from 'drizzle-orm/mysql2'
import { AppDatabase } from '../AppDatabase'
import { AppDatabase, drizzleDb } from '../AppDatabase'
import { openaiThreadsTable } from '../schemas'
import {
dbDeleteOpenaiThread,
@ -15,7 +14,7 @@ let db: MySql2Database
beforeAll(async () => {
await appDB.init()
db = appDB.getDrizzleDataSource()
db = drizzleDb()
try {
await db.delete(openaiThreadsTable)
} catch(e) {

View File

@ -1,6 +1,6 @@
import { desc, eq } from 'drizzle-orm'
import { drizzleDb } from '../AppDatabase'
import { openaiThreadsTable } from '../schemas/drizzle.schema'
import { drizzleDb } from '.'
// TODO: replace results with valibot schema after update to typescript 5 is possible