level
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
level-packager | 1.2.1 | 15.58 kB | MIT | prod | 1 |
leveldown | 1.6.0 | 656.76 kB | MIT | prod | 2 1 |
Level is a universal abstract-level database for Node.js and browsers, serving as a convenient package to access classic-level in Node.js and browser-level in browsers. This package is ideal for creating lexicographically sorted key-value databases. It allows users to create new databases, add database entries, fetch these entries via keys, and batch operations in Node.js and the browser. It supports both promises and callbacks for its asynchronous methods. Additionally, Level supports creating and using sublevels within the database.
To use Level, you install it via npm using npm install level
. This package should ideally be used with browserify, webpack, rollup or similar bundlers for use in browsers. Here is a brief example of how to use Level:
const { Level } = require('level')
// Create a database
const db = new Level('example', { valueEncoding: 'json' })
// Add an entry with key 'a' and value 1
await db.put('a', 1)
// Add multiple entries
await db.batch([{ type: 'put', key: 'b', value: 2 }])
// Get value of key 'a': 1
const value = await db.get('a')
// Iterate entries with keys that are greater than 'a'
for await (const [key, value] of db.iterator({ gt: 'a' })) {
console.log(value) // 2
}
If the user wants callback-based interaction, here is an example:
db.put('a', { x: 123 }, function (err) {
if (err) throw err
db.get('a', function (err, value) {
console.log(value) // { x: 123 }
})
})
Users can also use TypeScript type parameters for Level:
// Specify types of keys and values (any, in the case of json).
// The generic type parameters default to Level<string, string>.
const db = new Level<string, any>('./db', { valueEncoding: 'json' })
await db.put('a', { x: 123 })
// Specify different types when overriding encoding per operation
await db.get<string, string>('a', { valueEncoding: 'utf8' })
// Though in some cases TypeScript can infer them
await db.get('a', { valueEncoding: db.valueEncoding('utf8') })
// It works the same for sublevels
const abc = db.sublevel('abc')
const xyz = db.sublevel<string, any>('xyz', { valueEncoding: 'json' })
The Level documentation is available on GitHub. A comprehensive explanation of the API and more complex usage scenarios, including examples, can be found in the API section of the level package README file on GitHub. There are also links to documentation for certain parts of the codebase only available in abstract-level
, classic-level
, or browser-level
. For a quick way to get started, check out browserify-starter
or webpack-starter
.