Ok so, the gist: I've got a node.js service with (what should be) previously defined models and schemas, but I want to make a new connection to MongoDB in an effort to parallelize some bulk operations.
app.js:
// bunch of requires
var mongoose = require('mongoose');
mongoose.Promise = require('q').Promise;
if( mongoose.connection.readyState === 0 ) {
mongoose.connect('mongo:27017/MyDbName?connectTimeoutMS=30000', function(err) { if (err) console.log(err); }); // connect to our database
}
var app = express();
// some more stuff happens here
require('./routes/identityService')(app);
identityService.js:
require('./proc/importUserProcessor');
module.exports = function(app) {
// Create an express router to serve our identity service.
var router = express.Router();
router.post('/request/import', (req, res) => {
importProcessor.processImportFiles(req, res)
.done(() => {
res.sendStatus(200);
}, err => {
log.error('Error importing: ', err);
res.sendStatus(599);
});
});
authSchema.js:
var mongoose = require('mongoose');
var Q = require("q");
mongoose.Promise = Q.Promise;
var myDB = mongoose.connection.useDb('MyDbName');
// define the schema for our user model
var userSchema = mongoose.Schema({
// schema here
});
// create the model for users and expose it to our app
var userModel = myDB.model('Users', userSchema);
module.exports = {
User : userModel
};
and lastly importUserProcessor.js:
const Q = require('q');
const mongoose = require('mongoose');
mongoose.Promise = Q.Promise;
const AuthSchema = require('../models/authSchema');
const processingFunc = (chunk, encoding, completeCb) => {
let connUri = 'mongo:27017/MyDbName?connectTimeoutMS=30000';
let dbConn = ?????
// I've tried mongoose.createConnection(connUri) and
// mongoose.connection.useDb('MyDbName') but
// no matter what I've tried, it doesn't find any of the models
// from the authSchema file
dbConn.on('error', err=>{ //stuff });
dbConn.once('open', () => { // tried it in this block and out } );
let User = dbConn.model('Users'); // or 'User' or 'Userss' or 'users' or 'user'
dbConn.close();
completeCb();
};
const processImportFiles = (req, res) => {
// calls some streams which call processingFunc above
};
module.exports = {
processImportFiles: processImportFiles
};
So, hitting the defined route with files parses said files, then throws them through some streams, with the idea being to make "parallel" calls to the database to insert new records in bulk after some processing. I was hoping to do so with new connections, but no success. Anyone got anything?
via fetherolfjd
No comments:
Post a Comment