Last active
October 2, 2024 08:52
-
-
Save hackinf0/4f97a6a7e820cb612e0ff5c39139d08f to your computer and use it in GitHub Desktop.
Connecting Multiple Database in NodeJs with MongoDB and Mongoose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Connecting Multiple Database in NodeJs with MongoDB and Mongoose. | |
Mongoose doesn't allow to use multiple databases in single mongoose instance as the models are build on one connection. | |
While using multiple mongoose instances, Node.js will not allow multiple module instances as it has caching system in require(). | |
we've tried to use createConnection() and openSet() in mongoose, but it was not the solution. So we create all from start to finish. | |
Here you can get the solution | |
Explanation file: model.js | |
The following code defines the data models to be used with each of the databases by associating them with a | |
previously defined schema (userSchema and qrSchema). | |
Data models are exported for use elsewhere in the code. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Explanation file: db.js | |
This code imports the mongoose library which allows to connect to a MongoDB database. | |
There are two URIs defined to connect to two different databases: MONGO_URI and MOBILE_URI. | |
Each URI contains connection information to a remote MongoDB database, including user credentials. | |
The connectDBs function uses the mongoose.createConnection method to create connections to these two | |
databases using the respective URIs. The useUnifiedTopology and useNewUrlParser options are set to enable the new | |
unified topology policy and use MongoDB's new URL parser, respectively. | |
The function returns an object that contains the connections to these two databases as qrCodeDb and userDB properties. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose') | |
const MONGO_URI = | |
'mongodb+srv://owlhacking:[email protected]/qrCodeData?retryWrites=true&w=majority' | |
const MOBILE_URI = | |
'mongodb+srv://owlhacking:[email protected]/Cluster0?retryWrites=true&w=majority' | |
const connectDBs = () => { | |
try { | |
const qrCodeDb = mongoose.createConnection(MONGO_URI, { | |
useUnifiedTopology: true, | |
useNewUrlParser: true | |
}) | |
const userDB = mongoose.createConnection(MOBILE_URI, { | |
useUnifiedTopology: true, | |
useNewUrlParser: true | |
}) | |
return { qrCodeDb, userDB } | |
} catch (error) { | |
console.error(`Error:${error.message}`) | |
process.exit(1) | |
} | |
} | |
module.exports = { connectDBs } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { default: mongoose } = require('mongoose') | |
const { connectDBs } = require('../config/db') | |
const qrSchema = mongoose.Schema({ | |
qrInformation: { | |
type: String, | |
required: true | |
}, | |
qrImage: { | |
type: String, | |
required: true | |
}, | |
timeStamp: { | |
type: String, | |
default: Date() | |
} | |
}) | |
const userSchema = new mongoose.Schema({ | |
fullname: { | |
type: String, | |
required: true | |
}, | |
email: { | |
type: String, | |
required: true, | |
unique: true | |
}, | |
password: { | |
type: String, | |
required: true | |
} | |
}) | |
const { qrCodeDb, userDB } = connectDBs() | |
module.exports = { | |
userSchema: userDB.model('user', userSchema), | |
Qrcode: qrCodeDb.model('Qrcode', qrSchema) | |
} |
The above trick really work
Thanks :)
Useful, thanks!
You’re welcome😇
The above trick really work Thanks :)
Glad that it has helped you😊
Useful; works, danke.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful, thanks!