Advertisement
Guest User

Untitled

a guest
Mar 13th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. 'use strict';
  2. const Joi = require('joi'); // object schema validation tool
  3.  
  4. const optionsSchema = Joi.object().keys({
  5. // a string representing a hostname or an ip
  6. host: Joi.alternatives().try(Joi.string().hostname(), Joi.string().ip()).default('127.0.0.1'),
  7. // a number in the allowed TCP port range, default to default mongodb port
  8. port: Joi.number().min(1).max(65535).default(27017),
  9. // If no database name, we create an random one that look like 'database_abcd' with a,b,c,d random numbers
  10. database: Joi.string().min(1).replace(' ', '_').default(`database_${Math.floor(Math.random() * 10000)}`),
  11. // username to use to connect to the db, default to none
  12. username: Joi.string().min(1).optional(),
  13. // password to use to connect to the db, default to none
  14. password: Joi.string().min(1).optional(),
  15. // enable or disable mongoose debug mode, default to false
  16. debug: Joi.boolean().default(false),
  17. // reference to the implementation of promises to use
  18. promise: Joi.string().min(1).optional(),
  19. // array of strings to be used as logging tags for the plugin. Default is ["mongoose-nest"]
  20. logTags: Joi.array().items(Joi.string().min(1)).default(['mongoose-nest'])
  21. });
  22.  
  23.  
  24. module.exports.validateOptions = function (options) {
  25.  
  26. const parsed = Joi.validate(options, optionsSchema);
  27. return parsed;
  28. };
  29.  
  30. module.exports.builConnectionString = function (validOptions) {
  31.  
  32. let result = 'mongodb://';
  33.  
  34. if (validOptions.username && validOptions.password) {
  35. result += `${validOptions.username}:${validOptions.password}@`;
  36. }
  37.  
  38. result += `${validOptions.host}:${validOptions.port}/${validOptions.database}`;
  39.  
  40. return result;
  41. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement