zoukankan      html  css  js  c++  java
  • 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('读取失败');
            }
        }
    }
    以自己现在的努力程度,还没有资格和别人拼天赋
  • 相关阅读:
    [置顶] location.href你真的会用了?
    Hive HA使用说明
    十进制转换为任意进制及操作符重载
    《重构》学习总结
    linux kill进程和子进程小trick
    express for node 路由route几种实现方式的思考
    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第2章节--SharePoint 2013 App 模型概览 总结
    Can rename table but can not truncate table
    HBase
    链表解说和基本操作练习附代码
  • 原文地址:https://www.cnblogs.com/zhenjianyu/p/14629940.html
Copyright © 2011-2022 走看看