Norm is an utility built on top of Anorm to be used in playframework scala projects. It's not a complete ORM with a lot of complex things.
- Because repeating yourself is boring. For simple cases, when you create 2 or 3 classes(models?) to access the database, you'll end up writting a lot of the same things.
- Because to learn even a new idiomatic way to write the same old sql is not a great innovation
- If you're writting a very complex model (joins, inheritance,...) - for now - No
- If you're not comfortable with unchecked type - No
- If you want transaction between models - for now - No
- If you have a lot of models with simple access pattern and don't want to learn a new way to access SQL dbs - yes
- Add Norm to your project:
This project is not published yet, so just copy the file norm.scala to your project.
- Create a model class extending Norm:
import models.frameworks.{NormCompanion, Norm}
import java.math.BigDecimal
case class Product(
id: Long,
var name: String,
var description: Option[String],
var price: BigDecimal,
var taxRange: Int,
var inStock: Boolean) extends Norm[Product]
object Product extends NormCompanion[Product]
-
Inserting a product:
val optionId = Product.create( Map( "name" -> "productName", "description" -> "productDescription", "price" -> new BigDecimal("10.00"), "taxRange" -> 2, "inStock" -> true ) )
-
Updating a product
product.name = newProductName product.description = newDescription product.price = newPrice product.taxRange = newTaxRange product.inStock = newInStock product.update()
-
Partial update
product.update( Map( "name" -> newProductName, "description" -> newDescription ) )
-
Find a product
val product = Product.find(2l) // by id 2l
val productOption = Product.findOption(2l) // by id 2l
val products = Product.findByProperty("name", someName)
More details see the tests