Skip to content

Instantly share code, notes, and snippets.

@vdeturckheim
Created March 13, 2016 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdeturckheim/0f43f6d4f4268cfb96d5 to your computer and use it in GitHub Desktop.
Save vdeturckheim/0f43f6d4f4268cfb96d5 to your computer and use it in GitHub Desktop.
'use strict';
const Joi = require('joi'); // object schema validation tool
const optionsSchema = Joi.object().keys({
// a string representing a hostname or an ip
host: Joi.alternatives().try(Joi.string().hostname(), Joi.string().ip()).default('127.0.0.1'),
// a number in the allowed TCP port range, default to default mongodb port
port: Joi.number().min(1).max(65535).default(27017),
// If no database name, we create an random one that look like 'database_abcd' with a,b,c,d random numbers
database: Joi.string().min(1).replace(' ', '_').default(`database_${Math.floor(Math.random() * 10000)}`),
// username to use to connect to the db, default to none
username: Joi.string().min(1).optional(),
// password to use to connect to the db, default to none
password: Joi.string().min(1).optional(),
// enable or disable mongoose debug mode, default to false
debug: Joi.boolean().default(false),
// reference to the implementation of promises to use
promise: Joi.string().min(1).optional(),
// array of strings to be used as logging tags for the plugin. Default is ["mongoose-nest"]
logTags: Joi.array().items(Joi.string().min(1)).default(['mongoose-nest'])
});
module.exports.validateOptions = function (options) {
const parsed = Joi.validate(options, optionsSchema);
return parsed;
};
module.exports.builConnectionString = function (validOptions) {
let result = 'mongodb://';
if (validOptions.username && validOptions.password) {
result += `${validOptions.username}:${validOptions.password}@`;
}
result += `${validOptions.host}:${validOptions.port}/${validOptions.database}`;
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment