Created
March 13, 2016 21:22
-
-
Save vdeturckheim/0f43f6d4f4268cfb96d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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