improve on inserting optional values
sviezypan opened this issue · 0 comments
sviezypan commented
Let's have a table with nullable column:
val persons = ((string("name") ++ (int("age") @@ nullable)).table("person")
val (name , age) = persons.columns
result type of age
is then Expr[F, T, Option[Int]]
When inserting user defined case class its easy to insert None
case class Person(name: String, age: Option[Int])
implicit val schema = DeriveSchema.gen[Person]
val data = List(Person("my_name", None), Person("other_name", Some(42)))
insertInto(persons)(name ++ age).values(data)
however in case we are inserting tuples and we want to insert Some or None to age
we need to cast it to Option[LocalDate]
val data = List(("my_name", Option.empty[Int])), ("other_name", Option(42)))
insertInto(persons)(name ++ age).values(data)
This is because of strict type checks that accompany insert module. ZIO schema for tuple is derived automatically - in this case Schema[(String, Option[Int])] - but based on inserted values, compiler search for Schema[(String, None)] or Schema[(String, Some[Int])].
So we need to play around with inserting tuples more, ideally we want to be able to do:
val persons = List(("my_name", None), ("other_name", Some(42)))
insertInto(persons)(name ++ age).values(persons)