Skip to content

Commit db943f9

Browse files
author
Mishig
authored
[Assistants] Add new indices for efficient querying (#810)
* [Assistants] Add new indeices for efficient querying * update indices & use `createdById` * stronger typing
1 parent 3109a5e commit db943f9

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/lib/server/database.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ client.on("open", () => {
7575
messageEvents.createIndex({ createdAt: 1 }, { expireAfterSeconds: 60 }).catch(console.error);
7676
sessions.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 }).catch(console.error);
7777
sessions.createIndex({ sessionId: 1 }, { unique: true }).catch(console.error);
78-
assistants.createIndex({ createdBy: 1 }).catch(console.error);
78+
assistants.createIndex({ createdById: 1, userCount: -1 }).catch(console.error);
7979
assistants.createIndex({ userCount: 1 }).catch(console.error);
80-
assistants.createIndex({ featured: 1 }).catch(console.error);
80+
assistants.createIndex({ featured: 1, userCount: -1 }).catch(console.error);
81+
assistants.createIndex({ modelId: 1, userCount: -1 }).catch(console.error);
8182
reports.createIndex({ assistantId: 1 }).catch(console.error);
8283
});

src/routes/assistants/+page.server.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { base } from "$app/paths";
22
import { ENABLE_ASSISTANTS } from "$env/static/private";
33
import { collections } from "$lib/server/database.js";
44
import type { Assistant } from "$lib/types/Assistant";
5+
import type { User } from "$lib/types/User";
56
import { error, redirect } from "@sveltejs/kit";
67
import type { Filter } from "mongodb";
78

@@ -14,21 +15,25 @@ export const load = async ({ url, locals }) => {
1415

1516
const modelId = url.searchParams.get("modelId");
1617
const pageIndex = parseInt(url.searchParams.get("p") ?? "0");
17-
const createdByName = url.searchParams.get("user");
18-
const createdByCurrentUser = locals.user?.username && locals.user.username === createdByName;
18+
const username = url.searchParams.get("user");
19+
const createdByCurrentUser = locals.user?.username && locals.user.username === username;
1920

20-
if (createdByName) {
21-
const existingUser = await collections.users.findOne({ username: createdByName });
22-
if (!existingUser) {
23-
throw error(404, `User "${createdByName}" doesn't exist`);
21+
let user: Pick<User, "_id"> | null = null;
22+
if (username) {
23+
user = await collections.users.findOne<Pick<User, "_id">>(
24+
{ username },
25+
{ projection: { _id: 1 } }
26+
);
27+
if (!user) {
28+
throw error(404, `User "${username}" doesn't exist`);
2429
}
2530
}
2631

2732
// fetch the top assistants sorted by user count from biggest to smallest, filter out all assistants with only 1 users. filter by model too if modelId is provided
2833
const filter: Filter<Assistant> = {
2934
...(modelId && { modelId }),
3035
...(!createdByCurrentUser && { userCount: { $gt: 1 } }),
31-
...(createdByName ? { createdByName } : { featured: true }),
36+
...(user ? { createdById: user._id } : { featured: true }),
3237
};
3338
const assistants = await collections.assistants
3439
.find(filter)

0 commit comments

Comments
 (0)