Created
June 13, 2016 15:49
-
-
Save gianmarcotoso/b5c5a927173dafd0aa33cd1a92d880bf to your computer and use it in GitHub Desktop.
A search function to use with Dexie.js to search an IndexedDB table using an object of where clauses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let search = function(db, tableName, query) { | |
let table = db.table(tableName) | |
let clause = Object.keys(query).reduce( (r, n) => { | |
if (typeof query[n] === "undefined" || query[n] === null) return r; | |
let where = !r ? table.where(n) : r.or(n) | |
if (query[n] && query[n].constructor === String) { | |
return where.startsWith(query[n]) | |
} | |
return where.equals(query[n]) | |
}, null) | |
return clause | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment