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

jvmMain.dsl.RaptorMongoUpdate.kt Maven / Gradle / Ivy

There is a newer version: 0.27.0
Show newest version
package io.fluidsonic.raptor.mongo

import com.mongodb.client.model.*
import io.fluidsonic.mongo.*
import org.bson.*
import org.bson.conversions.*


internal class RaptorMongoUpdate(
	val filter: Bson?,
	val changes: Bson?,
	val isUpsert: Boolean,
)


@PublishedApi
internal suspend fun  MongoCollection.execute(update: RaptorMongoUpdate): Document? {
	val filter = update.filter ?: BsonDocument()
	val changes = update.changes ?: return find(filter = filter).firstOrNull()

	return findOneAndUpdate(
		filter = filter,
		update = changes,
		options = FindOneAndUpdateOptions().upsert(update.isUpsert).returnDocument(ReturnDocument.AFTER)
	)
}


public suspend inline fun  MongoCollection.updateOne(configure: RaptorMongodbUpdateBuilder.() -> Unit): Document? =
	execute(RaptorMongodbUpdateBuilder().apply(configure).build())


public suspend fun  MongoCollection.updateOneById(id: Any?, configure: RaptorMongodbUpdateBuilder.() -> Unit): Document? =
	updateOne {
		filter { id(id) }

		configure()
	}


public suspend inline fun  MongoCollection.upsertOne(configure: RaptorMongodbUpdateBuilder.() -> Unit): Document =
	execute(RaptorMongodbUpdateBuilder(isUpsert = true).apply(configure).build())!!


public suspend fun  MongoCollection.upsertOneById(id: Any?, configure: RaptorMongodbUpdateBuilder.() -> Unit): Document =
	upsertOne {
		filter { id(id) }

		configure()
	}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy