Coder Social home page Coder Social logo

norm's Introduction

Norm

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.

Why

  1. 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.
  2. Because to learn even a new idiomatic way to write the same old sql is not a great innovation

Should I use it?

  1. If you're writting a very complex model (joins, inheritance,...) - for now - No
  2. If you're not comfortable with unchecked type - No
  3. If you want transaction between models - for now - No
  4. 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

Getting Start

  1. Add Norm to your project:

This project was forked from Ricardo Lazaro's norm but it diverged too much and is not published yet, so just copy the file Norm.scala to your project.

  1. Create a model class extending Norm:
  import models.frameworks.{NormCompanion, Norm}
  import play.api.libs.json.{JsObject, JsValue, Json}

  case class Product(
                         name:              String,
                         otherEntityId:     Long,
                         monetaryValue:     BigDecimal,
                         config:            JsValue,
                         enabled:           Boolean = true,
                         priority:          Int = 0,
                         id:                Pk[Long] = NotAssigned
                         ) extends Norm[Product] {


  object Product extends NormCompanion[Product]{
    implicit val ProductFormat = Json.format[Product]
    def findByOtherEntity(otherEntity: OtherEntity): List[Product] = findByOtherEntity(otherEntity.id.get)
    def findByOtherEntity(otherEntityId: Long):      List[Product] = findByProperties(Map("otherEntityId" -> otherEntityId, "enabled" -> true))
  }
  1. Inserting a product:
        val product = Product(someName, otherEntityId, someMonetaryValue, someJson).save

Or

```scala
    val optionId = Product.create(
      Map(
        "name"          ->  "productName",
        "otherEntityId" ->  1l,
        "monetaryValue" ->  new BigDecimal("10.00"),
        "config"        ->  JsNull
      )
    )
```
  1. Updating a product

        product.name          = newProductName
        product.otherEntityId = otherProductId
        product.update
  2. Partial update

        product.update(
          Map(
            "name"        -> newProductName,
            "otherEntityId" -> otherProductId
          )
        )
  3. 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)
  val products = Product.findByProperties(Map("name" -> someName, "enabled" -> true))

norm's People

Contributors

ricardolazaro avatar guihps avatar migmruiz avatar ytallo avatar samuelwribeiro avatar davidphora avatar

Watchers

Fabio Hisamoto avatar Marcelo da Silva Gomes avatar Paulo Cheque avatar Flavio da Silva Mori Junior avatar Felipe Benevides avatar  avatar Fernando Ferreira Chucre avatar  avatar Adriano Mitre avatar Edson Hilios avatar Bruno Campos avatar James Cloos avatar  avatar Nykolas Laurentino de Lima avatar Gabriel Fernandes Rios avatar Luis Vieira avatar Sérgio Tanaka avatar Diego Ramos avatar Yros Aguiar avatar Rodrigo Perez Sasaki avatar  avatar  avatar Caio Augusto Barbirato avatar Gustavo Amigo avatar Tulio Gomez Rodrigues avatar Gabriel Francisco avatar Eduardo Costa avatar  avatar Raoni Sortica avatar Sammer Valgas avatar wanderley filho avatar Romulo Jovelli avatar Walkirya Heto avatar Vinícius Munhoz avatar Caio Tomazelli avatar Darcio Pacifico avatar Xyuan avatar  avatar Vinicius Murakami avatar bot99 avatar

Forkers

paulocheque

norm's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.