diff --git a/helpers/database.js b/helpers/database.js index 9a93448..22367a4 100644 --- a/helpers/database.js +++ b/helpers/database.js @@ -91,20 +91,20 @@ const getRelationships = datasets => { return relationships; }; -const getMongodbSchemas = params => { +const getMongodbSchemas = (params) => { return new Promise(async (resolve, reject) => { - if (!params.host) { + if (params.connection_method !== 'connection string' && !params.host) { return reject('host parameter is undefined'); } - if (!params.dbname) { + if (params.connection_method !== 'connection string' && !params.dbname) { return reject('dbname parameter is undefined'); } await generalHelper.timeout(2000); // Get mongodb connection url - const uri = getMongodbConnectionUrl(params); + const uri = params.connection_method == 'connection string' ? params.connection_string : getMongodbConnectionUrl(params); const client = await MongoClient.connect(uri, { useNewUrlParser: true }) .catch(err => { @@ -112,12 +112,13 @@ const getMongodbSchemas = params => { return null; }); + if (!client) { return; } // Connect to the proper db - const db = client.db(params.dbname); + const db = client.db(client.options.dbName); const datasets = {}; const collections = await db.listCollections().toArray(); diff --git a/helpers/generator.js b/helpers/generator.js index d357376..dd4aea3 100644 --- a/helpers/generator.js +++ b/helpers/generator.js @@ -59,7 +59,7 @@ const createAdminTemplate = async (databaseType, models, generalParams, dbParams const createDotEnvFile = (projectPath, generalParams, dbParams, database, useLocalCli) => { const amDbUrl = database === 'mongodb' ? - dbHelper.getMongodbConnectionUrl(dbParams) : + dbParams.connection_method == "connection string" ? dbParams.connection_string : dbHelper.getMongodbConnectionUrl(dbParams) : dbHelper.getSQLConnectionUrl(database, dbParams); const fileContent = `// IMPORTANT: This file should be removed from production env diff --git a/index.js b/index.js index 5e31650..8ab6d6e 100755 --- a/index.js +++ b/index.js @@ -141,11 +141,39 @@ const commandLine = () => { // Ask for the database creddentials ---------------------------------------------- - // Ask for the mission credentials & connection infos - // Just ask for the missing infos thanks to the second parameter - const databaseQuestions = getDatabaseCredentialsQuestions(params.db); - const databaseExistingAnswers = _.omit(params, ['name', 'id', 'sk', 'hash', 'db']); - const databaseCredentials = await inquirer.prompt(databaseQuestions, databaseExistingAnswers); + let databaseCredentials = {}; + + const isMongodb = params.db === 'mongodb'; + if(isMongodb) { + const connectionMethod = await inquirer.prompt([{ + name: 'connection_method', + type: 'list', + message: 'How do you want to connect? ', + choices: ['connection string', 'parameters (host, port, user, password, database name)'] + }]); + databaseCredentials.connection_method = connectionMethod.connection_method; + } + + if(isMongodb && databaseCredentials.connection_method !== 'connection string'){ + // Ask for the mission credentials & connection infos + // Just ask for the missing infos thanks to the second parameter + const databaseQuestions = getDatabaseCredentialsQuestions(params.db); + const databaseExistingAnswers = _.omit(params, ['name', 'id', 'sk', 'hash', 'db']); + databaseCredentials = await inquirer.prompt(databaseQuestions, databaseExistingAnswers); + }else{ + const databaseConnectionString = await inquirer.prompt([ + { + name: 'connection_string', + type: 'input', + message: 'Enter your MongoDB connection string', + validate: value => { + return value ? true : 'You have to set your connection string'; + } + } + ]); + databaseCredentials.connection_string = databaseConnectionString.connection_string; + databaseCredentials.dbname = databaseConnectionString.dbname; + } // Connect to the database --------------------------------------------------------