74 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-04-26 09:07:40 +02:00
import type { Question } from 'inquirer';
import type { Scope } from '../types';
interface QuestionFactory {
(options: { scope: Scope; client: 'postgres' | 'mysql' | 'sqlite' }): Question;
}
2021-06-29 16:27:35 +02:00
const DEFAULT_PORTS = {
postgres: 5432,
mysql: 3306,
2023-04-26 09:07:40 +02:00
sqlite: undefined,
2021-06-29 16:27:35 +02:00
};
2023-04-26 09:07:40 +02:00
const database: QuestionFactory = ({ scope }) => ({
type: 'input',
name: 'database',
message: 'Database name:',
default: scope.name,
2023-04-26 09:07:40 +02:00
validate(value: string) {
if (value.includes('.')) {
2020-01-07 14:23:15 +01:00
return `The database name can't contain a "."`;
}
return true;
},
});
2023-04-26 09:07:40 +02:00
const host: QuestionFactory = () => ({
type: 'input',
name: 'host',
message: 'Host:',
default: '127.0.0.1',
});
2023-04-26 09:07:40 +02:00
const port: QuestionFactory = ({ client }) => ({
type: 'input',
name: 'port',
2023-04-26 09:07:40 +02:00
message: 'Port:',
2021-06-29 16:27:35 +02:00
default: DEFAULT_PORTS[client],
});
2023-04-26 09:07:40 +02:00
const username: QuestionFactory = () => ({
type: 'input',
name: 'username',
message: 'Username:',
});
2023-04-26 09:07:40 +02:00
const password: QuestionFactory = () => ({
type: 'password',
name: 'password',
message: 'Password:',
mask: '*',
});
2023-04-26 09:07:40 +02:00
const ssl: QuestionFactory = () => ({
type: 'confirm',
name: 'ssl',
message: 'Enable SSL connection:',
default: false,
});
2023-04-26 09:07:40 +02:00
const filename: QuestionFactory = () => ({
type: 'input',
name: 'filename',
message: 'Filename:',
default: '.tmp/data.db',
});
2023-04-26 09:07:40 +02:00
export default {
sqlite: [filename],
postgres: [database, host, port, username, password, ssl],
mysql: [database, host, port, username, password, ssl],
};