Skip to content

Couchbase

Real Couchbase in a container or wired to an existing cluster. KV ops, N1QL queries, multi-collection support.

Open in setup wizard

Couchbase — wizard-synced snippet

Gradle

testImplementation("com.trendyol:stove-couchbase")

Stove configuration

Stove().with {
    couchbase {
      CouchbaseSystemOptions(
        bucketName = "testbucket",
        configureExposedConfiguration = { cfg ->
          listOf(
            "couchbase.hosts=${cfg.hostsWithPort}",
            "couchbase.username=${cfg.username}",
            "couchbase.password=${cfg.password}"
          )
        }
      )
    }
}

Test DSL

stove {
    couchbase {
      shouldGet<MyDoc>("doc-id") {
        it.field shouldBe "value"
      }
    }
}

In 30 seconds Register couchbase { CouchbaseSystemOptions(bucketName = "...", ...) }. Use save() / shouldGet<T>() / shouldDelete() / shouldQuery<T>(n1ql). Bucket name must match what your app reads. Collection-aware: pass collection to scope KV ops.

Configure

Stove().with {
  couchbase {
    CouchbaseSystemOptions(
      bucketName = "testbucket",
      configureExposedConfiguration = { cfg ->
        listOf(
          "couchbase.hosts=${cfg.hostsWithPort}",
          "couchbase.username=${cfg.username}",
          "couchbase.password=${cfg.password}"
        )
      }
    )
  }
}.run()

DSL

KV ops

stove {
  couchbase {
    save("doc-1", Product(id = "1", name = "Laptop"), collection = "products")
    saveToDefaultCollection("u-1", User(id = "1"))

    shouldGet<Product>("doc-1", collection = "products") {
      it.name shouldBe "Laptop"
    }

    shouldDelete("doc-1", collection = "products")
    shouldNotExist("doc-1", collection = "products")
  }
}

N1QL query

stove {
  couchbase {
    shouldQuery<Product>(
      "SELECT META().id, p.* FROM `testbucket` p WHERE p.type = 'product'"
    ) { products ->
      products shouldHaveSize 3
    }
  }
}

Pause / unpause (container mode)

couchbase {
  pause()    // simulate downtime
  unpause()
}

Migrations

class SeedProducts : DatabaseMigration<Cluster> {
  override val order = 1
  override suspend fun execute(cluster: Cluster) {
    cluster.query("CREATE PRIMARY INDEX ON `testbucket`")
  }
}

couchbase {
  CouchbaseSystemOptions(/* ... */).migrations {
    register<SeedProducts>()
  }
}

Pitfalls

Symptom Fix
BucketNotFoundException App uses different bucket; mirror bucketName exactly
N1QL returns empty Index missing; add CREATE PRIMARY INDEX migration
Collection not found Pass collection arg; default collection is _default

Pairs well with