Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

defining Object Mapper

  • Loading branch information...
commit 18ae29654f7856e9d73b1e643a222524ecb2c1cc 1 parent 95a110d
billy authored
4 Mongo.cfc
@@ -5,10 +5,6 @@
5 5 function init(){
6 6 return createObject('component','components.MongoDB');
7 7 }
8   -
9   -
10   -
11   -
12 8 </cfscript>
13 9
14 10
28 components/Document.cfc
... ... @@ -1,15 +1,18 @@
1 1 <cfcomponent output="false" implements="IDocument" hint="Default implementation of IDocument and used by the Mongo class to generate a document.">
2 2
3 3 <cfset this.__props__ = structNew() />
4   -<cfset mongo = createObject('component', 'MongoDB') />
  4 +<cfset variables.mongo = createObject('component', 'MongoDB') />
5 5
6 6
7   -<cffunction name="init" hint="Constructor">
8   - <cfargument name="collection_name" type="string" required="true" />
9   - <cfset mongo.collection(collection_name) />
  7 +<cffunction name="init" hint="Constructor. Creates an instance of a MongoDocument.">
  8 + <cfargument name="collection_name" type="string" required="true" hint="The name of the collection to which this document is bound." />
  9 + <cfargument name="_mongo" type="any" required="false" default="#variables.mongo#" hint="The instance of the Mongo wrapper to which the document is bound." />
  10 + <cfset variables.mongo.collection(collection_name) />
  11 + <cfset variables.mongo = _mongo />
10 12 <cfreturn this />
11 13 </cffunction>
12 14
  15 +
13 16 <cffunction name="setCollection">
14 17 </cffunction>
15 18
@@ -33,8 +36,9 @@
33 36 <cfreturn ret_val />
34 37 </cffunction>
35 38
36   -<cffunction name="remove" returntype="void">
  39 +<cffunction name="remove" returntype="void" hint="Removes a property form a Document. **NOT IMPLEMENTED**">
37 40 <cfargument name="property" hint="Removes the given property if it exists." />
  41 + <cfthrow type="NotImplementedException" message="To Do.">
38 42 </cffunction>
39 43
40 44
@@ -43,17 +47,21 @@
43 47 <cfreturn mongo.put(this.__props__) />
44 48 </cffunction>
45 49
  50 +
46 51 <cffunction name="delete" returntype="void" hint="Deletes this Document from the Collection">
47   - <cfargument name="id" hint="ID to remove" />
48   - <!--- todo: try ... catch --->
49   - <cfset mongo.deleteById(arguments.id.toString()) />
  52 + <cfset mongo.deleteById(this.__props__['_id']) />
50 53 </cffunction>
51 54
52   -<cffunction name="update">
53   - <cfargument name="field">
  55 +
  56 +<cffunction name="update" hint="Performs in-place update of the the value for 'property'. NOT IMPLEMENTED">
  57 + <cfargument name="property">
54 58 <cfargument name="value">
  59 + <cfthrow type="NotImplementedException" message="To Do.">
55 60 </cffunction>
56 61
57 62
  63 +<cffunction name="validate" hint="Should be called before save() to perform any required validation. NOT IMPLEMENTED" returntype="void">
  64 + <cfthrow type="NotImplementedException" message="To Do.">
  65 +</cffunction>
58 66
59 67 </cfcomponent>
4 components/IDocument.cfc
@@ -18,8 +18,10 @@
18 18 <cffunction name="save" hint="Commits this Document instance to the datastore and returns an ID" returntype="String"></cffunction>
19 19
20 20 <cffunction name="update" hint="Performs in-place updating - Instead of retrieving and updating every item within a document, this method is more efficient, allowing for 'Cherry Picking' of specific data elements to update. ">
21   - <cfargument name="field">
  21 + <cfargument name="property">
22 22 <cfargument name="value">
23 23 </cffunction>
24 24
  25 +<cffunction name="validate" hint="Should be called before save()" returntype="void"></cffunction>
  26 +
25 27 </cfinterface>
13 components/MongoConfig.cfc
... ... @@ -0,0 +1,13 @@
  1 +<cfcomponent output="false" hint="Main configuration information for MongoDb connections. Defaults are provided, but should be changed as needed. ">
  2 +<cfscript>
  3 +
  4 + //Default values
  5 + this.defaults = {
  6 + server_name = 'localhost',
  7 + server_port = 27017,
  8 + db_name = 'default_db',
  9 + collection_name = 'default_collection'
  10 + };
  11 +
  12 +</cfscript>
  13 +</cfcomponent>
22 components/MongoDB.cfc
@@ -19,6 +19,20 @@ db = mongo.getDb(config.db_name);
19 19 collection = db.getCollection(config.collection_name);
20 20 expression_builder = createObject('component', 'ExpressionBuilder') ;
21 21
  22 +
  23 +function init(config){
  24 + variables.config = arguments.config;
  25 + mongo = createObject('java', 'com.mongodb.Mongo').init( variables.config.server_name , variables.config.server_port );
  26 + db = mongo.getDb(config.db_name);
  27 + collection = db.getCollection(config.collection_name);
  28 + return this;
  29 +}
  30 +
  31 +
  32 +function config(){
  33 +
  34 +}
  35 +
22 36 /*---------------------------------------------------------------------
23 37
24 38 DSL for MongoDB searches:
@@ -50,7 +64,8 @@ expression_builder = createObject('component', 'ExpressionBuilder') ;
50 64 builder = createObject('component','ExpressionBuilder');
51 65
52 66 function new_doc(collection_name){
53   - return createObject('component','Document').init( collection_name );
  67 + var document = createObject('component','MongoDocument').init( collection_name, this );
  68 + return document;
54 69 }
55 70
56 71
@@ -135,7 +150,10 @@ function update(o){
135 150
136 151 //swtich to or create database
137 152 function getDB(db_name){
138   - return mongo.getDb(db_name);
  153 + variables.db = mongo.getDb(db_name);
  154 + this.db = mongo.getDb(db_name);
  155 + db = mongo.getDb(db_name);
  156 + return db;
139 157 }
140 158
141 159
67 components/MongoDocument.cfc
... ... @@ -0,0 +1,67 @@
  1 +<cfcomponent output="false" implements="IDocument" hint="Default implementation of IDocument and used by the Mongo class to generate a document.">
  2 +
  3 +<cfset this.model = structNew() />
  4 +<cfset variables.mongo = createObject('component', 'MongoDB') />
  5 +
  6 +
  7 +<cffunction name="init" hint="Constructor. Creates an instance of a MongoDocument.">
  8 + <cfargument name="collection_name" type="string" required="true" hint="The name of the collection to which this document is bound." />
  9 + <cfargument name="_mongo" type="any" required="false" default="#variables.mongo#" hint="The instance of the Mongo wrapper to which the document is bound." />
  10 + <cfset variables.mongo.collection(collection_name) />
  11 + <cfset variables.mongo = _mongo />
  12 + <cfreturn this />
  13 +</cffunction>
  14 +
  15 +
  16 +<cffunction name="setCollection">
  17 +</cffunction>
  18 +
  19 +
  20 +<cffunction name="set" hint="Sets a property for the Document" returntype="void">
  21 + <cfargument name="property" type="String" />
  22 + <cfargument name="value" type="Any" />
  23 + <cfset structInsert(this.model, arguments.property, arguments.value, true) />
  24 +</cffunction>
  25 +
  26 +
  27 +<cffunction name="get" hint="Fetches the value of the given property. Returns null if not found." returntype="Any">
  28 + <cfargument name="property" />
  29 + <cfset var ret_val = javacast('null', '') />
  30 + <cftry>
  31 + <cfset ret_val = this.model[arguments.property] />
  32 + <cfcatch type="Expression">
  33 + <!--- want to return null --->
  34 + </cfcatch>
  35 + </cftry>
  36 + <cfreturn ret_val />
  37 +</cffunction>
  38 +
  39 +<cffunction name="remove" returntype="void" hint="Removes a property form a Document. **NOT IMPLEMENTED**">
  40 + <cfargument name="property" hint="Removes the given property if it exists." />
  41 + <cfthrow type="NotImplementedException" message="To Do.">
  42 +</cffunction>
  43 +
  44 +
  45 +
  46 +<cffunction name="save" returntype="String">
  47 + <cfreturn mongo.put(this.model) />
  48 +</cffunction>
  49 +
  50 +
  51 +<cffunction name="delete" returntype="void" hint="Deletes this Document from the Collection">
  52 + <cfset mongo.deleteById(this.model['_id']) />
  53 +</cffunction>
  54 +
  55 +
  56 +<cffunction name="update" hint="Performs in-place update of the the value for 'property'. NOT IMPLEMENTED">
  57 + <cfargument name="property">
  58 + <cfargument name="value">
  59 + <cfthrow type="NotImplementedException" message="To Do.">
  60 +</cffunction>
  61 +
  62 +
  63 +<cffunction name="validate" hint="Should be called before save() to perform any required validation. NOT IMPLEMENTED" returntype="void">
  64 + <cfthrow type="NotImplementedException" message="To Do.">
  65 +</cffunction>
  66 +
  67 +</cfcomponent>
34 doc/docs/allclasses-frame.html
... ... @@ -0,0 +1,34 @@
  1 +
  2 +
  3 +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4 +<!--NewPage-->
  5 +<HTML>
  6 +<HEAD>
  7 +<!-- Generated by colddoc on {ts '2009-12-21 13:21:05'} -->
  8 +<TITLE>
  9 +All Classes
  10 +</TITLE>
  11 +
  12 +<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
  13 +
  14 +</HEAD>
  15 +
  16 +<BODY BGCOLOR="white">
  17 +<FONT size="+1" CLASS="FrameHeadingFont">
  18 +<B>All Classes</B></FONT>
  19 +
  20 +<BR>
  21 +
  22 +<TABLE BORDER="0" WIDTH="100%" SUMMARY="">
  23 +<TR>
  24 +<TD NOWRAP><FONT CLASS="FrameItemFont">
  25 +
  26 +</FONT>
  27 +</TD>
  28 +</TR>
  29 +</TABLE>
  30 +
  31 +</BODY>
  32 +</HTML>
  33 +
  34 +
42 doc/docs/index.html
... ... @@ -0,0 +1,42 @@
  1 +
  2 + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
  3 +<!--NewPage-->
  4 +<HTML>
  5 +<HEAD>
  6 +<!-- Generated by colddoc on {ts '2009-12-21 13:21:05'} -->
  7 +<TITLE>
  8 +Generated Documentation (Transfer version 1.1)
  9 +</TITLE>
  10 +<SCRIPT type="text/javascript">
  11 + targetPage = "" + window.location.search;
  12 + if (targetPage != "" && targetPage != "undefined")
  13 + targetPage = targetPage.substring(1);
  14 + if (targetPage.indexOf(":") != -1)
  15 + targetPage = "undefined";
  16 + function loadFrames() {
  17 + if (targetPage != "" && targetPage != "undefined")
  18 + top.classFrame.location = top.targetPage;
  19 + }
  20 +</SCRIPT>
  21 +<NOSCRIPT>
  22 +</NOSCRIPT>
  23 +</HEAD>
  24 +<FRAMESET cols="20%,80%" title="" onLoad="top.loadFrames()">
  25 +<FRAMESET rows="30%,70%" title="" onLoad="top.loadFrames()">
  26 +<FRAME src="overview-frame.html" name="packageListFrame" title="All Packages">
  27 +<FRAME src="allclasses-frame.html" name="packageFrame" title="All classes and interfaces (except non-static nested types)">
  28 +</FRAMESET>
  29 +<FRAME src="overview-summary.html" name="classFrame" title="Package, class and interface descriptions" scrolling="yes">
  30 +<NOFRAMES>
  31 +<H2>
  32 +Frame Alert</H2>
  33 +
  34 +<P>
  35 +This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client.
  36 +<BR>
  37 +Link to<A HREF="overview-summary.html">Non-frame version.</A>
  38 +</NOFRAMES>
  39 +</FRAMESET>
  40 +</HTML>
  41 +
  42 +
51 doc/docs/overview-frame.html
... ... @@ -0,0 +1,51 @@
  1 +
  2 +
  3 +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4 +<!--NewPage-->
  5 +<HTML>
  6 +<HEAD>
  7 +<!-- Generated by colddoc on {ts '2009-12-21 13:21:05'} -->
  8 +<TITLE>
  9 +Overview
  10 +</TITLE>
  11 +
  12 +<META NAME="keywords" CONTENT="Overview">
  13 +
  14 +<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
  15 +
  16 +
  17 +</HEAD>
  18 +
  19 +<BODY BGCOLOR="white">
  20 +
  21 +<TABLE BORDER="0" WIDTH="100%" SUMMARY="">
  22 +<TR>
  23 +<TH ALIGN="left" NOWRAP><FONT size="+1" CLASS="FrameTitleFont">
  24 +<B>Transfer version 1.1</B></FONT></TH>
  25 +</TR>
  26 +</TABLE>
  27 +
  28 +<TABLE BORDER="0" WIDTH="100%" SUMMARY="">
  29 +<TR>
  30 +<TD NOWRAP><FONT CLASS="FrameItemFont"><A HREF="allclasses-frame.html" target="packageFrame">All Classes</A></FONT>
  31 +<P>
  32 +<FONT size="+1" CLASS="FrameHeadingFont">
  33 +Packages</FONT>
  34 +<BR>
  35 +
  36 +
  37 +
  38 +
  39 +
  40 +<BR>
  41 +
  42 +</TD>
  43 +</TR>
  44 +</TABLE>
  45 +
  46 +<P>
  47 +&nbsp;
  48 +</BODY>
  49 +</HTML>
  50 +
  51 +
158 doc/docs/overview-summary.html
... ... @@ -0,0 +1,158 @@
  1 +
  2 +
  3 +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4 +<!--NewPage-->
  5 +<HTML>
  6 +<HEAD>
  7 +<!-- Generated by colddoc on {ts '2009-12-21 13:21:05'} -->
  8 +<TITLE>
  9 +Overview
  10 +</TITLE>
  11 +
  12 +<META NAME="keywords" CONTENT="Overview">
  13 +
  14 +<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
  15 +
  16 +<SCRIPT type="text/javascript">
  17 +function windowTitle()
  18 +{
  19 + parent.document.title="Overview";
  20 +}
  21 +</SCRIPT>
  22 +<NOSCRIPT>
  23 +</NOSCRIPT>
  24 +
  25 +</HEAD>
  26 +
  27 +<BODY BGCOLOR="white" onload="windowTitle();">
  28 +
  29 +
  30 +
  31 +
  32 +<!-- ========= START OF NAVBAR ======= -->
  33 +<A NAME="navbar_top"><!-- --></A>
  34 +<A HREF="#skip-navbar_top" title="Skip navigation links"></A>
  35 +<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
  36 +<TR>
  37 +<TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
  38 +<A NAME="navbar_top_firstrow"><!-- --></A>
  39 +<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
  40 + <TR ALIGN="center" VALIGN="top">
  41 +
  42 +
  43 +
  44 + <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Overview</B></FONT>&nbsp;</TD>
  45 +
  46 +
  47 +
  48 + <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <FONT CLASS="NavBarFont1">Package</FONT>&nbsp;</TD>
  49 +
  50 +
  51 +
  52 + <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <FONT CLASS="NavBarFont1">Class</FONT>&nbsp;</TD>
  53 +
  54 +
  55 +
  56 +
  57 + </TR>
  58 +</TABLE>
  59 +</TD>
  60 +<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
  61 +
  62 +Transfer version 1.1</EM>
  63 +
  64 +</TD>
  65 +</TR>
  66 +
  67 +<TR>
  68 +
  69 +<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
  70 +
  71 + <A HREF="index.html?overview-summary.html" target="_top"><B>FRAMES</B></A> &nbsp;
  72 +
  73 +</FONT></TD>
  74 +
  75 +</TR>
  76 +</TABLE>
  77 +<A NAME="skip-navbar_top"></A>
  78 +<!-- ========= END OF NAVBAR ========= -->
  79 +
  80 +<HR>
  81 +
  82 +<H2>
  83 +Transfer version 1.1
  84 +</H2>
  85 +
  86 +
  87 +<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
  88 +<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
  89 +<TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
  90 +<B>Packages</B></FONT></TH>
  91 +</TR>
  92 +
  93 +
  94 +
  95 +
  96 +
  97 +</TABLE>
  98 +
  99 +<P>
  100 +&nbsp;<HR>
  101 +
  102 +
  103 +
  104 +
  105 +<!-- ========= START OF NAVBAR ======= -->
  106 +<A NAME="navbar_top"><!-- --></A>
  107 +<A HREF="#skip-navbar_top" title="Skip navigation links"></A>
  108 +<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
  109 +<TR>
  110 +<TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
  111 +<A NAME="navbar_top_firstrow"><!-- --></A>
  112 +<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
  113 + <TR ALIGN="center" VALIGN="top">
  114 +
  115 +
  116 +
  117 + <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Overview</B></FONT>&nbsp;</TD>
  118 +
  119 +
  120 +
  121 + <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <FONT CLASS="NavBarFont1">Package</FONT>&nbsp;</TD>
  122 +
  123 +
  124 +
  125 + <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <FONT CLASS="NavBarFont1">Class</FONT>&nbsp;</TD>
  126 +
  127 +
  128 +
  129 +
  130 + </TR>
  131 +</TABLE>
  132 +</TD>
  133 +<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
  134 +
  135 +Transfer version 1.1</EM>
  136 +
  137 +</TD>
  138 +</TR>
  139 +
  140 +<TR>
  141 +
  142 +<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
  143 +
  144 + <A HREF="index.html?overview-summary.html" target="_top"><B>FRAMES</B></A> &nbsp;
  145 +
  146 +</FONT></TD>
  147 +
  148 +</TR>
  149 +</TABLE>
  150 +<A NAME="skip-navbar_top"></A>
  151 +<!-- ========= END OF NAVBAR ========= -->
  152 +
  153 +<HR>
  154 +
  155 +</BODY>
  156 +</HTML>
  157 +
  158 +
BIN  doc/docs/resources/inherit.gif
29 doc/docs/stylesheet.css
... ... @@ -0,0 +1,29 @@
  1 +/* Javadoc style sheet */
  2 +
  3 +/* Define colors, fonts and other style attributes here to override the defaults */
  4 +
  5 +/* Page background color */
  6 +body { background-color: #FFFFFF }
  7 +
  8 +/* Headings */
  9 +h1 { font-size: 145% }
  10 +
  11 +/* Table colors */
  12 +.TableHeadingColor { background: #CCCCFF } /* Dark mauve */
  13 +.TableSubHeadingColor { background: #EEEEFF } /* Light mauve */
  14 +.TableRowColor { background: #FFFFFF } /* White */
  15 +
  16 +/* Font used in left-hand frame lists */
  17 +.FrameTitleFont { font-size: 100%; font-family: Helvetica, Arial, sans-serif }
  18 +.FrameHeadingFont { font-size: 90%; font-family: Helvetica, Arial, sans-serif }
  19 +.FrameItemFont { font-size: 90%; font-family: Helvetica, Arial, sans-serif }
  20 +
  21 +/* Navigation bar fonts and colors */
  22 +.NavBarCell1 { background-color:#EEEEFF;} /* Light mauve */
  23 +.NavBarCell1Rev { background-color:#00008B;} /* Dark Blue */
  24 +.NavBarFont1 { font-family: Arial, Helvetica, sans-serif; color:#000000;}
  25 +.NavBarFont1Rev { font-family: Arial, Helvetica, sans-serif; color:#FFFFFF;}
  26 +
  27 +.NavBarCell2 { font-family: Arial, Helvetica, sans-serif; background-color:#FFFFFF;}
  28 +.NavBarCell3 { font-family: Arial, Helvetica, sans-serif; background-color:#FFFFFF;}
  29 +
25 test/CollectionEachIteratorTest.cfc
... ... @@ -0,0 +1,25 @@
  1 +<cfcomponent extends="BaseTest">
  2 +<!---
  3 + In a single file, implement multiple models
  4 + --->
  5 +<cfscript>
  6 +
  7 +config = {
  8 + server_name = 'localhost',
  9 + server_port = 27017,
  10 + db_name = 'dev_db',
  11 + collection_name = 'test'
  12 + };
  13 +
  14 +function testEachIterator(){
  15 + mongo = createObject('component','cfmongodb.components.MongoDB');
  16 + mongo.getDb('goodcoders');
  17 + doc = mongo.new_doc('person');
  18 + doc.set('name','bill');
  19 + doc.save();
  20 +
  21 +
  22 +}
  23 +
  24 +</cfscript>
  25 +</cfcomponent>
2  test/DatabaseTest.cfc
@@ -2,7 +2,7 @@
2 2 <cfscript>
3 3 function testThis(){
4 4 db = createObject('component','cfmongodb.components.Database');
5   - debug(db.getDB('default_db'));
  5 + debug(db.getDB('some_db').toString());
6 6 }
7 7
8 8 </cfscript>
19 test/DocumentFactoryModelTest.cfc
... ... @@ -0,0 +1,19 @@
  1 +<cfcomponent extends="BaseTest">
  2 +<cfscript>
  3 +
  4 +model = createObject('component','fixture.MyMongoModel');
  5 +
  6 +
  7 +function new_project(){
  8 + project = model.new_project( 'my project', 'owner=123' );
  9 + project.save();
  10 +}
  11 +
  12 +function new_person(){
  13 + person = model.new_person( 'bill', '123 main st' );
  14 + person.save();
  15 +}
  16 +
  17 +
  18 +</cfscript>
  19 +</cfcomponent>
19 test/DocumentTest.cfc
@@ -3,6 +3,25 @@
3 3
4 4 mongo = createObject('component','cfmongodb.Mongo').init();
5 5
  6 +
  7 +
  8 + function $setMongoInDocument(){
  9 + doc = mongo.new_doc(collection_name='tester');
  10 + doc.set('foo','bar');
  11 + doc.save();
  12 +
  13 + }
  14 +
  15 +
  16 + function $switchDb(){
  17 + mongo.getDb('foo');
  18 + doc = mongo.new_doc(collection_name='tester');
  19 + doc.set('foo','bar');
  20 + doc.save();
  21 +
  22 + }
  23 +
  24 +
6 25 function insert_embedded_doc(){
7 26 doc = mongo.new_doc(collection_name='test');
8 27 doc.set('parent','dad');
17 test/InheritedDocumentTest.cfc
... ... @@ -0,0 +1,17 @@
  1 +<cfcomponent extends="BaseTest">
  2 +<cfscript>
  3 +
  4 + mongo = createObject('component','cfmongodb.Mongo').init();
  5 +
  6 +
  7 +
  8 + function newDoc(){
  9 + doc = createObject('component','fixture.MyMongoDocument').init('my_mongo');
  10 + doc.set('title','my title');
  11 + doc.save();
  12 +
  13 + }
  14 +
  15 +
  16 +</cfscript>
  17 +</cfcomponent>
36 test/ModelTest.cfc
... ... @@ -0,0 +1,36 @@
  1 +<cfcomponent extends="BaseTest">
  2 +<!---
  3 + In a single file, implement multiple models
  4 + --->
  5 +<cfscript>
  6 +
  7 +
  8 +function testModel(){
  9 +mongo = createObject('component','cfmongodb.components.MongoDB').init('goodcoders');
  10 +mongo.getDb('goodcoders');
  11 +
  12 +debug(mongo);
  13 + developer = mongo.new_doc( 'developers' );
  14 + developer.set('name','joe');
  15 + skills = ['jquery','flex'];
  16 + developer.set('skills', skills );
  17 + developer.save();
  18 +
  19 + developer1 = mongo.new_doc( 'developers' );
  20 + developer1.set('name','bill');
  21 + skills1 = ['java','python','mongo'];
  22 + developer1.set('skills', skills1 );
  23 + developer1.save();
  24 +
  25 + proj_members = [ developer.get('_id'), developer1.get('_id') ];
  26 +
  27 +
  28 + project = mongo.new_doc( 'projects' );
  29 + project.set('title', 'tops4tots');
  30 + project.set('members',proj_members);
  31 + project.save();
  32 +
  33 +}
  34 +
  35 +</cfscript>
  36 +</cfcomponent>
18 test/NewDocumentTest.cfc
... ... @@ -0,0 +1,18 @@
  1 +<cfcomponent extends="mxunit.framework.TestCase">
  2 +<cfscript>
  3 +
  4 + mongo = createObject('component','cfmongodb.Mongo').init();
  5 +
  6 +
  7 +
  8 + function DocumentFactorySyntax(){
  9 + doc = mongo.new_doc(collection_name='test_this');
  10 + doc.set('foo','bar');
  11 + doc.save();
  12 + doc.delete();
  13 +
  14 + }
  15 +
  16 +
  17 +</cfscript>
  18 +</cfcomponent>
13 test/TestSuiteTest.cfc
... ... @@ -0,0 +1,13 @@
  1 +<cfcomponent extends="mxunit.framework.TestCase" output="false">
  2 +<cfscript>
  3 +
  4 +function testThis(){
  5 + suite = createObject('component','mxunit.framework.TestSuite').TestSuite();
  6 + suite.addAll('cfmongodb.test.DocumentTest');
  7 + results = suite.run();
  8 + //debug(results);
  9 +}
  10 +
  11 +</cfscript>
  12 +
  13 +</cfcomponent>
19 test/fixture/MyMongoDocument.cfc
... ... @@ -0,0 +1,19 @@
  1 +<cfcomponent persisten="true" extends="cfmongodb.components.MongoDocument" output="false">
  2 +<cfscript>
  3 +/* -------------------------------------------------
  4 + ** Syntax for defining a persistent model **
  5 +
  6 + this.model = structNew();
  7 + this.model['name'] = 'default value';
  8 +----------------------------------------------------*/
  9 +
  10 +
  11 + this.model = structNew();
  12 + this.model['title'] = 'default';
  13 + this.model['body'] = 'default';
  14 + this.model['timestamp'] = getTickCount();
  15 + this.model['tags'] = [];
  16 +
  17 +
  18 +</cfscript>
  19 +</cfcomponent>
27 test/fixture/MyMongoModel.cfc
... ... @@ -0,0 +1,27 @@
  1 +<cfcomponent output="false">
  2 +<cfscript>
  3 +/* -------------------------------------------------
  4 + Example of a condensed model. Instead of separate
  5 + files for each class, multiple "classes" can be
  6 + defined here using the mongo document factory.
  7 +----------------------------------------------------*/
  8 +config = {
  9 + server_name = 'localhost',
  10 + server_port = 27017,
  11 + db_name = 'dev',
  12 + collection_name = 'test_coll'
  13 + };
  14 +
  15 +mongo = createObject('component','cfmongodb.components.MongoDb').init(config);
  16 +
  17 +
  18 +function new_person(name,addr){
  19 + var person = mongo.new_doc('person',mongo);
  20 + person.set('name',name);
  21 + person.set('address',addr);
  22 + return person;
  23 +}
  24 +
  25 +
  26 +</cfscript>
  27 +</cfcomponent>

0 comments on commit 18ae296

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