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

jsdatabase.json

v1.1.0

Published

A wrapper around JSON to provide a lightweight, persistent, local database.

Downloads

5

Readme

JSONDB

A simple wrapper around JSON to implement a persistent psuedo-database.

Information

What does this module do, and how does it work?

This module is a wrapper around JSON which makes it easier to work with. This database is persistent, meaning that even after a Node.js process is closed, the data is not lost. This is a local database, meaning it will be in your codebase and ready to view at any time. It is also portable, meaning if you copy this folder to a new directory, the data will still be read in the same way.

Usage

JSONWrapper

This is a Class, which takes in an Options object as a parameter when intializing.

interface Options {
    /** The path to read from or write to. */
    file: string;
    /** Specify whether or not to create backups. Defaults to `false` */
    backup?: boolean;
    /** Specify whether or not to create verbose logs. Defaults to `true`. */
    verbose?: boolean;
    /** Whether or not to defer saves to file. `0` means no delay, default is `0`. */
    defer?: number;
    /** The amount of indents to use when saving to file. Defaults to `0`. */
    indents?: number;
}

Upon instantiation, this has a property called pool, where all handling of the database occurs.

Pool

Options.file determines the file it should write to. It is a mandatory property.

Options.backup is currently not implemented. In the future, it will store multiple backups of the database locally in the event of data loss. Defaults to false.

Options.verbose logs debug logs. Defaults to false.

Options.defer takes a number (in millisecond format), to save at that specific interval. Default is 0, meaning it saves after every operation.

Options.indents takes a number and indents the JSON database by it. Defaults to 0.

All methods of Pool are asynchronous, and return a Promise. Will return Promise<void> if Options.defer exists.

Pool.get(key, path) retreives a value from the database.

Pool.find(callback, all?) retrieves entries given a callback. all defaults to true, determines if all entries should be given back.

Pool.set(key, value, path) sets a value to the database.

Pool.forEach(callback) iterates over each member of a database and runs a callback.

Pool.ensure(key, value, path) sets a value to the database if the key does not have a value already assigned to it.

Pool.has(key, path) determines whether or not a key exists.

Pool.delete(key) deletes an entry from the database.

Pool.clear() deletes the database.

Pool.keys(), Pool.values(), and Pool.entries() are identical to their Array counterparts.

Pool.observe(key, path, cb) observes an object for any changes, and executes the callback if a change occurs.

Pool.lock() makes the database readonly, and Pool.unlock() makes it writable.

Pool.import(data) overwrites a database with incoming data, Pool.export() exports the database.

Example:

const { JSONWrapper } = require('jsdatabase.json');
const database = new JSONWrapper({
    file: 'db',
    verbose: true,
    indents: 4
}).pool;

// Get/Set without paths
database.set('a', 3);
console.log(database.get('a')); // 3

// Get/Set with paths
database.set('a', { b: 4 });
console.log(database.get('a')); // { b: 4 }
console.log(database.get('a', 'b')); // 4
database.set('a', 5, 'b');
console.log(database.get('a', 'b')); // 5