@@ -103,80 +103,79 @@ async function run() {
103
103
run().catch(console.error);
104
104
```
105
105
106
- ### Sample 2: Load key-values and trim prefix from keys
106
+ ### Sample 2: Load specific key-values using selectors
107
107
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 .
108
+ In this sample, you load a subset of key-values by specifying the ` selectors ` option .
109
+ Only keys starting with "app." are loaded .
110
+ Note that you can specify multiple selectors based on your needs, each with ` keyFilter ` and ` labelFilter ` properties .
111
111
112
112
``` javascript
113
113
const { load } = require (" @azure/app-configuration-provider" );
114
114
const connectionString = process .env .AZURE_APPCONFIG_CONNECTION_STRING ;
115
115
116
116
async function run () {
117
- console .log (" Sample 2 : Load key-values and trim prefix from keys " );
117
+ console .log (" Sample 3 : Load specific key-values using selectors " );
118
118
119
- // Load all key-values with no label, and trim "app." prefix from all keys .
119
+ // Load a subset of keys starting with "app." prefix.
120
120
const settings = await load (connectionString, {
121
- trimKeyPrefixes: [" app." ]
121
+ selectors: [{
122
+ keyFilter: " app.*"
123
+ }],
122
124
});
123
125
124
126
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' }
127
+ // The key "message" is not loaded as it does not start with "app."
128
+ console .log (' settings.has ("message"):' , settings .has (" message" )); // settings.has ("message"): false
129
+ // The key "app.greeting" is loaded
130
+ console .log (' settings.has("app. greeting"):' , settings .has ( " app. greeting" )); // settings.has("app. greeting"): true
131
+ // The key "app.json" is loaded
132
+ console .log (' settings.has("app. json"):' , settings .has ( " app. json" )); // settings.has("app. json"): true
131
133
132
134
console .log (" ---Consume configuration as an object---" );
133
- // Construct configuration object from loaded key-values with trimmed keys.
134
- const config = settings .constructConfigurationObject ();
135
+ // Construct configuration object from loaded key-values
136
+ const config = settings .constructConfigurationObject ({ separator : " . " } );
135
137
// 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' }
138
+ console .log (" config.message:" , config .message ); // config.message: undefined
139
+ console .log (" config.app. greeting:" , config .greeting ); // config.app .greeting: Hello World
140
+ console .log (" config.app. json:" , config .json ); // config.app .json: { myKey: 'myValue' }
139
141
}
140
142
141
143
run ().catch (console .error );
142
144
```
143
145
144
- ### Sample 3: Load specific key-values using selectors
145
-
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.
146
+ ### Sample 3: Load key-values and trim prefix from keys
149
147
148
+ In this sample, you load key-values with an option ` trimKeyPrefixes ` .
149
+ After key-values are loaded, the prefix "app." is trimmed from all keys.
150
+ This is useful when you want to load configurations that are specific to your application by filtering to a certain key prefix, but you don't want your code to carry the prefix every time it accesses the configuration.
150
151
151
152
``` javascript
152
153
const { load } = require (" @azure/app-configuration-provider" );
153
154
const connectionString = process .env .AZURE_APPCONFIG_CONNECTION_STRING ;
154
155
155
156
async function run () {
156
- console .log (" Sample 3 : Load specific key-values using selectors " );
157
+ console .log (" Sample 2 : Load key-values and trim prefix from keys " );
157
158
158
- // Load a subset of keys starting with "app." prefix.
159
+ // Load all key-values with no label, and trim "app." prefix from all keys .
159
160
const settings = await load (connectionString, {
160
161
selectors: [{
161
162
keyFilter: " app.*"
162
163
}],
164
+ trimKeyPrefixes: [" app." ]
163
165
});
164
166
165
167
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
168
+ // The original key "app.greeting" is trimmed as "greeting".
169
+ console .log (' settings.get("greeting"):' , settings .get (" greeting" )); // settings.get("greeting"): Hello World
170
+ // The original key "app.json" is trimmed as "json".
171
+ console .log (' settings.get("json"):' , settings .get (" json" )); // settings.get("json"): { myKey: 'myValue' }
172
172
173
173
console .log (" ---Consume configuration as an object---" );
174
- // Construct configuration object from loaded key-values
175
- const config = settings .constructConfigurationObject ({ separator : " . " } );
174
+ // Construct configuration object from loaded key-values with trimmed keys.
175
+ const config = settings .constructConfigurationObject ();
176
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' }
177
+ console .log (" config.greeting:" , config .greeting ); // config.greeting: Hello World
178
+ console .log (" config.json:" , config .json ); // config.json: { myKey: 'myValue' }
180
179
}
181
180
182
181
run ().catch (console .error );
@@ -280,20 +279,6 @@ run().catch(console.error);
280
279
281
280
**Sample 2**
282
281
283
- ```Output
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---
290
- config.message: Message from Azure App Configuration
291
- config.greeting: Hello World
292
- config.json: { myKey: 'myValue' }
293
- ```
294
-
295
- **Sample 3**
296
-
297
282
```Output
298
283
Sample 3: Load specific key-values using selectors
299
284
---Consume configuration as a Map---
@@ -306,6 +291,18 @@ run().catch(console.error);
306
291
config.app.json: { myKey: 'myValue' }
307
292
```
308
293
294
+ **Sample 3**
295
+
296
+ ```Output
297
+ Sample 2: Load key-values and trim prefix from keys
298
+ ---Consume configuration as a Map---
299
+ settings.get("greeting"): Hello World
300
+ settings.get("json"): { myKey: 'myValue' }
301
+ ---Consume configuration as an object---
302
+ config.greeting: Hello World
303
+ config.json: { myKey: 'myValue' }
304
+ ```
305
+
309
306
## Clean up resources
310
307
311
308
[!INCLUDE [azure-app-configuration-cleanup](../../includes/azure-app-configuration-cleanup.md)]
0 commit comments