ydn-db:如何使任何查询比通过id获取单个记录更复杂?

时间:2013-09-09 01:54:24

标签: ydn-db

使用来自此处的打包的ydn-db库:http://git.yathit.com/ydn-db/downloads/ydn.db-jquery-0.7.12.jshttps://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.

打包库的功能似乎与文档不一致。有什么提示吗?

2 个答案:

答案 0 :(得分:1)

实际上,您的架构没有索引名称id,因此正确的代码应如下所示。

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")

错误信息绝对不能提供信息。我推了一个补丁,所以下次,它会给索引找不到错误。

open方法也有api变化。文档需要更新。

编辑:可以在http://dev.yathit.com/ydn-db/nosql-query.html

中找到更复杂的查询

答案 1 :(得分:1)

好的,最后从上一个回答的链接中得到了它:

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