diff --git a/mongodata.js b/mongodata.js index 67b82b2..b0f30d4 100644 --- a/mongodata.js +++ b/mongodata.js @@ -1,47 +1,40 @@ -var MongoClient = require('mongodb').MongoClient; var ObjectID = require('mongodb').ObjectID; -module.exports = function (config) { +module.exports = function (db, config) { /* - options: - collection (mandatory) - query (mandatory) - */ + options: + * collection (mandatory) + * query (mandatory) + */ function getMongoContent(options, callback) { config.debug && console.log('getMongoContent options', options); - MongoClient.connect(config.mongo.server, config.mongo.options, function connection(err, db) { + return db.collection(options.collection, function collection(err, col) { if (err) { - config.errors && console.log('ERR1 getMongoContent', err); + config.errors && console.log('ERR2 getMongoContent', err); return callback(err); } - return db.collection(options.collection, function collection(err, col) { - if (err) { - config.errors && console.log('ERR2 getMongoContent', err); + if (!options.query) { + return callback(new Error('Data not found ' + options.collection + '/ missing query')); + } + if (options.query._id && !(options.query._id instanceof Object)) { + try { + options.query._id = new ObjectID.createFromHexString(options.query._id); + } catch (err) { + config.errors && console.log('ERR3 getMongoContent', err); return callback(err); } - if (!options.query) { - return callback(new Error('Data not found ' + options.collection + '/ missing query')); + } + return col.findOne(options.query, function foundOne(err, result) { + if (err) { + config.errors && console.log('ERR4 getMongoContent', err); + return callback(err); } - if (options.query._id && !(options.query._id instanceof Object)) { - try { - options.query._id = new ObjectID.createFromHexString(options.query._id); - } catch (err) { - config.errors && console.log('ERR3 getMongoContent', err); - return callback(err); - } + if (!result) { + return callback(new Error('Data not found ' + options.collection + '/' + JSON.stringify(options.query))); } - return col.findOne(options.query, function foundOne(err, result) { - if (err) { - config.errors && console.log('ERR4 getMongoContent', err); - return callback(err); - } - if (!result) { - return callback(new Error('Data not found ' + options.collection + '/' + JSON.stringify(options.query))); - } - return callback(null, result, col); - }); + return callback(null, result, col); }); }); } @@ -89,10 +82,10 @@ module.exports = function (config) { /* options: - collection (mandatory) - query (mandatory) - attribute (mandatory) - type [json|text] : returns {} or "" when not found. + * collection (mandatory) + * query (mandatory) + * attribute (mandatory) + * type [json|text] : returns {} or "" when not found. */ function getMongoAttribute(options, callback) { config.debug && console.log('getMongoAttribute options', options); @@ -169,35 +162,29 @@ module.exports = function (config) { function setMongoContent(data, options, callback) { config.debug && console.log('setMongoContent options', options); - MongoClient.connect(config.mongo.server, config.mongo.options, function connection(err, db) { + return db.collection(options.collection, function collection(err, col) { if (err) { - config.errors && console.log('ERR1 setMongoContent', err); + config.errors && console.log('ERR2 setMongoContent', err); return callback(err); } - return db.collection(options.collection, function collection(err, col) { - if (err) { - config.errors && console.log('ERR2 setMongoContent', err); - return callback(err); - } - if (options.operation) { - data.version = options.operation.v; - } - if (!data._id) { - config.debug && console.log('setMongoContent lookup by query', options.query); - return col.findOne(options.query, function foundOne(err, result) { - if (err) { - config.errors && console.log('ERR3 setMongoContent', err); - return callback(err); - } - if (result) { - data._id = result._id; - } - return saveData(col, data, callback); - }) - } else { + if (options.operation) { + data.version = options.operation.v; + } + if (!data._id) { + config.debug && console.log('setMongoContent lookup by query', options.query); + return col.findOne(options.query, function foundOne(err, result) { + if (err) { + config.errors && console.log('ERR3 setMongoContent', err); + return callback(err); + } + if (result) { + data._id = result._id; + } return saveData(col, data, callback); - } - }); + }) + } else { + return saveData(col, data, callback); + } }); } diff --git a/routes.js b/routes.js index e029dd4..c38dacd 100644 --- a/routes.js +++ b/routes.js @@ -7,13 +7,13 @@ var importer = require('./importer.js'); var path = require('path'); var fs = require('fs'); -module.exports = function (app, config) { +module.exports = function (app, db, config) { // share wraps express app with http.Server var server = ShareJS.server.attach(app, config.share); var model = app.model; - var mongoDataInstance = mongoData(config); + var mongoDataInstance = mongoData(db, config); var route; route = config.api.data + '/:collection/:guid/:attribute.:ext(css|less|js|html)'; app.get(route, diff --git a/server.js b/server.js index d582797..5f1f9ac 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,7 @@ var connect = require('connect'); var express = require('express'); -var instance = require('./routes.js'); +var MongoClient = require('mongodb').MongoClient; +var addRoutes = require('./routes.js'); process.title = "Prototyper"; @@ -50,15 +51,21 @@ app.use('/lib/markdown', express.static(config.statics.markdown_client)); //noinspection JSUnresolvedFunction app.use('/lib/ace', express.static(config.statics.ace_client)); - -var server = instance(app, config); - -server.listen(config.port, function handleServerResult(err) { +MongoClient.connect(config.mongo.server, config.mongo.options, function connection(err, db) { if (err) { - app.stop(); - console.log('Server error', err); + config.errors && console.log('ERR connection to database', err); return process.exit(1); } - config.debug && console.log('routes', app.routes); - return console.log('Server running at http://127.0.0.1:', config.port); + var server = addRoutes(app, db, config); + return server.listen(config.port, function handleServerResult(err) { + if (err) { + app.stop(); + console.log('Server error', err); + return process.exit(1); + } + config.debug && console.log('routes', app.routes); + return console.log('Server running at http://127.0.0.1:', config.port); + }); }); + +