A simple native Node.js implementation of the well-known key-value based LevelDB library.
npm i levelkv
// Open a database
const { LevelKV, DBMutableCursor } = require('levelkv');
const db = await LevelKV.initFromPath('your_directory_path');
let key = 'key';
let value = { a:1, b:2, c:3 };
// Add data
await db.put( `${key}`, value );
await db.put( `${key}2`, value );
// Get data
let result = await db.get( key );
console.log( result.length );
console.log( await result.toArray() );
// Delete data
await db.del( key );
// Get all data
result = await db.get();
for await( let value of result )
{
console.log( value );
}
// Use Mutable Cursor
const dbCursor = await db.get([], {mutable_cursor: true});
const segments = dbCursor.segments;
for( let segment of segments )
{
segment._in = false;
segment._v = 'newValue';
}
console.log( await dbCursor.toArray() );
// Close the database
await db.close();
All the operations in LevelKV are asynchronous, remember to add await
keyword in front of the function call if you need.
/**
* Initialize the database.
*
* @async
* @param {string} dir - Directory path of the database. Make sure you have created or it will fail if the directory does not exist.
* @param {object} options - Database creating options. Defaults to {auto_create:true}, which means create a new database automatically if not exist.
* @returns {Promise<LevelKV>} - Promise object represents the database itself.
*/
.initFromPath(dir, options={auto_create:true})
/**
* Close the database.
*
* @async
*/
.close()
/**
* Get data from the database.
*
* @async
* @param {string|string[]} keys - A specific key or an array of keys to retrieve, if not given it will retrieve all data from the database.
* @returns {DBCursor|DBMutableCursor} - Database cursor of the retrieved data.
*/
.get(keys=[])
/**
* Add data to the database.
*
* @async
* @param {string|string[]} keys - A specific key or an array of keys to add.
* @param {*} val - The value to add.
*/
.put(keys=[], val)
/**
* Delete data from the database.
*
* @async
* @param {string|string[]} keys - A specific key or an array of keys to delete.
*/
.del(keys=[])
- Clone Project:
git clone <project-url>
- Install Dependency Packages:
npm install