Skip to content

Commit

Permalink
chore : connect to database via Hajar
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansourkira committed Mar 20, 2024
1 parent 1d20719 commit fde4b9e
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions www/src/@sikka/hajar/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ import {
} from "./auth/index.js";

class Hajar {
async init(url, secrets, mongooseInstance) {
if (this.initialized) {
throw new Error("🚫 Hajar is already initialized");
}
console.log("βŒ› Starting the database connection process...");
try {
await mongooseInstance.connect(url);
mongooseInstance.connection.once("open", () => {
console.log("βœ…πŸ”— Connected to the database successfully!");
});
mongooseInstance.connection.on("error", (error) => {
console.error("βŒπŸ”— Error connecting to the database:", error);
throw error;
});

this.config = {
accessToken: secrets.accessToken,
refreshToken: secrets.refreshToken,
mongoose: mongooseInstance,
};

this.initialized = true;
console.log("βœ…πŸš€ Hajar initialized successfully.");
} catch (error) {
console.error("βŒπŸ”— Attempt to connect to the database failed: ", error);
throw error;
}
}
constructor() {
this.config = null;
this.initialized = false;
Expand Down Expand Up @@ -36,18 +64,6 @@ class Hajar {
},
};
}
initHajar(jwtSecret, refreshToken, mongooseInstance) {
if (this.initialized) {
throw new Error("Hajar is already initialized");
}
this.config = {
secret: jwtSecret,
refreshTokenSecret: refreshToken,
mongoose: mongooseInstance,
};
this.initialized = true;
console.log("Hajar initialized successfully.");
}
}

export default new Hajar();

0 comments on commit fde4b9e

Please sign in to comment.