All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.cedrickring.reactormongodb.database.ReactiveDatabase.kt Maven / Gradle / Ivy

The newest version!
package com.github.cedrickring.reactormongodb.database

import com.github.cedrickring.reactormongodb.collection.ReactiveCollection
import com.github.cedrickring.reactormongodb.database.flux.ListCollectionFlux
import com.mongodb.ReadConcern
import com.mongodb.ReadPreference
import com.mongodb.WriteConcern
import com.mongodb.async.client.MongoDatabase
import org.bson.Document
import org.bson.codecs.configuration.CodecRegistry
import reactor.core.publisher.Flux

class ReactiveDatabase(private var mongoDatabase: MongoDatabase) {

    val nativeDatabase: MongoDatabase
        get() = mongoDatabase

    fun listCollectionNames(): Flux = ListCollectionFlux(this)

    fun createCollection(name: String, errorCallback: (error: Throwable?) -> Unit = {}) {
        mongoDatabase.createCollection(name) { _, throwable -> errorCallback(throwable) }
    }

    fun getCollection(name: String): ReactiveCollection {
        return ReactiveCollection(mongoDatabase.getCollection(name))
    }

    fun  getCollection(name: String, clazz: Class): ReactiveCollection {
        return ReactiveCollection(mongoDatabase.getCollection(name, clazz))
    }

    fun drop(errorCallback: (error: Throwable?) -> Unit = {}) {
        mongoDatabase.drop { _, throwable -> errorCallback(throwable) }
    }

    fun withCodecRegistry(codecRegistry: CodecRegistry): ReactiveDatabase {
        mongoDatabase = mongoDatabase.withCodecRegistry(codecRegistry)
        return this
    }

    fun withReadPreference(readPreference: ReadPreference): ReactiveDatabase {
        mongoDatabase = mongoDatabase.withReadPreference(readPreference)
        return this
    }

    fun withWriteConcern(writeConcern: WriteConcern): ReactiveDatabase {
        mongoDatabase = mongoDatabase.withWriteConcern(writeConcern)
        return this
    }

    fun withReadConcern(readConcern: ReadConcern): ReactiveDatabase {
        mongoDatabase = mongoDatabase.withReadConcern(readConcern)
        return this
    }

}

inline fun  ReactiveDatabase.collection(name: String): ReactiveCollection {
    return this.getCollection(name, T::class.java)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy