Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '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;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement