只在启动时打开一次数据库连接

This commit is contained in:
jysperm
2014-02-19 20:55:27 +08:00
parent 09e3fc7b5c
commit 895883de0a
3 changed files with 38 additions and 42 deletions

View File

@@ -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
MongoClient.connect url, {}, (err, db) ->
throw err if err
exports.mongo = db
callback(db) if callback

View File

@@ -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

View File

@@ -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