Dexie.js自动增量主键-是否重置过?如何重置? [英] Dexie.js Autoincrement Primary Key - does it ever reset? How to reset it?

查看:610
本文介绍了Dexie.js自动增量主键-是否重置过?如何重置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Dexie.js中,您可以创建具有自动递增键的商店

In Dexie.js you can create a store with an auto-incrementing key

    let db = new Dexie("nav_api");
    db.version(1).stores({
        jobs: '++id, json'
    });

因此,要进行测试,我通过db.jobs.put({json: '[]'})在数据库中创建了14个对象,它们的所有键均按预期方式输出,从1开始直到14.然后删除了后来的一些db.jobs.where('id').above(6).delete(),并添加了另一个一个到数据库,它的索引是15.

So to test, I created 14 objects in the db via db.jobs.put({json: '[]'}), and all their keys came out as expected, started at 1 up to 14. Then deleted some of the later ones, db.jobs.where('id').above(6).delete(), and added another one to the db, and its index was 15.

有什么办法可以将索引重置为0?我在使用它进行排序,但是我不确定该值变得非常大时会发生什么—最终它会回绕到0吗?

Is there any way to reset the index to 0? I was using it for ordering, and I'm not sure what happens when the value gets incredibly large -- does it eventually wrap back around to 0?

另一方面,这可能也不是我需要担心的事情.根据"++ id"字段的高低,并且我假设它会达到数十亿美元,这是很多年(很多生命)才需要担心的事情.所以也许我应该忽略它.

On the other hand, it's also probably not something I need to worry about. Depending on how high the '++id' field can be, and I'm assuming it will be in the billions, it would be many many years (many lifetimes) before it was ever something to worry about. So maybe I should just ignore it.

我尝试清除表db.jobs.clear(),然后在表中放置新行,但是它再次使用了之后的下一个索引.我找不到完全删除表的方法,但是我可以完全删除整个数据库,但是我真的不想这么做.

[edit] I tried clearing the table, db.jobs.clear() and then putting a new row in the table, but again it used the next index after. I can't find a way to fully delete a table, however I can fully delete an entire database but I don't really want to do that.

推荐答案

您可能不会达到上限:

当前数字始终是小于或等于2 53 (9007199254740992)+ 1的正整数.

The current number is always a positive integer less than or equal to 253 (9007199254740992) + 1.

除了删除对象存储并重新创建之外,没有其他方法可以重置它:

And there is no way to reset it, other than deleting the object store and recreating it:

除了由于还原数据库操作之外,密钥生成器的当前编号从不减少.从对象存储中删除记录不会影响对象存储的密钥生成器.即使清除对象存储库中的所有记录(例如使用clear()方法),也不会影响对象存储库的密钥生成器的当前数量.

The current number for a key generator never decreases, other than as a result of database operations being reverted. Deleting a record from an object store never affects the object store’s key generator. Even clearing all records from an object store, for example using the clear() method, does not affect the current number of the object store’s key generator.

这篇关于Dexie.js自动增量主键-是否重置过?如何重置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆