Using Environment Variables
Background
A company needs to connect to different databases in development and production environments for debugging and data synchronization. For such scenarios, you can use the Environment Variables feature to configure environment variables for different runtime environments, improving development and collaboration efficiency, as well as enhancing code compatibility, security, and maintainability.
Implementation Steps
- In the application designer, select Global Settings, then enter Advanced Settings
- Add a new environment variable
- In the application management, enter the application settings, select Environment Variables, and configure the application's runtime environment
Using Environment Variables in Scripts
In automated code snippets or scripts, obtain environment variable values through informat.app.appEnvProp(id)
| Parameter | Type | Description | Return Value |
|---|---|---|---|
| id | String | Environment variable ID | Type String Environment variable value |
/**
* Get database connection
* @returns {informat.JDBCConnection} Returns a JDBC connection object
*/
export function connect() {
const env = informat.app.appEnvProp("env"); // Get the value of the environment variable with ID 'env' in the current application runtime environment
const parseEnv = JSON.parse(env); // Convert the stored JSON format configuration information into an object
return informat.jdbc.createConnection({
dburl: parseEnv.dburl, // Development environment database address
dbuser: parseEnv.dbuser, // Development environment database username
dbpassword: parseEnv.dbpassword, // Development environment database password
driverClassName: parseEnv.driverClassName, // Development environment database driver name
autoCommit: true,
});
}Using Environment Variables in Expressions
In the expression editor, enter Context and select the Context.appEnvProp(propKey) function
Effect
| Environment | ID | Value |
|---|---|---|
| DEV | url | {"url":"https://www.informat.cn/"} |
*Note 1: Click the Encrypt button to encrypt the value. The encrypted value will be automatically decrypted when used. Click the Decrypt button to decrypt an encrypted value; unencrypted values cannot be decrypted. You can encrypt and decrypt values multiple times, but only the outermost layer will be automatically decrypted when used. Clicking the Encrypt/Decrypt button will automatically check/uncheck Enable Encrypted Storage*
*Note 2: If the environment variable needs to be used in an expression editor executed on the client side, you need to check Allow Client Access*

