### Install @hapipal/confidence Source: https://github.com/hapipal/confidence/blob/master/README.md Installs the Confidence library using npm. This is the first step to using Confidence in your Node.js project. ```sh npm install @hapipal/confidence ``` -------------------------------- ### Setting Environment Variables Source: https://github.com/hapipal/confidence/blob/master/API.md Provides examples of environment variables that can be used with the Confidence configuration library. ```sh MYSQL_HOST=xxx.xxx.xxx.xxx MYSQL_PORT=3306 MYSQL_USER=user1 MYSQL_PASSWORD=some_password MYSQL_DATABASE=live_db ``` -------------------------------- ### Confidence Store Get Metadata Source: https://github.com/hapipal/confidence/blob/master/API.md Retrieves metadata from the configuration document after applying optional criteria. Keys must start with '/'. '/' returns the entire document. If the key is invalid, not found, or has no metadata, undefined is returned. ```javascript const value = store.meta('/c', { size: 'big' }); ``` -------------------------------- ### HapiPal Configuration Filtering Example Source: https://github.com/hapipal/confidence/blob/master/API.md Provides a comprehensive example of HapiPal configuration with nested filters and conditional logic. It demonstrates how different criteria (env, platform, xfactor, random) are applied to resolve values, including nested objects and default fallbacks. ```json { "key1": "abc", "key2": { "$filter": "env", "production": { "deeper": { "$value": "value" } }, "$default": { "$filter": "platform", "android": 0, "ios": 1, "$default": 2 } }, "key3": { "sub1": 123, "sub2": { "$filter": "xfactor", "yes": 6 } }, "ab": { "$filter": "random.a", "$range": [ { "limit": 10, "value": 4 }, { "limit": 20, "value": 5 } ], "$default": 6 }, "$meta": { "description": "example file" } } ``` -------------------------------- ### Confidence Store Get Value Source: https://github.com/hapipal/confidence/blob/master/API.md Retrieves a value from the configuration document after applying optional criteria. Keys must start with '/'. '/' returns the entire document. If the key is invalid or not found, undefined is returned. ```javascript const value = store.get('/c', { size: 'big' }); ``` -------------------------------- ### Confidence Document Basic Structure Source: https://github.com/hapipal/confidence/blob/master/API.md A basic configuration document starts with a simple object. Keys can only contain alphanumeric characters and '_'. The '$' prefix is reserved for special directives. Values can be any non-object type or arrays. ```json { "key1": "abc", "key2": 2 } ``` -------------------------------- ### Configure hapi Server with Confidence Source: https://github.com/hapipal/confidence/blob/master/README.md Demonstrates how to use Confidence to configure a hapi server. It shows how to define server host, port, and debug settings dynamically based on environment variables and filters. ```javascript const Hapi = require('@hapi/hapi'); const Confidence = require('@hapipal/confidence'); const store = new Confidence.Store({ server: { host: 'localhost', port: { $param: 'PORT', $coerce: 'number', $default: 3000 }, debug: { $filter: 'NODE_ENV', $default: { log: ['error'], request: ['error'] }, production: { request: ['implementation'] } } } }); const config = store.get('/', process.env); const server = Hapi.server(config); ``` -------------------------------- ### Confidence Store Initialization Source: https://github.com/hapipal/confidence/blob/master/API.md Creates an empty configuration storage container. The optional document parameter should be a valid confidence configuration object. If the document is invalid, an error will be thrown. Defaults to an empty object. ```javascript const Confidence = require('@hapipal/confidence'); const store = new Confidence.Store(); ``` -------------------------------- ### Confidence Store Load Document Source: https://github.com/hapipal/confidence/blob/master/API.md Validates the provided configuration, clears any existing configuration, and then loads the new configuration. The document parameter must be an object containing a confidence configuration object. Invalid documents will result in an error. ```javascript const document = { a: 1, b: 2, c: { $filter: 'size', big: 100, small: 1, $default: 50 } }; store.load(document); ``` -------------------------------- ### Parameter Substitution with $param Source: https://github.com/hapipal/confidence/blob/master/API.md Demonstrates how to reference values from a 'criteria' object using the $param directive for dynamic configuration. This is useful for injecting external data into configuration files, such as database credentials. ```json { "mysql": { "host": { "$param" : "credentials.mysql.host" }, "port": { "$param" : "credentials.mysql.port" }, "user": { "$param" : "credentials.mysql.user" }, "password": { "$param" : "credentials.mysql.password" }, "database": { "$param" : "credentials.mysql.database" } } } ``` -------------------------------- ### Using $env with $default Directive Source: https://github.com/hapipal/confidence/blob/master/API.md Illustrates the use of the '$default' directive to provide fallback values for environment variables if they are not set. This ensures a default configuration is used when an environment variable is missing. ```json { "mysql": { "host": { "$env" : "MYSQL_HOST" }, "port": { "$env" : "MYSQL_PORT", "$default": 3306 }, "user": { "$env" : "MYSQL_USER" }, "password": { "$env" : "MYSQL_PASSWORD" }, "database": { "$env" : "MYSQL_DATABASE" } } } ``` -------------------------------- ### Environment Variable Integration with $env Source: https://github.com/hapipal/confidence/blob/master/API.md Demonstrates referencing environment variables directly using the $env directive within filters. This allows configurations to be dynamically set based on the runtime environment, such as database connection details. ```json { "key1": "abc", "key2": { "$filter": { "$env": "NODE_ENV" }, "production": { "host": { "$env" : "MYSQL_HOST" }, "port": { "$env" : "MYSQL_PORT", "$coerce": "number", "$default": 3306 }, "user": { "$env" : "MYSQL_USER" }, "password": { "$env" : "MYSQL_PASSWORD" }, "database": { "$env" : "MYSQL_DATABASE" } }, "$default": { "host": "127.0.0.1", "port": 3306, "user": "dev", "password": "password", "database": "dev_db" } } } ``` -------------------------------- ### HapiPal Configuration with Metadata Source: https://github.com/hapipal/confidence/blob/master/API.md Demonstrates how to include metadata in HapiPal configuration files, which is ignored by the parser but useful for external tools and human readability. Metadata can be applied to the root object or specific values using the $meta directive. ```json { "key1": "abc", "key2": { "$filter": "system.env", "production": 1, "$default": 2 }, "key3": { "$filter": "random.a", "$range": [ { "limit": 10, "value": 4 }, { "limit": 20, "value": 5 } ], "$default": 6 }, "$meta": { "anything": "really" } } ``` ```json { "key1": { "$value": "abc", "$meta": "whatever" }, "key2": { "$filter": "system.env", "production": 1, "$default": 2 }, "key3": { "$filter": "random.a", "$range": [ { "limit": 10, "value": 4 }, { "limit": 20, "value": 5 } ], "$default": 6 }, "$meta": { "anything": "really" } } ``` -------------------------------- ### Using $env Directive for Environment Variables Source: https://github.com/hapipal/confidence/blob/master/API.md Demonstrates how to use the '$env' directive to pull values from environment variables into a JSON configuration. It shows a sample configuration and the resulting JSON when environment variables are set. ```json { "mysql": { "host": { "$env" : "MYSQL_HOST" }, "port": { "$env" : "MYSQL_PORT" }, "user": { "$env" : "MYSQL_USER" }, "password": { "$env" : "MYSQL_PASSWORD" }, "database": { "$env" : "MYSQL_DATABASE" } } } ``` -------------------------------- ### HapiPal Configuration with Shared Values ($base) Source: https://github.com/hapipal/confidence/blob/master/API.md Illustrates the use of the $base object in HapiPal configurations to define shared values across different configuration profiles. It shows how values from $base are merged and overridden by specific profile values, including array merging logic with $replace. ```json { "$filter": "env", "$base": { "logLocation": "/logs", "flags": ["a", "b"], "tags": { "$value": ["DEBUG"], "$replace": true } }, "production": { "logLevel": "error", "flags": ["c", "d"], "tags": ["INFO", "ERROR"] }, "qa": { "logLevel": "info", "logLocation": "/qa/logs", "flags": ["e", "f"], "tags": ["DEBUG"] }, "staging": { "logLevel": "debug" } } ``` -------------------------------- ### Default Values with $default Source: https://github.com/hapipal/confidence/blob/master/API.md Illustrates the use of the $default directive to provide fallback values when a referenced criterion is undefined or null. This ensures that configurations have sensible defaults, improving robustness. ```json { "mysql": { "host": { "$param" : "credentials.mysql.host" }, "port": { "$param" : "credentials.mysql.port", "$default": 3306 }, "user": { "$param" : "credentials.mysql.user" }, "password": { "$param" : "credentials.mysql.password" }, "database": { "$param" : "credentials.mysql.database" } } } ``` -------------------------------- ### Numerical Bucketing with $range Source: https://github.com/hapipal/confidence/blob/master/API.md Shows how to use the $range directive to assign values based on numerical buckets. A criterion value is matched against the lowest bucket limit it is less than or equal to, providing a way to map ranges of input to specific output values. ```json { "key1": "abc", "key2": { "$filter": "system.env", "production": 1, "$default": 2 }, "key3": { "$filter": "random.a", "$range": [ { "limit": 10, "value": 4 }, { "limit": 20, "value": 5 } ], "$default": 6 } } ``` -------------------------------- ### Confidence Document Nested Structure Source: https://github.com/hapipal/confidence/blob/master/API.md Configuration documents can have nested objects, allowing for hierarchical data organization. ```json { "key1": "abc", "key2": 2, "key3": { "sub1": 123 } } ``` -------------------------------- ### Using $env with $coerce Directive Source: https://github.com/hapipal/confidence/blob/master/API.md Shows how to use the '$coerce' directive to convert environment variable values to specific data types like 'number', 'boolean', 'array', or 'object'. If coercion fails, it falls back to the '$default' directive. ```json { "mysql": { "host": { "$env" : "MYSQL_HOST" }, "port": { "$env" : "MYSQL_PORT", "$coerce": "number", "$default": 3306 }, "user": { "$env" : "MYSQL_USER" }, "password": { "$env" : "MYSQL_PASSWORD" }, "database": { "$env" : "MYSQL_DATABASE" } } } ``` -------------------------------- ### Coercing Array Values with $splitToken Source: https://github.com/hapipal/confidence/blob/master/API.md Demonstrates coercing a string environment variable into an array by specifying a split token. The '$splitToken' key can be used to define the delimiter for splitting the string. ```json { "myArray": { "$env": "MY_ARRAY_VAR", "$coerce": "array", "$splitToken": "," } } ``` -------------------------------- ### Conditional Filtering with $filter Source: https://github.com/hapipal/confidence/blob/master/API.md Explains how to use the $filter directive to select values based on criteria keys. It supports nested filters using dot notation and provides a mechanism for selecting different configurations based on environmental factors. ```json { "key1": "abc", "key2": { "$filter": "env", "production": 1 } } ``` ```json { "key1": "abc", "key2": { "$filter": "system.env", "production": 1 } } ``` ```json { "key1": "abc", "key2": { "$filter": "system.env", "production": 1, "$default": 2 } } ``` -------------------------------- ### Coercing Object Values with JSON.parse Source: https://github.com/hapipal/confidence/blob/master/API.md Shows how to coerce a string environment variable into a JSON object by using the '$coerce': 'object' directive, which internally uses JSON.parse. ```json { "myObject": { "$env": "MY_OBJECT_VAR", "$coerce": "object" } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.