Following an example from the docs here http://dev.yathit.com/ydn-db/getting-started.html, the first example under "Sorting".
My code:
var schema = {
stores: [
{
name: "authors",
keyPath: "id",
indexes: [
{ keyPath: "born" }
]
}
]
};
var db = new ydn.db.Storage("library", schema);
db.put("authors", [{ id: "111", born: "zzz" }, { id: "555", born: "bbb" }, { id: "999", born: "aaa" }]).done(function() {
// query with default ordering
db.values("authors").done(function(r) {
console.log("A list of objects as expected", r);
});
// query by ordered by "born" field
db.values(new ydn.db.Cursors("authors", "born", null, false)).done(function(r) {
console.log("This is a list of ids, not objects", r);
});
});
Changing the query from default ordering to ordering by a particular column seems to change its behaviour from returning a list of objects to just returning a list of ids. Am I doing something wrong? How do I get a list of objects?
It should be
// query by ordered by "born" field
db.values(new ydn.db.IndexValueCursors("authors", "born", null, false)).done(function(r) {
console.log("list of objects sorted by born", r);
});
or
// query by ordered by "born" field
db.values("authors", "born", null, false).done(function(r) {
console.log("list of objects sorted by born", r);
});
or simply
db.values("authors", "born").done(function(r) {
console.log("list of objects sorted by born", r);
});
A good API should do these common query very easily without reading documentation. I will think better API. For now, you got to read how iterator work: http://dev.yathit.com/api-reference/ydn-db/iterator.html Reference value of ydn.db.Cursors
is primary key. That is why
values
return list primary keys. Whereas reference value of ydn.db.IndexValueCursors
is
record value, so values
return list of objects. In fact, these are how IndexedDB API work.
Another point is above two queries has different performance characteristics. Second method, direct query is faster than first method, using iterator. This is because, iterator will iterate, whereas second method will use batch query. Performance is much different on websql since it does not support iteration.