-
-
Save kafkadev/deb4f9d6f295e167977e4ea2e315b535 to your computer and use it in GitHub Desktop.
Database connnection
This file contains hidden or 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 mongodb = require("mongodb") | |
| const MongoClient = mongodb.MongoClient; | |
| let _db; | |
| // Connect to the database | |
| const mongoConnect = (cb) => { | |
| MongoClient.connect( | |
| //connection string here | |
| { useUnifiedTopology: true } | |
| ) | |
| .then((result) => { | |
| console.log('MongoDB Connected'); | |
| cb(result); | |
| .then((client) => { | |
| console.log('MongoDB Connected!'); | |
| _db = client.db(); | |
| cb(); | |
| }) | |
| .catch((err) => { | |
| console.log(err); | |
| throw err; | |
| }); | |
| }; | |
| // Get connection to the database | |
| const getDb = () => { | |
| if (_db) { | |
| return _db; | |
| } | |
| throw 'No database Found!'; | |
| }; | |
| exports.mongoConnect = mongoConnect; | |
| exports.getDb = getDb; |
This file contains hidden or 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
| mongoose | |
| .connect( | |
| //connection string here | |
| , | |
| //options | |
| ) | |
| .then((result) => { | |
| app.listen(3000, () => console.log('Server connected on port 3000')); | |
| }) | |
| .catch((err) => console.log(err)); |
This file contains hidden or 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 Sequelize = require('sequelize'); | |
| const sequelize = new Sequelize('database', 'root', 'password.', { | |
| dialect: 'mysql', | |
| host: 'localhost', | |
| }); | |
| module.exports = sequelize; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment