chore: identify api mongo connections

This commit is contained in:
dahyeon committed 2026-06-26 09:56:58 -07:00
1 parent 51ea9c42d3
commit 4ab5722759
2 files changed
+25 -2

No files matched your search

@@ -1,4 +1,4 @@
import { resolveMongoDbName } from './connection.service'; import { resolveMongoAppName, resolveMongoDbName } from './connection.service';
describe('resolveMongoDbName', () => { describe('resolveMongoDbName', () => {
it('uses production database only for prod', () => { it('uses production database only for prod', () => {
@@ -12,3 +12,15 @@ describe('resolveMongoDbName', () => {
expect(resolveMongoDbName('local')).toBe('lolympus-dev'); 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');
});
});
@@ -9,6 +9,13 @@ export function resolveMongoDbName(nodeEnv = process.env.NODE_ENV): string {
return 'lolympus-dev'; 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 }) @Injectable({ scope: Scope.DEFAULT })
export class ConnectionService implements OnModuleInit { export class ConnectionService implements OnModuleInit {
private connection: mongoose.Connection | null = null; private connection: mongoose.Connection | null = null;
@@ -25,15 +32,19 @@ export class ConnectionService implements OnModuleInit {
'MONGODB_CONNECTION_STRING', 'MONGODB_CONNECTION_STRING',
); );
const dbName = resolveMongoDbName(); const dbName = resolveMongoDbName();
const appName = resolveMongoAppName();
const defaultConnection = await mongoose const defaultConnection = await mongoose
.createConnection(connectionString, { .createConnection(connectionString, {
dbName, dbName,
appName,
minPoolSize: 10, minPoolSize: 10,
maxPoolSize: 20, maxPoolSize: 20,
}) })
.asPromise(); .asPromise();
this.connection = defaultConnection; this.connection = defaultConnection;
console.log(`Default Connection이 새로 생성됨. dbName=${dbName}`); console.log(
`Default Connection이 새로 생성됨. dbName=${dbName} appName=${appName}`,
);
return this.connection; return this.connection;
} }