简体   繁体   中英

How to get current data size of each objectStore in indexedDB programmatically?

I want to get current data size (Usage in MB/GB) of each objectStore in indexedDB, programmatically.

I think this might be popular question, but I can't find any similar questions/answers in StackOverflow.

Is this possible?

This code takes much time if the number of DB entries are many, but I can get the result by this code.

    const countDB = async (db, table) => {
        return new Promise((resolve, reject) => {
            const tx = db.transaction([table], 'readonly');
            const store = tx.objectStore(table);
            const cursorReq = store.openCursor();
            let count = 0;
            let size = 0;
            cursorReq.onsuccess = function(e) {
                const cursor = cursorReq.result;
                if (cursor) {
                    count++;
                    size = size + cursor.value.blob.size;
                    cursor.continue();
                }
            };
            cursorReq.onerror = function(e) {
                reject(e);
            };
            tx.oncomplete = function(e) {
                resolve({
                    count: count,
                    size: size
                });
            };
            tx.onabort = function(e) {
                reject(e);
            };
            tx.onerror = function(e) {
                reject(e);
            };
        });
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM