core: setup: prefer mongo connection uri

This commit is contained in:
undefined committed 2024-03-21 08:35:18 +08:00
1 parent 761706f685
commit f5a9660d9a
3 files changed
+12 -41

No files matched your search

+3 -1
View File
@@ -2,6 +2,7 @@
import {
Collection, Db, IndexDescription, MongoClient,
} from 'mongodb';
import mongoUri from 'mongodb-uri';
import { Time } from '@hydrooj/utils';
import { Logger } from '../logger';
import options from '../options';
@@ -38,6 +39,7 @@ class MongoService {
async start() {
const opts = options() || {};
let mongourl = MongoService.buildUrl(opts);
const url = mongoUri.parse(mongourl);
if (process.env.CI) {
const { MongoMemoryServer } = require('mongodb-memory-server');
const mongod = await MongoMemoryServer.create();
@@ -45,7 +47,7 @@ class MongoService {
}
this.opts = opts;
this.client = await MongoClient.connect(mongourl);
this.db = this.client.db(opts.name || 'hydro');
this.db = this.client.db(url.database || 'hydro');
await bus.parallel('database/connect', this.db);
setInterval(() => this.fixExpireAfter(), Time.hour);
}