UeberDB2: Abstract Your Databases

5.0.48 · active · verified Wed Apr 22

ueberdb2 is a Node.js library that provides a high-level abstraction layer over various database systems, allowing them to be used as simple key-value stores. It currently stands at version 5.0.48 and generally follows a release cadence driven by bug fixes, dependency updates, and new database support. Key differentiators include its ability to use a cache and buffer for performance optimization, reducing database transaction overhead through bulk writing, and offering a unified API across a wide array of database backends like MySQL, PostgreSQL, MongoDB, Redis, SQLite, Elasticsearch, and SurrealDB. It supports complex JSON objects and provides methods for getting and setting subkeys, making it versatile for diverse data storage needs.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates initializing a MySQL database, setting and retrieving key-value pairs, accessing subkeys, setting nested subkeys, and using `findKeys` to query patterns. It highlights `async/await` usage and environment variable best practices for credentials.

import { Database } from 'ueberdb2';
import type { DatabaseOptions } from 'ueberdb2';

(async () => {
  // Example: Using MySQL. Ensure 'mysql2' package is installed: npm install mysql2
  const mysqlOptions: DatabaseOptions = {
    user: process.env.DB_USER ?? 'root',
    host: process.env.DB_HOST ?? 'localhost',
    password: process.env.DB_PASSWORD ?? '',
    database: process.env.DB_DATABASE ?? 'store',
    engine: 'InnoDB',
  };

  const db = new Database('mysql', mysqlOptions);

  try {
    await db.init();
    console.log('Database initialized successfully.');

    await db.set('myKey', { data: 'someValue', timestamp: Date.now() });
    console.log('Set myKey:', await db.get('myKey'));

    const subValue = await db.getSub('myKey', ['data']);
    console.log('Get subkey data:', subValue);

    await db.setSub('myKey', ['nested', 'prop'], 'nestedValue');
    console.log('Set nested subkey. Full object:', await db.get('myKey'));

    console.log('Finding keys matching "myK*":', await db.findKeys('myK*', null));

  } catch (error) {
    console.error('Database operation failed:', error);
  } finally {
    await db.close();
    console.log('Database connection closed.');
  }
})();

view raw JSON →