darko.db

1.1.0 • Public • Published

darko.db

npm version

darko.db is a simple, file-based JSON database package for Node.js. It provides an easy-to-use interface for storing and retrieving data in a JSON format.

Installation

You can install darko.db using npm:

npm install darko.db

Usage

const { Database } = require("darko.db");
const db = new Database(); // Creates or uses "database.json" as the file name or use [ const db = new Database("json db file name"); ] for specific db file

//Example Usage

// Set a key-value pair
db.set("name", "NotDarko");

// Retrieve the value associated with a key
const name = db.get("name");
console.log(name); // Output: "NotDarko"

// Alias for `get`
const aliasname = db.fetch("name");
console.log(aliasname); // Output: "NotDarko"

// Check if a key exists
const keyexists = db.has("name");
console.log(keyexists); // Output: true

// Retrieve all data in the database
const alldb = db.fetchAll();
console.log(alldb);

// Delete a key and its associated value
db.delete("name");

// Reset the entire database
db.reset();

// Set key-value pairs
db.set("key1", "value1");
db.set("key2", "value2");
db.set("key3", "value3");

// Retrieve all key-value pairs
const allentries = db.all();
console.log(allentries);

// Slice a portion of key-value pairs
const sliceddata = db.slice(1, 3);
console.log(sliceddata);

// Find index of an element based on a condition
const index = db.findIndex(value => value === "value2");
console.log(index);

// Filter elements based on a condition
const filtereddb = db.filter(value => value.startsWith("value"));
console.log(filtereddb);

// Sort key-value pairs based on a compare function
const sorteddb = db.sort((a, b) => a.localeCompare(b));
console.log(sorteddb);

// Push a value to an array associated with a key
db.push("numbers", 5);
db.push("numbers", 10);
const numbers = db.get("numbers");
console.log(numbers);

// Perform arithmetic operations on a numeric key's value
db.set("count", 10);
db.math("count", "+", 5);
const count = db.get("count");
console.log(count);

// Remove a specific item from an array associated with a key
db.push("fruits", "apple");
db.push("fruits", "banana");
db.push("fruits", "cherry");
db.pull("fruits", "banana");
const fruits = db.get("fruits");
console.log(fruits);

// Get database statistics
const stats = db.getStats();
console.log(stats);

// Rename a key
db.renameKey('oldKey', 'newKey');

// Export data to a file
db.exportData();

// Import data from a file
db.importData('backup_database.json');

API Documentation

set(key, value) = Sets a key-value pair in the database.

get(key) = Retrieves the value associated with a key.

fetch(key) = Alias for get.

has(key) = Checks if a key exists in the database.

fetchAll() = Retrieves all data in the database.

delete(key) = Deletes a key and its associated value from the database.

reset(key) = Deletes all data if no key is provided. Deletes keys starting with the specified key if provided.

all() = Retrieves all key-value pairs in the database.

subtract(key, value) = Subtracts a value from a numeric key's value.

slice(start, end) = Creates a new object containing a portion of the key-value pairs, starting from index start up to but not including index end.

findIndex(callback) = Finds the index of the first element in the database that satisfies the provided testing function.

filter(callback) = Creates a new array with all elements that pass the test implemented by the provided function.

sort(comparefunction) = Sorts the key-value pairs based on the provided compare function.

add(key, value) = Adds a value to a numeric key's value.

push(key, value) = Pushes a value to an array associated with the key.

pull(key, value) = Removes a specific item from an array associated with a key.

math(key, limit, value) = Performs basic arithmetic operations on a numeric key's value.

renameKey(oldKey, newKey) = Renames a specific key to a new specific key.

getStats() = Retrieves statistics about the database, returns with an object with the number of keys and total data size.

exportData() = Exports the current data from the database.

importData(data file name) =  Imports data into the database.

Contributing

If you'd like to contribute, please fork the repository and make changes as you'd like. Pull requests are warmly welcome.

License

This project is licensed under the MIT License.

Package Sidebar

Install

npm i darko.db

Weekly Downloads

7

Version

1.1.0

License

MIT

Unpacked Size

11.5 kB

Total Files

4

Last publish

Collaborators

  • notdarko