1.2.6 • Published 7 years ago

storage.json v1.2.6

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

storage.json

NPM package providing simplified create, read, update, and delete methods for JSON files. Also provides methods which simplify using JSON files to store configuration data.


Installation:

Install:

npm install storage.json 

Require:

var storage = require('storage.json' );

File Names and Paths:

Storage.json accepts simplified path names and assumes that the file extension will always be ".json". Because of this, finding the correct file can be done in several ways.

Name only

"filename"

Name with extension

"filename.json"
With property
"filename.property"

With deep property

"filename.deeply.nested.property"

With extension and property

"filename.json.property"

With complete path

"/path/to/filename"

With complete path and property

"/path/to/filename.property"

Note: If your property is "json", you MUST include the extension ( e.g. "/path/to/filename.json.json" )

Usage and Examples:

For the "Finding" and "Reading" examples, assume filename.json contains :

{ "property": "This is the value of 'property' from filename.json" }

Finding Files and Data


has()


Returns TRUE if the filename and/or property exists and has been assigned a value, otherwise returns FALSE

console.log( storage.has( 'filename' )); // TRUE
console.log( storage.has( 'filename.property' )); // TRUE
console.log( storage.has( 'missing_file' )); // FALSE
console.log( storage.has( 'filename.missing' )); // FALSE  

Reading Files and Data


get()


Returns the files contents or value if found, otherwise returns undefined

    // Read entire file
    var contents = storage.get( 'filename' );
    console.log( contents ); //{ property: "This is the value of 'property' from filename.json" }
    
    // Read property within file
    var contents = storage.get( 'filename.property' );
    console.log( contents ); //"This is the value of 'property' from filename.json"
    

Writing Files and Data


set()


Merges an Object into a file, OR assigns a value to the property specified

    // Returns a boolean value asserting that the file was changed.
    var set = storage.set( 'new_file', { property: "value" });
    console.log( set ) //TRUE
    var set = storage.set( 'new_file', { property: "value" });
    console.log( set ) //FALSE
    
    // Creates a new file if the file does not exist
    console.log( storage.exists( 'new_file' )); //FALSE
    storage.set( 'new_file', { property: "value" });
    console.log( storage.exists( 'new_file' )); //TRUE
    
    // It will not overwrite existing properties, unless specified
    console.log( storage.get( 'filename' ) //{ property: "value" };
    storage.set( 'filename', {});
    console.log( storage.get( 'filename' ) //{ property: "value" };
    
    // It will overwrite existing properties, when specified
    console.log( storage.get( 'filename' ) //{ property: "value" };
    
    storage.set( 'filename', { property: "new value" });
    // OR
    storage.set( 'filename.property', "new_value" );
    
    console.log( storage.get( 'filename' ) //{ property: "new_value" };
    

insist()


Ensures that the file exists and return the filename to simplify inline calls

var file = storage.insist( 'new_file' );
console.log( storage.get( file )) //{};

update()


Overwrites the contents of an EXISTING file, with a new Object.

// Returns a boolean value
// File Missing:
console.log( storage.get( 'filename' ) //undefined
var updated = storage.update( 'filename', { property: "value" });
console.log( updated ); //FALSE

// File Exists:
console.log( storage.get( 'filename' ) //{}
var updated = storage.update( 'filename', { property: "value" });
console.log( updated ); //TRUE

It will NOT create a new file if the file does not exist

console.log( storage.get( 'filename' ) //undefined
var updated = storage.update( 'filename', { property: "value" });
console.log( updated ); //FALSE
console.log( storage.get( 'filename' ) //undefined

Configuration FIles and Data


config()

Create configuration files to store values common within an application, which may need to be changed depending upon their usage.

Examples:

// The default settings Object
var defaults = { some_prop: true, some_prop_2: false };
// A dynamic settings Object
var argument = { some_prop: false };
    
var config = storage.config( "MyConfigurationFile", argument );
console.log( config ); //{ "some_prop": "false" }
console.log( storage.get( "MyConfigurationFile" )); //{ "some_prop": "false" }
    
var config = storage.config( "MyConfigurationFile", defaults );
console.log( config ); //{ "some_prop": "true", "some_prop_2": "false" }
console.log( storage.get( "MyConfigurationFile" )); //{ "some_prop": "true", "some_prop_2": "false" }
    

If passing in two Objects, the Objects will be merged, and any overlapping properties will be assigned the first Object's assigned value.

var config = storage.config( "MyConfigurationFile", argument, defaults );
console.log( config ); //{ "some_prop": "false", "some_prop_2": "false" }
console.log( storage.get( "MyConfigurationFile" )); //{ "some_prop": "false", "some_prop_2": "false" }
1.2.6

7 years ago

1.2.5

8 years ago

1.2.3

8 years ago

1.2.2

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.8

8 years ago

1.1.7

8 years ago

1.1.6

9 years ago

1.1.5

9 years ago

1.1.4

9 years ago

1.1.3

9 years ago

1.1.2

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago