Criteria4s is a straightforward domain-specific language (DSL) designed to define criteria and predicate expressions for any data store using Scala's type class mechanisms in a type-safe manner. Its core is designed to be agnostic to any specific data store, making it extensible and capable of supporting a variety of data storage systems.
To utilize the Criteria4s DSL in your project, add the following dependencies to your build configuration:
- Core library: criteria4s-core
- SQL implementation: criteria4s-sql
- MongoDB implementation: criteria4s-mongodb
Add these to your build.sbt
:
libraryDependencies += "com.eff3ct" %% "criteria4s-core" % "<version>" // Core library
libraryDependencies += "com.eff3ct" %% "criteria4s-sql" % "<version>" // SQL implementation
libraryDependencies += "com.eff3ct" %% "criteria4s-mongodb" % "<version>" // MongoDB implementation
Important
Criteria4s is currently a work in progress and not ready for production use. It is available exclusively for Scala 2.13.
Criteria4s is extensible, supporting various types of data stores. Presently, it supports the following dialects:
Dialect | Package |
---|---|
SQL | sql |
MongoDB | mongodb |
PostgresSQL | postgresql |
Here are examples demonstrating how to use the Criteria DSL. More samples are available in the criteria4s-examples
module.
Start by importing the Criteria4s DSL and the SQL dialect:
import com.eff3ct.criteria4s.core._
import com.eff3ct.criteria4s.examples.datastores._
import com.eff3ct.criteria4s.extensions._
import com.eff3ct.criteria4s.functions._
Define criteria expressions in a polymorphic way using the Criteria DSL. Additional examples are provided in the Defining Criteria Expressions
document.
def expr[T <: CriteriaTag : LEQ : EQ : AND : OR : Show[Column, *]]: Criteria[T] =
(col[T]("field1") leq lit(3)) and (col[T]("field2") leq lit(4)) or (col[T]("field3") === lit("c"))
The following code snippets demonstrate how to use the defined criteria expressions:
def ageCriteria[T <: CriteriaTag : GT : LT : AND : Show[Column, *]]: Criteria[T] =
(col[T]("age") gt lit(18)) and (col[T]("age") lt lit(65))
def refCriteria[T <: CriteriaTag : EQ : Show[Column, *]](fieldName: String, id: UUID): Criteria[T] =
col[T](fieldName) === lit(id.toString)
Utilize these expressions to generate criteria for different data stores by importing the corresponding dialects. Evaluation examples for various dialects are available in the following documents:
- PostgreSQL Dialect: Evaluate criteria expressions for PostgreSQL data stores.
- MongoDB Dialect: Evaluate criteria expressions for MongoDB data stores.
- MySQL Dialect: Evaluate criteria expressions for MySQL data stores.
- Custom Dialect: Evaluate criteria expressions for custom data stores.
Leverage the Criteria DSL in an inline manner:
(col[PostgreSQL]("field1") leq lit(3)) and (col[PostgreSQL]("field2") leq lit(4)) or (col[PostgreSQL]("field3") === lit("c"))
// res: (('field1' <= 3) AND ('field2' <= 4)) OR ('field3' = c)
(col[MongoDB]("field1") leq lit(3)) and (col[MongoDB]("field2") leq lit(4)) or (col[MongoDB]("field3") === lit("c"))
// res: {$or: [{$and: [{"field1": {$lte: 3}}, {"field2": {$lte: 4}}]}, {"field3": {$eq: c}}]}
(col[WeirdDatastore]("field1") leq lit(3)) and (col[WeirdDatastore]("field2") leq lit(4)) or (col[WeirdDatastore]("field3") === lit("c"))
// res: {left: {left: {left: field1, opt: <=, right: 3 }, opt: AND, right: {left: field2, opt: <=, right: 4 } }, opt: OR, right: {left: field3, opt: =, right: c } }