Projects
View on GitHub Scala ActiveRecord
aselab/scala-activerecord
About
ActiveRecord-like ORM library for Scala
Developer ToolsWhat you can do
- Auto connection management
- Composable query operation
Measured star growth
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- Collecting history
- in 1 month
- Collecting history
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README.md
OriginalScala ActiveRecord

scala-activerecord is an ORM library for Scala.
This library is inspired by ActiveRecord of Ruby on Rails. It is designed following the CoC(Convention over Configuration), DRY(Don't Repeat Yourself) principles.
Minimal example
- Sample snippet: Scastie
Model implementation:
package models
import com.github.aselab.activerecord._
import com.github.aselab.activerecord.dsl._
case class Person(name: String, age: Int) extends ActiveRecord
object Person extends ActiveRecordCompanion[Person]
Schema definition:
package models
import com.github.aselab.activerecord._
import com.github.aselab.activerecord.dsl._
object Tables extends ActiveRecordTables {
val people = table[Person]
}
ActiveRecord model usage:
import com.github.aselab.activerecord.dsl._
import models._
object App extends App {
Tables.initialize
Person("person1", 25).save()
Person("person2", 18).save()
Person("person3", 40).save()
Person("person4", 18).save()
Person.findBy("name", "person1") //=> Some(Person("person1", 25))
Person.findBy("age", 55) //=> None
Person.findAllBy("age", 18).toList //=> List(Person("person2", 18), Person("person4", 18))
Person.where(_.age.~ >= 20).orderBy(_.age.desc).toList //=> List(Person("person3", 40), Person("person1", 25))
Tables.cleanup
}
Schema and query DSL is based on Squeryl.
Scala 3
The current master builds for Scala 3.7; Scala 2.13 users should stay on 0.6.x.
- Scala 3.8 and later are not supported yet: their standard library is compiled by Scala 3, and Squeryl 0.11
fails to initialize on it (it reads the public
$outerfield ofEnumeration#Valueby reflection). - Building requires JDK 17 or later (sbt 2). The library itself is compiled for Java 11, the minimum of its dependencies (HikariCP, nscala-time, json4s).
- Squeryl creates proxies of model classes with cglib. On JDK 9 and later, run your application and tests with
--add-opens=java.base/java.lang=ALL-UNNAMED(with sbt, e.g.fork := trueandjavaOptions += "..."). - The element types of fields such as
Option[Int]orList[Long]are captured at compile time when the model's companion object (ActiveRecordCompanion,ActiveModelCompanion) is defined. For a class without such a companion, callcom.github.aselab.activerecord.reflections.FieldTypes.register[MyClass]before using it. - Field names and value types passed to
newInstance,assign,find,findBy,findAllByandfindByOrCreateare still checked at compile time. They must be written out at the call site (string literals); use theunsafe*variants for dynamic names. - Squeryl on Scala 3 cannot infer the result type of arithmetic on columns of different types, such as
sum(item.price) + avg(item.price); pass the result factory explicitly:sum(item.price).+(avg(item.price))(optionFloatTEF). Aggregates (max,min,sum,avg) work as before. - scala-activerecord-specs is built on specs2 5 (5.9.1; 5.11 and later need Scala 3.9):
ActiveRecordSpecificationruns its examples sequentially. OverridebeforeAll()andafterAll()(methods, now with()) to add set-up and tear-down; callsuperto keep the schema handling.AutoRollbackrolls back each example withAroundEach.- The specs2
===matcher is disabled, as it would otherwise shadow Squeryl's===in queries; usemust beEqualTo(...)instead. specs2 5 removedmustEqual,must have sizeandScope.
- The tests use H2 2.x, where
valueis a keyword; add;NON_KEYWORDS=VALUEto the JDBC URL if a model has a field namedvalue. - The Play 2 and Scalatra modules have not been migrated to Scala 3 yet.
Features
- Auto connection management
- Composable query operation
- Callback
- Validation
- Association
Documents and other resources
Web framework support
License
MIT
Explore related projects
Loading…