Kontainer Integration

Register MongoDB connections and repositories in Kontainer.

Setup

import io.peekandpoke.monko.MongoDbConfig
import io.peekandpoke.monko.monko

val blueprint = kontainer {
    monko(MongoDbConfig(
        connectionString = "mongodb://root:root@localhost:27017",
        database = "my_app",
    ))

    // Register your repositories
    singleton(PersonsRepo::class)
    singleton(ArticlesRepo::class)
}

The monko(config) call registers:

  • MongoClient — singleton, shared connection pool
  • MongoDatabase — singleton, database handle
  • MonkoDriver — dynamic, created per request (carries profiler context)
  • MonkoCodec — dynamic, created per request (carries EntityCache)
The MongoClient is cached and reused across container instances. Only the driver and codec are created fresh per request — they carry per-request state like query profiling and entity deduplication.

With Funktor

In a Funktor application, Monko integrates as a pluggable backend. Each module declares its storage needs independently:

fun createBlueprint(config: MyAppConfig) = kontainer {
    funktor(
        config = config,
        auth = { useMonko() },       // Auth records in MongoDB
        logging = { useMonko() },    // Logs in MongoDB
        cluster = { useMonko() },    // Jobs, locks, storage in MongoDB
        messaging = { useMonko() },  // Sent messages in MongoDB
    )

    // MongoDB connection
    monko(config.mongodb)

    // Your repositories
    singleton(UsersRepo::class)
}

Mixing databases

You can use both Monko and Karango in the same application. Each Funktor module chooses its backend independently:

funktor(
    config = config,
    auth = { useKarango() },     // Auth in ArangoDB
    logging = { useMonko() },    // Logs in MongoDB
    cluster = { useKarango() },  // Cluster in ArangoDB
    messaging = { useMonko() },  // Messages in MongoDB
)

karango(config.arangodb)
monko(config.mongodb)

Database lifecycle

// Access through Kontainer
val driver = kontainer.get(MonkoDriver::class)

// Or in Funktor handlers, repositories are available via Database
val Database.persons get() = getRepository<PersonsRepo>()

On startup, Funktor's EnsureRepositoriesOnAppStarting hook automatically calls ensureIndexes() on all registered repositories.