How to incorporate custom types with the auto-generated case class / companion object files?

116 Views Asked by At

I'm building an application with alayered architecture pattern, specifically controller -> service -> repository, in which the repository layer ideally performs the database transactions and returns it back up the chain. I also have a models package that contains case classes for purely defining data types from the database.

I used the scalikejdbcGen <table_name> <class_name> command in SBT to generate my repository file, which contains a case class and a companion object. The problem is that I would like to define a custom type that lives outside of this that is simply for defining the data type. Ideally, this auto-generated repository file should just import this and use as a return type in the transactions. The problem is that an alias-related syntax error appears.

The setup I would like to have is something like this:

models/Profile:

case class Profile(profileId: Int, name: String)

repositories/ProfileRepository:

object ProfileRepository extends SQLSyntaxSupport[Profile] {

    override val autoSession = AutoSession

    override val tableName = "profile"
    override val columns = Seq("profile_id", "name")

    def apply(p: SyntaxProvider[Profile])(rs: WrappedResultSet): Profile = apply(p.resultName)(rs)

    def apply(p: ResultName[Profile])(rs: WrappedResultSet): Profile = new Profile(
        profileId = rs.get(p.profileId),
        name = rs.get(p.name)
    )

    val p = Profile.syntax("p")

    def count(): Long = DB readOnly { implicit session =>
        withSQL(select(sqls.count).from(Profile as p)).map(rs => rs.long(1)).single().apply().get
    }
}

It'd be nice to be able to simply specify a return type, something like Seq[Profile], and use the repository just for database interaction. In that way, (if I needed to) I could use this type for defining other functions throughout the rest of the application. I am sure this is an easy problem to solve, but I just cannot find the solution. How can I go about creating a repository for just db interaction that uses a separate case class for the typing?

Thanks for the help!

0

There are 0 best solutions below