how can i get current data size of each object stored in indexeddb?

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

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top