Skip to content

Commit f587c60

Browse files
committed
feat(db): centralize Mongo access via db service; refactor runtime to use it
- Add src/lib/server/db.ts with helpers for conversations, shared, messageEvents, settings, users, sessions, tokenCache, config, reports, assistants, files, abortedGenerations - Refactor runtime routes/libs to use db service instead of direct collections access - Keep migrations/tests on collections for now - No user-facing behavior changes; structural refactor only
1 parent a550b4e commit f587c60

File tree

32 files changed

+555
-431
lines changed

32 files changed

+555
-431
lines changed

src/hooks.server.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { config, ready } from "$lib/server/config";
22
import type { Handle, HandleServerError, ServerInit, HandleFetch } from "@sveltejs/kit";
3-
import { collections } from "$lib/server/database";
3+
import { db } from "$lib/server/db";
44
import { base } from "$app/paths";
55
import { authenticateRequest, refreshSessionCookie, requiresUser } from "$lib/server/auth";
66
import { ERROR_MESSAGES } from "$lib/stores/errors";
@@ -168,10 +168,10 @@ export const handle: Handle = async ({ event, resolve }) => {
168168
// if the request is a POST request or login-related we refresh the cookie
169169
refreshSessionCookie(event.cookies, auth.secretSessionId);
170170

171-
await collections.sessions.updateOne(
172-
{ sessionId: auth.sessionId },
173-
{ $set: { updatedAt: new Date(), expiresAt: addWeeks(new Date(), 2) } }
174-
);
171+
await db.sessions.updateBySessionId(auth.sessionId, {
172+
updatedAt: new Date(),
173+
expiresAt: addWeeks(new Date(), 2),
174+
});
175175
}
176176

177177
if (
@@ -196,10 +196,7 @@ export const handle: Handle = async ({ event, resolve }) => {
196196
!event.url.pathname.startsWith(`${base}/settings`) &&
197197
config.PUBLIC_APP_DISCLAIMER === "1"
198198
) {
199-
const hasAcceptedEthicsModal = await collections.settings.countDocuments({
200-
sessionId: event.locals.sessionId,
201-
ethicsModalAcceptedAt: { $exists: true },
202-
});
199+
const hasAcceptedEthicsModal = await db.settings.hasAcceptedEthicsModal(event.locals.sessionId);
203200

204201
if (!hasAcceptedEthicsModal) {
205202
return errorResponse(405, "You need to accept the welcome modal first");

src/lib/jobs/refresh-conversation-stats.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { ConversationStats } from "$lib/types/ConversationStats";
2-
import { CONVERSATION_STATS_COLLECTION, collections } from "$lib/server/database";
2+
import { CONVERSATION_STATS_COLLECTION, db } from "$lib/server/db";
33
import { logger } from "$lib/server/logger";
44
import type { ObjectId } from "mongodb";
55
import { acquireLock, refreshLock } from "$lib/migrations/lock";
66
import { Semaphores } from "$lib/types/Semaphore";
77

88
async function getLastComputationTime(): Promise<Date> {
9-
const lastStats = await collections.conversationStats.findOne({}, { sort: { "date.at": -1 } });
9+
const lastStats = await (await db.raw()).conversationStats.findOne({}, { sort: { "date.at": -1 } });
1010
return lastStats?.date?.at || new Date(0);
1111
}
1212

@@ -33,7 +33,7 @@ async function computeStats(params: {
3333
span: ConversationStats["date"]["span"];
3434
type: ConversationStats["type"];
3535
}) {
36-
const lastComputed = await collections.conversationStats.findOne(
36+
const lastComputed = await (await db.raw()).conversationStats.findOne(
3737
{ "date.field": params.dateField, "date.span": params.span, type: params.type },
3838
{ sort: { "date.at": -1 } }
3939
);
@@ -227,7 +227,7 @@ async function computeStats(params: {
227227
},
228228
];
229229

230-
await collections.conversations.aggregate(pipeline, { allowDiskUse: true }).next();
230+
await (await db.raw()).conversations.aggregate(pipeline, { allowDiskUse: true }).next();
231231

232232
logger.debug(
233233
{ minDate, dateField: params.dateField, span: params.span, type: params.type },

src/lib/migrations/lock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { collections } from "$lib/server/database";
1+
import { asCollections as collections } from "$lib/server/db";
22
import { ObjectId } from "mongodb";
33
import type { Semaphores } from "$lib/types/Semaphore";
44

src/lib/migrations/routines/01-update-search-assistants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Migration } from ".";
2-
import { collections } from "$lib/server/database";
2+
import { asCollections as collections } from "$lib/server/db";
33
import { ObjectId, type AnyBulkWriteOperation } from "mongodb";
44
import type { Assistant } from "$lib/types/Assistant";
55
import { generateSearchTokens } from "$lib/utils/searchTokens";

src/lib/migrations/routines/02-update-assistants-models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Migration } from ".";
2-
import { collections } from "$lib/server/database";
2+
import { asCollections as collections } from "$lib/server/db";
33
import { ObjectId } from "mongodb";
44

55
const updateAssistantsModels: Migration = {

src/lib/migrations/routines/04-update-message-updates.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Migration } from ".";
2-
import { collections } from "$lib/server/database";
2+
import { asCollections as collections } from "$lib/server/db";
33
import { ObjectId, type WithId } from "mongodb";
44
import type { Conversation } from "$lib/types/Conversation";
55
// Simple type to replace removed WebSearchSource for migration compatibility

src/lib/migrations/routines/05-update-message-files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ObjectId, type WithId } from "mongodb";
2-
import { collections } from "$lib/server/database";
2+
import { asCollections as collections } from "$lib/server/db";
33

44
import type { Migration } from ".";
55
import type { Conversation } from "$lib/types/Conversation";

src/lib/migrations/routines/06-trim-message-updates.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Migration } from ".";
2-
import { collections } from "$lib/server/database";
2+
import { asCollections as collections } from "$lib/server/db";
33
import { ObjectId, type WithId } from "mongodb";
44
import type { Conversation } from "$lib/types/Conversation";
55
import { MessageUpdateType, type MessageUpdate } from "$lib/types/MessageUpdate";

src/lib/migrations/routines/08-update-featured-to-review.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Migration } from ".";
2-
import { collections } from "$lib/server/database";
2+
import { asCollections as collections } from "$lib/server/db";
33
import { ObjectId } from "mongodb";
44
import { ReviewStatus } from "$lib/types/Review";
55

src/lib/migrations/routines/10-update-reports-assistantid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { collections } from "$lib/server/database";
1+
import { asCollections as collections } from "$lib/server/db";
22
import type { Migration } from ".";
33
import { ObjectId } from "mongodb";
44

0 commit comments

Comments
 (0)