From ea742e1ec3ba08f9e10091b00d5c8ac9ab0b1ecf Mon Sep 17 00:00:00 2001 From: Aiko Mastboom Date: Wed, 17 Apr 2013 00:10:05 +0200 Subject: [PATCH] reformatted --- mongodata.js | 41 +++++++++++++++++++++-------------------- preview.js | 14 +++++++------- responder.js | 4 ++-- routes.js | 49 ++++++++++++++++++++++++------------------------- server.js | 4 ++-- 5 files changed, 56 insertions(+), 56 deletions(-) diff --git a/mongodata.js b/mongodata.js index 7d6579c..45ae3be 100644 --- a/mongodata.js +++ b/mongodata.js @@ -8,12 +8,12 @@ module.exports = function (config) { config.debug && console.log('getMongoContent options', options); MongoClient.connect(config.mongo.server, config.mongo.options, function (err, db) { if (err) { - config.debug && console.log('ERR1 getMongoContent',err); + config.debug && console.log('ERR1 getMongoContent', err); return callback(err); } db.collection(options.collection, function (err, col) { if (err) { - config.debug && console.log('ERR2 getMongoContent',err); + config.debug && console.log('ERR2 getMongoContent', err); return callback(err); } if (options.query._id && !(options.query._id instanceof Object)) { @@ -21,7 +21,7 @@ module.exports = function (config) { } col.findOne(options.query, function (err, result) { if (err) { - config.debug && console.log('ERR3 getMongoContent',err); + config.debug && console.log('ERR3 getMongoContent', err); return callback(err); } if (!result) { @@ -37,11 +37,11 @@ module.exports = function (config) { config.debug && console.log('getMongoAttribute options', options); return getMongoContent(options, function (err, result) { if (err) { - config.errors && console.log('ERR1 getMongoAttribute',err); + config.errors && console.log('ERR1 getMongoAttribute', err); return callback(err); } var attribute_options = null; - config.debug && console.log('getMongoAttribute result', result); + config.debug && console.log('getMongoAttribute result', result); if (result && result.hasOwnProperty(options.attribute)) { attribute_options = { collection: options.collection, @@ -51,7 +51,7 @@ module.exports = function (config) { return getMongoContent(attribute_options, function (err, attribute_result) { if (err) { - config.errors && console.log('ERR2 getMongoAttribute',err); + config.errors && console.log('ERR2 getMongoAttribute', err); return callback(err); } config.debug && console.log('getMongoAttribute attribute_result', attribute_result); @@ -63,14 +63,14 @@ module.exports = function (config) { collection: options.collection, query: { parent: result._id, - name: result.name+'.'+options.attribute + name: result.name + '.' + options.attribute } }; config.debug && console.log('getMongoAttribute attribute_options', attribute_options); return getMongoContent(attribute_options, function (err, attribute_result) { if (err) { - config.errors && console.log('ERR3 getMongoAttribute',err); + config.errors && console.log('ERR3 getMongoAttribute', err); return callback(err); } if (attribute_result) { @@ -95,34 +95,35 @@ module.exports = function (config) { } }) } + function saveData(collection, data, callback) { if (data._id && !(data._id instanceof Object)) { data._id = new ObjectID.createFromHexString(data._id); } config.debug && console.log('saving', data._id); - collection.save(data, {safe:true}, callback); + collection.save(data, {safe: true}, callback); } function setMongoContent(data, options, callback) { config.debug && console.log('setMongoContent options', options); - MongoClient.connect(config.mongo.server, config.mongo.options, function (err, db) { + MongoClient.connect(config.mongo.server, config.mongo.options, function (err, db) { if (err) { - config.errors && console.log('ERR1 setMongoContent',err); + config.errors && console.log('ERR1 setMongoContent', err); return callback(err); } db.collection(options.collection, function (err, col) { if (err) { - config.errors && console.log('ERR2 setMongoContent',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); + config.debug && console.log('setMongoContent lookup by query', options.query); col.findOne(options.query, function (err, result) { - if (result){ - data._id=result._id; + if (result) { + data._id = result._id; } return saveData(col, data, callback); }) @@ -137,7 +138,7 @@ module.exports = function (config) { config.debug && console.log('setMongoAttribute options', options); getMongoContent(options, function (err, result, col) { if (err) { - config.errors && console.log('ERR1 setMongoAttribute',err); + config.errors && console.log('ERR1 setMongoAttribute', err); return callback(err); } var attribute_options = { @@ -148,7 +149,7 @@ module.exports = function (config) { attribute_options.query = {_id: result[options.attribute].guid}; getMongoContent(attribute_options, function (err, attribute_result, col) { if (err) { - config.errors && console.log('ERR2 setMongoAttribute',err); + config.errors && console.log('ERR2 setMongoAttribute', err); return callback(err); } attribute_result[options.attribute] = data; @@ -164,7 +165,7 @@ module.exports = function (config) { collection: options.collection, query: { parent: result._id, - name: result.name+'.'+options.attribute + name: result.name + '.' + options.attribute } }; config.debug && console.log('getMongoAttribute attribute_options', attribute_options); @@ -186,8 +187,8 @@ module.exports = function (config) { } return saveData(col, attribute_data, function (err, attribute_result) { - if(err){ - config.errors && console.log('ERR3 setMongoAttribute',err); + if (err) { + config.errors && console.log('ERR3 setMongoAttribute', err); } result[options.attribute] = { guid: attribute_result._id }; return saveData(col, result, callback); diff --git a/preview.js b/preview.js index 2d5f8b8..a724de2 100644 --- a/preview.js +++ b/preview.js @@ -15,10 +15,10 @@ module.exports = function (config) { var sourceBody = '\n' + - '\n' + - '\n' + - '\n' + - '\n'; + '\n' + + '\n' + + '\n' + + '\n'; var templateHead = Handlebars.compile(sourceHead); var templateBody = Handlebars.compile(sourceBody); @@ -34,15 +34,15 @@ module.exports = function (config) { function replace(marker, replacement) { var regExp = new RegExp(''); - if (html.match(regExp)){ + if (html.match(regExp)) { html = html.replace(regExp, replacement); } else { html = html.replace('', '\n'); } } - replace(options.collection+'_'+options.name+'_'+'_marker', styleMarkerReplacement); - replace(options.collection+'_'+options.name+'_'+'_marker', jsMarkerReplacement); + replace(options.collection + '_' + options.name + '_' + '_marker', styleMarkerReplacement); + replace(options.collection + '_' + options.name + '_' + '_marker', jsMarkerReplacement); return html; }; diff --git a/responder.js b/responder.js index b7dab54..52a5910 100644 --- a/responder.js +++ b/responder.js @@ -16,14 +16,14 @@ var getMimeType = function (ext) { module.exports = function (options, res, next) { var responder = function (err, result) { - console.log('err',err); + console.log('err', err); if (err) { if (/Data not found*/.test(err.message)) { res.status(404); } return next(err.message); } - console.log('responder options',options); + console.log('responder options', options); var contentType = getMimeType(options.ext); res.setHeader('Content-Type', contentType); var content = result; diff --git a/routes.js b/routes.js index ff7e43e..b341db3 100644 --- a/routes.js +++ b/routes.js @@ -19,7 +19,7 @@ module.exports = function (server, config) { collection: req.params.collection, attribute: req.params.attribute, ext: req.params.ext, - query: {_id:req.params.guid} + query: {_id: req.params.guid} }; mongodataInstance.getMongoAttribute(options, responder(options, res, next) @@ -32,7 +32,7 @@ module.exports = function (server, config) { var options = { collection: req.params.collection, ext: req.params.ext, - query: {_id:req.params.guid} + query: {_id: req.params.guid} }; mongodataInstance.getMongoContent(options, responder(options, res, next) @@ -47,7 +47,7 @@ module.exports = function (server, config) { collection: req.params.collection, attribute: req.params.attribute, ext: req.params.ext, - query: {name:req.params.name} + query: {name: req.params.name} }; mongodataInstance.getMongoAttribute(options, responder(options, res, next) @@ -60,7 +60,7 @@ module.exports = function (server, config) { var options = { collection: req.params.collection, ext: req.params.ext, - query: {name:req.params.name} + query: {name: req.params.name} }; mongodataInstance.getMongoContent(options, responder(options, res, next) @@ -75,7 +75,7 @@ module.exports = function (server, config) { } else { if (result) { var operation = null; - config.debug && console.log('handleMongoGetResult options',options, result); + config.debug && console.log('handleMongoGetResult options', options, result); var data = result; if (options.attribute) { data = result[options.attribute]; @@ -118,11 +118,11 @@ module.exports = function (server, config) { attribute: null }; if (splitId.length == 4) { - options.query= {_id:splitId[2]}; + options.query = {_id: splitId[2]}; options.attribute = splitId[3]; mongodataInstance.getMongoAttribute(options, handleMongoGetResult(options)); } else { - options.query={name:splitId[2]}; + options.query = {name: splitId[2]}; mongodataInstance.getMongoContent(options, handleMongoGetResult(options)); } }); @@ -134,7 +134,7 @@ module.exports = function (server, config) { config.errors && console.log('ERR1 handleMongoSetResult Error while saving document ', options.collection, JSON.stringify(options.query), options.attribute || "", err); return callback && callback(err); } - config.debug && console.log('current', current, 'result',result, 'options',options); + config.debug && console.log('current', current, 'result', result, 'options', options); if ((!current || !current.name) && result.name) { var operation = { op: [ { p: ['name'], oi: result.name, od: null } @@ -142,10 +142,10 @@ module.exports = function (server, config) { model.applyOp(options.documentId, operation, function appliedOp(error, version) { config.debug && console.log('setResult applyOp version', version); if (error) { - config.error && console.log('ERR2 handleMongoSetResult',error); + config.error && console.log('ERR2 handleMongoSetResult', error); return callback && callback(error); } - return callback && callback(null,version); + return callback && callback(null, version); }); } } @@ -159,22 +159,22 @@ module.exports = function (server, config) { config.errors && console.log('ERR1 handleMongoAttributeSetResult Error while saving document ', options.collection, JSON.stringify(options.query), options.attribute || "", err); return callback && callback(err); } - options.debug && console.log('current', current, 'result',result); + options.debug && console.log('current', current, 'result', result); if (result.hasOwnProperty('_id')) { config.debug && console.log('// new object created. need to update the parent object.'); var pieces = options.documentId.split(':'); - var parentDocId = pieces[0]+ ':' + pieces[1] + ':' + pieces[2]; + var parentDocId = pieces[0] + ':' + pieces[1] + ':' + pieces[2]; var operation = { op: [ - { p: [options.attribute], oi: { guid: result._id } , od: null } + { p: [options.attribute], oi: { guid: result._id }, od: null } ], v: options.operation.v }; model.applyOp(parentDocId, operation, function appliedOp(error, version) { config.debug && console.log('setResult applyOp parent version', version); if (error) { - config.error && console.log('ERR2 handleMongoAttributeSetResult',error); + config.error && console.log('ERR2 handleMongoAttributeSetResult', error); return callback && callback(error); } - return callback && callback(null,version); + return callback && callback(null, version); }) } } @@ -185,7 +185,7 @@ module.exports = function (server, config) { // 'applyOp' event is fired when an operational transform is applied to to a shareDoc // a shareDoc has changed and needs to be saved to mongo model.on('applyOp', function persistDocument(documentId, operation, current, previous) { - config.debug && console.log('applyOp',documentId,operation,current); + config.debug && console.log('applyOp', documentId, operation, current); var splitId = documentId.split(':'); var options = { documentId: documentId, @@ -195,21 +195,21 @@ module.exports = function (server, config) { operation: operation }; if (splitId.length == 4) { - options.query= {_id:splitId[2]}; + options.query = {_id: splitId[2]}; options.attribute = splitId[3]; var data = current; if (options.type == 'json') { data = JSON.stringify(current); } mongodataInstance.setMongoAttribute(data, options, handleMongoAttributeSetResult(options, current, function (err, result) { - if(err) { + if (err) { config.errors && console.log('ERR1 applyOp', err); } })); } else { - options.query={name:splitId[2]}; + options.query = {name: splitId[2]}; mongodataInstance.setMongoContent(current, options, handleMongoSetResult(options, current, function (err, result) { - if(err) { + if (err) { config.errors && console.log('ERR2 applyOp', err); } })); @@ -224,7 +224,7 @@ module.exports = function (server, config) { var options = { collection: req.params.collection, ext: req.params.ext, - query: {name:req.params.name}, + query: {name: req.params.name}, debug: req.query && req.query.hasOwnProperty('debug') }; mongodataInstance.getMongoContent(options, function (err, result) { @@ -233,11 +233,11 @@ module.exports = function (server, config) { } if (result) { var attribute_parts = options.query.name.split('.'); - var attribute = attribute_parts[attribute_parts.length-1]; + var attribute = attribute_parts[attribute_parts.length - 1]; var content = result[attribute]; - options.name=attribute_parts[0]; + options.name = attribute_parts[0]; //options.attribute=attribute; - config.debug && console.log('getPreviewContent content',content); + config.debug && console.log('getPreviewContent content', content); previewInstance.getPreviewHTML(options, content, responder(options, res, next) ); @@ -247,5 +247,4 @@ module.exports = function (server, config) { ); - }; \ No newline at end of file diff --git a/server.js b/server.js index db77289..d31e7e6 100644 --- a/server.js +++ b/server.js @@ -12,8 +12,8 @@ var config = { server: "mongodb://silo01.local:27017/Prototyper", options: { server: { - maxPoolSize:10, - auto_reconnect:true + maxPoolSize: 10, + auto_reconnect: true } } },