chore: restore api test baseline and logging
This commit is contained in:
1 parent
60a006008a
commit
871a5fe78a
3 files changed
+31
-18
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { Injectable, Scope, OnModuleInit } from '@nestjs/common';
|
||||
import { Injectable, Logger, Scope, OnModuleInit } from '@nestjs/common';
|
||||
import mongoose from 'mongoose';
|
||||
import { SecretService } from '../secret/secret.service';
|
||||
import { tournamentModelDefinitions, TournamentModelTypes } from './schema';
|
||||
@@ -36,13 +36,17 @@ export function resolveMongoConnectionOptions(
|
||||
|
||||
@Injectable({ scope: Scope.DEFAULT })
|
||||
export class ConnectionService implements OnModuleInit {
|
||||
private readonly logger = new Logger(ConnectionService.name);
|
||||
private connection: mongoose.Connection | null = null;
|
||||
constructor(private readonly secretService: SecretService) {}
|
||||
|
||||
private tournamentModels: Map<string, mongoose.Model<any>> | null = null;
|
||||
|
||||
public async getConnection(): Promise<mongoose.Connection> {
|
||||
if (this.connection !== null && this.connection.readyState === 1) {
|
||||
if (
|
||||
this.connection !== null &&
|
||||
this.connection.readyState === mongoose.ConnectionStates.connected
|
||||
) {
|
||||
return this.connection;
|
||||
}
|
||||
|
||||
@@ -51,15 +55,15 @@ export class ConnectionService implements OnModuleInit {
|
||||
);
|
||||
const dbName = resolveMongoDbName();
|
||||
const appName = resolveMongoAppName();
|
||||
const connectionOptions = resolveMongoConnectionOptions(dbName, appName);
|
||||
const defaultConnection = await mongoose
|
||||
.createConnection(
|
||||
connectionString,
|
||||
resolveMongoConnectionOptions(dbName, appName),
|
||||
)
|
||||
.createConnection(connectionString, connectionOptions)
|
||||
.asPromise();
|
||||
this.connection = defaultConnection;
|
||||
console.log(
|
||||
`Default Connection이 새로 생성됨. dbName=${dbName} appName=${appName}`,
|
||||
this.logger.log(
|
||||
`MongoDB connection created. dbName=${dbName} appName=${appName} autoIndex=${
|
||||
connectionOptions.autoIndex === true
|
||||
}`,
|
||||
);
|
||||
|
||||
return this.connection;
|
||||
@@ -113,15 +117,24 @@ export class ConnectionService implements OnModuleInit {
|
||||
const session = await connection.startSession(options);
|
||||
return session as unknown as mongoose.ClientSession;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
this.logError('Failed to start MongoDB session', error);
|
||||
}
|
||||
}
|
||||
|
||||
public async onModuleInit() {
|
||||
await this.getConnection().catch(console.error);
|
||||
await this.getConnection().catch((error: unknown) => {
|
||||
this.logError('Failed to initialize MongoDB connection', error);
|
||||
});
|
||||
}
|
||||
|
||||
public async onModuleDestroy() {
|
||||
await this.connection?.close();
|
||||
}
|
||||
|
||||
private logError(message: string, error: unknown): void {
|
||||
this.logger.error(
|
||||
`${message}: ${error instanceof Error ? error.message : String(error)}`,
|
||||
error instanceof Error ? error.stack : undefined,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user