Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions examples/configObject.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as dotenv from "dotenv";
dotenv.config()

/**
* This example demonstrates how to construct a configuration object from settings loaded from Azure App Configuration.
* If you are using configuration object instead of Map-styled settings, it would minimize the code changes required to use Azure App Configuration in your application.
*
* When you import configuration into Azure App Configuration from a local .json file, the keys are automatically flattened with a separator if specified.
* E.g. if you import the following .json file, specifying the separator as ".":
* {
* "app": {
* "settings": {
* "message": "Hello, Azure!"
* }
* }
* }
*
* In the configuration explorer, the key-values will be:
* - Key: "app.settings.message", Value: "Hello, Azure!"
*
* With the API `constructConfigurationObject`, you can construct a configuration object with the same shape as the original .json file.
* The separator is used to split the keys and construct the object.
* The constructed object will be: { app: { settings: { message: "Hello, Azure!" } } }
*
* Below environment variables are required for this example:
* - APPCONFIG_CONNECTION_STRING
*/

import { load } from "@azure/app-configuration-provider";
const connectionString = process.env.APPCONFIG_CONNECTION_STRING;
const settings = await load(connectionString, {
selectors: [{
keyFilter: "app.settings.*"
}]
});

const config = settings.constructConfigurationObject({
separator: "."
});

console.log("Constructed object 'config': ", config);
console.log(`Message from Azure App Configuration: ${config.app.settings.message}`);