Pro.db
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
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use `require()` in an ECMAScript Module (ESM) context (e.g., in a file where `type: module` is set in `package.json` or imported via `import`). pro.db is primarily a CommonJS package.fixChange your project or file to use CommonJS (`const db = require('pro.db');`) or transpile your ESM code to CJS before running. Alternatively, if your Node.js version supports it, you might be able to use dynamic import `import('pro.db').then(db => { /* use db */ });`. -
TypeError: db.set is not a function
cause This usually indicates that `db` did not correctly resolve to the `pro.db` module's exported object. This can happen if you tried `import db from 'pro.db'` in a CJS-only context without proper TypeScript/Babel setup, or if the module itself was malformed.fixEnsure you are using `const db = require('pro.db');` for Node.js CommonJS environments. If using TypeScript, check your `tsconfig.json` for `esModuleInterop` and `allowSyntheticDefaultImports` or provide a custom module declaration. -
EACCES: permission denied, open 'database.json'
cause The Node.js process does not have sufficient read/write permissions for the directory where `pro.db` attempts to create or access its database file (typically `database.json` in the current working directory).fixEnsure the user running the Node.js application has read and write permissions to the directory where the database file is stored. You may need to change directory permissions (e.g., `chmod 777 your-data-directory`) or run the application with elevated privileges (use with caution).
Warnings
- gotcha pro.db performs file I/O operations synchronously. In long-running or high-traffic Node.js applications, extensive synchronous disk access can block the event loop, leading to performance degradation or unresponsiveness.
- breaking The `db.reset()` function permanently deletes all data from the database. There is no confirmation prompt or undo mechanism built into the function itself.
- gotcha The package is primarily built for CommonJS (CJS) environments. Directly using ESM `import` statements without proper transpilation or Node.js module resolution configuration (`"type": "module"` with `"exports"` field) can lead to runtime errors.
- gotcha Error handling for file system operations (e.g., permission issues, disk full) is not explicitly demonstrated in the quickstart and may require manual `try...catch` blocks around `pro.db` calls if you need to gracefully handle such failures.
Install
-
npm install pro.db -
yarn add pro.db -
pnpm add pro.db
Imports
- db
import db from 'pro.db';
const db = require('pro.db'); - db (individual functions)
const db = require('pro.db').db;const { set, get } = require('pro.db'); - Pro.db in TypeScript
import * as db from 'pro.db';
import db from 'pro.db';
Quickstart
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.');