2016-04-01 6 views
0

私はindexedDBにはfindOneやfind関数(MongoDBのように)はありませんが、私が達成したいのはそれらの関数と似ています。indexedDBのfindとfindOne

私はindexedDBにデータストアを持っています。私はstop_idなどを使って店舗のインデックスを作成しました。

店内にはstop_idのすべての書類があります。複数のオブジェクトは1つのstop_id値を持つことがあります。

私が持っているもの:

私は(より良い方法があるなら、私を修正してください)

// this function is called from html via angularjs 
    $scope.findOne = function(stop_id) { 
     var db; 
     var request = indexedDB.open("Trans"); 
     request.onerror = function(event) { 
      alert("Couldn't connect to Database"); 
     }; 
     request.onsuccess = function(event) { 
      db = event.target.result; 
      var objectStore = db.transaction("times").objectStore("times"); 
      var index = objectStore.index("stop_id"); 

      var range = IDBKeyRange.only(stop_id); 

      // call something here to retrieve 
      // One or All documents with the ID of stop_id 
      // passed in from the html 
     } 
    } 

私は、ようにHTMLで呼び出すしたいビットをカンニングしようとしています:

<div class="medium-6 columns" ng-repeat="stops in objects | orderBy: 'stop_name'"> 
     <div class="card hoverable"> 
      <div class="content"> 
       <span class="title">{{stops.stop_name}}</span><small class="float-right">{{stops.stop_id}}</small> 
       <!-- this function will search another object store, then retrieve all documents matching the stop_id --> 
       {{ findOne(stops.stop_id)}}</p> 
      </div> 
     </div> 
    </div> 

indexedDBは結合をネイティブにサポートしていないため、私は上記のアプローチを検討しています。ネイティブのindexeddbの回避策その場で別のデータストア内のIDに関連する余分なデータを取得することができます。パフォーマンスは問題ではありません。両方のデータストア文書は150以上のアイテムではありません。

答えて

0

IDBIndex getまたはopenCursorメソッドを使用します。

// for one 
var range = IDBKeyRange.only(myId); 
var getRequest = index.get(range); 
getRequest.onsuccess = function(event) { 
    var result = event.target.result; 
    console.log(result); 
}; 

// for multiple ... 
var getRequest = index.openCursor(range); 
var documentsFound = []; 
getRequest.onsuccess = function(event) { 
    var request = event.target; 
    var cursor = request.result; 
    if(!cursor) { 
    console.log('no match found, or no more matches found'); 
    someFunction(documentsFound); 
    } else { 
    console.log('Found:', cursor.value); 
    documentsFound.push(cursor.value); 
    cursor.advance(); 
    } 
}; 

ストア内の複数のオブジェクトは、あなたのonupgradeneededハンドラでインデックスを作成するときにunique:trueフラグを使用していないことを確認した後、同じstop_idを持つことができます。

+0

ありがとうございました。試してみましょう。そして、はい、私はユニークなものを作りませんでした:彼らに真のインデックス – Rexford

+0

私は期待どおりに動作しません。私はコンソールで一貫性のない結果を繰り返すことになる。最初からクエリが複数のオブジェクトになるかどうかわからないので、上記の複数のオプションを使用しました。私はおそらく何か正しいことをしていないでしょう。私はこのindexeddbの全体を手に入れるために、より明確なことを理解するでしょう。 – Rexford

+0

上記の複数のレコードを繰り返し処理するセクションは、同じオブジェクトを2度返すことはありませんが、類似したオブジェクトを返す可能性があります。これは、指定されたstop_idパラメーターを持つすべてのオブジェクトをリストするだけです。マッチングストップを配列に押し込む例を示しました。おそらく、あなたはそのような呼び出しの間に配列をリセットしていないでしょうか? – Josh

関連する問題