Skip to content

Commit

Permalink
feat: add database create
Browse files Browse the repository at this point in the history
  • Loading branch information
Siumauricio committed Jun 13, 2024
1 parent 1b97e2c commit 99451d4
Show file tree
Hide file tree
Showing 17 changed files with 965 additions and 30 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"chalk": "^5.3.0",
"cli-table3": "^0.6.5",
"inquirer": "^9.2.23",
"slugify": "^1.6.6",
"superjson": "^2.2.1"
},
"devDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 44 additions & 30 deletions src/commands/app/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default class AppCreate extends Command {
name: project.name,
value: project.projectId,
})),
message: "Select a project to create the application in:",
message: "Select a project to create the database in:",
name: "selectedProject",
type: "list",
},
Expand All @@ -68,16 +68,32 @@ export default class AppCreate extends Command {
}
}

const databases = ["postgres", "mysql", "redis", "mariadb", "mongo"];

const databaseSelect = await inquirer.prompt([
{
choices: databases.map((database: any) => ({
name: database,
value: database,
})),
message: "Select a database to create the application in:",
name: "selectedDatabase",
type: "list",
},
]);

const urlSelected = `${auth.url}/api/trpc/${databaseSelect.selectedDatabase}.create`;

// Solicitar detalles de la nueva aplicación
const appDetails = await inquirer.prompt([
{
message: "Enter the application name:",
message: "Enter the database name:",
name: "appName",
type: "input",
validate: (input) => (input ? true : "Application name is required"),
validate: (input) => (input ? true : "Database name is required"),
},
{
message: "Enter the application description (optional):",
message: "Enter the database description (optional):",
name: "appDescription",
type: "input",
},
Expand All @@ -87,32 +103,30 @@ export default class AppCreate extends Command {

// Crear la aplicación en el proyecto seleccionado
try {
const response = await axios.post(
`${auth.url}/api/trpc/application.create`,
{
json: {
description: appDescription,
name: appName,
projectId,
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);

if (!response.data.result.data.json) {
this.error(chalk.red("Error creating application"));
}

this.log(
chalk.green(
`Application '${appName}' created successfully in project ID '${projectId}'.`,
),
);
// const response = await axios.post(
// `${auth.url}/api/trpc/application.create`,
// {
// json: {
// description: appDescription,
// name: appName,
// projectId,
// },
// },
// {
// headers: {
// Authorization: `Bearer ${auth.token}`,
// "Content-Type": "application/json",
// },
// },
// );
// if (!response.data.result.data.json) {
// this.error(chalk.red("Error creating application"));
// }
// this.log(
// chalk.green(
// `Application '${appName}' created successfully in project ID '${projectId}'.`,
// ),
// );
} catch (error) {
// @ts-expect-error TODO: Fix this
this.error(chalk.red(`Failed to create application: ${error.message}`));
Expand Down
121 changes: 121 additions & 0 deletions src/commands/database/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { Command, Flags } from "@oclif/core";
import axios from "axios";
import chalk from "chalk";
import inquirer from "inquirer";

import { readAuthConfig } from "../../utils/utils.js";

export default class DatabaseCreate extends Command {
static description = "Create a new database within a project.";

static examples = ["$ <%= config.bin %> app create"];

static flags = {
projectId: Flags.string({
char: "p",
description: "ID of the project",
required: false,
}),
};

public async run(): Promise<void> {
const auth = await readAuthConfig(this);

const { flags } = await this.parse(DatabaseCreate);

let { projectId } = flags;

if (!projectId) {
// Obtener la lista de proyectos y permitir la selección
console.log(chalk.blue.bold("\n Listing all Projects \n"));

try {
const response = await axios.get(`${auth.url}/api/trpc/project.all`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
});

if (!response.data.result.data.json) {
this.error(chalk.red("Error fetching projects"));
}

const projects = response.data.result.data.json;

if (projects.length === 0) {
this.log(chalk.yellow("No projects found."));
return;
}

// Permitir al usuario seleccionar un proyecto
const answers = await inquirer.prompt([
{
choices: projects.map((project: any) => ({
name: project.name,
value: project.projectId,
})),
message: "Select a project to create the application in:",
name: "selectedProject",
type: "list",
},
]);

projectId = answers.selectedProject;
} catch (error) {
// @ts-expect-error TODO: Fix this
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
}
}

// Solicitar detalles de la nueva aplicación
const appDetails = await inquirer.prompt([
{
message: "Enter the database name:",
name: "appName",
type: "input",
validate: (input) => (input ? true : "Application name is required"),
},
{
message: "Enter the application description (optional):",
name: "appDescription",
type: "input",
},
]);

const { appDescription, appName } = appDetails;

// Crear la aplicación en el proyecto seleccionado
try {
const response = await axios.post(
`${auth.url}/api/trpc/database.create`,
{
json: {
description: appDescription,
name: appName,
projectId,
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);

if (!response.data.result.data.json) {
this.error(chalk.red("Error creating application"));
}

this.log(
chalk.green(
`Application '${appName}' created successfully in project ID '${projectId}'.`,
),
);
} catch (error) {
// @ts-expect-error TODO: Fix this
this.error(chalk.red(`Failed to create application: ${error.message}`));
}
}
}
144 changes: 144 additions & 0 deletions src/commands/database/mariadb/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { Command, Flags } from "@oclif/core";
import axios from "axios";
import chalk from "chalk";
import inquirer from "inquirer";

import { slugify } from "../../../utils/slug.js";
import { readAuthConfig } from "../../../utils/utils.js";

export default class DatabaseMariadbCreate extends Command {
static description = "Create a new database within a project.";

static examples = ["$ <%= config.bin %> mariadb create"];

static flags = {
projectId: Flags.string({
char: "p",
description: "ID of the project",
required: false,
}),
};

public async run(): Promise<void> {
const auth = await readAuthConfig(this);

const { flags } = await this.parse(DatabaseMariadbCreate);

const { projectId } = flags;
if (!projectId) {
console.log(chalk.blue.bold("\n Listing all Projects \n"));

try {
const response = await axios.get(`${auth.url}/api/trpc/project.all`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
});

if (!response.data.result.data.json) {
this.error(chalk.red("Error fetching projects"));
}

const projects = response.data.result.data.json;

if (projects.length === 0) {
this.log(chalk.yellow("No projects found."));
return;
}

const { project } = await inquirer.prompt([
{
choices: projects.map((project: any) => ({
name: project.name,
value: project,
})),
message: "Select a project to create the database in:",
name: "project",
type: "list",
},
]);

const appDetails = await inquirer.prompt([
{
message: "Enter the name:",
name: "name",
type: "input",
validate: (input) => (input ? true : "Database name is required"),
},
{
message: "Database name:",
name: "databaseName",
type: "input",
validate: (input) => (input ? true : "Database name is required"),
},
{
message: "Enter the database description (optional):",
name: "description",
type: "input",
},
{
message: "Database Root Password (optional):",
name: "databaseRootPassword",
type: "input",
},
{
message: "Database password (optional):",
name: "databasePassword",
type: "input",
},
{
default: "mariadb:4",
message: "Docker Image (default: mariadb:4):",
name: "dockerImage",
type: "input",
},
{
default: "mariadb",
message: "Database User: (default: mariadb):",
name: "databaseUser",
type: "input",
},
]);

const appName = await inquirer.prompt([
{
default: `${slugify(project.name)}-${appDetails.name}`,
message: "Enter the App name: (optional):",
name: "appName",
type: "input",
validate: (input) => (input ? true : "Database name is required"),
},
]);

const responseDatabase = await axios.post(
`${auth.url}/api/trpc/mariadb.create`,
{
json: {
...appDetails,
appName: appName.appName,
projectId: project.projectId,
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);

if (!responseDatabase.data.result.data.json) {
this.error(chalk.red("Error creating database"));
}

this.log(
chalk.green(`Database '${appDetails.name}' created successfully.`),
);
} catch (error) {
// @ts-expect-error TODO: Fix this
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
}
}
}
}
Loading

0 comments on commit 99451d4

Please sign in to comment.