Skip to content

Commit cd5552a

Browse files
authored
Add example for configuration object usage (#53)
1 parent 87b0dbc commit cd5552a

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

examples/configObject.mjs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import * as dotenv from "dotenv";
5+
dotenv.config()
6+
7+
/**
8+
* This example demonstrates how to construct a configuration object from settings loaded from Azure App Configuration.
9+
* 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.
10+
*
11+
* When you import configuration into Azure App Configuration from a local .json file, the keys are automatically flattened with a separator if specified.
12+
* E.g. if you import the following .json file, specifying the separator as ".":
13+
* {
14+
* "app": {
15+
* "settings": {
16+
* "message": "Hello, Azure!"
17+
* }
18+
* }
19+
* }
20+
*
21+
* In the configuration explorer, the key-values will be:
22+
* - Key: "app.settings.message", Value: "Hello, Azure!"
23+
*
24+
* With the API `constructConfigurationObject`, you can construct a configuration object with the same shape as the original .json file.
25+
* The separator is used to split the keys and construct the object.
26+
* The constructed object will be: { app: { settings: { message: "Hello, Azure!" } } }
27+
*
28+
* Below environment variables are required for this example:
29+
* - APPCONFIG_CONNECTION_STRING
30+
*/
31+
32+
import { load } from "@azure/app-configuration-provider";
33+
const connectionString = process.env.APPCONFIG_CONNECTION_STRING;
34+
const settings = await load(connectionString, {
35+
selectors: [{
36+
keyFilter: "app.settings.*"
37+
}]
38+
});
39+
40+
const config = settings.constructConfigurationObject({
41+
separator: "."
42+
});
43+
44+
console.log("Constructed object 'config': ", config);
45+
console.log(`Message from Azure App Configuration: ${config.app.settings.message}`);

0 commit comments

Comments
 (0)