javascript - DexieJS (indexedDB) chain multiple .where clauses - Stack Overflow

I'm using DexieJS to fetch data from an IndexedDB. I've done the following tests both with v.

I'm using DexieJS to fetch data from an IndexedDB. I've done the following tests both with v. 1.1.0 and 1.2.0.

It is working great for simple queries, but unfortunately I am unable to chain multiple where clauses.

First, I've tried this

var collection = db[table];
collection = collection.where('Field').equals("1");
return collection.count();

And that was working. Then, I need to add a where clause, but only if a given value is set:

var collection = db[table];
collection = collection.where('Field').equals("1");
if(value) collection = collection.where('Field2').above(value);
return collection.count();

This one fails. For test purposes, I've also tried:

var collection = db[table];
collection = collection.where('Field').equals("1")
.and('Field2').above(value);
return collection.count();

var collection = db[table];
collection = collection.where('Field').equals("1")
.and().where('Field2').above(value);
return collection.count();

var collection = db[table];
collection = collection.where('Field').equals("1")
.where('Field2').above(value);
return collection.count();

None of these work. I'm starting to think that this is not possible at all, but since the method and() exists, there must be a way!

PS this works:

var collection = db[table];
collection = collection.where('Field2').above(value);
return collection.count();

I'm using DexieJS to fetch data from an IndexedDB. I've done the following tests both with v. 1.1.0 and 1.2.0.

It is working great for simple queries, but unfortunately I am unable to chain multiple where clauses.

First, I've tried this

var collection = db[table];
collection = collection.where('Field').equals("1");
return collection.count();

And that was working. Then, I need to add a where clause, but only if a given value is set:

var collection = db[table];
collection = collection.where('Field').equals("1");
if(value) collection = collection.where('Field2').above(value);
return collection.count();

This one fails. For test purposes, I've also tried:

var collection = db[table];
collection = collection.where('Field').equals("1")
.and('Field2').above(value);
return collection.count();

var collection = db[table];
collection = collection.where('Field').equals("1")
.and().where('Field2').above(value);
return collection.count();

var collection = db[table];
collection = collection.where('Field').equals("1")
.where('Field2').above(value);
return collection.count();

None of these work. I'm starting to think that this is not possible at all, but since the method and() exists, there must be a way!

PS this works:

var collection = db[table];
collection = collection.where('Field2').above(value);
return collection.count();
Share Improve this question edited Feb 28, 2016 at 7:10 don asked Feb 28, 2016 at 6:37 dondon 4,54214 gold badges48 silver badges73 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

DexieJS' AND operator is implemented either as a filter function or a pound index. The simple way to implement your query is to use the filter method, something like;

var collection = db[table];
collection = collection
    .where('Field').equals("1")
      .and(function(item) { return item.Field2 > value });
return collection.count();

This means that the first filter will run against IndexedDB, and the additional condition will be run against each found item by DexieJS, which may or may not be good enough for what you need.

As to how to use the pound index, it's a bit harder to apply to your exact situation without more details on the collections and exact queries you want, but there is much more information available here.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742378700a4432706.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信