删除indexedDB对象存储中的所有记录以获取特定的索引值 [英] Deleting all records in indexedDB object store for a specific index value

查看:127
本文介绍了删除indexedDB对象存储中的所有记录以获取特定的索引值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于具有 [a,b] 的数组键(其中 a 也是索引)的对象存储,是否有一种更有效的方法来删除索引的所有记录 a 的特定值,而不是在索引 a 上打开游标并在游标中逐步删除每条记录?

For an object store with an array key of [a,b] where a is also an index, is there a more efficient way to delete all records for a specific value of a than opening a cursor on the index a and deleting each record as step through the cursor?

有没有一种方法可以只为索引定义键范围,或者仅在 a 上定义键范围,而使 b 保持任意值开放,从而可以删除以下项的所有记录:那个关键范围?

Is there a way to define a key range for an index only, or just on a and leave b open to any value, such that can delete all records for that key range?

在这种特殊情况下, a 是一个排除零的正整数,而 b 是一个包含零的正整数.是否可以保证从[n,0]到[n + 1,0]的键范围返回等于索引 a = n 的所有键,而不管 b ?例如,IDBKeyRange.bound([2,0],[3,0],false,true)将返回索引 a = 2 ?

In this particular case, a is a positive integer excluding zero and b is a positive integer including zero. Would a key range from [n,0] to [n+1,0] be guaranteed to return all keys for equivalent to index a=n regardless of the value of b? For example, IDBKeyRange.bound( [2,0], [3,0], false, true) would return all keys for index a=2?

以上键范围适用于我的测试用例,但是我想知道如果 b 不是整数,如何处理这种情况,使其成为一种特殊情况.

The above key range works for my test cases, but I'd like to know how to handle the situation if b wasn't an integer, making it sort of a special case.

以下内容似乎不起作用,因为它只会删除 key 2 的记录.一般情况下有这样的方法吗?

It appears that the following would not work because it would delete only a record with a key of 2. Is there a method like this for a general case?

i = transaction.objectStore( name ).index( 'a' );
i.delete( 2 );

谢谢.

随着我了解更多信息并查看了下面的代码以生成所需的结果,我不再确定为什么它可以正常工作.关键是复合 [topic,note] ,而 k 仅设置为主题值.因此, n 中的任何键都不应该与 k 相匹配,对吗?

As I learned more and looked over code below that is generating the desired result, I'm not sure anymore why it is working. The key is compound [ topic, note ] and k is set to only the topic value. So, no key in n should match k, should it?

我不明白为什么 n.openCursor(k)返回任何要处理的记录,因为没有一个简单的键.是将 k 视为记录的关键字还是索引值?

I don't understand why n.openCursor( k ) returned any records to work on, since none have a simple key. Is k being treated as the key of the records or the index value?

T = DB_open.base.transaction( ['notes'], 'readwrite' ),
o = T.objectStore( 'notes' ),
k = IDBKeyRange.only( topic_value );
n = T.objectStore( 'notes' ).index( 'topic' );

n.openCursor( k ).onsuccess = ( e ) =>  { /* stepped through cursor */ };

似乎我不了解的是索引的 key 参数不是实际记录的键,而是索引的键,这里是主题值.对于读取操作和游标,效果很好;但是索引上没有删除方法,例如等效于 getAll deleteAll .

It appears that what I wasn't understanding is that the key parameter for an index is not the key of the actual record but the key of the index, here the topic value. For read operations and cursors, that works fine; but there is not a delete method on an index, such as a deleteAll equivalent to getAll.

我想我在几个月前编写游标代码时一定已经理解了这一点,但是现在让我感到困惑,因为他们试图在不打开游标的情况下删除具有特定索引值的一组记录.

I think I must have understood this several months ago when I wrote the cursor code but now got myself confused in trying to delete a set of records for a specific index value without opening a cursor.

推荐答案

似乎我不了解的是索引的 key 参数不是实际记录的键,而是索引的键,这里是主题值.对于读取操作和游标,效果很好;但是索引上没有删除方法,例如等效于 getAll deleteAll .

It appears that what I wasn't understanding is that the key parameter for an index is not the key of the actual record but the key of the index, here the topic value. For read operations and cursors, that works fine; but there is not a delete method on an index, such as a deleteAll equivalent to getAll.

您是正确的, key 是索引键.而且没有单个命令说删除与索引中某个键或键范围匹配的每条记录".这里有一些讨论-我的解释是,没有很好的论点相对于现有功能而言,但这是一个非常罕见的用例,它只是作为未实现的功能请求而存在.

You are correct, key is the index key. And there is no single command to say "delete every record matching a certain key or key range in an index". Here is some discussion about this - my interpretation is that there isn't a great argument against that functionality existing, but it's a rare enough use case that it's just sat there as an unimplemented feature request.

但是,如果主键是复合键,并且复合键中的第一个条目是要过滤的字段,则可以使用 KeyRange 并将其传递给 IDBObjectStore.按照您的建议删除:

However if the primary key is an compound key and the first entry in the compound key is the field you want to filter on, you can use a KeyRange and pass it to IDBObjectStore.delete like you suggested:

在这种特殊情况下, a 是一个排除零的正整数,而 b 是一个包含零的正整数.是否可以保证从[n,0]到[n + 1,0]的键范围返回等于索引 a = n 的所有键,而不管 b ?例如,IDBKeyRange.bound([2,0],[3,0],false,true)将返回索引 a = 2 ?

In this particular case, a is a positive integer excluding zero and b is a positive integer including zero. Would a key range from [n,0] to [n+1,0] be guaranteed to return all keys for equivalent to index a=n regardless of the value of b? For example, IDBKeyRange.bound( [2,0], [3,0], false, true) would return all keys for index a=2?

是的,那行得通.

您也可以自己玩看它:

var range = IDBKeyRange.bound( [2,0], [3,0], false, true);
console.log(range.includes([2, -1])); // false
console.log(range.includes([2, 0])); // true
console.log(range.includes([2, 100])); // true
console.log(range.includes([2, Infinity])); // true
console.log(range.includes([3, 0])); // false

只是为了好玩...您还可以这样定义键范围:

Just for fun... you could also define your key range like this:

var range = IDBKeyRange.bound( [2,0], [2,""], false, true);

因为数字在字符串之前排序.您可以通过多种其他方式来编写相同的内容.一切都取决于如何在IndexedDB规范中完成跨类型比较.一旦有了数组或多个类型,事情就会变得很有趣.

since numbers sort before strings. There's a bunch of other ways you could write the same thing. It all comes down to how cross-type comparisons are done in the IndexedDB spec. As soon as you have arrays or multiple types, things get interesting.

这篇关于删除indexedDB对象存储中的所有记录以获取特定的索引值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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