indexeddb 二次封装

本文档详细介绍了如何封装一个indexedDB使用类库,包括创建数据库、管理存储空间、版本控制以及各种操作方法如添加、获取、删除数据等。同时提供了异步操作的注意事项。
摘要由CSDN通过智能技术生成

1、封装一个indexedDB的使用类库(CreateDB)

// business database
const dbMap = {
   
    defaultDBName: "local_basic_db"
};

// Database default storage space (database table)
let defaultDBTable = "basic_table";
// default database version
let defaultVersion = 1;

class CreateDB {
   
    constructor (config = {
    }) {
   
        let dbName = config?.dbName || dbMap.defaultDBName;
        let dbTable = config?.dbTable || defaultDBTable;

		this.isInit = false;
        this.dbName = dbName;
        this.version = defaultVersion;
        this.dbTable = dbTable;
        this.databases = [];

        this.initPromise = this.initialization();
    }

    // initialization
    async initialization() {
   
        const database = await this.checkDB({
    dbName: this.dbName });

        if (window.indexedDB?.databases) {
   
            if (database) {
   
                this.version = database.version;
            }
        } else {
   
            const version = await new Promise((resolve, reject) => {
   
                const indexedDB = window.indexedDB.open(this.dbName);
        
                indexedDB.onupgradeneeded = (event) => {
   
                    console.log("verson indexedDB onupgradeneeded", event);
                    const db = event.target.result;
                    const version = db.version;

                    db.close();

                    resolve(version);
                };
            
                indexedDB.onsuccess = async(event) => {
   
                    console.log("verson indexedDB onsuccess", event);
                    const db = event.target.result;
                    const version = db.version;
    
                    db.close();
        
                    resolve(version); 
                };
            
                indexedDB.onerror = (event) => {
   
                    console.log("verson indexedDB onerror", event);

                    reject();
                };
            }).catch(err => {
   
                console.log("verson indexedDB error", err);
            });

            version && (this.version = version);
        }

        console.log("Initialization completed", database);
        this.isInit = true;
    }
    
	async initializationWait() {
   
        if (!this.isInit) {
   
            await this.initPromise;
        }
    }

    // Connect to the database
    openDB() {
   
        const db = window.indexedDB.open(this.dbName, this.version);

        return db;
    }

    // Get all databases under the browser
    async getDBS() {
   
        const res = await window.indexedDB.databases().catch(() => {
   
            return [];
        });
    
        this.databases = res;
    
        return res;
    }
    
    // Check if the specified database exists
    async checkDB({
     dbName }) {
   
        if (dbName) {
   
            const databaseList = await this.getDBS();

            if (databaseList.length) {
   
                let database;
                
                for (let i = 0; i < databaseList.length; i++) {
   
                    let curr = databaseList[i];
            
                    if (curr.name === dbName) {
   
                        database = curr;
                        break;
                    }
                }
            
                return database;
            }
        }
    
        return null;
    }
    
    // Check if the storage space exists
    checkTable({
     db, dbTable }) {
   
        const objectStoreNames = db.objectStoreNames;
    
        return objectStoreNames.contains(dbTable);
    <
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值