You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
705 B
JavaScript
23 lines
705 B
JavaScript
const Mongo = require('mongodb');
|
|
const bus = require('./bus');
|
|
const options = require('../options');
|
|
|
|
let mongourl = 'mongodb://';
|
|
if (options.username) mongourl += `${options.username}:${options.password}@`;
|
|
mongourl += `${options.host}:${options.port}/${options.name}`;
|
|
/**
|
|
* @type {import('mongodb').Db}
|
|
*/
|
|
let db = null;
|
|
Mongo.MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true })
|
|
.then((Client) => {
|
|
db = Client.db(options.name);
|
|
db.Client = Client;
|
|
bus.publish('system_database_connected', null);
|
|
});
|
|
|
|
global.Hydro.service.db = module.exports = {
|
|
collection: (c) => db.collection(c),
|
|
dropDatabase: () => db.dropDatabase(),
|
|
};
|