Wiki

Clone wiki

limeds-framework / Modules_H2_Storage

Storage is an important aspect of many applications. To get you up and running quickly, we've developed a small embedded key/value store based around the underlying implementation of the Java H2 database. To install this module, browse to the following URL:

http://[limeds_host]:[limeds_port]/_limeds/installables/org.ibcn.limeds.h2/latest/deploy

The H2 module provides a factory Segment called limeds.storage.DefaultFactory. When importing it in your Slice (using the Visual Editor), a dialog will prompt for the type of database operation you want to perform and the name of the instance to work with (i.e. the database collection, new collection is created if the id doesn't exist):

h2_1.png

Store

When store is selected as the database operation, you can save a JSON object to the specified collection as follows:

var objectToStore = {
  //...
};

//Apply returns the String id of the stored object
var id = dbStore.apply(objectToStore);

If the object doesn't contain an "id" attribute, a UUID String is generated and the object is saved as a new entry using the id as key. If the object does contain an id, the object is stored in the mapping using this id, implying that existing entries can be overridden (use this mechanism to update entries).

Query

The H2 module provides a simple query by example mechanism to retrieve entries, e.g. to retrieve all user account of type "moderator", one could write:

var query = {
  type: "moderator"
};

//The result is an array of matching entries (can be empty)
var results = dbQuery.apply(query);

The query API will be expanded with comparison operators and regex capabilities in future releases!

Remove

To remove an entry, it suffices to know the id of the object:

var someObject = {
  //...
};

//Remove the object entry
dbRemove.apply(someObject);

//Alternatively, if we only know the id:
var deleteQuery = {
  id: getIdToDelete()
};

//The operation returns the removed entry
var deletedEntry = dbRemove.apply(deleteQuery);

List

The following snippet lists all the entries in the collection:

var results = dbList.apply();

When using the H2 module in Java it is better to use it as an OSGi service through the LimeDS Storage interface:

@Segment
public StorageExample implements FunctionalSegment {

  @Service(filter="storage.type=h2-mvstore")
  private Storage storage;

  @Override
  public JsonValue apply(JsonValue... input) {
    //Create DAO instance for collection id
    DAO dao = getDaoInstance("demo");

    return dao.findAll();
  }

}

More information on the available DAO operations can be found here.

There is also a module (currently in beta) that provides a similar feature set, based around MongoDB. To install this module, browse the following URL:

http://[limeds_host]:[limeds_port]/_limeds/installables/org.ibcn.limeds.mongodb/latest/deploy

Updated