1.0.5 • Published 4 years ago

@reiwa/indexed v1.0.5

Weekly downloads
5
License
ISC
Repository
-
Last release
4 years ago

@reiwa/indexed

npm version

Installation

$ yarn add @reiwa/indexed

Usage

import { retrieve, startAt, sync } from '@reiwa/indexed'

const main = async () => {
  const openRequest = indexedDB.open('hello', 1)

  openRequest.addEventListener('upgradeneeded', () => {
    const database = request.result
    if (database.objectStoreNames.contains('words')) {
      database.deleteObjectStore('words')
    }
    const store = database.createObjectStore('words', { keyPath: 'text' })
    store.createIndex('text', 'text')
  })

  await sync(openRequest)

  const database = openRequest.result

  {
    const transaction = database.transaction('words', 'readwrite')

    const store = transaction.objectStore('words')

    store.clear()

    const words = [{ text: 'foo' }, { text: 'foo_a' }]

    for (const word of words) {
      store.add(word)
    }

    await sync(transaction)
  }

  {
    const transaction = database.transaction('words', 'readonly')

    const store = transaction.objectStore('words')

    const request = store.get('foo')

    const word = await sync(request)

    console.log(word)
    // {text: 'foo'}
  }

  {
    const transaction = database.transaction('words', 'readonly')

    const store = transaction.objectStore('words')

    const keyRange = startAt('foo')

    const request = store.index('text').openCursor(keyRange, 'next')

    const words = await retrieve(request)

    console.log(words)
    // [{text: 'foo'}, {text: 'foo_a'}]
  }

  database.close()
}

main().catch(err => {
  console.error(err)
})

Publish

$ npm version patch
$ npm publish
1.0.2

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago