@@ -134,7 +134,9 @@ Container container = await database.CreateContainerIfNotExistsAsync(containerPr
134
134
135
135
#### [ Java SDK v4] ( #tab/java-v4 )
136
136
137
- ``` java
137
+ #### [ JavaScript SDK v4] ( #tab/javascript-v4 )
138
+
139
+ ``` javascript
138
140
// List of partition keys, in hierarchical order. You can have up to three levels of keys.
139
141
List< String > subpartitionKeyPaths = new ArrayList < String > ();
140
142
subpartitionKeyPaths .add (" /TenantId" );
@@ -264,6 +266,21 @@ item.setSessionId("0000-11-0000-1111");
264
266
Mono<CosmosItemResponse<UserSession > > createResponse = container. createItem(item);
265
267
```
266
268
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
+ ```
267
284
---
268
285
269
286
#### Manually specify the path
@@ -317,6 +334,26 @@ PartitionKey partitionKey = new PartitionKeyBuilder()
317
334
Mono<CosmosItemResponse<UserSession > > createResponse = container. createItem(item, partitionKey);
318
335
```
319
336
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
+ ```
320
357
---
321
358
322
359
### Perform a key/value lookup (point read) of an item
@@ -359,26 +396,22 @@ PartitionKey partitionKey = new PartitionKeyBuilder()
359
396
// Perform a point read
360
397
Mono<CosmosItemResponse<UserSession > > readResponse = container. readItem(id, partitionKey, UserSession . class);
361
398
```
362
-
363
- ---
364
-
365
- ##### [ JavaScript SDK v4] ( #tab/javascript-v4 )
399
+ ##### [ Javascript SDK v4] ( #tab/javascript-v4 )
366
400
367
401
``` javascript
368
402
// Store the unique identifier
369
- String id = " f7da01b0-090b-41d2-8416-dacae09fbb4a" ;
403
+ const id = " f7da01b0-090b-41d2-8416-dacae09fbb4a" ;
370
404
371
405
// 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 )
376
410
.build ();
377
-
411
+
378
412
// 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 ( );
380
414
```
381
-
382
415
---
383
416
384
417
### Run a query
@@ -446,6 +479,20 @@ pagedResponse.byPage().flatMap(fluxResponse -> {
446
479
return Flux . empty();
447
480
}). blockLast();
448
481
```
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
+ ```
449
496
450
497
---
451
498
@@ -495,6 +542,20 @@ pagedResponse.byPage().flatMap(fluxResponse -> {
495
542
}). blockLast();
496
543
```
497
544
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
+ ```
498
559
---
499
560
500
561
## Limitations and known issues
0 commit comments