mongodb-dao


License

License

Categories

Categories

MongoDB Data Databases
GroupId

GroupId

com.github.durre
ArtifactId

ArtifactId

mongodb-dao_2.11
Last Version

Last Version

1.2.2
Release Date

Release Date

Type

Type

jar
Description

Description

mongodb-dao
mongodb-dao
Project URL

Project URL

https://github.com/durre/mongodb-dao
Project Organization

Project Organization

com.github.durre
Source Code Management

Source Code Management

https://github.com/durre/mongodb-dao

Download mongodb-dao_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.durre/mongodb-dao_2.11/ -->
<dependency>
    <groupId>com.github.durre</groupId>
    <artifactId>mongodb-dao_2.11</artifactId>
    <version>1.2.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.durre/mongodb-dao_2.11/
implementation 'com.github.durre:mongodb-dao_2.11:1.2.2'
// https://jarcasting.com/artifacts/com.github.durre/mongodb-dao_2.11/
implementation ("com.github.durre:mongodb-dao_2.11:1.2.2")
'com.github.durre:mongodb-dao_2.11:jar:1.2.2'
<dependency org="com.github.durre" name="mongodb-dao_2.11" rev="1.2.2">
  <artifact name="mongodb-dao_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.durre', module='mongodb-dao_2.11', version='1.2.2')
)
libraryDependencies += "com.github.durre" % "mongodb-dao_2.11" % "1.2.2"
[com.github.durre/mongodb-dao_2.11 "1.2.2"]

Dependencies

compile (2)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.8
org.reactivemongo : reactivemongo_2.11 jar 0.12.6

provided (1)

Group / Artifact Type Version
ch.qos.logback : logback-classic jar 1.1.7

test (1)

Group / Artifact Type Version
org.scalatest : scalatest_2.11 jar 3.0.1

Project Modules

There are no modules declared in this project.

mongodb-dao

Contains some common functionality when working with MongoDb together with Reactivemongo

Usage

libraryDependencies ++= Seq(
  "com.github.durre" %% "mongodb-dao" % "1.2.2",
  // This is a bit akward due to a bug in sbt I believe
  ("com.github.durre" %% "mongodb-dao" % "1.2.2" % "test").classifier("tests")
)

The dao

import se.durre.mongodb.formats.CommonDbFormats._

case class Person(
  id: UUID,
  name: String
)

class PersonDao(db: DefaultDB) extends MongoDao[Person, UUID](db, collectionName = "persons") {

  override protected implicit def reader: BSONDocumentReader[Person] = new BSONDocumentReader[Person] {
    override def read(bson: BSONDocument): Person = {
      (for {
        id <- bson.getAs[UUID]("_id")
        name <- bson.getAs[String]("name")
      } yield Person(id, name)).get
    }
  }

  override protected implicit def writer: BSONDocumentWriter[Person] = new BSONDocumentWriter[Person] {
    override def write(t: Person): BSONDocument = BSONDocument(
      "_id" -> t.id,
      "name" -> t.name
    )
  }
}

Configuration

In your application.conf

mongodb {
  uri = "mongodb://localhost:27017/testdb"
}

Create a connection

val config = ConfigFactory.load() 
MongoContext.connect(config.getConfig("mongodb")).map { db =>
  val dao = new PersonDao(db)
}

Writing tests

You can use the supplied helper class, MongoDbTest.

class PersonDaoSuite extends MongoDbTest {

  val dao = new PersonDao(db)

  // These collections will be dropped before & after each test
  override def touchedCollections: Set[String] = Set(dao.collectionName)

  test("insert a document") {
  
    val person = Person(
      id = UUID.randomUUID(),
      name = "Bob"
    )
  
    dao.insert(person).futureValue    
    val found = dao.findById(person.id).futureValue
    found shouldBe Some(person)
  }
}

Versions

Version
1.2.2
1.2.1
1.2.0