mirror of
/repos/Prototyper.git
synced 2025-12-30 06:31:32 +01:00
It appears MongoClient.connect only needs to called once
This commit is contained in:
parent
a76fb02f9a
commit
a6f71b6203
105
mongodata.js
105
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -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,
|
||||
|
||||
25
server.js
25
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);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user