dbtasker 1.0.0 → 2.5.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/check.js DELETED
@@ -1,18 +0,0 @@
1
- const fncs = require("./function");
2
-
3
- const DBInfo = {
4
- port: 3301,
5
- host: "localhost",
6
- user: "root",
7
- password: "1234",
8
- database: "test",
9
- waitForConnections: true,
10
- connectionLimit: 100,
11
- queueLimit: 0,
12
- };
13
-
14
- async function check() {
15
- const tableNames = await fncs.getForeignKeyDetails(DBInfo, "main", "deal_post");
16
- console.log(tableNames);
17
- }
18
- check();
package/checker.js DELETED
@@ -1,359 +0,0 @@
1
- const cstyler = require("cstyler");
2
- const fncs = require("./function");
3
-
4
-
5
- function parseQuotedListSafely(text) {
6
- if (typeof text !== 'string') return [];
7
- if (Array.isArray(text)) return text;
8
-
9
- // Check: does it look like a quoted comma-separated list?
10
- const isListLike = /^('.*?')(,\s*'.*?')*$/.test(text.trim());
11
-
12
- if (!isListLike) {
13
- return [];
14
- }
15
-
16
- return text
17
- .split(",")
18
- .map(item => item.trim().replace(/^'(.*)'$/, "$1"))
19
- .filter(item => item.length > 0);
20
- }
21
- function JSONchecker(table_json) {
22
- // lets check all table name and column name
23
- let badTableNames = [];
24
- let badColumnNames = [];
25
- let badautoincrement = [];
26
- let badindex = [];
27
- let badnulls = [];
28
- let baddefaults = [];
29
- let badlength = [];
30
- let badforeighkey = [];
31
-
32
- if (fncs.isJsonObject(table_json)) {
33
- for (const databaseName of Object.keys(table_json)) {
34
- if (fncs.isJsonObject(table_json[databaseName])) {
35
- for (const tableName of Object.keys(table_json[databaseName])) {
36
- if (!fncs.parseColumnWithOptionalLoopStrict(tableName.toLocaleLowerCase())) {
37
- badTableNames.push(
38
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ` +
39
- `${cstyler.purpal('of table:')} ${cstyler.blue(tableName)} ` +
40
- `${cstyler.red('- table name is not valid.')}`
41
- );
42
-
43
- }
44
- if (fncs.isJsonObject(table_json[databaseName][tableName])) {
45
- for (const columnName of Object.keys(table_json[databaseName][tableName])) {
46
- const deepColumn = table_json[databaseName][tableName][columnName];
47
- // lets check column names
48
- if (!fncs.isValidColumnName(columnName.toLocaleLowerCase())) {
49
- badColumnNames.push(
50
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ` +
51
- `${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ` +
52
- `${cstyler.purpal('> Column:')} ${cstyler.blue(columnName)} ` +
53
- `${cstyler.red('- column name is not valid.')}`
54
- );
55
-
56
- }
57
- // Let's check properties
58
- if (!deepColumn.hasOwnProperty("type")) {
59
- badColumnNames.push(
60
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ` +
61
- `${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ` +
62
- `${cstyler.purpal('> Column:')} ${cstyler.blue(columnName)} ` +
63
- `${cstyler.red('- must have type.')}`
64
- );
65
- continue;
66
- } else {
67
- if (!deepColumn.type.hasOwnProperty("name")) {
68
- badColumnNames.push(
69
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ` +
70
- `${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ` +
71
- `${cstyler.purpal('> Column:')} ${cstyler.blue(columnName)} ` +
72
- `${cstyler.red('> type - must have name.')}`
73
- );
74
- continue;
75
- }
76
- }
77
- // check auto increment
78
- const autoIncrementIntegerTypesWithUnsigned = [
79
- "TINYINT",
80
- "SMALLINT",
81
- "MEDIUMINT",
82
- "INT",
83
- "INTEGER",
84
- "BIGINT",
85
- "TINYINT UNSIGNED",
86
- "SMALLINT UNSIGNED",
87
- "MEDIUMINT UNSIGNED",
88
- "INT UNSIGNED",
89
- "INTEGER UNSIGNED",
90
- "BIGINT UNSIGNED"
91
- ];
92
- if (deepColumn.hasOwnProperty("AUTO_INCREMENT")) {
93
- if (
94
- !autoIncrementIntegerTypesWithUnsigned.includes(deepColumn.type?.name) ||
95
- !['PRIMARY KEY', 'UNIQUE', true].includes(deepColumn.index) ||
96
- deepColumn.NULL === true ||
97
- deepColumn.hasOwnProperty("DEFAULT")
98
- ) {
99
- badautoincrement.push(
100
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ` +
101
- `${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ` +
102
- `${cstyler.purpal('> Column:')} ${cstyler.blue(columnName)} ` +
103
- `${cstyler.red('column must be')} ${cstyler.yellow('integer')} ${cstyler.red('type, should be')} ${cstyler.yellow('primary key')} ${cstyler.red('or')} ${cstyler.yellow('unique indexed')}, ` +
104
- `${cstyler.red('should be')} ${cstyler.yellow('NOT NULL')}, ` +
105
- `${cstyler.red('can not have a')} ${cstyler.yellow('DEFAULT')} ${cstyler.red('value.')}`
106
- );
107
- }
108
- let autoincrementcolumnlist = [];
109
- for (const column of Object.keys(table_json[databaseName][tableName])) {
110
- if (table_json[databaseName][tableName][column].hasOwnProperty("AUTO_INCREMENT")) {
111
- if (table_json[databaseName][tableName][column].AUTO_INCREMENT === true) {
112
- autoincrementcolumnlist.push(column);
113
- }
114
- }
115
- }
116
- if (autoincrementcolumnlist.length > 1) {
117
- badautoincrement.push(
118
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ` +
119
- `${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ` +
120
- `${cstyler.purpal('> Column:')} ${cstyler.blue(columnName)} ` +
121
- `${cstyler.red('- This table has another')} ${cstyler.yellow('auto_increment')} ${cstyler.red('column. A table can have only one')} ${cstyler.yellow('auto_increment')} ${cstyler.red('column.')}`
122
- );
123
-
124
- }
125
- }
126
- // check index
127
- if (deepColumn.hasOwnProperty("index")) {
128
- if (deepColumn.type?.name === "JSON") {
129
- badindex.push(
130
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ` +
131
- `${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ` +
132
- `${cstyler.purpal('> Column:')} ${cstyler.blue(columnName)} ` +
133
- `${cstyler.red('- is a JSON column which can not have an ')}${cstyler.yellow('index')} ${cstyler.red('property')}`
134
- );
135
-
136
- }
137
- const validIndexValues = [
138
- true, // normal index
139
- "INDEX",
140
- "UNIQUE",
141
- "PRIMARY",
142
- "PRIMARY KEY",
143
- "FULLTEXT",
144
- "SPATIAL"
145
- ];
146
- const indexValue = typeof deepColumn.index === "string" ? deepColumn.index.toUpperCase() : deepColumn.index;
147
- if (!validIndexValues.includes(indexValue)) {
148
- badindex.push(
149
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ` +
150
- `${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ` +
151
- `${cstyler.purpal('> Column:')} ${cstyler.blue(columnName)} ` +
152
- `${cstyler.red('- has unsupported index value.')} ` +
153
- `${cstyler.red('Value must be')} ${cstyler.yellow(validIndexValues.join(', '))}`
154
- );
155
-
156
- }
157
- }
158
- // check ENUM
159
- if (deepColumn.type?.name === "ENUM") {
160
- const enumoptions = parseQuotedListSafely(deepColumn.type?.LengthValues);
161
- if (enumoptions.length < 1) {
162
- badlength.push(
163
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ` +
164
- `${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ` +
165
- `${cstyler.purpal('> Column:')} ${cstyler.blue(columnName)} ` +
166
- `${cstyler.yellow('LengthValues')} ${cstyler.red('for')} ${cstyler.yellow('ENUM')} ` +
167
- `${cstyler.red('must be a non-empty list with single-quoted items or an array:')} ` +
168
- `${cstyler.yellow(`"'red', 'blue',..."`)} ${cstyler.red('or')} ${cstyler.yellow("['red', 'blue',...]")}`
169
- );
170
-
171
- }
172
- }
173
- // check SET
174
- if (deepColumn.type?.name === "SET") {
175
- const setOptions = parseQuotedListSafely(deepColumn.type.LengthValues);
176
- if (setOptions.length < 1) {
177
- badlength.push(
178
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ` +
179
- `${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ` +
180
- `${cstyler.purpal('> Column:')} ${cstyler.blue(columnName)} ` +
181
- `${cstyler.yellow('LengthValues')} ${cstyler.red('for')} ${cstyler.yellow('SET')} ` +
182
- `${cstyler.red('must be a non-empty list with single-quoted items or an array:')} ` +
183
- `${cstyler.yellow(`"'red', 'blue',..."`)} ${cstyler.red('or')} ${cstyler.yellow("['red', 'blue',...]")}`
184
- );
185
-
186
- }
187
- }
188
- // check VARCHAR or CHAR
189
- if (["VARCHAR", "CHAR"].includes(deepColumn.type?.name)) {
190
- if (!Number.isInteger(deepColumn.type.LengthValues) || deepColumn.type.LengthValues < 1) {
191
- badlength.push(
192
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ${cstyler.purpal('> Column:')} ${cstyler.blue(columnName)}${cstyler.red("'s must define a positive integer Length for type")} ${cstyler.yellow(deepColumn.type?.name)}`
193
- );
194
-
195
- }
196
- }
197
- // check DECIMAL
198
- if (deepColumn.type?.name === "DECIMAL") {
199
- const [precision, scale] = deepColumn.type.LengthValues || [];
200
- if (!Number.isInteger(precision) || precision <= 0 || precision > 65) {
201
- badlength.push(
202
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ${cstyler.purpal('> Column:')} ${cstyler.blue(columnName)}${cstyler.red(" has invalid")} ${cstyler.yellow('DECIMAL')} ${cstyler.red('precision')}`
203
- );
204
-
205
- }
206
- if (!Number.isInteger(scale) || scale < 0 || scale > precision) {
207
- badlength.push(
208
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} > ${cstyler.purpal('Table:')} ${cstyler.blue(tableName)} > ${cstyler.purpal('Column:')} ${cstyler.blue(columnName)} ${cstyler.red('has invalid')} ${cstyler.yellow('DECIMAL')} ${cstyler.red('scale')}`
209
- );
210
-
211
-
212
- }
213
- }
214
- // lets check bad null
215
- if (deepColumn.hasOwnProperty("NULL")) {
216
- if (deepColumn.NULL !== true && deepColumn.NULL !== false) {
217
- badnulls.push(
218
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} > ${cstyler.purpal('Table:')} ${cstyler.blue(tableName)} > ${cstyler.purpal('Column:')} ${cstyler.blue(columnName)} ${cstyler.yellow('NULL')} ${cstyler.red('- must be true or false')}`
219
- );
220
-
221
- }
222
- }
223
- // check DEFAULT
224
- if (deepColumn.hasOwnProperty("DEFAULT")) {
225
- if (deepColumn.type?.name === "JSON" && deepColumn.DEFAULT !== null) {
226
- baddefaults.push(
227
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} > ${cstyler.purpal('Table:')} ${cstyler.blue(tableName)} > ${cstyler.purpal('Column:')} ${cstyler.blue(columnName)} ${cstyler.red('column can not have')} ${cstyler.yellow('DEFAULT')} ${cstyler.red('or must be null')}`
228
- );
229
-
230
- } else if (typeof deepColumn.DEFAULT !== "string" && typeof deepColumn.DEFAULT !== "number") {
231
- baddefaults.push(
232
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} > ` +
233
- `${cstyler.purpal('Table:')} ${cstyler.blue(tableName)} > ` +
234
- `${cstyler.purpal('Column:')} ${cstyler.blue(columnName)}'s ` +
235
- `${cstyler.yellow('DEFAULT')} ${cstyler.red('must be string')}`
236
- );
237
-
238
- }
239
- }
240
- // lets check bad foreign_key
241
- if (deepColumn.hasOwnProperty("foreign_key")) {
242
- const validDeleteOptions = new Set([null, true, 'set null', 'cascade', 'restrict', 'no action', 'set default']);
243
-
244
- if (!validDeleteOptions.has(deepColumn.foreign_key.delete)) {
245
- badforeighkey.push(
246
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} > ` +
247
- `${cstyler.purpal('Table:')} ${cstyler.blue(tableName)} > ` +
248
- `${cstyler.purpal('Column:')} ${cstyler.blue(columnName)} ` +
249
- `${cstyler.blue('foreign_key > delete')} ` +
250
- `${cstyler.red('must be one of:')} ` +
251
- `${cstyler.yellow('null')}, ${cstyler.blue('true')}, ${cstyler.yellow("'restrict'")}, ` +
252
- `${cstyler.yellow("'no action'")}, ${cstyler.yellow("'set default'")}`
253
- );
254
-
255
- } else {
256
- const deleteOption = deepColumn.foreign_key.delete;
257
-
258
- // If DELETE is null (SET NULL), column must allow NULLs
259
- if (deleteOption === null && deepColumn.NULL !== true) {
260
- badforeighkey.push(
261
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} > ` +
262
- `${cstyler.purpal('Table:')} ${cstyler.blue(tableName)} > ` +
263
- `${cstyler.purpal('Column:')} ${cstyler.blue(columnName)} ` +
264
- `${cstyler.blue('foreign_key >')} ` +
265
- `${cstyler.yellow('delete')} === ${cstyler.yellow('null')} - then column ${cstyler.red('NULL must be true')}`
266
- );
267
- }
268
-
269
- // If DELETE is 'set default', column must have a DEFAULT value
270
- else if (deleteOption === 'set default' && deepColumn.DEFAULT === undefined) {
271
- badforeighkey.push(
272
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ` +
273
- `${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ` +
274
- `${cstyler.purpal('> Column:')} ${cstyler.blue(columnName)} ` +
275
- `${cstyler.blue('foreign_key >')} ${cstyler.red("delete === 'set default'")} - then column ${cstyler.red('DEFAULT must be defined')}`
276
- );
277
-
278
- }
279
- }
280
- // check reference
281
- if (deepColumn.foreign_key.hasOwnProperty("REFERENCES")) {
282
- if (deepColumn.foreign_key.REFERENCES.hasOwnProperty("table") && deepColumn.foreign_key.REFERENCES.hasOwnProperty("column")) {
283
- if (table_json[databaseName].hasOwnProperty(deepColumn.foreign_key.REFERENCES.table)) {
284
- if (!table_json[databaseName][deepColumn.foreign_key.REFERENCES.table].hasOwnProperty(deepColumn.foreign_key.REFERENCES.column)) {
285
- badforeighkey.push(
286
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ` +
287
- `${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ` +
288
- `${cstyler.purpal('> Column:')} ${cstyler.blue(columnName)} ` +
289
- `${cstyler.purpal('foreign_key > REFERENCES > table > column -')} ${cstyler.yellow.underline(deepColumn.foreign_key.REFERENCES.column)} ${cstyler.red('do not exist')}`
290
- );
291
- }
292
- } else {
293
- badforeighkey.push(`${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ${cstyler.purpal('> Column:')} ${cstyler.blue(`${columnName} > foreign_key > REFERENCES >`)} ${cstyler.purpal('Table:')} ${cstyler.underline.yellow(deepColumn.foreign_key.REFERENCES.table)} - ${cstyler.red('do not exist')}`)
294
- }
295
- } else {
296
- badforeighkey.push(
297
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ` +
298
- `${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ` +
299
- `${cstyler.purpal('> Column:')} ${cstyler.blue(columnName)} ` +
300
- `${cstyler.blue('foreign_key > REFERENCES -')} ${cstyler.red('must have a table and column property')}`
301
- );
302
- }
303
- } else {
304
- badforeighkey.push(
305
- `${cstyler.purpal('Database:')} ${cstyler.blue(databaseName)} ` +
306
- `${cstyler.purpal('> Table:')} ${cstyler.blue(tableName)} ` +
307
- `${cstyler.purpal('> Column:')} ${cstyler.blue(columnName)} ` +
308
- `${cstyler.blue('foreign_key -')} ${cstyler.red('must have a REFERENCES property')}`
309
- );
310
- }
311
- }
312
- }
313
- } else {
314
- console.error("Column of table: ", tableName, " must be in json format.");
315
- return false;
316
- }
317
- }
318
- } else {
319
- console.error("Tables of database: ", databaseName, " must be in Json format.");
320
- return false;
321
- }
322
- }
323
- // Lets return result
324
- if (badTableNames.length === 0 && badColumnNames.length === 0 && badnulls.length === 0 && baddefaults.length === 0 && badlength.length === 0 && badforeighkey.length === 0) {
325
- return true;
326
- }
327
- if (badTableNames.length > 0) {
328
- console.error(`Table names are not correct: \n${badTableNames.join("\n")}`);
329
- }
330
- if (badColumnNames.length > 0) {
331
- console.error(`Column names are not correct: \n${badColumnNames.join("\n")}`);
332
- }
333
- if (badlength.length > 0) {
334
- console.error(`ENUM values that are not correct: \n${badlength.join("\n")}`);
335
- }
336
- if (badautoincrement.length > 0) {
337
- console.error(`Auto Increment values that are not correct: \n${badautoincrement.join("\n")}`);
338
- }
339
- if (badindex.length > 0) {
340
- console.error(`Index values that are not correct: \n${badindex.join("\n")}`);
341
- }
342
- if (badnulls.length > 0) {
343
- console.error(`NULL values that are not correct: \n${badnulls.join("\n")}`);
344
- }
345
- if (baddefaults.length > 0) {
346
- console.error(`DEFAULT values that are not correct: \n${baddefaults.join("\n")}`);
347
- }
348
- if (badforeighkey.length > 0) {
349
- console.error(`Foreign keys and values that are not correct: \n${badforeighkey.join("\n")}`);
350
- }
351
- return false;
352
- } else {
353
- console.error("Plese provide a valid json file");
354
- return false;
355
- }
356
- }
357
- module.exports = {
358
- JSONchecker,
359
- }