Skip to content

Commit

Permalink
feature: Better serializable column
Browse files Browse the repository at this point in the history
  • Loading branch information
LichtHund committed Oct 4, 2023
1 parent 7989fdd commit 303d1b4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 46 deletions.
44 changes: 0 additions & 44 deletions app/src/main/kotlin/database/Tables.kt

This file was deleted.

27 changes: 27 additions & 0 deletions app/src/main/kotlin/database/entity/DocsEntity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.triumphteam.docsly.database.entity

import dev.triumphteam.docsly.database.exposed.serializable
import dev.triumphteam.docsly.serializable.DocElement
import org.jetbrains.exposed.dao.LongEntity
import org.jetbrains.exposed.dao.LongEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.sql.Column

public object DocsTable : IdTable<Long>() {
public val guild: Column<Long> = long("guild_id").uniqueIndex()
public val project: Column<String> = text("project").uniqueIndex()
public val version: Column<String> = text("version").uniqueIndex()
public val location: Column<String> = text("location").uniqueIndex()
public val doc: Column<DocElement> = serializable("doc")

override val primaryKey: PrimaryKey = PrimaryKey(guild, version, location)
override val id: Column<EntityID<Long>> = guild.entityId()
}

public class DocEntity(entityId: EntityID<Long>) : LongEntity(entityId) {
public companion object : LongEntityClass<DocEntity>(DocsTable)

public var version: String by DocsTable.version
public var guild: Long by DocsTable.guild
}
8 changes: 6 additions & 2 deletions app/src/main/kotlin/database/exposed/SerializableColumn.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ import com.impossibl.postgres.jdbc.PGArray
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.TextColumnType
import java.sql.Clob
import kotlin.reflect.KClass

private val json = Json
public val json: Json = Json

public inline fun <reified T : Any> Table.serializable(name: String, serializer: KSerializer<T>): Column<T> =
registerColumn<T>(name, SerializableColumnType(T::class, serializer))
registerColumn(name, SerializableColumnType(T::class, serializer))

public inline fun <reified T : Any> Table.serializable(name: String): Column<T> =
registerColumn(name, SerializableColumnType(T::class, json.serializersModule.serializer()))

@Suppress("UNCHECKED_CAST")
public class SerializableColumnType<T : Any>(
Expand Down

0 comments on commit 303d1b4

Please sign in to comment.