Permalink
Browse files

got basics working

  • Loading branch information...
1 parent d0f24d6 commit 051a51a50d6976d3af33ad37312eab1e5d87454d billy committed Oct 18, 2009
Showing with 177 additions and 4 deletions.
  1. +73 −0 Mongo.cfc
  2. +27 −3 MongoDB.cfc
  3. +77 −1 MongoDBTest.cfc
View
73 Mongo.cfc
@@ -0,0 +1,73 @@
+<cfcomponent output="false">
+<!--- Maybe should extend an Abstract Mongo? --->
+
+<cfscript>
+//And this is configurable.
+server_name = 'localhost';
+server_port = 27017;
+db_name = 'default_db';
+collection_name = 'default_collection';
+
+//maybe this goes in super class
+mongo = createObject('java', 'com.mongodb.Mongo').init( variables.server_name , variables.server_port );
+db = mongo.getDb(db_name);
+collection = db.getCollection(collection_name);
+
+
+//------------------------------------------------------------------------//
+
+function put(){
+ var doc = createObject('java', 'com.mongodb.BasicDBObject').init();
+ if(arguments.size() eq 1){
+ doc.putAll(arguments[1]);
+ }
+ else {
+ doc.put(arguments[1],arguments[2]);
+ }
+
+ return collection.insert(doc).get("_id");
+}//end function
+
+
+
+function get(field,value){
+ var q = createObject('java', 'com.mongodb.BasicDBObject').init(field,value);
+ var cursor = collection.find(q);
+ return cursor.next();
+} //end function
+
+
+function count(){
+ return collection.getCount();
+}
+
+function find(){
+
+} //end function
+
+
+
+function delete(field,value){
+ var q = createObject('java', 'com.mongodb.BasicDBObject').init(field,value);
+ return collection.remove(q); //id's of deleted items
+} //end function
+
+
+function update(o){
+ var q = createObject('java', 'com.mongodb.BasicDBObject').init(o);
+ return collection.save(q);
+} //end function
+
+
+
+//not tested ...
+function switchCollection(collection_name){
+ collection = db.getCollection(collection_name);
+}
+
+</cfscript>
+
+<cffunction name="dump">
+ <cfdump var="#doc#">
+</cffunction>
+</cfcomponent>
View
30 MongoDB.cfc
@@ -4,6 +4,7 @@ variables.server_name = 'localhost';
variables.server_port = 27017;
variables.mongo = chr(0);
variables.db_name = "default_db";
+variables.collection_name = "default_collection";
/********************************
mongo = createObject('java', 'com.mongodb.Mongo').init( "localhost" , 27017 );
@@ -18,16 +19,39 @@ function init(){
if( arguments.size() gte 2 ) setPort(arguments[2]);
if( arguments.size() gte 3 ) setDatabase(arguments[3]);
variables.mongo = createObject('java', 'com.mongodb.Mongo').init( getServer() , getPort() );
- variables.db_name = getDatabase();
+ variables.db_name = getDBName();
return this;
}
-function getDatabase(){
+function getDb() {
+ if( arguments.size() ) {
+ return variables.mongo.getDB( arguments[1] );
+ }
+ else{
+ return variables.mongo.getDB( variables.db_name ); //default name
+ }
+}
+
+
+
+
+/*********************************************
+ acessor methods
+***********************************************/
+function setCollection(collection_name) {
+ variables.collection_name = arguments.collection_name;
+}
+
+function getCollection() {
+ return variables.collection_name;
+}
+
+function getDBName(){
return variables.db_name;
}
-function setDatabase(db_name){
+function setDBName(db_name){
variables.db_name = arguments.db_name;
}
View
78 MongoDBTest.cfc
@@ -1,16 +1,92 @@
<cfcomponent output="false" extends="mxunit.framework.TestCase">
<cfscript>
+ friend = {
+ name="f",
+ email = 'f@f.org',
+ address = {
+ street = '123 f st',
+ city = 'f',
+ state = 'f',
+ zip = '12345',
+ country = 'usa'
+ }
+ };
- function mongoShouldConnect() {
+ person = {
+ name="bill",
+ email = 'bill@mxunit.org',
+ address = {
+ street = '123 main st',
+ city = 'anytown',
+ state = 'here',
+ zip = '12345',
+ country = 'usa'
+ },
+ friend=friend
+ };
+
+function $endToEndSyntax(){
+ mongo = createObject('component','Mongo');
+ id = mongo.put(person); //name/value or struct
+ id2 = mongo.put('asdasd','xcvxcvxc');
+ the_guy = mongo.get('_id',id);
+ debug(id.toString());
+ debug(id2.toString());
+ the = mongo.get('_id',id2);
+ //debug(the);
+
+ debug( mongo.count() );
+
+ return;
+ mongo.delete('NAME','bill');
+ mongo.delete('asdasd','xcvxcvxc');
+ debug( mongo.count() );
+}
+
+
+
+function $updatePerson(){
+ mongo = createObject('component','Mongo');
+ id = mongo.put(person); //name/value or struct
+ person.name = 'ed';
+ newperson = mongo.update(person);
+ debug(newperson);
+
+
+ mongo.delete('NAME','bill');
+ mongo.delete('NAME','ed');
+
+}
+
+
+
+ person ={
+ name='bill',
+ email='bill@bill.com'
+ };
+
+
+ function basicDbTest() {
+ db = mongo.getDB("mydb");
+ assertEquals("com.mongodb.DBTCP", db.getClass().getName() );
+
+ coll = db.getCollection("testCollection");
+
+ doc = createObject('java', 'com.mongodb.BasicDBObject').init();
+ doc.putAll(person);
+
+ coll.insert(doc);
+
}
function smokeServerPort(){
debug("these are defaults");
assert( 'localhost'== mongo.getServer() );
assert( '27017'== mongo.getPort() );
+ assert( 'default_db'== mongo.getDBName() );
}
function mongoShouldBeCool() {

0 comments on commit 051a51a

Please sign in to comment.