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

storage-to-json

v1.0.48

Published

Save data to json, persistent!

Downloads

9

Readme

Storage-to-json

Installation


//Install it at your project directory!
npm i storage-to-json

Usage


// Example on how to utilize.
const Storage = require('storage-to-json');
const defaultSettings = {
  This_Needs_To_Exist: 'OK',
  ThisAlso: 'OK',
};

const JsonFile = 'testeroni';
const Store = new Storage(JsonFile);

//=> Applies only for non memory storage.
Store.clear();

//=> Returns MyName
Store.set('MyName', { name: 'Gerkiz' });
Store.logger('Name is: "' + Store.get('MyName').name + '"');

//=> Replaces MyName with FirstName
Store.replace('MyName', 'FirstName');
Store.logger('Name is: "' + Store.get('FirstName').name + '"');

//=> MyName is undefined.
Store.logger('Name is: "' + Store.get('MyName').name + '"');

//=> Default value set for "default"
Store.ensure('default', defaultSettings);

Store.each((key, val) => {
  console.log(JSON.stringify(key));
  console.log(JSON.stringify(val));
});

//=> We can also use the storage as a proxy, like below:
const data = Store.proxy();
data.awesome_storage = true;
Store.logger('Does awesome_storage exist? ' + Store.get('awesome_storage'));

//=> Backups the persistent storage.
Store.backup(JsonFile);

//=> Removes a key
Store.remove('FirstName');
Store.remove('default');
Store.remove('awesome_storage');

API


//=> Require at first.
const Storage = require('storage-to-json');

//=> Use a storage that already exists or creates it if it does not exist.
//=> You can now utilize multiple instances of Storage.
//=>
//=> NOTE!
//=> If no name is provided for the datastore then
//=> the storage is not persistent but is instead
//=> kept in memory.
const Store = new Storage('datastore');

//=> Set a key, value -- .set can also be used to replace an old value of a key.
Store.set('key', { name: 'value' });

//=> Get key.
Store.get('key').name;
//=> value

//=> Validates that given key exists.
Store.validate('key');

//=> Same as Store.validate
Store.has('key');

//=> Fetches the whole storage.
Store.get_storage();

//=> Remove key.
Store.remove('key');

//=> Remove value only.
Store.remove('key', 'value');

//=> Same as Store.remove
Store.delete('key', 'value');

//=> Backup a file for future use. Appends %date%.
Store.backup('datastore');

//=> Clear all keys.
//=> Only applies if we're using non memory storage.
Store.clear();

//=> Replace a key with another key.
Store.replace('old', 'new');

//=> enableLogger, boolean (default true).
Store.enableLogger(boolean);

//=> Ensures that the key default exists, if not then obj is copied to key default.
Store.ensure('defualt', obj);

//=> If enableLogger, then stdout to console.
Store.logger(str);

//=> Loops over all stored values.
Store.each(function (value, key) {
  console.log(key, '==', value);
});

//=> We can also use the storage as a proxy to modify nested datasets.
Store.proxy();

// Hint, most of the API is awaitable
await Store.setAsync(...)
await Store.eachAsync(...)
await Store.ensureAsync(async (...) => {})