Skip to content

Commit

Permalink
fix: create index timing out
Browse files Browse the repository at this point in the history
  • Loading branch information
akshatmittal61 committed Jul 19, 2024
1 parent 5916745 commit 08c5b60
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 11 additions & 0 deletions db/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-unused-vars */
import { url } from "@/config";
import logger from "@/log";
import { UserModel } from "@/models";
import mongoose from "mongoose";

declare global {
Expand All @@ -26,10 +27,20 @@ export class DatabaseManager {
logger.info("MongoDB connected");
global.isConnected = db.connections[0].readyState === 1;
global.db = db;
await this.ensureIndexes();
} catch (error) {
logger.error("Error connecting to MongoDB", error);
}
}

async ensureIndexes() {
try {
await UserModel.createIndexes();
logger.info("MongoDB indexes created");
} catch (error) {
logger.error(error);
}
}
}

export const db = new DatabaseManager();
2 changes: 0 additions & 2 deletions models/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ UserSchema.index({ name: "text", email: "text", phone: "text" });

const UserModel = mongoose.models.User || mongoose.model("User", UserSchema);

UserModel.createIndexes();

export { UserModel };

export type User = {
Expand Down

0 comments on commit 08c5b60

Please sign in to comment.