diff --git a/apps/api/src/modules/global/database/connection.service.spec.ts b/apps/api/src/modules/global/database/connection.service.spec.ts index e48d72b0..6fd09b3f 100644 --- a/apps/api/src/modules/global/database/connection.service.spec.ts +++ b/apps/api/src/modules/global/database/connection.service.spec.ts @@ -1,4 +1,4 @@ -import { resolveMongoDbName } from './connection.service'; +import { resolveMongoAppName, resolveMongoDbName } from './connection.service'; describe('resolveMongoDbName', () => { it('uses production database only for prod', () => { @@ -12,3 +12,15 @@ describe('resolveMongoDbName', () => { expect(resolveMongoDbName('local')).toBe('lolympus-dev'); }); }); + +describe('resolveMongoAppName', () => { + it('identifies api environment and process for MongoDB connection tracking', () => { + expect(resolveMongoAppName('staging', 1234)).toBe( + 'lolympus-api:staging:1234', + ); + }); + + it('uses dev as the fallback environment label', () => { + expect(resolveMongoAppName('', 1234)).toBe('lolympus-api:dev:1234'); + }); +}); diff --git a/apps/api/src/modules/global/database/connection.service.ts b/apps/api/src/modules/global/database/connection.service.ts index 413e6568..ceab476d 100644 --- a/apps/api/src/modules/global/database/connection.service.ts +++ b/apps/api/src/modules/global/database/connection.service.ts @@ -9,6 +9,13 @@ export function resolveMongoDbName(nodeEnv = process.env.NODE_ENV): string { return 'lolympus-dev'; } +export function resolveMongoAppName( + nodeEnv = process.env.NODE_ENV, + pid = process.pid, +): string { + return `lolympus-api:${nodeEnv || 'dev'}:${pid}`; +} + @Injectable({ scope: Scope.DEFAULT }) export class ConnectionService implements OnModuleInit { private connection: mongoose.Connection | null = null; @@ -25,15 +32,19 @@ export class ConnectionService implements OnModuleInit { 'MONGODB_CONNECTION_STRING', ); const dbName = resolveMongoDbName(); + const appName = resolveMongoAppName(); const defaultConnection = await mongoose .createConnection(connectionString, { dbName, + appName, minPoolSize: 10, maxPoolSize: 20, }) .asPromise(); this.connection = defaultConnection; - console.log(`Default Connection이 새로 생성됨. dbName=${dbName}`); + console.log( + `Default Connection이 새로 생성됨. dbName=${dbName} appName=${appName}`, + ); return this.connection; }