Files
RootPanel/core/model/Model.coffee
2014-03-09 22:03:22 +08:00

119 lines
3.0 KiB
CoffeeScript

MongoClient = require('mongodb').MongoClient
ObjectID = require('mongodb').ObjectID;
_ = require 'underscore'
db = require '../db'
# @param callback(args, callback)
mongoOverloadHelper = (that, commend, args, callback) ->
args = Array.prototype.slice.call args, 0
originalCallback = _.find args, _.isFunction
collection = that.collection()
args[args.length - 1] = (err, result) ->
callback err, result, originalCallback
commend.apply collection, args
module.exports = class Model
constructor: (@data) ->
@create: ->
throw 'this function must be overrided'
@createModels: (docs) ->
result = []
for doc in docs
result.push @create doc
return result
@collection: ->
return db.mongo.collection "#{@name.toLowerCase()}s"
@find: (selector, options, callback) ->
args = Array.prototype.slice.call arguments, 0
callback = _.find args, _.isFunction
collection = @collection()
args[args.length - 1] = null
collection.find.apply(collection, args).toArray (err, result) =>
throw err if err
callback @createModels result
@findOne: (selector, options, callback) ->
mongoOverloadHelper @, @collection().findOne, arguments, (err, result, callback) =>
throw err if err
if result
callback @create result
else
callback null
@findById: (id, callback) ->
@findOne {_id: id}, callback
@insert: (data, options, callback = null) ->
mongoOverloadHelper @, @collection().insert, arguments, (err, result, callback) =>
throw err if err
if callback
if _.isArray data
callback @createModels result
else
callback @create result[0]
@update: (selector, documents, options, callback = null) ->
mongoOverloadHelper @, @collection().update, arguments, (err, result, callback) =>
throw err if err
if callback
callback result
save: (options, callback = null) ->
mongoOverloadHelper @constructor, @constructor.collection().save, arguments, (err, result, callback) =>
throw err if err
if callback
callback result
update: (modifiers, options, callback = null) ->
args = Array.prototype.slice.call arguments, 0
callback = _.find args, _.isFunction
collection = @constructor.collection()
args.unshift {_id: @data._id}
args[args.length - 1] = (err, result) ->
throw err if err
if callback
callback result
collection.update.apply collection, args
@remove: (selector, options, callback = null) ->
mongoOverloadHelper @, @collection().remove, arguments, (err, result, callback) =>
throw err if err
if callback
callback result
remove: (options, callback = null) ->
args = Array.prototype.slice.call arguments, 0
callback = _.find args, _.isFunction
collection = @constructor.collection()
args.unshift {_id: @data._id}
args[args.length - 1] = (err, result) ->
throw err if err
if callback
callback result
collection.remove.apply collection, args