使用IndexedDB封裝器JsStore您可以創建一個表,用於存儲具有自動增量列(例如Id)的郵件並使用此列(Id)對數據進行排序。
與本案#2凝視:
從發件人檢索所有的電子郵件
var Connection = new JsStore.Instance("Database_name");
Connection.select({
From: "Inbox", // Inbox is your table name
Where:{
Sender: sender_email
},
Order: {
By: "Id",
Type: "desc"
},
OnSuccess:function (results){
console.log(results);
},
OnError:function (error) {
console.log(error);
}
});
情況#1:
這將是很容易檢索如果我們可以在JsStore中使用GROUP BY子句,但是不幸的是它沒有在當前版本的JsStore中提供,但它可能會支持其next release。
直到那時我們可以過濾郵件的完整記錄或可以使用發件人的電子郵件ID一個接一個地獲取。
過濾上完整記錄:
var Connection = new JsStore.Instance("Database_name");
Connection.select({
From: "Inbox", // Inbox is the table name
Order: {
By: 'Id',
Type: "desc"
},
OnSuccess:function (results){
var sendersName = [];
var filteredMails = results.filter(function(mail){
if(sendersName.indexOf(mail.sender) == -1){
sendersName.push(mail.sender);
return mail;
}
});
console.log(filteredMails);
},
OnError:function (error) {
console.log(error);
}
});
一個獲取最新的郵件爲每個發件人一個推到一個數組:
var mails=[];
senders_email.forEach(function(value){ // keep the list of senders email in the array "sender_email"
Connection.select({
From:'Inbox',
Where:{
Sender:value
},
Limit:1,
Order: {
By: 'Id',
Type: "desc"
},
OnSuccess:function(result){
result.length > 0 && mails.push(result[0]);
},
OnError:function(err){
console.log(err);
}
})
});
你可以有它的SQL來看看用於查詢IndexedDB的official site的類型語法。您可以輕鬆地解釋上述代碼。
感謝您的建議。你會考慮在我的問題中描述一個獨立的商店'Latest'嗎?一個可行的選擇? –
除非這是唯一的方法。使用nosql dbs,您會追求更高性能而不是普通形式。 – Josh
請注意,next或prev unique可能無法正常工作,因爲發件人和時間都是關鍵路徑的一部分。也許嘗試使用提前或繼續只發件人,而如果可能的話。 – Josh