{"id":13800932,"url":"https://github.com/ReactiveCouchbase/reactivecouchbase-rs-core","last_synced_at":"2025-05-13T10:30:41.756Z","repository":{"id":59966896,"uuid":"81304974","full_name":"ReactiveCouchbase/reactivecouchbase-rs-core","owner":"ReactiveCouchbase","description":"New ReactiveCouchbase driver using reactive-streams","archived":false,"fork":false,"pushed_at":"2018-04-23T09:37:17.000Z","size":7622,"stargazers_count":26,"open_issues_count":8,"forks_count":10,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-08-04T00:05:38.127Z","etag":null,"topics":["akka-streams","async","couchbase","json","non-blocking","nosql","reactive-programming","reactive-streams","scala"],"latest_commit_sha":null,"homepage":null,"language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ReactiveCouchbase.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-08T08:16:45.000Z","updated_at":"2023-07-07T12:12:08.000Z","dependencies_parsed_at":"2022-09-25T11:30:33.432Z","dependency_job_id":null,"html_url":"https://github.com/ReactiveCouchbase/reactivecouchbase-rs-core","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ReactiveCouchbase%2Freactivecouchbase-rs-core","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ReactiveCouchbase%2Freactivecouchbase-rs-core/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ReactiveCouchbase%2Freactivecouchbase-rs-core/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ReactiveCouchbase%2Freactivecouchbase-rs-core/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ReactiveCouchbase","download_url":"https://codeload.github.com/ReactiveCouchbase/reactivecouchbase-rs-core/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225198940,"owners_count":17437002,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["akka-streams","async","couchbase","json","non-blocking","nosql","reactive-programming","reactive-streams","scala"],"created_at":"2024-08-04T00:01:17.759Z","updated_at":"2024-11-18T15:31:22.392Z","avatar_url":"https://github.com/ReactiveCouchbase.png","language":"Scala","readme":"# ReactiveCouchbase RS (ReactiveStreams edition) [![Build Status](https://travis-ci.org/ReactiveCouchbase/reactivecouchbase-rs-core.svg?branch=master)](https://travis-ci.org/ReactiveCouchbase/reactivecouchbase-rs-core)\n\nYes, it's happening !!! with **ReactiveStreams** support ;-)\n\n## If you want to try it\n\nadd a resolver to your `build.sbt` file\n\n```scala\nResolver.bintrayRepo(\"mathieuancelin\", \"reactivecouchbase-maven\")\n\nor\n\nResolver.jcenterRepo\n```\n\nor you can build it to get the nice goodies\n\n```sh\ngit clone https://github.com/ReactiveCouchbase/reactivecouchbase-rs-core.git\ncd reactivecouchbase-rs-core\nsbt ';clean;compile;publish-local'\n```\n\nthen in your project add the following dependency\n\n```scala\nlibraryDependencies += \"org.reactivecouchbase\" %% \"reactivecouchbase-rs-core\" % \"1.2.1\"\n```\n\nand you're ready to go\n\n## A small example\n\n```scala\nimport akka.actor.ActorSystem\nimport akka.stream.ActorMaterializer\nimport com.typesafe.config.ConfigFactory\nimport org.reactivecouchbase.rs.scaladsl.{N1qlQuery, ReactiveCouchbase}\nimport org.reactivecouchbase.rs.scaladsl.json._\nimport play.api.libs.json._\nimport akka.stream.scaladsl.Sink\nimport akka.actor.ActorSystem\nimport akka.stream.ActorMaterializer\nimport com.typesafe.config.ConfigFactory\n\nobject ReactiveCouchbaseTest extends App {\n\n  val system = ActorSystem(\"ReactiveCouchbaseSystem\")\n\n  implicit val materializer = ActorMaterializer.create(system)\n  implicit val ec = system.dispatcher\n\n  val driver = ReactiveCouchbase(ConfigFactory.parseString(\n    \"\"\"\n      |buckets {\n      |  default {\n      |    name = \"default\"\n      |    hosts = [\"127.0.0.1\"]\n      |  }\n      |}\n    \"\"\".stripMargin))\n\n  val bucket = driver.bucket(\"default\")\n\n  val future = for {\n    _        \u003c- bucket.insert[JsValue](\"key1\", Json.obj(\"message\" -\u003e \"Hello World\", \"type\" -\u003e \"doc\"))\n    doc      \u003c- bucket.get(\"key1\")\n    exists   \u003c- bucket.exists(\"key1\")\n    docs     \u003c- bucket.search(N1qlQuery(\"select message from default where type = $type\")\n                  .on(Json.obj(\"type\" -\u003e \"doc\").asQueryParams))\n                  .asSeq\n    messages \u003c- bucket.search(N1qlQuery(\"select message from default where type = 'doc'\"))\n                  .asSource.map(doc =\u003e (doc \\ \"message\").as[String].toUpperCase)\n                  .runWith(Sink.seq[String])\n    _        \u003c- driver.terminate()\n  } yield (doc, exists, docs)\n\n  future.map {\n    case (_, _, docs) =\u003e println(s\"found $docs\")\n  }\n\n}\n```\n\n## Example using custom environment and timeout\n```scala\nimport akka.actor.ActorSystem\nimport akka.stream.ActorMaterializer\nimport com.typesafe.config.ConfigFactory\nimport com.couchbase.client.java.env.DefaultCouchbaseEnvironment\nimport org.reactivecouchbase.rs.scaladsl.{N1qlQuery, ReactiveCouchbase}\nimport org.reactivecouchbase.rs.scaladsl.json._\nimport play.api.libs.json._\nimport akka.stream.scaladsl.Sink\nimport akka.actor.ActorSystem\nimport akka.stream.ActorMaterializer\nimport com.typesafe.config.ConfigFactory\nimport scala.concurrent.duration._\n\nobject ReactiveCouchbaseTest extends App {\n\n  val system = ActorSystem(\"ReactiveCouchbaseSystem\")\n\n  implicit val materializer = ActorMaterializer.create(system)\n  implicit val ec = system.dispatcher\n\n  val driver = ReactiveCouchbase(ConfigFactory.parseString(\n    \"\"\"\n      |buckets {\n      |  default {\n      |    name = \"default\"\n      |    hosts = [\"127.0.0.1\"]\n      |  }\n      |}\n    \"\"\".stripMargin))\n  \n  // Sets a custom environment builder for the Couchbase bucket so that mutation tokens are enabled\n  def customEnv: DefaultCouchbaseEnvironment.Builder =\u003e DefaultCouchbaseEnvironment.Builder = env =\u003e env.mutationTokensEnabled(true) \n  \n  // Uses the custom environment, sets a default timeout of 10 seconds for all operations\n  val bucket = driver.bucket(\"default\", customEnv, Some(10.seconds))\n\n  val future = for {\n    _        \u003c- bucket.insert[JsValue](\"key1\", Json.obj(\"message\" -\u003e \"Hello World\", \"type\" -\u003e \"doc\"))\n    doc      \u003c- bucket.get(\"key1\")\n    exists   \u003c- bucket.exists(\"key1\")\n    docs     \u003c- bucket.search(N1qlQuery(\"select message from default where type = $type\")\n                  .on(Json.obj(\"type\" -\u003e \"doc\").asQueryParams), Some(5.seconds)) // Overrides the default timeout with its own timeout of 5 seconds\n                  .asSeq\n    messages \u003c- bucket.search(N1qlQuery(\"select message from default where type = 'doc'\"))\n                  .asSource.map(doc =\u003e (doc \\ \"message\").as[String].toUpperCase)\n                  .runWith(Sink.seq[String])\n    _        \u003c- driver.terminate()\n  } yield (doc, exists, docs)\n\n  future.map {\n    case (_, _, docs) =\u003e println(s\"found $docs\")\n  }\n\n}\n```\n\n\n## What about the Play Framework plugin ?\n\nI don't think you actually need a plugin, if you want to use it from Play Framework, you can define a service to access your buckets like the following :\n\n\n```scala\nimport javax.inject._\nimport play.api.inject.ApplicationLifecycle\nimport play.api.Configuration\nimport org.reactivecouchbase.rs.scaladsl._\n\n@Singleton\nclass Couchbase @Inject()(configuration: Configuration, lifecycle: ApplicationLifecycle) {\n\n  private val driver = ReactiveCouchbase(configuration.underlying.getConfig(\"reactivecouchbase\"))\n\n  def bucket(name: String): Bucket = driver.bucket(name)\n\n  lifecycle.addStopHook { () =\u003e\n    driver.terminate()\n  }\n}\n```\n\nso you can define a controller like the following\n\n```scala\nimport javax.inject._\nimport scala.concurrent.ExecutionContext\nimport play.api.mvc._\nimport akka.stream.Materializer\nimport play.api.libs.json._\n\n@Singleton\nclass ApiController @Inject()(couchbase: Couchbase)(implicit ec: ExecutionContext, materializer: Materializer) extends Controller {\n\n  def eventsBucket = couchbase.bucket(\"events\")\n\n  def events(filter: Option[String] = None) = Action {\n    val source = eventsBucket\n      .search(N1qlQuery(\"select id, payload, date, params, type from events where type = $type\")\n      .on(Json.obj(\"type\" -\u003e filter.getOrElse(\"doc\")).asQueryParams)\n      .asSource\n      .map(Json.stringify)\n      .intersperse(\"[\", \",\", \"]\")\n    Ok.chunked(source).as(\"application/json\")\n  }\n}\n```\n\n## What if I want to use a JSON lib other than Play Json ?\n\nyou can easily do that, actually everything linked to Play Json is imported from:\n\n```scala\nimport org.reactivecouchbase.rs.scaladsl.json._\n```\n\nthen you just have to reimplement a few things\n\n```scala\nimport akka.util.ByteString\nimport com.couchbase.client.java.document.json.JsonObject\nimport org.reactivecouchbase.rs.scaladsl.json.{JsonReads, JsonWrites, JsonSuccess, QueryParams}\nimport foo.bar.jsonlib.{JsonNode, JsonObj}\n\nval read: JsonReads[JsonNode] = JsonReads(bs =\u003e JsonSuccess(JsonNode.parse(bs.utf8String)))\nval write: JsonWrites[JsonNode] = JsonWrites(jsv =\u003e ByteString(JsonNode.stringify(jsv)))\n\nimplicit val defaultByteStringFormat: JsonFormat[JsonNode] = JsonFormat(read, write)\n\nimplicit val defaultByteStringConverter: CouchbaseJsonDocConverter[JsonNode] = new CouchbaseJsonDocConverter[JsonNode] {\n  override def convert(ref: AnyRef): JsonNode = ...\n}\n\ncase class JsonObjQueryParams(query: JsonObj = ByteString.empty) extends QueryParams {\n  override def isEmpty: Boolean = !query.hasValue\n  override def toJsonObject: JsonObject = ...\n}\n```\n\nYou have a few examples at\n\n* https://github.com/ReactiveCouchbase/reactivecouchbase-rs-core/blob/master/src/main/scala/org/reactivecouchbase/rs/scaladsl/json/package.scala\n* https://github.com/ReactiveCouchbase/reactivecouchbase-rs-core/blob/master/src/main/scala/org/reactivecouchbase/rs/scaladsl/json/bytestring.scala \n* https://github.com/ReactiveCouchbase/reactivecouchbase-rs-core/blob/master/src/main/scala/org/reactivecouchbase/rs/scaladsl/json/circejson.scala\n* https://github.com/ReactiveCouchbase/reactivecouchbase-rs-core/blob/master/src/main/scala/org/reactivecouchbase/rs/scaladsl/json/converter.scala#L21-L31\n\n\n## How about Circe support?\n\nReactive Couchbase also supports Circe for JSON serialization:\n\n```scala\nimport io.circe.{ Encoder, Decoder }\nimport io.circe.syntax._\nimport io.circe.generic.semiauto._\nimport org.reactivecouchbase.rs.scaladsl.json._\nimport org.reactivecouchbase.rs.scaladsl.circejson._\n\nsealed case class TestModel(message: String, `type`: Option[String])\n\nimplicit val encoder: Encoder[TestModel] = deriveEncoder\nimplicit val decoder: Decoder[TestModel] = deriveDecoder\nimplicit val format2: JsonFormat[TestModel] = createCBFormat\n```\n\n## Todo\n\n* [ ] separate play-json and circe in modules ?\n\n## Release\n\n```\nsbt \"release with-defaults\"\n```","funding_links":[],"categories":["Table of Contents"],"sub_categories":["Database"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FReactiveCouchbase%2Freactivecouchbase-rs-core","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FReactiveCouchbase%2Freactivecouchbase-rs-core","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FReactiveCouchbase%2Freactivecouchbase-rs-core/lists"}