( key: string, minTimeTyping: number, dropIfMismatch = true, )
| 351 | } |
| 352 | |
| 353 | async function createIndex( |
| 354 | key: string, |
| 355 | minTimeTyping: number, |
| 356 | dropIfMismatch = true, |
| 357 | ): Promise<void> { |
| 358 | const index = { |
| 359 | [`${key}.wpm`]: -1, |
| 360 | [`${key}.acc`]: -1, |
| 361 | [`${key}.timestamp`]: -1, |
| 362 | [`${key}.raw`]: -1, |
| 363 | [`${key}.consistency`]: -1, |
| 364 | banned: 1, |
| 365 | lbOptOut: 1, |
| 366 | needsToChangeName: 1, |
| 367 | timeTyping: 1, |
| 368 | uid: 1, |
| 369 | name: 1, |
| 370 | discordId: 1, |
| 371 | discordAvatar: 1, |
| 372 | inventory: 1, |
| 373 | premium: 1, |
| 374 | }; |
| 375 | const partial = { |
| 376 | partialFilterExpression: { |
| 377 | [`${key}.wpm`]: { |
| 378 | $gt: 0, |
| 379 | }, |
| 380 | timeTyping: { |
| 381 | $gt: minTimeTyping, |
| 382 | }, |
| 383 | }, |
| 384 | }; |
| 385 | try { |
| 386 | await getUsersCollection().createIndex(index, partial); |
| 387 | } catch (e) { |
| 388 | if (!dropIfMismatch) throw e; |
| 389 | if ( |
| 390 | (e as Error).message.startsWith( |
| 391 | "An existing index has the same name as the requested index", |
| 392 | ) |
| 393 | ) { |
| 394 | Logger.warning(`Index ${key} not matching, dropping and recreating...`); |
| 395 | |
| 396 | const existingIndex = (await getUsersCollection().listIndexes().toArray()) |
| 397 | // oxlint-disable-next-line no-unsafe-member-access |
| 398 | .map((it) => it.name as string) |
| 399 | .find((it) => it.startsWith(key)); |
| 400 | |
| 401 | if (existingIndex !== undefined && existingIndex !== null) { |
| 402 | await getUsersCollection().dropIndex(existingIndex); |
| 403 | return createIndex(key, minTimeTyping, false); |
| 404 | } else { |
| 405 | throw e; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 |
no test coverage detected