3.0.3 • Published 2 months ago

propertiesmanager v3.0.3

Weekly downloads
13
License
ISC
Repository
github
Last release
2 months ago

propertiesmanager

This module helps to easily manage all the configuration properties needed in a system, giving a simple and consistent configuration interface to your application. The properties must have a profile category, production,dev or test. File properties are stored in folder config in a file named properties.json within your application home directory(.../config/properties.json). The package use npm minimist, so your properties can be overridden by command line parameters. The package is compliant to JSON5 so you can add single and multi line comments like // or /% %/ in your JSON file

NPMNPM

Installation

To install propertiesmanager, type:

$ npm install propertiesmanager

Property file creation

Configuration files must be created in a folder named config in the home directory of your application. The filename must be named default.json. Type:

$ mkdir config
$ vi config/default.json

Property file population

The file containing your properties is a JSON file having a mandatory dictionary called "production". It contains all the configuration parameters of your application running in production or default mode. Other dictionaries can be used, "dev" for development properties and "test" for testing properties.

An example of empty property file:

{
    "production":{}
}

An example of populated property file:

{
    "production":{
        "properties_One":"One",
        "properties_Two":"Two",
        "Objectproperties":{
            "Obj_One":1,
            "Obj_Two":2
        }    
    }
}

An example of property file with dev and test dictionaries defined:

{
    "production":{
        "properties_One":"One",
        "properties_Two":"Two",
        "Objectproperties":{
            "Obj_One":1,
            "Obj_Two":2
        }    
    },
    "test":{
       "properties_One":"TestOne",
       "Objectproperties":{
           "Obj_One":1,
           "Obj_Two":2
       }  
    },
    "dev":{
       "properties_One":"Test Development",
       "DevLogs":{
           "path":"/logs/log.xml",
           "format":"xml"
       }  
    }
}

Usage

Including propertiesmanager

Just require it like a simple package:

var propertiesmanager = require('propertiesmanager').conf;

Using propertiesmanager

propertiesmanager returns a dictionary containing all the properties from a configuration file. These properties can be overridden by command line parameters.

   // print the loaded properties dictionary
   console.log(propertiesmanager);   

Usage in nodule_module packages

If you use this package to develop other node_modules, then add "install": "npm install propertiesmanager" in the scripts tag in your package.json as below:


 // yur package.json
{
   ......
   ......
   "scripts": {
       .......
       "install": "npm install propertiesmanager"
     },
   ......
   ......
}

You need to do this because propertiesmanager looks for the property file (config/default.json) in a folder located two levels up the node_modules folder, so propertiesmanager must be installed in the node_modules folder of the package that uses it as a dependence

Loading a running profile

The application using this package runs under one profile among three (production, dev, test), set by NODE_ENV environment variable. If NODE_ENV is not defined the default profile is production

Running your app in default mode. production properties are loaded:

$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ npm start   

Running your app in production mode. production properties are loaded:

$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=production npm start

Running your app in dev mode. dev properties are loaded:

$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=dev npm start

Running your app in test mode. test properties are loaded:

$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=test npm start

Override loaded parameters from command line

The package propertiesmanager use npm minimist, so your properties stored in default.json can be overridden by command line parameters. Just type in command line --ParamName=ParamValue, as in:

$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=dev npm start -- --properties_One="Override_TestOne"

The first -- after npm start means that the following params must be passed to node bin/www, so if you run your application directly calling node bin/www the first -- must be not used, as in:

$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=dev node bin/www --properties_One="Override_TestOne"

To override parameters that are complex objects, use dotted (".") notation. For example:


 // We want to override Obj_One
{
    "production":{
        "properties_One":"One",
        "properties_Two":"Two",
        "Objectproperties":{
            "Obj_One":1,
            "Obj_Two":2
        }    
    }     
}

$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=dev node bin/www --Objectproperties.Obj_One="Override_Obj_One"

For further information about passing parameter see https://www.npmjs.com/package/minimist

Examples

File Properties creation

From your home project directory type:

$ mkdir config
$ vi config/default.json

Write default.json property file:

{
    "production":{
        "properties_One":"One",
        "properties_Two":"Two",
        "Objectproperties":{
            "Obj_One":1,
            "Obj_Two":2
        }    
    },
    "test":{
       "properties_One":"TestOne",
       "Objectproperties":{
           "Obj_One":1,
           "Obj_Two":2
       }  
    },
    "dev":{
       "properties_One":"Test Development",
       "DevLogs":{
           "path":"/logs/log.xml",
           "format":"xml"
       }  
    }
}

Now you can print all your properties:

   var propertiesmanager = require('propertiesmanager').conf;
   
   // print the loaded properties dictionary
   console.log("########### Readed Properties ###########" );
   console.log(propertiesmanager);   

Running your app in default mode, production properties are loaded:

$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ npm start
########### Read Properties ###########
"production":{
          "properties_One":"One",
          "properties_Two":"Two",
          "Objectproperties":{
              "Obj_One":1,
              "Obj_Two":2
          }    
      }     

Running your app in production mode (NODE_ENV=production) is equivalent to run in default mode:

$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=production npm start
########### Readed Properties ###########
"production":{
          "properties_One":"One",
          "properties_Two":"Two",
          "Objectproperties":{
              "Obj_One":1,
              "Obj_Two":2
          }    
      }     

Running your app in dev mode (NODE_ENV=dev), dev properties are loaded:

$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=dev npm start
########### Readed Properties ###########
"dev":{
       "properties_One":"Test Development",
       "DevLogs":{
           "path":"/logs/log.xml",
           "format":"xml
       }  
    }

Running your app in test mode (NODE_ENV=test), test properties are loaded:

$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=test npm start
########### Readed Properties ###########
 "test":{
       "properties_One":"TestOne",
       "Objectproperties":{
           "Obj_One":1,
           "Obj_Two":2
       }  
    }

Overriding some test mode (NODE_ENV=test) properties:

$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=dev npm start -- --properties_One="Override_TestOne"
########### Readed Properties ###########
 "test":{
       "properties_One":"Override_TestOne",
       "Objectproperties":{
           "Obj_One":1,
           "Obj_Two":2
       }  
    }

License - "MIT License"

MIT License

Copyright (c) 2016 aromanino

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Author

CRS4 Microservice Core Team (cmc.smartenv@crs4.it)

Contributors

Alessandro Romanino (a.romanino@gmail.com) Guido Porruvecchio (guido.porruvecchio@gmail.com)

3.0.3

2 months ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.3

6 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago