|
| 1 | +package com.avsystem.commons |
| 2 | +package mongo.typed |
| 3 | + |
| 4 | +import com.avsystem.commons.mongo.BsonValueInput |
| 5 | +import com.avsystem.commons.serialization.GenCodec |
| 6 | +import com.mongodb._ |
| 7 | +import com.mongodb.connection.ClusterDescription |
| 8 | +import com.mongodb.reactivestreams.client.{MongoClient, MongoClients} |
| 9 | +import monix.eval.Task |
| 10 | +import monix.reactive.Observable |
| 11 | +import org.bson.Document |
| 12 | + |
| 13 | +import java.io.Closeable |
| 14 | + |
| 15 | +object TypedMongoClient { |
| 16 | + def apply(): TypedMongoClient = |
| 17 | + new TypedMongoClient(MongoClients.create()) |
| 18 | + |
| 19 | + def apply(connectionString: String): TypedMongoClient = |
| 20 | + new TypedMongoClient(MongoClients.create(connectionString)) |
| 21 | + |
| 22 | + def apply(connectionString: ConnectionString): TypedMongoClient = |
| 23 | + new TypedMongoClient(MongoClients.create(connectionString)) |
| 24 | + |
| 25 | + def apply(settings: MongoClientSettings): TypedMongoClient = |
| 26 | + new TypedMongoClient(MongoClients.create(settings)) |
| 27 | + |
| 28 | + def apply( |
| 29 | + connectionString: ConnectionString, |
| 30 | + driverInformation: MongoDriverInformation, |
| 31 | + ): TypedMongoClient = |
| 32 | + new TypedMongoClient(MongoClients.create(connectionString, driverInformation)) |
| 33 | + |
| 34 | + def apply( |
| 35 | + settings: MongoClientSettings, |
| 36 | + driverInformation: MongoDriverInformation, |
| 37 | + ): TypedMongoClient = |
| 38 | + new TypedMongoClient(MongoClients.create(settings, driverInformation)) |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * A better-typed wrapper over [[MongoClient]]. Uses Monix [[Task]] and [[Observable]] instead of |
| 43 | + * [[org.reactivestreams.Publisher]]. Returns similar better-typed wrappers for database and client session objects. |
| 44 | + */ |
| 45 | +class TypedMongoClient( |
| 46 | + val nativeClient: MongoClient, |
| 47 | + val clientSession: OptArg[TypedClientSession] = OptArg.Empty, |
| 48 | +) extends TypedMongoUtils with Closeable { |
| 49 | + private val sessionOrNull = clientSession.toOpt.map(_.nativeSession).orNull |
| 50 | + |
| 51 | + def withSession(session: TypedClientSession): TypedMongoClient = |
| 52 | + new TypedMongoClient(nativeClient, session) |
| 53 | + |
| 54 | + def getDatabase(name: String): TypedMongoDatabase = |
| 55 | + new TypedMongoDatabase(nativeClient.getDatabase(name)) |
| 56 | + |
| 57 | + def listDatabaseNames: Observable[String] = |
| 58 | + multi(optionalizeFirstArg(nativeClient.listDatabaseNames(sessionOrNull))) |
| 59 | + |
| 60 | + def listDatabases: Observable[Document] = |
| 61 | + multi(optionalizeFirstArg(nativeClient.listDatabases(sessionOrNull))) |
| 62 | + |
| 63 | + def listDatabases[T: GenCodec]: Observable[T] = |
| 64 | + listDatabases.map(doc => BsonValueInput.read[T](doc.toBsonDocument)) |
| 65 | + |
| 66 | + //TODO: `watch` methods |
| 67 | + |
| 68 | + def startSession( |
| 69 | + options: ClientSessionOptions = ClientSessionOptions.builder().build(), |
| 70 | + ): Task[TypedClientSession] = |
| 71 | + single(nativeClient.startSession(options)).map(new TypedClientSession(_)) |
| 72 | + |
| 73 | + /** |
| 74 | + * Executes some code in context of a MongoDB client session. The session is closed afterwards. |
| 75 | + * |
| 76 | + * Note: in order for actual MongoDB operations to be associated with the session, you need to use |
| 77 | + * `withSession` on [[TypedMongoClient]], [[TypedMongoDatabase]] or [[TypedMongoCollection]] and use the |
| 78 | + * returned copy of these objects. |
| 79 | + */ |
| 80 | + def inSession[T]( |
| 81 | + options: ClientSessionOptions = ClientSessionOptions.builder().build(), |
| 82 | + )( |
| 83 | + task: TypedClientSession => Task[T], |
| 84 | + ): Task[T] = |
| 85 | + startSession(options).bracket(task)(s => Task(s.close())) |
| 86 | + |
| 87 | + /** |
| 88 | + * Executes some code in context of a MongoDB client session, within a transaction. |
| 89 | + * After the [[Task]] finishes, fails or is cancelled, the transaction is either committed or aborted depending |
| 90 | + * on the outcome and the session is closed. |
| 91 | + * |
| 92 | + * Note: in order for actual MongoDB operations to be associated with the session and the transaction, |
| 93 | + * you need to use `withSession` on [[TypedMongoClient]], [[TypedMongoDatabase]] or [[TypedMongoCollection]] |
| 94 | + * and use the returned copy of these objects. |
| 95 | + */ |
| 96 | + def inTransaction[T]( |
| 97 | + sessionOptions: ClientSessionOptions = ClientSessionOptions.builder().build(), |
| 98 | + transactionOptions: TransactionOptions = TransactionOptions.builder().build(), |
| 99 | + )( |
| 100 | + task: TypedClientSession => Task[T], |
| 101 | + ): Task[T] = |
| 102 | + inSession(sessionOptions)(s => s.inTransaction(transactionOptions)(task(s))) |
| 103 | + |
| 104 | + def clusterDescription(): ClusterDescription = |
| 105 | + nativeClient.getClusterDescription |
| 106 | + |
| 107 | + def close(): Unit = nativeClient.close() |
| 108 | +} |
0 commit comments