1+ import { UserDBConnector } from './user-db.connector' ;
12import { Injectable } from '@nestjs/common' ;
2- import { ConfigService } from '@nestjs/config' ;
3- import { Connection , createConnection } from 'mysql2/promise' ;
3+ import * as console from 'node:console' ;
44
55@Injectable ( )
6- export class UserDBConnectionService {
7- private connectionMap = new Map < string , Connection > ( ) ;
8-
9- constructor ( private readonly configService : ConfigService ) { }
10-
11- private async getConnection ( pod : string ) {
12- if ( ! this . connectionMap . has ( pod ) ) {
13- const connection = await this . createConnectionByPod ( pod ) ;
14- this . connectionMap . set ( pod , connection ) ;
15- }
16- return this . connectionMap . get ( pod ) ;
17- }
18-
19- private async createConnectionByPod ( pod : string ) {
20- return createConnection ( {
21- host : pod ,
22- user : this . configService . get < string > ( 'QUERY_DB_USER' ) ,
23- password : this . configService . get < string > ( 'QUERY_DB_PASSWORD' ) ,
24- port : this . configService . get < number > ( 'QUERY_DB_PORT' ) ,
25- } ) ;
26- }
6+ export class UserDBService {
7+ constructor ( private readonly userDBConnector : UserDBConnector ) { }
278
289 public async initUserDatabase ( pod : string , sessionId : string ) {
2910 try {
@@ -35,7 +16,7 @@ export class UserDBConnectionService {
3516 database : identify ,
3617 } ;
3718
38- const connection = await this . getConnection ( pod ) ;
19+ const connection = await this . userDBConnector . getConnection ( pod ) ;
3920
4021 await connection . query ( `create database ${ connectInfo . database } ;` ) ;
4122 await connection . query (
@@ -51,7 +32,7 @@ export class UserDBConnectionService {
5132
5233 public async removeDatabase ( pod : string , sessionId : string ) {
5334 try {
54- const connection = await this . getConnection ( pod ) ;
35+ const connection = await this . userDBConnector . getConnection ( pod ) ;
5536
5637 const identify = sessionId . substring ( 0 , 10 ) ;
5738
@@ -63,4 +44,4 @@ export class UserDBConnectionService {
6344 console . error ( e ) ;
6445 }
6546 }
66- }
47+ }
0 commit comments