Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
314 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.