testcontainer_formulas/mongo

Mongo formula. Defaults to mongo:7 with root auth.

use db <- testcontainer.with_formula(
  mongo.new()
  |> mongo.with_database("app_test")
  |> mongo.with_username("app")
  |> mongo.with_password("secret")
  |> mongo.formula(),
)

// db.connection_url, db.host, db.port, db.database, db.username

Types

Mongo-specific configuration. Build with new/0 + with_*.

pub opaque type MongoConfig

Typed output handed to the body of testcontainer.with_formula/2.

pub type MongoContainer {
  MongoContainer(
    container: container.Container,
    connection_url: String,
    host: String,
    port: Int,
    database: String,
    username: String,
  )
}

Constructors

  • MongoContainer(
      container: container.Container,
      connection_url: String,
      host: String,
      port: Int,
      database: String,
      username: String,
    )

Values

pub fn formula(c: MongoConfig) -> formula.Formula(MongoContainer)

Builds the Formula(MongoContainer) ready to pass to testcontainer.with_formula/2.

pub fn new() -> MongoConfig

Sensible defaults: mongo:7 / db app / user root / pass root.

pub fn on_network(
  c: MongoConfig,
  net: network.Network,
) -> MongoConfig
pub fn on_network_name(
  c: MongoConfig,
  net: String,
) -> MongoConfig

Same as on_network/2 when you already have the Docker network name.

pub fn with_auth_database(
  c: MongoConfig,
  db: String,
) -> MongoConfig

Authentication database used in the returned Mongo URI.

pub fn with_database(c: MongoConfig, db: String) -> MongoConfig

Sets MONGO_INITDB_DATABASE. The image creates this database during bootstrap, but mongosh will still connect through auth_database (default "admin").

pub fn with_extra_wait(
  c: MongoConfig,
  s: wait.WaitStrategy,
) -> MongoConfig

Adds an extra wait strategy on top of the default all_of([port(27017), log_times("Waiting for connections", 2)]). The log_times(_, 2) matches the second emission of that line, which is the post-bootstrap restart with --auth enabled — earlier, clients using MONGO_INITDB_ROOT_USERNAME race the auth-bootstrap and fail with “Authentication failed”. Stays image-agnostic (no dependency on mongosh being present in the image).

pub fn with_image(c: MongoConfig, image: String) -> MongoConfig

Replaces the image entirely.

pub fn with_name(c: MongoConfig, n: String) -> MongoConfig
pub fn with_password(c: MongoConfig, pass: String) -> MongoConfig

Sets MONGO_INITDB_ROOT_PASSWORD for the root user.

pub fn with_secret_password(
  c: MongoConfig,
  pass: cowl.Secret(String),
) -> MongoConfig

Same as with_password/2 when the value is already a cowl.Secret.

pub fn with_username(c: MongoConfig, user: String) -> MongoConfig

Sets MONGO_INITDB_ROOT_USERNAME. This is the root user the image creates at startup, used by the returned connection_url.

pub fn with_version(
  c: MongoConfig,
  version: String,
) -> MongoConfig

Shorthand: keep mongo: prefix, override only the tag.

Search Document