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

jsonfile-storage

v0.0.16

Published

Package with a simple API for storing json files to a local directory.

Downloads

18

Readme

JSON File Storage

Package with a simple API for storing json files to a local directory.

Coverage Status Build Status

Getting Started

npm i jsonfile-storage

var JSONFileStorage = require('jsonfile-storage');

// directory must exist before creating storage instance
var storage = new JSONFileStorage('./data');

// put stuff into the storage
storage.put({ "key" : "stuff" }).then(result => {
    console.log(result);
    // outputs: { key: 'stuff', id: '137280bc-8afe-4ba2-9625-8c1d20b13c58' }
    // the id is generated automatically if it does not exist
    
    // remove item from the storage
    storage.remove(result.id).then(() => {
        // file was removed from storage
        console.log(storage.getFiles());
        // outputs: []
    })
})

API

JSONFileStorage

Kind: global class

new JSONFileStorage(directoryPath)

Creates a new instance of JSONFileStorage

Throws:

  • Error if directoryPath is not a string or an empty string

| Param | Type | Description | | --- | --- | --- | | directoryPath | string | The path of the folder where the data will be saved/retrieved |

jsonFileStorage.get(id) ⇒ Promise.<any>

Get a single item from the directory with the id

Kind: instance method of JSONFileStorage
Returns: Promise.<any> - a promise that resolves into the file that matches the id. The promise is rejected if the file is not found with the id. Or if there is an error parsing the JSON.

| Param | Type | Description | | --- | --- | --- | | id | string | id of the item to get |

jsonFileStorage.getBulk() ⇒ Promise.<Array.<any>>

Gets all the items from the directory and their content

Kind: instance method of JSONFileStorage

jsonFileStorage.put(item, [updateListing]) ⇒ Promise.<any>

Puts a single item in the directory

Kind: instance method of JSONFileStorage
Returns: Promise.<any> - promise that resolves into the file that was put

| Param | Type | Default | Description | | --- | --- | --- | --- | | item | any | | item to be put in to the directory | | [updateListing] | boolean | true | should the files property be updated. Default is true |

jsonFileStorage.putBulk(items) ⇒ Promise.<Array.<any>>

Puts items in the directory in bulk

Kind: instance method of JSONFileStorage
Returns: Promise.<Array.<any>> - items putted to the directory
Throws:

  • Error if items is not an array

| Param | Type | Description | | --- | --- | --- | | items | Array.<any> | items to be put to the directory |

jsonFileStorage.remove(id, [updateListing])

removes the specified file from the directory

Kind: instance method of JSONFileStorage

| Param | Type | Default | Description | | --- | --- | --- | --- | | id | string | | id of the file | | [updateListing] | boolean | true | should the file listing be updated. Default is true |

jsonFileStorage.removeBulk(ids) ⇒ Promise.<void>

Deletes files in bulk

Kind: instance method of JSONFileStorage

| Param | Type | Description | | --- | --- | --- | | ids | Array.<string> | Array of ids for the files to be deleted |

jsonFileStorage.getFiles() ⇒ Array.<string>

Get the json files inside the directory

Kind: instance method of JSONFileStorage
Returns: Array.<string> - files in the directory

jsonFileStorage.changeDirectory(directoryPath)

Changes the directory used within this class

Kind: instance method of JSONFileStorage

| Param | Type | Description | | --- | --- | --- | | directoryPath | string | path where to change to |