An open API service indexing awesome lists of open source software.

https://github.com/emfjson/emfjson-mongo

MongoDB adapter for EMF
https://github.com/emfjson/emfjson-mongo

Last synced: 4 months ago
JSON representation

MongoDB adapter for EMF

Awesome Lists containing this project

README

          

# MongoDB Adapter for Eclipse Modeling Framework (EMF)

[![Build Status](https://secure.travis-ci.org/emfjson/emfjson-mongo.png)](http://travis-ci.org/emfjson/emfjson-mongo)

An easy to use adapter that works on top of the EMF Resource API to store and retrieve EMF Models on MongoDB.

### Download

Add the following dependency to your pom file.

```xml

org.emfjson
emfjson-mongo
0.3.0

```

You can also find the jars in [maven central](http://search.maven.org/#search|ga|1|emfjson-mongo)

### Usage

Setup the ResourceSet:

```java
ResourceSet resourceSet = new ResourceSetImpl();

resourceSet.getPackageRegistry().put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new JsonResourceFactory());
resourceSet.getURIConverter().getURIHandlers().add(0, new MongoHandler());
```

Create a Resource with a URI pointing to MongoDB. The URI must contains 3 segments identifying the database, the collection and the document id.

The URI should have the form ```mongodb://{host}[:{port}]/{db}/{collection}/{id}```

```java
URI uri = URI.createURI("mongodb://localhost:27017/emfjson-test/models/model1");
Resource resource = resourceSet.createResource(uri);
```

Alternatively, you can use a URI mapping:

```java
resourceSet.getURIConverter().getURIMap().put(
URI.createURI("http://resources/"),
URI.createURI("mongodb://localhost:27017/emfjson-test/models/"));

Resource resource = resourceSet.createResource(URI.createURI("http://resources/model1"));
```

Override the name of the field to avoid problem with the reserved name of Bson. And set the uri handler, to avoid problem with external resources.

```java
HashMap DEFAULT_OPTIONS = new HashMap();
DEFAULT_OPTIONS.put(EMFJs.OPTION_URI_HANDLER, new IdentityURIHandler());
DEFAULT_OPTIONS.put(EMFJs.OPTION_REF_FIELD, "_ref");
resourceSet.getLoadOptions().putAll(DEFAULT_OPTIONS);
```

Saving documents

```java
EPackage p = EcoreFactory.eINSTANCE.createEPackage();
p.setName("p");

EClass c = EcoreFactory.eINSTANCE.createEClass();
c.setName("A");

p.getEClassifiers().add(c);

resource.getContents().add(p);
resource.save(null);
```

Loading document

```java
resource.load(null);
```