2

Using the packaged ydn-db library from here: http://git.yathit.com/ydn-db/downloads/ydn.db-jquery-0.7.12.js . Same happens with https://storage.cloud.google.com/download.yathit.com/ydn-db/build/zsk-ydn.db-dev-0.7.15.js .

var db = new ydn.db.Storage("mydb");
var clog = function(r) { console.log(r); }

db.put({name: "store1", keyPath: "id"}, {id: "id1", value: "value1"});
db.put({name: "store1", keyPath: "id"}, {id: "id2", value: "value2"});

db.get("store1", "id1").done(clog); // works fine

// Example as mentioned in the docs here: http://dev.yathit.com/ydn-db/getting-started.html
var reverse = false, limit = 10;
db.values(new ydn.db.Cursors("store1", "id", null, reverse), limit).done(clog)
// TypeError: Cannot call method 'getName' of null

// Also adapted from docs:
db.values(ydn.db.IndexValueCursors.where("store1", "id", "=", "id1")).done(clog)
// TypeError: Cannot call method 'getName' of null

// Also adapted from docs:
var iter = new ydn.db.ValueCursors("store1", ydn.db.KeyRange.starts("a"))
db.open(iter, clog, "readwrite")
// ydn.error.ArgumentException: Second argument must be cursor range iterator.

The functionality of the packaged library just doesn't seem to align with the docs. Any tips?

Andrew Magee
  • 6,506
  • 4
  • 35
  • 58

2 Answers2

1

Actually, your schema don't have index name id, so the correct code should be as follow.

var db = new ydn.db.Storage("mydb");
var clog = function(r) { console.log(r); }

db.put({name: "store1", keyPath: "id"}, {id: "id1", value: "value1"});
db.put({name: "store1", keyPath: "id"}, {id: "id2", value: "value2"});

db.get("store1", "id1").done(clog); // works fine


 // Example as mentioned in the docs here: http://dev.yathit.com/ydn-db/getting-started.html
var reverse = false, limit = 10;
db.values(new ydn.db.KeyCursors("store1", null, reverse), limit).done(clog)
// TypeError: Cannot call method 'getName' of null

// Also adapted from docs:
db.values(ydn.db.ValueCursors.where("store1",  "=", "id1")).done(clog)
// TypeError: Cannot call method 'getName' of null

// Also adapted from docs:
var iter = new ydn.db.ValueCursors("store1", ydn.db.KeyRange.starts("i"))
var pnt = function(r) {console.log(r.getValue())}
db.open(pnt, iter, "readwrite")

Error message is definitely not informative. I push a patch, so next time, it gives index not found error.

There is api changes in open method, as well. Documentation need to be update.

Edit: more complex query can be found in http://dev.yathit.com/ydn-db/nosql-query.html

Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83
  • These snippets do produce output with no errors, but I'm unclear on what these queries actually do now that no column is actually specified. How would I say, sort by the `value` column, or do a query analogous to `select * from store1 where value = 'value1'`? If I need to do something with a schema that's fine. – Andrew Magee Sep 09 '13 at 05:27
  • I agree that it is not clear without consulting documentation. I am also thinking LINQ like query interface on top of these range iterators. Currently ydn-db expose as IndexedDB API provided. – Kyaw Tun Sep 09 '13 at 05:51
  • Ok but is it possible to do these things with the current API at all? – Andrew Magee Sep 09 '13 at 06:44
  • The last second query is `select * from store1 where value = 'value1'`. YDN-DB provide all query possible with IndexedDB. You have to be specific about query. I don't know how *complex query*, you means. – Kyaw Tun Sep 09 '13 at 07:21
  • Sorry I don't see how any of the queries can be `select * from store1 where value = 'value1'` since none of them reference `value`... – Andrew Magee Sep 09 '13 at 12:04
  • Sorry, yes `id` have to be changed to `value` and `value` must be defined as index in database schema. – Kyaw Tun Sep 10 '13 at 04:58
  • Can you give an example? I couldn't get any queries referencing any columns to work, hence my original question. The queries in your answer don't reference any columns at all so I can't use them either. – Andrew Magee Sep 10 '13 at 11:40
1

Ok, finally got it from the link in the previous answer:

// Equivalent to: SELECT * FROM store1 where value = 'value1':
var keyRange = ydn.db.KeyRange.only("value1");
var cursor = new ydn.db.IndexValueCursors("store1", "value", keyRange);
db.get(cursor, keyRange).then(function(record) {
   console.log("record:", record);
}, function(err) {
   throw err;
});
Andrew Magee
  • 6,506
  • 4
  • 35
  • 58