Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactored base repository

  • Loading branch information...
commit 24a2a3c6b828b0599e1ca2a98066fb1436a7e149 1 parent 5af5815
@toantran authored
View
200 coffee/repository/base.coffee
@@ -1,13 +1,14 @@
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
@@ -15,170 +16,153 @@ 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
View
6 coffee/repository/matches2.coffee
@@ -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
View
6 coffee/repository/news.coffee
@@ -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
View
6 coffee/repository/teams2.coffee
@@ -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 = ->) ->
View
13 coffee/repository/users2.coffee
@@ -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'
View
4 coffee/services/match.coffee
@@ -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()
View
4 coffee/services/news.coffee
@@ -5,20 +5,16 @@ exports.getNews = (callback = ->) ->
return callback(err) if err? or not cursor?
cursor.sort createdat: -1
cursor.toArray ->
- db = cursor.db
callback.apply null, arguments
cursor.close()
- db.close()
exports.getHighlights = (callback = ->) ->
newsrepo.read {highlight: 1}, (err, cursor) ->
return callback(err) if err? or not cursor?
cursor.sort createdat: -1
cursor.toArray ->
- db = cursor.db
callback.apply null, arguments
cursor.close()
- db.close()
View
3  coffee/services/post.coffee
@@ -39,6 +39,7 @@ exports.loadComment = loadComment = (comment, callback = ->) ->
exports.makePostGen = (user) ->
(post, callback = ->) ->
+
return callback(null, post) unless post?
setPictureUrl = (pictureurl) ->
@@ -48,6 +49,8 @@ exports.makePostGen = (user) ->
post.desc = desc
returnback = (err) ->
+
+ console.trace err if err
if err
callback err, post
else
View
5 coffee/services/team.coffee
@@ -109,10 +109,8 @@ exports.getChallenge = getChallenge = (teamid, opponentid, callback = ->) ->
, {}, (err, cursor) ->
return callback(err) if err
cursor.toArray ->
- db = cursor.db
callback.apply null, arguments
cursor.close()
- db.close()
catch e
console.trace e
@@ -633,11 +631,8 @@ exports.getAll = (availableOnly, callback = ->) ->
callback readErr
else if cursor?
cursor.toArray ->
- db = cursor.db
callback.apply null, arguments
cursor.close()
- db.close()
-
else
callback()
View
14 coffee/services/user.coffee
@@ -573,11 +573,8 @@ exports.getAllPlayers = (callback = ->) ->
callback readErr
else if cursor?
cursor.toArray ->
- db = cursor.db
callback.apply null, arguments
cursor.close()
- db.close()
-
else
callback()
@@ -625,7 +622,6 @@ exports.getUserByToken = (token, callback = ->) ->
db = cursor.db
callback.apply null, arguments
cursor.close()
- db.close()
.then (err, users, cb = ->) ->
if err
@@ -712,6 +708,7 @@ exports.getFullUser = (userid, callback = ->) ->
utils.execute( newUserRepo.getById, userid ) # load user
.then (err, @user, cb = ->) =>
+ console.log 'load team'
# load team
return callback( err ) if err
@@ -724,11 +721,13 @@ exports.getFullUser = (userid, callback = ->) ->
else
cb()
.then (err, @team, cb = ->) =>
+ console.log 'load posts'
# Load posts
return callback( err ) if err
@user?.team = @team
if @user?.posts? and @user?.posts?.length
+ console.log 'load posts begin'
try
postGen = require './post'
postGen.init()
@@ -737,9 +736,11 @@ exports.getFullUser = (userid, callback = ->) ->
console.trace e
cb e
else
+ console.log 'load no posts'
cb null, null
- .then (err, fullposts, cb = ->) =>
+ .then (err, fullposts, cb = ->) =>
+ console.log 'load invites'
if fullposts?
posts = (post for post in fullposts when post?.desc?)
else
@@ -760,6 +761,7 @@ exports.getFullUser = (userid, callback = ->) ->
console.trace e
cb e
.then (err, invites, cb = ->) =>
+ console.log 'load challenges'
return callback( err ) if err
@user?.invites = invites
@@ -776,6 +778,7 @@ exports.getFullUser = (userid, callback = ->) ->
else
cb null, null
.then ( err, challenges, cb = ->) =>
+ console.log 'load matches'
@team?.challenges = challenges
@user?.challenges = challenges
allmatches = @team?.matches
@@ -819,6 +822,7 @@ exports.getFullUser = (userid, callback = ->) ->
else
callback null, @user
.then ( err, matches, cb = ->) =>
+ console.log 'load end'
@user.matches = matches
callback null, @user
View
13 coffee/services/utils.coffee
@@ -89,7 +89,20 @@ exports.execute = (task, args...) ->
return wrapper;
+exports.bind = (fn, scope, args, appendArgs) ->
+ if arguments.length is 2
+ return ->
+ fn.apply scope, arguments
+
+ method = fn
+ ->
+ callArgs = args ? arguments
+ if appendArgs is true
+ callArgs = arguments[0..]
+ callArgs = callArgs.concat args
+
+ method.apply (scope ? global) ? window, callArgs
View
251 repository/base.js
@@ -1,9 +1,11 @@
(function() {
- var Connection, Db, ObjectId, Server, Timestamp, checkError, dbName, errorHandler, getDb, host, mongo, port, repository, _ref, _ref2,
+ var Connection, Db, ObjectId, Repository, Server, Timestamp, checkError, dbName, db_connector, errorHandler, host, mongo, port, root, utils, _ref, _ref2,
__slice = Array.prototype.slice;
GLOBAL.DEBUG = true;
+ root = typeof global !== "undefined" && global !== null ? global : window;
+
mongo = require('mongodb');
Db = mongo.Db, ObjectId = mongo.ObjectID, Timestamp = mongo.Timestamp, Connection = mongo.Connection, Server = mongo.Server;
@@ -14,9 +16,9 @@
port = (_ref2 = process.env['MONGO_NODE_DRIVER_PORT']) != null ? _ref2 : Connection.DEFAULT_PORT;
- exports.setCollectionName = function(collectionName) {
- this.collectionName = collectionName;
- };
+ db_connector = new Db(dbName, new Server(host, port, {}), {
+ native_parser: false
+ });
exports.Db = Db;
@@ -28,135 +30,130 @@
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 = function(error, errorFn, next) {
+ checkError = function(error, errorFn, next) {
if (next == null) next = function() {};
if (error != null) {
console.log(error);
return errorFn();
- } else if ((next != null) && typeof next === 'function') {
- return next.call();
+ } else {
+ return next();
}
};
/*
- Return the default DB
- */
-
- exports.getDb = getDb = function() {
- return new Db(dbName, new Server(host, port, {}), {
- native_parser: false
- });
- };
-
- /*
Return a default error handler
*/
- exports.errorHandler = errorHandler = function(db, callback) {
+ errorHandler = errorHandler = function(db, callback) {
if (callback == null) callback = function() {};
return function(error) {
- db.close;
+ try {
+ if (db != null) db.close();
+ } catch (e) {
+ console.trace(e);
+ }
return callback(error);
};
};
- repository = function(collectionName) {
+ Repository = function(collectionName) {
this.collectionName = collectionName;
};
- repository.prototype.setCollectionName = function(collectionName) {
+ Repository.prototype.setCollectionName = function(collectionName) {
this.collectionName = collectionName;
};
+ Repository.prototype.getDb = function(callback) {
+ var _ref3,
+ _this = this;
+ if (callback == null) callback = function() {};
+ if ((root != null ? (_ref3 = root.db) != null ? _ref3.state : void 0 : void 0) !== 'connected') {
+ return db_connector.open(function(err, db) {
+ root.db = db;
+ return callback(err, db);
+ });
+ } else {
+ return callback(null, root.db);
+ }
+ };
+
/*
Return the default collection
*/
- repository.prototype.getCollection = function(db, callback) {
- var errorFn,
- _this = this;
+ Repository.prototype.getCollection = function(callback) {
+ var _this = this;
if (callback == null) callback = function() {};
- errorFn = function(error) {
- return callback(error);
- };
try {
- return db.open(function(openErr, openDb) {
- return checkError(openErr, errorFn, function() {
- try {
- return openDb.collection(_this.collectionName, function(collectionErr, collection) {
- if (collectionErr != null) {
- callback(collectionErr);
- return db.close();
- } else {
- return callback(null, collection);
- }
- });
- } catch (e2) {
- console.trace(e2);
- return callback(e2);
- }
- });
+ return utils.execute(utils.bind(this.getDb, this)).then(function(err, db, cb) {
+ if (cb == null) cb = function() {};
+ if (err) return callback(err);
+ return db != null ? db.collection(_this.collectionName, callback) : void 0;
});
- } catch (e) {
- console.trace(e);
- return callback(e);
+ } catch (e2) {
+ console.trace(e2);
+ return callback(e2);
}
};
- repository.prototype.create = function(doc, callback) {
- var db, errorFn;
+ Repository.prototype.create = function(doc, callback) {
+ var _this = this;
if (callback == null) callback = function() {};
if (typeof console !== "undefined" && console !== null) {
console.assert(doc != null, 'Doc cannot be null');
}
if (doc == null) throw 'doc null';
- db = getDb();
- errorFn = errorHandler(db, callback);
doc.createdat = new Date();
- return this.getCollection(db, function(collectionErr, collection) {
- return checkError(collectionErr, errorFn, function() {
- try {
- return collection.insert(doc, {
- safe: true
- }, function() {
- callback.apply(this, arguments);
- return db.close();
- });
- } catch (e) {
- db.close();
- console.trace(e);
- return callback(e);
- }
- });
+ return utils.execute(utils.bind(this.getCollection, this)).then(function(err, collection, cb) {
+ if (cb == null) cb = function() {};
+ if (err) return callback(err);
+ try {
+ return collection != null ? collection.insert(doc, {
+ safe: true
+ }, callback) : void 0;
+ } catch (e) {
+ console.trace(e);
+ return callback(e);
+ }
});
};
- repository.prototype.read = function() {
- var callback, db, errorFn, findArgs, _i;
+ Repository.prototype.read = function() {
+ var callback, findArgs, _i,
+ _this = this;
findArgs = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), callback = arguments[_i++];
if (callback == null) callback = function() {};
- db = getDb();
- errorFn = errorHandler(db, callback);
- return this.getCollection(db, function(collectionErr, collection) {
- return checkError(collectionErr, errorFn, function() {
- var cursor;
- try {
- cursor = collection.find.apply(collection, findArgs);
- return callback(null, cursor);
- } catch (e) {
- console.trace(e);
- callback(e);
- return db.close();
- }
- });
+ return utils.execute(utils.bind(this.getCollection, this)).then(function(err, collection, cb) {
+ var cursor;
+ if (cb == null) cb = function() {};
+ if (err) return callback(err);
+ try {
+ cursor = collection != null ? collection.find.apply(collection, findArgs) : void 0;
+ return callback(null, cursor);
+ } catch (e) {
+ console.trace(e);
+ return callback(e);
+ }
});
};
- repository.prototype.getById = function(docid, callback) {
+ Repository.prototype.getById = function(docid, callback) {
+ var _this = this;
if (callback == null) callback = function() {};
if (typeof console !== "undefined" && console !== null) {
console.assert((docid != null) && docid !== 'undefined', 'docid must be defined');
@@ -167,82 +164,66 @@
_id: docid
}, function(readErr, cursor) {
if (readErr) return callback(readErr);
- return cursor.toArray(function(toArrayErr, docs) {
- var db;
+ return cursor != null ? cursor.toArray(function(toArrayErr, docs) {
if (toArrayErr) return callback(toArrayErr);
if (!(docs != null ? docs.length : void 0)) return callback('Not found');
callback(null, docs[0]);
- db = cursor.db;
- cursor.close();
- return db.close();
- });
+ return cursor.close();
+ }) : void 0;
});
};
- repository.prototype.update = function(criteria, objNew, options, callback) {
- var db, errorFn;
+ Repository.prototype.update = function(criteria, objNew, options, callback) {
+ var _this = this;
if (options == null) options = {};
- db = getDb();
- errorFn = errorHandler(db, callback);
if (options.safe == null) options.safe = callback != null ? true : false;
if (options.multi == null) options.multi = true;
if (options.upsert == null) options.upsert = true;
if (options['new'] == null) options['new'] = true;
- return this.getCollection(db, function(collectionErr, collection) {
- return checkError(collectionErr, errorFn, function() {
- try {
- return collection.update(criteria, objNew, options, function() {
- db.close();
- return callback.apply(this, arguments);
- });
- } catch (e) {
- db.close();
- console.trace(e);
- if (callback != null) return callback(e);
- }
- });
+ return utils.execute(utils.bind(this.getCollection, this)).then(function(err, collection, cb) {
+ if (cb == null) cb = function() {};
+ if (err) return callback(err);
+ try {
+ return collection != null ? collection.update(criteria, objNew, options, callback) : void 0;
+ } catch (e) {
+ console.trace(e);
+ if (callback != null) return callback(e);
+ }
});
};
- repository.prototype.save = function(doc, callback) {
- var db, errorFn;
+ Repository.prototype.save = function(doc, callback) {
+ var _this = this;
if (callback == null) callback = function() {};
+ if (typeof console !== "undefined" && console !== null) {
+ console.assert(doc != null, 'Doc must not be null');
+ }
if (doc == null) throw 'Empty doc';
- db = getDb();
- errorFn = errorHandler(db, callback);
- return this.getCollection(db, function(collectionErr, collection) {
- return checkError(collectionErr, errorFn, function() {
- try {
- return collection.save(doc, {
- safe: true
- }, function() {
- db.close();
- return callback.apply(this, arguments);
- });
- } catch (e) {
- db.close();
- console.trace(e);
- if (callback != null) return callback(e);
- }
- });
+ return utils.execute(utils.bind(this.getCollection, this)).then(function(err, collection, cb) {
+ if (cb == null) cb = function() {};
+ if (err) return callback(err);
+ try {
+ return collection != null ? collection.save(doc, {
+ safe: true
+ }, callback) : void 0;
+ } catch (e) {
+ console.trace(e);
+ return callback(e);
+ }
});
};
- repository.prototype.remove = function(criteria, callback) {
- var db, errorFn;
+ Repository.prototype.remove = function(criteria, callback) {
+ var _this = this;
if (callback == null) callback = function() {};
if (Object.keys(criteria).length === 0) throw 'Criteria is empty';
- db = getDb();
- errorFn = errorHandler(db, callback);
- return this.getCollection(db, function(collectionErr, collection) {
- return checkError(collectionErr, errorFn, function() {
- collection.remove(criteria);
- callback();
- return db.close();
- });
+ return utils.execute(utils.bind(this.getCollection, this)).then(function(err, collection, cb) {
+ if (cb == null) cb = function() {};
+ if (collection != null) collection.remove(criteria);
+ return callback();
});
};
- exports.repository = repository;
+ exports.Repository = Repository;
}).call(this);
View
10 repository/matches2.js
@@ -1,11 +1,11 @@
(function() {
- var Connection, Db, ObjectId, Server, Timestamp, baseDb, baseRepo, checkError, errorHandler, getDb;
+ var Connection, Db, ObjectId, Server, Timestamp, baseDb, baseRepo;
baseDb = require('./base');
- baseRepo = new baseDb.repository('matches');
+ baseRepo = new baseDb.Repository('matches');
- Db = baseDb.Db, ObjectId = baseDb.ObjectId, Timestamp = baseDb.Timestamp, Connection = baseDb.Connection, Server = baseDb.Server, checkError = baseDb.checkError, errorHandler = baseDb.errorHandler, getDb = baseDb.getDb;
+ Db = baseDb.Db, ObjectId = baseDb.ObjectId, Timestamp = baseDb.Timestamp, Connection = baseDb.Connection, Server = baseDb.Server;
exports.create = function() {
return baseRepo.create.apply(baseRepo, arguments);
@@ -33,4 +33,8 @@
exports.ObjectId = ObjectId;
+ exports.closeDb = function() {
+ return baseRepo.closeDb.apply(baseRepo, arguments);
+ };
+
}).call(this);
View
10 repository/news.js
@@ -1,11 +1,11 @@
(function() {
- var Connection, Db, ObjectId, Server, Timestamp, baseDb, baseRepo, checkError, errorHandler, getDb;
+ var Connection, Db, ObjectId, Server, Timestamp, baseDb, baseRepo;
baseDb = require('./base');
- baseRepo = new baseDb.repository('news');
+ baseRepo = new baseDb.Repository('news');
- Db = baseDb.Db, ObjectId = baseDb.ObjectId, Timestamp = baseDb.Timestamp, Connection = baseDb.Connection, Server = baseDb.Server, checkError = baseDb.checkError, errorHandler = baseDb.errorHandler, getDb = baseDb.getDb;
+ Db = baseDb.Db, ObjectId = baseDb.ObjectId, Timestamp = baseDb.Timestamp, Connection = baseDb.Connection, Server = baseDb.Server;
exports.create = function() {
return baseRepo.create.apply(baseRepo, arguments);
@@ -33,4 +33,8 @@
exports.ObjectId = ObjectId;
+ exports.closeDb = function() {
+ return baseRepo.closeDb.apply(baseRepo, arguments);
+ };
+
}).call(this);
View
10 repository/teams2.js
@@ -1,11 +1,11 @@
(function() {
- var Connection, Db, ObjectId, Server, Timestamp, baseDb, baseRepo, checkError, errorHandler, getDb;
+ var Connection, Db, ObjectId, Server, Timestamp, baseDb, baseRepo;
baseDb = require('./base');
- baseRepo = new baseDb.repository('teams');
+ baseRepo = new baseDb.Repository('teams');
- Db = baseDb.Db, ObjectId = baseDb.ObjectId, Timestamp = baseDb.Timestamp, Connection = baseDb.Connection, Server = baseDb.Server, checkError = baseDb.checkError, errorHandler = baseDb.errorHandler, getDb = baseDb.getDb;
+ Db = baseDb.Db, ObjectId = baseDb.ObjectId, Timestamp = baseDb.Timestamp, Connection = baseDb.Connection, Server = baseDb.Server;
exports.create = function() {
return baseRepo.create.apply(baseRepo, arguments);
@@ -33,6 +33,10 @@
exports.ObjectId = ObjectId;
+ exports.closeDb = function() {
+ return baseRepo.closeDb.apply(baseRepo, arguments);
+ };
+
exports.removeChallenge = function(teamid, otherteamid, callback) {
var findObj, removingLog, updateObj;
if (callback == null) callback = function() {};
View
15 repository/users2.js
@@ -1,11 +1,11 @@
(function() {
- var Connection, Db, ObjectId, Server, Timestamp, baseDb, baseRepo, checkError, errorHandler, getDb;
+ var Connection, Db, ObjectId, Server, Timestamp, baseDb, baseRepo;
baseDb = require('./base');
- baseRepo = new baseDb.repository('users');
+ baseRepo = new baseDb.Repository('users');
- Db = baseDb.Db, ObjectId = baseDb.ObjectId, Timestamp = baseDb.Timestamp, Connection = baseDb.Connection, Server = baseDb.Server, checkError = baseDb.checkError, errorHandler = baseDb.errorHandler, getDb = baseDb.getDb;
+ Db = baseDb.Db, ObjectId = baseDb.ObjectId, Timestamp = baseDb.Timestamp, Connection = baseDb.Connection, Server = baseDb.Server;
exports.create = function() {
return baseRepo.create.apply(baseRepo, arguments);
@@ -33,17 +33,19 @@
exports.ObjectId = ObjectId;
+ exports.closeDb = function() {
+ return baseRepo.closeDb.apply(baseRepo, arguments);
+ };
+
exports.getByUsername = function(username, callback) {
if (callback == null) callback = function() {};
if (!username) return callback('Invalid username');
return baseRepo.read({
username: username
}, function(err, cursor) {
- var db;
if (err != null) {
return callback(err);
} else if (cursor != null) {
- db = cursor.db;
return cursor.toArray(function(toArrayErr, users) {
if (toArrayErr != null) {
callback(toArrayErr);
@@ -52,8 +54,7 @@
} else {
callback(null, null);
}
- cursor.close();
- return db.close();
+ return cursor.close();
});
} else {
return callback('DB read failed');
View
10 services/match.js
@@ -34,11 +34,8 @@
return callback(readErr);
} else if (cursor != null) {
return cursor.toArray(function() {
- var db;
- db = cursor.db;
callback.apply(null, arguments);
- cursor.close();
- return db.close();
+ return cursor.close();
});
} else {
return callback();
@@ -64,11 +61,8 @@
return callback(readErr);
} else if (cursor != null) {
return cursor.toArray(function() {
- var db;
- db = cursor.db;
callback.apply(null, arguments);
- cursor.close();
- return db.close();
+ return cursor.close();
});
} else {
return callback();
View
10 services/news.js
@@ -12,11 +12,8 @@
createdat: -1
});
return cursor.toArray(function() {
- var db;
- db = cursor.db;
callback.apply(null, arguments);
- cursor.close();
- return db.close();
+ return cursor.close();
});
});
};
@@ -31,11 +28,8 @@
createdat: -1
});
return cursor.toArray(function() {
- var db;
- db = cursor.db;
callback.apply(null, arguments);
- cursor.close();
- return db.close();
+ return cursor.close();
});
});
};
View
1  services/post.js
@@ -70,6 +70,7 @@
};
returnback = function(err) {
var _ref;
+ if (err) console.trace(err);
if (err) {
return callback(err, post);
} else {
View
10 services/team.js
@@ -147,11 +147,8 @@
}, {}, function(err, cursor) {
if (err) return callback(err);
return cursor.toArray(function() {
- var db;
- db = cursor.db;
callback.apply(null, arguments);
- cursor.close();
- return db.close();
+ return cursor.close();
});
});
} catch (e) {
@@ -880,11 +877,8 @@
return callback(readErr);
} else if (cursor != null) {
return cursor.toArray(function() {
- var db;
- db = cursor.db;
callback.apply(null, arguments);
- cursor.close();
- return db.close();
+ return cursor.close();
});
} else {
return callback();
View
16 services/user.js
@@ -749,11 +749,8 @@
return callback(readErr);
} else if (cursor != null) {
return cursor.toArray(function() {
- var db;
- db = cursor.db;
callback.apply(null, arguments);
- cursor.close();
- return db.close();
+ return cursor.close();
});
} else {
return callback();
@@ -812,8 +809,7 @@
var db;
db = cursor.db;
callback.apply(null, arguments);
- cursor.close();
- return db.close();
+ return cursor.close();
});
}
}).then(function(err, users, cb) {
@@ -921,6 +917,7 @@
var _ref, _ref2, _ref3;
_this.user = user;
if (cb == null) cb = function() {};
+ console.log('load team');
if (err) return callback(err);
if (((_ref = _this.user) != null ? _ref.team : void 0) != null) {
try {
@@ -936,9 +933,11 @@
var postGen, _ref, _ref2, _ref3, _ref4, _ref5;
_this.team = team;
if (cb == null) cb = function() {};
+ console.log('load posts');
if (err) return callback(err);
if ((_ref = _this.user) != null) _ref.team = _this.team;
if ((((_ref2 = _this.user) != null ? _ref2.posts : void 0) != null) && ((_ref3 = _this.user) != null ? (_ref4 = _ref3.posts) != null ? _ref4.length : void 0 : void 0)) {
+ console.log('load posts begin');
try {
postGen = require('./post');
postGen.init();
@@ -948,11 +947,13 @@
return cb(e);
}
} else {
+ console.log('load no posts');
return cb(null, null);
}
}).then(function(err, fullposts, cb) {
var post, posts, _ref, _ref2, _ref3, _ref4, _ref5;
if (cb == null) cb = function() {};
+ console.log('load invites');
if (fullposts != null) {
posts = (function() {
var _i, _len, _results;
@@ -985,6 +986,7 @@
}).then(function(err, invites, cb) {
var _ref, _ref2, _ref3, _ref4;
if (cb == null) cb = function() {};
+ console.log('load challenges');
if (err) return callback(err);
if ((_ref = _this.user) != null) _ref.invites = invites;
if ((_ref2 = _this.team) != null ? (_ref3 = _ref2.challenges) != null ? _ref3.length : void 0 : void 0) {
@@ -1002,6 +1004,7 @@
}).then(function(err, challenges, cb) {
var allmatches, async, match, matches, matchsvc, _ref, _ref2, _ref3;
if (cb == null) cb = function() {};
+ console.log('load matches');
if ((_ref = _this.team) != null) _ref.challenges = challenges;
if ((_ref2 = _this.user) != null) _ref2.challenges = challenges;
allmatches = (_ref3 = _this.team) != null ? _ref3.matches : void 0;
@@ -1074,6 +1077,7 @@
}
}).then(function(err, matches, cb) {
if (cb == null) cb = function() {};
+ console.log('load end');
_this.user.matches = matches;
return callback(null, _this.user);
});
View
19 services/utils.js
@@ -125,4 +125,23 @@
return wrapper;
};
+ exports.bind = function(fn, scope, args, appendArgs) {
+ var method;
+ if (arguments.length === 2) {
+ return function() {
+ return fn.apply(scope, arguments);
+ };
+ }
+ method = fn;
+ return function() {
+ var callArgs, _ref;
+ callArgs = args != null ? args : arguments;
+ if (appendArgs === true) {
+ callArgs = arguments.slice(0);
+ callArgs = callArgs.concat(args);
+ }
+ return method.apply((_ref = scope != null ? scope : global) != null ? _ref : window, callArgs);
+ };
+ };
+
}).call(this);
Please sign in to comment.
Something went wrong with that request. Please try again.