Skip to content

Commit 7ea105a

Browse files
committed
1.4.0 RELEASE
1 parent 3a733a3 commit 7ea105a

File tree

19 files changed

+180
-120
lines changed

19 files changed

+180
-120
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Versions
44

5+
### 1.4.0
6+
* dropped operations with pattern <name>Result (use headResult, list result or implicit conversion instead)
7+
8+
59
### 1.3.1
610
* update mongo-scala-driver to 2.4.0
711
* Operations Base count method support CountOptions

README.md

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,28 +71,76 @@ object RestaurantDatabase {
7171

7272
```
7373

74+
Import the database object and execute some find and CRUD functions on the DAO object ...
7475

75-
Import the database object and execute find and CRUD functions on the DAO object.
76+
```scala
77+
78+
import com.sfxcode.nosql.mongo.demo.restaurant.RestaurantDemoDatabase._
79+
import com.sfxcode.nosql.mongo._
80+
81+
trait RestaurantDemoDatabaseFunctions {
82+
83+
/**
84+
* single result with implicit conversion to Entity Option
85+
*/
86+
def findRestaurantByName(name: String): Option[Restaurant] =
87+
RestaurantDAO.find("name", name)
88+
89+
def restaurantsSize: Long = RestaurantDAO.count()
90+
91+
/**
92+
* result with implicit conversion to List of Entities
93+
*/
94+
def findAllRestaurants(filterValues: Map[String, Any] = Map()): List[Restaurant] =
95+
RestaurantDAO.find(filterValues)
96+
97+
```
98+
99+
100+
Use the mongodb functions in your app ...
76101

77102
```scala
78-
import RestaurantDatabase._
103+
object RestaurantDemoApp extends App with RestaurantDemoDatabaseFunctions {
79104

80-
import com.sfxcode.nosql.mongo._
105+
// find specific restaurant by name as Option Result
106+
val restaurant = findRestaurantByName("Dj Reynolds Pub And Restaurant")
81107

82-
object RestaurantApp extends App {
108+
println(restaurant)
83109

84-
val restaurant: Option[Restaurant] = RestaurantDAO.find("name", "Dj Reynolds Pub And Restaurant")
110+
// use count function
111+
println(restaurantsSize)
85112

86-
println(restaurant.get.grades)
87-
88-
val restaurants: List[Restaurant] = RestaurantDAO.find(Map("address.zipcode" -> "10075", "cuisine" -> "Italian"))
113+
// find restaurants by filter
114+
private val filter = Map("address.zipcode" -> "10075", "cuisine" -> "Italian")
115+
val restaurants = findAllRestaurants(filter)
89116

90117
restaurants.sortBy(r => r.name).foreach(r => println(r.name))
91118

92119
}
93120

94121
```
95122

123+
Write some spec tests ...
124+
125+
````scala
126+
import com.sfxcode.nosql.mongo.demo.restaurant.RestaurantDemoDatabase._
127+
import org.specs2.mutable.Specification
128+
129+
class RestaurantDemoSpec extends Specification with RestaurantDemoDatabaseFunctions {
130+
131+
"RestaurantDemo" should {
132+
133+
"find restaurant by name in" in {
134+
135+
val restaurantSearch = findRestaurantByName("Dj Reynolds Pub And Restaurant")
136+
restaurantSearch must beSome[Restaurant]
137+
val restaurant = restaurantSearch.get
138+
restaurant.borough must be equalTo "Manhattan"
139+
}
140+
}
141+
142+
}
143+
```
96144

97145

98146

src/main/paradox/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Documentation for [MongoDB](https://docs.mongodb.com/)
3030

3131
- [Tutorial Setup](tutorial/setup.md)
3232
- [Tutorial Database](tutorial/database.md)
33+
- [Tutorial Functions](tutorial/functions.md)
3334
- [Tutorial Application](tutorial/application.md)
3435
- [MongoDAO Overview](dao/mongo_dao.md)
3536
- [MongoDAO Base](dao/base.md)

src/main/paradox/tutorial/application.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
# Tutorial Part 3 - Application
1+
# Tutorial Part 4 - Application
22

33
## Info
44

55
This Demo Application should demonstrate the basic usage of simple-mongo.
66

7-
## Imports
8-
9-
@@snip [RestaurantDatabase.scala](../../../test/scala/com/sfxcode/nosql/mongo/demo/restaurant/RestaurantDemoDatabase.scala) { #import }
10-
11-
127
## Simple Application
138

149
@@snip [RestaurantApp](../../../test/scala/com/sfxcode/nosql/mongo/demo/restaurant/RestaurantDemoApp.scala) { #app }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Tutorial Part 3 - Functions
2+
3+
## Info
4+
5+
Use a functions trait to define some commonly used funtions.
6+
7+
## Imports
8+
9+
@@snip [RestaurantDemoDatabaseFunctions.scala](../../../test/scala/com/sfxcode/nosql/mongo/demo/restaurant/RestaurantDemoDatabaseFunctions.scala) { #import }
10+
11+
12+
## Simple Application
13+
14+
@@snip [RestaurantDemoDatabaseFunctions](../../../test/scala/com/sfxcode/nosql/mongo/demo/restaurant/RestaurantDemoDatabaseFunctions.scala) { #trait }
15+

src/main/scala/com/sfxcode/nosql/mongo/operation/Base.scala

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.sfxcode.nosql.mongo.operation
22

3-
import com.sfxcode.nosql.mongo._
43
import com.typesafe.scalalogging.LazyLogging
54
import org.mongodb.scala.bson.conversions.Bson
6-
import org.mongodb.scala.model.{ CountOptions, IndexOptions }
75
import org.mongodb.scala.model.Sorts._
6+
import org.mongodb.scala.model.{ CountOptions, IndexOptions }
87
import org.mongodb.scala.{ Completed, Document, MongoCollection, Observable, Observer, SingleObservable }
98

109
import scala.reflect.ClassTag
@@ -13,35 +12,23 @@ abstract class Base[A]()(implicit ct: ClassTag[A]) extends LazyLogging {
1312

1413
protected def coll: MongoCollection[A]
1514

16-
def count(filter: Bson = Document(), options: CountOptions = CountOptions()): Observable[Long] = coll.countDocuments(filter)
17-
18-
def countResult(filter: Bson = Document(), options: CountOptions = CountOptions()): Long = count(filter, options)
15+
def count(filter: Bson = Document(), options: CountOptions = CountOptions()): Observable[Long] = coll.countDocuments(filter, options)
1916

2017
def drop(): Observable[Completed] = coll.drop()
2118

22-
def dropResult(): Completed = drop().headResult()
23-
2419
def createIndexForField(field: String, sortAscending: Boolean = true): SingleObservable[String] = {
2520
if (sortAscending)
2621
createIndex(ascending(field))
2722
else
2823
createIndex(descending(field))
2924
}
3025

31-
def createIndexForFieldResult(field: String, sortAscending: Boolean = true): String = createIndexForField(field, sortAscending)
32-
3326
def createIndex(key: Bson, options: IndexOptions = IndexOptions()): SingleObservable[String] = coll.createIndex(key, options)
3427

35-
def createsIndexResult(key: Bson, options: IndexOptions = IndexOptions()): String = createIndex(key, options)
36-
3728
def dropIndexForName(name: String): SingleObservable[Completed] = coll.dropIndex(name)
3829

39-
def dropIndexForNameResult(name: String): Completed = dropIndexForName(name)
40-
4130
def dropIndex(keys: Bson): SingleObservable[Completed] = coll.dropIndex(keys)
4231

43-
def dropIndexResult(keys: Bson): Completed = dropIndex(keys)
44-
4532
}
4633

4734
class SimpleCompletedObserver[T] extends Observer[T] with LazyLogging {

src/main/scala/com/sfxcode/nosql/mongo/operation/Crud.scala

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,12 @@ abstract class Crud[A]()(implicit ct: ClassTag[A]) extends Base[A] {
1414
// create
1515
def insertOne(value: A): Observable[Completed] = coll.insertOne(value)
1616

17-
def insertOneResult(value: A): Completed = coll.insertOne(value)
18-
1917
def insertOne(value: A, options: InsertOneOptions): Observable[Completed] = coll.insertOne(value, options)
2018

21-
def insertOneResult(value: A, options: InsertOneOptions): Completed = insertOne(value, options)
22-
2319
def insertMany(values: Seq[A]): Observable[Completed] = coll.insertMany(values)
2420

25-
def insertManyResult(values: Seq[A]): Completed = insertMany(values)
26-
2721
def insertMany(values: Seq[A], options: InsertManyOptions): Observable[Completed] = coll.insertMany(values, options)
2822

29-
def insertManyResult(values: Seq[A], options: InsertManyOptions): Completed = insertMany(values, options)
30-
3123
// update
3224

3325
def replaceOne(value: A): Observable[UpdateResult] = {
@@ -36,81 +28,47 @@ abstract class Crud[A]()(implicit ct: ClassTag[A]) extends Base[A] {
3628
coll.replaceOne(equal("_id", oid), value)
3729
}
3830

39-
def replaceOneResult(value: A): UpdateResult = replaceOne(value)
40-
4131
def replaceOne(value: A, options: ReplaceOptions): Observable[UpdateResult] = {
4232
val document = Converter.toDocument(value)
4333
val oid = document.get("_id").get
4434
coll.replaceOne(equal("_id", oid), value, options)
4535
}
4636

47-
def replaceOneResult(value: A, options: ReplaceOptions): UpdateResult = replaceOne(value, options)
48-
4937
def replaceOne(filter: Bson, value: A): Observable[UpdateResult] =
5038
coll.replaceOne(filter, value)
5139

52-
def replaceOneResult(filter: Bson, value: A): UpdateResult = replaceOne(filter, value)
53-
5440
def replaceOne(filter: Bson, value: A, options: ReplaceOptions): Observable[UpdateResult] =
5541
coll.replaceOne(filter, value, options)
5642

57-
def replaceOneResult(filter: Bson, value: A, options: ReplaceOptions): UpdateResult = replaceOne(filter, value, options)
58-
5943
def updateOne(filter: Bson, update: Bson): Observable[UpdateResult] =
6044
coll.updateOne(filter, update)
6145

62-
def updateOneResult(filter: Bson, update: Bson): UpdateResult =
63-
updateOne(filter, update)
64-
6546
def updateOne(filter: Bson, update: Bson, options: UpdateOptions): Observable[UpdateResult] =
6647
coll.updateOne(filter, update, options)
6748

68-
def updateOneResult(filter: Bson, update: Bson, options: UpdateOptions): UpdateResult =
69-
updateOne(filter, update, options)
70-
7149
def updateMany(filter: Bson, update: Bson): Observable[UpdateResult] =
7250
coll.updateMany(filter, update)
7351

74-
def updateManyResult(filter: Bson, update: Bson): UpdateResult =
75-
updateMany(filter, update)
76-
7752
def updateMany(filter: Bson, update: Bson, options: UpdateOptions): Observable[UpdateResult] =
7853
coll.updateMany(filter, update, options)
7954

80-
def updateManyResult(filter: Bson, update: Bson, options: UpdateOptions): UpdateResult =
81-
updateMany(filter, update, options)
82-
8355
// delete
8456

8557
def deleteOne(filter: Bson): Observable[DeleteResult] = coll.deleteOne(filter)
8658

87-
def deleteOneResult(filter: Bson): DeleteResult = deleteOne(filter)
88-
8959
def deleteOne(filter: Bson, options: DeleteOptions): Observable[DeleteResult] = coll.deleteOne(filter, options)
9060

91-
def deleteOneResult(filter: Bson, options: DeleteOptions): DeleteResult = deleteOne(filter, options)
92-
9361
def deleteOne(value: A): Observable[DeleteResult] = {
9462
val oid = Converter.toDocument(value).get("_id").get
9563
coll.deleteOne(equal("_id", oid))
9664
}
9765

98-
def deleteOneResult(value: A): DeleteResult = deleteOne(value)
99-
10066
def deleteMany(filter: Bson): Observable[DeleteResult] = coll.deleteMany(filter)
10167

102-
def deleteManyResult(filter: Bson): DeleteResult = deleteMany(filter)
103-
10468
def deleteMany(filter: Bson, options: DeleteOptions): Observable[DeleteResult] = coll.deleteMany(filter, options)
10569

106-
def deleteManyResult(filter: Bson, options: DeleteOptions): DeleteResult = deleteMany(filter, options)
107-
10870
def deleteAll(): Observable[DeleteResult] = deleteMany(Map())
10971

110-
def deleteAllResult(): DeleteResult = deleteAll()
111-
11272
def deleteAll(options: DeleteOptions): Observable[DeleteResult] = deleteMany(Map(), options)
11373

114-
def deleteAllResult(options: DeleteOptions): DeleteResult = deleteAll(options)
115-
11674
}

src/test/scala/com/sfxcode/nosql/mongo/TestDatabase.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ object TestDatabase extends ObservableImplicits {
2828

2929
object PersonDAO extends MongoDAO[Person](database, "person")
3030

31-
PersonDAO.dropResult()
31+
val dropResult: Completed = PersonDAO.drop()
3232

3333
val persons: List[Person] = Person.personList
3434

35-
PersonDAO.insertMany(persons).head()
35+
val insertResult: Completed = PersonDAO.insertMany(persons)
3636

3737
def printDatabaseStatus(): Unit = {
38-
printDebugValues("Database Status", "%s rows for collection person found".format(PersonDAO.countResult()))
38+
val count: Long = PersonDAO.count()
39+
printDebugValues("Database Status", "%s rows for collection person found".format(count))
3940
}
4041

4142
def printDebugValues(name: String, result: Any): Unit = {

src/test/scala/com/sfxcode/nosql/mongo/demo/restaurant/RestaurantDemoApp.scala

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
package com.sfxcode.nosql.mongo.demo.restaurant
22

3-
// #import
4-
// static import of needed DAO objects
5-
import com.sfxcode.nosql.mongo.demo.restaurant.RestaurantDemoDatabase._
3+
// #app
64

7-
// static import of mongo package object for needed implicits
8-
import com.sfxcode.nosql.mongo._
9-
// #import
5+
object RestaurantDemoApp extends App with RestaurantDemoDatabaseFunctions {
106

11-
// #app
7+
// find specific restaurant by name as Option Result
8+
val restaurant = findRestaurantByName("Dj Reynolds Pub And Restaurant")
129

13-
object RestaurantDemoApp extends App {
14-
// find specific restaurant key and value as Option Result
15-
val restaurant = RestaurantDAO.find("name", "Dj Reynolds Pub And Restaurant")
10+
println(restaurant)
1611

17-
println(restaurant.get)
12+
// use count function
13+
println(restaurantsSize)
1814

15+
// find restaurants by filter
1916
private val filter = Map("address.zipcode" -> "10075", "cuisine" -> "Italian")
20-
// find restaurants by filter - use implicit map to document conversion
21-
// as List result
22-
val restaurants: List[Restaurant] = RestaurantDAO.find(filter)
17+
val restaurants = findAllRestaurants(filter)
2318

2419
restaurants.sortBy(r => r.name).foreach(r => println(r.name))
2520

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.sfxcode.nosql.mongo.demo.restaurant
2+
3+
// #import
4+
// static import of needed DAO objects
5+
import com.sfxcode.nosql.mongo.demo.restaurant.RestaurantDemoDatabase._
6+
7+
// static import of mongo package object for needed implicits
8+
import com.sfxcode.nosql.mongo._
9+
// #import
10+
11+
// #trait
12+
13+
trait RestaurantDemoDatabaseFunctions {
14+
15+
/**
16+
* single result with implicit conversion to Entity Option
17+
*/
18+
def findRestaurantByName(name: String): Option[Restaurant] =
19+
RestaurantDAO.find("name", name)
20+
21+
def restaurantsSize: Long = RestaurantDAO.count()
22+
23+
/**
24+
* result with implicit conversion to List of Entities
25+
*/
26+
def findAllRestaurants(filterValues: Map[String, Any] = Map()): List[Restaurant] =
27+
RestaurantDAO.find(filterValues)
28+
29+
// #trait
30+
31+
}

0 commit comments

Comments
 (0)