Instead of writing CQL queries in lagom projects it would be nice if we could use phantom-dsl (see https://github.com/outworkers/phantom) and use the lagom infrastructure for the Cassandra session management.
The benefits: The dsl provides a type safe way on querying and writing into the Cassandra database, for example:
case class DomainObject(id: UUID, description: String)
abstract class DomainObjects extends Table[DomainObjects, DomainObject] {
object id extends UUIDColumn with PartitionKey
object description extends StringColumn
def getById(id: UUID): Future[Option[DomainObject]] = {
select.where(_.id eqs id).one()
}
}
There are several things to consider:
CassandraSession?Connector that's ServiceLocator aware so that contact points don't need to be statically configured?I think defining a specific third party wouldn't be a good idea. There are many other DSLs to use Cassandra (e.g. quill). I think it would be great to show how can one use different libs to accomplish that in Docs using multiple famous libs.
It might be best to implement this as a separate module, outside the lagom/lagom project.
Seems that I've found a way to use quill in lagom. What kind of project should I create?
This PR make quill to support lagom. Can you help me to review it? @TimMoore @ignasi35
@WayneWang12 In my view, you need to create a separate project for integrating Quill and Lagom. I'm not sure, that Quill team accept your pull request.
@ihostage I'm communicating with them. If they don't accept, I'll create my own project.
Luckily one of quill's maintainers @jilen is Chinese. I can talk to him directly in native language.
quill-cassandra-lagom is released! Add libraryDependencies += "io.getquill" %% "quill-cassandra-lagom" % "3.1.0" to your build.sbt, then create a context in your application definition:
lazy val quillContext: CassandraLagomAsyncContext[SnakeCase] = wire[CassandraLagomAsyncContext[SnakeCase]]
lazy val quillStreamContext: CassandraLagomStreamContext[SnakeCase] = wire[CassandraLagomStreamContext[SnakeCase]]
It will reuse Lagom's cassandra session automatically. Then you can use it in quill way in your code:
import ctx._
val q = quote(
query[AnalysisEvent]
.filter(evt =>
evt.tenantId == lift(tenantId) &&
evt.userSetId == lift(userSetId)))
ctx.run(q)
If you are using CassandraLagomAsyncContext, result will be Future[List[AnalysisEvent]]; if CassandraLagomStreamContext, result will be Source[AnalysisEvent, NotUsed].
Most helpful comment
quill-cassandra-lagomis released! AddlibraryDependencies += "io.getquill" %% "quill-cassandra-lagom" % "3.1.0"to yourbuild.sbt, then create a context in your application definition:It will reuse Lagom's cassandra session automatically. Then you can use it in quill way in your code:
If you are using
CassandraLagomAsyncContext, result will beFuture[List[AnalysisEvent]]; ifCassandraLagomStreamContext, result will beSource[AnalysisEvent, NotUsed].