Dexie.js: count affects result of anyOf

Created on 11 Feb 2019  路  5Comments  路  Source: dfahlander/Dexie.js

Hi, I've come across an issue where calling count() on a result after an anyOf() seems to drop all but one of the anyOf inputs when doing a subsequent toArray()

Reproduced in this fiddle, the count returns 3 but then the toArray() only returns the two matches from the last entry in the anyOf argument. The reverse happens if you toArray() before count()

https://jsfiddle.net/93tL6osc/

question

Most helpful comment

Thanks. Collection in current version of Dexie is not immutable. This is a limitation I'm hoping to overcome without affecting backward compatibility. Currently, one cannot apply filters on a Collection and reuse it after it has been executed (using count(), toArray(), etc). There's a helper method Collection.clone() that can be used before doing further configuration on it.

So basically, it's just safe to do the expression twice:

const count = await db.friends.where('age').anyOf(42,46).count();
const items = await db.friends.where('age').anyOf(42,46).toArray();

Keeping issue open to fix in dexie@3.

/David

All 5 comments

Thanks. Collection in current version of Dexie is not immutable. This is a limitation I'm hoping to overcome without affecting backward compatibility. Currently, one cannot apply filters on a Collection and reuse it after it has been executed (using count(), toArray(), etc). There's a helper method Collection.clone() that can be used before doing further configuration on it.

So basically, it's just safe to do the expression twice:

const count = await db.friends.where('age').anyOf(42,46).count();
const items = await db.friends.where('age').anyOf(42,46).toArray();

Keeping issue open to fix in dexie@3.

/David

I've decided to keep current behavior in Dexie 3.0 in order to not break backward compability. Removing the "backlog" label.

I was expecting that we can workaround this problem with using Collection.clone() method but it did not work. Could you improve clone() method for these cases or create a new method which gives a reusable copy?

const col = db.friends.where('age').anyOf(42,46)
const count = await col.clone().count();
const items = await col.toArray();

@alievrenkut In what way did it not work?

It did not work in my workspace. I changed the fiddle above to use clone. It also did not work.
You can test it by commenting out the count() line, you will see result changes.
https://jsfiddle.net/5jpvswan

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abieri picture abieri  路  15Comments

ValeriyPogosyan picture ValeriyPogosyan  路  21Comments

carlos00027 picture carlos00027  路  14Comments

acicali picture acicali  路  43Comments

bennycode picture bennycode  路  28Comments