Coder Social home page Coder Social logo

cagette's Introduction

Cagette

Cagette is a quick and dirty data store for prototyping. It allows to focus on designing your domain model, business rules, screens, user interactions, batch, reports, ... without bothering about how your data will eventually be persisted.

"Cagette saved my hackday" — Mr. Speaker

Setting up with sbt

Configure a new resolver:

resolvers += "Guillaume Bort" at "http://guillaume.bort.fr/repository"

Add the library dependency:

libraryDependencies += "guillaume.bort" %% "cagette" % "0.2"

Using – as easy as 1,2,3

1. Create your domain class with an id field:

Let's try with a User class:

case class User(id: Long, email: String, groups: Seq[String])

2. Create a Cageot[DomainType,IdType]:

We usually name the Cageot with the same name as the domain class:

object User extends cagette.Cageot[User,Long]

3. Query the cageot using Scala functions

The Cageot type provides convenient higher order query functions you can use directly:

findAll()

val allusers: Seq[User] = User.findAll()

findById(id: IdType)

val maybeUser: Option[User] = User.findById(88)

findOneBy(predicate: DomainType => Boolean)

val maybeUser: Option[User] = User.findOneBy(_.email == "[email protected]")

findBy(predicate: DomainType => Boolean)

val adminUsers: Seq[User] = User.findBy(_.groups.contains("admin"))

Setting up initial data set

While defining your Cageot you can define the initial data set it will contain:

object User extends cagette.Cageot[User,Long] {
	
	override def initialData = Seq(
		User(1, "[email protected]", groups = Seq("admin", "user")),
		User(2, "[email protected]", groups = Seq("user")),
		User(3, "[email protected]", groups = Nil)
	)

}

Updating data

save will insert a new instance

If you want to store a new instance into your Cageot, just use the save operation as:

User.save( 
  User(88, "[email protected]", groups = Seq("user")) 
)

but save will also update an existing instance

If the store already contains an instance with the same id, this instance will just be udpated:

User.findOneBy(_.email == "[email protected]").map { user =>
	User.save(user.copy(groups = (user.groups :+ "admin").distinct ))
}

then you can delete them later

You can either delete by using the id of the instances you want to remove as:

User.delete(88)

or batch delete using a predicate as:

User.delete(_.groups.contains("archived"))

How to avoid the evil id field

If you don't want to artificially specify an id field in your case class, you can still provide your own Identifier instance when creating your Cageot:

case class User(email: String, groups: Seq[String])
object User extends Cageot[User,String]()(Identifier(_.email)) // Specify the identifier as `email`

Using with Play framework

You can directly use this for prototyping with http://www.playframework.org. Just add the resolver and library dependency to your Build.scala file, and start defining your model. The initialData set will be applied automatically at startup and each time your code change.

Bonus

Check the source code and find secret features like pagination and autoincrement for domain class identifiers.

Enjoy!

cagette's People

Contributors

guillaumebort avatar mrspeaker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.