Coder Social home page Coder Social logo

couchbase-scala's Introduction

couchbase-scala

This is a library for accessing Couchbase in Scala.

Using

couchbase-scala is published to maven center

  1. using typesafe config file application.conf to config couchbase connection url, buckets, timeout,..
# see class com.couchbase.client.java.env.DefaultCouchbaseEnvironment
com.couchbase.timeout {
  connect=10s
}
com.sandinh.couchbase {
  connectionString = "couchbase://dev.sandinh.com"
  buckets {
    # syntax: bucketName.password=".."
    # we will use CBCluster.openBucket(bucketName) to retrieve a ScalaBucket object
    acc.password=""
    # we can also use the following verbose syntax to customize the real bucket name when connect to couchbase server
    bk1 {
      name = fodi
      password=""
    }
  }
}
  1. load the config, instantiate a CBCluster instance, then open a bucket
val config = ConfigFactory.load()
val cluster = new CBCluster(config);
val accBucket = cluster.openBucket("acc");

Or, you can use DI (example google guice):

class CBModule extends AbstractModule {
  override def configure(): Unit = {
    bind(classOf[Config]).toInstance(ConfigFactory.load())
  }
}

class MyClient @Inject() (cluster: CBCluster) {
  val accBucket = cluster.openBucket("acc");
}
  1. access couchbase using ScalaBucket's api
val s = accBucket.get[StringDocument]("some_key").map(_.content)
val s = accBucket.getT[String]("some_key")
val s = accBucket.getOrElseT("some_key")("default value")
//see other methods (insert, replace, remove, touch, counter, append, unlock, getFromReplica, getAndLock,..)
//from ScalaBucket class
  1. you can use play-json to retrieve a JsValue directly
case class Acc(name: String, gender: Option[Boolean])
object Acc { implicit val fmt = Json.format[Acc] }
val name = accBucket.getJsT[Acc]("some_key").map(_.name)

Changelogs

see CHANGES.md

Licence

This software is licensed under the Apache 2 license: http://www.apache.org/licenses/LICENSE-2.0

Copyright 2014 Sân Đình (http://sandinh.com)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.