Form to configure database connection

This commit is contained in:
Jim Laurie 2018-01-05 15:17:59 +01:00
parent 7a7bd4d649
commit a892a6943d
5 changed files with 127 additions and 20 deletions

View File

@ -1,17 +0,0 @@
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-mongoose",
"settings": {
"client": "mongo",
"host": "localhost",
"port": 27017,
"database": "development",
"username": "",
"password": ""
},
"options": {}
}
}
}

View File

@ -0,0 +1,12 @@
'use strict';
module.exports = scope => {
// Finally, return the JSON.
return {
defaultConnection: 'default',
connections: {
default: scope.database
}
};
};

View File

@ -10,6 +10,7 @@ const path = require('path');
// Public node modules.
const _ = require('lodash');
const fs = require('fs-extra');
const inquirer = require('inquirer');
// Logger.
const logger = require('strapi-utils').logger;
@ -45,8 +46,113 @@ module.exports = (scope, cb) => {
// ...
}
logger.info('Copying the dashboard...');
logger.info('Let\s configurate the connection to your database:');
inquirer
.prompt([
{
type: 'list',
prefix: '',
name: 'client',
message: 'Choose your database:',
choices: [
{
name: 'MongoDB (highly recommended)',
value: {
database: 'mongo',
connector: 'strapi-mongoose'
}
},
{
name: 'Postgres',
value: {
database: 'postgres',
connector: 'strapi-bookshelf'
}
},
{
name: 'MySQL',
value: {
database: 'mysql',
connector: 'strapi-bookshelf'
}
},
{
name: 'Sqlite3',
value: {
database: 'sqlite3',
connector: 'strapi-bookshelf'
}
},
{
name: 'Redis',
value: {
database: 'redis',
connector: 'strapi-bookshelf'
}
}
]
},
{
type: 'input',
prefix: '',
name: 'name',
message: 'Database name:',
default: 'strapi'
},
{
type: 'input',
prefix: '',
name: 'host',
message: 'Host:',
default: 'localhost'
},
{
type: 'input',
prefix: '',
name: 'port',
message: 'Port:',
default: (answers) => {
const ports = {
mongo: 27017,
postgres: 5432,
mysql: 3306,
sqlite3: 1433,
redis: 6379
};
// Trigger callback with no error to proceed.
return cb.success();
return ports[answers.client.database];
}
},
{
type: 'input',
prefix: '',
name: 'username',
message: 'Username:'
},
{
type: 'input',
prefix: '',
name: 'password',
message: 'Password:'
}
])
.then(answers => {
scope.database = {
connector: answers.client.connector,
settings: {
client: answers.client.database,
host: answers.host,
port: answers.port,
database: answers.name,
username: answers.username,
password: answers.password
},
options: {}
};
logger.info('Copying the dashboard...');
// Trigger callback with no error to proceed.
return cb.success();
});
};

View File

@ -9,6 +9,7 @@ const path = require('path');
// Local dependencies.
const packageJSON = require('../json/package.json.js');
const database = require('../json/database.json.js');
/**
* Copy required files for the generated application
@ -29,6 +30,10 @@ module.exports = {
jsonfile: packageJSON
},
'config/environments/development/database.json': {
jsonfile: database
},
// Copy dot files.
'.editorconfig': {
copy: 'editorconfig'

View File

@ -16,6 +16,7 @@
"enpeem": "^2.2.0",
"fs-extra": "^4.0.0",
"get-installed-path": "^3.0.1",
"inquirer": "^4.0.2",
"lodash": "^4.17.4",
"strapi-utils": "3.0.0-alpha.7.3",
"uuid": "^3.1.0"