Skip to content

Commit f0a9d97

Browse files
committed
edits
1 parent 2360fd0 commit f0a9d97

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

articles/cosmos-db/nosql/how-to-javascript-create-container.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,41 @@ const { statusCode, container} = await database.containers.create({ id: containe
5555

5656
The statusCode is an HTTP response code. A successful response is in the 200-299 range.
5757

58+
## Access a container
59+
60+
A container is accessed from the [Container](/javascript/api/@azure/cosmos/container) object either directly or chained from the [CosmosClient](/javascript/api/@azure/cosmos/cosmosclient) or [Database](/javascript/api/@azure/cosmos/database) objects.
61+
62+
```javascript
63+
const databaseName = 'myDb';
64+
const containerName = 'myContainer';
65+
66+
// Chained - assumes database and container already exis
67+
const { container, statusCode } = await client.database(databaseName).container(containerName);
68+
69+
// Direct - assumes database and container already exist
70+
const { database, statusCode } = await database(databaseName);
71+
if(statusCode < 400){
72+
const { container, statusCode } = await database.container(containerName);
73+
}
74+
75+
// Query - assumes database and container already exist
76+
const { resources } = await client.database(databaseName).containers
77+
.query({
78+
query: `SELECT * FROM root r where r.id =@containerId`,
79+
parameters: [
80+
{
81+
name: '@containerId',
82+
value: containerName
83+
}
84+
]
85+
})
86+
.fetchAll();
87+
```
88+
89+
> [!TIP]
90+
> [Containers](/javascript/api/@azure/cosmos/containerss) (plural): create or query containers
91+
> [Container](/javascript/api/@azure/cosmos/container) (singular): delete container, work with items
92+
5893
## Delete a container
5994

6095
Once you get the [Container](/javascript/api/@azure/cosmos/container) object, you can use the Container object to [delete](/javascript/api/@azure/cosmos/container#@azure-cosmos-container-delete) the container:

articles/cosmos-db/nosql/how-to-javascript-create-database.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,34 @@ const {statusCode, database } = await client.databases.create({ id: databaseName
5555

5656
The statusCode is an HTTP response code. A successful response is in the 200-299 range.
5757

58+
## Access a database
59+
60+
A database is accessed from the [Database](/javascript/api/@azure/cosmos/database) object either directly or through a query result from the [CosmosClient](/javascript/api/@azure/cosmos/cosmosclient).
61+
62+
```javascript
63+
const databaseName = 'myDb';
64+
65+
// Direct - assumes database already exists
66+
const { database, statusCode } = await client.database(databaseName);
67+
68+
// Query - assumes database already exists
69+
const { resources } = await client.databases
70+
.query({
71+
query: `SELECT * FROM root r where r.id =@dbId`,
72+
parameters: [
73+
{
74+
name: '@dbId',
75+
value: databaseName
76+
}
77+
]
78+
})
79+
.fetchAll();
80+
```
81+
82+
> [!TIP]
83+
> [Databases](/javascript/api/@azure/cosmos/databases) (plural): Used for creating new databases, or querying/reading all databases.
84+
> [Database](/javascript/api/@azure/cosmos/database) (singular): Used for reading, updating, or deleting a existing database by id or accessing containers belonging to that database.
85+
5886
## Delete a database
5987

6088
Once you get the [Database](/javascript/api/@azure/cosmos/database) object, you can use the Database object to [delete](/javascript/api/@azure/cosmos/database#@azure-cosmos-database-delete) the database:

articles/cosmos-db/nosql/how-to-javascript-create-item.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ const { statusCode, item, resource, activityId, etag} = await container.items.cr
7070
});
7171
```
7272

73+
## Access an item
74+
75+
Access an item through the [Item](/javascript/api/@azure/cosmos/item) object. This can accessed from the [Container](/javascript/api/@azure/cosmos/container) object or changed from either the [Database](/javascript/api/@azure/cosmos/database) or [CosmosClient](javascript/api/@azure/cosmos/cosmosclient) objects.
76+
77+
```javascript
78+
// Chained, then use a method of the Item object such as `read`
79+
const { statusCode, item, resource, activityId, etag} = await client.database(databaseId).container(containerId).item(itemId).read();
80+
```
81+
82+
> [!TIP]
83+
> [Items](/javascript/api/@azure/cosmos/items) (plural): Create, batch, watch change feed, read all, upsert, or query items.
84+
> [Item](/javascript/api/@azure/cosmos/item) (singular): Read, patch, replace, or delete an item.
85+
7386
## Replace an item
7487

7588
Replace the data with the [Item](/javascript/api/@azure/cosmos/item) object with the [replace](/javascript/api/@azure/cosmos/item#@azure-cosmos-item-replace) method.

0 commit comments

Comments
 (0)