timt / naive-db

A very simple scala db for storing case classes and collections of case classes, backed by a json file.

GitHub

naive-db Build Status Download

A really simple json db library implemented in scala.

Requirements

Usage

Add the following lines to your build.sbt

resolvers += "Tim Tennant's repo" at "http://dl.bintray.com/timt/repo/"

libraryDependencies += "io.shaka" %% "naive-http" % "94"

and Circe for encoding/decoding json

val circeVersion = "0.8.0"  //Change this to prefered version
libraryDependencies ++=Seq(
  "io.circe" %% "circe-core" % circeVersion,
  "io.circe" %% "circe-generic" % circeVersion,
  "io.circe" %% "circe-parser"  % circeVersion
)

Start hacking

import io.shaka.db.Db
import io.circe._
import io.circe.generic.semiauto._
import io.circe.parser.parse
import io.circe.syntax.EncoderOps

 case class Post(id: Int, title: String)

  object Post {
    implicit val decoder: Decoder[Post] = deriveDecoder[Post]
    implicit val encoder: Encoder[Post] = deriveEncoder[Post]
  }
  
  val db = Db.initialize("./db.json") //creates/loads the file ./db.json
  
  //Set an item/object in database
  db.set("post", Post(1, "another new thing"))
  
  //Get an item/object from database
  val item: Option[Post] = db.get[Post]("post")
  
  //Add items to a list in the database
  db
    .addItem[Post]("posts", Post(1, "some post"))
    .addItem[Post]("posts", Post(2, "another post"))
    
  //Find and item in a list
  val post: Option[Post] = Db.find[Post]("posts", _.title.contains("another"))
  
  //Remove an item/object
  db.remove("post")
  db.remove("posts")
  
  //Remove and item from a list
  db.removeItem[Post]("posts", _.id == 2)

For more examples see

Code license

Apache License 2.0