0.0.16 • Published 6 years ago

jsonfile-storage v0.0.16

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

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
ParamTypeDescription
directoryPathstringThe 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.

ParamTypeDescription
idstringid 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

ParamTypeDefaultDescription
itemanyitem to be put in to the directory
updateListingbooleantrueshould 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
ParamTypeDescription
itemsArray.<any>items to be put to the directory

jsonFileStorage.remove(id, updateListing)

removes the specified file from the directory

Kind: instance method of JSONFileStorage

ParamTypeDefaultDescription
idstringid of the file
updateListingbooleantrueshould the file listing be updated. Default is true

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

Deletes files in bulk

Kind: instance method of JSONFileStorage

ParamTypeDescription
idsArray.<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

ParamTypeDescription
directoryPathstringpath where to change to
0.0.16

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago