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

Add support of multiple databases #51

Closed
heaven-born opened this issue Jan 24, 2024 · 3 comments · Fixed by #52
Closed

Add support of multiple databases #51

heaven-born opened this issue Jan 24, 2024 · 3 comments · Fixed by #52
Assignees
Milestone

Comments

@heaven-born
Copy link

I worked with the tranzactio a long time ago, and I remember an issue I could not overcome: I had more than one database and I wanted to combine two ZIOs that required two connections to two different databases and then do transactionOrDie for each of the connection separately. This is just a prototype, but I wanted something like this:


type ConnectionOne = Connection[DatabaseOne]
type ConnectionTwo = Connection[DatabaseTwo]

val list: ZIO[ConnectionOne, DbException, List[String]] = tzio {
  sql"SELECT name FROM users".query[String].to[List]
}

val list2: ZIO[ConnectionTwo, DbException, List[String]] = tzio {
  sql"SELECT name FROM users".query[String].to[List]
}

val combined:ZIO[ConnectionOne && ConnectionTwo, DbException, List[String]] = list *> list2

val foo: ZIO[DatabaseOne && DatabaseTwo, String, Long] = Database.transactionOrDie(combined)


I don't think something like this is doable in scala 2.13, but maybe in scala 3 is possible.

@guersam
Copy link

guersam commented Jan 25, 2024

It's possible using Subtypes in ZIO Prelude in Scala 2.13, but not yet in Scala 3 due to a compiler issue or something AFAIK.

As a workaround I manually declare different subtypes of Database using inheritance. It's cumbersome but just works. Instead of extending Connection, I prefer Database for more flexibility and simplicity.

class DatabaseOne(underlying: Database) extends Database {
  override def transaction[R, E, A](...) = ???
  // other necessary overrides ...
}

@gaelrenoux
Copy link
Owner

I can definitely see how such a feature would be useful.

A simple way to make it easier would be for Tranzactio to provide directly a DatabaseWrapper that inherits Database (equivalent to your DatabaseOne). Then you could declare your own subclasses, where you would only need to extend the wrapper without having to override anything.

I could also provide a DatabaseTyped[A], with an arbitrary type-parameter that would work as a marker. It would also inherit the wrapper, and you would only need to provide a marker type. Not sure if that's better than just having you declare a subclass of DatabaseWrapper directly.

@gaelrenoux
Copy link
Owner

gaelrenoux commented Mar 9, 2024

I created a PR for this use case. What do you think? I'm not fan of the name DatabaseT for the typed database, but couldn't find a better idea. DatabaseCustom isn't great, it's not especially custom. MyDatabase maybe?

@gaelrenoux gaelrenoux added this to the 5.2.0 milestone Mar 24, 2024
@gaelrenoux gaelrenoux self-assigned this Mar 24, 2024
@gaelrenoux gaelrenoux linked a pull request Mar 24, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants