qd.db
TypeScript icon, indicating that this package has built-in type declarations

5.1.6 • Public • Published

QuantumDB Logo

QuantumDB

npm version License: MIT Node.js Version

Key-Value Database with Dual Storage

Core Features

  1. JSON & SQLite Backends

    // Uses JSON if file ends with .json
    const jsonDB = new QuantumDB('data.json');
    
    // Uses SQLite if file ends with .db/.sqlite
    const sqlDB = new QuantumDB('data.db');
  2. Automatic File Creation

    • Creates quantum.sqlite by default if no file specified
    • Initializes empty JSON/SQLite files when they don't exist
  3. Basic CRUD Operations

    await db.set('key', 'value');
    const value = await db.get('key');
    await db.delete('key');
  4. Enhanced Security

    • Strict key validation (256 char max)
    • File permission controls (0600)
    • Path traversal protection

📦 Installation

npm install qd.db better-sqlite3

🛠️ API Reference

Data Operations

Method Description Example
set(key, value) Stores any serializable value await db.set('config', { darkMode: true })
get(key) Retrieves stored value const config = await db.get('config')
delete(key) Removes key-value pair await db.delete('temp')

Array Operations

Method Description
push(key, value) Appends to array (auto-creates if needed)
pull(key, value) Removes all occurrences of value from array

Numeric Operations

Method Description
add(key, amount) Increments number (default +1)
subtract(key, amount) Decrements number (default -1)

Bulk Operations

Method Description
bulkDelete(keys) Deletes multiple keys at once
getAll() Returns all key-value pairs
clearAll() Clears entire database

Key Search

Method Description
has(key) Checks if key exists
findKeys(pattern) Regex search for keys
startsWith(prefix) Finds keys with prefix

📝 Usage Examples

Initialization

const { QuantumDB } = require('qd.db');

// Default SQLite database
const db = new QuantumDB(); 

// Or specify file
const jsonDB = new QuantumDB('config.json');

Working with Numbers

await db.add('visits', 1); // Increment counter
await db.subtract('balance', 50); // Decrease balance

Array Management

await db.push('todos', 'Buy milk');
await db.pull('todos', 'Buy milk');

⚠️ Error Handling

The library throws descriptive errors:

try {
  await db.set('', 'value'); // Empty key
} catch (err) {
  console.error(err.message); // "Key must be a non-empty string"
}

🔒 Security Notes

  1. All database files created with restrictive permissions (0600)
  2. Only allows .json, .db, and .sqlite extensions
  3. Keys are validated against invalid characters

Community & Support

Join our developer community for support and updates: Discord Server

License

MIT © Quantum Developers

Dependencies (1)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i qd.db

    Weekly Downloads

    13

    Version

    5.1.6

    License

    MIT

    Unpacked Size

    19.7 kB

    Total Files

    10

    Last publish

    Collaborators

    • na3san