+table prefix support

pull/10/head
undefined 4 years ago
parent b472b5c245
commit 081a75af8c

@ -1,6 +1,6 @@
{
"name": "hydrooj",
"version": "2.12.10",
"version": "2.12.11",
"bin": "bin/hydrooj.js",
"main": "dist/loader.js",
"typings": "dist/loader.d.ts",

@ -12,6 +12,7 @@ const tmpdir = path.resolve(os.tmpdir(), 'hydro');
const lockfile = path.resolve(tmpdir, 'lock.json');
export async function load(call: Entry) {
fs.ensureDirSync(tmpdir);
if (fs.existsSync(lockfile) && !argv.ignorelock) {
try {
const file = require(lockfile);

@ -52,6 +52,12 @@ class Loader {
<input name="name" type="text" value="hydro">
</label>
</div></div>
<div class="row"><div class="columns">
<label class="inverse material textbox">
{{ _('Table Prefix') }}
<input name="prefix" type="text" placeholder="{{ _('Leave blank if none') }}">>
</label>
</div></div>
<div class="row"><div class="columns">
<label class="inverse material textbox">
{{ _('Database Username') }}
@ -103,7 +109,7 @@ async function get(ctx: Context) {
async function post(ctx: Context) {
const {
host, port, name, username, password,
host, port, name, username, password, prefix,
} = ctx.request.body;
let mongourl = 'mongodb://';
if (username) mongourl += `${username}:${password}@`;
@ -113,23 +119,24 @@ async function post(ctx: Context) {
useNewUrlParser: true, useUnifiedTopology: true,
});
const db = Database.db(name);
const coll = prefix ? db.collection('system') : db.collection(`${prefix}.system`);
await Promise.all([
db.collection('system').updateOne(
coll.updateOne(
{ _id: 'server.host' },
{ $set: { value: ctx.request.host } },
{ upsert: true },
),
db.collection('system').updateOne(
coll.updateOne(
{ _id: 'server.hostname' },
{ $set: { value: ctx.request.hostname } },
{ upsert: true },
),
db.collection('system').updateOne(
coll.updateOne(
{ _id: 'server.url' },
{ $set: { value: ctx.request.href } },
{ upsert: true },
),
db.collection('system').updateOne(
coll.updateOne(
{ _id: 'server.port' },
{ $set: { value: parseInt(listenPort as string, 10) } },
{ upsert: true },

@ -19,6 +19,7 @@ mongodb.MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopolog
});
export function collection(c: string) {
if (opts.prefix) return db.collection(`${opts.prefix}.${c}`);
return db.collection(c);
}

Loading…
Cancel
Save