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.
26 lines
737 B
TypeScript
26 lines
737 B
TypeScript
4 years ago
|
import mongodb from 'mongodb';
|
||
|
import * as bus from './bus';
|
||
|
import options from '../options';
|
||
|
|
||
4 years ago
|
const opts = options();
|
||
|
|
||
4 years ago
|
let mongourl = 'mongodb://';
|
||
4 years ago
|
if (opts.username) mongourl += `${opts.username}:${opts.password}@`;
|
||
|
mongourl += `${opts.host}:${opts.port}/${opts.name}`;
|
||
4 years ago
|
|
||
4 years ago
|
// eslint-disable-next-line import/no-mutable-exports
|
||
4 years ago
|
export let db: mongodb.Db = null;
|
||
|
|
||
|
mongodb.MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true })
|
||
|
.then((Client) => {
|
||
4 years ago
|
db = Client.db(opts.name);
|
||
4 years ago
|
global.Hydro.service.db.db = db;
|
||
4 years ago
|
bus.publish('system_database_connected', null);
|
||
|
});
|
||
|
|
||
|
export function collection(c: string) {
|
||
|
return db.collection(c);
|
||
|
}
|
||
|
|
||
4 years ago
|
global.Hydro.service.db = { collection, db };
|