101 lines
3.0 KiB
TypeScript
101 lines
3.0 KiB
TypeScript
import { Injectable, Scope, OnModuleInit } from '@nestjs/common';
|
|
import mongoose from 'mongoose';
|
|
import { SecretService } from '../secret/secret.service';
|
|
import { tournamentModelDefinitions, TournamentModelTypes } from './schema';
|
|
|
|
export function resolveMongoDbName(nodeEnv = process.env.NODE_ENV): string {
|
|
if (nodeEnv === 'prod') return 'lolympus';
|
|
if (nodeEnv === 'staging') return 'lolympus-staging';
|
|
return 'lolympus-dev';
|
|
}
|
|
|
|
@Injectable({ scope: Scope.DEFAULT })
|
|
export class ConnectionService implements OnModuleInit {
|
|
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) {
|
|
return this.connection;
|
|
}
|
|
|
|
const connectionString = await this.secretService.get(
|
|
'MONGODB_CONNECTION_STRING',
|
|
);
|
|
const dbName = resolveMongoDbName();
|
|
const defaultConnection = await mongoose
|
|
.createConnection(connectionString, {
|
|
dbName,
|
|
minPoolSize: 10,
|
|
maxPoolSize: 20,
|
|
})
|
|
.asPromise();
|
|
this.connection = defaultConnection;
|
|
console.log(`Default Connection이 새로 생성됨. dbName=${dbName}`);
|
|
|
|
return this.connection;
|
|
}
|
|
|
|
public async getModel<K extends keyof TournamentModelTypes>(
|
|
modelKey: K,
|
|
): Promise<mongoose.Model<TournamentModelTypes[K]>> {
|
|
if (this.tournamentModels)
|
|
return this.tournamentModels.get(modelKey) as mongoose.Model<
|
|
TournamentModelTypes[K]
|
|
>;
|
|
|
|
const models = new Map<K, mongoose.Model<TournamentModelTypes[K]>>();
|
|
const connection = await this.getConnection();
|
|
|
|
tournamentModelDefinitions.forEach((modelDefinition) => {
|
|
// 이미 생성된 경우 컴파일 스킵.
|
|
if (
|
|
Object.prototype.hasOwnProperty.call(
|
|
connection.models,
|
|
modelDefinition.name,
|
|
)
|
|
) {
|
|
models.set(
|
|
modelDefinition.name as K,
|
|
connection.models[modelDefinition.name],
|
|
);
|
|
return;
|
|
} else {
|
|
models.set(
|
|
modelDefinition.name as K,
|
|
connection.model<TournamentModelTypes[K]>(
|
|
modelDefinition.name,
|
|
modelDefinition.schema,
|
|
modelDefinition.schemaName,
|
|
),
|
|
);
|
|
}
|
|
});
|
|
|
|
this.tournamentModels = models;
|
|
return models.get(modelKey) as mongoose.Model<TournamentModelTypes[K]>;
|
|
}
|
|
|
|
public async createSession(
|
|
options?: mongoose.ClientSessionOptions,
|
|
): Promise<mongoose.ClientSession | undefined> {
|
|
try {
|
|
const connection = await this.getConnection();
|
|
const session = await connection.startSession(options);
|
|
return session as unknown as mongoose.ClientSession;
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
public async onModuleInit() {
|
|
await this.getConnection().catch(console.error);
|
|
}
|
|
|
|
public async onModuleDestroy() {
|
|
this.connection?.close();
|
|
}
|
|
}
|