Skip to content

Commit 1c0774d

Browse files
committed
CRUD Refactoring
1 parent 6572deb commit 1c0774d

File tree

2 files changed

+103
-24
lines changed

2 files changed

+103
-24
lines changed
Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,116 @@
11
package com.sfxcode.nosql.mongo.operation
22

33
import com.sfxcode.nosql.mongo.{ Converter, _ }
4-
import org.bson.conversions.Bson
4+
import org.mongodb.scala.bson.conversions.Bson
55
import org.mongodb.scala.model.Filters._
6+
import org.mongodb.scala.model._
67
import org.mongodb.scala.result.{ DeleteResult, UpdateResult }
7-
import org.mongodb.scala.{ Completed, Observable, Observer }
8+
import org.mongodb.scala.{ Completed, Observable }
89

910
import scala.reflect.ClassTag
1011

1112
abstract class Crud[A]()(implicit ct: ClassTag[A]) extends Base[A] {
1213

14+
// create
1315
def insertOne(value: A): Observable[Completed] = coll.insertOne(value)
1416

15-
def insert(value: A, observer: Observer[Completed] = new SimpleCompletedObserver[Completed]): Unit = {
16-
insertOne(value).subscribe(observer)
17-
}
17+
def insertOneResult(value: A): Completed = coll.insertOne(value)
18+
19+
def insertOne(value: A, options: InsertOneOptions): Observable[Completed] = coll.insertOne(value, options)
1820

19-
def insertResult(value: A): Completed = insertOne(value)
21+
def insertOneResult(value: A, options: InsertOneOptions): Completed = insertOne(value, options)
2022

2123
def insertMany(values: Seq[A]): Observable[Completed] = coll.insertMany(values)
2224

23-
def insertValues(values: Seq[A], observer: Observer[Completed] = new SimpleCompletedObserver[Completed]): Unit = {
24-
insertMany(values).subscribe(observer)
25-
}
25+
def insertManyResult(values: Seq[A]): Completed = insertMany(values)
26+
27+
def insertMany(values: Seq[A], options: InsertManyOptions): Observable[Completed] = coll.insertMany(values, options)
2628

27-
def insertValuesResult(values: Seq[A]): Completed = insertMany(values)
29+
def insertManyResult(values: Seq[A], options: InsertManyOptions): Completed = insertMany(values, options)
30+
31+
// update
2832

2933
def replaceOne(value: A): Observable[UpdateResult] = {
3034
val document = Converter.toDocument(value)
3135
val oid = document.get("_id").get
3236
coll.replaceOne(equal("_id", oid), value)
3337
}
3438

35-
def update(value: A, observer: Observer[UpdateResult] = new SimpleCompletedObserver[UpdateResult]): Unit = {
36-
replaceOne(value).subscribe(observer)
39+
def replaceOneResult(value: A): UpdateResult = replaceOne(value)
40+
41+
def replaceOne(value: A, options: ReplaceOptions): Observable[UpdateResult] = {
42+
val document = Converter.toDocument(value)
43+
val oid = document.get("_id").get
44+
coll.replaceOne(equal("_id", oid), value, options)
3745
}
3846

39-
def updateResult(value: A): UpdateResult = replaceOne(value)
47+
def replaceOneResult(value: A, options: ReplaceOptions): UpdateResult = replaceOne(value, options)
48+
49+
def replaceOne(filter: Bson, value: A): Observable[UpdateResult] =
50+
coll.replaceOne(filter, value)
51+
52+
def replaceOneResult(filter: Bson, value: A): UpdateResult = replaceOne(filter, value)
53+
54+
def replaceOne(filter: Bson, value: A, options: ReplaceOptions): Observable[UpdateResult] =
55+
coll.replaceOne(filter, value, options)
56+
57+
def replaceOneResult(filter: Bson, value: A, options: ReplaceOptions): UpdateResult = replaceOne(filter, value, options)
58+
59+
def updateOne(filter: Bson, update: Bson): Observable[UpdateResult] =
60+
coll.updateOne(filter, update)
61+
62+
def updateOneResult(filter: Bson, update: Bson): UpdateResult =
63+
updateOne(filter, update)
64+
65+
def updateOne(filter: Bson, update: Bson, options: UpdateOptions): Observable[UpdateResult] =
66+
coll.updateOne(filter, update, options)
67+
68+
def updateOneResult(filter: Bson, update: Bson, options: UpdateOptions): UpdateResult =
69+
updateOne(filter, update, options)
70+
71+
def updateMany(filter: Bson, update: Bson): Observable[UpdateResult] =
72+
coll.updateMany(filter, update)
73+
74+
def updateManyResult(filter: Bson, update: Bson): UpdateResult =
75+
updateMany(filter, update)
76+
77+
def updateMany(filter: Bson, update: Bson, options: UpdateOptions): Observable[UpdateResult] =
78+
coll.updateMany(filter, update, options)
79+
80+
def updateManyResult(filter: Bson, update: Bson, options: UpdateOptions): UpdateResult =
81+
updateMany(filter, update, options)
82+
83+
// delete
4084

4185
def deleteOne(filter: Bson): Observable[DeleteResult] = coll.deleteOne(filter)
4286

43-
def deleteResult(filter: Bson): DeleteResult = deleteOne(filter)
87+
def deleteOneResult(filter: Bson): DeleteResult = deleteOne(filter)
4488

45-
def delete(filter: Bson, observer: Observer[DeleteResult] = new SimpleCompletedObserver[DeleteResult]): Unit = {
46-
deleteOne(filter)
47-
}
89+
def deleteOne(filter: Bson, options: DeleteOptions): Observable[DeleteResult] = coll.deleteOne(filter, options)
90+
91+
def deleteOneResult(filter: Bson, options: DeleteOptions): DeleteResult = deleteOne(filter, options)
4892

4993
def deleteOne(value: A): Observable[DeleteResult] = {
5094
val oid = Converter.toDocument(value).get("_id").get
5195
coll.deleteOne(equal("_id", oid))
5296
}
5397

54-
def deleteByValueResult(value: A): DeleteResult = deleteOne(value)
55-
56-
def deleteByValue(value: A, observer: Observer[DeleteResult] = new SimpleCompletedObserver[DeleteResult]): Unit = {
57-
val oid = Converter.toDocument(value).get("_id").get
58-
val filter = equal("_id", oid)
59-
deleteOne(filter)
60-
}
98+
def deleteOneResult(value: A): DeleteResult = deleteOne(value)
6199

62100
def deleteMany(filter: Bson): Observable[DeleteResult] = coll.deleteMany(filter)
63101

64102
def deleteManyResult(filter: Bson): DeleteResult = deleteMany(filter)
65103

104+
def deleteMany(filter: Bson, options: DeleteOptions): Observable[DeleteResult] = coll.deleteMany(filter, options)
105+
106+
def deleteManyResult(filter: Bson, options: DeleteOptions): DeleteResult = deleteMany(filter, options)
107+
66108
def deleteAll(): Observable[DeleteResult] = deleteMany(Map())
67109

68110
def deleteAllResult(): DeleteResult = deleteAll()
69111

112+
def deleteAll(options: DeleteOptions): Observable[DeleteResult] = deleteMany(Map(), options)
113+
114+
def deleteAllResult(options: DeleteOptions): DeleteResult = deleteAll(options)
115+
70116
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.sfxcode.nosql.mongo.operation
2+
3+
import com.sfxcode.nosql.mongo.Converter
4+
import org.mongodb.scala.bson.conversions.Bson
5+
import org.mongodb.scala.model.Filters.equal
6+
import org.mongodb.scala.result.{ DeleteResult, UpdateResult }
7+
import org.mongodb.scala.{ Completed, Observer }
8+
9+
trait CrudObserver[A] extends Crud[A] {
10+
11+
def insertValue(value: A, observer: Observer[Completed] = new SimpleCompletedObserver[Completed]): Unit = {
12+
insertOne(value).subscribe(observer)
13+
}
14+
15+
def insertValues(values: Seq[A], observer: Observer[Completed] = new SimpleCompletedObserver[Completed]): Unit = {
16+
insertMany(values).subscribe(observer)
17+
}
18+
19+
def replaceValue(value: A, observer: Observer[UpdateResult] = new SimpleCompletedObserver[UpdateResult]): Unit = {
20+
replaceOne(value).subscribe(observer)
21+
}
22+
23+
def deleteValue(value: A, observer: Observer[DeleteResult] = new SimpleCompletedObserver[DeleteResult]): Unit = {
24+
val oid = Converter.toDocument(value).get("_id").get
25+
val filter = equal("_id", oid)
26+
deleteOne(filter)
27+
}
28+
29+
def deleteValues(filter: Bson, observer: Observer[DeleteResult] = new SimpleCompletedObserver[DeleteResult]): Unit = {
30+
deleteMany(filter)
31+
}
32+
33+
}

0 commit comments

Comments
 (0)