Take values from IDBStore or IDBIndex
idb-take is a set of functions on top of .openCursor() functionality.
Think about it as improved IndexedDB 2.0's getAll(range, limit) method, which in addition to limit argument accepts unique, offset and reverse.
npm install --save idb-take
import { take, takeOne, takeRight, takeRightOne } from 'idb-take'
import { open } from 'idb-factory'
// let's assume you have IndexedDB database with 'books' store and `byFrequency` index
const db = await open('mydb', 1, (e) => {
e.target.result.createObjectStore('books', { keyPath: 'id' })
e.target.result.objectStore('books').createIndex('byFrequency')
})
// query store
const store = db.transaction(['books'], 'readonly').objectStore('books')
await take(store, { gte: 'a' }) // find all keys matching `a` pattern
await take(store, null, { limit: 10, offset: 20 }) // page=3
await takeOne(store, { lt: 'z' }) // get first value
// query index
const index = db.transaction(['books'], 'readonly').objectStore('books').index('byFrequency')
await take(index, { lte: 3 }, { unique: true, limit: 2 })
await takeRight(index, null, { offset: 5 }) // in reverse order
await takeRightOne(index, { gte: 10 }) // get last valueEach function accepts 3 arguments:
store- an instance ofIDBStoreorIDBIndex. It also integrates with high level libraries which support.openCursor()method (for example: treo).range(optional) -IDBKeyRangeinstance or any idb-range argumentopts(optional) - filter options:limit- limit amount or returned valuesoffset- skip valuesreverse- get values in reverse order usingprevorprevuniquewhen open cursor.unique- get only unique values usingprevuniqueornextunique(applicable only forIDBIndex).
Take values from store, which satisfy range criteria.
import { take } from 'idb-take'
await take(store, null, { limit: 20, offset: 20 })
await take(index, { gt: 10, lt: 20 })Take first value.
Take values in reverse order.
Take last value.