From b6e43dd504129cd682677bed6346b104bab7881b Mon Sep 17 00:00:00 2001 From: HerrHase Date: Thu, 19 Oct 2023 10:16:06 +0200 Subject: [PATCH] adding --- mariadb-create.js | 15 +++----------- mariadb-drop.js | 52 ++++++++++++++++------------------------------- 2 files changed, 21 insertions(+), 46 deletions(-) diff --git a/mariadb-create.js b/mariadb-create.js index 026b23c..89f778e 100644 --- a/mariadb-create.js +++ b/mariadb-create.js @@ -15,23 +15,14 @@ const log = console.log */ const user = { - name: undefined, + name: 'root', password: undefined } -user.name = await input({ - message: 'Root:', - async validate(value) { - if (!value) { - return 'Required!' - } - - return true - } -}) +log(chalk.red('Mariadb Create Database...')) user.password = await password({ - message: 'Password:', + message: 'Enter Root Password:', mask: '*', async validate(value) { if (!value) { diff --git a/mariadb-drop.js b/mariadb-drop.js index 0d25a89..c1edb15 100644 --- a/mariadb-drop.js +++ b/mariadb-drop.js @@ -1,5 +1,8 @@ import mysql from 'mysql2/promise' import { input, password } from '@inquirer/prompts' +import chalk from 'chalk' + +const log = console.log /** * mariadb-drop.js @@ -11,12 +14,15 @@ import { input, password } from '@inquirer/prompts' */ const user = { - name: undefined, + name: 'root', password: undefined } -user.name = await input({ - message: 'Root:', +log(chalk.cyan('Mariadb drop Database...')) + +user.password = await password({ + message: 'Enter Root Password:', + mask: '*', async validate(value) { if (!value) { return 'Required!' @@ -26,9 +32,8 @@ user.name = await input({ } }) -user.password = await password({ - message: 'Password:', - mask: '*', +const database = await input({ + message: 'Database:', async validate(value) { if (!value) { return 'Required!' @@ -46,38 +51,17 @@ const connection = await mysql.createConnection({ password: user.password }) -const database = { - name: 'db', - user: 'u' -} - -database.name += generator.generate({ - length: 15, - numbers: true -}) +// create database -database.user += generator.generate({ - length: 15, - numbers: true -}) +const [results ] = await connection.query("SELECT User FROM mysql.db WHERE Db = '" + database + "'") -database.password = generator.generate({ - length: 30, - numbers: true +results.forEach(async (entity) => { + await connection.query("DROP USER IF EXISTS '" + entity.User + "'@localhost"); + await connection.query("DROP USER IS EXISTS '" + entity.User + "'@'%'"); }) -// create database - -await connection.query('CREATE DATABASE ' + database.name + ' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci') -await connection.query("CREATE USER " + database.user + "@'localhost' IDENTIFIED BY '" + database.password + "'") -await connection.query("CREATE USER " + database.user + "@'%' IDENTIFIED BY '" + database.password + "'") -await connection.query("GRANT ALL PRIVILEGES ON " + database.name + ".* TO " + database.user + "@localhost") -await connection.query("FLUSH PRIVILEGES") +await connection.query("DROP DATABASE IF EXISTS " + database); connection.destroy() -// show name, user and passwort - -console.log(database.name) -console.log(database.user) -console.log(database.password) +log(chalk.green('Database ' + database + ' droped!'))