-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectToSQLConverter.js
More file actions
168 lines (142 loc) · 3.44 KB
/
objectToSQLConverter.js
File metadata and controls
168 lines (142 loc) · 3.44 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const util = require('util')
let tables = []
function updateTables(obj) {
let table
if (obj.primaryKey.length > 1) {
let keys = []
obj.primaryKey.forEach((key) => {
keys.push(key.attribute)
})
table = {
name: obj.name,
primaryKey: keys,
}
} else {
table = {
name: obj.name,
primaryKey: obj.primaryKey,
}
}
tables.push(table)
}
function displayTables() {
console.log(
util.inspect(tables, { showHidden: false, depth: null, colors: true })
)
}
function convertObjectToSQL(obj, typeFlag) {
let constraintsFK = []
let constraintsPK = []
let attributes = []
let uniques = []
updateTables(obj)
let query = `CREATE TABLE ${obj.name} (\n`
try {
// set keys values
obj.primaryKey.forEach((key) => {
let value =
Object.getPrototypeOf(key.value) === Object.prototype
? key.value.attribute
: key.value
let type = key.type
if (Object.getPrototypeOf(key.value) === Object.prototype) {
let tmp = {
name: 'FK_' + obj.name + '_' + key.value.attribute,
value: key.value.attribute,
reference: {
name: key.value.relation,
value: tables.find((table) => table.name === key.value.relation)
.primaryKey[0].value,
},
}
constraintsFK.push(tmp)
}
attributes.push({
value,
type,
nullable: false,
autoIncrement: key.autoIncrement,
})
constraintsPK.push(value)
})
// set attributes values
obj.attributes.forEach((attr) => {
let value =
Object.getPrototypeOf(attr.value) === Object.prototype
? attr.value.attribute
: attr.value
let type = attr.type
if (Object.getPrototypeOf(attr.value) === Object.prototype) {
let tmp = {
name: 'FK_' + obj.name + '_' + attr.value.attribute,
value: attr.value.attribute,
reference: {
name: attr.value.relation,
value: tables.find((table) => table.name === attr.value.relation)
.primaryKey[0].value,
},
}
constraintsFK.push(tmp)
}
attributes.push({
value,
type,
nullable: attr.nullable,
autoIncrement: attr.autoIncrement,
})
})
if (obj.unique !== undefined) {
obj.unique.forEach((attr) => {
let uniqueObj = {
name: 'UN_',
}
if (Array.isArray(attr)) {
let innerAttr = []
attr.forEach((a) => {
if (a.attribute !== undefined) {
a = a.attribute
}
innerAttr.push({ value: a })
})
uniqueObj = {
name: 'UN_' + innerAttr[0].value,
value: innerAttr.map((a) => a.value),
}
} else {
uniqueObj = {
name: 'UN_' + attr,
value: attr,
}
}
uniques.push(uniqueObj)
})
}
} catch (exception) {
throw new Error('Relations are not in order')
}
// add attributes
attributes.forEach((attr) => {
query += `\t${attr.value} ${typeFlag ? attr.type : ''} ${
attr.nullable ? '' : 'NOT NULL'
}`
query += `${attr.autoIncrement ? ' AUTO_INCREMENT' : ''}`
query += `,\n`
})
query += `\tCONSTRAINT PK_${obj.name} PRIMARY KEY (${constraintsPK.join(
','
)}),\n`
constraintsFK.forEach((fk) => {
query += `\tCONSTRAINT ${fk.name} FOREIGN KEY (${fk.value}) REFERENCES ${fk.reference.name}(${fk.reference.value}),\n`
})
uniques.forEach((unique) => {
let value = unique.value
if (Array.isArray(unique)) {
value = unique.map((un) => un.value).join(',')
}
query += `\tCONSTRAINT ${unique.name} UNIQUE (${value}),\n`
})
query = query.slice(0, -2) + '\n'
query += `);`
return query
}
module.exports = convertObjectToSQL