简体   繁体   English

在dexie.js中使用具有布尔类型的字段

[英]using a field with a Boolean type in dexie.js

Does not work select by value true. 按值true选择不起作用。 Please tell me what is the problem? 请告诉我是什么问题?

const db = new Dexie('ToDo_DB');

db.version(1).stores({
    list: '++id, task, dateTime, done'
});

db.list.clear();

db.list.add({task: "1 task", dateTime: Date.now(), done: true});
db.list.add({task: "2 task", dateTime: Date.now(), done: false});
db.list.add({task: "3 task", dateTime: Date.now(), done: true});
db.list.add({task: "4 task", dateTime: Date.now(), done: false});

db.list.where('done').equals(false).each(function(item) {
    console.log('Found: ' + item.task + ' with date ' + item.dateTime);
});

Booleans cannot be indexed in Dexie. 布尔值无法在Dexie中建立索引。 See the doco: https://dexie.org/docs/Indexable-Type 参见文档: https ://dexie.org/docs/Indexable-Type

The following javascript types are possible to index: 可以对以下javascript类型进行索引:

  • string
  • number
  • Date 日期
  • Arrays of strings, numbers, Dates or a mix of those. 字符串,数字,日期或它们的混合数组。
  • ArrayBuffer (IndexedDB 2.0 only) ArrayBuffer(仅限IndexedDB 2.0)
  • Typed arrays (IndexedDB 2.0 only) 类型化数组(仅适用于IndexedDB 2.0)

Note that all other types are non-indexable, including: 请注意,所有其他类型都是不可索引的,包括:

  • boolean 布尔值
  • undefined 未定义
  • Object 宾语
  • null 空值

I'm trying to think of a good alternative to this at the moment but a simple solution would be to use a number with 0=false and 1=true. 我目前正在尝试寻找一种更好的替代方法,但是一个简单的解决方案是使用0 = false和1 = true的数字。 This has the benefit that you can still rely on the index of the database to do your query: 这样的好处是您仍然可以依靠数据库的索引来执行查询:

const db = new Dexie('ToDo_DB');

db.version(1).stores({
    list: '++id, task, dateTime, done'
});

db.list.clear();

db.list.add({task: "1 task", dateTime: Date.now(), done: 1});
db.list.add({task: "2 task", dateTime: Date.now(), done: 0});
db.list.add({task: "3 task", dateTime: Date.now(), done: 1});
db.list.add({task: "4 task", dateTime: Date.now(), done: 0});

db.list.where('done').equals(0).each(function(item) {
    console.log('Found: ' + item.task + ' with date ' + item.dateTime);
});

If you don't have much data in your store and don't need to index, you can use your own filter() functions, like: 如果您的存储中没有太多数据并且不需要索引,则可以使用自己的filter()函数,例如:

const db = new Dexie('ToDo_DB');

db.version(1).stores({
    list: '++id, task, dateTime, done'
});

db.list.clear();

db.list.add({task: "1 task", dateTime: Date.now(), done: true});
db.list.add({task: "2 task", dateTime: Date.now(), done: false});
db.list.add({task: "3 task", dateTime: Date.now(), done: true});
db.list.add({task: "4 task", dateTime: Date.now(), done: false});

db.list.filter(e => e.done === false).each(function(item) {
    console.log('Found: ' + item.task + ' with date ' + item.dateTime);
});

If you have large objects (with blobs) or lots of records, this is probably not a very performant option. 如果您有大对象(带有斑点)或很多记录,那么这可能不是一个非常有效的选择。 You should use the index instead. 您应该改用索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM