mirror of
https://github.com/HackPlan/RootPanel.git
synced 2026-05-22 06:34:02 +08:00
41 lines
781 B
CoffeeScript
41 lines
781 B
CoffeeScript
MongoClient = (require 'mongodb').MongoClient
|
|
ObjectID = require('mongodb').ObjectID
|
|
_ = require 'underscore'
|
|
|
|
config = require './config'
|
|
|
|
exports.db = {}
|
|
|
|
exports.connect = (callback) ->
|
|
MongoClient.connect config.mongodb, {}, (err, db) ->
|
|
throw err if err
|
|
exports.mongo = db
|
|
|
|
callback db
|
|
|
|
exports.buildModel = (collection) ->
|
|
model = exports.mongo.collection collection
|
|
|
|
model.findId = (id, callback) ->
|
|
if _.isString id
|
|
id = exports.ObjectID id
|
|
|
|
model.findOne
|
|
_id: id
|
|
, callback
|
|
|
|
return model
|
|
|
|
exports.ObjectID = (id) ->
|
|
try
|
|
return new ObjectID id
|
|
catch e
|
|
return null
|
|
|
|
exports.buildByXXOO = (xxoo, mongo) ->
|
|
return (value, callback) ->
|
|
selector = {}
|
|
selector[xxoo] = value
|
|
|
|
mongo.findOne selector, callback
|