IndexDB

作者: 叶小七的真命天子 | 来源:发表于2021-12-15 16:08 被阅读0次
    const databaseName = 'shop-cart'
    const tableName = 'goods'
    
    export default new class IndexDB {
        constructor() {
            this.connectDB()
        }
        connectDB() {
            const request = window.indexedDB.open(databaseName);
            request.onerror = () => {
                console.log('数据库打开报错');
            };
            request.onsuccess = () => {
                this.db = request.result;
                console.log('数据库打开成功');
            };
            request.onupgradeneeded = (event) => {
                console.log('数据库onupgradeneeded');
                this.db = event.target.result;
                if (!this.db.objectStoreNames.contains(tableName)) {
                    const objectStore = this.db.createObjectStore(tableName, { keyPath: 'id' });
                    objectStore.createIndex('name', 'name', { unique: false });
                }
            }
        }
        getTransaction() {
            return this.db.transaction([tableName], 'readwrite').objectStore(tableName)
        }
        add(record) {
            return new Promise((resolve, reject) => {
                try {
                    const request = this.getTransaction().add(record);
                    request.onsuccess = () => {
                        resolve()
                    };
                    request.onerror = (event) => {
                        reject(event)
                    }
                } catch (error) {
                    reject(error)
                }
            })
        }
        getRecordById(id) {
            return new Promise((resolve, reject) => {
                try {
                    const objectStore = this.getTransaction()
                    const request = objectStore.get(id);
                    request.onerror = (event) => {
                        reject(event)
                    };
                    request.onsuccess = () => {
                        resolve(request.result ? request.result : {})
                    };
                } catch (error) {
                    reject(error)
                }
            })
        }
    
        getAll() {
            return new Promise((resolve, reject) => {
                try {
                    const result = []
                    const openCursor = this.getTransaction().openCursor()
                    openCursor.onsuccess = (event) => {
                        const cursor = event.target.result;
                        if (cursor) {
                            result.push(cursor.value)
                            cursor.continue();
                        } else {
                            resolve(result)
                        }
                    };
                    openCursor.onerror = (event) => {
                        reject(event)
                    }
                } catch (error) {
                    reject(error)
                }
            })
        }
    
        update(record) {
            return new Promise((resolve, reject) => {
                try {
                    const request = this.getTransaction().put(record);
                    request.onsuccess = () => {
                        resolve()
                    };
                    request.onerror = (event) => {
                        reject(event)
                    }
                } catch (error) {
                    reject(error)
                }
            })
        }
    
        removeById(id) {
            return new Promise((resolve, reject) => {
                try {
                    const request = this.getTransaction().delete(id);
                    request.onsuccess = () => {
                        resolve()
                    };
                    request.onerror = (event) => {
                        reject(event)
                    };
                } catch (error) {
                    reject(error)
                }
            })
        }
    
        getByName(name) {
            return new Promise((resolve, reject) => {
                try {
                    const result = []
                    const index = this.getTransaction().index('name');
                    const request = index.openCursor(name);
                    request.onsuccess = function (e) {
                        const cursor = e.target.result;
                        if (cursor) {
                            result.push(cursor.value)
                            cursor.continue();
                        } else {
                            resolve(result)
                        }
                    }
                    request.onerror = (event) => {
                        reject(event)
                    };
                } catch (error) {
                    reject(error)
                }
            })
        }
    
    }()
    

    相关文章

      网友评论

          本文标题:IndexDB

          本文链接:https://www.haomeiwen.com/subject/hhdpfrtx.html