Advertisement
Guest User

mongodb lib

a guest
Mar 21st, 2018
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mongodb = require('mongodb');
  2. const MongoClient = mongodb.MongoClient;
  3.  
  4. const config = require('../config');
  5.  
  6. const url = config.connectionString;
  7.  
  8. let connection;
  9. const getConnection = () => connection;
  10.  
  11. let users;
  12. let log;
  13.  
  14. module.exports = {
  15.     getConnection: getConnection,
  16.     closeConnection: () => {
  17.         if (connection) {
  18.             connection.close();
  19.         }
  20.     },
  21.     init: () => {
  22.         process.once('SIGUSR2', () => {
  23.             connection.close(() => {
  24.                 process.kill(process.pid, 'SIGUSR2');
  25.             });
  26.         });
  27.  
  28.         return MongoClient
  29.             .connect(url)
  30.             .then((conn) => {
  31.                 console.log('Connected to MongoDB');
  32.                 connection = conn;
  33.                 users = conn.collection('users');
  34.                 log = conn.collection('log');
  35.                 return connection;
  36.             });
  37.     },
  38.     collections: {
  39.         users: () => users,
  40.         log: () => log,
  41.     }
  42. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement