-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (126 loc) · 3.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class JSONtoMysql {
constructor(name, json) {
this.json = json;
this.name = name;
this.structure = [];
this.query = "";
for (var i in this.json) {
if (this.isArray(this.json[i])) {
throw "Multi Array JSON, serialize your JSON";
} else {
// Do another thing
}
}
this.structure_explore(this.json);
this.structure_build();
}
structure_explore(json) {
let i = 0;
for (let key in json) {
this.structure[i] = {};
var value = json[key];
this.structure[i].name = key;
var type = typeof value;
// console.log('Test Json:', type);
switch (type) {
case "string":
this.structure[i] = this.extend(
this.structure[i],
this.string_type(value)
);
break;
case "number":
this.structure[i] = this.extend(
this.structure[i],
this.number_type(value)
);
break;
default:
this.structure[i].type = "INT";
this.structure[i].length = 10;
this.structure[i].default = "NOT NULL EFAULT '0'";
break;
}
i++;
}
}
structure_build() {
this.query += "CREATE TABLE IF NOT EXISTS `" + this.name + "` ( \n";
this.structure.forEach((element, index, array) => {
if (element.length > 0) {
element.type = element.type + " (" + element.length + ") ";
}
if (index === array.length - 1) {
// Last element
this.query +=
"`" +
element.name +
"` " +
element.type +
" " +
element.default +
" \n";
} else {
this.query +=
"`" +
element.name +
"` " +
element.type +
" " +
element.default +
", \n";
}
});
this.query += ") ENGINE=INNODB DEFAULT CHARSET=utf8; \n";
return this.query;
}
string_type(string) {
let field = {};
// Not real string
if (this.string_isnumber(string)) {
return this.number_type(string);
}
if (string.length > 255) {
field.type = "TEXT";
field.length = 0;
field.default = "NOT NULL";
} else {
field.type = "VARCHAR";
field.length = 255;
field.default = "NOT NULL";
}
return field;
}
number_type(number) {
let field = {};
if (number % 1 === 0) {
field.type = "INT";
field.length = 10;
field.default = "NOT NULL DEFAULT '0'";
} else {
field.type = "FLOAT";
field.default = "NOT NULL DEFAULT '0'";
field.length = 0;
}
return field;
}
string_isnumber(string) {
if (!isNaN(string) && string.toString().indexOf(".") != -1) {
return true;
}
if (!isNaN(string) && string.toString().indexOf(".") == -1) {
return true;
}
return false;
}
isArray(what) {
return Object.prototype.toString.call(what) === "[object Array]";
}
extend(obj, src) {
Object.keys(src).forEach(function(key) {
obj[key] = src[key];
});
return obj;
}
}
module.exports = JSONtoMysql;