Add Database.getCount function

This commit is contained in:
Daniel Gasienica 2018-03-29 16:15:23 -04:00
parent 1f8556b049
commit 0fdc1140dd

View file

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