Search code examples
postgresqlkotlinkotlin-multiplatformktorkotlin-exposed

How to create array of LocalDate in jetbrains Exposed table


I have a Postgres Table that has a column of "date[]" representing various dates that a user has a specific behavior, meaning each cell can have multiple dates (for example {"2022-10-31", "2022-10-10"}).

This is connected to an API built in Ktor using Exposed library. I'm trying to represent this in Ktor.

The object property is

val user_behaviour_dates: List<LocalDate?>? 

where LocalDate comes from kotlinx.datetime library, so that it can be serialized.

How can I represent this in the Users Table? I have successfully represented another list property that is:

val equipment: List<String?>? 

using this library that supports array for ColumnType as

var equipment = array<String?>("equipment", VarCharColumnType(120)).nullable()

but when I am trying to do this with LocalDate, i get the error that the IDateColumnType (from Exposed library) doesn't have constructors (since its an interface).

Here is the Table. Any ideas?

object UsersTable: Table() {

val userId : Column<Int> = integer("id").autoIncrement()
override val primaryKey: PrimaryKey = PrimaryKey(userId)
val email = varchar("email", 128).uniqueIndex()
val password = varchar("password", 64)
val username = varchar("username", 120)

var equipment = array<String?>("equipment", VarCharColumnType(120)).nullable()

var user_behaviour_dates = array<LocalDate?>("user_behaviour_dates ", IDateColumnType()).nullable()

}


Solution

  • It depends on what you want to achieve. If you want have all data in a single field using array column than you have to ask library author to support it.

    Otherwise I would suggest to create a separate table UsersBehaviorDatesTable and use native Exposed references to manage it. You can check that part of wiki