dbtasker 1.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,99 +1,149 @@
1
1
  const fncs = require("./function");
2
2
  const recordedjson = require("./tables");
3
3
  const cstyler = require("cstyler");
4
- const checker = require("./checker");
4
+ const checker = require("./validation");
5
5
 
6
6
 
7
7
 
8
8
 
9
- const moduleName = "databaser";
9
+ const moduleName = "dbtasker";
10
+ const truers = [true, 1, "1", "true", "True", "TRUE"];
10
11
 
11
- async function runit(config, table_json) {
12
+ module.exports = async function(allconfig, table_json) {
12
13
  try {
13
- let errorLog = [];
14
- let availabledbnames = [];
15
- let unavailabledbnames = [];
16
- // lets check database type
17
- const ifmysqldatabase = await fncs.isMySQLDatabase(config);
18
- if (ifmysqldatabase === false) {
19
- console.error("My SQL database is required to run ", moduleName, " module. Please install mysql2 to use this module. To install run this code on the terminal > npm install mysql2");
14
+ console.log(cstyler.blue("Initializing DBTASKER..."))
15
+ // check if enough database available on table json
16
+ const databaseNames = Object.keys(table_json);
17
+ if (databaseNames.length === 0) {
18
+ console.error(cstyler.bold("No data in table information object. Please add some database and tables."));
20
19
  return;
21
20
  }
22
- // Lets check both json file is same or not
23
- if (fncs.isJsonObject(table_json)) {
24
- if (fncs.isJsonSame(table_json, recordedjson)) {
25
- console.log(cstyler.bold.italic.underline.green("All the tables are up to date. No changes needed"));
26
- return;
27
- }
28
- } else {
29
- console.error("Please provice a valid json file to continue.");
21
+ // Check config
22
+ console.log("Lets check config");
23
+ const isvalidconfig = fncs.isValidMySQLConfig(allconfig);
24
+ if (isvalidconfig === false) {
25
+ console.error("Please check your config object. It may requires more information...");
30
26
  return;
31
27
  }
32
- console.log(cstyler.bold.underline.yellow("Table need an upgrade."))
33
- // json file checking is done
34
- const databaseNames = Object.keys(table_json);
35
- if (Array.isArray(databaseNames)) {
36
- for (let dbName of databaseNames) {
37
- dbName = dbName.toLocaleLowerCase();
38
- if (fncs.isValidDatabaseName(dbName)) {
39
- const ifexist = await fncs.checkDatabaseExists(config, dbName);
40
- if (ifexist === false) {
41
- const createDB = await fncs.createDatabase(config, dbName);
42
- if (createDB === true) {
43
- console.log(`${cstyler.purpal('Database name:')} ${cstyler.blue(dbName)} ${cstyler.green('has created successfully.')}`)
44
- } else if (createDB === false) {
45
- console.log(`${cstyler.purpal('Database name:')} ${cstyler.blue(dbName)} ${cstyler.yellow("is already created.")}`)
46
- } else {
47
- throw new Error("Can not perform. There is a database connection problem.");
28
+ const config = {
29
+ 'host': allconfig.host,
30
+ 'user': allconfig.user,
31
+ 'password': allconfig.password,
32
+ 'port': allconfig.port,
33
+ 'waitForConnections': true,
34
+ 'connectionLimit': 100
35
+ }
36
+ // get don't touch database
37
+ let donttouchdb = [];
38
+ const donttouchkeys = ['donttouch', 'donottouch', 'donttouchdb', 'donottouchdb', 'donttouchdatabase', 'donottouchdatabase', 'dontdelete', 'donotdelete', 'dontdeletedb', 'donotdeletedb', 'dontdeletedatabase', 'donotdeletedatabase', 'dont_touch', 'do_not_touch', 'dont_touch_db', 'do_not_touch_db', 'dont_touch_database', 'do_not_touch_database', 'dont_delete', 'do_not_delete', 'dont_delete_db', 'do_not_delete_db', 'dont_delete_database', 'do_not_delete_database', 'reserveddb', 'reserved_db'];
39
+ for (const item of Object.keys(allconfig)) {
40
+ if (donttouchkeys.includes(item.toLowerCase())) {
41
+ if (Array.isArray(allconfig[item])) {
42
+ for (const dbs of allconfig[item]) {
43
+ if (typeof dbs !== "string") {
44
+ console.error("Non deletable database names must be string. Please provide valid data type.");
45
+ return;
48
46
  }
49
- } else if (ifexist === true) {
50
- console.log(`${cstyler.purpal('Database name:')} ${cstyler.blue(dbName)} ${cstyler.green('is available')}`);
51
- } else {
52
- throw new Error("Can not perform. There is a database connection problem.");
53
47
  }
48
+ donttouchdb = allconfig[item];
54
49
  } else {
55
- errorLog.push(`${cstyler.purpal('Database name:')} "${cstyler.yellow(dbName)}" ${cstyler.red("is not valid")}`);
50
+ console.error(cstyler.bold("Please provide database name as an array that can not be deleted."));
51
+ return;
56
52
  }
57
53
  }
58
- // Lets check how many database added
59
- const getalldbnames = await fncs.getAllDatabaseNames(config);
60
- if (getalldbnames === null) {
61
- throw new Error("Can not perform. There is a database connection problem.");
54
+ }
55
+ // Declare seperator
56
+ let seperator = "_";
57
+ const sepkeys = ['sep', 'Sep', 'Seperator', 'seperator'];
58
+ for (const item of sepkeys) {
59
+ if (allconfig.hasOwnProperty(item)) {
60
+ seperator = allconfig[item];
61
+ break;
62
62
  }
63
- if (Array.isArray(getalldbnames)) {
64
- for (const items of databaseNames) {
65
- if (!getalldbnames.includes(items.toLocaleLowerCase())) {
66
- unavailabledbnames.push(items.toLocaleLowerCase());
67
- console.error(`${cstyler.purpal('Database name:')} ${items} is not available. Please try again.`);
68
- } else {
69
- availabledbnames.push(items.toLocaleLowerCase());
70
- }
71
- }
63
+ }
64
+ if (!fncs.isValidMySQLIdentifier(seperator)) {
65
+ seperator = "_";
66
+ }
67
+ let dropdatabase;
68
+ const dropdbkeys = ['dropdb', 'dropdatabase', 'deletedb', 'deletedatabase', 'drop_db', 'drop_database', 'delete_db', 'delete_database', 'removedb', 'removedatabase', 'remove_db', 'remove_database'];
69
+ for (const item of Object.keys(allconfig)) {
70
+ if (dropdbkeys.includes(item.toLowerCase())) {
71
+ dropdatabase = allconfig[item];
72
72
  }
73
- if (availabledbnames.length === 0) {
74
- console.error("There is no available database to work on tables.");
73
+ }
74
+ if (truers.includes(dropdatabase)) {
75
+ dropdatabase = true;
76
+ } else {
77
+ dropdatabase = false;
78
+ }
79
+ let droptable;
80
+ const droptablekeys = ['droptable', 'deletetable', 'drop_table', 'delete_table', 'removetable', 'remove_table', 'dropdbtable', 'deletedbtable', 'removedbtable', 'dropdatabasetable', 'deletedatabasetable', 'removedatabasetable', 'drop_db_table', 'delete_db_table', 'remove_db_table', 'drop_database_table', 'delete_database_table', 'remove_database_table',];
81
+ for (const item of Object.keys(allconfig)) {
82
+ if (droptablekeys.includes(item.toLowerCase())) {
83
+ droptable = allconfig[item];
75
84
  }
76
- // lets check all table name and column name
77
- const checkeing = checker.JSONchecker(table_json);
78
- if (checkeing === false) {
79
- console.log(cstyler.bold.underline.red("Please correct those information and try again."))
85
+ }
86
+ if (truers.includes(droptable)) {
87
+ droptable = true;
88
+ } else {
89
+ droptable = false;
90
+ }
91
+ let dropcolumn;
92
+ const dropcolkeys = ['dropcol', 'dropcolumn', 'deletecol', 'deletecolumn', 'removecol', 'removecolumn', 'drop_col', 'drop_column', 'delete_col', 'delete_column', 'remove_col', 'remove_column',];
93
+ for (const item of Object.keys(allconfig)) {
94
+ if (dropcolkeys.includes(item.toLowerCase())) {
95
+ dropcolumn = allconfig[item];
96
+ }
97
+ }
98
+ if (truers.includes(dropcolumn)) {
99
+ dropcolumn = true;
100
+ } else {
101
+ dropcolumn = false;
102
+ }
103
+ // lets check database type
104
+ const ifmysqldatabase = await fncs.isMySQLDatabase(config);
105
+ if (ifmysqldatabase === false) {
106
+ console.error("My SQL database is required to run ", moduleName, " module. Please install mysql2 to use this module. To install run this code on the terminal > npm install mysql2");
107
+ return;
108
+ }
109
+ const isvalidmysqlversion = await fncs.isMySQL578OrAbove(config);
110
+ if (isvalidmysqlversion === false) {
111
+ console.error("My SQL version 5.7.8 or above is required. Please check if you have installed mysql2. To install: npm install mysql2");
112
+ return;
113
+ }
114
+ console.log(cstyler.bold.underline.yellow("Lets check if the table need an upgrade"))
115
+ // lets check all table name and column name
116
+ const checking = await checker.JSONchecker(table_json, config, seperator);
117
+ if (checking.status === false) {
118
+ console.log(cstyler.bold.underline.red("Please correct those information and try again."))
119
+ return;
120
+ }
121
+ const jsondata = checking.data;
122
+ console.log(cstyler.bold.purple("Lets start operation on databases."));
123
+ const dbop = require("./dbop");
124
+ const databaseop = await dbop.databaseAddDeleteAlter(config, jsondata, dropdatabase, donttouchdb, seperator);
125
+ if (databaseop === null) {
126
+ return;
127
+ }
128
+ // Drop tables
129
+ if (droptable) {
130
+ console.log(cstyler.bold.purple("Lets drop unlisted table if needed."));
131
+ const droptableifneeded = await dbop.dropTable(config, jsondata, seperator);
132
+ if (droptableifneeded === null) {
80
133
  return;
81
134
  }
82
- // table json file checking done
83
- //
84
135
  }
136
+
137
+ console.log(cstyler.bold.purple("Lets start working on columns"));
138
+ const colop = require("./columnop");
139
+ const columnop = await colop.columnAddDeleteAlter(config, jsondata, dropcolumn, seperator);
140
+ if (columnop === null) {
141
+ return;
142
+ }
143
+ console.log(cstyler.bold.underline.green("<<<All database work is done perfectly>>>"));
85
144
  } catch (err) {
86
145
  console.error(err.message);
87
146
  return;
88
147
  }
89
148
  }
90
149
 
91
-
92
-
93
-
94
-
95
-
96
-
97
- const { DBInfo } = require("./app");
98
- const tables = require("./user_tables");
99
- runit(DBInfo, tables);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dbtasker",
3
- "version": "1.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "You can create database and table on your own with a lot of functionality.",
5
5
  "license": "ISC",
6
6
  "author": "Md Nasiruddin Ahmed",
@@ -14,13 +14,23 @@
14
14
  "mysql2",
15
15
  "alter",
16
16
  "modify",
17
- ""
17
+ "mysql2"
18
18
  ],
19
19
  "scripts": {
20
20
  "test": "echo \"Error: no test specified\" && exit 1"
21
21
  },
22
22
  "dependencies": {
23
- "cstyler": "^1.0.0",
24
- "mysql2": "^3.14.2"
25
- }
23
+ "cstyler": "^3.0.0",
24
+ "fs": "^0.0.1-security",
25
+ "mysql2": "^3.14.3",
26
+ "path": "^0.12.7"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/kormoi/dbtasker"
31
+ },
32
+ "bugs": {
33
+ "url": "https://github.com/kormoi/dbtasker/issues"
34
+ },
35
+ "homepage": "https://github.com/kormoi/dbtasker#readme"
26
36
  }
package/tables.js CHANGED
@@ -1 +0,0 @@
1
- module.exports = {}