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

com.github.cedrickring.reactormongodb.collection.ReactiveCollection.kt Maven / Gradle / Ivy

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

import com.github.cedrickring.reactormongodb.collection.flux.*
import com.github.cedrickring.reactormongodb.collection.mono.*
import com.mongodb.MongoNamespace
import com.mongodb.ReadConcern
import com.mongodb.ReadPreference
import com.mongodb.WriteConcern
import com.mongodb.async.client.MongoCollection
import com.mongodb.bulk.BulkWriteResult
import com.mongodb.client.model.*
import com.mongodb.client.model.changestream.ChangeStreamDocument
import com.mongodb.client.result.DeleteResult
import com.mongodb.client.result.UpdateResult
import org.bson.codecs.configuration.CodecRegistry
import org.bson.conversions.Bson
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import java.util.*

class ReactiveCollection(var nativeCollection: MongoCollection) {

    val namespace: MongoNamespace
        get() = nativeCollection.namespace

    val documentClass: Class
        get() = nativeCollection.documentClass

    val codecRegistry: CodecRegistry
        get() = nativeCollection.codecRegistry

    val writeConcern: WriteConcern
        get() = nativeCollection.writeConcern

    val readConcern: ReadConcern
        get() = nativeCollection.readConcern

    val readPreference: ReadPreference
        get() = nativeCollection.readPreference

    @JvmOverloads
    fun count(filter: Bson? = null, countOptions: CountOptions? = null): Mono =
            CountMono(this, filter, countOptions)

    @JvmOverloads
    fun  distinct(fieldName: String, resultClass: Class, filter: Bson? = null): Flux =
            DistinctFlux(this, fieldName, filter, resultClass)

    @JvmOverloads
    fun  find(resultClass: Class, filter: Bson? = null): Flux =
            FindFlux(this, filter, resultClass)

    @JvmOverloads
    fun find(filter: Bson? = null): Flux =
            FindFlux(this, filter, documentClass)

    fun aggregate(pipeline: List): Flux =
            AggregateFlux(this, pipeline, documentClass)

    fun  aggregate(pipeline: List, resultClass: Class): Flux =
            AggregateFlux(this, pipeline, resultClass)

    fun watch(pipeline: List = Collections.emptyList()): Flux> =
            WatchFlux(this, pipeline, documentClass)

    @JvmOverloads
    fun  watch(pipeline: List = Collections.emptyList(), resultClass: Class): Flux> =
            WatchFlux(this, pipeline, resultClass)

    fun mapReduce(mapFunction: String, reduceFunction: String): Flux =
            MapReduceFlux(this, mapFunction, reduceFunction, documentClass)

    fun  mapReduce(mapFunction: String, reduceFunction: String, resultClass: Class): Flux =
            MapReduceFlux(this, mapFunction, reduceFunction, resultClass)

    @JvmOverloads
    fun bulkWrite(requests: List>, options: BulkWriteOptions = BulkWriteOptions()): Mono =
            BulkWriteMono(this, requests, options)

    @JvmOverloads
    fun insertOne(document: T, insertOneOptions: InsertOneOptions = InsertOneOptions()): Mono =
            InsertOneMono(this, document, insertOneOptions)

    @JvmOverloads
    fun insertMany(documents: List, insertManyOptions: InsertManyOptions = InsertManyOptions()): Mono =
            InsertManyMono(this, documents, insertManyOptions)

    @JvmOverloads
    fun deleteOne(filter: Bson, deleteOptions: DeleteOptions = DeleteOptions()): Mono =
            DeleteMono(this, filter, deleteOptions)

    @JvmOverloads
    fun deleteMany(filter: Bson, deleteOptions: DeleteOptions = DeleteOptions()): Mono =
            DeleteMono(this, filter, deleteOptions, true)

    @JvmOverloads
    fun replaceOne(filter: Bson, replacement: T, replaceOptions: ReplaceOptions = ReplaceOptions()): Mono =
            ReplaceOneMono(this, filter, replacement, replaceOptions)

    @JvmOverloads
    fun updateOne(filter: Bson, update: Bson, updateOptions: UpdateOptions = UpdateOptions()): Mono =
            UpdateMono(this, filter, update, updateOptions)

    @JvmOverloads
    fun updateMany(filter: Bson, update: Bson, updateOptions: UpdateOptions = UpdateOptions()): Mono =
            UpdateMono(this, filter, update, updateOptions, true)

    @JvmOverloads
    fun findOneAndDelete(filter: Bson, findOneAndDeleteOptions: FindOneAndDeleteOptions = FindOneAndDeleteOptions()): Mono =
            FindOneAndDeleteMono(this, filter, findOneAndDeleteOptions)

    @JvmOverloads
    fun findOneAndReplace(filter: Bson, replacement: T, findOneAndReplaceOptions: FindOneAndReplaceOptions = FindOneAndReplaceOptions()): Mono =
            FindOneAndReplaceMono(this, filter, replacement, findOneAndReplaceOptions)

    @JvmOverloads
    fun findOneAndUpdate(filter: Bson, update: Bson, findOneAndUpdateOptions: FindOneAndUpdateOptions = FindOneAndUpdateOptions()): Mono =
            FindOneAndUpdateMono(this, filter, update, findOneAndUpdateOptions)

    fun drop(): Mono {
        return Mono.from {
            nativeCollection.drop { void, throwable -> if (throwable != null) it.onError(throwable) else it.onNext(void) }
        }
    }


    fun  withDocumentClass(clazz: Class): com.github.cedrickring.reactormongodb.collection.ReactiveCollection {
        return com.github.cedrickring.reactormongodb.collection.ReactiveCollection(nativeCollection.withDocumentClass(clazz))
    }

    fun withCodecRegistry(codecRegistry: CodecRegistry): com.github.cedrickring.reactormongodb.collection.ReactiveCollection {
        nativeCollection = nativeCollection.withCodecRegistry(codecRegistry)
        return this
    }

    fun withReadPreference(readPreference: ReadPreference): com.github.cedrickring.reactormongodb.collection.ReactiveCollection {
        nativeCollection = nativeCollection.withReadPreference(readPreference)
        return this
    }

    fun withWriteConcern(writeConcern: WriteConcern): com.github.cedrickring.reactormongodb.collection.ReactiveCollection {
        nativeCollection = nativeCollection.withWriteConcern(writeConcern)
        return this
    }

    fun withReadConcern(readConcern: ReadConcern): com.github.cedrickring.reactormongodb.collection.ReactiveCollection {
        nativeCollection = nativeCollection.withReadConcern(readConcern)
        return this
    }

}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy