summaryrefslogtreecommitdiff
path: root/ext/js/data/database.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-03-13 11:25:10 -0500
committerGitHub <noreply@github.com>2021-03-13 11:25:10 -0500
commita00ed41838da08f5f81813443c0471a1d6be1291 (patch)
tree39d308b3fd744e70e33d2877a1d7a1b7dd2de4ff /ext/js/data/database.js
parent638e5783acc20a58ec4de258848e44d4470bc6cf (diff)
Database API updates (#1520)
* Add Database.findFirst * Add DictionaryDatabase.findTagMetaBulk
Diffstat (limited to 'ext/js/data/database.js')
-rw-r--r--ext/js/data/database.js35
1 files changed, 20 insertions, 15 deletions
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) {