Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
jatcwang
@jatcwang:matrix.org
[m]
I'm unsure what the JDBC behaviour is around streams though if an error happened, so you'll need to test that it actually works too
Meriam Lachkar
@mlachkar

Hello, I have a question about Read Instance asnd how they compose.

  final case class Full(dependency: NewDependency, release: Option[NewRelease])
  //  I have a custom reader for NewRelease 
  // But I notice that Read[Full] uses a generic/derived reader for NewRelease`

So the code compiles fine, but reading fails, since it's not using my custom reader .
Right now to avoid this , I have re-written a Reader for Full, which copies the NewRelease reader with some differences to deal with the option.

      implicit val fullReader: Read[Full] = Read[(NewDependency, Option[String], ..........).map...

I am doing something wrong ? How can I fix this please ? thanks

Geovanny Junio
@geovannyjs
Deprecated. Please move to https://sca.la/typeleveldiscord
Meriam Lachkar
@mlachkar
thanks !
Nik
@your-psychiatrist:ellipsen.net
[m]
Can i use doobie to deserialize rows to case classes in scala 3? the docs suggest i need shapeless but that didnt build w/ scala 3 for me ad hoc.. figure'd i'd ask here first before wasting an hour on this (shapeless-3 seems to at least build)
Nik
@your-psychiatrist:ellipsen.net
[m]
found out that i'm gonna have to supply my own Get and Read instance :/ but it seems to work at least
peterstorm
@peterstorm:matrix.org
[m]
I’ve just used Circe Decoders and Encoders for my case classes and Doobie, and that seems to work fine
micky44
@Micky44Scoll_twitter
Hi, guys how can i write doobie with a custom enum list on scala 3?
jatcwang
@jatcwang:matrix.org
[m]
@Micky44Scoll_twitter: What do you mean by custom enum list? Can you give an example? :)
micky44
@Micky44Scoll_twitter

i.g, i have a custom enum list on database like

implicit val animalArray: Meta[Array[Animal]] = ???

how i can implement animalArray ?
when Animal is a database enum like

enum Animal:
  case Cat, Dog, Horse
end Animal

implicit val metaAnimal: Meta[Animal] = pgEnumString("animal_type", Animal.valueOf, _.toString)
jatcwang
@jatcwang:matrix.org
[m]
@Micky44Scoll_twitter: how are you storing these values in the db? What is the db column type?
micky44
@Micky44Scoll_twitter
@jatcwang:matrix.org i'm using Enum column type like
CREATE TYPE animal_type AS ENUM ('Cat', 'Dog', 'Horse');
jatcwang
@jatcwang:matrix.org
[m]
@Micky44Scoll_twitter: have you seen https://tpolecat.github.io/doobie/docs/11-Arrays.html? If you query that column as List[Animal] I think it should work.
Failing that, try querying something like SELECT 'Cat' :: animal_type and see if it maps into Animal correctly. If not it suggests that your implicit Meta[Animal] isn't in scope
(Looking at your scala 3 type definition, you probably want to put the Meta instance in the companion object of Animal?)
ruslan
@unoexperto

How do I quote brackets in doobie string interpolator ? I have case-sensitive column name in PSQL and I'm not sure how to properly write query for it. I tried this

sql"select nconst, \"birthYear\" from name_basics limit 10"

but I get Exception in thread "main" org.postgresql.util.PSQLException: ERROR: syntax error at or near "\" runtime error.

2 replies
Greg Fisher
@gnfisher
Doobie is upset that I am including columns in my select that I am not including in the case class the results are being read into. These are two similiarity(..) "columns" used for sorting but I don't need the data outside of that. Is there something I can do differently that doesn't including writing a READ for the case class?
jatcwang
@jatcwang:matrix.org
[m]
@gnfisher: Do another SELECT around it is the easiest way I can think of. Or you can query into an HList, drop the columns you don't need, and then convert that HList into your case class but that comes with bandwidth/performance cost ofc
jatcwang
@jatcwang:matrix.org
[m]
@unoexperto: he doesn't monitor this channel. Can you provide a minimal repo reproducing the problem? My guess is that something isn't being escaped properly in your code leading to the \ char in the SQL statement
ruslan
@unoexperto

@jatcwang:matrix.org Too bad. Rob used to be very active here. Where is he now ?

As per sample project it's located here
https://github.com/unoexperto/scala3-example-project/blob/main/src/main/scala/Main.scala
I just took official Scala 3 sample project and added few lines of doobie code.

jatcwang
@jatcwang:matrix.org
[m]
@unoexperto: he hangs out around typelevel discord but I can take a look first
ruslan
@unoexperto
@jatcwang:matrix.org Thanks!
jatcwang
@jatcwang:matrix.org
[m]
sql"""select nconst, "birthYear" from name_basics limit 10""" works @unoexperto
Arne Claassen
@sdether
Been away from Doobie for a while and trying to find a good pattern for Akka integration. The two obvious ways from IOConnection to ActorRef are:
1) unsafeToFuture and pipe the future and use onComplete to pipe it back to ActorRef
2) Create a pool of DB actors each with a connection, that use unsafeRunSync to block on the incoming message and reply back to ActorRef
Is there another way to run transactions on their own threadpool with a callback that can send the results to ActorRef?
thx
Dmitriy Kuzkin
@kuzkdmy

Hi, looks like a bug, before request it will be happy to get some assistance here. Thanks

Read not derived, next code results in such error

Cannot find or construct a Read instance for type:
  com.test.Main.DataWrapper2
But derivation works with simple Read[NewTypeWrapper]
package com.test

import doobie._
import doobie.postgres.implicits._
import io.estatico.newtype.macros.newtype

object Main extends App {
  @newtype case class NewTypeWrapper(value: Int)
  object NewTypeWrapper {
    implicit val read: Read[NewTypeWrapper] = Read[Int].map(NewTypeWrapper(_))
  }
  case class DataWrapper(one: NewTypeWrapper)
  case class DataWrapper1(list: List[Int])
  case class DataWrapper2(list: List[NewTypeWrapper])

  val t = NewTypeWrapper(1)
  println(Read[NewTypeWrapper])
  println(Read[DataWrapper])
  println(Read[DataWrapper1])
  println(Read[DataWrapper2])
}
Dmitriy Kuzkin
@kuzkdmy
^^^ Created ticket tpolecat/doobie#1569
In my pet project https://github.com/kuzkdmy/ipsa-course-lab-programming-and-db/tree/master/src/main/scala/ua/kpi/ipsa
have to update db domain model to no longer use newtype
It is extremely required in api routes when working with tapir
But now unfortunately it can't be used as part of db model
It's a pity that also can not bring this to real production project where we also stay without newtype on db model
Thanks
ex0ns
@ex0ns

Hello, I'm having some issue regarding the Instant class with doobie, I'm inserting a class that is looking like that:

2021-10-18T17:59:41.876362Z // Instant.now()
DomainOk(2021-10-18T18:59:41.882887Z) // Instant.now.plusMillis(60.minutes.toMillis)

But when I retrieve it, I get

DomainOk(2021-10-18T20:59:41.882887Z) did not satisfy equalTo(DomainOk(2021-10-18T18:59:41.882887Z))

I've checked into my DB, and this instant is stored as 2021-10-18 20:59:41.882887 expiration, which is of type 'TIMESTAMP'

Which is definitely related to the timezone, I wonder if this is a known issue or not ? This is happening in my tests while using https://github.com/zonkyio/embedded-postgres.

3 replies
Greg Fisher
@gnfisher
Hello, the docs here (https://tpolecat.github.io/doobie-0.2.1/13-Extensions-PostgreSQL.html) mention you can't set an enum to null in postgres. You can set an enum field to null, though. I've set these as Option[MyEnum] and everything seems to typecheck and work. However I've not tried to write data to the enum field. I'm working on a larger feature, and doing it in small iterations, so I'm not adding write functionality just yet. But wanted to make sure there isn't something I'm missing here.
Assen Kolov
@kolov

I am struggling with an unexpected problem with doobie. A simple doobie query that worked fine yesterday failed after adding a few new dependencies. The query started returning Unit, instead of the value of the expected type, causing class scala.runtime.BoxedUnit cannot be cast to class java.lang.String). Here's a simple version of the code demonstrating the issue:

      val u = for {
          u <- sql"select id from user ".queryWithLogHandler[String](LogHandler.jdkLogHandler).to[List]
          _ = println(s"users=$u")
        } yield u

... u.transact(xa) ...

The output is users=List(()) What could cause this? No matter how many values ar selected in the query, the result is always unit, as long as the query executes correctly.

Steve Moss
@stevemossovo
Is anyone able to help me with a couple of queries regarding Doobie and custom type mappings using the Meta typeclass?
I'm new to Scala, which doesn't help :persevere:
I have the following case class and object defined:
@AvroNamespace("com.spm.kafka.asp.event")
case class End(
  reasonForEnd: ReasonForEnd,
  date: LocalDate
)

object End {
  implicit val endCodec: Codec[End] = Codec.derive

  implicit val endMeta: Meta[End] = ???
}
Not really sure where to start to be honest :weary:
Assen Kolov
@kolov

@stevemossovo I assume you will store reasonForEnd and date in a separate column. Then you need to provide

implicit val reasonGet: Get[ReasonForEnd] = ???
implicit val reasonPut: Put[ReasonForEnd] = ???

Doobie has instances for LocalDate etc. available at doobie.implicits.javatime._. See https://tpolecat.github.io/doobie/docs/12-Custom-Mappings.html. With these, you can sql" select reason, endDate from mytable where ...".query[End].to[List]... etc.

Steve Moss
@stevemossovo
Thanks, @kolov, I'll take a look :smile:
James Phillips
@jdrphillips
Is it possible to mix Update0, ConnectionIO, Free[ConnectionOp, ?] together?
I just want lots of operations done in the same transaction, but the types are not making it easy at all
James Phillips
@jdrphillips
A ConnectionIO is a Free[ConnectionOp, ?]? hmmm, well, it's not compiling right now but I'll investigate that.
jatcwang
@jatcwang:matrix.org
[m]
@jdrphillips: Yeah ConnectionIO is just a type alias for Free[ConnectionOp, ?] so you can use them together.
For Update0, you'll need to convert it into a ConnectionIO using .run
Jarek Rozanski
@jarek_rozanski:matrix.org
[m]

Hi there, I think I might have misunderstood some concepts of TX in Doobie. I an aggregate with two services which both create an Kleisli. When second return Left side of either, and thus returning an error, I was expecting the whole thing to end with error and rollback. But, alas, this is not the case.

trait C1 {
  toF: ConnectionIO ~> F
}

val k1: Kleisli[F, C1, Either[Error1, Value1] = ???
val k2: Kleisli[F, C2, Either[Error2, Value2] = ???

val r = for {
  r1 <- k1 // returns Right[Value1]; this is where DB write happens
  r2 <- k2 // returns Left[Error2]
} yield r2

r.run(context)

It was my assumption that Doobie will apply transaction after only at the end of the world on success.

This is using Hikari TX Pool.

Jarek Rozanski
@jarek_rozanski:matrix.org
[m]
I guess this boils down to question: Should Kleisli composition yield the oops action, rather than after.
Jarek Rozanski
@jarek_rozanski:matrix.org
[m]
I think it was me 😃, r1 and r2 is Either so the the flatMap in on Kleisli, not on Either, hence for comprehension does not short.
Diego E. Alonso Blas
@diesalbla

@jarek_rozanski:matrix.org The Discord server of Typelevel may be the best channel nowadays...

When second return Left side of either, and thus returning an error, I was expecting the whole thing to end with error and rollback

Well, in that case, if you are intending to use the Either in a monadic-like action, you would need the EitherT data type.

However, before you get into using more transformer types, it may help you to first write what is the behaviour you are looking for, as you would write it with plain functions and flatMap on F.

I gather that you are aiming to a "semantics" like this:
def k1(c: C1): F[Either[Error1, Value1] = ???
def k2(c: C2): F[Either[Error2, Value2] = ???

def r(c: Context): F[Either[Error12 , Value2] = 
  k1(c).flatMap {
    case Left(err1) => F.pure(Left(err1))
    case Right(r1) => 
      k2(c).flatMap { 
        case Left(err2) => F.pure(left(err2)
        case Right(r2) => F.pure(r2)
      }
  }
Adrian
@adrian-salajan
Hi, i'm new to doobie and would like some pointers. Given I have a Fragment already can I use it as a prepared statement ?
I'm trying to use prepare statement for better efficiency
My fragment is an postgresql upsert: INSERT ...ON CONFLICT DO UPDATE...WHERE
Eric K Richardson
@ekrich
Might want to try at typelevel discord.
jatcwang
@jatcwang:matrix.org
[m]
reactormonk
@reactormonk:matrix.org
[m]
Can I run \copy commands from within doobie? I suppose they're psql commands, not JDBC commands?
Daniel de Oliveira Mantovani
@mantovani
It's there on the documentation how to use copy
reactormonk
@reactormonk:matrix.org
[m]
Oh, looks useful. Thanks for the Info.