Reputation: 91
How can we get last 40 records from all records of a table.
As far as i know we can get the count of number of records which exits in table of indexed db.
For the same...
function doCount(){
db.friends.toCollection().count(function (count) {
console.log(count + " friends in total");
});
Above will return whatever the count is in table.Now i wanted to get the last 40 records from table.Every time table will have different records. is it possible to do so. Can we also use .each with count!!I wanted to get count and as well as whatever the data that key holds i need to return those data and display it.
var db = new Dexie("MyDB");
db.version(1).stores({
friends: "id,name,shoeSize"
});
db.open();
//
// Populate some data
//
function populateSomeData() {
log("Populating some data", "heading");
return db.transaction("rw", db.friends, function () {
db.friends.clear();
db.friends.add({ id:1,name: "David", shoeSize: 43 });
db.friends.add({ id:2,name: "Ylva", shoeSize: 37 });
db.friends.add({ id:3,name: "Jon", shoeSize: 44 });
db.friends.add({id:4, name: "Måns", shoeSize: 42 });
db.friends.add({ id:5,name: "Daniel", shoeSize: 39 });
db.friends.add({ id:6,name: "Nils", shoeSize: 45 });
db.friends.add({ id:7,name: "Zlatan", shoeSize: 47 });
// Log data from DB:
db.friends.orderBy('name').each(function (friend) {
log(JSON.stringify(friend));
});
}).catch(function (e) {
log(e, "error");
});
}
//
// Examples
//
function equalsAnyOf() {
log("db.friends.where('name').anyOf('David', 'Zlatan', 'Daniel')", "heading");
return db.friends.where('name').anyOf('David', 'Zlatan', 'Daniel')
.each(function (friend) {
log(JSON.stringify(friend));
});
}
function equalsIgnoreCase() {
log("db.friends.where('name').equalsIgnoreCase('david')", "heading");
return db.friends.where('name').equalsIgnoreCase('david')
.each(function (friend) {
log(JSON.stringify(friend));
});
}
function startsWithIgnoreCase() {
log("db.friends.where('name').startsWithIgnoreCase('da')", "heading");
return db.friends.where('name').startsWithIgnoreCase('da')
.each(function (friend) {
log(JSON.stringify(friend));
});
}
function logicalOR() {
log("db.friends.where('name').startsWithIgnoreCase('da').or('shoeSize').below(40)", "heading");
return db.friends.where('name').startsWithIgnoreCase('da')
.or('shoeSize').below(40)
.each(function (friend) {
log(JSON.stringify(friend));
});
}
function logicalAND() {
log("db.friends.where('name').startsWithIgnoreCase('da').and(function (friend) { return friend.shoeSize > 40; })", "heading");
return db.friends.where('name').startsWithIgnoreCase('da')
.and(function (friend) { return friend.shoeSize > 40; })
.each(function (friend) {
log(JSON.stringify(friend));
});
}
var lakho =[];
function doCount(){
db.friends.toCollection().count(function (count) {
lakho = count;
console.log(count + " friends in total");
var div = document.getElementById('po');
div.innerHTML = div.innerHTML + count;
});
console.log(lakho + "lakho");
db.friends.where('id').between(1,3).count(function (count) {
console.log(count + " friends in total");
var div = document.getElementById('po');
div.innerHTML = div.innerHTML + count;
});
db.friends
.where('shoeSize').above(9)
.each (function (friend) {
console.log("Found big friend: " + friend.name);
});
}
//
// Helpers
//
function log(txt, clazz) {
var li = document.createElement('li');
li.textContent = txt.toString();
if (clazz) li.className = clazz;
document.getElementById('log').appendChild(li);
}
function runSamples() {
populateSomeData()
.then(equalsAnyOf)
.then(equalsIgnoreCase)
.then(startsWithIgnoreCase)
.then(logicalOR)
.then(logicalAND)
.then(doCount)
.catch(function (e) {
log(e, "error");
});
}
<script src="https://unpkg.com/dexie@latest/dist/dexie.js"></script>
<body onload="runSamples();">
<ul id="log"></ul>
<div id="po"> </div>
</body>
Upvotes: 2
Views: 3619
Reputation: 91
This is the way i did it.
db.friends.orderBy('id')
.reverse()
.limit(3)
.toArray()
.then(function (results) {
console.log (JSON.stringify(results));
});
Ref : git hub ref
Upvotes: 2
Reputation: 5671
You can use
db.yourTable
.orderBy('id')
.reverse()
.limit(40)
.each(callback)
It will iterate the last 40 records.
Upvotes: 0