Skip to content

Commit 8d0c91d

Browse files
committed
fix: starting chat-ui with no .env.local
1 parent a0f4112 commit 8d0c91d

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

src/lib/server/config.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ class ConfigManager {
2626
return;
2727
}
2828

29-
const { collections, ready } = await import("./database");
30-
await ready;
31-
if (!collections) {
32-
throw new Error("Database not initialized");
33-
}
29+
const { getCollectionsEarly } = await import("./database");
30+
const collections = await getCollectionsEarly();
3431

3532
this.configCollection = collections.config;
3633
this.semaphoreCollection = collections.semaphores;

src/lib/server/database.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,14 @@ export class Database {
6666
});
6767
}
6868

69-
this.client.connect().catch((err) => {
69+
try {
70+
await this.client.connect();
71+
this.client.db(config.MONGODB_DB_NAME + (import.meta.env.MODE === "test" ? "-test" : ""));
72+
this.client.on("open", () => this.initDatabase());
73+
} catch (err) {
7074
logger.error(err, "Connection error");
7175
process.exit(1);
72-
});
73-
this.client.db(config.MONGODB_DB_NAME + (import.meta.env.MODE === "test" ? "-test" : ""));
74-
this.client.on("open", () => this.initDatabase());
76+
}
7577

7678
// Disconnect DB on exit
7779
onExit(async () => {
@@ -198,22 +200,6 @@ export class Database {
198200
conversations
199201
.createIndex({ "messages.createdAt": 1 }, { sparse: true })
200202
.catch((e) => logger.error(e));
201-
202-
// Text index for searching conversation titles and message content
203-
conversations
204-
.createIndex(
205-
{
206-
userId: 1,
207-
sessionId: 1,
208-
title: "text",
209-
"messages.content": "text",
210-
},
211-
{
212-
default_language: "en",
213-
}
214-
)
215-
.catch((e) => logger.error(e));
216-
217203
// Unique index for stats
218204
conversationStats
219205
.createIndex(
@@ -311,9 +297,19 @@ export let collections: ReturnType<typeof Database.prototype.getCollections>;
311297

312298
export const ready = (async () => {
313299
if (!building) {
314-
await Database.getInstance();
315-
collections = await Database.getInstance().then((db) => db.getCollections());
300+
const db = await Database.getInstance();
301+
collections = db.getCollections();
316302
} else {
317303
collections = {} as unknown as ReturnType<typeof Database.prototype.getCollections>;
318304
}
319305
})();
306+
307+
export async function getCollectionsEarly(): Promise<
308+
ReturnType<typeof Database.prototype.getCollections>
309+
> {
310+
await ready;
311+
if (!collections) {
312+
throw new Error("Database not initialized");
313+
}
314+
return collections;
315+
}

0 commit comments

Comments
 (0)