import dbVersion from './version' ; import MigrationExecutor from './migrations/MigrationExecutor' ; import Store from './Store' ; export default class Storage { db : Promise < IDBDatabase > ; param db param event private dbVersionChangeHandler ( db , event ) { console . info ( 'onversionchange' , event ) ; db . close ( ) ; } param resolve param event private dbRequestSuccessHandler ( resolve , event ) { const target = < IIDBOpenEventTargetWithResult > event . target ; const database = target . result ; database . onversionchange = ( event ) => { this . dbVersionChangeHandler ( database , event ) ; } ; resolve ( database ) ; } param event private dbRequestUpgradeNeededHandler ( event ) { const target = < IIDBOpenEventTargetWithResult > event . target ; const database = target . result ; database . onversionchange = ( event ) => { this . dbVersionChangeHandler ( database , event ) ; } ; const migrationExecutor = new MigrationExecutor ( database ) ; migrationExecutor . applyMigrations ( ) ; } /**
     * Open db
     */ getDB ( ) { return new Promise < IDBDatabase > ( ( resolve , reject ) => { const request = indexedDB . open ( 'PUSHWOOSH_SDK_STORE' , dbVersion ) ; request . onsuccess = ( event ) => { this . dbRequestSuccessHandler ( resolve , event ) ; } ; request . onupgradeneeded = ( event ) => { this . dbRequestUpgradeNeededHandler ( event ) ; } ; request . onerror = ( ) => reject ( request . error ) ; } ) ; } param storeName param data param key async put ( storeName , data , key ? ) { const db = await this . getDB ( ) ; const store = new Store ( db , storeName ) ; const result = await store . put ( data , key ) ; db . close ( ) ; return result ; } param storeName param key async delete ( storeName , key ) { const db = await this . getDB ( ) ; const store = new Store ( db , storeName ) ; const result = await store . delete ( key ) ; db . close ( ) ; return result ; } param storeName param key param defaultValue async get < Response , D > ( storeName , key , defaultValue ? ) { const db = await this . getDB ( ) ; const store = new Store ( db , storeName ) ; const result = await store . get < Response , D > ( key , defaultValue ) ; db . close ( ) ; return result ; } param storeName async getAll < Response > ( storeName ) { const db = await this . getDB ( ) ; const store = new Store ( db , storeName ) ; const result = await store . getAll < Response > ( ) ; db . close ( ) ; return result || [ ] ; } param storeName param query async count ( storeName , query ? ) { const db = await this . getDB ( ) ; const store = new Store ( db , storeName ) ; const result = await store . count ( query ) ; db . close ( ) ; return result ; } param storeName param indexName param key async countByIndex ( storeName , indexName , key ? ) { const db = await this . getDB ( ) ; const store = new Store ( db , storeName ) ; store . index = indexName ; const result = await store . countByIndex ( key ) ; db . close ( ) ; return result ; } }