Skip to content

Commit cd9f246

Browse files
Mishigcoyotte508
andauthored
[Mongo] optimizations (#859)
* [Mongo] remove duplicated assistants query * Update src/routes/+layout.server.ts Co-authored-by: Eliott C. <coyotte508@gmail.com> * fix avatarHash problem * limit convo to last 300 --------- Co-authored-by: Eliott C. <coyotte508@gmail.com>
1 parent c8e89ef commit cd9f246

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

src/routes/+layout.server.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
7373
createdAt: 1,
7474
assistantId: 1,
7575
})
76+
.limit(300)
7677
.toArray();
7778

78-
const assistantIds = conversations
79-
.map((conv) => conv.assistantId)
80-
.filter((el) => !!el) as ObjectId[];
79+
const assistantIds = [
80+
...(settings?.assistants?.map((assistantId) => assistantId) ?? []),
81+
...(conversations.map((conv) => conv.assistantId).filter((el) => !!el) as ObjectId[]),
82+
];
8183

8284
const assistants = await collections.assistants.find({ _id: { $in: assistantIds } }).toArray();
8385

@@ -160,6 +162,12 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
160162
unlisted: model.unlisted,
161163
})),
162164
oldModels,
165+
assistants: assistants.map((el) => ({
166+
...el,
167+
_id: el._id.toString(),
168+
createdById: undefined,
169+
createdByMe: el.createdById.toString() === (locals.user?._id ?? locals.sessionId).toString(),
170+
})),
163171
user: locals.user && {
164172
id: locals.user._id.toString(),
165173
username: locals.user.username,

src/routes/settings/+layout.server.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import { collections } from "$lib/server/database";
2-
import { ObjectId } from "mongodb";
32
import type { LayoutServerLoad } from "./$types";
43
import type { Report } from "$lib/types/Report";
54

65
export const load = (async ({ locals, parent }) => {
7-
const { settings } = await parent();
8-
9-
// find assistants matching the settings assistants
10-
const assistants = await collections.assistants
11-
.find({
12-
_id: { $in: settings.assistants.map((el) => new ObjectId(el)) },
13-
})
14-
.toArray();
6+
const { assistants } = await parent();
157

168
let reportsByUser: string[] = [];
179
const createdBy = locals.user?._id ?? locals.sessionId;
@@ -25,10 +17,7 @@ export const load = (async ({ locals, parent }) => {
2517
return {
2618
assistants: assistants.map((el) => ({
2719
...el,
28-
_id: el._id.toString(),
29-
createdById: undefined,
30-
createdByMe: el.createdById.toString() === (locals.user?._id ?? locals.sessionId).toString(),
31-
reported: reportsByUser.includes(el._id.toString()),
20+
reported: reportsByUser.includes(el._id),
3221
})),
3322
};
3423
}) satisfies LayoutServerLoad;

0 commit comments

Comments
 (0)