2016-09-26 31 views
0

現在、私は2つの機能を持っています。特定のキーと値のペアを持つ結果と特定のキーでソートされたgetAll行のDBを照会するためのものです。 これらの2つを組み合わせる方法はありますか?だから私はあなたがa = 1がb ascでソートされているオブジェクトのストアを検索する関数を持っています。あなたの助けのためのIndexedDBキーとソートの検索

getSorted : function(store,sortKey,callback){ 
    var results = []; 
    this._initTransaction(store,"readonly",function(err,transaction){ 

     var index = transaction.objectStore(store).index(sortKey); 
     index.openCursor(null,'next').onsuccess = function(event) { 
      var cursor = event.target.result; 
      if (cursor) { 
      results.push(cursor.value); 
      cursor.continue(); 
      } else { 
      callback(true,results); 
      } 
     }; 
    })  
}, 

ありがとう:キーでソート結果を得るために

getByIndex : function(store,indexKey,indexValue,callback){ 
    var results = []; 
    this._initTransaction(store,"readonly",function(err,transaction){ 

     var index = transaction.objectStore(store).index(indexKey); 
     index.openCursor(IDBKeyRange.only(indexValue)).onsuccess = function(event) { 
      var cursor = event.target.result; 
      if (cursor) { 
      results.push(cursor.value); 
      cursor.continue(); 
      } else { 
      callback(true,results); 
      } 
     }; 
    }) 
} 

機能:インデックスで結果を得るために

機能!

答えて

0

二つのプロパティにインデックスを作成します。

function myOnUpgradeNeeded(event) { 
    // ... 
    var index = store.createIndex('myIndex', ['a', 'b']); 
} 

は、インデックスの上に有界でカーソルを開く機能を定義し、反復:

function query(db, a, callback) { 
    if(!a) { 
    throw new TypeError('this only works if a is defined'); 
    } 

    var results = []; 
    var tx = db.transaction('store'); 
    tx.oncomplete = function(event) { 
    callback(results); 
    }; 
    var store = tx.objectStore('store'); 
    var index = store.index('myIndex'); 

    var lowerBound = [a]; 

    // How you define the upper bound depends on the type. This assumes 
    // that a is a number. For example, if a is a string, you need to 
    // use a different boundary definition. See the gist by Mr. Bell 
    // below for an example of how to create other types of bounds. You 
    // simply need to determine whatever is the 'next' value that would 
    // come after `a`s value in your data. 
    var upperBound = [a+1]; 

    var range = IDBKeyRange.bound(lowerBound, upperBound, false, true); 
    var request = index.openCursor(range); 
    request.onsuccess = function(event) { 
    var cursor = event.target.result; 
    if(cursor) { 
     results.push(cursor.value); 
     cursor.continue(); 
    } 
    }; 
} 

そしてそれを呼び出す:

var request = indexedDB.open(...); 
request.onsuccess = function(event) { 
    var db = event.target.result; 
    query(db, 1, function(results) { 
    // ... 
    }); 
}; 

これは、異なる長さの配列キーパスを比較するため、インデックスが[a、b]で定義されていても、r angeは[a] ... [a + 1]だけを比較します。異なる長さの配列キーパスを比較することはまったく問題ありません。比較では、値が短い配列の長さまでしか比較されません。

インデックスのエントリの順番は[a、b]です。ですから、インデックスエントリをaを含むものに限定すれば、bによって順序付けられたすべてのaがターゲットaであるサブセットが得られます。これは私が目的であると理解しているものです。

+1

'' a = 1 sorted by b asc ''のインデックスを '['a'、 'b']'にするには、 'IDBKeyRange.bound([1]、[2 ]、false、true) 'を返します。インデックスを元に戻すと、!= 1の値が表示されます。答えを更新する際に刺すようにしたいですか? –

+0

ありがとうジョシュ、私はそれを修正したと思う。あなたはまだ回答を編集するのに十分なポイントがありますか?もしそうなら、あなたを歓迎します。 – Josh

+1

IDBKeyRange.bound([a]、[a])は可能なキーを1つだけ含むキー範囲です。プレフィックスの一致ではありません。私は+1 –