mirror of
https://github.com/HackPlan/RootPanel.git
synced 2026-03-29 08:38:02 +08:00
加入@update update
This commit is contained in:
@@ -6,10 +6,10 @@ db = require '../db'
|
||||
module.exports = class Model
|
||||
constructor: (@data) ->
|
||||
|
||||
@create : ->
|
||||
@create: ->
|
||||
throw 'this function must be overrided'
|
||||
|
||||
@createModels : (docs)->
|
||||
@createModels: (docs)->
|
||||
throw 'docs must be array' if not _.isArray docs
|
||||
results = []
|
||||
if docs.length is 1
|
||||
@@ -18,21 +18,21 @@ module.exports = class Model
|
||||
for doc in docs
|
||||
results.push @create doc
|
||||
results
|
||||
@table : ->
|
||||
@table: ->
|
||||
"#{@name.toLowerCase()}s"
|
||||
|
||||
@collection: ->
|
||||
db.mongo.collection @table()
|
||||
|
||||
set : (key, value = null) ->
|
||||
set: (key, value = null) ->
|
||||
if (_.isObject key) is 'object' then attrs = key else attrs[key] = value
|
||||
@data[k] = v for k, v of attrs
|
||||
return @
|
||||
|
||||
get : (attr) ->
|
||||
get: (attr) ->
|
||||
@data[attr]
|
||||
|
||||
@insert : (data, callback = null) ->
|
||||
@insert: (data, callback = null) ->
|
||||
@collection().insert data, {w:1}, (err, docs) =>
|
||||
throw err if err
|
||||
if callback
|
||||
@@ -47,19 +47,43 @@ module.exports = class Model
|
||||
else
|
||||
callback 'there is more then 1 documents with the same id'
|
||||
|
||||
update : (newObj , opts = {},callback) ->
|
||||
|
||||
remove: (callback = null)->
|
||||
@constructor.removeById @data._id,callback
|
||||
|
||||
@find : (data, opts = {}, callback = null) ->
|
||||
if _.isFunction data
|
||||
callback = data
|
||||
data = {}
|
||||
@update: (selector, documents,opts = {w: 1,multi: true},callback = null) ->
|
||||
if _.isFunction opts
|
||||
callback = opts
|
||||
opts = {w: 1,multi: true}
|
||||
throw 'arguments wrong' if not ((_.isObject selector) and (_.isObject documents))
|
||||
@collection().update selector,documents,opts,(err,numberUpdated)=>
|
||||
throw err if err
|
||||
if callback
|
||||
@find selector,(err,results)->
|
||||
throw err if err
|
||||
callback err,results
|
||||
|
||||
|
||||
update: (documents , opts = {w:1},callback = null) ->
|
||||
if _.isFunction opts
|
||||
callback = opts
|
||||
opts = {w: 1}
|
||||
throw 'arguments wrong' if not _.isObject documents
|
||||
@constructor.collection().update {_id: @data._id},documents,opts,(err,doc)=>
|
||||
throw err if err
|
||||
if callback
|
||||
results = @constructor.createModels doc
|
||||
callback err,results
|
||||
|
||||
|
||||
|
||||
@find: (selector, opts = {}, callback = null) ->
|
||||
if _.isFunction selector
|
||||
callback = selector
|
||||
selector = {}
|
||||
else if _.isFunction opts
|
||||
callback = opts
|
||||
opts = {}
|
||||
@collection().find(data, opts).toArray (err, docs)=>
|
||||
@collection().find(selector, opts).toArray (err, docs)=>
|
||||
throw err if err
|
||||
if callback
|
||||
results = @createModels docs
|
||||
|
||||
Reference in New Issue
Block a user