keystonesdb
v1.1.3
Published
KeystonesDB is a simple and lightweight JSON Database
Downloads
17
Maintainers
Readme
KeystonesDB
KeystonesDB is a simple and lightweight JSON Database
npm install keystonesdb
const KeystonesDB = require('keystonesdb');
// Creating the database -> path of db, format json, other options
let DB = new KeystonesDB('./path.json', true, {
caching: 'none'
});
Use an array of keys (e.g., ['parentKey', 'childKey', ...]
) to specify nested keys within the database. This allows you to access values at deeper levels of the data structure.
Functions
Basics
set(key, value, createIfMissing)
- Sets the value of a key. If createIfMissing is true it will create the keys if they are missing. This value is true by default. The key placeholder can hold a path to a key in the form of an array (e.g ['key1', 'key2',...]).get(key)
- Returns the value of a keycontains(key)
- If a key exists in a databaseremove(key)
- Removes a keyall()
- Returns the entire database
Database
backup(path, async)
- Creates a backup of the database at the provided path. If Async is set to true it will create the backup in a new worker thread (Useful for bigger databases)restore(path)
- Restores the database from the provided backup json file pathimport(path)
- Imports the json from the provided file path into the database. (Works the same as the restore() funtion)
Other
addValue(key, number)
- Adds a number to a keysubtractValue(key, number)
- Subtracts a number from a keyrenameKey(oldKey, newKey)
- Renames a keyisOfType(key, dataType)
- Checks if the value of a key is the provided data type (e.g boolean, string, array, object,...)- `` - more soon
Arrays
push(key, value, createIfMissing)
- Pushes an item into an array. if createIfMissing is set to true, the array will be created in case it doesn't exist.removeFromArray(key, value)
- Removes all the occurences of the provided valuearrayLength(key)
- Checks the array lengthgetItemFromArray(key, index)
- Returns the the item of an array based on its indexupdateItemInArray(key, index, newValue)
- Updates the the item of an array based on its indexclearArray(key)
- Clears all the items in an array
Filtering & Conditions
matchesCondition(key, conditionFunction)
- Checks if the value of the key meets the condition you provided (Scroll down to learn more)filterArray(key, conditionFunction)
- Filters an array based on the provided condition (Scroll down to learn more)
Events (w.i.p)
beforeSet
- Event before a value is set
DB.on('beforeSet', ({ pathOrKey, value }) => {
console.log(`Before setting value at ${pathOrKey}:`, value);
});
afterSet
- Event after a value is set
DB.on('afterSet', ({ pathOrKey, value }) => {
console.log(`After setting value at ${pathOrKey}:`, value);
});
beforeRemove
- Before a key is removed
DB.on('beforeRemove', ({ pathOrKey }) => {
console.log(`Before removing key at ${pathOrKey}`);
});
afterRemove
- After a key is removed
DB.on('afterRemove', ({ pathOrKey }) => {
console.log(`After removing key at ${pathOrKey}`);
});
Conditions
Using the MatchesCondition method we can check if a certain key matches our provided condition. You can use about almost any conditioning you can think about including regex. A couple of examples: Check if a user is older than 18
const isOver18 = value => value >= 18;
console.log(DB.matchesCondition(['user', 'age'], isOver18));
Check if an email is valid using regex
const isValidEmail = value => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
console.log(DB.matchesCondition('email', isValidEmail));
Their age is between 25 and 40. They have a job title containing the word "Engineer". They are active employees (i.e., their isActive status is true).
const isDesiredEmployee = employee => {
return employee.age >= 25 && employee.age <= 40 &&
employee.jobTitle.includes('Engineer') &&
employee.isActive === true;
};
console.log(DB.matchesCondition(['employees', '123'], isDesiredEmployee));
Caching
(This is a new feature and is subject to bugs and problems. Importing and Restoring might not work as expected)
Since of v1.1.0 You can use a very basic version of caching in your database allowing for faster loading times. Caching works completely automatically and you don't have to setup anything besides setting the caching
value to your preferred method.
| Caching Method | Description |
|-----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| write-through
| This method first writes data to a cache and then to the database. It's efficient for smaller databases, enhancing data retrieval speed. However, as the database grows, updating the cache with every write can slow down the database due to increased management overhead. |
Changelog
v1.1.3 - minified code fix v1.1.2 - Get method spams keys fix v1.1.1 - Updated README v1.1.0 - Added basic "Write-Through" Caching