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

json-fs-store

v1.0.1

Published

File system storage and retrieval of objects as JSON

Downloads

925

Readme

This Node.js npm module simply serializes JavaScript objects to JSON files into the file system directory of your choosing.

Build Status Dependencies

Installing

npm install json-fs-store

Creating a store

The store module is a function that takes a single parameter: the path to the location on the file system where you want to store your objects. If you omit the storage location the 'store' directory in your current working directory will be used.

var store = require('json-fs-store')('/path/to/storage/location');

Adding an object

A stored object must have an id attribute (one will be provided if it does not). The object will be serialized to JSON using JSON.stringify and written to the storage location.

To customize the JSON, you can define the #toJSON function on the object to be stored. That function must return a JavaScript object.

var donkey = {
  id: '12345',
  name: 'samuel',
  color: 'brown'
};

store.add(donkey, function(err) {
  // called when the file has been written
  // to the /path/to/storage/location/12345.json
  if (err) throw err; // err if the save failed
});

Retrieving an object

To retrieve an object, you must know its id attribute and use it as a parameter for the load() function.

store.load('12345', function(err, object){
  if(err) throw err; // err if JSON parsing failed

  // do something with object here

});

Listing stored objects

Every call to the list() function reads the file system and returns the objects stored in the directory you specified when you created your store. Objects will be sorted according to their name attribute, if defined.

store.list(function(err, objects) {
  // err if there was trouble reading the file system
  if (err) throw err;
  // objects is an array of JS objects sorted by name, one per JSON file
  console.log(objects);
});

Removing stored objects

A stored object may be removed simply by passing the object's id attribute to the remove() function. The attribute will be used to remove the object's file from the file system.

store.remove('12345', function(err) {
  // called after the file has been removed
  if (err) throw err; // err if the file removal failed
});

FIN.