Bootstrap

indexeddb 二次封装

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);
    <
;