【问题标题】:IndexedDB using YDN-DB: What is the most efficient way to retrieve the highest Primary Key value?使用 YDN-DB 的 IndexedDB:检索最高主键值的最有效方法是什么?
【发布时间】: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 方法很简单。

标签: indexeddb ydn-db


【解决方案1】:

这是很常见的操作。我会用

db.keys('store_name', null, 1, 0, true)

使用查询模块db.from 是上述方法的包装。对order 的回归感到抱歉,忽略了方向。我会修复的。感谢您的报告。

【讨论】:

  • 感谢您的及时回复。我一直在研究您建议的 db.keys() 语法。在决定如何最好地使用它之前,尝试了解它的所有论点和用法。现在我在这里:dev.yathit.com/ydn-db/schema.html。还有什么地方可以指导我吗?
猜你喜欢
  • 2014-10-25
  • 2014-02-26
  • 2012-12-03
  • 2014-02-03
  • 1970-01-01
  • 2014-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多