Giter Club home page Giter Club logo

soriento's Introduction

Soriento

Join the chat at https://gitter.im/dimparf/Soriento Build Status

Scala OrientDb object mapping library

Soriento is an object-relational mapping framework from scala case classes to OrientDb ODocument. ##News

  • Pre-maven 0.1.0alpha release
  • fix LinkSet deserialization
  • add saveAsT to RichODatabaseDocument
  • fix recursive serialization
  • fix primitive type OType detection
  • update OrientDb dependencies

##Note Please use develop branch for development and master as production version of library.

##New: Polymorphic loading (by class name)

See PolyMorphicLoadByNameTest.scala

    val teSchema = List(db.getMetadata().getSchema().createClass("TraceElement")).asJava
    createOClass[TraceElementViewEvent].setSuperClasses(teSchema)
    createOClass[TraceElementLoginEvent].setSuperClasses(teSchema)

    db.save(new TraceElementLoginEvent(123, LoginEvent(1000)))
    db.save(new TraceElementViewEvent(124, ViewEvent(1000, 99)))

    val blogs: List[TraceElement] = db.queryAnyBySql[TraceElement]("select * from TraceElement;")

To achieve this, type names need to be loaded into a registry before the query.

TODO. Dynamically load types from a set of packages if not found.

Features

  • Creating/deleting OrientDb classes by case classes.
  • Transparent CRUD for documents represented as case classes.
  • Linked or Embedded definitions for case classes.
  • Support serialization/deserialization for case classes with @Embedded list/set of case classes.
  • Support OType mapping Scala => OrientDb OTypes.
  • Transactions support.
  • Query by SQL.

Supported types:

  • Long, Double, Int, Float, Short, String, case classes: Embedded, EmbeddedList, EmbeddedSet, Link, LinkList, LinkSet

##Coming soon

  • LinkMap.
  • EmbeddedMap.

##Add to you project

 lazy val youProject = Project("YouProject", file("."))
  .settings(commonSettings: _*)
  .dependsOn(sorientoProject)
  
 lazy val sorientoProject = RootProject(uri("https://github.com/dimparf/Soriento.git#master"))

##Usage Simple example:

  import com.emotioncity.soriento.Dsl._ // or extends Dsl trait
  import com.emotioncity.soriento.ODocumentReader._

  implicit val orientDb: ODatabaseDocumentTx = ???

  case class Message(content: String)
  
  case class Blog(author: String, @Embedded message: Message) // or @Linked
  
  case class BlogWithEmbeddedMessages(author: String, @EmbeddedSet messages: List[Message])

  //schema-full (use com.emotioncity.soriento.ODb trait) mode or without this lines - schema less
  createOClass[Message] 
  createOClass[Blog]
  createOClass[BlogWithEmbeddedMessages]
  
  val blog = Blog("Dim", message = Message("Hi")) //or without named params Blog("Dim", Message("Hi))
  val blogWithEmbeddedMessages = BlogWithEmbeddedMessages("John", List(Message("Hi"), Message("New blog note")))
  //ActiveRecord style
  blog.save
  blogWithEmbeddedMessages.save
  
  //..or
  orientDb.save(blog)
  
  
  val blogs: List[Blog] = db.queryBySql[Blog]("select from blog")
  
  //Save object graph (from test code, use scalatest)
  val messageOne = LinkedMessage("This is my first message")
  val messageOneSaved = messageOne.save.as[LinkedMessage].get
  val messageTwo = LinkedMessage("last")
  val messageTwoSaved = messageTwo.save.as[LinkedMessage].get
  
  //Warning: Soriento use immutable case classes,
  //unsaved messages don't have id. Save your values and get saved object with id with as[T] method.
  val blogWithLinkSetMessages = BlogWithLinkSetMessages("MyBlog", Set(messageOneSaved, messageTwoSaved))
  blogWithLinkSetMessages.save
  
  val extractedBlogsOpt = orientDb
  .queryBySql[BlogWithLinkSetMessages]("select from BlogWithLinkSetMessages where name = 'MyBlog'").headOption
    extractedBlogsOpt match {
      case Some(extractedBlog) =>
        inside(extractedBlog) { case BlogWithLinkSetMessages(name, messages) =>
          name should equal("MyBlog")
          messages should have size 2
          messages should contain(LinkedMessage("This is my first message", messageOneId))
          messages should contain(LinkedMessage("last", messageTwoId))
        }
      case None => fail("Model not saved or retrieved")
    }
  }
    
  deleteOClass[Message]
  deleteOClass[Blog]
  deleteOClass[BlogWithEmbeddedMessages]
  deleteOClass[BlogWithLinkSetMessages]

You can also create custom readers for models:

  object BlogWithEmbeddedMessages {
    implicit object BlogWithEmbeddedMessagesReader extends ODocumentReader[BlogWithEmbeddedMessages] {
       def read(oDocument: ODocument): BlogWithEmbeddedMessages = {
          BlogWithEmbeddedMessages(
            oDocument.get[String]("author").get,
            oDocument.getAs[Message, List[Message]]("message").get
          )
       }
     }
  }
  

and import it:

import BlogWithEmbeddedMessages._

More examples in test directory.

Testing

To run unit tests:

sbt test

Contributors

Contributing

Welcome to contribute! You can always post an issue or (even better) fork the project, implement your idea or fix the bug you have found and send a pull request. Just remember to test it when you are done. Here is how:

Run sbt test to run and compile tests.

License

This software is available under the Apache License, Version 2.0.

soriento's People

Contributors

b0c1 avatar dimparf avatar gitter-badger avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

pabloa

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.