Skip to content

Commit a878187

Browse files
Merge pull request #251246 from sajeetharan/js_hp_fixes
fix samples
2 parents 8753635 + 6cc5dca commit a878187

File tree

2 files changed

+75
-14
lines changed

2 files changed

+75
-14
lines changed

articles/cosmos-db/hierarchical-partition-keys.md

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ Container container = await database.CreateContainerIfNotExistsAsync(containerPr
134134

135135
#### [Java SDK v4](#tab/java-v4)
136136

137-
```java
137+
#### [JavaScript SDK v4](#tab/javascript-v4)
138+
139+
```javascript
138140
// List of partition keys, in hierarchical order. You can have up to three levels of keys.
139141
List<String> subpartitionKeyPaths = new ArrayList<String>();
140142
subpartitionKeyPaths.add("/TenantId");
@@ -264,6 +266,21 @@ item.setSessionId("0000-11-0000-1111");
264266
Mono<CosmosItemResponse<UserSession>> createResponse = container.createItem(item);
265267
```
266268

269+
##### [Javascript SDK v4](#tab/javascript-v4)
270+
271+
```javascript
272+
// Create a new item
273+
const item: UserSession = {
274+
Id: 'f7da01b0-090b-41d2-8416-dacae09fbb4a',
275+
TenantId: 'Microsoft',
276+
UserId: '8411f20f-be3e-416a-a3e7-dcd5a3c1f28b',
277+
SessionId: '0000-11-0000-1111'
278+
}
279+
280+
// Pass in the object, and the SDK automatically extracts the full partition key path
281+
const { resource: document } = await = container.items.create(item);
282+
283+
```
267284
---
268285

269286
#### Manually specify the path
@@ -317,6 +334,26 @@ PartitionKey partitionKey = new PartitionKeyBuilder()
317334
Mono<CosmosItemResponse<UserSession>> createResponse = container.createItem(item, partitionKey);
318335
```
319336

337+
##### [Javascript SDK v4](#tab/javascript-v4)
338+
339+
```javascript
340+
const item: UserSession = {
341+
Id: 'f7da01b0-090b-41d2-8416-dacae09fbb4a',
342+
TenantId: 'Microsoft',
343+
UserId: '8411f20f-be3e-416a-a3e7-dcd5a3c1f28b',
344+
SessionId: '0000-11-0000-1111'
345+
}
346+
347+
// Specify the full partition key path when creating the item
348+
const partitionKey: PartitionKey = new PartitionKeyBuilder()
349+
.addValue(item.TenantId)
350+
.addValue(item.UserId)
351+
.addValue(item.SessionId)
352+
.build();
353+
354+
// Create the item in the container
355+
const { resource: document } = await container.items.create(item, partitionKey);
356+
```
320357
---
321358

322359
### Perform a key/value lookup (point read) of an item
@@ -359,26 +396,22 @@ PartitionKey partitionKey = new PartitionKeyBuilder()
359396
// Perform a point read
360397
Mono<CosmosItemResponse<UserSession>> readResponse = container.readItem(id, partitionKey, UserSession.class);
361398
```
362-
363-
---
364-
365-
##### [JavaScript SDK v4](#tab/javascript-v4)
399+
##### [Javascript SDK v4](#tab/javascript-v4)
366400

367401
```javascript
368402
// Store the unique identifier
369-
String id = "f7da01b0-090b-41d2-8416-dacae09fbb4a";
403+
const id = "f7da01b0-090b-41d2-8416-dacae09fbb4a";
370404

371405
// Build the full partition key path
372-
PartitionKey partitionKey = new PartitionKeyBuilder()
373-
.add("Microsoft") //TenantId
374-
.add("8411f20f-be3e-416a-a3e7-dcd5a3c1f28b") //UserId
375-
.add("0000-11-0000-1111") //SessionId
406+
const partitionKey: PartitionKey = new PartitionKeyBuilder()
407+
.addValue(item.TenantId)
408+
.addValue(item.UserId)
409+
.addValue(item.SessionId)
376410
.build();
377-
411+
378412
// Perform a point read
379-
Mono<CosmosItemResponse<UserSession>> readResponse = container.readItem(id, partitionKey, UserSession.class);
413+
const { resource: document } = await container.item(id, partitionKey).read();
380414
```
381-
382415
---
383416

384417
### Run a query
@@ -446,6 +479,20 @@ pagedResponse.byPage().flatMap(fluxResponse -> {
446479
return Flux.empty();
447480
}).blockLast();
448481
```
482+
##### [Javascript SDK v4](#tab/javascript-v4)
483+
484+
```javascript
485+
// Define a single-partition query that specifies the full partition key path
486+
const query: string = "SELECT * FROM c WHERE c.TenantId = 'Microsoft' AND c.UserId = '8411f20f-be3e-416a-a3e7-dcd5a3c1f28b' AND c.SessionId = '0000-11-0000-1111'";
487+
488+
// Retrieve an iterator for the result set
489+
const queryIterator = container.items.query(query);
490+
491+
while (queryIterator.hasMoreResults()) {
492+
const { resources: results } = await queryIterator.fetchNext();
493+
// Process result
494+
}
495+
```
449496

450497
---
451498

@@ -495,6 +542,20 @@ pagedResponse.byPage().flatMap(fluxResponse -> {
495542
}).blockLast();
496543
```
497544

545+
##### [Javascript SDK v4](#tab/javascript-v4)
546+
547+
```javascript
548+
// Define a targeted cross-partition query specifying prefix path[s]
549+
const query: string = "SELECT * FROM c WHERE c.TenantId = 'Microsoft'";
550+
551+
// Retrieve an iterator for the result set
552+
const queryIterator = container.items.query(query);
553+
554+
while (queryIterator.hasMoreResults()) {
555+
const { resources: results } = await queryIterator.fetchNext();
556+
// Process result
557+
}
558+
```
498559
---
499560

500561
## Limitations and known issues

articles/cosmos-db/nosql/change-feed-pull-model.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ Here's an example of how to obtain the iterator in latest version mode that retu
310310

311311
```js
312312
const options = {
313-
changeFeedStartFrom: ChangeFeedStartFrom.Beginning()
313+
changeFeedStartFrom: ChangeFeedStartFrom.Now()
314314
};
315315

316316
const iterator = container.items.getChangeFeedIterator(options);

0 commit comments

Comments
 (0)