2019-09-18 19:54:28 +00:00
|
|
|
import getLevelDB, { resNull } from "./storage";
|
|
|
|
import { Lock } from "@hibas123/utils";
|
|
|
|
|
|
|
|
interface IDatabaseConfig {
|
|
|
|
name: string;
|
|
|
|
publickey?: string;
|
|
|
|
rules?: string;
|
|
|
|
accesskey?: string;
|
2019-10-01 15:45:38 +00:00
|
|
|
rootkey?: string;
|
2019-09-18 19:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class SettingComponent {
|
2019-11-04 16:52:18 +00:00
|
|
|
db = getLevelDB("_server").data;
|
2019-09-18 19:54:28 +00:00
|
|
|
databaseLock = new Lock();
|
|
|
|
|
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
private async setField(name: string, field: string, value: string) {
|
|
|
|
return this.db.put("database:" + name + ":" + field, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getField(name: string, field: string) {
|
|
|
|
return this.db.get("database:" + name + ":" + field).then(r => r.toString()).catch(resNull)
|
|
|
|
}
|
|
|
|
|
|
|
|
private getDatabaseList() {
|
|
|
|
return this.db.get("databases")
|
|
|
|
.then(res => res.toString())
|
|
|
|
.then(res => res.split(":"))
|
|
|
|
.catch(err => err.notFound ? [] as string[] : Promise.reject(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
async getDatabases() {
|
|
|
|
const lock = await this.databaseLock.getLock();
|
|
|
|
|
|
|
|
const databases = await this.getDatabaseList().then(res => Promise.all(res.map(async database => {
|
|
|
|
let res: IDatabaseConfig = {
|
|
|
|
name: database
|
|
|
|
}
|
|
|
|
await Promise.all([
|
|
|
|
this.getField(database, "publickey").then(r => res.publickey = r),
|
|
|
|
this.getField(database, "rules").then(r => res.rules = r),
|
2019-10-01 15:45:38 +00:00
|
|
|
this.getField(database, "accesskey").then(r => res.accesskey = r),
|
|
|
|
this.getField(database, "rootkey").then(r => res.rootkey = r)
|
2019-09-18 19:54:28 +00:00
|
|
|
])
|
|
|
|
return res;
|
|
|
|
})))
|
|
|
|
|
|
|
|
lock.release();
|
|
|
|
|
|
|
|
return databases;
|
|
|
|
}
|
|
|
|
|
|
|
|
// hasDatabase(name: string): boolean {
|
|
|
|
// //TODO may require lock
|
|
|
|
// return this.databases.has(name);
|
|
|
|
// }
|
|
|
|
|
|
|
|
async addDatabase(name: string) {
|
|
|
|
//TODO: Check for valid name
|
|
|
|
if (name.indexOf(":") >= 0)
|
|
|
|
throw new Error("Invalid Database name. Cannot contain ':'!");
|
|
|
|
|
|
|
|
const lock = await this.databaseLock.getLock();
|
|
|
|
|
|
|
|
let dbs = await this.getDatabaseList()
|
|
|
|
dbs.push(name);
|
|
|
|
await this.db.put("databases", dbs.join(":"))
|
|
|
|
|
|
|
|
lock.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
async setDatabasePublicKey(name: string, publickey: string) {
|
|
|
|
const lock = await this.databaseLock.getLock();
|
|
|
|
|
|
|
|
await this.setField(name, "publickey", publickey);
|
|
|
|
|
|
|
|
lock.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
async setDatabaseRules(name: string, rules: string) {
|
|
|
|
const lock = await this.databaseLock.getLock();
|
|
|
|
|
|
|
|
await this.setField(name, "rules", rules);
|
|
|
|
|
|
|
|
lock.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
async setDatabaseAccessKey(name: string, accesskey: string) {
|
|
|
|
const lock = await this.databaseLock.getLock();
|
|
|
|
|
|
|
|
await this.setField(name, "accesskey", accesskey);
|
|
|
|
|
|
|
|
lock.release();
|
|
|
|
}
|
|
|
|
|
2019-10-01 15:45:38 +00:00
|
|
|
async setDatabaseRootKey(name: string, accesskey: string) {
|
|
|
|
const lock = await this.databaseLock.getLock();
|
|
|
|
|
|
|
|
await this.setField(name, "rootkey", accesskey);
|
|
|
|
|
|
|
|
lock.release();
|
|
|
|
}
|
|
|
|
|
2019-09-18 19:54:28 +00:00
|
|
|
async deleteDatabase(name: string) {
|
|
|
|
const lock = await this.databaseLock.getLock();
|
|
|
|
|
|
|
|
let pref = "database:" + name;
|
|
|
|
|
|
|
|
let dbs = await this.getDatabaseList().then(res => res.filter(e => e !== name));
|
|
|
|
|
|
|
|
await this.db.batch()
|
|
|
|
.put("databases", dbs.join(":"))
|
|
|
|
.del(pref + ":publickey")
|
|
|
|
.del(pref + ":rules")
|
|
|
|
.del(pref + ":accesskey")
|
2019-10-01 15:45:38 +00:00
|
|
|
.del(pref + ":rootkey")
|
2019-09-18 19:54:28 +00:00
|
|
|
.write();
|
|
|
|
|
|
|
|
lock.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Settings = new SettingComponent();
|
|
|
|
export default Settings;
|