1.1.6 • Published 3 years ago
slowkidb v1.1.6
SlowkiDB
A simple key-value database store.
Functions
db.set(key, value)
- Set a value in the database.db.get(key)
- Get a value in the database.db.has(key)
- Returns true if the key has an associated value or is listed in the database, false if it doesn't exist.db.delete(key)
- Delete a value in the database.db.clear()
- Clear all values in the database.db.add(key, amount)
- Add a certain amount to a value in the database. Adds to 0 if a number isn't present.db.subtract(key, amount)
- Subtract a certain amount from a value in the database. Subtracts from 0 if a number isn't present.db.push(key, value)
- Pushes a value to an array. Makes a new array if an array isn't present.db.list(prefix)
- Lists all keys in the database that starts with a specific prefix. Lists every key if prefix is ommited.
Installation:
npm install slowkidb
or using shorthand
npm i slowkidb
Importing:
Import with node require()
const SlowkiDB = require("slowkidb");
const db = new SlowkiDB()
Usage:
const SlowkiDB = require("slowkidb") // Imports the database.
const db = new SlowkiDB() // Initialises the database.
db.clear() // Deletes all items in database.
db.set("string", "A cool string!") // Sets a value in the database.
console.log(db.get("string")) // A cool string!
/* --------------- Arrays --------------- */
console.log(db.get("items")) // undefined
db.push("items", "Sword") // Adds "Sword" to an array (is created if doesn't exist)
console.log(db.get("items")) // [ 'Sword' ]
db.push("items", "Coffee") // Adds "Coffee" to the array.
console.log(db.get("items")) // [ 'Sword', 'Coffee' ]
/* --------------- Numbers --------------- */
db.set("number", 31) // Sets "number" to 31.
console.log(db.get("number")) // 31
db.add("number", 10.5) // Adds 10.5 to "number" (if entry doesn't exist, it adds to 0.)
console.log(db.get("number")) // 41.5
db.subtract("number", 23) // Subtracts 23 from "number" if entry doesn't exist, it subtracts from 0.)
console.log(db.get("number")) // 18.5
/* --------------- Database --------------- */
console.log(db.list()) // Every key in the database, in our case it would be: ["items", "number", "string"], which could be iterated through and using db.get(key).
// Setting entries for next example.
db.set("entry1", "Hello!")
db.set("entry2", "This is a")
db.set("entry3", "database example")
db.set("entry4", "where keys are listed")
db.set("entry5", "by a prefix!")
console.log(db.list('entry')) // Lists all keys starting with a specific prefix, in our case the prefix "entry" which would return: ["entry1", "entry2", "entry3", "entry4", "entry5"].
console.log(db.has("entry5")) // true, as "entry5" exists within the database.
console.log(db.has("entry6")) // false, as we have not yet added "entry6" to our database.
db.delete("entry5") // returns with "true" if deletion was successful, "false" if it couldn't be deleted, or "undefined" if it does not exist.
Utils:
Utilities for working with raw database.
const Utils = db.Utils
// Converts provided .skidb data and checks if it's JSON.
console.log(Utils.isJSON('4e6a55334f5452684e6d59324d54557a4e446b7a4e6a526b4e5451304d6a4d35')) // True, as the info provided converts into {"hi": 10}.
console.log(Utils.isJSON('{"hi": "10"}')) // False, as it is in plain JSON, and not encoded.
// Converts provided JSON in to .skidb data.
console.log(Utils.toSkiDB({"hi": 10})) // Returns with '4e6a55334f5452684e6d59324d54557a4e446b7a4e6a526b4e5451304d6a4d35'.
// Converts provided .skidb data in to plain JSON. Returns with a string if converted data isn't JSON.
console.log(Utils.toJSON('4e6a55334f5452684e6d59324d54557a4e446b7a4e6a526b4e5451304d6a4d35')) // {"hi": 10}
console.log(Utils.toJSON('4e446b325a4459344e7a41304f5459334d32517a5a413d3d')) // "hi". Note how it is a string and not JSON.
Support Links:
Changelog
Version 1.0.0
- Everything!
Version 1.0.1
- README.md added.
- Minor bug fixes.
Version 1.0.2
- README.md edited.
Version 1.1.0
- Revamped whole system.
- JSON data is now encoded. (4 times!)
- Removed all dependencies (except for fs).
Version 1.1.1
- README.MD edited (changelog added).
- A very major bug fix (you can now use any function that changes a value.)
Version 1.1.2
- README.MD edited (fixed a typo, style is now consistent).
Version 1.1.3
- README.MD edited (fixed a typo again).
Version 1.1.4
- README.MD edited (fixed example and added a new section).
- Optimised code.
const Utils = db.Utils // Utils added.
Version 1.1.6
- SlowkiDB now caches the entire Database for faster read/write.