Uno-Dan

DBMagic Lib

Feb 27th, 2021
951
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.   que_add( args ) {
  57.     return new Promise(( resolve, reject ) => {
  58.       let openReq = this.indexedDB.open( this.dbName );
  59.  
  60.       openReq.onerror = e => reject( e );
  61.  
  62.       openReq.onsuccess = () => {
  63.         let tx = openReq.result.transaction ( [ this.store ], 'readwrite' );
  64.         let addReq = tx.objectStore( this.store ).add( args );
  65.         tx.commit();
  66.  
  67.         addReq.onerror = e => {
  68.           if ( this.debug )
  69.             log( `[ ERROR:add ], could not add key [${ args.id }] in store [${
  70.             addReq.source.name }] in database [${ this.dbName }]` );
  71.           reject( e );  
  72.         };
  73.  
  74.         addReq.onsuccess = () => {  
  75.           if ( this.debug )
  76.             log( `[ INFO:add ], Added key [${ args.id }] in store [${
  77.             addReq.source.name }] in database [${ this.dbName }]` );
  78.           resolve( this );  
  79.           openReq.result.close();
  80.         };
  81.       };
  82.     } );
  83.   }
  84.  
  85.   que_get( args ) {
  86.     let store = this.store;
  87.     let dbName = this.dbName;
  88.    
  89.     return new Promise( ( resolve, reject ) => {
  90.       var openReq = this.indexedDB.open( dbName );
  91.  
  92.         openReq.onerror = e => reject( e );
  93.  
  94.         openReq.onsuccess = () => {
  95.           let tx = openReq.result.transaction ( [ store ], 'readonly' );
  96.        
  97.           let obj = tx.objectStore( store );
  98.           let req = args ? obj.get( args.id ) : obj.getAll();
  99.           req.onerror = e => {
  100.             if ( this.debug )
  101.               log( `[ ERROR:getAll ], could not retrieve keys from store [${
  102.               req.source.name }] in database [${ dbName }]` );
  103.             reject( e );  
  104.           };
  105.  
  106.           req.onsuccess = () => {
  107.             if ( this.debug )
  108.               log( `[ INFO:getAll ], Retrieved all keys from store [${
  109.               req.source.name }] in database [${ dbName }]` );
  110.            
  111.             resolve( req.result );  
  112.             openReq.result.close();
  113.           };
  114.         };
  115.     } );
  116.   }
  117.  
  118.   que_update( args ) {
  119.     return this.get( { id: args.id } ).then( values => {
  120.       Object.keys( values ).forEach( key => {
  121.         if ( ! key in args ) args[ key ] = values[ key ]
  122.       } );
  123.      
  124.       return new Promise(( resolve, reject ) => {
  125.         let openReq = this.indexedDB.open( this.dbName );
  126.  
  127.         openReq.onerror = e => reject( e );
  128.  
  129.         openReq.onsuccess = () => {
  130.           let tx = openReq.result.transaction ( [ this.store ], 'readwrite' );
  131.          
  132.           let addReq = tx.objectStore( this.store ).put( args );
  133.           tx.commit();
  134.  
  135.           addReq.onerror = e => {
  136.             if ( this.debug )
  137.               log( `[ ERROR:add ], could not update key [${ args.id }] in store [${
  138.               addReq.source.name }] in database [${ this.dbName }]` );
  139.             reject( e );  
  140.           };
  141.  
  142.           addReq.onsuccess = () => {  
  143.             if ( this.debug )
  144.               log( `[ INFO:update ], Updated key [${ args.id }] in store [${
  145.               addReq.source.name }] in database [${ this.dbName }]` );
  146.            
  147.             openReq.result.close();
  148.             resolve( this );  
  149.           };
  150.         };
  151.       } );
  152.     } );
  153.   }
  154.  
  155.   que_delete( args ) {
  156.     let store = this.store;
  157.     let dbName = this.dbName;
  158.      
  159.     return new Promise( ( resolve, reject ) => {
  160.       var openReq = this.indexedDB.open( dbName );
  161.  
  162.         openReq.onerror = e => reject( e );
  163.  
  164.         openReq.onsuccess = () => {
  165.           let tx = openReq.result.transaction ( [ store ], 'readwrite' );
  166.        
  167.           let obj = tx.objectStore( store );
  168.           let req = tx.objectStore( store ).delete( args.id );
  169.           req.onerror = e => {
  170.             if ( this.debug )
  171.               log( `[ ERROR:delete ], could not delete keys in store [${
  172.               req.source.name }] in database [${ dbName }]` );
  173.             reject( e );  
  174.           };
  175.  
  176.           req.onsuccess = () => {
  177.             if ( this.debug )
  178.               log( `[ INFO:delete ], Deleted keys in store [${
  179.               req.source.name }] in database [${ dbName }]` );
  180.            
  181.             openReq.result.close();
  182.             resolve();  
  183.           };
  184.         };
  185.     } );
  186.   }
  187.  
  188.   add( args ) {
  189.     let promises = [];
  190.     return new Promise( ( resolve, reject ) => {
  191.       if ( ! Array.isArray( args ) ) args = [ args ];
  192.       args.forEach( item => { promises.push( this.que_add( item ) ); } );
  193.       Promise.all(promises)
  194.       .then( () => resolve( this ) )
  195.       .catch( err => reject( err ) );
  196.     } );
  197.   }
  198.  
  199.   get( args ) {
  200.     let promises = [];
  201.     return new Promise( ( resolve, reject ) => {
  202.       if ( ! Array.isArray( args ) ) args = [ args ];
  203.       args.forEach( item => { promises.push( this.que_get( item ) ); } );
  204.       Promise.all(promises)
  205.       .then( results => resolve( results ) )
  206.       .catch( err => reject( err ) );
  207.     } );
  208.   }
  209.  
  210.   update( args ) {
  211.     let promises = [];
  212.     return new Promise( ( resolve, reject ) => {
  213.       if ( ! Array.isArray( args ) ) args = [ args ];;
  214.       args.forEach( item => { promises.push( this.que_update( item ) ); } );
  215.       Promise.all(promises)
  216.       .then( () => resolve( this ) )
  217.       .catch( err => reject( err ) );
  218.     } );
  219.   }
  220.  
  221.   delete( args ) {
  222.     let promises = [];
  223.     return new Promise( ( resolve, reject ) => {
  224.       if ( ! Array.isArray( args ) ) args = [ args ];
  225.       args.forEach( item => { promises.push( this.que_delete( item ) ); } );
  226.       Promise.all(promises)
  227.       .then( () => resolve( this ) )
  228.       .catch( err => reject( err ) );
  229.     } );
  230.   }
  231. }
  232.  
  233. export default class DBMagic {
  234.   constructor( name, debug=false) {
  235.     this.db = name;
  236.     this.debug = debug;
  237.     this.stores = [];
  238.     this.upgrade = false;
  239.     this.init();
  240.   }
  241.  
  242.   init() {
  243.     let indexedDB = window.indexedDB || window.mozIndexedDB ||
  244.     window.webkitIndexedDB || window.msIndexedDB;
  245.    
  246.     return new Promise( ( resolve, reject ) => {
  247.       let req = indexedDB.open( this.db );
  248.      
  249.       req.onerror = e => reject( e );
  250.       req.onsuccess = () => { req.result.close(); resolve(); };
  251.       req.onupgradeneeded = () => { return true; };
  252.     } );
  253.   }
  254.  
  255.   table( name ) {
  256.     let tbl = new Table( this, name, this.debug );
  257.    
  258.     return tbl;
  259.   }
  260. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×