bun-lmdb-connector is a library for working with LMDB (Lightning Memory-Mapped Database) in the Bun environment. It provides a simple and efficient way to interact with LMDB through REST API and WebSocket.
You can install bun-lmdb-connector using npm:
bun add bun-lmdb-connector
bun-lmdb-connector provides the following main features:
- Initialization and configuration of databases
- CRUD operations through REST API
- CRUD operations through WebSocket
- Flexible configuration of the server and databases
import { setConfig, initializeDatabases, createServer, startServer } from 'bun-lmdb-connector';
// Configure the server (optional)
setConfig({
databases: [
{ name: 'mydb', path: './data/' }
],
server: {
port: 3000,
host: 'localhost'
},
logLevel: 'info'
});
// Initialize databases
initializeDatabases();
// Create and start the server
const app = createServer();
startServer(app);
The Connector class provides methods for working with the database:
import { Connector } from 'bun-lmdb-connector';
// Get value
const value = await Connector.get('mydb', 'key');
// Write value
await Connector.put('mydb', 'key', 'value');
// Delete value
await Connector.remove('mydb', 'key');
// Get all values
const allValues = await Connector.getAll('mydb');
bun-lmdb-connector automatically creates REST API endpoints:
- GET /api/:dbName/:key - get value by key
- POST /api/:dbName - write new value
- DELETE /api/:dbName/:key - delete value by key
- GET /api/:dbName - get all values from the database
To work through WebSocket, connect to the /ws endpoint and send messages in JSON format:
const ws = new WebSocket('ws://localhost:3000/ws');
ws.onopen = () => {
ws.send(JSON.stringify({
action: 'get',
dbName: 'mydb',
key: 'someKey'
}));
};
ws.onmessage = (event) => {
console.log('Received:', JSON.parse(event.data));
};
Make sure you have Bun and all necessary dependencies installed before using this library.