Changing sender to session in Query

This commit is contained in:
Fabian 2019-11-12 13:02:28 +01:00
parent e42f0d3991
commit 10f3b4fa50
3 changed files with 13 additions and 12 deletions

View File

@ -117,7 +117,7 @@ export class ConnectionManager {
if (!handler)
throw new Error("Invalid Request!");
let query = db.getQuery(path || [], session.sessionid, isDoc ? "document" : "collection");
let query = db.getQuery(path || [], session.id, isDoc ? "document" : "collection");
let res = await handler({
id,
data,
@ -156,7 +156,7 @@ export class ConnectionManager {
})
socket.on("close", () => {
Logging.log(`${session.sessionid} has disconnected!`);
Logging.log(`${session.id} has disconnected!`);
Logging.debug("Clearing stored:", stored);
stored.forEach(query => (query as DocumentQuery | CollectionQuery).unsubscribe());
stored.clear();

View File

@ -3,6 +3,7 @@ import { resNull } from "../storage";
import nanoid = require("nanoid/generate");
import Logging from "@hibas123/nodelogging";
import * as MSGPack from "what-the-pack";
import Session from "./session";
export const MP = MSGPack.initialize(2 ** 20);
@ -23,7 +24,7 @@ export class Query {
return path.every(e => (e.match(/[^a-zA-Z0-9_\-\<\>]/g) || []).length === 0);
}
constructor(protected database: Database, protected path: string[], protected sender: string) {
constructor(protected database: Database, protected path: string[], protected session: Session) {
if (path.length > 10) {
throw new Error("Path is to long. Path is only allowed to be 10 Layers deep!");
}
@ -73,7 +74,7 @@ export class Query {
type,
document,
data,
sender: this.sender
sender: this.session.id
}
let s = this.database.changes.get(this.getKey(collection, document))
@ -86,8 +87,8 @@ export class Query {
}
protected static getConstructorParams(query: Query): [Database, string[], string] {
return [query.database, query.path, query.sender];
protected static getConstructorParams(query: Query): [Database, string[], Session] {
return [query.database, query.path, query.session];
}
}
@ -98,8 +99,8 @@ interface UpdateData {
}
}
export class DocumentQuery extends Query {
constructor(database: Database, path: string[], sender: string) {
super(database, path, sender);
constructor(database: Database, path: string[], session: Session) {
super(database, path, session);
this.onChange = this.onChange.bind(this);
}
@ -308,8 +309,8 @@ interface DocRes {
}
export class CollectionQuery extends Query {
constructor(database: Database, path: string[], sender: string) {
super(database, path, sender);
constructor(database: Database, path: string[], session: Session) {
super(database, path, session);
this.onChange = this.onChange.bind(this);
}
@ -319,7 +320,7 @@ export class CollectionQuery extends Query {
public async add(value: any) {
let id = nanoid(ALPHABET, 32);
let q = new DocumentQuery(this.database, [...this.path, id], this.sender);
let q = new DocumentQuery(this.database, [...this.path, id], this.session);
await q.set(value, {});
return id;
}

View File

@ -1,6 +1,6 @@
export default class Session {
constructor(private _sessionid: string) { }
get sessionid() {
get id() {
return this._sessionid;
}
root: boolean = false;