JSON File Storage
Package with a simple API for storing json files to a local directory.
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
- JSONFileStorage
- new JSONFileStorage(directoryPath)
- .get(id) ⇒
Promise.<any>
- .getBulk() ⇒
Promise.<Array.<any>>
- .put(item, [updateListing]) ⇒
Promise.<any>
- .putBulk(items) ⇒
Promise.<Array.<any>>
- .remove(id, [updateListing])
- .removeBulk(ids) ⇒
Promise.<void>
- .getFiles() ⇒
Array.<string>
- .changeDirectory(directoryPath)
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 |
Promise.<any>
jsonFileStorage.get(id) ⇒ 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 |
Promise.<Array.<any>>
jsonFileStorage.getBulk() ⇒ Gets all the items from the directory and their content
Kind: instance method of JSONFileStorage
Promise.<any>
jsonFileStorage.put(item, [updateListing]) ⇒ 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 |
Promise.<Array.<any>>
jsonFileStorage.putBulk(items) ⇒ 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 |
Promise.<void>
jsonFileStorage.removeBulk(ids) ⇒ Deletes files in bulk
Kind: instance method of JSONFileStorage
Param | Type | Description |
---|---|---|
ids | Array.<string> |
Array of ids for the files to be deleted |
Array.<string>
jsonFileStorage.getFiles() ⇒ 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 |