Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting multiple mongo backends #42

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3c2f730
adding support for multiple morphia backends through a new model anno…
sumdan Oct 14, 2011
2db90a1
remove bad file
sumdan Oct 14, 2011
c564650
first commit
sumdan Oct 14, 2011
49d8f24
Merge branch 'master' of github.com:dbusch/play-morphia
sumdan Oct 14, 2011
cf88384
migrated plugin to use Play's dependency management
sumdan Oct 17, 2011
4f10d5f
add lib directory to .gitignore
sumdan Oct 17, 2011
3aa02a7
add lib directory to .gitignore
sumdan Oct 17, 2011
1e30f5d
Merge branch 'master' of https://github.com/greenlaw110/play-morphia
dbusch Oct 24, 2011
c58e04d
merge latest play-morphia base code. update module to use dependenci…
sumdan Oct 25, 2011
28f0339
Revert "merge latest play-morphia base code. update module to use de…
sumdan Oct 25, 2011
5f2d15f
merge lastest 1.2.4 plugin code.
sumdan Oct 25, 2011
27e40aa
try to get unit tests to pass, almost there
sumdan Oct 25, 2011
973ec73
fix count method enhancement
sumdan Oct 26, 2011
c3ae3ac
stick with mongo 2.6.5
sumdan Oct 26, 2011
358d0e8
remove debug line
sumdan Oct 26, 2011
307b42d
fix count method from Model enhancements
dbusch Oct 27, 2011
061ee30
fix lifecycle events. update version to 1.2.4
sumdan Oct 27, 2011
4f4296d
merge
sumdan Oct 27, 2011
20ddef9
merge latest devlop changes
sumdan Oct 27, 2011
44ab45d
rename module org
sumdan Oct 27, 2011
68591a2
merge latest from upstream
sumdan Nov 7, 2011
087fd82
merge in latest upstream changes
sumdan Nov 10, 2011
3b28f54
merge
sumdan Nov 20, 2011
3c73e7d
merge
sumdan Nov 20, 2011
26f8ece
merge
sumdan Nov 20, 2011
0015978
merge
sumdan Nov 20, 2011
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ build.bat
.classpath
.project
.eclipse
play-morphia.jar
dist
/eclipse
Empty file added README
Empty file.
308 changes: 154 additions & 154 deletions README.textile
Original file line number Diff line number Diff line change
@@ -1,155 +1,155 @@
h1. PlayMorphia overview
The PlayMorphia module a powerful and yet easy to use framework that provides "MongoDB":http://www.mongodb.org/ access to your Play!framework application. This module is built on top of the famous "Morphia project":http://code.google.com/p/morphia/.
Stick to the philosophy of Play!Framework, PlayMorphia provides a data layer infrastructure to help developer focusing on the business logic instead of technical details. PlayMorphia is by far the most welcome MongoDB access module in the community
h2. <a name="model">Easy to use Model class</a>
Thanks to the super powerful code enhancement mechanism of Play!framework, we could bring you a very simple model class and provides all weapons you need in the same time.
You define a model class as:
bc. @Entity
public class User extends Model {
public String fName;
public String lName;
public String region;
public String department;
public int age;
public int score;
public User(String firstName, String lastName, region, department) {
this.fName = firstName;
this.lName = lastName;
this.region = region;
this.department = department;
}
}
You use the model in your application in the following ways:
bc. User user = new User("John", "Smith", "AU", "IT").save()
Object id = user.getId();
...
User user = User.findById(id);
...
user.region = "ZH";
user.department = "Sales";
user.save();
...
user.delete();
h2. <a name="query">Powerful query interface</a>
As shown above you could use @Model.findById@ interface to fetch an entity by it's identifier. But how to query data using non-id properties? Don't worry, PlayMorphia provides comprehensive query interfaces enable you to dig out every bit of data in whatever way you want:
bc. List<User> allUsers = User.findAll();
...
List<User> auUsers = User.find("region", "AU").asList();
...
List<User> auITUsers;
auITUsers = User.find("region department", "AU", "IT").asList();
auITUsers = User.find("region,department", "AU", "IT").asList();
auITUsers = User.find("byRegionAndDepartment", "AU", "IT").asList();
auITUsers = User.q().filter("region", "AU").filter("department", "IT").asList();
auITUsers = User.q().findBy("region department", "AU", "IT").asList();...
...
List<User> auLittleUsers = User.find("region", "AU").filter("age <", 18).asList();
h3. <a name="query-order">Sort data returned</a>
It's bit of easy to sort data in ascend or descend order:
bc. List<User> usersByAgeAsc = User.q().order("age").asList();
...
List<User> usersByAgeDes = User.q().order("-age").asList();
h3. <a name="query-page">Pagination Support</a>
Usually you couldn't present all data in one request. This is why we need "Pagination":http://en.wikipedia.org/wiki/Pagination. Leveraging morphia's query interface, PlayMorphia provides an easy way to support query pagination:
bc. List<User> users = User.q().offset(30).limit(10).asList();
h2. Statistics
"Map-Reduce":http://www.mongodb.org/display/DOCS/MapReduce is no doubt a powerful weapon provided by MongoDB for data aggregation. However it's by no means an easy tool in terms of Java programming. PlayMorphia module hide all the complexity behind a set of simple clean interfaces:
bc. long userCount = User.count();
long auUserCount = User.find("region", "AU").count();
int minAge = User._min("age");
int maxAge = User._max("age");
int totalScore = User._sum("score");
int averageScore = User._average("score");
...
// group aggregation interface is still so easy to use
AggregationResult minValueDistribution = User.groupMin("age", "region department");
int minAge_AU_IT = (int)minValueDistribution.get("region department", "AU", "IT");
...
h2. Entity Lifecycle callbacks
Sometimes it's interesting to listen to the lifecycle events of an entity and proceed with relevant logics. For example, you want to log an entry input, or you want to send an notification email on entry removed. PlayMorphia provides a set of lifecycle annotations to mark the methods to be called when certain event triggered:
bc. @Entity public class User {
public String fName;
public String lName;
@OnLoad void beforeLoad() {
Logger.info("user entity about to load data from mongodb");
}
@Loaded void afterLoad() {
Logger.info("user entity now filled with data from mongodb");
}
@OnAdd void beforeAddNew() {
Logger.info("About to create new user record");
}
@Added void afterAddNew() {
Logger.info("new user record created in the database");
}
@OnUpdate void beforeSaveUpdated(){
Logger.info("About to persist updated user into database");
}
@Updated void afterUpdatedSaved() {
Logger.info("Updated user data persisted to the database");
}
@OnDelete void beforeDelete() {
Logger.info("About to delete a user record from the database");
}
@Deleted void afterDelete() {
Logger.info("This user record is removed from mongodb");
}
@OnBatchDelete static void beforeBatchDelete(MorphiaQuery q) {
Logger.info("About to delete all records specified by the query");
}
@BatchDeleted static void afterBatchDelete(MorphiaQuery q) {
Logger.info("Records specified by the query has been deleted";
// Note q.count() will always return 0 in this method as the
// records specified by the query has already been removed
}
}
h2. Low level interface
The facilities directly provided by PlayMorphia can cover almost all cases you could encountered when dealing with MongoDB. Though I want to say that , you could go to the low level interfaces exposed by PlayMorphia.
h3. Morphia interface
Once you installed PlayMorphia you get everything out of Morphia already. But trust me you almost have no chance to use them:
bc. // get Morphia object
com.google.code.morphia.Morphia morphia = MorphiaPlugin.morphia();
// get Datastore
com.google.code.morphia.Datastore ds = MorphiaPlugin.ds();
// get AdvancedDatastore
com.google.code.morphia.AdvancedDatastore ds = (AdvancedDatastore)MorphiaPlugin.ds();
h3. The MongoDB Java Driver interface
This is listed here just for curious:
bc. // get Mongo object
com.mongodb.Mongo mongo = MorphiaPlugin.ds().getMongo();
p(note). **Give it a try**
h1. PlayMorphia overview

The PlayMorphia module a powerful and yet easy to use framework that provides "MongoDB":http://www.mongodb.org/ access to your Play!framework application. This module is built on top of the famous "Morphia project":http://code.google.com/p/morphia/.

Stick to the philosophy of Play!Framework, PlayMorphia provides a data layer infrastructure to help developer focusing on the business logic instead of technical details. PlayMorphia is by far the most welcome MongoDB access module in the community

h2. <a name="model">Easy to use Model class</a>

Thanks to the super powerful code enhancement mechanism of Play!framework, we could bring you a very simple model class and provides all weapons you need in the same time.

You define a model class as:

bc. @Entity
public class User extends Model {
public String fName;
public String lName;
public String region;
public String department;
public int age;
public int score;

public User(String firstName, String lastName, region, department) {
this.fName = firstName;
this.lName = lastName;
this.region = region;
this.department = department;
}
}

You use the model in your application in the following ways:

bc. User user = new User("John", "Smith", "AU", "IT").save()
Object id = user.getId();
...
User user = User.findById(id);
...
user.region = "ZH";
user.department = "Sales";
user.save();
...
user.delete();

h2. <a name="query">Powerful query interface</a>

As shown above you could use @Model.findById@ interface to fetch an entity by it's identifier. But how to query data using non-id properties? Don't worry, PlayMorphia provides comprehensive query interfaces enable you to dig out every bit of data in whatever way you want:

bc. List<User> allUsers = User.findAll();
...
List<User> auUsers = User.find("region", "AU").asList();
...
List<User> auITUsers;
auITUsers = User.find("region department", "AU", "IT").asList();
auITUsers = User.find("region,department", "AU", "IT").asList();
auITUsers = User.find("byRegionAndDepartment", "AU", "IT").asList();
auITUsers = User.q().filter("region", "AU").filter("department", "IT").asList();
auITUsers = User.q().findBy("region department", "AU", "IT").asList();...
...
List<User> auLittleUsers = User.find("region", "AU").filter("age <", 18).asList();

h3. <a name="query-order">Sort data returned</a>

It's bit of easy to sort data in ascend or descend order:

bc. List<User> usersByAgeAsc = User.q().order("age").asList();
...
List<User> usersByAgeDes = User.q().order("-age").asList();

h3. <a name="query-page">Pagination Support</a>

Usually you couldn't present all data in one request. This is why we need "Pagination":http://en.wikipedia.org/wiki/Pagination. Leveraging morphia's query interface, PlayMorphia provides an easy way to support query pagination:

bc. List<User> users = User.q().offset(30).limit(10).asList();

h2. Statistics

"Map-Reduce":http://www.mongodb.org/display/DOCS/MapReduce is no doubt a powerful weapon provided by MongoDB for data aggregation. However it's by no means an easy tool in terms of Java programming. PlayMorphia module hide all the complexity behind a set of simple clean interfaces:

bc. long userCount = User.count();
long auUserCount = User.find("region", "AU").count();
int minAge = User._min("age");
int maxAge = User._max("age");
int totalScore = User._sum("score");
int averageScore = User._average("score");
...
// group aggregation interface is still so easy to use
AggregationResult minValueDistribution = User.groupMin("age", "region department");
int minAge_AU_IT = (int)minValueDistribution.get("region department", "AU", "IT");
...

h2. Entity Lifecycle callbacks

Sometimes it's interesting to listen to the lifecycle events of an entity and proceed with relevant logics. For example, you want to log an entry input, or you want to send an notification email on entry removed. PlayMorphia provides a set of lifecycle annotations to mark the methods to be called when certain event triggered:

bc. @Entity public class User {
public String fName;
public String lName;
@OnLoad void beforeLoad() {
Logger.info("user entity about to load data from mongodb");
}
@Loaded void afterLoad() {
Logger.info("user entity now filled with data from mongodb");
}
@OnAdd void beforeAddNew() {
Logger.info("About to create new user record");
}
@Added void afterAddNew() {
Logger.info("new user record created in the database");
}
@OnUpdate void beforeSaveUpdated(){
Logger.info("About to persist updated user into database");
}
@Updated void afterUpdatedSaved() {
Logger.info("Updated user data persisted to the database");
}
@OnDelete void beforeDelete() {
Logger.info("About to delete a user record from the database");
}
@Deleted void afterDelete() {
Logger.info("This user record is removed from mongodb");
}
@OnBatchDelete static void beforeBatchDelete(MorphiaQuery q) {
Logger.info("About to delete all records specified by the query");
}
@BatchDeleted static void afterBatchDelete(MorphiaQuery q) {
Logger.info("Records specified by the query has been deleted";
// Note q.count() will always return 0 in this method as the
// records specified by the query has already been removed
}
}

h2. Low level interface

The facilities directly provided by PlayMorphia can cover almost all cases you could encountered when dealing with MongoDB. Though I want to say that , you could go to the low level interfaces exposed by PlayMorphia.

h3. Morphia interface

Once you installed PlayMorphia you get everything out of Morphia already. But trust me you almost have no chance to use them:

bc. // get Morphia object
com.google.code.morphia.Morphia morphia = MorphiaPlugin.morphia();
// get Datastore
com.google.code.morphia.Datastore ds = MorphiaPlugin.ds();
// get AdvancedDatastore
com.google.code.morphia.AdvancedDatastore ds = (AdvancedDatastore)MorphiaPlugin.ds();

h3. The MongoDB Java Driver interface

This is listed here just for curious:

bc. // get Mongo object
com.mongodb.Mongo mongo = MorphiaPlugin.ds().getMongo();

p(note). **Give it a try**

"Install the PlayMorphia plugin":http://www.playframework.org/modules/morphia and start developing your application with MongoDB!
2 changes: 1 addition & 1 deletion conf/dependencies.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
self: play -> morphia 1.2.4

require:
- play 1.2
- play 1.2
Loading