com.skillw.pouvoir.internal.feature.database.mongodb.MongoContainer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Pouvoir Show documentation
Show all versions of Pouvoir Show documentation
Bukkit Script Engine Plugin.
The newest version!
package com.skillw.pouvoir.internal.feature.database.mongodb
import com.google.gson.Gson
import com.mongodb.client.FindIterable
import com.mongodb.client.MongoCollection
import com.mongodb.client.MongoDatabase
import com.mongodb.client.result.DeleteResult
import com.mongodb.client.result.UpdateResult
import com.skillw.pouvoir.api.feature.database.BaseContainer
import org.bson.Document
import taboolib.module.configuration.util.asMap
/**
* @className MongoContainer
*
* @author Glom
* @date 2023/8/11 23:00 Copyright 2024 Glom.
*/
/**
* MongoContainer -> Collection
*
* @constructor
* @property key String Collection
* @property database MongoDatabase
*/
open class MongoContainer(
final override val key: String,
holder: MongoContainerHolder,
val database: MongoDatabase,
) : BaseContainer(key, holder) {
protected val collection: MongoCollection = database.getCollection(key)
protected val gson = Gson()
fun insert(vararg objects: Any) {
collection.insertMany(objects.map { Document().apply { putAll(gson.toJsonTree(it).asMap()) } })
}
fun delete(map: Map): DeleteResult {
val bson = Document().apply { putAll(gson.toJsonTree(map).asMap()) }
return collection.deleteMany(bson)
}
fun update(target: Map, new: Map): UpdateResult {
val toUpdate = Document().apply { putAll(target) }
val changed = Document().apply { putAll(new) }
return collection.updateMany(toUpdate, changed)
}
fun find(map: Map): FindIterable {
val bson = Document().apply { putAll(map.mapValues { it.value!! }) }
return collection.find(bson)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy