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.
Hydro/hydro/service/db.ts

25 lines
696 B
TypeScript

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