Git Product home page Git Product logo

orientdb's Introduction

OrientDB Module

OrientDB for Play! Framework

Features

  • play.db.Model support
  • CRUD support
  • Embedded server for development
  • Injection of database sessions in controllers
  • Annotation-based transaction demarcation
  • Live class-reloading as usual
  • Basic support for RecordHooks and DatabaseListeners

Configuration

These are the default values that you can override in conf/application.conf

# OrientDB Module
# ~~~~~
# odb.url=memory:temp
# odb.user=admin
# odb.password=admin
# Separate url for graphDB, if not specified uses odb.url
# odb.graph.url=memory:temp
# Path to custom configuration file, in classpath or retalive to conf/ dir
# odb.config.file=/play/modules/orientdb/db.config
# Package prefix to scan entities
# odb.entities.package=models
# Control open session in view
# odb.open-in-view.documentdb=true
# odb.open-in-view.objectdb=true
# odb.open-in-view.graphdb=false

Note: Please, set odb.open-in-view.[dbtype] to true only for the database types that you want to inject. By default all are enabled.

Basic usage

Object Database

The access to ODatabaseObjectTx is wrapped in Model, so if you don’t need direct access simply create an entity that extends play.modules.orientdb.Model

public class Item extends Model {
    @SuppressWarnings("unused")
    @Id
    private Object id;

    @SuppressWarnings("unused")
    @Version
    private Object version;

    @Required
    public String name;

    public String description;
}

And use that entity on the controller:

public class MyController extends Controller {

    public static void index() {
        OObjectIteratorClass<Item> items = Item.all();
        render(items);
    }

    public static void detail(ORecordId id) {
        Item item = Item.findById(id);
        notFoundIfNull(item);
        render(item);
    }

    @Transactional
    public static void save(String name, String description) {
        Item item = new Item();
        item.name = name;
        item.description = description;
        item.save();
        index();
    }
    
    public static void search(String query) {
    	List<Item> result = Item.find("select * from Item where name like ?", "%"+query+"%");
    	render(result);
    }
}

Note: to obtain the ORID call the method entity.getIdentity()

Document Database

Inject the session in the controller

public class MyController extends Controller {

    @Inject
    static ODatabaseDocumentTx docdb;

    ...

    @Transactional(db = DBTYPE.DOCUMENT)
    public static void good() {
        ODocument doc = new ODocument(docdb, "Account");
        doc.field("name", "good");
        doc.save();
        index();
    }

}

If you have an entity that maps the document you can obtain an object representation, because OrientDB ODatabaseObject is built on top of ODatabaseDocument

public class MyController extends Controller {
    @Inject
    static ODatabaseObjectTx db;

    public static void index() {
        OObjectIteratorMultiCluster<Account> accounts = db.browseClass(Account.class);
        render(accounts);
    }

}

Graph Database

A simple test:

public class MyController extends Controller {
   @Inject static OGraphDatabase database;
	
   public static void someMethod() {
      OClass vehicleClass = database.createVertexType("GraphVehicle");
      database.createVertexType("GraphCar", vehicleClass);
      database.createVertexType("GraphMotocycle", "GraphVehicle");

      ODocument carNode = (ODocument) database.createVertex("GraphCar").field("brand", "Hyundai")
                    .field("model", "Coupe").field("year", 2003).save();
      ODocument motoNode = (ODocument) database.createVertex("GraphMotocycle").field("brand", "Yamaha")
                    .field("model", "X-City 250").field("year", 2009).save();

      database.createEdge(carNode, motoNode).save();
      ...
   }
   ...
}

Direct access

If you need direct access to any ODatabase use play.modules.orientdb.ODB

ODatabaseDocumentTx docdb = ODB.openDocumentDB();

and so on

Hooks and Listeners

To register RecordHooks or DatabaseListeners, implement the proper interface (ORecordHook or ODatabaseListener) and put the code under /app, the module will find and register these classes at startup.

References

//

orientdb's People

Contributors

mfornos avatar tazmaniax 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

Watchers

 avatar  avatar  avatar

orientdb's Issues

Updated to orientdb-rc9

Hi Marc,

I have cloned a copy of your source and tried to update the play module to orientdb rc9.

However, I cannot get the compiled modules to be used as locally for testing. I have tried to create a local repository as specified in the play documentation but there are some path issues which caused Model class problems on my system.

Can I commit my code so that you can take a quick look and test on your system? I have no idea how to do this in git (sorry!). Basically what I have done is

  1. Copied all the rc9 jar files from orientdb distribution into /lib.
  2. Changed dependencies.yml to "self: play -> orientdb 0.1.2"
  3. Changed all references of OObjectIteratorMultiCluster to OObjectIteratorCluster.
  4. Upcast the classes to Object and downcast them to OObjectIteratorCluster in OModelLoader.java:
  • return toList((OObjectIteratorCluster) (Object)ODB.openObjectDB().browseClass(field.getType()) );
  • return toList((OObjectIteratorCluster) (Object)ODB.openObjectDB().browseClass(fieldType));
    Notice the (Object) casting before re-casting to (OObjectIteratorCluster). If this is not done, it will throw a inconvertible type exception - a known bug in java for some time:
    http://weblogs.java.net/blog/carcassi/archive/2010/04/09/two-problems-generics-java-0

    ant build was fine.

    Please let me know what I should do :D I really really want my play project to work with rc9 or even 1.0 if it is released! :D

Problem injecting OGraphDB

There's a bug in the order of injection evaluation. OGraphDb must be evaluated before DocumentTx in DatabaseSource:

public <T> T getBeanOfType(Class<T> clazz) {
        if (ODatabaseObjectTx.class.isAssignableFrom(clazz)) {
            return (T) objectDB;
        } else if (OGraphDatabase.class.isAssignableFrom(clazz)) {
            return (T) documentDB;
        } else if (ODatabaseDocumentTx.class.isAssignableFrom(clazz)) {
            return (T) graphDB;
        } else {
            return null;
        }
    }

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.