Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reafactor/2024 12 schema project replacement #336

Merged
merged 22 commits into from
Dec 16, 2024

Conversation

takapi327
Copy link
Owner

@takapi327 takapi327 commented Dec 13, 2024

Implementation Details

This modification changes the way Table types are constructed using the Schema project.
Below we will look at the construction of the Table type corresponding to the User model.

case class User(
  id: Long,
  name: String,
  age: Option[Int],
)

Before

Until now, we had to create instances of Table directly; the arguments of Table had to be passed the corresponding columns in the same order as the properties possessed by the User class, and the data type of the columns had to be set as well, which was mandatory.

The TableQuery using this table type was implemented using Dynamic, which allows type-safe access, but the development tools could not do the completion.

This method of construction was also a bit slower in compile time than class generation

val userTable = Table[User]("user")(
  column("id", BIGINT, AUTO_INCREMENT, PRIMARY_KEY),
  column("name", VARCHAR(255)),
  column("age", INT.UNSIGNED.DEFAULT(None)),
)    

After

In this modification, Table type generation has been changed to a method of creating a class by extending Table. In addition, the data type of a column is no longer required, but can be set arbitrarily by the implementer.

This change to a construction method similar to that of Slick has made it more familiar to implementers.

class UserTable extends Table[User]("user"):
  def id: Column[Long] = column[Long]("id")
  def name: Column[String] = column[String]("name")
  def age: Column[Option[Int]] = column[Option[Int]]("age")

  override def * : Column[User] = (id *: name *: age).to[User]

The data type of the columns can still be set. This setting is used, for example, when generating a schema using this table class.

class UserTable extends Table[User]("user"):
  def id: Column[Long] = column[Long]("id", BIGINT, AUTO_INCREMENT, PRIMARY_KEY)
  def name: Column[String] = column[String]("name", VARCHAR(255))
  def age: Column[Option[Int]] = column[Option[Int]]("age", INT.UNSIGNED.DEFAULT(None))

  override def * : Column[User] = (id *: name *: age).to[User]

Pull Request Checklist

  • Wrote unit and integration tests
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code formatting by scalafmt (sbt scalafmtAll command execution)
  • Add copyright headers to new files

References

@takapi327 takapi327 added 🔧 refactor Refactoring project:schema Addition and modification of functionality to Schema projects labels Dec 13, 2024
@takapi327 takapi327 added this to the 0.3.0 milestone Dec 13, 2024
@takapi327 takapi327 self-assigned this Dec 13, 2024
@takapi327 takapi327 merged commit c4b09b9 into master Dec 16, 2024
27 checks passed
@takapi327 takapi327 deleted the reafactor/2024-12-Schema-project-replacement branch December 16, 2024 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
project:schema Addition and modification of functionality to Schema projects 🔧 refactor Refactoring
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant