From 5284d6838cd4ec2a7602c3f298bfb3165b999549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Mon, 22 Jun 2026 19:54:16 +0200 Subject: [PATCH] feat(driver-coroutines): Implement CoroutineMongoClient --- .../jvmMain/kotlin/CoroutineMongoClient.kt | 7 +- .../kotlin/CoroutineMongoClientImpl.kt | 84 +++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 driver-coroutines/src/jvmMain/kotlin/CoroutineMongoClientImpl.kt diff --git a/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoClient.kt b/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoClient.kt index c7b4ae67..521d0cb4 100644 --- a/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoClient.kt +++ b/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoClient.kt @@ -14,6 +14,9 @@ * limitations under the License. */ +@file:JvmMultifileClass +@file:JvmName("KtMongo") + package opensavvy.ktmongo.coroutines import opensavvy.ktmongo.api.MongoClient @@ -46,7 +49,7 @@ import opensavvy.ktmongo.api.MongoClient * ) * * fun main() = runBlocking { - * val client: CoroutineMongoClient = // TODO + * val client = CoroutineMongoClient("mongodb://localhost:27017") * * val database = client.database("my-app") * val users = database.collection("users") @@ -54,6 +57,8 @@ import opensavvy.ktmongo.api.MongoClient * println("The database contains ${users.count()} users.") * } * ``` + * + * @see asKtMongo Convert an existing instance from the official Kotlin driver. */ interface CoroutineMongoClient : MongoClient { diff --git a/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoClientImpl.kt b/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoClientImpl.kt new file mode 100644 index 00000000..3950a1bd --- /dev/null +++ b/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoClientImpl.kt @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2026, OpenSavvy and contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@file:JvmMultifileClass +@file:JvmName("KtMongo") + +package opensavvy.ktmongo.coroutines + +import com.mongodb.kotlin.client.coroutine.MongoClient + +private class CoroutineMongoClientImpl( + private val inner: MongoClient, +) : CoroutineMongoClient { + + override fun asOfficial(): MongoClient = + inner + + override fun database(name: String): CoroutineMongoDatabase { + TODO("Not yet implemented") + } + + override fun toString(): String = + "CoroutineMongoClient()" +} + +/** + * Instantiates a KtMongo [CoroutineMongoClient] using an existing client from the official Kotlin driver. + * + * This method allows taking advantage of the full configuration power of the official client. + * + * ### Example + * + * ```kotlin + * fun main() = runBlocking { + * val client = CoroutineMongoClient() + * + * val database = client.database("my-app") + * val users = database.collection("users") + * + * println("Users: ${users.count()}") + * } + * ``` + */ +fun CoroutineMongoClient( + connectionString: String = "mongodb://localhost:27017", +): CoroutineMongoClient = + CoroutineMongoClientImpl(MongoClient.create(connectionString)) + +/** + * Instantiates a KtMongo [CoroutineMongoClient] using an existing client from the official Kotlin driver. + * + * This method allows taking advantage of the full configuration power of the official client. + * + * ### Example + * + * ```kotlin + * import com.mongodb.kotlin.client.coroutine.MongoClient + * + * fun main() = runBlocking { + * val client = MongoClient.create(/* … */) + * .asKtMongo() + * + * val database = client.database("my-app") + * val users = database.collection("users") + * + * println("Users: ${users.count()}") + * } + * ``` + */ +fun MongoClient.asKtMongo(): CoroutineMongoClient = + CoroutineMongoClientImpl(this) -- 2.51.2