|
1 | 1 | package com.sfxcode.nosql.mongo.operation |
2 | 2 |
|
3 | 3 | import com.sfxcode.nosql.mongo.{ Converter, _ } |
4 | | -import org.bson.conversions.Bson |
| 4 | +import org.mongodb.scala.bson.conversions.Bson |
5 | 5 | import org.mongodb.scala.model.Filters._ |
| 6 | +import org.mongodb.scala.model._ |
6 | 7 | import org.mongodb.scala.result.{ DeleteResult, UpdateResult } |
7 | | -import org.mongodb.scala.{ Completed, Observable, Observer } |
| 8 | +import org.mongodb.scala.{ Completed, Observable } |
8 | 9 |
|
9 | 10 | import scala.reflect.ClassTag |
10 | 11 |
|
11 | 12 | abstract class Crud[A]()(implicit ct: ClassTag[A]) extends Base[A] { |
12 | 13 |
|
| 14 | + // create |
13 | 15 | def insertOne(value: A): Observable[Completed] = coll.insertOne(value) |
14 | 16 |
|
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) |
18 | 20 |
|
19 | | - def insertResult(value: A): Completed = insertOne(value) |
| 21 | + def insertOneResult(value: A, options: InsertOneOptions): Completed = insertOne(value, options) |
20 | 22 |
|
21 | 23 | def insertMany(values: Seq[A]): Observable[Completed] = coll.insertMany(values) |
22 | 24 |
|
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) |
26 | 28 |
|
27 | | - def insertValuesResult(values: Seq[A]): Completed = insertMany(values) |
| 29 | + def insertManyResult(values: Seq[A], options: InsertManyOptions): Completed = insertMany(values, options) |
| 30 | + |
| 31 | + // update |
28 | 32 |
|
29 | 33 | def replaceOne(value: A): Observable[UpdateResult] = { |
30 | 34 | val document = Converter.toDocument(value) |
31 | 35 | val oid = document.get("_id").get |
32 | 36 | coll.replaceOne(equal("_id", oid), value) |
33 | 37 | } |
34 | 38 |
|
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) |
37 | 45 | } |
38 | 46 |
|
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 |
40 | 84 |
|
41 | 85 | def deleteOne(filter: Bson): Observable[DeleteResult] = coll.deleteOne(filter) |
42 | 86 |
|
43 | | - def deleteResult(filter: Bson): DeleteResult = deleteOne(filter) |
| 87 | + def deleteOneResult(filter: Bson): DeleteResult = deleteOne(filter) |
44 | 88 |
|
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) |
48 | 92 |
|
49 | 93 | def deleteOne(value: A): Observable[DeleteResult] = { |
50 | 94 | val oid = Converter.toDocument(value).get("_id").get |
51 | 95 | coll.deleteOne(equal("_id", oid)) |
52 | 96 | } |
53 | 97 |
|
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) |
61 | 99 |
|
62 | 100 | def deleteMany(filter: Bson): Observable[DeleteResult] = coll.deleteMany(filter) |
63 | 101 |
|
64 | 102 | def deleteManyResult(filter: Bson): DeleteResult = deleteMany(filter) |
65 | 103 |
|
| 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 | + |
66 | 108 | def deleteAll(): Observable[DeleteResult] = deleteMany(Map()) |
67 | 109 |
|
68 | 110 | def deleteAllResult(): DeleteResult = deleteAll() |
69 | 111 |
|
| 112 | + def deleteAll(options: DeleteOptions): Observable[DeleteResult] = deleteMany(Map(), options) |
| 113 | + |
| 114 | + def deleteAllResult(options: DeleteOptions): DeleteResult = deleteAll(options) |
| 115 | + |
70 | 116 | } |
0 commit comments