Skip to content

Commit

Permalink
MongoDB: Switch to kx.ser
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Jan 14, 2024
1 parent 939732d commit cad7979
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 166 deletions.
18 changes: 11 additions & 7 deletions data-adapters/adapter-mongodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ This includes some codecs, for commonly-used types - which you can use in your o

To switch to the MongoDB data adapter follow these steps:

1. Set the `ADAPTER_MONGODB_URI` environmental variable to a MongoDB connection string.
2. Use the `mongoDB` function to set up the data adapter.
1. Add the MongoDB dependencies to your project. You can get the latest version number
[from Maven Central](https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-kotlin-sync):
- `org.mongodb:mongodb-driver-kotlin-coroutine`
- `org.mongodb:bson-kotlinx`
2. Set the `ADAPTER_MONGODB_URI` environmental variable to a MongoDB connection string.
3. Use the `mongoDB` function to set up the data adapter.

```kotlin
suspend fun main() {
val bot = ExtensibleBot(System.getenv("TOKEN")) {
mongoDB()
}
val bot = ExtensibleBot(System.getenv("TOKEN")) {
mongoDB()
}

bot.start()
bot.start()
}
```

3. If you use MongoDB elsewhere in your project, you can use the provided codecs to handle these types:
4. If you use MongoDB elsewhere in your project, you can use the provided codecs to handle these types:
- `DateTimePeriod` (kotlinx Datetime)
- `Instant` (Kotlinx Datetime)
- `Snowflake` (Kord)
Expand Down
1 change: 1 addition & 0 deletions data-adapters/adapter-mongodb/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
implementation(libs.kx.coro)
implementation(libs.bundles.logging)
implementation(libs.mongodb)
implementation(libs.mongodb.bson.kotlinx)

implementation(project(":kord-extensions"))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.kotlindiscord.kord.extensions.adapters.mongodb

import com.kotlindiscord.kord.extensions.adapters.mongodb.codecs.*
import com.kotlindiscord.kord.extensions.adapters.mongodb.db.Metadata
import com.kotlindiscord.kord.extensions.storage.StorageType
import dev.kord.common.entity.Snowflake
import kotlinx.datetime.DateTimePeriod
import kotlinx.datetime.Instant
import org.bson.codecs.configuration.CodecRegistries
import org.bson.codecs.configuration.CodecRegistry
import org.bson.codecs.kotlinx.KotlinSerializerCodec

public val kordExCodecRegistry: CodecRegistry = CodecRegistries.fromCodecs(
DateTimePeriodCodec,
InstantCodec,
SnowflakeCodec,
StorageTypeCodec,
Metadata.codec,

// Required b/c the BSON codec library doesn't support standard polymorphism.
ConfigStorageTypeCodec,
DataStorageTypeCodec,
KotlinSerializerCodec.create<DateTimePeriod>(),
KotlinSerializerCodec.create<Instant>(),
KotlinSerializerCodec.create<Snowflake>(),
KotlinSerializerCodec.create<StorageType>(),
)

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ import org.bson.codecs.pojo.annotations.BsonId
@Serializable
@Suppress("ConstructorParameterNaming", "DataClassShouldBeImmutable")
internal data class AdaptedData(
@BsonId
override val _id: String,
@BsonId
override val _id: String,

val identifier: String,
val identifier: String,

val type: StorageType? = null,
val type: StorageType? = null,

val channel: Snowflake? = null,
val guild: Snowflake? = null,
val message: Snowflake? = null,
val user: Snowflake? = null,
val channel: Snowflake? = null,
val guild: Snowflake? = null,
val message: Snowflake? = null,
val user: Snowflake? = null,

var data: String,
var data: String,
) : Entity<String>
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ package com.kotlindiscord.kord.extensions.adapters.mongodb.db

@Suppress("VariableNaming", "PropertyName")
internal interface Entity<ID> {
val _id: ID
val _id: ID
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,25 @@ import com.mongodb.client.model.Filters.eq
import com.mongodb.client.model.ReplaceOptions
import com.mongodb.client.result.UpdateResult
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable
import org.bson.codecs.pojo.annotations.BsonId
import org.bson.codecs.kotlinx.KotlinSerializerCodec

@Serializable
@Suppress("DataClassContainsFunctions", "DataClassShouldBeImmutable")
internal data class Metadata(
@BsonId
@Contextual
override val _id: String,

var version: Int,
) : Entity<String> {

suspend inline fun save(): UpdateResult =
Companion.save(this)

companion object {
val codec = KotlinSerializerCodec.create<Metadata>()

const val COLLECTION_NAME: String = "metadata"

private val Filters = object {
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
logback-groovy = { module = "io.github.virtualdogbert:logback-groovy-config", version.ref = "logback-groovy" }
kotlin-logging = { module = "io.github.oshai:kotlin-logging", version.ref = "logging" }
mongodb = { module = "org.mongodb:mongodb-driver-kotlin-coroutine", version.ref = "mongodb" }
mongodb-bson-kotlinx = { module = "org.mongodb:bson-kotlinx", version.ref = "mongodb" }
pf4j = { module = "org.pf4j:pf4j", version.ref = "pf4j" }
semver = { module = "io.github.z4kn4fein:semver", version.ref = "semver" }
sentry = { module = "io.sentry:sentry", version.ref = "sentry" }
Expand Down

0 comments on commit cad7979

Please sign in to comment.