Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docs/modules/couchdb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# CouchDB

## Install

```bash
npm install @testcontainers/couchdb --save-dev
```

## Examples

These examples use the following libraries:

- [nano](https://www.npmjs.com/package/nano)

npm install nano

Choose an image from the [container registry](https://hub.docker.com/_/couchdb) and substitute `IMAGE`.

### Execute a query

<!--codeinclude-->
[](../../packages/modules/couchdb/src/couchdb-container.test.ts) inside_block:startContainer
<!--/codeinclude-->

### With credentials

By default, this module uses `root:root` as credentials.

<!--codeinclude-->
[](../../packages/modules/couchdb/src/couchdb-container.test.ts) inside_block:customCredentials
<!--/codeinclude-->
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ nav:
- CockroachDB: modules/cockroachdb.md
- CosmosDB: modules/cosmosdb.md
- Couchbase: modules/couchbase.md
- CouchDB: modules/couchdb.md
- Elasticsearch: modules/elasticsearch.md
- Etcd: modules/etcd.md
- GCloud: modules/gcloud.md
Expand Down
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/modules/couchdb/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM couchdb:3.5
37 changes: 37 additions & 0 deletions packages/modules/couchdb/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@testcontainers/couchdb",
"version": "11.5.1",
"license": "MIT",
"keywords": [
"couchdb",
"testing",
"docker",
"testcontainers"
],
"description": "CouchDB module for Testcontainers",
"homepage": "https://github.com/testcontainers/testcontainers-node#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/testcontainers/testcontainers-node.git"
},
"bugs": {
"url": "https://github.com/testcontainers/testcontainers-node/issues"
},
"main": "build/index.js",
"files": [
"build"
],
"publishConfig": {
"access": "public"
},
"scripts": {
"prepack": "shx cp ../../../README.md . && shx cp ../../../LICENSE .",
"build": "tsc --project tsconfig.build.json"
},
"dependencies": {
"testcontainers": "^11.5.1"
},
"devDependencies": {
"nano": "^11.0.0"
}
}
62 changes: 62 additions & 0 deletions packages/modules/couchdb/src/couchdb-container.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import nano from "nano";
import { expect } from "vitest";
import { getImage } from "../../../testcontainers/src/utils/test-helper";
import { CouchDBContainer } from "./couchdb-container";

const IMAGE = getImage(__dirname);

type User = {
username: string;
email: string;
};

describe("CouchDBContainer", { timeout: 240_000 }, () => {
it("should write and read a collection", async () => {
// startContainer {
await using container = await new CouchDBContainer(IMAGE).start();

const client = nano({
url: container.getUrl(),
});
await client.db.create("users");
const db = client.use<User>("users");

const document = await db.insert({
username: "j-doe",
email: "[email protected]",
});

expect(await db.get(document.id)).toEqual({
_id: document.id,
_rev: document.rev,
username: "j-doe",
email: "[email protected]",
});
// }
});

it("should use custom credentials", async function () {
// customCredentials {
await using container = await new CouchDBContainer(IMAGE).withUsername("admin").withPassword("foo").start();

const client = nano({
url: container.getUrl(),
});
await client.db.create("users");
const db = client.use<User>("users");

const document = await db.insert({
username: "j-doe",
email: "[email protected]",
});

expect(container.getUrl()).toBe(`http://admin:foo@${container.getHost()}:${container.getPort()}`);
expect(await db.get(document.id)).toEqual({
_id: document.id,
_rev: document.rev,
username: "j-doe",
email: "[email protected]",
});
// }
});
});
59 changes: 59 additions & 0 deletions packages/modules/couchdb/src/couchdb-container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers";

const COUCHDB_PORT = 5984;

export class CouchDBContainer extends GenericContainer {
private username: string = "root";
private password: string = "root";

constructor(image: string) {
super(image);
this.withExposedPorts(COUCHDB_PORT)
.withStartupTimeout(120_000)
.withWaitStrategy(Wait.forHttp("/_up", COUCHDB_PORT).forStatusCode(200).withStartupTimeout(60_000));
}

public withUsername(username: string): this {
this.username = username;
return this;
}

public withPassword(password: string): this {
this.password = password;
return this;
}

public override async start(): Promise<StartedCouchDBContainer> {
this.withEnvironment({
COUCHDB_USER: this.username,
COUCHDB_PASSWORD: this.password,
});
return new StartedCouchDBContainer(await super.start(), this.username, this.password);
}
}

export class StartedCouchDBContainer extends AbstractStartedContainer {
constructor(
startedTestContainer: StartedTestContainer,
private readonly username: string,
private readonly password: string
) {
super(startedTestContainer);
}

public getPort(): number {
return this.getMappedPort(COUCHDB_PORT);
}

public getUsername(): string {
return this.username;
}

public getPassword(): string {
return this.password;
}

public getUrl(): string {
return `http://${this.username}:${this.password}@${this.getHost()}:${this.getPort()}`;
}
}
1 change: 1 addition & 0 deletions packages/modules/couchdb/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { CouchDBContainer, StartedCouchDBContainer } from "./couchdb-container";
12 changes: 12 additions & 0 deletions packages/modules/couchdb/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "./tsconfig.json",
"exclude": [
"build",
"src/**/*.test.ts"
],
"references": [
{
"path": "../../testcontainers"
}
]
}
20 changes: 20 additions & 0 deletions packages/modules/couchdb/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build",
"paths": {
"testcontainers": [
"../../testcontainers/src"
]
}
},
"exclude": [
"build"
],
"references": [
{
"path": "../../testcontainers"
}
]
}