Hello,
i would like to kow if there is any way to search for a specified pattern in a column (sql like operator) ?
just found on MDN website
Limitation of IndexedDb :
Full text searching. The API does not have an equivalent of the LIKE operator in SQL.
so it will be a good idea to add this feature to dexie.js
Dexie is designed to embrace optimal use of indexes. The SQL "LIKE" operator will in many cases require a full search and not be able to utilize an index. That's why Dexie provides only efficient compare methods such as startsWith() etc.
However, you can achieve any pattern matching using the filter() method, but it will not utilize any index but instead scan every item in the object store (table). See sample below:
var db = new Dexie("likeTest");
db.version(1).stores({
friends: "name,age"
});
db.on('populate', function() {
db.friends.add({name: "Foo Barsson", age: 33});
});
db.open();
db.friends.filter (function (friend) { return /Bar/.test(friend.name); })
.toArray()
.then(function(result) {
alert ("Found " + result.length + " friends containing the word 'Bar' in its name...");
});
That said, if you require an optimal full text search mechanism, you could also have a look at the full-text search samples in this repo. You could also extend the indexing capability to support endsWith() but that would require you to store a reversed version of the string and index that. You can achieve computed properties on objects by listening to the "creating" and "updating" hooks and make sure to enforce a computed property side by side with your original property. At present, all these things are possible but is still a bit cumbersome. Maybe a future version of Dexie, or a future Dexie-addon will support computed properties and full text indexes out of the box, we'll see.
Most helpful comment
Dexie is designed to embrace optimal use of indexes. The SQL "LIKE" operator will in many cases require a full search and not be able to utilize an index. That's why Dexie provides only efficient compare methods such as startsWith() etc.
However, you can achieve any pattern matching using the filter() method, but it will not utilize any index but instead scan every item in the object store (table). See sample below:
That said, if you require an optimal full text search mechanism, you could also have a look at the full-text search samples in this repo. You could also extend the indexing capability to support endsWith() but that would require you to store a reversed version of the string and index that. You can achieve computed properties on objects by listening to the "creating" and "updating" hooks and make sure to enforce a computed property side by side with your original property. At present, all these things are possible but is still a bit cumbersome. Maybe a future version of Dexie, or a future Dexie-addon will support computed properties and full text indexes out of the box, we'll see.