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(
|
||||
|
||||
@@ -253,9 +253,7 @@ function migrateResult(result: Record<string, unknown>) {
|
||||
seriesResults.length > 0 ? seriesResults : buildSeries(games);
|
||||
|
||||
if (typeof migrated.winnerTeamIndex !== 'number') {
|
||||
const migratedSeriesResults = Array.isArray(migrated.seriesResults)
|
||||
? migrated.seriesResults
|
||||
: [];
|
||||
const migratedSeriesResults = asArray(migrated.seriesResults);
|
||||
migrated.winnerTeamIndex =
|
||||
resolveWinnerTeamIndex(migratedSeriesResults) ??
|
||||
(isSide(migrated.winnerSide)
|
||||
|
||||
+3
-1
@@ -117,7 +117,9 @@ function createDb(params: {
|
||||
function matchesFilter(doc: Doc, filter: Record<string, unknown>): boolean {
|
||||
const clauses = filter.$or;
|
||||
if (Array.isArray(clauses)) {
|
||||
return clauses.some((clause) => matchesFilter(doc, clause));
|
||||
return (clauses as unknown[]).some(
|
||||
(clause) => isRecord(clause) && matchesFilter(doc, clause),
|
||||
);
|
||||
}
|
||||
|
||||
return Object.entries(filter).every(([key, value]) => {
|
||||
|
||||
+6
-2
@@ -11,7 +11,11 @@ import {
|
||||
} from '../../../double-elimination/double-elimination.util';
|
||||
import { TOURNAMENT_CODE_EXPIRY_MARGIN_MS } from '../../../tournament-code/tournament-code.constants';
|
||||
import { ConnectionService } from '../connection.service';
|
||||
import type { MatchDocument, TournamentCodeDocument } from '../schema';
|
||||
import type {
|
||||
MatchDocument,
|
||||
TournamentCode,
|
||||
TournamentCodeDocument,
|
||||
} from '../schema';
|
||||
import { SecretModule } from '../../secret/secret.module';
|
||||
|
||||
interface CliOptions {
|
||||
@@ -334,7 +338,7 @@ function printPlan(plan: MatchRepairPlan) {
|
||||
async function applyPlan(
|
||||
match: MatchDocument,
|
||||
plan: MatchRepairPlan,
|
||||
TournamentCodeModel: mongoose.Model<any>,
|
||||
TournamentCodeModel: mongoose.Model<TournamentCode>,
|
||||
session: mongoose.ClientSession,
|
||||
) {
|
||||
if (plan.skippedReason) return;
|
||||
|
||||
@@ -2485,7 +2485,9 @@ MatchStatsSchema.index({ communityId: 1, userIds: 1 });
|
||||
MatchStatsSchema.index({ communityId: 1, userIds: 1, scheduledAt: -1 });
|
||||
MatchStatsSchema.index({ communityId: 1, championIds: 1 });
|
||||
|
||||
export const tournamentModelDefinitions: ModelDefinition[] = [
|
||||
export const tournamentModelDefinitions: Array<
|
||||
ModelDefinition & { name: keyof TournamentModelTypes }
|
||||
> = [
|
||||
...competitionModelDefinitions,
|
||||
{ name: 'identity', schema: IdentitySchema, schemaName: 'identity' },
|
||||
{ name: 'community', schema: CommunitySchema, schemaName: 'community' },
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
export interface ModelDefinition {
|
||||
name: string;
|
||||
schema: mongoose.Schema<any>;
|
||||
schema: unknown;
|
||||
schemaName?: string;
|
||||
}
|
||||
@@ -34,10 +34,13 @@ type SchemaWithPerformanceMarker = mongoose.Schema & {
|
||||
[PERFORMANCE_PLUGIN_APPLIED]?: boolean;
|
||||
};
|
||||
|
||||
export function applyMongoosePerformancePlugin(schema: mongoose.Schema): void {
|
||||
const markedSchema = schema as SchemaWithPerformanceMarker;
|
||||
if (markedSchema[PERFORMANCE_PLUGIN_APPLIED]) return;
|
||||
markedSchema[PERFORMANCE_PLUGIN_APPLIED] = true;
|
||||
export function applyMongoosePerformancePlugin(schemaValue: unknown): void {
|
||||
if (!(schemaValue instanceof mongoose.Schema)) {
|
||||
throw new TypeError('Mongoose performance plugin requires a Schema.');
|
||||
}
|
||||
const schema = schemaValue as SchemaWithPerformanceMarker;
|
||||
if (schema[PERFORMANCE_PLUGIN_APPLIED]) return;
|
||||
schema[PERFORMANCE_PLUGIN_APPLIED] = true;
|
||||
|
||||
schema.pre(QUERY_OPERATION_REGEX, function () {
|
||||
const query = this as QueryWithPerformanceState;
|
||||
|
||||
Reference in new issue
Block a user