diff --git a/js/modules/database.js b/js/modules/database.js index f89bac59528..f780a40318a 100644 --- a/js/modules/database.js +++ b/js/modules/database.js @@ -4,6 +4,9 @@ // and using promises. Revisit use of `idb` dependency as it might cover // this functionality. +const isObject = require('lodash/isObject'); + + exports.open = (name, version) => { const request = indexedDB.open(name, version); return new Promise((resolve, reject) => { @@ -37,3 +40,17 @@ exports.getVersion = async (name) => { connection.close(); return version; }; + +exports.getCount = async ({ store } = {}) => { + if (!isObject(store)) { + throw new TypeError('"store" is required'); + } + + const request = store.count(); + return new Promise((resolve, reject) => { + request.onerror = event => + reject(event.target.error); + request.onsuccess = event => + resolve(event.target.result); + }); +};