0.3.6 • Published 5 years ago

levelkv v0.3.6

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

LevelKV

A simple native Node.js implementation of the well-known key-value based LevelDB library.

Installation

npm i levelkv

Examples

// 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();

Notice

All the operations in LevelKV are asynchronous, remember to add await keyword in front of the function call if you need.

Operations

Open A Database

/**
 * 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 A Database

/**
 * Close the database.
 *
 * @async
 */
.close()

Reads And Writes

/**
 * 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=[])

For Maintainer

Install Project

  • Clone Project:

    git clone \<project-url>

  • Install Dependency Packages:

    npm install

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago