Permalink
Browse files

refactored base repository

  • Loading branch information...
1 parent 5af5815 commit 24a2a3c6b828b0599e1ca2a98066fb1436a7e149 @toantran committed Mar 28, 2012
@@ -1,184 +1,168 @@
GLOBAL.DEBUG = true
+root = global ? window
+
mongo = require 'mongodb'
{Db, ObjectID: ObjectId, Timestamp, Connection, Server} = mongo
dbName = 'wifefoosdb'
host = process.env['MONGO_NODE_DRIVER_HOST'] ? 'localhost'
port = process.env['MONGO_NODE_DRIVER_PORT'] ? Connection.DEFAULT_PORT
-
-exports.setCollectionName = (@collectionName) ->
+db_connector = new Db dbName, new Server(host, port, {}), native_parser:false
exports.Db = Db
exports.ObjectId = ObjectId
exports.Timestamp = Timestamp
exports.Connection = Connection
exports.Server = Server
+utils = require '../services/utils'
+###
+exports.Db = Db
+exports.ObjectId = ObjectId
+exports.Timestamp = Timestamp
+exports.Connection = Connection
+exports.Server = Server
+###
###
Default error handler
###
-exports.checkError = checkError = (error, errorFn, next = ->) ->
+checkError = (error, errorFn, next = ->) ->
if error?
console.log error
errorFn()
- else if next? and typeof next is 'function'
- next.call()
-
-
-###
-Return the default DB
-###
-exports.getDb = getDb = ->
- new Db dbName, new Server(host, port, {}), native_parser:false
+ else
+ next()
+
###
Return a default error handler
###
-exports.errorHandler = errorHandler = (db, callback = ->) ->
+errorHandler = errorHandler = (db, callback = ->) ->
(error) ->
- db.close
+ try
+ db.close() if db?
+ catch e
+ console.trace e
callback error
-repository = (@collectionName) ->
-repository::setCollectionName = (@collectionName) ->
+Repository = (@collectionName) ->
+
+
+Repository::setCollectionName = (@collectionName) ->
+
+
+Repository::getDb = (callback = ->) ->
+ if root?.db?.state isnt 'connected'
+ db_connector.open (err, db) =>
+ root.db = db
+ callback err, db
+ else
+ callback null, root.db
+
###
Return the default collection
###
-repository::getCollection = (db, callback = ->) ->
- errorFn = (error) ->
- callback error
-
+Repository::getCollection = (callback = ->) ->
try
- db.open (openErr, openDb) =>
- checkError openErr, errorFn, =>
- try
- openDb.collection @collectionName, (collectionErr, collection) ->
- if collectionErr?
- callback collectionErr
- db.close()
- else
- callback null, collection
- catch e2
- console.trace e2
- callback e2
- catch e
- console.trace e
- callback e
+ utils.execute( utils.bind(@getDb, @) )
+ .then (err, db, cb = ->) =>
+ return callback(err) if err
+ db?.collection @collectionName, callback
+ catch e2
+ console.trace e2
+ callback e2
-repository::create = (doc, callback = ->) ->
+Repository::create = (doc, callback = ->) ->
console?.assert doc?, 'Doc cannot be null'
throw 'doc null' unless doc?
- db = getDb()
- errorFn = errorHandler db, callback
doc.createdat = new Date()
- @getCollection db, (collectionErr, collection) ->
- checkError collectionErr, errorFn, ->
- try
- collection.insert doc, {safe: true}, () ->
- callback.apply this, arguments
- db.close()
- catch e
- db.close()
- console.trace e
- callback e
-
-
-repository::read = (findArgs..., callback = ->) ->
- db = getDb()
- errorFn = errorHandler db, callback
+ utils.execute( utils.bind(@getCollection, @) )
+ .then (err, collection, cb = ->) =>
+ return callback err if err
+ try
+ collection?.insert doc, {safe: true}, callback
+ catch e
+ console.trace e
+ callback e
+
+
+Repository::read = (findArgs..., callback = ->) ->
- @getCollection db, (collectionErr, collection) ->
- checkError collectionErr, errorFn, ->
- try
- cursor = collection.find.apply collection, findArgs
- callback null, cursor
-# cursor.toArray (toArrayErr, docs) ->
-# db.close()
-# console.log toArrayErr if toArrayErr
-# callback.apply this, arguments
- catch e
- console.trace e
- callback e
- db.close()
+ utils.execute( utils.bind(@getCollection, @) )
+ .then (err, collection, cb = ->) =>
+ return callback(err) if err
+ try
+ cursor = collection?.find.apply collection, findArgs
+ callback null, cursor
+ catch e
+ console.trace e
+ callback e
-repository::getById = (docid, callback = ->) ->
+Repository::getById = (docid, callback = ->) ->
console?.assert docid? and docid isnt 'undefined', 'docid must be defined'
throw 'Invalid id' unless docid? and docid isnt 'undefined'
docid = new ObjectId(docid) if typeof docid is 'string'
- @read _id: docid, (readErr, cursor) ->
+ @read _id: docid, (readErr, cursor) =>
return callback( readErr ) if readErr
- cursor.toArray (toArrayErr, docs) ->
+ cursor?.toArray (toArrayErr, docs) =>
return callback(toArrayErr) if toArrayErr
return callback('Not found') unless docs?.length
callback null, docs[0]
- db = cursor.db
cursor.close()
- db.close()
-repository::update = (criteria, objNew, options = {}, callback ) ->
- db = getDb()
- errorFn = errorHandler db, callback
+Repository::update = (criteria, objNew, options = {}, callback ) ->
options.safe ?= if callback? then true else false
options.multi ?= true
options.upsert ?= true
options['new'] ?= true
- @getCollection db, (collectionErr, collection) ->
- checkError collectionErr, errorFn, ->
- try
- collection.update criteria, objNew, options, () ->
- db.close()
- callback.apply this, arguments
- catch e
- db.close()
- console.trace e
- callback e if callback?
+ utils.execute( utils.bind(@getCollection, @) )
+ .then (err, collection, cb = ->) =>
+ return callback(err) if err
+ try
+ collection?.update criteria, objNew, options, callback
+ catch e
+ console.trace e
+ callback e if callback?
-repository::save = (doc, callback = ->) ->
+Repository::save = (doc, callback = ->) ->
+ console?.assert doc?, 'Doc must not be null'
throw 'Empty doc' unless doc?
- db = getDb()
- errorFn = errorHandler db, callback
- @getCollection db, (collectionErr, collection) ->
- checkError collectionErr, errorFn, ->
- try
- collection.save doc, {safe: true}, () ->
- db.close()
- callback.apply this, arguments
- catch e
- db.close()
- console.trace e
- callback e if callback?
+ utils.execute( utils.bind(@getCollection, @) )
+ .then (err, collection, cb = ->) =>
+ return callback(err) if err
+ try
+ collection?.save doc, {safe: true}, callback
+ catch e
+ console.trace e
+ callback e
-repository::remove = (criteria, callback = ->) ->
+Repository::remove = (criteria, callback = ->) ->
throw 'Criteria is empty' if Object.keys(criteria).length is 0
-
- db = getDb()
- errorFn = errorHandler db, callback
-
- @getCollection db, (collectionErr, collection) ->
- checkError collectionErr, errorFn, ->
- collection.remove criteria
- callback()
- db.close()
+
+ utils.execute( utils.bind(@getCollection, @) )
+ .then (err, collection, cb = ->) =>
+ collection?.remove criteria
+ callback()
-exports.repository = repository
+exports.Repository = Repository
@@ -1,7 +1,7 @@
baseDb = require('./base')
-baseRepo = new baseDb.repository('matches')
+baseRepo = new baseDb.Repository('matches')
-{Db, ObjectId, Timestamp, Connection, Server, checkError, errorHandler, getDb} = baseDb
+{Db, ObjectId, Timestamp, Connection, Server} = baseDb
exports.create = () ->
baseRepo.create.apply baseRepo, arguments
@@ -16,3 +16,5 @@ exports.remove = () ->
exports.getById = () ->
baseRepo.getById.apply baseRepo, arguments
exports.ObjectId = ObjectId
+exports.closeDb = () ->
+ baseRepo.closeDb.apply baseRepo, arguments
@@ -1,7 +1,7 @@
baseDb = require('./base')
-baseRepo = new baseDb.repository('news')
+baseRepo = new baseDb.Repository('news')
-{Db, ObjectId, Timestamp, Connection, Server, checkError, errorHandler, getDb} = baseDb
+{Db, ObjectId, Timestamp, Connection, Server} = baseDb
exports.create = () ->
baseRepo.create.apply baseRepo, arguments
@@ -16,3 +16,5 @@ exports.remove = () ->
exports.getById = () ->
baseRepo.getById.apply baseRepo, arguments
exports.ObjectId = ObjectId
+exports.closeDb = () ->
+ baseRepo.closeDb.apply baseRepo, arguments
@@ -1,7 +1,7 @@
baseDb = require('./base')
-baseRepo = new baseDb.repository('teams')
+baseRepo = new baseDb.Repository('teams')
-{Db, ObjectId, Timestamp, Connection, Server, checkError, errorHandler, getDb} = baseDb
+{Db, ObjectId, Timestamp, Connection, Server} = baseDb
exports.create = () ->
baseRepo.create.apply baseRepo, arguments
@@ -16,6 +16,8 @@ exports.remove = () ->
exports.getById = () ->
baseRepo.getById.apply baseRepo, arguments
exports.ObjectId = ObjectId
+exports.closeDb = () ->
+ baseRepo.closeDb.apply baseRepo, arguments
exports.removeChallenge = ( teamid, otherteamid, callback = ->) ->
@@ -1,12 +1,7 @@
-#baseDb = require './base'
-#baseDb.setCollectionName 'users'
-#{getDb, checkError, getCollection, errorHandler, create, read, update, remove, getById, ObjectId} = baseDb
-#[exports.create, exports.read, exports.update, exports.remove, exports.getById, exports.ObjectId] = [create, read, update, remove, getById, ObjectId]
-
baseDb = require('./base')
-baseRepo = new baseDb.repository('users')
+baseRepo = new baseDb.Repository('users')
-{Db, ObjectId, Timestamp, Connection, Server, checkError, errorHandler, getDb} = baseDb
+{Db, ObjectId, Timestamp, Connection, Server} = baseDb
exports.create = () ->
baseRepo.create.apply baseRepo, arguments
@@ -21,6 +16,8 @@ exports.remove = () ->
exports.getById = () ->
baseRepo.getById.apply baseRepo, arguments
exports.ObjectId = ObjectId
+exports.closeDb = () ->
+ baseRepo.closeDb.apply baseRepo, arguments
exports.getByUsername = (username, callback = ->) ->
@@ -30,7 +27,6 @@ exports.getByUsername = (username, callback = ->) ->
if err?
callback err
else if cursor?
- db = cursor.db
cursor.toArray (toArrayErr, users) ->
if toArrayErr?
callback toArrayErr
@@ -39,7 +35,6 @@ exports.getByUsername = (username, callback = ->) ->
else
callback null, null
cursor.close()
- db.close()
else
callback 'DB read failed'
@@ -27,10 +27,8 @@ exports.getCompleteMatches = (callback = ->) ->
callback readErr
else if cursor?
cursor.toArray ->
- db = cursor.db
callback.apply null, arguments
cursor.close()
- db.close()
else
callback()
@@ -51,10 +49,8 @@ exports.getPendingExpiredMatches = (callback) ->
callback readErr
else if cursor?
cursor.toArray ->
- db = cursor.db
callback.apply null, arguments
cursor.close()
- db.close()
else
callback()
Oops, something went wrong.

0 comments on commit 24a2a3c

Please sign in to comment.