Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using a field with a Boolean type in dexie.js

Does not work select by value 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);
});
like image 965
Евгений Куркин Avatar asked Oct 20 '25 02:10

Евгений Куркин


1 Answers

Booleans cannot be indexed in Dexie. See the doco: https://dexie.org/docs/Indexable-Type

The following javascript types are possible to index:

  • string
  • number
  • Date
  • Arrays of strings, numbers, Dates or a mix of those.
  • ArrayBuffer (IndexedDB 2.0 only)
  • Typed arrays (IndexedDB 2.0 only)

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. 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:

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.

like image 174
Tom Saleeba Avatar answered Oct 22 '25 17:10

Tom Saleeba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!