Andrew Magee
Andrew Magee

Reputation: 6684

ydn-db: how to make any query more complex than getting a single record by id?

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?

Upvotes: 2

Views: 1232

Answers (2)

Andrew Magee
Andrew Magee

Reputation: 6684

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;
});

Upvotes: 1

Kyaw Tun
Kyaw Tun

Reputation: 13151

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

Upvotes: 1

Related Questions