关于indexedDB的运用,以及一个简朴的封装

//由于ES6写起来简约,所以用ES6写法来作申明
//IDBTransacation接口由IndexedDB API供应,异步transaction运用数据库中的事宜对象属性。一切的读取和写入数据均在transactions中完成。由IDBDatabase提议transaction,经由过程IDBTransaction 来设置transaction的形式(比方是不是只读或读写),以及经由过程IDBObjectStore来取得一个request。同时你也能够运用它来中断transactions。

let idxDB = {
    db: {},
    transaction: {},
    startTransaction() {
        //一个IDBTransacation只能运用一次
        //建立transaction有3个请求,一、有connection(数据库衔接),二、storeName(读取的store名)、三、mode(包含readonly,readwrite和versionchange)
        this.transaction = this.db.transaction("diary", 'readwrite');
        this.transaction.oncomplete = () => console.log("transaction complete");
        this.transaction.onerror = e => console.dir(e);
    },
    initDB() {
        //下面一行代码,以数据库名(danote)和版本号(1)为参数,异步翻开一个数据库
        let request = indexedDB.open('danote', 1);
        request.onerror = e => console.log(e.currentTarget.error.message);
        request.onsuccess = e => this.db = e.target.result;
        request.onupgradeneeded = e => {
            //假如之前数据库不存在,也会运转onupgradeneeded
            //新建objectStore
            let thisDB = e.target.result;
            if (!thisDB.objectStoreNames.contains("diary")) {
                let objStore = thisDB.createObjectStore("diary", {
                    keyPath: "id",
                    autoIncrement: true
                });
                //第一个参数是index称号,第二个参数是keyPath
                objStore.createIndex("by_create_date", "create_date", {
                    unique: false
                });
            }
        };
    },
    closeDB() {
        //主动close一个connection(实在没什么意义,在被渣滓接纳机制消灭或建立上下文被destroy,connection会自动close)
        db.close();
    },
    deleteDB() {
        indexedDB.deleteDatabase('danote');
    },
    addData(data, cb) {
        this.startTransaction();
        //Object Store是indexedDB的重要贮存机制
        //IDBTransaction.objectStore()返回你查询的objectStore(IDBObjectStore对象)
        let objectStore = this.transaction.objectStore("diary");
        let request = objectStore.add(data);
        request.onsuccess = () => {
            if (cb) cb({
                error: 0,
                data: data
            })
        };
        request.onerror = () => {
            if (cb) cb({
                error: 1
            })
        };
    },
    clearObjectStore(id, cb) {
        this.startTransaction();
        let objectStore = this.transaction.objectStore("diary");
        let request = objectStore.clear();
        request.onsuccess = () => {
            if (cb) cb({
                error: 0,
                data: id
            });
        };
        request.onerror = () => {
            if (cb) cb({
                error: 1
            });
        };
    },
    addmData(mdata, cb) {
        this.startTransaction();
        let objectStore = this.transaction.objectStore("diary");
        for (let c = 0; c < mdata.length; c++) {
            let request = objectStore.add(mdata[c]);
            request.onerror = () => {
                if (cb) cb({
                    error: 1
                })
            }
        }
    },
    deleteData(id, cb) {
        this.startTransaction();
        let objectStore = this.transaction.objectStore("diary");
        let request = objectStore.delete(id);
        request.onsuccess = () => {
            if (cb) cb({
                error: 0,
                data: id
            })
        };
        request.onerror = () => {
            if (cb) cb({
                error: 1
            })
        }
    },
    getDataById(id, cb) {
        this.startTransaction();
        let objectStore = this.transaction.objectStore("diary");
        let request = objectStore.get(id);
        request.onsuccess = () => {
            if (cb) cb({
                error: 0,
                data: e.target.result
            })
        };
        request.onerror = () => {
            if (cb) cb({
                error: 1
            })
        }
    },
    getDataAll(cb) {
        this.startTransaction();
        let objectStore = this.transaction.objectStore("diary");
        let rowData = [];
        objectStore.openCursor(IDBKeyRange.lowerBound(0)).onsuccess = (e) => {
            let cursor = e.target.result;
            if (!cursor && cb) {
                cb({
                    error: 0,
                    data: rowData
                });
                return;
            }
            rowData.unshift(cursor.value);
            cursor.continue();
        };
    },
    updateData(id, updateData, cb) {
        this.startTransaction();
        let objectStore = this.transaction.objectStore("diary");
        let request = objectStore.get(id);
        request.onsuccess = e => {
            let thisDB = e.target.result;
            for (key in updateData) {
                thisDB[key] = updateData[key];
            }
            objectStore.put(thisDB);
            if (cb) cb({
                error: 0,
                data: thisDB
            })
        };
        request.onerror = e => {
            if (cb) cb({
                error: 1
            })
        }
    },
    getDataBySearch(keywords, cb) {
        this.startTransaction();
        let objectStore = this.transaction.objectStore("diary");
        let boundKeyRange = IDBKeyRange.only(keywords);
        let rowData = [];
        objectStore.index("folder").openCursor(boundKeyRange).onsuccess = e => {
            let cursor = e.target.result;
            if (!cursor) {
                if (cb) cb({
                    error: 0,
                    data: rowData
                })
                return;
            }
            rowData.push(cursor.value);
            cursor.continue();
        };
    },
    getDataByPager(start, end, cb) {
        this.startTransaction();
        let objectStore = transaction.objectStore("diary");
        let boundKeyRange = IDBKeyRange.bound(start, end, false, true);
        //关于keyRange https://www.w3.org/TR/IndexedDB/#dfn-key-range
        let rowData = [];
        objectStore.openCursor(boundKeyRange).onsuccess = e => {
            let cursor = e.target.result;
            if (!cursor && cb) {
                cb({
                    error: 0,
                    data: rowData
                });
                return;
            }
            rowData.push(cursor.value);
            cursor.continue();
        };
    }
}
//objectStore的称号在例子里全都写死了,由于我只建了一个objectStore,运用时发起还是以参数传进函数
    原文作者:ssshooter
    原文地址: https://segmentfault.com/a/1190000009069754
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞