Skip to content

Commit fe431d7

Browse files
committed
add docs
1 parent 0e6f470 commit fe431d7

4 files changed

Lines changed: 76 additions & 3 deletions

File tree

docs/modules/cosmosdb.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Cosmos DB Emulator Module (Linux-based)
2+
3+
[Azure Cosmos DB](https://azure.microsoft.com/en-GB/products/cosmos-db) is a globally distributed, multi-model database service provided by Microsoft.
4+
5+
## Install
6+
7+
```bash
8+
npm install @testcontainers/azurecosmosdb --save-dev
9+
```
10+
11+
## Examples
12+
<!--codeinclude-->
13+
[Connect to emulator and create a database:](../../packages/modules/azurecosmosdb/src/azure-cosmosdb-emulator-container.test.ts) inside_block:httpCreateDB
14+
<!--/codeinclude-->
15+
16+
<!--codeinclude-->
17+
[Using HTTPS:](../../packages/modules/azurecosmosdb/src/azure-cosmosdb-emulator-container.test.ts) inside_block:httpsCreateDB
18+
<!--/codeinclude-->
19+
20+
<!--codeinclude-->
21+
[Create and read items:](../../packages/modules/azurecosmosdb/src/azure-cosmosdb-emulator-container.test.ts) inside_block:createAndRead
22+
<!--/codeinclude-->
23+
24+
## Caveats
25+
### Compatibility
26+
This testcontainer uses the [linux-based](https://learn.microsoft.com/en-us/azure/cosmos-db/emulator-linux) version. In general, it:
27+
28+
- Provides better compatibility on a variety of systems
29+
- Consumes significantly less resources
30+
- Comes with much faster startup times
31+
32+
However, not all features of a full CosmosDB are implemented yet - please refer to [this overview](https://learn.microsoft.com/en-us/azure/cosmos-db/emulator-linux#feature-support) for a detailed list.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ nav:
4646
- Azurite: modules/azurite.md
4747
- Cassandra: modules/cassandra.md
4848
- ChromaDB: modules/chromadb.md
49+
- CosmosDB: modules/cosmosdb.md
4950
- Couchbase: modules/couchbase.md
5051
- CockroachDB: modules/cockroachdb.md
5152
- Elasticsearch: modules/elasticsearch.md

packages/modules/azurecosmosdb/src/azure-cosmosdb-emulator-container.test.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CosmosClient } from "@azure/cosmos";
1+
import { CosmosClient, PartitionKeyKind } from "@azure/cosmos";
22
import * as https from "node:https";
33
import { expect } from "vitest";
44
import { AzureCosmosDbEmulatorContainer } from "./azure-cosmosdb-emulator-container";
@@ -17,6 +17,7 @@ describe("AzureCosmosDbEmulatorContainer", { timeout: 180_000 }, async () => {
1717
await container.stop();
1818
});
1919

20+
// httpCreateDB {
2021
it("should be able to create a database using http", async () => {
2122
const container = await new AzureCosmosDbEmulatorContainer().withProtocol("http").start();
2223
const cosmosClient = new CosmosClient({
@@ -35,13 +36,16 @@ describe("AzureCosmosDbEmulatorContainer", { timeout: 180_000 }, async () => {
3536

3637
await container.stop();
3738
});
39+
// }
40+
41+
// httpsCreateDB {
3842
it("should be able to create a database using https", async () => {
3943
const container = await new AzureCosmosDbEmulatorContainer().withProtocol("https").start();
4044
const cosmosClient = new CosmosClient({
4145
endpoint: container.getEndpoint(),
4246
key: container.getKey(),
4347
agent: new https.Agent({
44-
rejectUnauthorized: false,
48+
rejectUnauthorized: false, //allows insecure TLS; import * as https from "node:https";
4549
}),
4650
});
4751

@@ -56,4 +60,40 @@ describe("AzureCosmosDbEmulatorContainer", { timeout: 180_000 }, async () => {
5660

5761
await container.stop();
5862
});
63+
// }
64+
65+
// createAndRead {
66+
it("should be able to create a container and store and retrieve items", async () => {
67+
const container = await new AzureCosmosDbEmulatorContainer().withProtocol("http").start();
68+
const cosmosClient = new CosmosClient({
69+
endpoint: container.getEndpoint(),
70+
key: container.getKey(),
71+
});
72+
73+
const dbName = "testdb";
74+
await cosmosClient.databases.createIfNotExists({
75+
id: dbName,
76+
});
77+
const dbClient = cosmosClient.database(dbName);
78+
79+
const containerName = "testcontainer";
80+
await dbClient.containers.createIfNotExists({
81+
id: containerName,
82+
partitionKey: {
83+
kind: PartitionKeyKind.Hash,
84+
paths: ["/foo"],
85+
},
86+
});
87+
88+
const containerClient = dbClient.container(containerName);
89+
const createResponse = await containerClient.items.create({
90+
foo: "bar",
91+
});
92+
93+
const readItem = await containerClient.item(createResponse.item.id, "bar").read();
94+
expect(readItem.resource.foo).toEqual("bar");
95+
96+
await container.stop();
97+
});
98+
// }
5999
});

packages/modules/azurecosmosdb/src/azure-cosmosdb-emulator-container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class AzureCosmosDbEmulatorContainer extends GenericContainer {
4747
}
4848

4949
/**
50-
* The mapped port has to be passed to CosmosDB as an environment variable for HTTPS to work.
50+
* The mapped port has to be passed to CosmosDB as an environment variable.
5151
* Since the mapped port would only be known after container creation, we need to find an available port ourselves
5252
* before starting the container.
5353
* @private

0 commit comments

Comments
 (0)