First basic version

This commit is contained in:
Fabian Stamm
2019-11-04 00:45:30 +01:00
parent a78e98a0c8
commit 68011e3866
10 changed files with 431 additions and 393 deletions

View File

@ -1,9 +1,8 @@
import { Rules } from "./rules";
import Settings from "../settings";
import getLevelDB from "../storage";
import PathLock from "./lock";
import getLevelDB, { LevelDB, deleteLevelDB } from "../storage";
import DocumentLock from "./lock";
import Query from "./query";
import { Observable } from "@hibas123/utils";
export class DatabaseManager {
static databases = new Map<string, Database>();
@ -36,27 +35,33 @@ export class DatabaseManager {
if (db) {
await Settings.deleteDatabase(name);
await db.stop();
await deleteLevelDB(db.name)
}
}
}
export enum ChangeTypes {
SET,
PUSH
}
export type ChangeTypes = "added" | "modified" | "deleted";
export type Change = {
data: any;
document: string;
type: ChangeTypes;
path: string[]
sender: string;
}
export class Database {
public level = getLevelDB(this.name);
public rules: Rules;
public locks = new PathLock()
public changeObservable = new Observable<Change>();
export class Database {
private level = getLevelDB(this.name);
get data() {
return this.level;
}
public rules: Rules;
public locks = new DocumentLock()
public changes = new Map<string, Set<(change: Change) => void>>();
toJSON() {
return {
@ -94,11 +99,11 @@ export class Database {
}
getQuery(path: string[]) {
return new Query(this, path);
getQuery(path: string[], sender: string) {
return new Query(this, path, sender);
}
async stop() {
await this.level.close();
await this.data.close();
}
}