Spaces:
Sleeping
Sleeping
fix: starting chat-ui with no .env.local
Browse files- src/lib/server/config.ts +2 -5
- src/lib/server/database.ts +18 -22
src/lib/server/config.ts
CHANGED
|
@@ -26,11 +26,8 @@ class ConfigManager {
|
|
| 26 |
return;
|
| 27 |
}
|
| 28 |
|
| 29 |
-
const {
|
| 30 |
-
await
|
| 31 |
-
if (!collections) {
|
| 32 |
-
throw new Error("Database not initialized");
|
| 33 |
-
}
|
| 34 |
|
| 35 |
this.configCollection = collections.config;
|
| 36 |
this.semaphoreCollection = collections.semaphores;
|
|
|
|
| 26 |
return;
|
| 27 |
}
|
| 28 |
|
| 29 |
+
const { getCollectionsEarly } = await import("./database");
|
| 30 |
+
const collections = await getCollectionsEarly();
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
this.configCollection = collections.config;
|
| 33 |
this.semaphoreCollection = collections.semaphores;
|
src/lib/server/database.ts
CHANGED
|
@@ -66,12 +66,14 @@ export class Database {
|
|
| 66 |
});
|
| 67 |
}
|
| 68 |
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
logger.error(err, "Connection error");
|
| 71 |
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());
|
| 75 |
|
| 76 |
// Disconnect DB on exit
|
| 77 |
onExit(async () => {
|
|
@@ -198,22 +200,6 @@ export class Database {
|
|
| 198 |
conversations
|
| 199 |
.createIndex({ "messages.createdAt": 1 }, { sparse: true })
|
| 200 |
.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 |
-
|
| 217 |
// Unique index for stats
|
| 218 |
conversationStats
|
| 219 |
.createIndex(
|
|
@@ -311,9 +297,19 @@ export let collections: ReturnType<typeof Database.prototype.getCollections>;
|
|
| 311 |
|
| 312 |
export const ready = (async () => {
|
| 313 |
if (!building) {
|
| 314 |
-
await Database.getInstance();
|
| 315 |
-
collections =
|
| 316 |
} else {
|
| 317 |
collections = {} as unknown as ReturnType<typeof Database.prototype.getCollections>;
|
| 318 |
}
|
| 319 |
})();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
});
|
| 67 |
}
|
| 68 |
|
| 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) {
|
| 74 |
logger.error(err, "Connection error");
|
| 75 |
process.exit(1);
|
| 76 |
+
}
|
|
|
|
|
|
|
| 77 |
|
| 78 |
// Disconnect DB on exit
|
| 79 |
onExit(async () => {
|
|
|
|
| 200 |
conversations
|
| 201 |
.createIndex({ "messages.createdAt": 1 }, { sparse: true })
|
| 202 |
.catch((e) => logger.error(e));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
// Unique index for stats
|
| 204 |
conversationStats
|
| 205 |
.createIndex(
|
|
|
|
| 297 |
|
| 298 |
export const ready = (async () => {
|
| 299 |
if (!building) {
|
| 300 |
+
const db = await Database.getInstance();
|
| 301 |
+
collections = db.getCollections();
|
| 302 |
} else {
|
| 303 |
collections = {} as unknown as ReturnType<typeof Database.prototype.getCollections>;
|
| 304 |
}
|
| 305 |
})();
|
| 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 |
+
}
|