{"library":"node-lmdb","title":"Node.js LMDB Binding","description":"node-lmdb provides a high-performance Node.js binding to LMDB (Lightning Memory-Mapped Database), a transactional key-value store renowned for its speed and efficiency. It operates as an in-process, zero-copy database, eliminating the overhead of socket communication. The library supports transactions, multiple databases within a single environment, and is designed for multi-threaded and multi-process use, offering crash-proof persistence. The current stable version is 0.10.1, with releases occurring periodically to address bugs and introduce features. Its key differentiators include direct memory-mapped access, support for binary and string values via Node.js Buffers, and an API designed to align with JavaScript conventions while maintaining parity with the underlying LMDB C API. It's suitable for applications requiring extremely fast, durable, local data storage.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install node-lmdb"],"cli":null},"imports":["import { Env } from 'node-lmdb';","import { Txn } from 'node-lmdb';","import * as lmdb from 'node-lmdb';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Env } from 'node-lmdb';\nimport * as fs from 'fs';\nimport * as path from 'path';\n\nconst dbPath = path.join(__dirname, 'mydata');\nif (!fs.existsSync(dbPath)) {\n    fs.mkdirSync(dbPath);\n}\n\nconst env = new Env();\nenv.open({\n    path: dbPath,\n    mapSize: 2 * 1024 * 1024 * 1024, // 2GB maximum database size\n    maxDbs: 5 // Allow up to 5 named databases\n});\n\nconst dbi = env.openDbi({\n    name: 'myPrettyDatabase',\n    create: true // Create the database if it doesn't exist\n});\n\ntry {\n    let txn = env.beginTxn();\n    const key1 = 1;\n    let value1 = txn.getString(dbi, key1);\n    \n    console.log(`Initial value for key ${key1}: ${value1}`);\n\n    if (value1 === null) {\n        txn.putString(dbi, key1, \"Hello from node-lmdb!\");\n        console.log(`Set value for key ${key1} to 'Hello from node-lmdb!'`);\n    } else {\n        txn.del(dbi, key1);\n        console.log(`Deleted value for key ${key1}`);\n    }\n    \n    const key2 = 'my_string_key';\n    txn.putBinary(dbi, key2, Buffer.from('Binary data example'));\n    console.log(`Put binary data for key '${key2}'`);\n\n    txn.commit(); // Commit the transaction\n\n    // Read the binary data back in a new transaction\n    txn = env.beginTxn();\n    const binaryValue = txn.getBinary(dbi, key2);\n    console.log(`Retrieved binary data for key '${key2}': ${binaryValue?.toString()}`);\n    txn.abort(); // Abort a read-only transaction\n\n} catch (e) {\n    console.error('LMDB operation failed:', e);\n} finally {\n    dbi.close();\n    env.close();\n    fs.rmSync(dbPath, { recursive: true, force: true }); // Clean up\n}","lang":"typescript","description":"This quickstart demonstrates how to initialize an LMDB environment, open a database, perform read and write operations within a transaction, and then properly close resources. It covers storing strings and binary data, and includes cleanup.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}