Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

level-js

Level2.8mMITdeprecated6.1.0TypeScript support: definitely-typed

Superseded by browser-level (https://github.com/Level/community#faq)

An abstract-leveldown compliant store on top of IndexedDB

level, leveldb, indexeddb, abstract-leveldown

readme

level-js

An abstract-leveldown compliant store on top of IndexedDB.

level badge npm Test Coverage Standard Common Changelog Donate

Table of Contents

<summary>Click to expand</summary>

Background

Here are the goals of level-js:

  • Store large amounts of data in modern browsers
  • Pass the full abstract-leveldown test suite
  • Support string and Buffer keys and values
  • Be as fast as possible
  • Sync with multilevel over ASCII or binary transports.

Being abstract-leveldown compliant means you can use many of the Level modules on top of this library.

Example

If you are upgrading: please see UPGRADING.md.

const levelup = require('levelup')
const leveljs = require('level-js')
const db = levelup(leveljs('bigdata'))

db.put('hello', Buffer.from('world'), function (err) {
  if (err) throw err

  db.get('hello', function (err, value) {
    if (err) throw err

    console.log(value.toString()) // 'world'
  })
})

With async/await:

const levelup = require('levelup')
const leveljs = require('level-js')
const db = levelup(leveljs('bigdata'))

await db.put('hello', Buffer.from('world'))
const value = await db.get('hello')

Browser Support

Sauce Test Status

Type Support

Keys and values can be a string or Buffer. Any other type will be irreversibly stringified. The only exceptions are null and undefined. Keys and values of that type are rejected.

In order to sort string and Buffer keys the same way, for compatibility with leveldown and the larger ecosystem, level-js internally converts keys and values to binary before passing them to IndexedDB.

If you desire non-destructive encoding (e.g. to store and retrieve numbers as-is), wrap level-js with encoding-down. Alternatively install level which conveniently bundles levelup, level-js and encoding-down. Such an approach is also recommended if you want to achieve universal (isomorphic) behavior. For example, you could have leveldown in a backend and level-js in the frontend. The level package does exactly that.

When getting or iterating keys and values, regardless of the type with which they were stored, keys and values will return as a Buffer unless the asBuffer, keyAsBuffer or valueAsBuffer options are set, in which case strings are returned. Setting these options is not needed when level-js is wrapped with encoding-down, which determines the optimal return type by the chosen encoding.

db.get('key', { asBuffer: false })
db.iterator({ keyAsBuffer: false, valueAsBuffer: false })

Install

With npm do:

npm install level-js

Not to be confused with leveljs.

This library is best used with browserify.

API

db = leveljs(location[, options])

Returns a new leveljs instance. location is the string name of the IDBDatabase to be opened, as well as the object store within that database. The database name will be prefixed with options.prefix.

options

The optional options argument may contain:

  • prefix (string, default: 'level-js-'): Prefix for IDBDatabase name.
  • version (string | number, default: 1): The version to open the database with.

See IDBFactory#open for more details.

Big Thanks

Cross-browser Testing Platform and Open Source ♥ Provided by Sauce Labs.

Sauce Labs logo

Contributing

Level/level-js is an OPEN Open Source Project. This means that:

Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.

See the Contribution Guide for more details.

Support us with a monthly donation on Open Collective and help us continue our work.

License

MIT

changelog

Changelog

6.1.0 - 2021-09-28

Added

  • Add db.getMany(keys) (#214) (f5a3ca3) (Vincent Weevers).

6.0.0 - 2021-04-09

If you are upgrading: please see UPGRADING.md.

Changed

  • Breaking: bump abstract-leveldown (720aced) (Vincent Weevers)
  • Breaking: bump buffer from 5.x to 6.x (#210) (cc68b21) (Alex Potsides)
  • Modernize syntax and bump standard (Level/community#98) (0ce815f) (Vincent Weevers)
  • Every browser in our test matrix now supports binary keys (2c20127) (Vincent Weevers)

5.0.2 - 2020-04-03

Changed

  • Use nextTick of abstract-leveldown (#195) (@vweevers) (same underlying code)
  • Upgrade nyc devDependency from ^14.0.0 to ^15.0.0 (#187) (@vweevers)
  • Upgrade airtap devDependency from ^2.0.0 to ^3.0.0 (#189) (@vweevers)

Fixed

5.0.1 - 2019-11-29

Fixed

  • Restore support of empty prefix option (#184) (@achingbrain). This restores a previous behavior (of level-js < 3) that unknown to us, was provided by the since-removed IDBWrapper.

5.0.0 - 2019-10-04

If you are upgrading: please see UPGRADING.md.

Changed

Added

4.0.1 - 2019-03-31

Changed

Removed

  • Remove outdated sentence about nullish values from README (#166) (@vweevers)

4.0.0 - 2018-12-30

If you are upgrading: please see UPGRADING.md.

Changed

Added

Removed

  • Remove now irrelevant serialization of nullish values (#155) (@vweevers)
  • Remove unused IndexedDBShim from tests (#162) (@vweevers)

3.0.0 - 2018-06-17

If you are upgrading: please see UPGRADING.md.

Changed

Added

Fixed

Removed

3.0.0-rc1 - 2018-05-26

Changed

  • Upgrade abstract-leveldown from 0.12.0 to 5.0.0 (@vweevers)
  • Upgrade typedarray-to-buffer from 1.0.0 to 3.1.5 (@vweevers)
  • Upgrade levelup devDependency from 0.18.2 to 3.0.0 (@vweevers)
  • Upgrade browserify devDependency from 4.1.2 to 16.2.2 (@vweevers)
  • Switch license from BSD to MIT (@ralphtheninja)
  • Replace IDBWrapper with straight IndexedDB code (@vweevers)
  • Change default database prefix from IDBWrapper- to level-js- (@vweevers)
  • Implement abstract #_serializeKey with support of all IndexedDB Second Edition types including binary keys (as Buffers) (@vweevers)
  • Implement abstract #_serializeValue with support of all types of the structured clone algorithm except for null and undefined (@vweevers)
  • Use immediate module for consistent microtask behavior (@vweevers)
  • Replace Buffer() with Buffer.from() (@vweevers)
  • Rename Iterator#iterator to #transaction (@vweevers)
  • Replace beefy with airtap --local for local testing (@vweevers)
  • Homogenize README title, description and headers (@vweevers)
  • Make real tape tests out of test-levelup.js (@vweevers)
  • Restructure custom tests to follow abstract test suite format (@vweevers)

Added

  • Add continuous browser tests with airtap and Sauce Labs (@vweevers)
  • Add prefix and version options to constructor (@vweevers)
  • Detect binary key support and fallback to String(buffer) (@vweevers)
  • Detect array key support and fallback to String(array) (@vweevers)
  • Test all value types of the structured clone algorithm (@vweevers)
  • Catch DataCloneError if the environment does not support serializing the type of a key or value (@vweevers)
  • Include Promise polyfill for levelup integration tests (@vweevers)
  • Test that Iterator stringifies Buffer.from() argument (@vweevers)
  • Add README badges, new goals and a code example with levelup (@vweevers)
  • Add npm files to .gitignore (@vweevers)

Fixed

  • Start Iterator cursor immediately and fill an in-memory cache to fulfill abstract-leveldown snapshot guarantees (@vweevers)
  • Stop advancing Iterator cursor when options.limit is reached (@vweevers)
  • Rename public #iterator to private #_iterator (@vweevers)
  • Fix #_iterator({ limit: 0 }) to yield 0 entries (@vweevers)
  • Handle transaction errors in Iterator (@vweevers)
  • Fix constructor to call super (@vweevers)
  • Make one request at a time in a batch transaction, saving CPU time (@vweevers)
  • Properly close and destroy db's in custom tests (@vweevers)
  • Update README links (@vweevers)

Removed

  • Remove support of ArrayBuffer values in favor of Buffer (@vweevers)
  • Remove now unneeded raw option from #_get() and #_iterator() (@vweevers)
  • Run tests without IndexedDBShim (@vweevers)
  • Remove Buffer to Uint8Array conversion in #_put() and #_batch() (@vweevers)
  • Remove obsolete #_approximateSize (@vweevers)
  • Remove obsolete #_isBuffer (@vweevers)
  • Remove obsolete testBuffer from abstract tests (@vweevers)
  • Remove obsolete writestream test from test-levelup.js (@vweevers)
  • Rely on abstract-leveldown defaults in Iterator constructor (@vweevers)
  • Rely on abstract-leveldown callback defaults (@vweevers)
  • Remove testling from package.json (@vweevers)
  • Remove level.js logo (@vweevers)

2.2.4 - 2016-05-09

Changed

  • Use toArrayBuffer() only when present (@substack)

2.2.3 - 2015-12-10

Changed

2.2.2 - 2015-09-12

This release introduced `this.keyRangeError`._

Added

Fixed

2.2.1 - 2015-07-05

Changed

2.2.0 - 2015-07-03

Added

  • Add Collaborators section to README (@maxogden)

Changed

Removed

2.1.6 - 2014-06-15

Fixed

2.1.5 - 2014-05-29

Changed

2.1.4 - 2014-05-13

Changed

2.1.3 - 2014-04-09

Added

  • Use typedarray-to-buffer to avoid copying to Buffer (@mafintosh)

2.1.2 - 2014-04-05

Added

Changed

  • Update browser configuration for Testling (@maxogden)

2.1.1 - 2014-03-12

This was not published to npm. There's also a gap between 2.1.1 and 2.0.0 that is inconsistent. The options.raw property was introduced in this release.

Changed

  • Update browser configuration for Testling (@maxogden)
  • Update abstract-leveldown to ~0.12.0 (@maxogden)
  • Update levelup to ~0.18.2 (@maxogden)
  • Make sure to store Uint8Array (@maxogden)
  • Test storing native JS types with raw = true (@maxogden)

2.0.0 - 2014-03-09

For some reason both tape and browserify were moved from devDependencies to dependencies. This release only had one commit.

Changed

  • Update browserify to ~3.32.0 (@maxogden)
  • Update tape to ~2.10.2 (@maxogden)
  • Change default encoding of values to strings to more closely match leveldown (@maxogden)

Fixed

1.2.0 - 2014-03-09

Added

Changed

  • Update browser configuration for Testling (@maxogden)
  • Pass through open options to idbwrapper (@maxogden)

Fixed

  • Don't use indexedDB.webkitGetDatabasesNames() in tests (@maxogden)

1.1.2 - 2014-02-02

Removed

1.1.1 - 2014-02-02

Changed

  • Modify a copy of the batch array, not the original (@nrw)

Fixed

1.1.0 - 2014-01-30

In this time period bops shows up and gets removed. Also, `.isBuffer()usesBuffer.isBuffer()in favor ofis-buffer` module._

Added

Changed

Fixed

  • Fix incorrect version of abstract-leveldown (@maxogden)
  • Pass error to callback in approximateSize() (@mcollina)

Removed

1.0.8 - 2013-08-12

Changed

Removed

1.0.7 - 2013-07-02

Changed

Fixed

  • Fix git url to abstract-leveldown (@maxogden)

1.0.6 - 2013-05-31

Changed

  • Update idb-wrapper to 1.2.0 (@maxogden)
  • Switch abstract-leveldown#master (@maxogden)
  • Disable batch and chainable batch tests (@maxogden)

1.0.5 - 2013-05-30

Changed

1.0.4 - 2013-05-30

Added

  • Test batch and chainable batch (@rvagg)

Changed

  • Update abstract-leveldown to ~0.7.1 (@rvagg)
  • Update levelup to ~0.9.0 (@brycebaril)

1.0.3 - 2013-05-14

Changed

1.0.2 - 2013-05-04

Fixed

  • Don't convert ArrayBuffer and typed arrays to strings (@maxogden)

1.0.1 - 2013-05-03

Added

  • Add optional options argument to .open() (@rvagg)
  • Add test-levelup.js (@maxogden)

Changed

1.0.0 - 2013-05-03

:seedling: Initial release.