From 895883de0a75588cd0196ff8669d6aa08e04ece2 Mon Sep 17 00:00:00 2001 From: jysperm Date: Wed, 19 Feb 2014 20:55:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AA=E5=9C=A8=E5=90=AF=E5=8A=A8=E6=97=B6?= =?UTF-8?q?=E6=89=93=E5=BC=80=E4=B8=80=E6=AC=A1=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/db.coffee | 18 ++++++------- core/index.coffee | 4 ++- core/model/Model.coffee | 58 ++++++++++++++++++----------------------- 3 files changed, 38 insertions(+), 42 deletions(-) diff --git a/core/db.coffee b/core/db.coffee index 59ca0d6..1a386e9 100644 --- a/core/db.coffee +++ b/core/db.coffee @@ -1,12 +1,12 @@ -Db = (require 'mongodb').Db -Server = (require 'mongodb').Server -assert = require 'assert' +MongoClient = (require 'mongodb').MongoClient config = (require './config').db -# url = "mongodb://#{config.user}:#{config.passwd}@#{config.server}/#{config.name}" -url = "mongodb://127.0.0.1:27017/#{config.name}" -# MongoClient.connect url, {}, (err, db) -> -# assert.equal null,err -# module.exports = db +exports.connect = (callback = null)-> + #url = "mongodb://#{config.user}:#{config.passwd}@#{config.server}/#{config.name}" + url = "mongodb://#{config.server}/#{config.name}" -module.exports = new Db config.name, (new Server 'localhost',27017),safe : true \ No newline at end of file + MongoClient.connect url, {}, (err, db) -> + throw err if err + exports.mongo = db + + callback(db) if callback diff --git a/core/index.coffee b/core/index.coffee index 98c3975..77d253f 100644 --- a/core/index.coffee +++ b/core/index.coffee @@ -4,6 +4,7 @@ path = require 'path' config = require './config' router = require './router' +db = require './db' app = express() @@ -25,4 +26,5 @@ for url, controller of router.get for url, controller of router.post app.post url, controller -app.listen config.web.port +db.connect -> + app.listen config.web.port diff --git a/core/model/Model.coffee b/core/model/Model.coffee index 4c87d0c..3696d37 100644 --- a/core/model/Model.coffee +++ b/core/model/Model.coffee @@ -12,8 +12,8 @@ module.exports = class Model @table : -> "#{@name.toLowerCase()}s" - @collection: (db) -> - db.collection @table() + @collection: -> + db.mongo.collection @table() set : (key, value = null) -> if (_.isObject key) is 'object' then attrs = key else attrs[key] = value @@ -24,18 +24,16 @@ module.exports = class Model @data[attr] save : (data, callback) -> - db.open (err,db) => - @collection(db).insert data, {}, (err, docs) => - throw err if err - db.close() - if callback - results = [] - if docs.length is 1 - for doc in doc - results.push @create doc - else - results = @create docs[0] - callback err, results + @collection().insert data, {}, (err, docs) => + throw err if err + if callback + results = [] + if docs.length is 1 + for doc in doc + results.push @create doc + else + results = @create docs[0] + callback err, results @find : (data, opts = {}, callback = null) -> if _.isFunction data @@ -44,26 +42,22 @@ module.exports = class Model else if _.isFunction opts callback = opts opts = {} - db.open (err,db) => - @collection(db).find(data, opts).toArray (err, docs)=> - throw err if err - db.close() - if callback - results = [] - if docs.length is 1 - results = @create docs[0] - else - for doc in docs - results.push @create doc - callback err, results + @collection().find(data, opts).toArray (err, docs)=> + throw err if err + if callback + results = [] + if docs.length is 1 + results = @create docs[0] + else + for doc in docs + results.push @create doc + callback err, results @findById: (id, callback) -> if _.isString id id = new ObjectID id - db.open (err,db) => - @collection().findOne {_id: id}, (err, result) => - throw err if err - db.close() - result = @create result - callback err, result + @collection().findOne {_id: id}, (err, result) => + throw err if err + result = @create result + callback err, result