cord-db v1.0.2
Cord-DB
A simple Node.js class for managing JSON data storage. It allows saving, loading, updating, and deleting JSON data.
Installation
To use this package, make sure you have Node.js installed, then run:
npm install cord-db
Usage
1. Importing the Package
const DataManager = require('cord-db');
2. Creating an Instance
You can specify the file path where your JSON data will be stored (default is ./data.json
).
const db = new DataManager('./path-to-your-data-file.json');
3. Saving Data
To save data (an object) to the JSON file:
const data = { name: "John", age: 30 };
db.save(data);
4. Loading Data
To load the data from the JSON file:
const loadedData = db.load();
console.log(loadedData);
If the file doesn't exist or is empty, it will return null
.
5. Updating a User's Key
You can update a specific user's data by specifying the userId
, key
, and value
.
db.updateUserKey(userId, 'coins', amount);
This will update the coins
field for the user with ID 1234567890
with new amount.
6. Retrieving a User's Key
To get the value of a specific key for a user:
const userCoins = db.getUserKey('user123', 'coins');
console.log(userCoins);
const coins = db.getUserKey(userId, 'coins') || 0;
console.log(coins);
If the key doesn't exist for the user, it will return null
.
7. Deleting the Data File
To delete the entire data file:
db.delete();
This will remove the file where your data is stored.
Methods Summary
save(data)
: Saves the provideddata
(object) to the JSON file.load()
: Loads and returns the data from the JSON file.updateUserKey(userId, key, value)
: Updates a specific key-value pair for a user.getUserKey(userId, key)
: Retrieves a specific key's value for a user.delete()
: Deletes the JSON file.
Error Handling
If any error occurs during file operations (e.g., file doesn't exist, permission issues), an error message will be logged to the console.
Visit Developer Support Server for any question or error! Created by .rya8 Enjoy managing your JSON data with ease!