https://kotlinlang.org logo
#exposed
Title
# exposed
g

Gunslingor

04/04/2020, 3:55 AM
It took 48 hours to get this right, man it was tough but beautiful when it works... just wish the docs where updated:
Copy code
package com.newsworthy.db.tables

import org.jetbrains.exposed.dao.Entity
import org.jetbrains.exposed.dao.EntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.Table
import org.joda.time.DateTime


val defaultCategories = mutableMapOf<EntityID<String>, String>(
    EntityID("general", Categories) to "Useless and frowned upon",
    EntityID("science", Categories) to "",
    EntityID("technology", Categories) to "",
    EntityID("world", Categories) to "",
    EntityID("local", Categories) to "",
    EntityID("top", Categories) to ""
)

class Category(category: EntityID<String>) : Entity<String>(category){
    companion object : EntityClass<String, Category>(Categories)
    var category by Categories.category
    var description by Categories.description
}

object Categories : IdTable<String>("Categories") {
        val category = varchar("category", 255).default("general").entityId().default(EntityID("unknown", Categories))
        val description = varchar("description", 255).nullable()
        override val primaryKey = PrimaryKey(category, name = "PK_Category_category")
        override val id: Column<EntityID<String>> = category
}
t

tapac

04/06/2020, 7:50 AM
Why do you need
defaultCategories
map? Maybe it's better to have a flag in a table which will show what category is default? Also, I prefer to keep category simple varchar and make id - entityId like:
Copy code
object Categories : IdTable<String>("Categories") {
    val category = varchar("category", 255).default("general")
    val description = varchar("description", 255).nullable()
    override val primaryKey = PrimaryKey(category, name = "PK_Category_category")
    override val id: Column<EntityID<String>> = category.entityId()
}
g

Gunslingor

04/07/2020, 3:15 AM
That's a piece of wisdom there, I really didn't know how that worked and took forever to get that going, but your version makes much more sense. the function docs say "
Copy code
/** Converts the @receiver column to an [EntityID] column. */
I'd enhance:
Copy code
/** Uses the @receiver column to make an [EntityID] column when assigned to a tables Tid field such as... lol. */
I have defaultCategories so that I can insert default records after auto creating the DB via gradle, I also made testCategories for insert/delete tests and such.