diff --git a/src/database/query.ts b/src/database/query.ts index b9c9dc0..ef26e27 100644 --- a/src/database/query.ts +++ b/src/database/query.ts @@ -139,48 +139,49 @@ export default class Query { } async set(value: any) { - if (value === null || value === undefined) - return this.delete(value); - const lock = await this.database.locks.lock(this.path); let batch = this.database.level.batch(); try { - let field = await this.getField(this.path); - if (field) { - await this.delete(batch); + if (value === null || value === undefined) { + this.delete(value); } else { - for (let i = 0; i < this.path.length; i++) { - let subpath = this.path.slice(0, i); - let field = await this.getField(subpath); - if (!field) { - batch.put(this.pathToKey(subpath), FieldEncoder.encode({ - type: FieldTypes.OBJECT - })); - } else if (field.type !== FieldTypes.OBJECT) { - throw new Error("Parent elements not all Object. Cannot set value!"); - } - } - } - - const saveValue = (path: string[], value: any) => { - if (typeof value === "object") { - //TODO: Handle case array! - // Field type array? - batch.put(this.pathToKey(path), FieldEncoder.encode({ - type: FieldTypes.OBJECT - })) - for (let field in value) { - saveValue([...path, field], value[field]); - } + let field = await this.getField(this.path); + if (field) { + await this.delete(batch); } else { - batch.put(this.pathToKey(path), FieldEncoder.encode({ - type: FieldTypes.VALUE, - value - })); + for (let i = 0; i < this.path.length; i++) { + let subpath = this.path.slice(0, i); + let field = await this.getField(subpath); + if (!field) { + batch.put(this.pathToKey(subpath), FieldEncoder.encode({ + type: FieldTypes.OBJECT + })); + } else if (field.type !== FieldTypes.OBJECT) { + throw new Error("Parent elements not all Object. Cannot set value!"); + } + } } - } - saveValue(this.path, value); + const saveValue = (path: string[], value: any) => { + if (typeof value === "object") { + //TODO: Handle case array! + // Field type array? + batch.put(this.pathToKey(path), FieldEncoder.encode({ + type: FieldTypes.OBJECT + })) + for (let field in value) { + saveValue([...path, field], value[field]); + } + } else { + batch.put(this.pathToKey(path), FieldEncoder.encode({ + type: FieldTypes.VALUE, + value + })); + } + } + + saveValue(this.path, value); + } await batch.write(); this.database.changeObservable.send({ diff --git a/src/web/helper/hb.ts b/src/web/helper/hb.ts index 4361599..eb574c1 100644 --- a/src/web/helper/hb.ts +++ b/src/web/helper/hb.ts @@ -36,8 +36,6 @@ Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) { }); -const formsTemplate = Handlebars.compile(readFileSync("./views/forms.hbs").toString()); - const cache = new Map(); export default function getTemplate(name: string) {