Skip to content

Commit d9887cf

Browse files
authored
refactor: use findOne instead of limit(1) (@fehmer) (monkeytypegame#6924)
1 parent 177eb59 commit d9887cf

File tree

5 files changed

+87
-104
lines changed

5 files changed

+87
-104
lines changed

backend/src/api/controllers/dev.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,15 @@ async function updateUser(uid: string): Promise<void> {
211211
);
212212

213213
for (const mode of modes) {
214-
const best = (
215-
await ResultDal.getResultCollection()
216-
.find({
217-
uid,
218-
language: mode.language,
219-
mode: mode.mode,
220-
mode2: mode.mode2,
221-
})
222-
.sort({ wpm: -1, timestamp: 1 })
223-
.limit(1)
224-
.toArray()
225-
)[0] as DBResult;
214+
const best = (await ResultDal.getResultCollection().findOne(
215+
{
216+
uid,
217+
language: mode.language,
218+
mode: mode.mode,
219+
mode2: mode.mode2,
220+
},
221+
{ sort: { wpm: -1, timestamp: 1 } }
222+
)) as DBResult;
226223

227224
if (personalBests[mode.mode] === undefined) personalBests[mode.mode] = {};
228225
if (personalBests[mode.mode][mode.mode2] === undefined)

backend/src/dal/result.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,25 @@ export async function getResult(uid: string, id: string): Promise<DBResult> {
7070
}
7171

7272
export async function getLastResult(uid: string): Promise<DBResult> {
73-
const [lastResult] = await getResultCollection()
74-
.find({ uid })
75-
.sort({ timestamp: -1 })
76-
.limit(1)
77-
.toArray();
78-
if (!lastResult) throw new MonkeyError(404, "No last result found");
73+
const lastResult = await getResultCollection().findOne(
74+
{ uid },
75+
{ sort: { timestamp: -1 } }
76+
);
77+
78+
if (lastResult === null) throw new MonkeyError(404, "No last result found");
7979
return convert(lastResult);
8080
}
8181

8282
export async function getLastResultTimestamp(uid: string): Promise<number> {
83-
const [lastResult] = await getResultCollection()
84-
.find({ uid }, { projection: { timestamp: 1, _id: 0 } })
85-
.sort({ timestamp: -1 })
86-
.limit(1)
87-
.toArray();
88-
if (!lastResult) throw new MonkeyError(404, "No last result found");
83+
const lastResult = await getResultCollection().findOne(
84+
{ uid },
85+
{
86+
projection: { timestamp: 1, _id: 0 },
87+
sort: { timestamp: -1 },
88+
}
89+
);
90+
91+
if (lastResult === null) throw new MonkeyError(404, "No last result found");
8992
return lastResult.timestamp;
9093
}
9194

backend/src/dal/user.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,12 @@ export async function getPartialUser<K extends keyof DBUser>(
265265
}
266266

267267
export async function findByName(name: string): Promise<DBUser | undefined> {
268-
return (
269-
await getUsersCollection()
270-
.find({ name })
271-
.collation({ locale: "en", strength: 1 })
272-
.limit(1)
273-
.toArray()
274-
)[0];
268+
const found = await getUsersCollection().findOne(
269+
{ name },
270+
{ collation: { locale: "en", strength: 1 } }
271+
);
272+
273+
return found !== null ? found : undefined;
275274
}
276275

277276
export async function isNameAvailable(

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"@commitlint/cli": "17.7.1",
6868
"@commitlint/config-conventional": "19.2.2",
6969
"@monkeytype/release": "workspace:*",
70+
"@vitest/coverage-v8": "3.2.4",
7071
"conventional-changelog": "6.0.0",
7172
"eslint": "8.57.1",
7273
"husky": "8.0.1",

0 commit comments

Comments
 (0)