CosmosDB
Info
This module uses the Linux-based version of the CosmosDB emulator. In general, it:
- Provides better compatibility on a variety of systems.
- Consumes significantly less resources.
- Comes with much faster startup times.
However, not all features of a full CosmosDB are implemented yet. Refer to this overview for a detailed list.
Install
| npm install @testcontainers/azurecosmosdb --save-dev
|
Examples
These examples use the following libraries:
Choose an image from Microsoft Artifact Registry and substitute IMAGE
. For example, mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:vnext-preview
.
Execute a query
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 | const dbName = "testdb";
const containerName = "testcontainer";
await using container = await new AzureCosmosDbEmulatorContainer(IMAGE).withProtocol("http").start();
const cosmosClient = new CosmosClient({
endpoint: container.getEndpoint(),
key: container.getKey(),
});
await cosmosClient.databases.createIfNotExists({ id: dbName });
const dbClient = cosmosClient.database(dbName);
await dbClient.containers.createIfNotExists({
id: containerName,
partitionKey: {
kind: PartitionKeyKind.Hash,
paths: ["/foo"],
},
});
const containerClient = dbClient.container(containerName);
const createResponse = await containerClient.items.create({ foo: "bar" });
const readItem = await containerClient.item(createResponse.item.id, "bar").read();
expect(readItem.resource.foo).toEqual("bar");
|
With HTTPS
| await using container = await new AzureCosmosDbEmulatorContainer(IMAGE).withProtocol("https").start();
const cosmosClient = new CosmosClient({
endpoint: container.getEndpoint(),
key: container.getKey(),
agent: new https.Agent({
rejectUnauthorized: false,
}),
});
|