Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
157 lines (137 sloc) 6.29 KB
_und = require "underscore"
mongoose = require "mongoose"
module.exports = class ModelLoader
constructor: (@dbPath,@version,@winston,@errDocUrl) ->
mongoose.connect(@dbPath)
autoload: (serv,modelpath) ->
fs = require "fs"
path = require "path"
@winston.info "Loading models from path " + modelpath
files = fs.readdirSync modelpath
modelNames = _und.map files, (f) ->
path.basename f
_und.each modelNames, (modelName) =>
if modelName != undefined
suffix = ".coffee"
suffix1 = ".js"
if (modelName.indexOf(suffix, modelName.length - suffix.length) != -1 || modelName.indexOf(suffix1, modelName.length - suffix1.length) != -1)
modelC = require modelpath + "/" + modelName
@winston.info "Creating mogoose object for: "+modelName
model = new modelC({winston: @winston})
@expose(model,serv)
# The expose will introduce its own set of request handler for handling model requests
expose : (model, serv)->
collection = model.getCollection()
@winston.info 'ModelLoader: installing request handlers for /'+collection
serv.get '/'+@version+'/'+collection, (req, res) =>
@winston.info 'ModelLoader: GET for '+collection+' received, sending the collection for '+model.getDBModel().modelName
skipC = 0;
projection = undefined
if req.query["offset"] != undefined
skipC = parseInt req.query["offset"]
@winston.info "Query Parameter 'offset' provided with value "+skipC
if isNaN(skipC)
@.createJSONErrMsg res, 400,
'Bad Request Query Parameter provided to the clb-modelloader API for "'+model.getCollection()+'": "offset" parameter is not at number',
'0001',@errDocUrl+'0001'
return
limit = model.getQueryLimit()
if req.query["limit"] != undefined
limC = parseInt req.query["limit"]
@winston.info "Query Parameter 'limit' provided with value "+limC
if isNaN(limC)
@.createJSONErrMsg res, 400,
'Bad Request Query Parameter provided to the clb-modelloader API for "'+model.getCollection()+'": "limit" parameter is not at number',
'0001',@errDocUrl+'0001'
return
else
if limC < model.getQueryLimit()
limit = limC
if req.query["fields"] != undefined
projection = {}
_und.each req.query["fields"].split(","), (elem, index, list) ->
projection[elem] = 1
@winston.info("Got projection "+projection)
if req.query["maxRec"] != undefined
@winston.info "No count query necessary"
maxRec = parseInt req.query["maxRec"]
@winston.info "Query Parameter 'maxRec' provided with value "+maxRec
if isNaN(maxRec)
@.createJSONErrMsg res, 400,
'Bad Request Query Parameter provided to the clb-modelloader API for "'+model.getCollection()+'": "maxRec" parameter is not at number',
'0002',@errDocUrl+'0002'
return
@getCollection res,model,skipC,maxRec,projection,limit
else
query = model.getDBModel().find({})
query.count (err, count) =>
@winston.info "Number of records "+count+" skip "+skipC
@getCollection res,model,skipC,count,projection,limit
serv.get '/'+@version+'/'+collection+'/:id', (req, res) =>
@winston.info 'ModelLoader: GET received for '+collection+' model '+req.params.id
conditions = { _id: req.params.id }
model.getDBModel().find(conditions, (err, docs) =>
@winston.info "JSON Data", docs
if err != null
@.createJSONErrMsg res, 500,
'There was a technical error when requesting an entity "'+model.getCollection()+'":'+err,
'0100',@errDocUrl+'0100'
else
res.send(docs))
serv.put '/'+@version+'/'+collection+'/:id', (req, res) =>
@winston.info 'ModelLoader: PUT received for model '+req.params.id
@winston.info "JSON Data received ", req.body
conditions = { _id: req.params.id }
doc = req.body
delete doc._id
model.getDBModel().update conditions, doc,{}, (err, numAffected) =>
@winston.info 'ModelLoader: Update done on '+numAffected+" row - errors: "+err
if err == null
res.send(doc)
else
@.createJSONErrMsg res, 500,
'There was a technical error when updating an entity "'+model.getCollection()+'":'+err,
'0100',@errDocUrl+'0100'
serv.del '/'+@version+'/'+collection+'/:id', (req, res) =>
@winston.info 'ModelLoader: DELETE received for model '+req.params.id
conditions = { _id: req.params.id }
model.getDBModel().remove conditions, (err, numAffected) =>
@winston.info 'ModelLoader: Delete done on '+numAffected+" row - errors: "+err
if err == null
res.json 200
else
res.json err, 500
serv.post '/'+@version+'/'+collection, (req, res) =>
@winston.info 'ModelLoader: POST received for model '+ collection
@winston.info "JSON Data received ", req.body
conditions = { _id: req.params.id }
doc = req.body
@winston.info 'ModelLoader: Creating new instance for '+ model.getModelName()
dbModel = model.getDBModel()
obj = new dbModel(doc)
obj.save()
@winston.info 'ModelLoader: New record created'
@winston.info obj
res.send obj
createJSONErrMsg: (res, statusCode, usrMsg, errCode, moreInfo) ->
errMsg = {
'message' : usrMsg,
'errorCode' : errCode,
'moreInfo' : moreInfo
}
res.json errMsg, statusCode
getCollection: (res,model,skipC,count,projection,limit) ->
@winston.info "Got projection '"+projection+"'"
query = model.getDBModel().find({},projection).limit(limit).skip(skipC).sort({"_id":-1})
query.exec {}, (err, docs) =>
@winston.info "Fetched records with skip "+skipC
countStr = count+''
limitStr = limit+''
skipStr = skipC+''
docs.push( _maxRec: countStr, _limit: limitStr, _offset: skipStr);
if err != null
@.createJSONErrMsg res, 500,
'There was a technical error when requesting entities "'+model.getCollection()+'":'+err,
'0100',@errDocUrl+'0100'
else
res.send(docs)