@@ -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" );
@@ -250,22 +252,26 @@ UserSession item = new UserSession()
250
252
ItemResponse < UserSession > createResponse = await container .CreateItemAsync (item );
251
253
```
252
254
253
- ##### [ Java SDK v4] ( #tab/java-v4 )
255
+ ##### [ Javascript SDK v4] ( #tab/javascript-v4 )
256
+
257
+ ``` javascript
258
+ // Create a new item
259
+ const item: UserSession = {
260
+ Id: ' f7da01b0-090b-41d2-8416-dacae09fbb4a' ,
261
+ TenantId: ' Microsoft' ,
262
+ UserId: ' 8411f20f-be3e-416a-a3e7-dcd5a3c1f28b' ,
263
+ SessionId: ' 0000-11-0000-1111'
264
+ }
254
265
255
- ``` java
256
- // Create a new item
257
- UserSession item = new UserSession ();
258
- item. setId(" f7da01b0-090b-41d2-8416-dacae09fbb4a" );
259
- item. setTenantId(" Microsoft" );
260
- item. setUserId(" 8411f20f-be3e-416a-a3e7-dcd5a3c1f28b" );
261
- item. setSessionId(" 0000-11-0000-1111" );
262
-
263
266
// Pass in the object, and the SDK automatically extracts the full partition key path
264
- Mono<CosmosItemResponse<UserSession > > createResponse = container. createItem(item);
267
+ const { resource: document } = await = container .items .create (item);
268
+
265
269
```
266
270
267
271
---
268
272
273
+ // Pass in the object, and the SDK automatically extracts the full partition key path
274
+ const { resource: document } = await = container.items.create(it
269
275
#### Manually specify the path
270
276
271
277
The ` PartitionKeyBuilder ` class in the SDK can construct a value for a previously defined hierarchical partition key path. Use this class when you add a new item to a container that has subpartitioning enabled.
@@ -319,6 +325,28 @@ Mono<CosmosItemResponse<UserSession>> createResponse = container.createItem(item
319
325
320
326
---
321
327
328
+ ##### [ Javascript SDK v4] ( #tab/javascript-v4 )
329
+
330
+ ``` javascript
331
+ const item: UserSession = {
332
+ Id: ' f7da01b0-090b-41d2-8416-dacae09fbb4a' ,
333
+ TenantId: ' Microsoft' ,
334
+ UserId: ' 8411f20f-be3e-416a-a3e7-dcd5a3c1f28b' ,
335
+ SessionId: ' 0000-11-0000-1111'
336
+ }
337
+
338
+ // Specify the full partition key path when creating the item
339
+ const partitionKey: PartitionKey = new PartitionKeyBuilder ()
340
+ .addValue (item .TenantId )
341
+ .addValue (item .UserId )
342
+ .addValue (item .SessionId )
343
+ .build ();
344
+
345
+ // Create the item in the container
346
+ const { resource: document } = await container .items .create (item, partitionKey);
347
+
348
+ -- -
349
+
322
350
### Perform a key/ value lookup (point read) of an item
323
351
324
352
Key/ value lookups (point reads) are performed in a way that' s similar to a non-subpartitioned container. For example, assume you have a hierarchical partition key that consists of `TenantId` > `UserId` > `SessionId`. The unique identifier for the item is a GUID. It' s represented as a string that serves as a unique document transaction identifier . To perform a point read on a single item, pass in the ` id` property of the item and the full value for the partition key, including all three components of the path.
@@ -359,26 +387,22 @@ PartitionKey partitionKey = new PartitionKeyBuilder()
359
387
// Perform a point read
360
388
Mono<CosmosItemResponse<UserSession>> readResponse = container.readItem(id, partitionKey, UserSession.class);
361
389
` ` `
362
-
363
- ---
364
-
365
- ##### [ JavaScript SDK v4] ( #tab/javascript-v4 )
390
+ ##### [Javascript SDK v4](#tab/ javascript- v4)
366
391
367
392
` ` ` javascript
368
393
// Store the unique identifier
369
- String id = " f7da01b0-090b-41d2-8416-dacae09fbb4a" ;
394
+ const id = "f7da01b0-090b-41d2-8416-dacae09fbb4a";
370
395
371
396
// 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
397
+ const partitionKey: PartitionKey = new PartitionKeyBuilder()
398
+ .addValue(item. TenantId)
399
+ .addValue(item. UserId)
400
+ .addValue(item. SessionId)
376
401
.build();
377
-
402
+
378
403
// Perform a point read
379
- Mono < CosmosItemResponse < UserSession >> readResponse = container .readItem (id, partitionKey, UserSession . class );
404
+ const { resource: document } = await container.item (id, partitionKey).read( );
380
405
` ` `
381
-
382
406
-- -
383
407
384
408
### Run a query
@@ -446,6 +470,20 @@ pagedResponse.byPage().flatMap(fluxResponse -> {
446
470
return Flux.empty();
447
471
}).blockLast();
448
472
```
473
+ ##### [Javascript SDK v4](#tab/javascript-v4)
474
+
475
+ ```javascript
476
+ // Define a single-partition query that specifies the full partition key path
477
+ 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 ' ";
478
+
479
+ // Retrieve an iterator for the result set
480
+ const queryIterator = container.items.query(query);
481
+
482
+ while (queryIterator.hasMoreResults()) {
483
+ const { resources: results } = await queryIterator.fetchNext();
484
+ // Process result
485
+ }
486
+ ```
449
487
450
488
---
451
489
@@ -495,6 +533,20 @@ pagedResponse.byPage().flatMap(fluxResponse -> {
495
533
}).blockLast();
496
534
` ` `
497
535
536
+ ##### [Javascript SDK v4](#tab/ javascript- v4)
537
+
538
+ ` ` ` javascript
539
+ // Define a targeted cross-partition query specifying prefix path[s]
540
+ const query: string = "SELECT * FROM c WHERE c.TenantId = 'Microsoft'";
541
+
542
+ // Retrieve an iterator for the result set
543
+ const queryIterator = container.items.query(query);
544
+
545
+ while (queryIterator.hasMoreResults()) {
546
+ const { resources: results } = await queryIterator.fetchNext();
547
+ // Process result
548
+ }
549
+ ` ` `
498
550
-- -
499
551
500
552
## Limitations and known issues
0 commit comments