Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit c40b277

Browse files
committed
feat(coroutines): Changed Wrappers to be interfaces
The following are now interfaces: - `MonktClient` - `MonktCollection` - `MonktDatabase` Added utility function `createMonktClient(...)`
1 parent 6638c16 commit c40b277

3 files changed

Lines changed: 61 additions & 6 deletions

File tree

coroutines/src/main/kotlin/MonktClient.kt

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.cufy.monkt
1818
import com.mongodb.ClientSessionOptions
1919
import com.mongodb.connection.ClusterDescription
2020
import com.mongodb.connection.ClusterSettings
21+
import com.mongodb.reactivestreams.client.MongoClients
2122
import com.mongodb.reactivestreams.client.ChangeStreamPublisher
2223
import com.mongodb.reactivestreams.client.ClientSession
2324
import com.mongodb.reactivestreams.client.ListDatabasesPublisher
@@ -27,6 +28,32 @@ import org.cufy.bson.BsonDocument
2728
import org.reactivestreams.Publisher
2829
import java.io.Closeable
2930

31+
/**
32+
* Create a new client with the given connection string.
33+
*
34+
* @param connectionString the connection.
35+
* @return the client.
36+
* @since 2.0.0
37+
* @see MongoClients.create
38+
*/
39+
fun createMonktClient(connectionString: String): MonktClient {
40+
val client = MongoClients.create(connectionString)
41+
return MonktClient(client)
42+
}
43+
44+
/**
45+
* Create a new [MonktClient] instance wrapping
46+
* the given [client] instance.
47+
*
48+
* @param client the client to be wrapped.
49+
* @since 2.0.0
50+
*/
51+
fun MonktClient(client: MongoClient): MonktClient {
52+
return object : MonktClient {
53+
override val client = client
54+
}
55+
}
56+
3057
/**
3158
* A generic-free coroutine dependant wrapper for
3259
* [MongoClient]s
@@ -36,12 +63,14 @@ import java.io.Closeable
3663
* @author LSafer
3764
* @since 2.0.0
3865
*/
39-
open class MonktClient(
66+
interface MonktClient : Closeable {
4067
/**
4168
* The wrapped client.
4269
*/
4370
val client: MongoClient
44-
) : Closeable by client {
71+
72+
override fun close() = client.close()
73+
4574
/**
4675
* Gets the current cluster description.
4776
*

coroutines/src/main/kotlin/MonktCollection.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ import org.cufy.bson.BsonDocument
3131
import org.cufy.bson.bdocument
3232
import org.reactivestreams.Publisher
3333

34+
/**
35+
* Create a new [MonktCollection] instance wrapping
36+
* the given [collection] instance.
37+
*
38+
* @param collection the collection to be wrapped.
39+
* @since 2.0.0
40+
*/
41+
fun MonktCollection(collection: MongoCollection<BsonDocument>): MonktCollection {
42+
return object : MonktCollection {
43+
override val collection = collection
44+
}
45+
}
46+
3447
/**
3548
* A generic-free coroutine dependant wrapper for
3649
* [MongoCollection]s
@@ -40,12 +53,12 @@ import org.reactivestreams.Publisher
4053
* @author LSafer
4154
* @since 2.0.0
4255
*/
43-
open class MonktCollection(
56+
interface MonktCollection {
4457
/**
4558
* The wrapped collection.
4659
*/
4760
val collection: MongoCollection<BsonDocument>
48-
) {
61+
4962
// ignored members
5063
// - documentClass : reflection
5164
// - codecRegistry : reflection

coroutines/src/main/kotlin/MonktDatabase.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ import org.cufy.bson.Bson
2525
import org.cufy.bson.BsonDocument
2626
import org.reactivestreams.Publisher
2727

28+
/**
29+
* Create a new [MonktDatabase] instance wrapping
30+
* the given [database] instance.
31+
*
32+
* @param database the database to be wrapped.
33+
* @since 2.0.0
34+
*/
35+
fun MonktDatabase(database: MongoDatabase): MonktDatabase {
36+
return object : MonktDatabase {
37+
override val database = database
38+
}
39+
}
40+
2841
/**
2942
* A generic-free coroutine dependant wrapper for
3043
* [MongoDatabase]s
@@ -34,12 +47,12 @@ import org.reactivestreams.Publisher
3447
* @author LSafer
3548
* @since 2.0.0
3649
*/
37-
open class MonktDatabase(
50+
interface MonktDatabase {
3851
/**
3952
* The wrapped collection.
4053
*/
4154
val database: MongoDatabase
42-
) {
55+
4356
// ignored members
4457
// - codecRegistry : reflection
4558
// - withCodecRegistry() : reflection

0 commit comments

Comments
 (0)