Skip to content
Snippets Groups Projects
Commit be779894 authored by W. Ross Morrow's avatar W. Ross Morrow
Browse files

trying a 'load all collections' default in mongodb

parent 4025dc1f
No related branches found
No related tags found
No related merge requests found
{ {
"name": "darc-mongoreadyserver", "name": "darc-mongoreadyserver",
"version": "0.1.18", "version": "0.1.19",
"description": "A \"MongoDB ready\" server", "description": "A \"MongoDB ready\" server",
"main": "src/index.js", "main": "src/index.js",
"scripts": { "scripts": {
...@@ -8,12 +8,13 @@ ...@@ -8,12 +8,13 @@
}, },
"author": "W. Ross Morrow", "author": "W. Ross Morrow",
"license": "Apache-2.0", "license": "Apache-2.0",
"repository":"https://code.stanford.edu/morrowwr/darc-mongoreadyserver",
"dependencies": { "dependencies": {
"axios": "^0.19.2", "axios": "^0.19.2",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"cors": "^2.8.5", "cors": "^2.8.5",
"daat-coordinator": "^1.0.6", "daat-coordinator": "^1.0.6",
"darc-mongoreadyserverconfig": "^0.1.3", "darc-mongoreadyserverconfig": "^0.1.4",
"express": "^4.17.1", "express": "^4.17.1",
"log4js": "^6.1.1", "log4js": "^6.1.1",
"mongodb": "^3.5.2", "mongodb": "^3.5.2",
......
...@@ -76,7 +76,7 @@ const loadMongoDBClient = ( config ) => { ...@@ -76,7 +76,7 @@ const loadMongoDBClient = ( config ) => {
// local variables // local variables
var col = { } , waitingForDB , waitingTimer; var col = { } , waitingForDB , waitingTimer;
// set "global" variables // define collection names to search for
if( config.mdb.names ) { if( config.mdb.names ) {
mongodbNames = { ...mongodbNames , ..._config.mdb.names }; mongodbNames = { ...mongodbNames , ..._config.mdb.names };
} }
...@@ -89,7 +89,8 @@ const loadMongoDBClient = ( config ) => { ...@@ -89,7 +89,8 @@ const loadMongoDBClient = ( config ) => {
} , 1000 ); } , 1000 );
// connect to MongoDB // connect to MongoDB
_mongodb.MongoClient.connect( config.mdb.uri , { useNewUrlParser: true , useUnifiedTopology: true } , function ( err , client ) { let mdbOptions = { useNewUrlParser: true , useUnifiedTopology: true };
_mongodb.MongoClient.connect( config.mdb.uri , mdbOptions , ( err , client ) => {
if( err ) { return reject(err); } if( err ) { return reject(err); }
...@@ -105,59 +106,63 @@ const loadMongoDBClient = ( config ) => { ...@@ -105,59 +106,63 @@ const loadMongoDBClient = ( config ) => {
// list of collection names // list of collection names
var names = items.map( i => i.name ); var names = items.map( i => i.name );
// this is the list of __ desired __ collections __ missing __ if( _config.mdb.names ) {
var missing = Object.values( mongodbNames ).filter( (i) => ( names.indexOf(i) < 0 ) );
// process different possibilities // this is the list of __desired__ collections __missing__
if( missing.length === 0 ) { var missing = Object.values( mongodbNames ).filter( (i) => ( names.indexOf(i) < 0 ) );
// define the collection references // process different possibilities...
Object.keys( mongodbNames ).forEach( c => { if( missing.length === 0 ) { // there are NO missing collections
// wrapper reference for each collection "c", named "mongodbNames[c]" in this MongoDB instance
col[c] = client.db( config.mdb.db ).collection( mongodbNames[c] );
// log that we loaded this collection
_log.info( ` Loaded collection "${c}" (from ${mongodbNames[c]})` );
} );
// we're done // define the collection references
resolve( { client : client , col : col } ); Object.keys( mongodbNames ).forEach( c => {
// wrapper reference for each collection "c", named "mongodbNames[c]" in this MongoDB instance
col[c] = client.db( config.mdb.db ).collection( mongodbNames[c] );
// log that we loaded this collection
_log.info( ` Loaded collection "${c}" (from ${mongodbNames[c]})` );
} );
} else { // we're done
resolve( { client : client , col : col } );
let done = 0 , todo = Object.keys( mongodbNames ).length; } else { // there are SOME missing collections
// define the collection references let done = 0 , todo = Object.keys( mongodbNames ).length;
Object.keys( mongodbNames ).forEach( c => {
if( missing.indexOf( mongodbNames[c] ) >= 0 ) { // define the collection references
Object.keys( mongodbNames ).forEach( c => {
// create collection... is the callback ok? need coordinator? if( missing.indexOf( mongodbNames[c] ) >= 0 ) { // collection does NOT exist
client.db( config.mdb.db ).createCollection( mongodbNames[c] , ( error , newcol ) => {
// wrapper reference for each collection "c", named "mongodbNames[c]" in this MongoDB instance
col[c] = newcol;
// log that we created this collection
_log.info( ` Created collection "${mongodbNames[c]}" (for ${c})` );
//
done += 1;
//
if( done == todo ) { resolve( { client : client , col : col } ); }
} );
} else { // create collection... is the callback ok? need coordinator?
client.db( config.mdb.db ).createCollection( mongodbNames[c] , ( error , newcol ) => {
// wrapper reference for each collection "c", named "mongodbNames[c]" in this MongoDB instance
col[c] = newcol;
// log that we created this collection
_log.info( ` Created collection "${mongodbNames[c]}" (for ${c})` );
//
done += 1; if( done == todo ) { resolve( {client:client,col:col} ); }
} );
// wrapper reference for each collection "c", named "mongodbNames[c]" in this MongoDB instance } else {
col[c] = client.db( _config.mdb.db ).collection( mongodbNames[c] );
// log that we loaded this collection // wrapper reference for each collection "c", named "mongodbNames[c]" in this MongoDB instance
_log.info( ` Loaded collection "${c}" (from ${mongodbNames[c]})` ); col[c] = client.db( _config.mdb.db ).collection( mongodbNames[c] );
// // log that we loaded this collection
done += 1; _log.info( ` Loaded collection "${c}" (from ${mongodbNames[c]})` );
// //
if( done == todo ) { resolve( { client : client , col : col } ); } done += 1; if( done == todo ) { resolve( {client:client,col:col} ); }
}
} } );
}
} ); } else {
names.forEach( c => {
col[c] = client.db( _config.mdb.db ).collection( c )
} );
} }
} ); } );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment