Add seeds and db install

This commit is contained in:
rijkvanzanten
2020-07-17 17:08:22 -04:00
parent e065950440
commit 9bcff8ea88
10 changed files with 575 additions and 48 deletions

View File

@@ -6,8 +6,8 @@ export const drivers = {
mssql: 'Microsoft SQL Server',
};
export function getDriverForClient(client: string) {
export function getDriverForClient(client: string): keyof typeof drivers {
for (const [key, value] of Object.entries(drivers)) {
if (value === client) return key;
if (value === client) return key as keyof typeof drivers;
}
}

View File

@@ -5,6 +5,8 @@ import { resolve } from 'path';
import { databaseQuestions } from './questions';
import { drivers, getDriverForClient } from './drivers';
import installDB, { Credentials } from './install-db';
export default async function create(directory: string, options: Record<string, any>) {
const path = resolve(directory);
checkRequirements();
@@ -42,10 +44,17 @@ export default async function create(directory: string, options: Record<string,
client = getDriverForClient(client);
const responses = await inquirer.prompt(
databaseQuestions[client].map((question) => question({ client }))
const credentials: Credentials = await inquirer.prompt(
databaseQuestions[client].map((question: Function) => question({ client }))
);
try {
await installDB(client, credentials);
} catch (error) {
console.log(`${chalk.red('Database Error')}: Couln't install the database:`);
console.log(error.message);
}
/** @todo
* - See if you can connect to DB
* - Install Directus system stuff into DB

View File

@@ -0,0 +1,53 @@
import knex, { Config } from 'knex';
export type Credentials = {
filename?: string;
host?: string;
port?: number;
database?: string;
username?: string;
password?: string;
};
export default async function installDB(
client: 'sqlite3' | 'mysql' | 'pg' | 'oracledb' | 'mssql',
credentials: Credentials
): Promise<void> {
let connection: Config['connection'] = {};
if (client === 'sqlite3') {
const { filename } = credentials;
connection = {
filename: filename,
};
} else {
const { host, port, database, username, password } = credentials as Credentials;
connection = {
host: host,
port: port,
database: database,
user: username,
password: password,
};
}
const db = knex({
client: client,
connection: connection,
seeds:
process.env.NODE_ENV === 'development'
? {
extension: 'ts',
directory: './src/database/seeds/',
}
: {
extension: 'js',
directory: './dist/database/seeds/',
},
});
await db.seed.run();
db.destroy();
}

View File

@@ -28,6 +28,13 @@ const port = ({ client }) => ({
},
});
const database = () => ({
type: 'input',
name: 'database',
message: 'Database Name:',
default: 'directus',
});
const username = () => ({
type: 'input',
name: 'username',
@@ -43,8 +50,8 @@ const password = () => ({
export const databaseQuestions = {
sqlite3: [filename],
mysql: [host, port, username, password],
pg: [host, port, username, password],
oracledb: [host, port, username, password],
mssql: [host, port, username, password],
mysql: [host, port, database, username, password],
pg: [host, port, database, username, password],
oracledb: [host, port, database, username, password],
mssql: [host, port, database, username, password],
};