Ninja Framework module for easy integration of MognoDB and optional moprhia (a MongoDB ORM)
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
.travis.yml
LICENSE
README.md
pom.xml
release-snapshot.sh
release.sh

README.md

MongoDB module for Ninja framework

This is an easly plugable module for the Ninja web framework to work with MongoDB and optional moprhia (a MongoDB ORM) by providing convinent services, all required dependencies and some testing utilities.

Morphia is a lightweight type-safe library for mapping Java objects to/from MongoDB.

This is a recreation of the module of Sven Kubiak (svenkubiak). Unfortunately the github repository was deleted.

Build Status license Maven Central

Setup

  1. Add the ninja-mongodb dependency to your pom.xml:

    net.binggl ninja-mongodb-module x.x.x
  2. Configure the mongodb connection in your ninja application.conf (values show are the default values used by this module if no properties are provided)

    ninja.mongodb.host=localhost ninja.mongodb.port=27017 ninja.mongodb.dbname=MyMongoDB

    if mechanism is MONGO-X509: The x.509 certificate derived user name, e.g. "CN=user,OU=OrgUnit,O=myOrg,..."

    ninja.mongodb.user=
    ninja.mongodb.pass=

    possible values: SCRAM-SHA-1 (default)|MONGODB-CR|MONGO-X509

    ninja.mongodb.authMechanism=SCRAM-SHA-1 ninja.mongodb.authdb=MyMongoDB ninja.mongodb.connectonstart=true ninja.morphia.package=models ninja.morphia.init=true

ninja.morphia.package, ninja.mongodb.user and ninja.mongodb.pass are optional.

If connectonstart is set to 'false' the connection can be later initiated by invoking the 'connect' method.

mongoDB.connect();

The same approach is possible to initiate the morphia module. This is done automatically, or can be controlled individually.

mongoDB.initMorphia();

If ninja.mongodb.connectonstart is 'false' and ninja.morphia.init is 'true' no init is done - because a connection is needed!

  1. Inject the ninja-morphia service where needed

    @Inject private MyDataService(MongoDB mongoDB) { this.mongoDB = mongoDB; }

  2. The MongoDB instance handles the connection and gives you the MongoClient to interact with MognoDB

    this.mongoDB.getMongoClient()

If you want to use Morphia, you can get the Morphia instance or the Morphia Datastore

this.mongoDB.getMorphia()
this.mongoDB.getDatastore()

The MongoDB instance also has some convenient methods for interacting with Morphia

public abstract <T extends Object> T findById(Object id, Class<T> clazz);
public abstract <T extends Object> List<T> findAll(Class<T> clazz);
public abstract <T extends Object> long countAll(Class<T> clazz);
public abstract void save(Object object);
public abstract void delete(Object object);
public abstract <T extends Object> void deleteAll(Class<T> clazz);
public abstract void dropDatabase();

The ninja-morphia-module provides a superclass with a ObjectId member variable and an appropriate getter method. You may extend your model classes if you want to.

@Entity
public class MyEntity extends MorphiaModel

Please note that you still have to annotate your model classes with morphia annotations!