Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

check in prior to branch

  • Loading branch information...
commit 340bd98938e1b73a4203e3054f3f10aafe4a7c0f 1 parent 9316b4e
billy authored
17 Blog.cfc
... ... @@ -0,0 +1,17 @@
  1 +<cfcomponent output="false">
  2 +<cfscript>
  3 +this.title = '';
  4 +this.body = '';
  5 +this.tags = [];
  6 +this.author = '';
  7 +this.pub_data = now();
  8 +this.comments = [
  9 + comment= {
  10 + comment_author = 'Coward',
  11 + comment_email = '',
  12 + comment_www = '',
  13 + comment_text = ''}
  14 + ];
  15 +
  16 +</cfscript>
  17 +</cfcomponent>
11 Mixin.cfc
... ... @@ -0,0 +1,11 @@
  1 +<cfcomponent output="false">
  2 +<cfscript>
  3 +foo = 'bar';
  4 +id = 123;
  5 +
  6 +function bar(){
  7 + return foo;
  8 +}
  9 +</cfscript>
  10 +
  11 +</cfcomponent>
41 Mongo.cfc
@@ -15,19 +15,17 @@ collection = db.getCollection(collection_name);
15 15
16 16
17 17 //------------------------------------------------------------------------//
  18 +function add(key,value,o){
  19 + //add key value pair to object ... todo
  20 +}
18 21
19   -function put(){
  22 +function put(o){
20 23 var doc = createObject('java', 'com.mongodb.BasicDBObject').init();
21   - //var o = arguments[1];
22   - if(arguments.size() eq 1){
23   - doc.putAll(arguments[1]);
24   - arguments[1]._id = doc.get("_id");
25   - }
26   - else {
27   - doc.put(arguments[1],arguments[2]);
28   - }
29   -
30   - return collection.insert(doc).get("_id");
  24 + var id = chr(0);
  25 + doc.putAll(o);
  26 + id = collection.insert(doc).get("_id");
  27 + o._id = id;
  28 + return id;
31 29 }//end function
32 30
33 31
@@ -48,8 +46,16 @@ function find(){
48 46 } //end function
49 47
50 48
  49 +function delete(o){
  50 + var obj = get("_id", o._id);
  51 + return collection.remove(obj); //id's of deleted items
  52 +} //end function
  53 +
  54 +
51 55
52   -function delete(field,value){
  56 +
  57 +//old
  58 +function __delete(field,value){
53 59 var q = createObject('java', 'com.mongodb.BasicDBObject').init(field,value);
54 60 return collection.remove(q); //id's of deleted items
55 61 } //end function
@@ -62,8 +68,15 @@ function delete(field,value){
62 68 //in the structure.
63 69
64 70 //update by object id ..._id
65   -function update(_id,o){
66   - var obj = get("_id", _id);
  71 +
  72 +//testing if pass in map what it return
  73 +function findSame(o){
  74 + var new_object = createObject('java', 'com.mongodb.BasicDBObject').init(o);
  75 + return collection.findOne(new_object);
  76 +}
  77 +
  78 +function update(o){
  79 + var obj = get("_id", o._id);
67 80 var new_object = createObject('java', 'com.mongodb.BasicDBObject').init(o);
68 81 return collection.update(obj, new_object, false, false);
69 82
40 MongoBlogTest.cfc
... ... @@ -0,0 +1,40 @@
  1 +<cfcomponent output="false" extends="mxunit.framework.TestCase">
  2 +<cfscript>
  3 + mongo = createObject('component', 'MongoDB');
  4 +
  5 + function testBlog() {
  6 +
  7 + blog = createObject('component', 'Blog');
  8 + blog.title = 'Look Ma. No SQL ...';
  9 + blog.text = 'Rapid development with MongoDB';
  10 + blog.author = 'bill';
  11 + /*
  12 + blog.author = 'billy';
  13 + blog.title = 'blog test';
  14 + blog.tags = ['politics','food','satire'];
  15 + blog.body = '<h1>testing ...</h1>';
  16 + blog.comments[1].author = 'Foo';
  17 + blog.comments[1].comment_text = 'whatever';
  18 + blog.comments[2].author = 'Bar';
  19 + blog.comments[2].comment_www = 'http://google.com';
  20 + blog.comments[2].comment_text = 'cool';
  21 + */
  22 +
  23 + mongo.put(blog);
  24 +
  25 + // debug(blog);
  26 +
  27 + // blog.author = 'ed';
  28 + // mongo.update(blog);
  29 +
  30 + // mongo.delete(blog);
  31 +
  32 + bb = mongo.findOne();
  33 + debug(bb);
  34 + }
  35 +
  36 +
  37 +
  38 +
  39 +</cfscript>
  40 +</cfcomponent>
109 MongoDB.cfc
... ... @@ -1,75 +1,80 @@
1 1 <cfcomponent output="false">
  2 +<!--- Maybe should extend an Abstract Mongo? --->
  3 +
2 4 <cfscript>
3   -variables.server_name = 'localhost';
4   -variables.server_port = 27017;
5   -variables.mongo = chr(0);
6   -variables.db_name = "default_db";
7   -variables.collection_name = "default_collection";
  5 +//And this is configurable.
  6 +server_name = 'localhost';
  7 +server_port = 27017;
  8 +db_name = 'default_db';
  9 +collection_name = 'default_collection';
8 10
9   -/********************************
10   - mongo = createObject('java', 'com.mongodb.Mongo').init( "localhost" , 27017 );
11   - //debug(mongo);
12   - db = mongo.getDB( "mydb" );
  11 +//maybe this goes in super class
  12 +mongo = createObject('java', 'com.mongodb.Mongo').init( variables.server_name , variables.server_port );
  13 +db = mongo.getDb(db_name);
  14 +collection = db.getCollection(collection_name);
13 15
14   -************************************/
15   -
16   -
17   -function init(){
18   - if( arguments.size() gte 1 ) setServer(arguments[1]);
19   - if( arguments.size() gte 2 ) setPort(arguments[2]);
20   - if( arguments.size() gte 3 ) setDatabase(arguments[3]);
21   - variables.mongo = createObject('java', 'com.mongodb.Mongo').init( getServer() , getPort() );
22   - variables.db_name = getDBName();
23   - return this;
  16 +
  17 +//------------------------------------------------------------------------//
  18 +function add(key,value,o){
  19 + //add key value pair to object ... todo
24 20 }
25 21
  22 +function put(o){
  23 + var doc = createObject('java', 'com.mongodb.BasicDBObject').init();
  24 + var id = chr(0);
  25 + doc.putAll(o);
  26 + id = collection.insert(doc).get("_id");
  27 + o._id = id; //add the _id object to the struct
  28 + return id;
  29 +}//end function
26 30
27   -function getDb() {
28   - if( arguments.size() ) {
29   - return variables.mongo.getDB( arguments[1] );
30   - }
31   - else{
32   - return variables.mongo.getDB( variables.db_name ); //default name
33   - }
34   -}
35 31
36 32
  33 +function get(field,value){
  34 + var q = createObject('java', 'com.mongodb.BasicDBObject').init(field,value);
  35 + var cursor = collection.find(q);
  36 + return cursor.next();
  37 +} //end function
37 38
38 39
39   -/*********************************************
40   - acessor methods
41   -***********************************************/
42   -function setCollection(collection_name) {
43   - variables.collection_name = arguments.collection_name;
44   -}
45 40
46   -function getCollection() {
47   - return variables.collection_name;
  41 +function count(){
  42 + return collection.getCount();
48 43 }
49 44
50   -function getDBName(){
51   - return variables.db_name;
52   -}
  45 +function findOne(){
  46 + return collection.findOne();
  47 +} //
53 48
54   -function setDBName(db_name){
55   - variables.db_name = arguments.db_name;
56   -}
  49 +function find(){
57 50
58   -function setServer(server_name){
59   - variables.server_name = arguments.server_name;
60   -}
  51 +} //end function
61 52
62   -function setPort(port){
63   - variables.server_port = port;
64   -}
65 53
  54 +function delete(o){
  55 + var obj = get("_id", o._id);
  56 + return collection.remove(obj); //id's of deleted items
  57 +} //end function
66 58
67   -function getPort(){
68   - return variables.server_port;
69   -}
70   -function getServer(){
71   - return variables.server_name;
  59 +
  60 +
  61 +function update(o){
  62 + var obj = get("_id", o._id);
  63 + var new_object = createObject('java', 'com.mongodb.BasicDBObject').init(o);
  64 + return collection.update(obj, new_object, false, false);
  65 +
  66 +} //end function
  67 +
  68 +
  69 +
  70 +//not tested ...
  71 +function switchCollection(collection_name){
  72 + collection = db.getCollection(collection_name);
72 73 }
73 74
74 75 </cfscript>
  76 +
  77 +<cffunction name="dump">
  78 + <cfdump var="#doc#">
  79 +</cffunction>
75 80 </cfcomponent>
20 MongoDBTest.cfc
@@ -26,6 +26,16 @@
26 26 friend=friend
27 27 };
28 28
  29 +
  30 +function testSerializeJSONThis(){
  31 + var p = createObject('component','Person');
  32 + var j = serializeJSON(p);
  33 + debug(j);
  34 + mongo = createObject('component','Mongo');
  35 + id = mongo.put(p);
  36 + o = mongo.get('_id',id);
  37 + debug(o);
  38 +}
29 39
30 40 function $endToEndSyntax(){
31 41 mongo = createObject('component','Mongo');
@@ -45,7 +55,15 @@ function $endToEndSyntax(){
45 55 debug( mongo.count() );
46 56 }
47 57
48   -
  58 +function $testFindSame(){
  59 + return;
  60 + mongo = createObject('component','Mongo');
  61 + //id = mongo.put(person); //name/value or struct
  62 + person.name = 'ed';
  63 + newperson = mongo.findSame(person);
  64 + debug(person);
  65 + debug(newperson);
  66 +}
49 67
50 68 function $updatePerson(){
51 69 mongo = createObject('component','Mongo');
80 MongoMapper.cfc
... ... @@ -0,0 +1,80 @@
  1 +
  2 +<!--- Maybe should extend an Abstract Mongo? --->
  3 +
  4 +<cfscript>
  5 +//And this is configurable.
  6 +server_name = 'localhost';
  7 +server_port = 27017;
  8 +db_name = 'default_db';
  9 +collection_name = 'default_collection';
  10 +
  11 +//maybe this goes in super class
  12 +mongo = createObject('java', 'com.mongodb.Mongo').init( variables.server_name , variables.server_port );
  13 +db = mongo.getDb(db_name);
  14 +collection = db.getCollection(collection_name);
  15 +
  16 +
  17 +//------------------------------------------------------------------------//
  18 +
  19 +function put(){
  20 + var doc = createObject('java', 'com.mongodb.BasicDBObject').init();
  21 + doc.putAll( this );
  22 + return collection.insert(doc).get("_id");
  23 +}//end function
  24 +
  25 +
  26 +
  27 +function get(field,value){
  28 + var q = createObject('java', 'com.mongodb.BasicDBObject').init(field,value);
  29 + var cursor = collection.find(q);
  30 + return cursor.next();
  31 +} //end function
  32 +
  33 +
  34 +function count(){
  35 + return collection.getCount();
  36 +}
  37 +
  38 +function _find(){
  39 +
  40 +} //end function
  41 +
  42 +
  43 +
  44 +function delete(field,value){
  45 + var q = createObject('java', 'com.mongodb.BasicDBObject').init(field,value);
  46 + return collection.remove(q); //id's of deleted items
  47 +} //end function
  48 +
  49 +
  50 +
  51 +//ideally, we would have an object that when called would update itself
  52 +//however, when working with structs, we need to tell MongoDB, which
  53 +//item we want to update, since we are not maintaining the string id
  54 +//in the structure.
  55 +
  56 +//update by object id ..._id
  57 +
  58 +//testing if pass in map what it return
  59 +function findSame(o){
  60 + var new_object = createObject('java', 'com.mongodb.BasicDBObject').init(o);
  61 + return collection.findOne(new_object);
  62 +}
  63 +
  64 +function update(_id,o){
  65 + var obj = get("_id", _id);
  66 + var new_object = createObject('java', 'com.mongodb.BasicDBObject').init(o);
  67 + return collection.update(obj, new_object, false, false);
  68 +
  69 +} //end function
  70 +
  71 +
  72 +
  73 +//not tested ...
  74 +function switchCollection(collection_name){
  75 + collection = db.getCollection(collection_name);
  76 +}
  77 +
  78 +</cfscript>
  79 +
  80 +
10 Person.cfc
... ... @@ -0,0 +1,10 @@
  1 +<cfcomponent output="false">
  2 +<cfscript>
  3 +this.name = 'bill';
  4 +this.email = 'bill@bill.com';
  5 +this.address = {
  6 + street = '123 main',
  7 + city = 'anytown'
  8 +};
  9 +</cfscript>
  10 +</cfcomponent>

0 comments on commit 340bd98

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