From 14c53ff7103926da7ab4b35e7ff950c4b2deb017 Mon Sep 17 00:00:00 2001 From: lilia Date: Thu, 18 Dec 2014 18:28:42 -0800 Subject: [PATCH] Support for lower-level queries on indexedDB superfeedr has done a nice job with this backbone -> indexedDB adapter, but their query interface is somewhat limited. This commit adds an alternate interface that lets us specify the index and cursor bounds we want. This interface requires deeper knowledge of indexedDB indices, but is more powerful overall. --- .../backbone-indexeddb.js | 19 +++++++++++++++++++ js/components.js | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/components/indexeddb-backbonejs-adapter/backbone-indexeddb.js b/components/indexeddb-backbonejs-adapter/backbone-indexeddb.js index 36735091..ac77b6ce 100644 --- a/components/indexeddb-backbonejs-adapter/backbone-indexeddb.js +++ b/components/indexeddb-backbonejs-adapter/backbone-indexeddb.js @@ -419,6 +419,25 @@ } } }); + } else if (options.index) { + index = store.index(options.index.name); + if (index) { + if (options.index.lower && options.index.upper) { + bounds = IDBKeyRange.bound(options.index.lower, options.index.upper); + } else if (options.index.lower) { + bounds = IDBKeyRange.lowerBound(options.index.lower); + } else if (options.index.upper) { + bounds = IDBKeyRange.upperBound(options.index.upper); + } else if (options.index.only) { + bounds = IDBKeyRange.only(options.index.only); + } + + if (typeof options.index.order === 'string' && options.index.order.toLowerCase() === 'desc') { + readCursor = index.openCursor(bounds, window.IDBCursor.PREV || "prev"); + } else { + readCursor = index.openCursor(bounds, window.IDBCursor.NEXT || "next"); + } + } } else { // No conditions, use the index if (options.range) { diff --git a/js/components.js b/js/components.js index 74b0f736..1890ca14 100644 --- a/js/components.js +++ b/js/components.js @@ -21777,6 +21777,25 @@ return jQuery; } } }); + } else if (options.index) { + index = store.index(options.index.name); + if (index) { + if (options.index.lower && options.index.upper) { + bounds = IDBKeyRange.bound(options.index.lower, options.index.upper); + } else if (options.index.lower) { + bounds = IDBKeyRange.lowerBound(options.index.lower); + } else if (options.index.upper) { + bounds = IDBKeyRange.upperBound(options.index.upper); + } else if (options.index.only) { + bounds = IDBKeyRange.only(options.index.only); + } + + if (typeof options.index.order === 'string' && options.index.order.toLowerCase() === 'desc') { + readCursor = index.openCursor(bounds, window.IDBCursor.PREV || "prev"); + } else { + readCursor = index.openCursor(bounds, window.IDBCursor.NEXT || "next"); + } + } } else { // No conditions, use the index if (options.range) {