Rana_093

MongoDB Connection Setup.

Jul 3rd, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. //database.js
  2. const mongodb = require('mongodb');
  3. const MongoClient = mongodb.MongoClient;
  4. let _db;
  5.  
  6. const mongoConnect = (callback) => {
  7. MongoClient.connect(
  8. 'mongodb+srv://RanaM:201505093@cluster0.bitxd.mongodb.net/<dbname>?retryWrites=true&w=majority'
  9. , { useUnifiedTopology: true,
  10. useNewUrlParser: true
  11. })
  12. .then( client => {
  13. console.log('Connected!');
  14. _db = client.db('test');
  15. callback(client);
  16. })
  17. .catch(err => {
  18. console.log(err);
  19. throw err;
  20. });
  21. };
  22.  
  23. const getDb = () => {
  24. if(_db){
  25. return _db;
  26. }
  27. throw 'No Database found';
  28. }
  29.  
  30. exports.mongoConnect = mongoConnect;
  31. exports.getDb = getDb;
  32.  
  33.  
  34. //app.js
  35. const express = require('express');
  36. const adminRoutes = require('./routes/admin');
  37. const shopRoutes = require('./routes/shop');
  38. const bodyParser = require('body-parser');
  39. const path = require('path');
  40. const app = express();
  41. const errorController = require('./controllers/error');
  42. const mongoConnect = require('./util/database').mongoConnect;
  43.  
  44. app.set('view engine', 'ejs');
  45. app.set('views', 'views');
  46.  
  47. app.use(bodyParser.urlencoded({ extended: false}));
  48.  
  49. app.use(express.static(path.join(__dirname, 'public')));
  50.  
  51. app.use('/admin',adminRoutes);
  52. //app.use(shopRoutes);
  53.  
  54. app.use(errorController.get404);
  55.  
  56. mongoConnect((client) => {
  57. console.log(client);
  58. app.listen(3000);
  59. });
  60.  
  61. //app.listen(3000);
  62.  
  63. ///Sample Insert into Database.!
  64.  
  65. exports.postAddProduct = (req, res, next) => {
  66. const title = req.body.title;
  67. const imageUrl = req.body.imageUrl;
  68. const price = req.body.price;
  69. const description = req.body.description;
  70. const product = new Product(title,imageUrl,description,price);
  71. product
  72. .save()
  73. .then( result => {
  74. console.log('Added Product.!');
  75. res.redirect('/admin/products');
  76. })
  77. .catch(err => {
  78. console.log(err);
  79. });
  80. };
  81.  
  82. ///Using in Model
  83. const getDb = require('../util/database').getDb;
  84.  
  85. class Product{
  86. constructor(title, price, description, imageUrl){
  87. this.title = title;
  88. this.price = price;
  89. this.description = description;
  90. this.imageUrl = imageUrl;
  91. }
  92. save(){
  93. const db = getDb();
  94. return db.collection('products')
  95. .insertOne(this)
  96. .then(result => {
  97. console.log(result);
  98. }).
  99. catch(err => {
  100. console.log(err);
  101. });
  102. }
  103.  
  104. static fetchAll(){
  105. const db = getDb();
  106. return db.collection('test')
  107. .find().toArray()
  108. .then(products => {
  109. console.log(products);
  110. return products;
  111. })
  112. .catch(err => {
  113. console.log(err);
  114. });
  115. }
  116. }
  117.  
  118. module.exports = Product;
Add Comment
Please, Sign In to add comment