dexie / Dexie.js

A Minimalistic Wrapper for IndexedDB
https://dexie.org
Apache License 2.0
11.68k stars 641 forks source link

startsWith on compound index? #544

Closed plaxdan closed 7 years ago

plaxdan commented 7 years ago

Is there no way to perform a [WhereClause.startsWith()](http://dexie.org/docs/WhereClause/WhereClause.startsWith().html) on a compound index? The following snippets are from this fiddle.

db.version(1).stores({
    friends: '++id,first,last,[first+last],age'
});

//...

db.friends.add({first: "Mickey", last: "Mouse", age: 42});

//...

// Expected the following to return Mickey Mouse..but it returns nothing.
return db.friends
    .where('[first+last]')
    .startsWith('M')
    .toArray();

The following variants were also tried, but to no avail:

// Pass an array of items - this behavior is implied by 
// http://dexie.org/docs/Compound-Index.html
return db.friends
    .where('[first+last]')
    .startsWith(['M', 'M'])
    .toArray();

// Use startsWithAnyOf
return db.friends
    .where('[first+last]')
    .startsWithAnyOf(['M', 'M'])
    .toArray();
dfahlander commented 7 years ago

No, but startsWith() is actually just sugar for between(prefixString, prefixString + '\uffff'). So lets say you want to search for documents where type="email" and name starts with "a", you could express it like

db.documents.where('[type+name]').between(
    ["email", "a"],
    ["email", "a\uffff"])

...assuming you've indexed "[type+name]".

edotassi commented 5 years ago

@dfahlander this should be added in the docs 😄