Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mongodb connection string method #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions helpers/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,33 +91,34 @@ 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 => {
reject(err.message);
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();
Expand Down
2 changes: 1 addition & 1 deletion helpers/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 33 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 --------------------------------------------------------

Expand Down