Advertisement
Uno-Dan

dbmagic.js

Feb 26th, 2021
1,314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. const log = console.log;
  3.  
  4. class Table {
  5.   constructor( db, name, debug=false) {
  6.     this.db = db;
  7.     this.dbName = db.db;
  8.     this.store = name;
  9.     this.debug = debug;
  10.    
  11.     this.indexedDB = window.indexedDB || window.mozIndexedDB ||
  12.     window.webkitIndexedDB || window.msIndexedDB;
  13.    
  14.     return this.table( name );
  15.   }
  16.  
  17.   table( store ) {
  18.     let stores = this.db.stores;
  19.    
  20.     const do_upgrade = () => {
  21.       stores.push( store );
  22.      
  23.       return new Promise( ( resolve, reject ) => {
  24.        
  25.         let req = this.indexedDB.open( this.dbName, stores.length + 1 );
  26.        
  27.         req.onerror = e => reject( e );
  28.         req.onsuccess = () => {
  29.           this.db.stores.push( store );
  30.           req.result.close();
  31.           resolve( this );
  32.         };
  33.         req.onupgradeneeded = () => req.result
  34.         .createObjectStore( store, { keyPath: 'id' } );
  35.       } );
  36.     };
  37.    
  38.     return new Promise( ( resolve, reject ) => {
  39.      let openReq = this.indexedDB.open( this.dbName );
  40.      
  41.       openReq.onerror = () => {
  42.         do_upgrade();
  43.         resolve( this );
  44.       };
  45.      
  46.       openReq.onsuccess = () => {
  47.         openReq.result.close();
  48.         if ( ! stores.includes( store ) ) {
  49.           do_upgrade();
  50.         }
  51.         resolve( this );
  52.       };
  53.     } );  
  54.   }
  55.          
  56.   get( args ) {
  57.     let store = this.store;
  58.     let dbName = this.dbName;
  59.    
  60.     return new Promise( ( resolve, reject ) => {
  61.       var openReq = this.indexedDB.open( dbName );
  62.  
  63.         openReq.onerror = e => reject( e );
  64.  
  65.         openReq.onsuccess = () => {
  66.           let tx = openReq.result.transaction ( [ store ], 'readonly' );
  67.        
  68.           let obj = tx.objectStore( store );
  69.           let req = args ? obj.get( args.id ) : obj.getAll();
  70.           req.onerror = e => {
  71.             if ( this.debug )
  72.               log( `[ ERROR:getAll ], could not retrieve keys from store [${
  73.               req.source.name }] in database [${ dbName }]` );
  74.             reject( e );  
  75.           };
  76.  
  77.           req.onsuccess = () => {
  78.             if ( this.debug )
  79.               log( `[ INFO:getAll ], Retrieved all keys from store [${
  80.               req.source.name }] in database [${ dbName }]` );
  81.             resolve( req.result );  
  82.             openReq.result.close();
  83.           };
  84.         };
  85.     } );
  86.   }
  87.  
  88.   add( args ) {
  89.     return new Promise(( resolve, reject ) => {
  90.       let openReq = this.indexedDB.open( this.dbName );
  91.  
  92.       openReq.onerror = e => reject( e );
  93.  
  94.       openReq.onsuccess = () => {
  95.         let tx = openReq.result.transaction ( [ this.store ], 'readwrite' );
  96.         let addReq = tx.objectStore( this.store ).add( args );
  97.         tx.commit();
  98.  
  99.         addReq.onerror = e => {
  100.           if ( this.debug )
  101.             log( `[ ERROR:add ], could not add key [${ args.id }] in store [${
  102.             addReq.source.name }] in database [${ this.dbName }]` );
  103.           reject( e );  
  104.         };
  105.  
  106.         addReq.onsuccess = () => {  
  107.           if ( this.debug )
  108.             log( `[ INFO:add ], Added key [${ args.id }] in store [${
  109.             addReq.source.name }] in database [${ this.dbName }]` );
  110.           resolve( this );  
  111.           openReq.result.close();
  112.         };
  113.       };
  114.     } );
  115.   }
  116. }
  117.  
  118. class DB {
  119.   constructor( name, debug=false) {
  120.     this.db = name;
  121.     this.debug = debug;
  122.     this.stores = [];
  123.     this.upgrade = false;
  124.     this.init();
  125.   }
  126.  
  127.   init() {
  128.     let indexedDB = window.indexedDB || window.mozIndexedDB ||
  129.     window.webkitIndexedDB || window.msIndexedDB;
  130.    
  131.    
  132.     return new Promise( ( resolve, reject ) => {
  133.       let req = indexedDB.open( this.db );
  134.      
  135.       req.onerror = e => reject( e );
  136.       req.onsuccess = () => { req.result.close(); resolve(); };
  137.       req.onupgradeneeded = () => { return true; };
  138.     } );
  139.   }
  140.  
  141.   table( name ) {
  142.     let tbl = new Table( this, name, this.debug );
  143.    
  144.     return tbl;
  145.   }
  146. }
  147.  
  148. jQuery( document ).ready( () => {
  149.   $( '#testDB' ).on( 'click', function() {
  150.      
  151.     $( 'body' ).append( $( '<div></br>Creating the databases.</div>' ) );
  152.    
  153.     let db1 = new DB( 'MyDB1', true );
  154.     const setup = async () => {
  155.         let tbl = await db1.table( 'users' );
  156.         tbl = await tbl.add( { id:1, fName: 'Joe', lName: 'Doe', group: 1 } );
  157.         tbl = await tbl.add( { id:2, fName: 'Jane', lName: 'Doe', group: 1 } );
  158.         log('Results:', await tbl.get( { id: 2 } ) );
  159.     };
  160.     setup();
  161.  
  162.     let db2 = new DB( 'MyDB2', true );
  163.     db2.table( 'users' )
  164.     .then( tbl => {
  165.         return tbl.add( { id:1, fName: 'Joe', lName: 'Smith', group: 1 } );    
  166.     } )
  167.     .then( tbl => { return tbl.get(); } )
  168.     .then( results => {
  169.         log('Results:', results );
  170.         $( 'body' ).append( $( '<div></br>Databases created, check the console now.</div>' ) );
  171.     }
  172.        
  173.     );
  174.   } )
  175. } );  
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement