indexedDB

 

indexedDB兼容性

indexedDB

 代码:

class indexedDB {
    constructor(){
        this.indexedDBInstanceObject = null;
    }
    openIndexedDB(dbName = ‘cacheList‘, version = 1){
        let request = window.indexedDB && window.indexedDB.open(dbName, version);
        request.onerror = (error) => {
            console.error(‘数据库打开报错:‘,error);
        }
        request.onsuccess = (result) => {
            this.indexedDBInstanceObject = request.result;
            console.info(‘数据库打开成功:‘,result);
        }
        request.onupgradeneeded = (event) => {
            this.indexedDBInstanceObject = event.target.result;
        }
    }
    createTable(tableName = ‘person‘, params = { keyPath: ‘id‘}){
        let objectStore = null;
        if(!this.indexedDBInstanceObject.objectStoreNames.contains(tableName)){
            objectStore = this.indexedDBInstanceObject.createObjectStore(tableName, params);
        }
        return objectStore;
    }
    createIndex(indexArr = []){
        let objectStore = this.createTable();
        indexArr.forEach(item => {
            let { indexName, indexKey, indexObj } = item;
            objectStore.createIndex(indexName, indexKey, indexObj);
        });
    }
    add(tableName, data){
        let request = this.indexedDBInstanceObject.transaction([tableName], ‘readwrite‘).objectStore(tableName).add(data);
        request.onsuccess = () => {
            console.log(‘数据写入成功‘);
        }
        request.onerror = () => {
            console.log(‘数据写入失败‘);
        }
    }
    read(tableName, mainKey = 1){
        let request = this.indexedDBInstanceObject.transaction([tableName]).objectStore(tableName).get(mainKey);
        request.onsuccess = () => {
            if(request.result){
                console.log(request.result);
            }else{
                console.log(‘未获得数据‘);
            }
        }
        request.onerror = () => {
            console.log(‘读取失败‘);
        }
    }
    put(tableName, data){
        let request = this.indexedDBInstanceObject.transaction([tableName], ‘readwrite‘).objectStore(tableName).put(data);
        request.onsuccess = () => {
            console.log(‘数据更新成功‘);
        }
        request.onerror = () => {
            console.log(‘数据更新失败‘);
        }
    }
    remove(tableName, mainKey = 1){
        let request = this.indexedDBInstanceObject.transaction([tableName], ‘readwrite‘).objectStore(tableName).delete(mainKey);
        request.onsuccess = () => {
            console.log(‘数据删除成功‘);
        }
        request.onerror = () => {
            console.log(‘数据删除失败‘);
        }
    }
    readByIndex(index, value){
        let request = this.indexedDBInstanceObject.transaction([tableName], ‘readonly‘).objectStore(tableName).index(index).get(value);
        request.onsuccess = () => {
            if(request.result){
                console.log(request.result);
            }else{
                console.log(‘未获得数据‘);
            }
        }
        request.onerror = () => {
            console.log(‘读取失败‘);
        }
    }
}

indexedDB

上一篇:Oracle rowum和rowid的区别


下一篇:IDEA 配置数据库的Generate POJOs.groocvy