html5 indexeddb的簡明api

網上搜來一堆api,demo,幾乎沒有找到滿意的答案,通過彙總並結合本身的理解,封裝了一套簡單的api數據庫

// indexedDB是一個異步對象,必須使用回調函數方式進行調用

//打開一個數據庫,open支持兩個參數,第二個參數指定版本號,我沒用到,讓瀏覽器本身建立版本號。
var request = indexedDB.open('myDbName');
request.onsuccess = function(e) {
    //拿到db對象
    var db = e.target.result;
    
    
    //新增一個數據表:給數據表指定一個搜索的鍵值
    var store = db.createObjectStore('myTableName', {keyPath: 'mykey'});
    
    
    // 建立數據庫事務(不是很理解事務二字),我理解的是:指定一個即將用來讀寫的數據
    //除了新建數據表都用這一行代碼 和上面的createObjectStore不能同時使用
    var transaction = db.transaction("myTableName", 'readwrite').objectStore("myTableName");
    //到此,就能夠進行數據庫的數據表操做了
    
    
    //如下四個方法不是同時使用的,是分別封裝在不一樣方法內的,這裏爲了演示,所有寫一塊兒了
    
    //增:增長一條數據 add(anything)
    //通常狀況下不用add,而是用put方法
    var result = store.add({name:'mykey',value:'test1'});
    
    //刪:刪除一條數據 delete(keyPath)
    var result = store.delete('mykey');
    
    //改:修改一條數據,通常狀況下put使用比較多
    var result = store.put({name:'mykey',value:'test value'});
    
    //查:讀取一條數據
    var result = store.get('mykey');
    
    
    result.onsuccess = function(e) {
        //若是是get,這裏就是讀取的數據
        var obj = e.target.result;
        success(obj);
    };
    result.onerror = function(e) {
    };
}
request.onerror = function() {
};

如下是我本身封裝好的方法:api

var myDb = {
    options:{
        dbName:'myDbName',
        tbName:'myTableName',
        keyPath:'name',
    },
    getIndexedDB: function(name) {
        var that = this;
        return new Promise(function(success, error) {
            // 打開數據庫
            var request = indexedDB.open(that.options.dbName);
            request.onsuccess = function(e) {
                var database = e.target.result;
                // 操做數據表
                var store = database.transaction(that.options.tbName, 'readwrite').objectStore(that.options.tbName);
                var result = store.get(name);
                result.onsuccess = function(e) {
                    var obj = e.target.result;
                    success(obj);
                };
                result.onerror = function(e) {
                    error(obj);
                };
            }
            request.onerror = function() {
                error(event);
            };
            // 數據庫不存在時,或者版本號不一致時,就會執行這裏
            request.onupgradeneeded = function(e) {
                e.target.result.createObjectStore(that.options.tbName, { keyPath: that.options.keyPath });
            };
        });
    },
    getIndexedDBAll: function() {
        var that = this;
        return new Promise(function(success, error) {
            // 打開數據庫
            var request = indexedDB.open(that.options.dbName);
            request.onsuccess = function(e) {
                var database = e.target.result;
                // 操做數據表
                var store = database.transaction(that.options.tbName, 'readwrite').objectStore(that.options.tbName);
                var cursorRequest = store.openCursor();
                var list = [];
                cursorRequest.onsuccess = function(event) {
                    var cursor = event.target.result;
                    if (cursor) {
                        var value = cursor.value;
                        list.push(value);
                        cursor.continue();
                    } else {
                        success(list);
                    }
                }
            }
            request.onerror = function() {
                error(event);
            };
            // 數據庫不存在時,或者版本號不一致時,就會執行這裏
            request.onupgradeneeded = function(e) {
                e.target.result.createObjectStore(that.options.tbName, { keyPath: that.options.keyPath });
            };
        });
    },
    saveOneIndexedDB: function(keyValue) {
        var that = this;
        return new Promise(function(success, error) {
            // 打開數據庫
            var request = indexedDB.open(that.options.dbName);
            request.onsuccess = function(e) {
                var database = e.target.result;
                var store = null;
                store = database.transaction(that.options.tbName, 'readwrite').objectStore(that.options.tbName);
                var result = store.put(keyValue);
                result.onsuccess = function(event) {
                    success(keyValue);
                };
                result.onerror = function(event) {
                    error(event);
                };
            }
            request.onerror = function(event) {
                error(event)
            };
            // 數據庫不存在時,或者版本號不一致時,就會執行這裏
            request.onupgradeneeded = function(e) {
                var store = e.target.result.createObjectStore(that.options.tbName, { keyPath: that.options.keyPath });
                var result = store.put(keyValue);
                result.onsuccess = function(event) {
                    success(keyValue);
                };
                result.onerror = function(event) {
                    error(event);
                };
            };
        });
    },
    saveListIndexedDB: function(list, back, error) {
        var that = this;
        return Promise.all(list.map(function(v) {
            return that.saveOneIndexedDB(v);
        }));
    },
    deleteDB: function(name) {
        var that = this;
        return new Promise(function(success, error) {
            // 打開數據庫
            var request = indexedDB.open(that.options.dbName);
            request.onsuccess = function(e) {
                var database = e.target.result;
                var store = database.transaction(that.options.tbName, 'readwrite').objectStore(that.options.tbName);
                var result = store.delete(name);
                result.onsuccess = function(event) {
                    success(name)
                };
                result.onerror = function(event) {
                    error(event)
                };
            }
            request.onerror = function(event) {
                error(event)
            }
            // 數據庫不存在時,或者版本號不一致時,就會執行這裏
            request.onupgradeneeded = function(e) {
                e.target.result.createObjectStore(that.options.tbName, { keyPath: that.options.keyPath });
            };
        });
    }
};

測試一下:瀏覽器

//設置數據庫名稱和表名
myDb.options.dbName = 'file_list';
myDb.options.tbName = 'files';

//保存一條數據:
myDb.saveOneIndexedDB({
    name:'test',
    file:'123123',
}).then(function(file){
    /**file:{
        name:'test',
        file:'123123',
    }*/
});

//讀取數據
myDb.getIndexedDB('ex_zh.js').then(function(v){console.log(v)});

clipboard.png

相關文章
相關標籤/搜索