【发布时间】:2014-08-10 07:25:01
【问题描述】:
在使用 YDN-DB 包装器为 IndexedDB 构建离线应用程序时,我现在需要在任何对象存储中查询存储中最高的 Integer 主键值。通过扩展,我可以预测并使用下一个 autoIncrement 数字,甚至在运行下一个 INSERT 之前。
目前我可以想到以下方法:
db.values('store_name').done(function (recordset) {
//run through the key/value pairs in recordset until I arrive at the
highest value.
});
或许:
db.from('store_name').select('primary_key').order('primary_key', true).list(1)
.done(function(value) {
//use value for some task;
});
有没有更有效的方法?
虽然我们在上面,但.order('primary_key', true) 中指定的"primary_key" 参数对我来说似乎是多余的,因为这种排序通常是按主键。如果正确,跳过它的语法是什么?
谢谢。
编辑:
在等待的时候,我意识到,事实上,对于.order('primary_key', true),true(==reverse) 的第二个参数完全被查询引擎忽略了。但下面的表格来拯救:
db.from('store_name').select('primary_key').reverse().list(1)
.done(function(highest){
console.log(highest);
});
虽然这看起来有点时尚,但我仍然渴望得到“教授”的认可。
谢谢。
【问题讨论】:
-
更新
order的 API 文档不支持第二个参数,方向。如果有先前的订单方向,那就令人困惑了。reverse方法很简单。