Simple Mongo Object Framework (SMOF) is an ORM built in Java for MongoDB. SMOF relieves the burden of dealing with object serialization and deserialization, as well as managing such objects in a data store. Furthermore, in order to achieve optimal performance, SMOF caches objects through the Guava cache and uses ByteBuddy to lazy load objects from the database. Unlike other MongoDB ORMs, SMOF is able to deal with complex object hierarchy schemas.
So, consider this simple Bottle class:
public static class Bottle extends AbstractElement {
private static final String CAPACITY = "capacity";
private static final String AMOUNT = "liquid_amount";
private static final String LIQUID = "liquid";
@SmofString(name = LIQUID)
private String liquid;
@SmofNumber(name = AMOUNT)
private double amount;
@SmofNumber(name = CAPACITY)
private double capacity;
public Bottle(String liquid, double capacity) {
this(liquid, capacity, 0.0);
}
@SmofBuilder
public Bottle(@SmofParam(name=LIQUID) String liquid,
@SmofParam(name = CAPACITY) Double capacity,
@SmofParam(name = AMOUNT) Double amount) {
this.liquid = liquid;
this.capacity = capacity;
this.amount = amount;
}
public boolean isFull() {
return capacity == amount;
}
public double fill(Double amount) {
final double left = capacity-amount;
if(left < amount) {
this.amount = capacity;
return amount-left;
}
this.amount += amount;
return left-amount;
}
}
In order to perform some write operations (insert, update), all we have to do is:
public static void main(String[] args) {
//create the smof object with host, port and database name
final Smof smof = Smof.create("localhost", 27017, "myDB");
//create a new bottle
final Bottle bottle = new Bottle("water", 1.0);
//create a collection and map it to a type
smof.createCollection("bottles", Bottle.class);
//saves the bottle
smof.insert(bottle);
//fill the bottle
bottle.fill(0.5);
//update the object on the database
smof.replace(Bottle.class, bottle);
smof.close();
}
The API is quite straightforward as this example shows. Check the Getting Started section for further information.
Not at all! This is by far the simplest scenario. With SMOF you can:
- Have multiple subtypes pointing to the same collection
Aa
andAb
both implementA
- You map
A
to collectionColA
- objects of type
Aa
andAb
are automatically saved toColA
- Use referencing
- Type
A
is mapped toColA
andB
is mapped toColB
A
has a reference toB
- When you store
A
,B
is automatically stored inColB
(how cool/useful is that?)
- Type
- Lazy-load
- Crazy models like:
A
referencesB
that referencesC
that referencesD
.... that referencesZ
- When you load
A
from the database,B
(and so forth) will only be loaded the first time you access it!
- Crazy models like:
- So much more! (see Wiki)
Despite being simple, SMOF carries a lot of elaborate features that need to be documented in great detail, so I added a Wiki which I recommend if you're planning on using the framework.
Also, checkout the examples package for common uses of this project.
And by all means, feel free to ask me any question :)
SMOF is an open project, thus it welcomes new contributors. Follow the general Github Flow to contribute to the project! Also, feel free to contact me :)