This repository has been archived on 2021-06-02. You can view files and clone it, but cannot push or open issues or pull requests.
RealtimeDB-OLD/src/settings.ts

129 lines
3.4 KiB
TypeScript
Raw Normal View History

2019-09-18 19:54:28 +00:00
import getLevelDB, { resNull } from "./storage";
import Encoder, { DataTypes } from "@hibas123/binary-encoder";
import Logging from "@hibas123/nodelogging";
import { Lock } from "@hibas123/utils";
interface IDatabaseConfig {
name: string;
publickey?: string;
rules?: string;
accesskey?: string;
}
// const DatabaseEncoder = new Encoder<IDatabaseConfig>({
// publickey: {
// index: 1,
// type: DataTypes.STRING,
// allowNull: true
// },
// rules: {
// index: 2,
// type: DataTypes.STRING
// }
// });
class SettingComponent {
db = getLevelDB("_server");
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),
this.getField(database, "accesskey").then(r => res.accesskey = r)
])
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();
}
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")
.write();
lock.release();
}
}
const Settings = new SettingComponent();
export default Settings;