Adding HTTP Query Endpoint and refining some things

This commit is contained in:
Fabian Stamm
2019-11-15 16:36:42 +01:00
parent 4cee0048f5
commit d2621fdd3c
6 changed files with 163 additions and 87 deletions

View File

@ -2,7 +2,7 @@ import { Rules } from "./rules";
import Settings from "../settings";
import getLevelDB, { LevelDB, deleteLevelDB } from "../storage";
import DocumentLock from "./lock";
import { DocumentQuery, CollectionQuery, Query } from "./query";
import { DocumentQuery, CollectionQuery, Query, QueryError } from "./query";
import Logging from "@hibas123/nodelogging";
import Session from "./session";
import nanoid = require("nanoid");
@ -134,7 +134,20 @@ export class Database {
return new Query(this, path, session);
}
private validate(query: ITypedQuery<any>) {
const inv = new QueryError("Malformed query!");
if (!query || typeof query !== "object")
throw inv;
if (!query.type)
throw inv;
if (!query.path)
throw inv;
}
async run(query: IQuery, session: Session) {
this.validate(query);
const isCollection = query.path.length % 2 === 1;
if (isCollection) {
const q = new CollectionQuery(this, query.path, session);
@ -178,6 +191,8 @@ export class Database {
}
async snapshot(query: ITypedQuery<"snapshot">, session: Session, onchange: (change: any) => void) {
this.validate(query);
const isCollection = query.path.length % 2 === 1;
let q: DocumentQuery | CollectionQuery;
if (isCollection) {
@ -208,8 +223,6 @@ export class Database {
}
}
async stop() {
await this.data.close();
}