Pro.db

3.0.8 · active · verified Wed Apr 22

pro.db is a lightweight, easy-to-use database package designed for simple data storage, primarily in Node.js environments. It operates by storing data as JSON objects within local files, functioning as a file-based key-value store. The current stable version is 3.0.8. Its primary differentiator lies in its simplicity and direct API for common database operations like setting, getting, deleting, and manipulating numeric values or arrays within the stored JSON structure. The package appears to follow an as-needed release cadence, focusing on stability for its core features, and is particularly suited for small-scale applications or Discord bot development where a full-fledged relational database might be overkill. It does not require external database server setup, relying solely on local file system operations.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates common CRUD operations (set, get, add, push, delete) with pro.db, including checking key existence, fetching all data, and creating a backup file, highlighting its synchronous nature and file-based storage.

const db = require('pro.db');
const path = require('path');
const fs = require('fs');

const dbFilePath = path.join(__dirname, 'database.json');
// Ensure the database file exists or is created before operations
if (!fs.existsSync(dbFilePath)) {
  fs.writeFileSync(dbFilePath, '{}', 'utf8');
}

// Set a value
db.set('user:123:name', 'Alice');
db.set('user:123:age', 30);
db.set('products', [{ id: 1, name: 'Laptop' }, { id: 2, name: 'Mouse' }]);

console.log('User name:', db.get('user:123:name'));
console.log('Products:', db.get('products'));

// Add to a numeric key
db.add('user:123:age', 5);
console.log('New age:', db.get('user:123:age'));

// Push to an array key
db.push('products', { id: 3, name: 'Keyboard' });
console.log('Updated products:', db.get('products'));

// Check if a key exists
console.log('Has user:123:name?', db.has('user:123:name'));

// Delete a key
db.delete('user:123:name');
console.log('User name after delete:', db.get('user:123:name'));

// Fetch all data
console.log('All data:', db.fetchAll());

// Example of backup (create a dummy backup file)
db.backup('my_backup_file');
console.log('Backup initiated (check your file system for my_backup_file.json)');

// Caution: db.reset() will delete all data.
// Uncomment to clear the database:
// db.reset();
// console.log('Database reset. All data removed.');

view raw JSON →