diff --git a/mariadb-create.js b/mariadb-create.js new file mode 100644 index 0000000..026b23c --- /dev/null +++ b/mariadb-create.js @@ -0,0 +1,90 @@ +import mysql from 'mysql2/promise' +import { input, password } from '@inquirer/prompts' +import generator from 'generate-password' +import chalk from 'chalk' + +const log = console.log + +/** + * mariadb-create.js + * + * create database and generate name, user, password + * and grant this user single priveleges + * + * + */ + +const user = { + name: undefined, + password: undefined +} + +user.name = await input({ + message: 'Root:', + async validate(value) { + if (!value) { + return 'Required!' + } + + return true + } +}) + +user.password = await password({ + message: 'Password:', + mask: '*', + async validate(value) { + if (!value) { + return 'Required!' + } + + return true + } +}) + +// create connection, generate name for db and user, and generate password + +const connection = await mysql.createConnection({ + host: 'localhost', + user: user.name, + password: user.password +}) + +const database = { + name: 'db', + user: 'u' +} + +database.name += generator.generate({ + length: 15, + numbers: true +}) + +database.user += generator.generate({ + length: 15, + numbers: true +}) + +database.password = generator.generate({ + length: 30, + numbers: true +}) + +// 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") + +connection.destroy() + +// show name, user and passwort + +log(' ') +log(chalk.green('Database created!')) +log(' ') +log(chalk.green('name:') + ' ' + chalk.cyan(database.name)) +log(chalk.green('user:') + ' ' + chalk.cyan(database.user)) +log(chalk.green('password:') + ' ' + chalk.cyan(database.password)) diff --git a/mariadb-add.js b/mariadb-drop.js similarity index 92% rename from mariadb-add.js rename to mariadb-drop.js index 8a67458..0d25a89 100644 --- a/mariadb-add.js +++ b/mariadb-drop.js @@ -1,6 +1,14 @@ import mysql from 'mysql2/promise' import { input, password } from '@inquirer/prompts' -import generator from 'generate-password' + +/** + * mariadb-drop.js + * + * create database and generate name, user, password + * and grant this user single priveleges + * + * + */ const user = { name: undefined, diff --git a/package-lock.json b/package-lock.json index bf54d3c..922e103 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,6 +6,7 @@ "": { "dependencies": { "@inquirer/prompts": "^3.2.0", + "chalk": "^5.3.0", "generate-password": "^1.7.1", "mysql2": "^3.6.2" } @@ -25,6 +26,21 @@ "node": ">=14.18.0" } }, + "node_modules/@inquirer/checkbox/node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, "node_modules/@inquirer/confirm": { "version": "2.0.14", "resolved": "https://registry.npmjs.org/@inquirer/confirm/-/confirm-2.0.14.tgz", @@ -38,6 +54,21 @@ "node": ">=14.18.0" } }, + "node_modules/@inquirer/confirm/node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, "node_modules/@inquirer/core": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/@inquirer/core/-/core-5.1.0.tgz", @@ -62,6 +93,21 @@ "node": ">=14.18.0" } }, + "node_modules/@inquirer/core/node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, "node_modules/@inquirer/editor": { "version": "1.2.12", "resolved": "https://registry.npmjs.org/@inquirer/editor/-/editor-1.2.12.tgz", @@ -76,6 +122,21 @@ "node": ">=14.18.0" } }, + "node_modules/@inquirer/editor/node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, "node_modules/@inquirer/expand": { "version": "1.1.13", "resolved": "https://registry.npmjs.org/@inquirer/expand/-/expand-1.1.13.tgz", @@ -90,6 +151,21 @@ "node": ">=14.18.0" } }, + "node_modules/@inquirer/expand/node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, "node_modules/@inquirer/input": { "version": "1.2.13", "resolved": "https://registry.npmjs.org/@inquirer/input/-/input-1.2.13.tgz", @@ -103,6 +179,21 @@ "node": ">=14.18.0" } }, + "node_modules/@inquirer/input/node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, "node_modules/@inquirer/password": { "version": "1.1.13", "resolved": "https://registry.npmjs.org/@inquirer/password/-/password-1.1.13.tgz", @@ -117,6 +208,21 @@ "node": ">=14.18.0" } }, + "node_modules/@inquirer/password/node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, "node_modules/@inquirer/prompts": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/@inquirer/prompts/-/prompts-3.2.0.tgz", @@ -149,6 +255,21 @@ "node": ">=14.18.0" } }, + "node_modules/@inquirer/rawlist/node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, "node_modules/@inquirer/select": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/@inquirer/select/-/select-1.3.0.tgz", @@ -164,6 +285,21 @@ "node": ">=14.18.0" } }, + "node_modules/@inquirer/select/node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, "node_modules/@inquirer/type": { "version": "1.1.5", "resolved": "https://registry.npmjs.org/@inquirer/type/-/type-1.1.5.tgz", @@ -230,15 +366,11 @@ } }, "node_modules/chalk": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", - "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", - "dependencies": { - "ansi-styles": "^4.1.0", - "supports-color": "^7.1.0" - }, + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.3.0.tgz", + "integrity": "sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==", "engines": { - "node": ">=10" + "node": "^12.17.0 || ^14.13 || >=16.0.0" }, "funding": { "url": "https://github.com/chalk/chalk?sponsor=1" diff --git a/package.json b/package.json index 6a29f2d..3f44f7a 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "type": "module", "dependencies": { "@inquirer/prompts": "^3.2.0", + "chalk": "^5.3.0", "generate-password": "^1.7.1", "mysql2": "^3.6.2" }