fix(api): eliminate unsafe lint warnings
This commit is contained in:
1 parent
fd1fa2366c
commit
bb68be14fa
12 files changed
+260
-123
No files matched your search
@@ -2,7 +2,10 @@ import { Injectable, Logger, Scope, OnModuleInit } from '@nestjs/common';
|
||||
import mongoose from 'mongoose';
|
||||
import { applyMongoosePerformancePlugin } from '../observability/mongoose-performance.plugin';
|
||||
import { SecretService } from '../secret/secret.service';
|
||||
import { tournamentModelDefinitions, TournamentModelTypes } from './schema';
|
||||
import {
|
||||
tournamentModelDefinitions,
|
||||
type TournamentModelTypes,
|
||||
} from './schema';
|
||||
|
||||
export function resolveMongoDbName(nodeEnv = process.env.NODE_ENV): string {
|
||||
if (nodeEnv === 'prod') return 'lolympus';
|
||||
@@ -42,7 +45,8 @@ export class ConnectionService implements OnModuleInit {
|
||||
private connectionPromise: Promise<mongoose.Connection> | null = null;
|
||||
constructor(private readonly secretService: SecretService) {}
|
||||
|
||||
private tournamentModels: Map<string, mongoose.Model<any>> | null = null;
|
||||
private tournamentModels: Map<keyof TournamentModelTypes, unknown> | null =
|
||||
null;
|
||||
|
||||
public async getConnection(): Promise<mongoose.Connection> {
|
||||
if (
|
||||
@@ -89,14 +93,18 @@ export class ConnectionService implements OnModuleInit {
|
||||
modelKey: K,
|
||||
): Promise<mongoose.Model<TournamentModelTypes[K]>> {
|
||||
const connection = await this.getConnection();
|
||||
if (this.tournamentModels)
|
||||
return this.tournamentModels.get(modelKey) as mongoose.Model<
|
||||
TournamentModelTypes[K]
|
||||
>;
|
||||
if (this.tournamentModels) {
|
||||
return this.requireTournamentModel(this.tournamentModels, modelKey);
|
||||
}
|
||||
|
||||
const models = new Map<K, mongoose.Model<TournamentModelTypes[K]>>();
|
||||
const models = new Map<keyof TournamentModelTypes, unknown>();
|
||||
|
||||
tournamentModelDefinitions.forEach((modelDefinition) => {
|
||||
if (!(modelDefinition.schema instanceof mongoose.Schema)) {
|
||||
throw new Error(
|
||||
`Invalid Mongoose schema for model ${modelDefinition.name}.`,
|
||||
);
|
||||
}
|
||||
applyMongoosePerformancePlugin(modelDefinition.schema);
|
||||
// 이미 생성된 경우 컴파일 스킵.
|
||||
if (
|
||||
@@ -106,14 +114,14 @@ export class ConnectionService implements OnModuleInit {
|
||||
)
|
||||
) {
|
||||
models.set(
|
||||
modelDefinition.name as K,
|
||||
modelDefinition.name,
|
||||
connection.models[modelDefinition.name],
|
||||
);
|
||||
return;
|
||||
} else {
|
||||
models.set(
|
||||
modelDefinition.name as K,
|
||||
connection.model<TournamentModelTypes[K]>(
|
||||
modelDefinition.name,
|
||||
connection.model(
|
||||
modelDefinition.name,
|
||||
modelDefinition.schema,
|
||||
modelDefinition.schemaName,
|
||||
@@ -123,7 +131,18 @@ export class ConnectionService implements OnModuleInit {
|
||||
});
|
||||
|
||||
this.tournamentModels = models;
|
||||
return models.get(modelKey) as mongoose.Model<TournamentModelTypes[K]>;
|
||||
return this.requireTournamentModel(models, modelKey);
|
||||
}
|
||||
|
||||
private requireTournamentModel<K extends keyof TournamentModelTypes>(
|
||||
models: ReadonlyMap<keyof TournamentModelTypes, unknown>,
|
||||
modelKey: K,
|
||||
): mongoose.Model<TournamentModelTypes[K]> {
|
||||
const model = models.get(modelKey);
|
||||
if (model === undefined) {
|
||||
throw new Error(`Mongoose model ${modelKey} is not registered.`);
|
||||
}
|
||||
return model as mongoose.Model<TournamentModelTypes[K]>;
|
||||
}
|
||||
|
||||
public async createSession(
|
||||
|
||||
Reference in new issue
Block a user