Skip to content
Browse files

added dbref impl

  • Loading branch information...
1 parent 80fa080 commit a95dd749512669bfbf6983e23ed4fcc550a9fc0f unknown committed
View
3 components/MongoConfig.cfc
@@ -1,6 +1,7 @@
<cfcomponent output="false" hint="Main configuration information for MongoDb connections. Defaults are provided, but should be changed as needed. ">
<cfscript>
-
+ //To Do: Add various deployment environments: dev,test,stagging, production...
+
//Default values
this.defaults = {
server_name = 'localhost',
View
39 components/MongoDB.cfc
@@ -1,28 +1,23 @@
<cfcomponent output="false" hint="Facade for Mongo DB. 90% of calls will go through this comonent.">
<cfscript>
-//This maybe should be a config object
-config = {
- server_name = 'localhost',
- server_port = 27017,
- db_name = 'default_db',
- collection_name = 'default_collection'
- };
+config = createObject('component','MongoConfig');
//maybe this goes in super class? Or make factory for returning mongos
/*--------------------------------------------------------------------
mongo1 = factory.createMongo(config);
--------------------------------------------------------------------*/
-mongo = createObject('java', 'com.mongodb.Mongo').init( variables.config.server_name , variables.config.server_port );
-db = mongo.getDb(config.db_name);
-collection = db.getCollection(config.collection_name);
+mongo = createObject('java', 'com.mongodb.Mongo').init( config.defaults.server_name , config.defaults.server_port );
+db = mongo.getDb(config.defaults.db_name);
+collection = db.getCollection(config.defaults.collection_name);
expression_builder = createObject('component', 'ExpressionBuilder') ;
+//Starting to smell ...
function init(config){
- variables.config = arguments.config;
- mongo = createObject('java', 'com.mongodb.Mongo').init( variables.config.server_name , variables.config.server_port );
+ config.defaults = arguments.config;
+ mongo = createObject('java', 'com.mongodb.Mongo').init( config.defaults.server_name , config.defaults.server_port );
db = mongo.getDb(config.db_name);
collection = db.getCollection(config.collection_name);
return this;
@@ -63,8 +58,10 @@ function config(){
builder = createObject('component','ExpressionBuilder');
+// TO DO: loose the reference to duplicate(this)!!!
+//This has a bad smell ...
function new_doc(collection_name){
- var document = createObject('component','MongoDocument').factory_init( collection_name, this );
+ var document = createObject('component','MongoDocument').factory_init( collection_name, duplicate(this) );
return document;
}
@@ -101,8 +98,11 @@ function get(field,value){
//when only a string id is available
function getById(id){
- var objId = createObject('java','com.mongodb.ObjectId').init(id);
- return get("_id", objId);
+ var str_id = id;
+ var objId = chr(0);
+ if(isObject(id)) str_id = id.toString();
+ obj_id = createObject('java','com.mongodb.ObjectId').init(str_id);
+ return get("_id", obj_id);
} //en
@@ -131,7 +131,6 @@ function delete(o){
//Note the ObjectId object. This creates an ObjectId from
//the string representation of
-
function deleteById(id){
var objId = createObject('java','com.mongodb.ObjectId').init(id);
var obj = get("_id", objId);
@@ -140,7 +139,7 @@ function deleteById(id){
function update(o){
- var obj = get("_id", o._id);
+ var obj = getById( o._id);
var new_object = createObject('java', 'com.mongodb.BasicDBObject').init(o);
return collection.update(obj, new_object, false, false);
@@ -151,7 +150,6 @@ function update(o){
//swtich to or create database
function getDB(db_name){
variables.db = mongo.getDb(db_name);
- this.db = mongo.getDb(db_name);
db = mongo.getDb(db_name);
return db;
}
@@ -180,6 +178,11 @@ function listToStruct(list){
}
+
+function dbRef(collection_name, id){
+ return createObject('java','com.mongodb.DBRef').init( db, collection_name, id);
+}
+
/*---------------------------------------------------------
Expression Builder Wrappers
---------------------------------------------------------*/
View
13 components/MongoDocument.cfc
@@ -75,21 +75,22 @@
<cfset var o_id = '' />
<cfset validate() />
<cfset o_id = mongo.put(this.model) />
- <cfset this.model['_id'] = o_id.toString() />
+ <cfset this.model['_id'] = o_id />
<cfreturn o_id />
</cffunction>
<cffunction name="delete" returntype="void" hint="Deletes this Document from the Collection">
- <cfset mongo.deleteById(this.model['_id']) />
+ <cfset mongo.delete(this.model) />
</cffunction>
+<!--- To Do: Update in-place using obj.update(property,value) --->
<cffunction name="update" hint="Performs in-place update of the the value for 'property'. NOT IMPLEMENTED">
- <cfargument name="property">
- <cfargument name="value">
- <cfset validate() />
- <cfthrow type="NotImplementedException" message="To Do.">
+ <cfargument name="property" required="false" />
+ <cfargument name="value" required="false" />
+ <cfset validate() />
+ <cfset variables.mongo.update(this.model) />
</cffunction>
View
1,535 doc/api/cfmongodb/components/.tmp_MongoDB.html.71003~
1,535 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
9 test/MongoConfigTest.cfc
@@ -0,0 +1,9 @@
+<cfcomponent extends="BaseTest" output="false">
+<cfscript>
+function testConfig(){
+ config = createObject('component','cfmongodb.components.MongoConfig');
+ debug(config);
+ assertEquals(config.defaults.collection_name, 'default_collection');
+}
+</cfscript>
+</cfcomponent>
View
68 test/ObjectMappingTest.cfc
@@ -0,0 +1,68 @@
+<cfcomponent extends="BaseTest" output="false">
+<cfscript>
+
+function dbRefTest(){
+ mongo = createObject('component','cfmongodb.components.MongoDB');
+
+ db = mongo.getDb('default_db');
+
+
+ prog = mongo.new_doc('programmers');
+ // //should be in mongo wrapper!
+ prog.set('name','bill');
+ pid = prog.save();
+
+ prog2 = mongo.new_doc('programmers');
+ // //should be in mongo wrapper!
+ prog2.set('name','pete');
+ pid2 = prog2.save();
+
+ //ref = createObject('java','com.mongodb.DBRef').init( db, 'programmers', pid);
+ ref1 = mongo.dbRef('programmers', pid);
+ ref2 = mongo.dbRef('programmers', pid2);
+ refs = [ ref1, ref2 ];
+
+ proj = mongo.new_doc('projects');
+ proj.set('foo','bar');
+ proj.set('programers', refs);
+
+ id = proj.save();
+ debug(id);
+ debug(proj);
+
+ progz = proj.get('programers');
+ debug(progz[1].fetch());
+
+ proj.delete();
+ prog.delete();
+
+}
+
+function testCreatePersonModel(){
+ var person = createObject( 'component','fixture.PersonModel');
+ person.foo = 'bar';
+ person.model.foo = 'barbar';
+ person.set('name','bill');
+ colors = ['red','green','blue'];
+ person.set('email','bill@bill.com');
+ person.set('street','123 main street');
+ person.set('city','anytown');
+ person.set('state','VA');
+ person.set('zip','12345');
+ person.set('colors', colors);
+ person.save();
+
+ //debug( getComponentMetaData('fixture.PersonModel') );
+
+ person.set('name','john');
+ person.update();
+
+ debug( person.model );
+
+ //no side effects
+ //person.delete();
+
+}
+
+</cfscript>
+</cfcomponent>
View
18 test/fixture/PersonModel.cfc
@@ -0,0 +1,18 @@
+<cfcomponent output="true" extends="cfmongodb.components.MongoDocument">
+
+ <cfproperty name="name" />
+ <cfproperty name="email" />
+ <cfproperty name="street" />
+ <cfproperty name="city" />
+ <cfproperty name="state" />
+ <cfproperty name="zip" />
+ <cfproperty name="colors" type="array" />
+ <cfset super.init( collection_name='members' ) />
+
+<!--- Representation invariant. To Do --->
+ <cffunction name="validate" returntype="void" hint="Called before save(). Should throw exception on failure.">
+
+ </cffunction>
+
+
+</cfcomponent>
View
4 test/fixture/SomeCFCWithProperties.cfc
@@ -2,8 +2,8 @@
<cfproperty name="name" default="bill" />
<cfproperty name="address" />
- <cfproperty name="city" />
- <cfset super.init( collection_name='members' ) />
+ <cfproperty name="city" />
+ <cfset super.init( collection_name='members' ) />
<!--- Representation invariant --->

0 comments on commit a95dd74

Please sign in to comment.
Something went wrong with that request. Please try again.