Skip to content

Instantly share code, notes, and snippets.

@gianmarcotoso
Created June 13, 2016 15:49
Show Gist options
  • Save gianmarcotoso/b5c5a927173dafd0aa33cd1a92d880bf to your computer and use it in GitHub Desktop.
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
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