Permalink
Browse files

Refactored repository base, added account signup page

  • Loading branch information...
1 parent 9453a15 commit 195097d2cc769b563b2a3337ec81d055f5d5e30d @toantran committed Mar 27, 2012
View
4 app.js
@@ -71,6 +71,10 @@
}
};
+ app.get('/accountadd', routes.accountadd);
+
+ app.post('/accountadd', routes.createaccount);
+
app.get('/data', restrict, routes.data);
app.get('/customers', restrict, routes.customers);
View
@@ -55,6 +55,8 @@ restrict = (req, res, next) ->
# Routes
+app.get '/accountadd', routes.accountadd
+app.post '/accountadd', routes.createaccount
app.get '/data', restrict, routes.data
app.get '/customers', restrict, routes.customers
app.get '/login', routes.login
@@ -1,204 +1,169 @@
GLOBAL.DEBUG = true
+root = global ? window
+
mongo = require 'mongodb'
{Db, ObjectID: ObjectId, Timestamp, Connection, Server} = mongo
dbName = 'whereartthoudb'
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()
-
-
-m_db = null
-m_refcount = 0
-
-
-exports.closeDb = closeDb = ->
- m_refcount--
- if m_refcount <= 0 and m_db?
- m_db.close()
-
-
-###
-Return the default DB
-###
-exports.getDb = getDb = ->
- m_refcount++
- if not m_db?
- m_db = new Db dbName, new Server(host, port, {}), native_parser:false
- close = m_db.close
- m_db.close = ->
- m_db.close = close
- closeDb()
- m_db
-
-
+ 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 = ->) ->
+ console.log 'db = ', root?.db
+ 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('customers')
+baseRepo = new baseDb.Repository('customers')
-{Db, ObjectId, Timestamp, Connection, Server, checkError, errorHandler, getDb} = baseDb
+{Db, ObjectId, Timestamp, Connection, Server} = baseDb
exports.create = () ->
baseRepo.create.apply baseRepo, arguments
@@ -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
@@ -30,7 +25,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 +33,6 @@ exports.getByUsername = (username, callback = ->) ->
else
callback null, null
cursor.close()
- db.close()
else
callback 'DB read failed'
Oops, something went wrong.

0 comments on commit 195097d

Please sign in to comment.