Skip to content

Commit 4ef45c9

Browse files
committed
Documentation added
1 parent 8023035 commit 4ef45c9

File tree

11 files changed

+234
-7
lines changed

11 files changed

+234
-7
lines changed

src/main/paradox/dao/base.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# MongoDAO - Base Functions
2+
3+
## Info
4+
5+
Base Collection functions. All functions support synchronous result handling (add Result to function name, e.g. drop -> dropResult).
6+
7+
## Drop
8+
9+
Drop Collection.
10+
11+
```scala
12+
def drop(): Observable[Completed]
13+
```
14+
15+
## Count
16+
17+
Count of collection with optional filter.
18+
19+
```scala
20+
def count(filter: Bson = Document()): Observable[Long]
21+
```
22+
23+
## Indexes
24+
25+
```scala
26+
27+
def createIndex(key: Bson, options: IndexOptions = IndexOptions()): SingleObservable[String]
28+
29+
// Simple Index creation
30+
def createIndexForField(field: String, sortAscending: Boolean = true): SingleObservable[String]
31+
32+
def dropIndex(keys: Bson): SingleObservable[Completed]
33+
34+
// Simple Index delete
35+
def dropIndexForName(name: String): SingleObservable[Completed]
36+
37+
```

src/main/paradox/dao/crud.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# MongoDAO - CRUD Functions
2+
3+
## Info
4+
5+
CRUD Collection functions. All functions support synchronous result handling (add Result to function name, e.g. drop -> dropResult).
6+
7+
## Create
8+
9+
```scala
10+
11+
def insertOne(value: A): Observable[Completed]
12+
13+
def insertOne(value: A), options: InsertOneOptions: Observable[Completed]
14+
15+
def insertMany(values: Seq[A]): Observable[Completed]
16+
17+
def insertMany(values: Seq[A], options: InsertManyOptions): Observable[Completed]
18+
19+
```
20+
21+
## Update
22+
23+
```scala
24+
25+
def replaceOne(value: A): Observable[UpdateResult]
26+
27+
def replaceOne(value: A, options: ReplaceOptions): Observable[UpdateResult]
28+
29+
```
30+
31+
## Delete
32+
33+
```scala
34+
35+
def deleteOne(value: A): Observable[DeleteResult]
36+
37+
def deleteOne(filter: Bson): Observable[DeleteResult]
38+
39+
def deleteMany(filter: Bson): Observable[DeleteResult]
40+
41+
def deleteAll(): Observable[DeleteResult]
42+
43+
44+
```

src/main/paradox/dao/mongo_dao.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# MongoDAO - Overview
2+
3+
## Info
4+
5+
MongoDAO is the core of this framework. The [DAO](https://en.wikipedia.org/wiki/Data_access_object) Pattern simplifies database usage.
6+
7+
The MongoDAO object holds a reference to a [MongoCollection](http://mongodb.github.io/mongo-scala-driver/2.3/scaladoc/org/mongodb/scala/MongoCollection.html) and adds functions for easy collection handling.
8+
9+
All functions support synchronous result handling (add Result to function name, e.g. drop -> dropResult, insert ->insertResult).
10+
11+
## Features
12+
13+
* @ref:[MongoDAO Base](base.md) (Drop, Index, Count)
14+
* @ref:[MongoDAO CRUD](crud.md) (Create, Insert, Delete)
15+
* @ref:[MongoDAO Search](search.md) (Search, Distinct, Aggregate)
16+
17+
## Creation
18+
19+
A [MongoDatabase](http://mongodb.github.io/mongo-scala-driver/2.3/scaladoc/org/mongodb/scala/MongoDatabase.html) and a collection name is needed.
20+
21+
A Type Parameter is used for automatic Document to Class conversion (case classes needs to be registered).
22+
23+
24+
```scala
25+
object RestaurantDAO extends MongoDAO[Restaurant](database, "restaurants")
26+
```
27+
28+
29+
30+

src/main/paradox/dao/search.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# MongoDAO - Search Functions
2+
3+
## Info
4+
5+
## Demo
6+
7+
### Setup imports
8+
9+
```scala
10+
// Filter helper functions
11+
import com.sfxcode.nosql.mongo.Filter._
12+
// sort helper functions
13+
import com.sfxcode.nosql.mongo.Sort._
14+
// implicits like Document from Map ...
15+
import com.sfxcode.nosql.mongo._
16+
```
17+
18+
### Execute Search
19+
20+
```scala
21+
val females = PersonDAO.find(Map("gender" -> "female"),
22+
sortByKey("name")).resultList()
23+
24+
25+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Features - Aggregation
2+
3+
## Info
4+
5+
MongoDB support an easy to use [Aggregation Handling](https://docs.mongodb.com/manual/aggregation/).
6+
7+
## Demo
8+
9+
### Setup imports
10+
11+
@@snip [RestaurantDatabase.scala](../../../test/scala/com/sfxcode/nosql/mongo/operation/AggregationSpec.scala) { #agg_imports }
12+
13+
### Define stages
14+
15+
@@snip [RestaurantDatabase.scala](../../../test/scala/com/sfxcode/nosql/mongo/operation/AggregationSpec.scala) { #agg_stages }
16+
17+
### Execute Aggregation
18+
19+
@@@ note { title=Important }
20+
21+
In most cases we have to use the RAW attribute, because the aggregation result not follows the case class pattern. RAW returns always Documents instead of case classes.
22+
23+
@@@
24+
25+
@@snip [RestaurantDatabase.scala](../../../test/scala/com/sfxcode/nosql/mongo/operation/AggregationSpec.scala) { #agg_execute }
26+
27+
### Convert Result
28+
29+
For easy result handling, using the implicit Document to Map conversion can be useful.
30+
31+
@@snip [RestaurantDatabase.scala](../../../test/scala/com/sfxcode/nosql/mongo/operation/AggregationSpec.scala) { #agg_convert }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Features - BSON Converter
2+
3+
## Info
4+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Features - Relationships
2+
3+
## Info
4+
5+
Normal Relationship handling is the use of [embedded documents](https://docs.mongodb.com/manual/tutorial/model-embedded-one-to-one-relationships-between-documents/).
6+
However, sometimes there is a need for [relationsips beetween collections](https://docs.mongodb.com/manual/tutorial/model-referenced-one-to-many-relationships-between-documents/). There is a Relations Trait to be used in case classes for easy relationship handling.
7+
8+
## Relations trait
9+
10+
The Relations trait extends DAO case classes with relationship functions.
11+
12+
* relatedRecordForOneToOne (OneToOneRelationship and reference vlue needed)
13+
* relatedRecordsForOneToMany (OneToManyRelationship and reference vlue needed)
14+
15+
## Demo
16+
17+
Simple Setup.
18+
19+
* User Collection(should have one login and multiple friends)
20+
* Login Collection
21+
* Friend Collection
22+
23+
```scala
24+
25+
case class User(id: Long, name: String, loginId: String)
26+
case class Login(id: String, email: String, password: String)
27+
case class Friend(id: Long, name: String, userId: Long)
28+
29+
object UserDAO extends MongoDAO[User](database, "user")
30+
object LoginDAO extends MongoDAO[Login](database, "login")
31+
object FriendDAO extends MongoDAO[Friend](database, "friend")
32+
33+
```
34+
35+
For relationship setup we create two Relationships in the UserDAO.
36+
37+
* OneToOne loginRelation (LoginDAO, key is id in user collection)
38+
* OneToMany friendsRelation (FriendDAO, key is userId in friend collection)
39+
40+
@@snip [RestaurantDatabase.scala](../../../test/scala/com/sfxcode/nosql/mongo/relation/RelationDemoDatabase.scala) { #user_dao }
41+
42+
43+
We extend the User case class with the Relations trait and add relation specific functions.
44+
45+
* login (create an Option of Login)
46+
* friends (create a List of Friend)
47+
48+
@@snip [RestaurantDatabase.scala](../../../test/scala/com/sfxcode/nosql/mongo/relation/RelationDemoDatabase.scala) { #user_class }
49+
50+

src/main/paradox/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ Documentation for [MongoDB](https://docs.mongodb.com/)
3131
- [Tutorial Setup](tutorial/setup.md)
3232
- [Tutorial Database](tutorial/database.md)
3333
- [Tutorial Application](tutorial/application.md)
34+
- [MongoDAO Overview](dao/mongo_dao.md)
35+
- [MongoDAO Base](dao/base.md)
36+
- [MongoDAO CRUD](dao/crud.md)
37+
- [MongoDAO Search](dao/search.md)
38+
- [Feature Aggregation](features/aggregation.md)
39+
- [Feature Relationships](features/relationships.md)
3440
- [Changes ](changes.md)
3541

3642
@@@

src/main/paradox/tutorial/application.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ This Demo Application should demonstrate the basic usage of simple-mongo.
66

77
## Imports
88

9-
@@snip [RestaurantDatabase.scala](../../../test/scala/com/sfxcode/nosql/mongo/restaurant/RestaurantApp.scala) { #import }
9+
@@snip [RestaurantDatabase.scala](../../../test/scala/com/sfxcode/nosql/mongo/demo/restaurant/RestaurantDemoDatabase.scala) { #import }
1010

1111

1212
## Simple Application
1313

14-
@@snip [RestaurantDatabase](../../../test/scala/com/sfxcode/nosql/mongo/restaurant/RestaurantApp.scala) { #app }
14+
@@snip [RestaurantApp](../../../test/scala/com/sfxcode/nosql/mongo/demo/restaurant/RestaurantDemoApp.scala) { #app }
1515

src/main/paradox/tutorial/database.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ The database provides static import to our [DAO](https://en.wikipedia.org/wiki/D
66

77
## Imports
88

9-
@@snip [RestaurantDatabase.scala](../../../test/scala/com/sfxcode/nosql/mongo/restaurant/RestaurantDatabase.scala) { #import }
9+
@@snip [RestaurantDatabase.scala](../../../test/scala/com/sfxcode/nosql/mongo/demo/restaurant/RestaurantDemoDatabase.scala) { #import }
1010

1111

1212
## case classes for document representation
1313

14-
@@snip [RestaurantDatabase](../../../test/scala/com/sfxcode/nosql/mongo/restaurant/RestaurantDatabase.scala) { #case_classes }
14+
@@snip [RestaurantDatabase](../../../test/scala/com/sfxcode/nosql/mongo/demo/restaurant/RestaurantDemoDatabase.scala) { #case_classes }
1515

1616
## Registry and database
1717

1818
scala-mongo-driver use a registry pattern for deserialization
1919

20-
@@snip [RestaurantDatabase](../../../test/scala/com/sfxcode/nosql/mongo/restaurant/RestaurantDatabase.scala) { #registry }
20+
@@snip [RestaurantDatabase](../../../test/scala/com/sfxcode/nosql/mongo/demo/restaurant/RestaurantDemoDatabase.scala) { #registry }
2121

2222
## Restaurant DAO
2323

24-
@@snip [RestaurantDatabase](../../../test/scala/com/sfxcode/nosql/mongo/restaurant/RestaurantDatabase.scala) { #dao }
24+
@@snip [RestaurantDatabase](../../../test/scala/com/sfxcode/nosql/mongo/demo/restaurant/RestaurantDemoDatabase.scala) { #dao }
2525

2626
## Usage
2727
* Create Database object

0 commit comments

Comments
 (0)