@@ -65,79 +65,122 @@ If your application requires configuration to be consumed as an object, you can
65
65
This API constructs a configuration object based on the key-values loaded from Azure App Configuration.
66
66
It minimizes the code changes required to integrate Azure App Configuration into your application.
67
67
68
- 1. Create a new file called *app.js* in the *app-configuration-quickstart* directory and add the following code:
68
+ The following samples show how to load configuration and consume it as either a `Map` or an object.
69
+ Create a new file called *app.js* in the *app-configuration-quickstart* directory and copy the code from each sample.
69
70
70
- ### [Use configuration as a Map](#tab/config-as-map)
71
+ ### Sample 1: Load key-values with default selector
71
72
72
- ```javascript
73
- const { load } = require("@azure/app-configuration-provider");
74
- const connectionString = process.env.AZURE_APPCONFIG_CONNECTION_STRING;
73
+ In this sample, you connect to Azure App Configuration using a connection string and load key-values without specifying advanced options.
74
+ By default, it loads all key-values with no label.
75
75
76
- async function run() {
77
- let settings;
76
+ ```javascript
77
+ const { load } = require("@azure/app-configuration-provider");
78
+ const connectionString = process.env.AZURE_APPCONFIG_CONNECTION_STRING;
78
79
79
- // Sample 1: Connect to Azure App Configuration using a connection string and load all key-values with null label.
80
- settings = await load(connectionString );
80
+ async function run() {
81
+ console.log("Sample 1: Load key-values with default selector" );
81
82
82
- // Find the key "message" and print its value .
83
- console.log(settings.get("message")); // Output: Message from Azure App Configuration
83
+ // Connect to Azure App Configuration using a connection string and load all key-values with null label .
84
+ const settings = await load(connectionString);
84
85
85
- // Find the key "app.json" as an object, and print its property "myKey".
86
- const jsonObject = settings.get("app.json");
87
- console.log(jsonObject.myKey); // Output: myValue
86
+ console.log("---Consume configuration as a Map---");
87
+ // Find the key "message" and print its value.
88
+ console.log('settings.get("message"):', settings.get("message")); // settings.get("message"): Message from Azure App Configuration
89
+ // Find the key "app.greeting" and print its value.
90
+ console.log('settings.get("app.greeting"):', settings.get("app.greeting")); // settings.get("app.greeting"): Hello World
91
+ // Find the key "app.json" whose value is an object.
92
+ console.log('settings.get("app.json"):', settings.get("app.json")); // settings.get("app.json"): { myKey: 'myValue' }
88
93
89
- // Sample 2: Load all key-values with null label and trim "app." prefix from all keys.
90
- settings = await load(connectionString, {
91
- trimKeyPrefixes: ["app."]
92
- });
94
+ console.log("---Consume configuration as an object---");
95
+ // Construct configuration object from loaded key-values, by default "." is used to separate hierarchical keys.
96
+ const config = settings.constructConfigurationObject();
97
+ // Use dot-notation to access configuration
98
+ console.log("config.message:", config.message); // config.message: Message from Azure App Configuration
99
+ console.log("config.app.greeting:", config.app.greeting); // config.app.greeting: Hello World
100
+ console.log("config.app.json:", config.app.json); // config.app.json: { myKey: 'myValue' }
101
+ }
93
102
94
- // From the keys with trimmed prefixes, find a key with "greeting" and print its value.
95
- console.log(settings.get("greeting")); // Output: Hello World
103
+ run().catch(console.error);
104
+ ```
96
105
97
- // Sample 3: Load all keys starting with "app." prefix and null label.
98
- settings = await load(connectionString, {
99
- selectors: [{
100
- keyFilter: "app.*"
101
- }],
102
- });
106
+ ### Sample 2: Load key-values and trim prefix from keys
103
107
104
- // Print true or false indicating whether a setting is loaded.
105
- console.log(settings.has("message")); // Output: false
106
- console.log(settings.has("app.greeting")); // Output: true
107
- console.log(settings.has("app.json")); // Output: true
108
- }
108
+ In this sample, you load key-values with an option ` trimKeyPrefixes ` .
109
+ After key-values are loaded, the prefix "app." is trimmed from all keys.
110
+ This can be convenient especially when the keys share a long prefix.
109
111
110
- run().catch(console.error);
111
- ```
112
+ ``` javascript
113
+ const { load } = require (" @azure/app-configuration-provider" );
114
+ const connectionString = process .env .AZURE_APPCONFIG_CONNECTION_STRING ;
112
115
113
- ### [Use configuration as an object](#tab/config-as-object)
116
+ async function run () {
117
+ console .log (" Sample 2: Load key-values and trim prefix from keys" );
114
118
115
- ```javascript
116
- const { load } = require("@azure/app-configuration-provider");
117
- const connectionString = process.env.AZURE_APPCONFIG_CONNECTION_STRING;
119
+ // Load all key-values with no label, and trim "app." prefix from all keys.
120
+ const settings = await load (connectionString, {
121
+ trimKeyPrefixes: [" app." ]
122
+ });
123
+
124
+ console .log (" ---Consume configuration as a Map---" );
125
+ // Find the key "message" and print its value. The key is not trimmed as it does not start with "app."
126
+ console .log (' settings.get("message"):' , settings .get (" message" )); // settings.get("message"): Message from Azure App Configuration
127
+ // The original key "app.greeting" is trimmed as "greeting".
128
+ console .log (' settings.get("greeting"):' , settings .get (" greeting" )); // settings.get("greeting"): Hello World
129
+ // The original key "app.json" is trimmed as "json".
130
+ console .log (' settings.get("json"):' , settings .get (" json" )); // settings.get("json"): { myKey: 'myValue' }
131
+
132
+ console .log (" ---Consume configuration as an object---" );
133
+ // Construct configuration object from loaded key-values with trimmed keys.
134
+ const config = settings .constructConfigurationObject ();
135
+ // Use dot-notation to access configuration
136
+ console .log (" config.message:" , config .message ); // config.message: Message from Azure App Configuration
137
+ console .log (" config.greeting:" , config .greeting ); // config.greeting: Hello World
138
+ console .log (" config.json:" , config .json ); // config.json: { myKey: 'myValue' }
139
+ }
118
140
119
- async function run() {
120
- // Load all key-values from Azure App Configuration
121
- const settings = await load(connectionString);
122
-
123
- // Construct configuration object from loaded key-values
124
- const config = settings.constructConfigurationObject({
125
- separator: "."
126
- });
141
+ run ().catch (console .error );
142
+ ```
127
143
128
- // Print out the configuration object
129
- console.log("Constructed configuration object 'config': ");
130
- console.log(config);
144
+ ### Sample 3: Load specific key-values using selectors
131
145
132
- // Use dot-notation to access configuration
133
- console.log(`config.message:\t${config.message}`);
134
- console.log(`config.app.greeting:\t${config.app.greeting}`);
135
- console.log(`config.app.json.myKey:\t${config.app.json.myKey}`);
136
- }
146
+ In this sample, you load a subset of key-values by specifying the ` selectors ` option.
147
+ Only keys starting with "app." are loaded.
148
+ Note that you can specify multiple selectors based on your needs, each with ` keyFilter ` and ` labelFilter ` properties.
137
149
138
- run().catch(console.error);
139
- ```
140
150
151
+ ``` javascript
152
+ const { load } = require (" @azure/app-configuration-provider" );
153
+ const connectionString = process .env .AZURE_APPCONFIG_CONNECTION_STRING ;
154
+
155
+ async function run () {
156
+ console .log (" Sample 3: Load specific key-values using selectors" );
157
+
158
+ // Load a subset of keys starting with "app." prefix.
159
+ const settings = await load (connectionString, {
160
+ selectors: [{
161
+ keyFilter: " app.*"
162
+ }],
163
+ });
164
+
165
+ console .log (" ---Consume configuration as a Map---" );
166
+ // The key "message" is not loaded as it does not start with "app."
167
+ console .log (' settings.has("message"):' , settings .has (" message" )); // settings.has("message"): false
168
+ // The key "app.greeting" is loaded
169
+ console .log (' settings.has("app.greeting"):' , settings .has (" app.greeting" )); // settings.has("app.greeting"): true
170
+ // The key "app.json" is loaded
171
+ console .log (' settings.has("app.json"):' , settings .has (" app.json" )); // settings.has("app.json"): true
172
+
173
+ console .log (" ---Consume configuration as an object---" );
174
+ // Construct configuration object from loaded key-values
175
+ const config = settings .constructConfigurationObject ({ separator: " ." });
176
+ // Use dot-notation to access configuration
177
+ console .log (" config.message:" , config .message ); // config.message: undefined
178
+ console .log (" config.app.greeting:" , config .greeting ); // config.app.greeting: Hello World
179
+ console .log (" config.app.json:" , config .json ); // config.app.json: { myKey: 'myValue' }
180
+ }
181
+
182
+ run ().catch (console .error );
183
+ ```
141
184
142
185
## Run the application
143
186
@@ -175,6 +218,8 @@ It minimizes the code changes required to integrate Azure App Configuration into
175
218
export AZURE_APPCONFIG_CONNECTION_STRING='<app-configuration-store-connection-string>'
176
219
```
177
220
221
+ ---
222
+
178
223
1. Print the value of the environment variable to validate that it's set properly with the following command.
179
224
180
225
### [Windows command prompt](#tab/windowscommandprompt)
@@ -209,36 +254,56 @@ It minimizes the code changes required to integrate Azure App Configuration into
209
254
echo "$AZURE_APPCONFIG_CONNECTION_STRING"
210
255
```
211
256
257
+ ---
258
+
212
259
1. After the environment variable is properly set, run the following command to run the app locally:
213
260
214
261
```bash
215
262
node app.js
216
263
```
217
264
218
- You should see the following output:
265
+ You should see the following output for each sample :
219
266
220
- ### [Use configuration as a Map](#tab/config-as-map)
267
+ **Sample 1**
221
268
222
269
```Output
223
- Message from Azure App Configuration
224
- myValue
225
- Hello World
226
- false
227
- true
228
- true
270
+ Sample 1: Load key-values with default selector
271
+ ---Consume configuration as a Map---
272
+ settings.get("message"): Message from Azure App Configuration
273
+ settings.get("app.greeting"): Hello World
274
+ settings.get("app.json"): { myKey: 'myValue' }
275
+ ---Consume configuration as an object---
276
+ config.message: Message from Azure App Configuration
277
+ config.app.greeting: Hello World
278
+ config.app.json: { myKey: 'myValue' }
229
279
```
230
280
231
- ### [Use configuration as an object](#tab/config-as-object)
281
+ **Sample 2**
232
282
233
283
```Output
234
- Constructed configuration object 'config':
235
- {
236
- app: { greeting: 'Hello World', json: { myKey: 'myValue' } },
237
- message: 'Message from Azure App Configuration'
238
- }
284
+ Sample 2: Load key-values and trim prefix from keys
285
+ ---Consume configuration as a Map---
286
+ settings.get("message"): Message from Azure App Configuration
287
+ settings.get("greeting"): Hello World
288
+ settings.get("json"): { myKey: 'myValue' }
289
+ ---Consume configuration as an object---
239
290
config.message: Message from Azure App Configuration
240
- config.app.greeting: Hello World
241
- config.app.json.myKey: myValue
291
+ config.greeting: Hello World
292
+ config.json: { myKey: 'myValue' }
293
+ ```
294
+
295
+ **Sample 3**
296
+
297
+ ```Output
298
+ Sample 3: Load specific key-values using selectors
299
+ ---Consume configuration as a Map---
300
+ settings.has("message"): false
301
+ settings.has("app.greeting"): true
302
+ settings.has("app.json"): true
303
+ ---Consume configuration as an object---
304
+ config.message: undefined
305
+ config.app.greeting: Hello World
306
+ config.app.json: { myKey: 'myValue' }
242
307
```
243
308
244
309
## Clean up resources
0 commit comments