export default class Store { private readonly name : TSdkStoreName ; private store : IDBObjectStore ; private _index : IDBIndex ; constructor ( db , name ) { this . name = name ; this . store = db . transaction ( this . name , 'readwrite' ) . objectStore ( this . name ) ; } set index ( index ) { const indexNames = this . store . indexNames ; if ( indexNames . contains ( index ) ) { this . _index = this . store . index ( index ) ; } else { console . warn ( `Index "${ index }" in ` ) ; } } private writeRequestPromise < T > ( request , result ? ) { return new Promise ( ( resolve , reject ) => { request . onsuccess = ( ) => { resolve ( result ) ; } ; request . onerror = ( ) => { reject ( request . error ) ; } ; } ) ; } private readRequestPromise < Response , D > ( request , defaultValue ? ) { return new Promise ( ( resolve , reject ) => { request . onsuccess = ( event ) => { const target = < IIDBGetTransactionEventTargetWithResult > event . target ; resolve ( < Response > target . result || defaultValue ) ; } ; request . onerror = ( ) => { reject ( request . error ) ; } ; } ) ; } put ( data , key ? ) { const request = this . store . put ( data , key ) ; return this . writeRequestPromise < TIDBKeyType > ( request , key ) ; } param data param key add ( data , key ? ) { return this . put ( data , key ) ; } delete ( key ) { const request = this . store . delete ( key ) ; return this . writeRequestPromise ( request ) ; } get < Response , D > ( key , defaultValue ? ) { const request = this . store . get ( key ) ; return this . readRequestPromise < Response , D > ( request , defaultValue ) ; } getAll < Response > ( ) { const cursor = this . store . openCursor ( ) ; const result = [ ] ; return new Promise ( ( resolve , reject ) => { cursor . onsuccess = ( event ) => { const target = < IIDBGetTransactionEventTargetWithResult > event . target ; const cursorResult = target . result ; if ( cursorResult ) { result . push ( cursorResult . value ) ; cursorResult . continue ( ) ; } else { resolve ( result ) ; } } ; cursor . onerror = ( ) => { reject ( cursor . error ) ; } ; } ) ; } count ( query ? ) { const request = this . store . count ( query ) ; return this . readRequestPromise < number , number > ( request , 0 ) ; } countByIndex ( key ? ) { const request = this . _index . count ( key ) ; return this . readRequestPromise < number , number > ( request , 0 ) ; } }