Setting the Database connection

The Voluntarily Application uses Mongodb to persist its data so both in local development, testing and in production we need to provide the application with the location and if required authentication details to access the database.

see https://voluntarily.atlassian.net/wiki/spaces/VP/pages/4685950/MongoDB+Atlas+Cloudfor how to setup a cloud mongodb.

or install your own locally.

e. $ brew install mongodb

The app sets up the database connection in server.js.

if (process.env.NODE_ENV !== 'test') { mongoose.connect(serverConfig.mongoURL, (error) => { if (error) { console.error('Please make sure Mongodb is installed and running!'); // eslint-disable-line no-console throw error; } // feed some dummy data in DB. initialPosts(); initialActivities(); initialOpportunities(); initialOrganisations(); initialPeople(); }); }

 

and gets the config string from config.js

const config = { mongoURL: process.env.MONGO_URL || 'mongodb://localhost:27017/vly-dev', port: process.env.PORT || 8000, };

This sets the mongoURL to

  • either the default localhost server with a database of vly-dev

  • or whatever is in the enclosing environment variable MONGO_URL.

Note that in development mode if the initial database is empty it is populated by dummy data by the calls initialActivities etc. These simply load dummy JSON files stored with each module into the database.

Note: this does not occur in test mode. Tests cannot assume anything about the database and must populate or use mocks accordingly.

Setting the environment

in a terminal or shell.

export MONGO_URL=mongodb+srv://vly-client:ZhF3BUDiwpy8C3xK@cluster0-kwmsu.mongodb.net/vly-test?retryWrites=true

Please don’t commit scripts that expose your passwords into the code base.