From a00ed41838da08f5f81813443c0471a1d6be1291 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 13 Mar 2021 11:25:10 -0500 Subject: Database API updates (#1520) * Add Database.findFirst * Add DictionaryDatabase.findTagMetaBulk --- ext/js/data/database.js | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) (limited to 'ext/js/data') diff --git a/ext/js/data/database.js b/ext/js/data/database.js index 068f4a5f..f44ea1d9 100644 --- a/ext/js/data/database.js +++ b/ext/js/data/database.js @@ -111,27 +111,32 @@ class Database { } } - find(objectStoreName, indexName, query, predicate=null, defaultValue) { + find(objectStoreName, indexName, query, predicate, predicateArg, defaultValue) { return new Promise((resolve, reject) => { const transaction = this.transaction([objectStoreName], 'readonly'); const objectStore = transaction.objectStore(objectStoreName); const objectStoreOrIndex = indexName !== null ? objectStore.index(indexName) : objectStore; - const request = objectStoreOrIndex.openCursor(query, 'next'); - request.onerror = (e) => reject(e.target.error); - request.onsuccess = (e) => { - const cursor = e.target.result; - if (cursor) { - const value = cursor.value; - if (typeof predicate !== 'function' || predicate(value)) { - resolve(value); - } else { - cursor.continue(); - } + this.findFirst(objectStoreOrIndex, query, resolve, reject, predicate, predicateArg, defaultValue); + }); + } + + findFirst(objectStoreOrIndex, query, resolve, reject, predicate, predicateArg, defaultValue) { + const noPredicate = (typeof predicate !== 'function'); + const request = objectStoreOrIndex.openCursor(query, 'next'); + request.onerror = (e) => reject(e.target.error); + request.onsuccess = (e) => { + const cursor = e.target.result; + if (cursor) { + const {value} = cursor; + if (noPredicate || predicate(value, predicateArg)) { + resolve(value); } else { - resolve(defaultValue); + cursor.continue(); } - }; - }); + } else { + resolve(defaultValue); + } + }; } bulkCount(targets, resolve, reject) { -- cgit v1.2.3