mongo-kit


License

License

Categories

Categories

IDE Development Tools
GroupId

GroupId

com.github.mideo
ArtifactId

ArtifactId

mongo-kit_2.11
Last Version

Last Version

0.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

mongo-kit
mongo-kit
Project URL

Project URL

https://github.com/MideO/mongo-kit
Project Organization

Project Organization

com.github.mideo
Source Code Management

Source Code Management

https://github.com/MideO/mongo-kit

Download mongo-kit_2.11

How to add to project

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

Dependencies

compile (5)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.7
org.mongodb : bson jar 3.4.2
com.typesafe.play : play_2.11 jar 2.5.9
com.typesafe.play : play-json_2.11 jar 2.5.9
org.reactivemongo : play2-reactivemongo_2.11 jar 0.12.3

test (3)

Group / Artifact Type Version
org.scalatest : scalatest_2.11 jar 3.0.1
org.mockito : mockito-core jar 2.8.9
org.scalamock : scalamock-scalatest-support_2.11 jar 3.5.0

Project Modules

There are no modules declared in this project.

Mongo Kit

Build Status

Maven Central

Setup dependency

Scala 2.11.7

Simple API abstraction to quick integration of mongodb CRUD operations.

Usage

  • example-playframework [2.5.9]
import com.github.mideo.mongo.reactive.{Crud => ReactiveCrud}]
import com.github.mideo.mongo.inmemory.{Crud => InMemoryCrud}
import com.google.inject.Inject
import play.modules.reactivemongo.ReactiveMongoApi
import reactivemongo.api.collections.bson.BSONCollection
import reactivemongo.bson.{BSONDocumentReader, BSONDocumentWriter, Macros}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future


object Card { implicit val formatter: OFormat[Card] = Json.format[Card] }

case class Card(colour: String)

//Create a document in mongo
class ReactiveCardRepo @Inject()(reactiveMongoApi: ReactiveMongoApi)   
    extend ReactiveCrud[Card] {
  
  override val reactiveMongo: ReactiveMongoApi = reactiveMongoApi
  override val repoName: String = "card"
  override val collection: Future[BSONCollection] = reactiveMongo.database map {_.collection[BSONCollection](repoName) }

  override implicit def Writer: BSONDocumentWriter[Card] = Macros.writer[Card]

  override implicit def Reader: BSONDocumentReader[Card] = Macros.reader[Card]
}

val reactiveCardRepo: ReactiveCardRepo = new ReactiveCardRepo(reactiveMongo)
reactiveCardRepo.create(Card("green"))


//Test with in-memory db
class InMemoryCardRepo extends InMemoryCrud[Card] 

val inMemoryCardRepo: InMemoryCardRepo = new InMemoryCardRepo()
inMemoryCardRepo.create(Car("Red"))
  • example-scalatra
import com.example.app.AppConfig
import com.github.mideo.mongo.reactive.{Crud, ReactiveMongoApiWrapper}
import com.typesafe.config.Config
import play.api.libs.json.{Json, OFormat}
import play.modules.reactivemongo.ReactiveMongoApi
import reactivemongo.api.collections.bson.BSONCollection
import reactivemongo.bson.{BSONDocumentReader, BSONDocumentWriter, Macros}

import scala.concurrent._


object Card {
  implicit val formatter: OFormat[Card] = Json.format[Card]
}

case class Card(colour: String)

trait CardRepo {
  def read:Future[List[Card]]
}

class ReactiveCardRepo extends CardRepo with Crud[Card]{
  private val config: Config = ConfigFactory.load()
  private val reactiveMongoApiWrapper = new ReactiveMongoApiWrapper(config, config.getString("mongo.uri"), config.getString("mongo.db"))

  override implicit def Writer: BSONDocumentWriter[Card] = Macros.writer[Card]

  override implicit def Reader: BSONDocumentReader[Card] = Macros.reader[Card]

  override val repoName: String = "card"

  override val reactiveMongo: ReactiveMongoApi = reactiveMongoApiWrapper.mongoApi

  override val collection: Future[BSONCollection] = reactiveMongoApiWrapper.mongoCollection(repoName)

}

Versions

Version
0.0.1