diff --git a/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoClientImpl.kt b/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoClientImpl.kt index 3950a1bd..50fe50d7 100644 --- a/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoClientImpl.kt +++ b/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoClientImpl.kt @@ -28,9 +28,8 @@ private class CoroutineMongoClientImpl( override fun asOfficial(): MongoClient = inner - override fun database(name: String): CoroutineMongoDatabase { - TODO("Not yet implemented") - } + override fun database(name: String): CoroutineMongoDatabase = + inner.getDatabase(name).asKtMongo() override fun toString(): String = "CoroutineMongoClient()" diff --git a/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoDatabase.kt b/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoDatabase.kt index 943960b9..ce74bb05 100644 --- a/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoDatabase.kt +++ b/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoDatabase.kt @@ -14,6 +14,9 @@ * limitations under the License. */ +@file:JvmMultifileClass +@file:JvmName("KtMongo") + package opensavvy.ktmongo.coroutines import opensavvy.ktmongo.api.MongoClient @@ -46,6 +49,8 @@ import kotlin.reflect.typeOf * ### External resources * * - [Official documentation](https://www.mongodb.com/docs/manual/core/databases-and-collections/) + * + * @see asKtMongo Convert an existing instance from the official Kotlin driver. */ interface CoroutineMongoDatabase : MongoDatabase { diff --git a/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoDatabaseImpl.kt b/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoDatabaseImpl.kt new file mode 100644 index 00000000..3bfe6f11 --- /dev/null +++ b/driver-coroutines/src/jvmMain/kotlin/CoroutineMongoDatabaseImpl.kt @@ -0,0 +1,67 @@ +/* + * 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.MongoDatabase +import opensavvy.ktmongo.dsl.LowLevelApi +import kotlin.reflect.KType + +private class CoroutineMongoDatabaseImpl( + private val inner: MongoDatabase, +) : CoroutineMongoDatabase { + + override fun asOfficial(): MongoDatabase = + inner + + @LowLevelApi + override fun collection(name: String, type: KType): CoroutineMongoCollection { + TODO("Not yet implemented") + } + + override val name: String + get() = inner.name + + override fun toString(): String = + "CoroutineMongoDatabase($name)" +} + +/** + * Instantiates a KtMongo [CoroutineMongoDatabase] using an existing client from the official Kotlin driver. + * + * ### Example + * + * ```kotlin + * import com.mongodb.kotlin.client.coroutine.MongoClient + * + * fun main() = runBlocking { + * val client = MongoClient.create(/* … */) + * val database = client.database("my-app") + * .asKtMongo() + * + * val users = database.collection("users") + * + * println("Users: ${users.count()}") + * } + * ``` + */ +fun MongoDatabase.asKtMongo(): CoroutineMongoDatabase = + CoroutineMongoDatabaseImpl( + inner = this, + )