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
- Host: GitHub
- URL: https://github.com/emfjson/emfjson-mongo
- Owner: emfjson
- License: epl-1.0
- Created: 2014-10-02T16:06:52.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2017-08-30T08:16:12.000Z (almost 9 years ago)
- Last Synced: 2026-01-14T18:26:47.578Z (5 months ago)
- Language: Java
- Homepage:
- Size: 70.3 KB
- Stars: 9
- Watchers: 3
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MongoDB Adapter for Eclipse Modeling Framework (EMF)
[](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);
```