Reactivemongo Shortcuts for Play and JSON

Short methods for different reactivemongo scenarios, created for Play and JSON

License

License

Categories

Categories

React User Interface Web Frameworks JSON Data
GroupId

GroupId

com.github.andriykuba
ArtifactId

ArtifactId

reactivemongo-shortcuts-play-json
Last Version

Last Version

2.6.1
Release Date

Release Date

Type

Type

jar
Description

Description

Reactivemongo Shortcuts for Play and JSON
Short methods for different reactivemongo scenarios, created for Play and JSON
Project URL

Project URL

https://github.com/andriykuba/reactivemongo-shortcuts-play-json
Source Code Management

Source Code Management

http://github.com/andriykuba/reactivemongo-shortcuts-play-json

Download reactivemongo-shortcuts-play-json

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.andriykuba/reactivemongo-shortcuts-play-json/ -->
<dependency>
    <groupId>com.github.andriykuba</groupId>
    <artifactId>reactivemongo-shortcuts-play-json</artifactId>
    <version>2.6.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.andriykuba/reactivemongo-shortcuts-play-json/
implementation 'com.github.andriykuba:reactivemongo-shortcuts-play-json:2.6.1'
// https://jarcasting.com/artifacts/com.github.andriykuba/reactivemongo-shortcuts-play-json/
implementation ("com.github.andriykuba:reactivemongo-shortcuts-play-json:2.6.1")
'com.github.andriykuba:reactivemongo-shortcuts-play-json:jar:2.6.1'
<dependency org="com.github.andriykuba" name="reactivemongo-shortcuts-play-json" rev="2.6.1">
  <artifact name="reactivemongo-shortcuts-play-json" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.andriykuba', module='reactivemongo-shortcuts-play-json', version='2.6.1')
)
libraryDependencies += "com.github.andriykuba" % "reactivemongo-shortcuts-play-json" % "2.6.1"
[com.github.andriykuba/reactivemongo-shortcuts-play-json "2.6.1"]

Dependencies

provided (3)

Group / Artifact Type Version
com.typesafe.play : play_2.11 jar 2.6.1
com.typesafe.play : play-json_2.11 jar 2.6.2
org.reactivemongo : play2-reactivemongo_2.11 jar 0.12.5-play26

test (9)

Group / Artifact Type Version
junit : junit jar 4.12
org.mockito : mockito-core jar 2.8.47
org.slf4j : slf4j-simple jar 1.7.25
org.scalatest : scalatest_2.11 jar 3.0.3
org.scalatestplus.play : scalatestplus-play_2.11 jar 3.1.0
com.typesafe.play : play-test_2.11 jar 2.6.1
org.apache.commons : commons-compress jar 1.14
com.github.simplyscala : scalatest-embedmongo_2.11 jar 0.2.4
org.mongodb.scala : mongo-scala-driver_2.11 jar 2.1.0

Project Modules

There are no modules declared in this project.

Reactivemongo Shortcuts for Play (with JSON)

Maven Central

Reduce code for the common cases of ReactiveMongo usage in Play framework, Scala language.

Table of Contents

Install

Add the library in built.sbt

Play 2.6, Scala 2.11

libraryDependencies += "com.github.andriykuba" % "reactivemongo-shortcuts-play-json" % "2.6.1"

Play 2.5, Scala 2.11

libraryDependencies += "com.github.andriykuba" % "reactivemongo-shortcuts-play-json" % "2.5.19" 

Usage

Suppose, we want to get some field from all documents. The "native" ReactiveMongo code will look like:

def native()(implicit mongo: ReactiveMongoApi):Future[List[JsObject]] = 
  mongo.database
    .map(_.collection[JSONCollection](collectionName)).flatMap(_
      .find(Json.obj(), Json.obj("field" -> 1))
        .cursor[JsObject]()
          .collect[List](-1, 
            Cursor.FailOnError(
              (a: List[JsObject], e: Throwable) => Cursor.Fail(e))))

With the Shortcuts it will:

def shortcuts()(implicit mongo: ReactiveMongoApi): Future[List[JsObject]] = 
  all(Json.obj(), Json.obj("name" -> 1))

The Shortcuts reduce method "chaining" to one call and use Play Framework JSON to work with data.

To start use shortcuts just extend com.github.andriykuba.play.reactivemongo.shortcuts.Collection

object Users extends Collection{
  val collectionName = "users"
}

...

val allUsersInLondon = Users.all(Json.obj("city" -> "London"))

ReactiveMongoApi and ExecutionContext are passed implicitly to all methods but defineSubCollectionName. ReactiveMongo is asynchronous, so all methods but defineSubCollectionName return results wrapped in Future.

Methods and properties

Collection

collectionName

The name of the collection in MongoDB

defineSubCollectionName

Sugar for creating name of higher detailed collection. For example you need to create a collection of user's page visits and you already have the "users" collection. Good naming are:

users
users.pagevisits

So you can do:

object Users extends Collection{
  val collectionName = "users"
}

object UsersPagevisits extends Collection{
  val collectionName = "users.pagevisits"
}

Or:

object Users extends Collection{
  val collectionName = "users"
}

object UsersPagevisits extends Collection{
  val collectionName = Users.defineSubCollectionName("pagevisits")
}

collection

Shortcut for getting a collection from a database.

So this string

mongo.database.map(_.collection[JSONCollection](collectionName))

become as short as

MyCollection.collection()

count

Shortcut for getting a number of documents in the collection.

So this string

mongo.database.map(_.collection[JSONCollection](collectionName)).flatMap(c => c.count())

become as short as

MyCollection.count()

fold

Fold the collection. A start value and a fold function are set with the Folder case class. Projection and sort can also be used.

val folder = Folder(0, (count: Int, doc: JsObject) => {
  count + 1
})

MyCollection.fold(Json.obj("age" -> 20), folder)

foldM

Fold the collection asynchronously. A start value and a fold function are set with the FolderM case class. Projection and sort can also be used.

val folderM = FolderM(0, (count: Int, doc: JsObject) => {
  Future(count + 1)
})

MyCollection.foldM(Json.obj("age" -> 20), folderM)

Document

all

Find all documents. It returns a list of JsObject. Projection and sort can also be used.

MyCollection.all(Json.obj("age" -> 30))

one

Find zero or one document. It throw an exception if more than one document found. Projection can also be used.

MyCollection.one(Json.obj("name" -> "Adam Smith"))

first

Find zero or one document. It returns first document if more than one document found. Projection and sort can also be used.

MyCollection.first(Json.obj("age" -> 40))

first in a set of collections

Look for the first document in a set of collections with different selects.

Scenario to use - check if there is some document in a set of collection that meets the criteria.

Collection.first(List(
    (TrainsCollection, Json.obj("price" -> Json.obj("$lte" -> 100))),
    (BusesCollection, Json.obj("price" -> Json.obj("$lte" -> 100))),
    (PlanesCollection, Json.obj("price" -> Json.obj("$lte" -> 100)))))

Field

fieldOpt[T]

Get an optional field from a single document. Throw an exception if more than one document is found. Field type T is obligatory.

MyCollection.fieldOpt[Int](Json.obj("name"->"Adam Smith"), "age")

field[T]

Get a field from a single document. It throws an exception if no document or field are found. Throw an exception if more than one document is found. Field type T is obligatory.

MyCollection.field[Int](Json.obj("name"->"Adam Smith"), "age")

fieldStringOrEmpty

Get a string field from a single document or empty string if no document or file was found. Throw an exception if more than one document is found.

MyCollection.fieldStringOrEmpty(Json.obj("name"->"Adam Smith"), "address")

Update/Create

update

Update matching document (using findAndModify). This method is also used for document creation (upsert = true).

MyCollection.update(
  Json.obj("name"->"Adam Smith"), 
  Json.obj("$set" -> Json.obj("age" -> 80)))  
  
  
MyCollection.update(
  mySelector, 
  myDocument,
  upsert = true)   

createUnique

Finds some matching document and creates new document if nothing found. It throws a DocumentAlreadyExists exception otherwise

MyCollection.createUnique(
  Json.obj("name"->"John Silver"), 
  Json.obj(Json.obj("age" -> 40)))   

insert

Inserts a document into the collection and wait for the results.

val document = Json.obj("name"->"Adam Smith")

MyCollection.insert(document)  

Remove

remove

Remove matching document (using findAndModify). Return true if document was removed or false if it was not found.

MyCollection.remove(Json.obj("name"->"Adam Smith"))  

Field Shortcuts

In beta, look the source code of the FieldShortcuts object

Versions

Version
2.6.1
2.5.19
2.5.18
2.5.17
2.5.16
2.5.15
2.5.14
2.5.12
2.5.11
2.5.10
2.5.9
2.5.8
2.5.7
2.5.6
2.5.5
2.5.4
2.5.3
2.5.2
2.5.1