mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 09:38:19 +08:00
core: add 'hydrooj db' method
This commit is contained in:
1 parent
5513bd6929
commit
551cf9c19f
4 files changed
+30
-6
No files matched your search
@@ -3,6 +3,7 @@ import * as bus from './bus';
|
||||
import { Collections } from '../interface';
|
||||
|
||||
interface MongoConfig {
|
||||
protocol?: string,
|
||||
username?: string,
|
||||
password?: string,
|
||||
host?: string,
|
||||
@@ -19,12 +20,17 @@ class MongoService {
|
||||
public db2: Db;
|
||||
private opts: MongoConfig;
|
||||
|
||||
async start(opts: MongoConfig) {
|
||||
this.opts = opts;
|
||||
let mongourl = 'mongodb://';
|
||||
static buildUrl(opts: MongoConfig) {
|
||||
let mongourl = `${opts.protocol}://`;
|
||||
if (opts.username) mongourl += `${opts.username}:${opts.password}@`;
|
||||
mongourl += `${opts.host}:${opts.port}/${opts.name}`;
|
||||
if (opts.url) mongourl = opts.url;
|
||||
return mongourl;
|
||||
}
|
||||
|
||||
async start(opts: MongoConfig) {
|
||||
this.opts = opts;
|
||||
const mongourl = MongoService.buildUrl(opts);
|
||||
this.client = await MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true });
|
||||
this.db = this.client.db(opts.name);
|
||||
this.client2 = await MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true });
|
||||
|
||||
Reference in new issue
Block a user