npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-mdbx

v0.4.0

Published

Node bindings for mdbx.

Downloads

23

Readme

node-mdbx

This module contains libmdbx bindings.

MDBX is a fork of well-known LMDB embeddable database. It contains various fixes and improvements. See libmdbx for details.

Supported platforms:

  • Linux
  • Windows

Requirements

On Linux:

  • CMake
  • gcc

On Windows:

Installation

npm install node-mdbx

Usage

See API for details.

const MDBX = require('node-mdbx');
const db = new MDBX({
  // path to DB directory
  path: '/tmp/test_db',
  
  // maximum number of inner Dbis (default is 1)
  maxDbs: 5,

  // trade-off for speed and durability
  syncMode: 'safeNoSync',
  
  // Read-only mode (default is false)
  readOnly: false,
  
  // Page size (default is 4096)
  pageSize: 65536,
  
  // Key convert mode. Possible values:
  // 'string' (default) - all returned keys will be auto-converted from buffer to string
  // 'buffer' - returned keys will remain buffers
  keyMode: 'string',

  // Value convert mode. Possible values:
  // 'string' - all returned values will be auto-converted from buffer to string
  // 'buffer' (default) - returned values will remain buffers
  valueMode: 'buffer',
});

// All database operations should be enclosed inside transaction with transact method.
// It accepts *sync* function as only parameter. Nested calls are possible
// (but commit/rollback mechanics will only work at top-level .transact call).

const result = db.transact(txn => {
  const dbi = txn.getDbi('main');
  
  // working with dbi
  
  dbi.put('key', 'value');

  // Buffer.isBuffer(value) == true && String(value) == 'value'
  const value = dbi.get('key');
  
  // true
  dbi.has('key');
  
  dbi.del('key');
  
  // false
  dbi.has('key');
  
  // undefined
  dbi.get('key')
  
  ...
  
  // iterate over all values:
  for (let key = dbi.first(); key != null; key = dbi.next(key)) {
    console.log([
      key,
      String(dbi.get(key)),
    ]);
  }
  
  // returned value will be passed as a result of .transact call:
  return 'someresult';
});

// There is also an async version of transact: asyncTransact.
// It accepts *async* function as only parameter. 
// asyncTransact call queues execution of passed action until no transaction is active.

// Warning 1: Remember that during action execution other transactions cannot be processed,
//   so action should be as fast as possible.

// Warning 2: Deadlock will be created with await of nested asyncTransact call. To overcome it:
//   1. Don't use such a nested calls, or
//   2. Don't wait (asynchronously, directly or indirectly) on nested asyncTransact calls within
// initial asyncTransact call stack.

async function dbExample() {
  await db.asyncTransact(async txn => {
    const dbi = txn.getDbi('main');
    
    // do something with dbi
    // can use async calls
  });
}

API

class MDBX

new MDBX(options)

Creates or opens MDBX database. Accepts options object.

  • options.path - database directory path (required)
  • options.readOnly - if true, then opens in read-only mode (default: false)
  • options.maxDbs - maximum number of inner Dbis (default: 1)
  • options.pageSize - page size (default: 4096)
  • options.keyMode - key mode:
    • 'string' - all key-returning methods convert keys from buffer to string (default)
    • 'buffer' - no key conversion
  • options.valueMode - value mode:
    • 'string' - all value-returning methods convert value from buffer to string
    • 'buffer' - no value conversion (default)
  • options.syncMode - sync mode (ordered by decreasing safety):
    • 'durable' (default) - durable sync mode (MDBX_SYNC_DURABLE)
    • 'noMetaSync' - no meta sync on commit (MDBX_NOMETASYNC)
    • 'safeNoSync' - don't sync anything but keep previous steady commits (MDBX_NOMETASYNC + MDBX_SAFE_NOSYNC)
    • 'unsafe' (fastest) - don't sync anything and wipe previous steady commits (MDBX_NOMETASYNC + MDBX_UTTERLY_NOSYNC) See http://erthink.github.io/libmdbx/group__sync__modes.html for details.

.transact(action)

Executes syncronous action inside transaction. If transaction is already active, then uses it. action has single parameter txn - current transaction. txn should be used to get dbis and do data manipulations. Rollbacks on error (only for top-level .transact call). Returns the returned value of action call.

.asyncTransact(action)

Executes async action inside transaction. Queues execution if needed. Warning! Avoid nested .asyncTransact awaits as it could lead to a deadlock!

.close()

Closes the database.

.closed

Has true value if database has been closed.

.hasTransaction()

Returns true if there is a transaction active.

static clearDb(path)

Deletes whole database by it's directory path. Database should not be opened in any process!

class TXN

.getDbi(name)

Opens and returns DBI of a given name (null or empty string - open main/default dbi). Don't reuse DBI or TXN objects between different transactions. Storing them for later use is undefined behaviour. Remember that the maximum number of dbis maxDbs greater than 1 should be specified to MDBX constructor at database creation time to use dbis other than the default one (name == null). Otherwise, an error will occur: "MDBX_DBS_FULL: Too many DBI-handles (maxdbs reached)".

.clearDbi(name, remove)

Clears (remove == false) or removes (remove == true) dbi with name name. If remove == true than all corresponding dbi objects will be invalidated. To recreate dbi with the same name, one should use .getDbi(name) afterwards.

class DBI

.put(key, value)

Set value of a key. Key and value should be Buffer or string.

.get(key)

Get value of a key. Returns Buffer (or string, if 'string' valueMode is used) with it's value if such a key exists. Returns undefined otherwise.

.has(key)

Returns true if key exists. Returns false otherwise.

.del(key)

Deletes key.

.first()

Returns the smallest (lexicographically) key in Dbi. If there are no keys returns undefined.

.last()

Returns the biggest (lexicographically) key in Dbi. If there are no keys returns undefined.

.next(key)

Returns the smallest (lexicographically) key greater than the given input key. It there are no such a keys, returns undefined.

.prev(key)

Returns the largest (lexicographically) key less than the given input key. It there are no such a keys, returns undefined.

.lowerBound(key)

Returns the smallest (lexicographically) key greater or equal to the given input key. It there are no such a keys, returns undefined.