Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Database {
  2.  
  3.     protected data: object;
  4.     protected path: string;
  5.  
  6.     constructor() {
  7.         this.data = {};
  8.     }
  9.  
  10.     public ref(path: string): Database {
  11.         if(!path) {
  12.             throw new Error("Path is not set");  
  13.         }
  14.         this.path = path;
  15.         return this;
  16.     }
  17.  
  18.     public push(obj: object): string {
  19.         if(!obj) {
  20.             throw new Error("Data object is missing");
  21.         }
  22.         const pushId: string= this.generatePushID();
  23.         const key: string = this.path + '/' + pushId;
  24.  
  25.         this.data[key] = obj;  
  26.         return pushId;
  27.     }
  28.  
  29.     public remove() {
  30.         Object.keys(this.data).forEach((key) => {
  31.             if(key.substr(0, this.path.length) === this.path) {
  32.                 delete this.data[key];
  33.             }
  34.         })
  35.     }
  36.  
  37.    
  38.     public once(continuation: any) {
  39.         // tslint:disable-next-line:no-console
  40.         console.log("Implement once");
  41.     }
  42.  
  43.     // This should be updated to return more firebase like push id
  44.     private generatePushID() {
  45.         return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);        
  46.     }
  47.  
  48. }
  49.  
  50. export default Database;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement