- Redis based
abstract-level
database for Node.js.
Requirements:
- redis-2.8 or more recent.
- node-12.x
Uses a sorted-set to order the keys and a hash to store the values.
Fetches the ordered key value pairs during iterations with a single redis lua call.
const { RedisLevel } = require('redis-level')
// Create a database
const db = new RedisLevel('tokens', { valueEncoding: 'buffer' })
// 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
}
With callbacks:
db.put('example', { hello: 'world' }, (err) => {
if (err) throw err
db.get('example', (err, value) => {
if (err) throw err
console.log(value) // { hello: 'world' }
})
})
The API of redis-level
follows that of abstract-level
with a few additional options and methods specific to Redis. The documentation below covers it all except for Encodings, Events and Errors which are exclusively documented in abstract-level
.
An abstract-level
and thus redis-level
database is at its core a key-value database. A key-value pair is referred to as an entry here and typically returned as an array, comparable to Object.entries()
.
Returns a new RedisLevel instance. location
is a String pointing at the root namespace of the data in redis.
location+':h'
is the hash where the values are stored.location+':z'
is the set where the keys are sorted.
Aside from the basic abstract-level
options, the optional options
object may contain:
connection
represents the connection parameters, and can be one of the following :- an already configured redis client. RedisLevel will not open or close it.
- a string representing a redis URL
- options as specified by ioredis
highWaterMark
number of values to fetch in one redis call for iteration. Defaults to 256.clearOnOpen
clears the database on open. Defaults tofalse
.
The createIfMissing
option of abstract-level
is ignored, as this is the way that redis
works.
With npm do:
npm install redis-level
Based on redisDown
, copyright HMalphettes.
Level/redis-level
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.