Skip to content

Commit

Permalink
fix: mongo connection async
Browse files Browse the repository at this point in the history
  • Loading branch information
hhow09 committed Jan 22, 2025
1 parent 7f2c5bc commit e4bd654
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ const uri = process.env.MONGODB_URI || "mongodb://localhost:27017";
const dbName = "math-chat";
const collectionName = "chat-session";

const logger = pino();
const collection = getMongoCollection(uri, dbName, collectionName);
const commandService = new CommandService(logger, new ChatRepo(collection));
const chatServer = new ChatServer(logger, commandService, 3000);
chatServer.listen();

function getMongoCollection(uri: string, dbName: string, collectionName: string): Collection<ChatSession> {
async function getMongoCollection(uri: string, dbName: string, collectionName: string): Promise<Collection<ChatSession>> {
const mongoClient = new MongoClient(uri);
await mongoClient.connect();
const db = mongoClient.db(dbName);
const collection = db.collection<ChatSession>(collectionName);
return collection;
}
}

async function main() {
const logger = pino();
const collection = await getMongoCollection(uri, dbName, collectionName);
const commandService = new CommandService(logger, new ChatRepo(collection));
const chatServer = new ChatServer(logger, commandService, 3000);
chatServer.listen();
}

main();

0 comments on commit e4bd654

Please sign in to comment.