MongoDB Revamp
This update introduces extremely easy ways of using MongoDB!
Just like before, you can setup your MongoDB connection in your Twilight builder:
override fun onEnable() {
twilight(this) {
mongo() // you can just use this if you're using env for your URI & database, or:
mongo {
uri = "mongodb://localhost:27017"
database = "test"
}
}
}
Below are examples of how you can use the new API additions.
class Profile(
@field:Id val id: UUID,
val name: String
) : MongoSerializable
val profile = Profile(UUID.randomUUID(), "Name")
profile.save()
// this returns a CompletableFuture of the UpdateResult
// or we could do profile.saveSync() if we need it to be sync, does not return wrapped by a CompletableFuture
profile.delete()
// this returns a CompletableFuture of the DeleteResult
// same as save, can do profile.deleteSync() for sync, does not return wrapped by a CompletableFuture
val collection = MongoDB.collection<Profile>() // by default this assumes the name of the collection is the plural camel case of the type, f.x. Profile -> profiles, SomeExampleThing -> someExampleThings
// you can specify the name of the collection if you wish it to be different like so
val collection = MongoDB.collection<Profile>("myCollection")
collection.find() // returns a CompletableFuture<MongoIterable<Profile>>
collection.find(BsonFilter) // returns a CompletableFuture<MongoIterable<Profile>>
collection.findById(id) // id must be the same type as the field marked as the id on the class, returns a CompletableFuture<MongoIterable<Profile>>
collection.delete(BsonFilter) // returns a CompletableFuture<DeleteResult>
collection.deleteById(id) // id must be the same type as the field marked as the id on the class, returns a CompletableFuture<DeleteResult>
// all of these have sync versions which follow the same pattern, f.x. collection.findSync(), where the return value is the same as the async version, just not wrapped by a CompletableFuture
Get started
Include Twilight in your Gradle project from our repository:
repositories {
maven("https://repo.flyte.gg/releases")
}
dependencies {
implementation("gg.flyte:twilight:1.1.0")
}