npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

keystonesdb

v1.1.3

Published

KeystonesDB is a simple and lightweight JSON Database

Downloads

1

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 key
  • contains(key) - If a key exists in a database
  • remove(key) - Removes a key
  • all() - 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 path
  • import(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 key
  • subtractValue(key, number) - Subtracts a number from a key
  • renameKey(oldKey, newKey) - Renames a key
  • isOfType(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 value
  • arrayLength(key) - Checks the array length
  • getItemFromArray(key, index) - Returns the the item of an array based on its index
  • updateItemInArray(key, index, newValue) - Updates the the item of an array based on its index
  • clearArray(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