📜 ⬆️ ⬇️

Sorm New elegant and scalable ORM framework for Scala

Anyone who has dealt with the choice of ORM for Scala has probably heard about such libraries as Slick (Scala Query), Squeryl, Circumflex, etc., and is equally likely to agree with the following statements: they are not abstracted from relational concepts they require a description of the model in specific ways, the API is often confused and dispersed, and finally, the extent to which the abstractions offered by these libraries actually simplify working with the database is at least questionable.

So the idea was born to create a framework that
  1. will build an abstraction above the database to a higher level, presenting it through the standard Scala data types: primitives, tuples, options, collections, etc., as well as standard case classes representing entities,
  2. performing the first task will be “clean”, which means complete elimination of the relational side concepts from the framework API: no tables, rows and relational links,
  3. raise to the principle of the basis of functional programming: only non-mutable data types, minimizing State,
  4. will perform for the user everything that he can, due to which he will reach minimization of the boilerplate.

As you can see, the tasks were all fairly uncompromising, but they were solved. Due to this, it was possible to achieve consistency, simplicity, and the ensuing intuitiveness of the framework - and this is by no means weak. Boilerplate succeeded in eliminating it altogether. Next code.

//  . //  ,      .   // -,         - //  ,   view. case class Artist ( name : String, genres : Set[Genre] ) case class Genre ( name : String ) //   .   SORM   : import sorm._ object Db extends Instance ( entities = Set() + Entity[Artist]() + Entity[Genre](), url = "jdbc:h2:mem:test" ) //     : val metal = Db.save( Genre("Metal") ) val rock = Db.save( Genre("Rock") ) Db.save( Artist("Metallica", Set() + metal + rock) ) Db.save( Artist("Dire Straits", Set() + rock) ) //  -: // Option[Artist]: val metallica = Db.query[Artist].whereEqual("name", "Metallica").fetchOne() // Stream[Artist]: val rockArtists = Db.query[Artist].whereEqual("genres.item.name", "Rock").fetch() 

As they say, that's all folks! This is the complete code of the program that creates the database and saves and requests entities from it. No additional action by the user for its performance is required.
')
Installation information, documentation, tutorial and comparison with Slick (Scala Query) can be found on the official SORM website .

Spoiler: in the next versions of SORM macros are coming and the resulting even simpler API.

Source: https://habr.com/ru/post/159867/


All Articles