+table prefix support

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

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

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

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

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

Loading…
Cancel
Save