Skip to content

Commit

Permalink
daos folder with dao module added
Browse files Browse the repository at this point in the history
  • Loading branch information
yesobo committed Dec 1, 2012
1 parent b0fcba7 commit 7fdb23a
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 79 deletions.
103 changes: 68 additions & 35 deletions daos/dao.coffee
Expand Up @@ -6,62 +6,95 @@ Use:
connector = new MongoDBConnector 'design_patterns', 'alex.mongohq.com', 100001 connector = new MongoDBConnector 'design_patterns', 'alex.mongohq.com', 100001
connector.findAll (err, items) -> res.send items connector.findAll (err, items) -> res.send items
### ###
class MongoDBConnector module.exports = class MongoDBConnector
# dbName = 'design_patterns' # dbName = 'design_patterns'
# port = 100001
# host = 'alex.mongohq.com' # host = 'alex.mongohq.com'
# port = 10001
constructor: (@dbName, @host, @port) -> constructor: (@dbName, @host, @port) ->
@db = new mongodb.Db(@dbName, new mongodb.Server(@host, @port, {auto_reconnect:true}), {}); @db = new mongodb.Db(@dbName, new mongodb.Server(@host, @port, {auto_reconnect:true}), {});
@db.open (err, p_client) ->
@db.authenticate 'admin', '1234', (err) ->

#call: callback parameters are (err, collection) #call: callback parameters are (err, collection)
initTransaction = (callback) -> initTransaction = (callback) ->
@db.collection @dbName, callback if @db._state == 'connected'

console.log "Conection opened"
@db.collection @dbName, callback
else
console.log "opening connection..."
privateDb = @db
privateDBName = @dbName
@db.open (err, p_client) ->
if err?
console.log "ERROR opening connection: #{err}"
else
console.log "connection opened"
console.log "authenticating..."
privateDb.authenticate 'admin', '1234', (err) ->
console.log "autenticated!"
privateDb.collection privateDBName, callback


#call: callback parameters are (err, items) #call: callback parameters are (err, items)
findAll: (callback) -> findAll: (callback) ->
initTransaction (err, collection) -> initTransaction.call this, (err, collection) ->
collection.find().toArray (err, items) -> if err?
callback err, items console.log "ERROR!"
callback err, null
else
collection.find().toArray (err, items) ->
callback err, items


#call: callback parameters are (err, count) #call: callback parameters are (err, count)
count: (callback) -> count: (callback) ->
initTransaction (err, collection) -> initTransaction.call this, (err, collection) ->
collection.count (err, count) -> if err?
callback err, count console.log "ERROR"
callback err, null
else
collection.count (err, count) ->
callback err, count


#call: callback parameters are (err, item) #call: callback parameters are (err, item)
findById: (pId, callback) -> findById: (pId, callback) ->
initTransaction (err, collection) -> initTransaction.call this, (err, collection) ->
collection.findOne id:pId, (err, item) -> if err?
callback err, item console.log "ERROR!"
callback err, null
else
collection.findOne id:pId, (err, item) ->
callback err, item


#call: callback parameters are (err, doc) #call: callback parameters are (err, doc)
insert: (pattern, callback) -> insert: (pattern, callback) ->
initTransaction (err, collection) -> initTransaction.call this, (err, collection) ->
collection.count (err, count) -> if err?
pattern.id = count + 1 console.log "ERROR!"
callback err, null
else
collection.insert pattern, (err, doc) -> collection.insert pattern, (err, doc) ->
callback err, doc callback err, doc


#call: callback parameters are (err, pattern) #call: callback parameters are (err)
update: (pattern, callback) -> update: (pattern, callback) ->
initTransaction (err, collection) -> initTransaction.call this, (err, collection) ->
collection.update id:pattern.id, if err?
$set: console.log "ERROR!"
name: pattern.name callback err, null
category: pattern.category else
intent: pattern.intent collection.update id:pattern.id,
motivation: pattern.motivation $set:
applicability: pattern.applicability name: pattern.name
structure: pattern.structure, category: pattern.category
(err) -> intent: pattern.intent
callback err, pattern motivation: pattern.motivation
applicability: pattern.applicability
structure: pattern.structure,
(err) ->
callback err


#call: callback parameters are (err, removed) #call: callback parameters are (err)
delete: (pId, callback) -> delete: (pId, callback) ->
initTransaction (err, collection) -> initTransaction.call this, (err, collection) ->
collection.remove id:pId, (err, removed) -> if err?
callback err, removed console.log "ERROR!"
else
collection.remove id:pId, (err) ->
callback err
131 changes: 87 additions & 44 deletions daos/dao.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions daos/dao_test.coffee
@@ -0,0 +1,59 @@
modules_url = '../node_modules'

MongoDBConnector = require "./dao.js"

mongodb = require 'mongodb'
should = require "#{modules_url}/should"

describe 'Tests for MongoDBConnector', ->
daoObj = null
new_pattern = {
"id": 3,
"name": "Factory Method",
"category": "Creational",
"intent": "Define an interface for creating an object, but let subclasses decide which class to instantiate. Lets a class defer instantiation to subclasses",
"motivation": "",
"applicability": "",
"structure": ""
};
beforeEach ->
daoObj = new MongoDBConnector 'design_patterns', 'alex.mongohq.com', 10001
it 'Can be instatiated with paraneters', (done) ->
daoObj.should.have.property 'dbName', 'design_patterns'
daoObj.should.have.property 'host', 'alex.mongohq.com'
daoObj.should.have.property 'port', 10001
daoObj.should.have.property 'db'
should.exist daoObj.db
daoObj.db.should.be.an.instanceof mongodb.Db
done()
it 'findAll returns my collection', (done) ->
daoObj.findAll (err, items) ->
items.should.be.an.instanceOf Array
items.should.have.length 2
done()
it "count returns my collection's number", (done) ->
daoObj.count (err, count) ->
should.strictEqual count, 2
done()
it "findById with id = 2 returns the Prototype pattern", (done) ->
daoObj.findById 2, (err, item) ->
item.should.have.property 'name', 'Prototype'
done()
it "insert new_pattern (Factory Method) returns the pattern", (done) ->
daoObj.insert new_pattern, (err, docs) ->
docs[0].should.have.property 'name', new_pattern.name
done()

it "update new_pattern returns the pattern updated", (done) ->
new_name = "Modified Name"
new_pattern.name = new_name;
daoObj.update new_pattern, (err) ->
daoObj.findById new_pattern.id, (err, item) ->
item.should.have.property 'name', new_name
done()

it "delete new_pattern returns the removed alement", (done) ->
daoObj.delete new_pattern.id, (err) ->
daoObj.count (err, count) ->
should.strictEqual count, 2
done()

0 comments on commit 7fdb23a

Please sign in to comment.