-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateTableSchema.js
75 lines (72 loc) · 2.67 KB
/
createTableSchema.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
var jsonSchema = require("../generated-files/tableSchema.json");
// var jsonSchema = require("./nearbuySchema.json")
var fs = require("fs");
function writeTableJson(){
let relations = {};
for (let i = 0; i < jsonSchema.length; i++) {
let object = jsonSchema[i];
let tableName = object["TABLE_NAME"];
if (!relations[tableName]) {
relations[tableName] = {
primaryKey: [],
columns: [],
foreignKeys: [],
};
}
//Adding Primary key
if (object["INDEX_NAME"] == "PRIMARY") {
let pk = {
column_name: object.COLUMN_NAME,
column_key: object.COLUMN_KEY,
data_type: object.DATA_TYPE,
column_type: object.COLUMN_TYPE,
};
let found = relations[tableName]["primaryKey"].find(o => (o.column_name === pk.column_name))
if(found == undefined)
relations[tableName]["primaryKey"].push(pk)
}
//Adding Foreign keys
if (relations[tableName] && object["referenced_table_name"] != null) {
let fk = {
columns_name: object.COLUMN_NAME,
table_name: object.TABLE_NAME,
referenced_column_table: object.referenced_table_name,
referenced_column_name: object.referenced_column_name,
column_type: object.COLUMN_TYPE,
is_nullable: object.IS_NULLABLE,
};
let found = relations[tableName]["foreignKeys"].find(o => (o.referenced_column_name === fk.referenced_column_name) && (o.referenced_column_table === fk.referenced_column_table))
if(found == undefined)
relations[tableName]["foreignKeys"].push(fk)
}
//Adding Columns
let column = {
"column_name": object.COLUMN_NAME,
"column_key": object.COLUMN_KEY,
"is_nullable": object.IS_NULLABLE,
"data_type": object.DATA_TYPE,
"column_type":object.COLUMN_TYPE
}
let found = relations[tableName]["columns"].find(o => (o.column_name === column.column_name))
if(found == undefined)
relations[tableName]["columns"].push(column)
}
return new Promise((resolve, reject) => {
fs.readFile("../generated-files/tables.js", "utf-8", (err, data) => {
if (err) {
console.log("ERROR", err);
throw err;
}
let writeData = `module.exports = function(){
return ${JSON.stringify(relations, null, '\t')}
}`;
fs.writeFile("../generated-files/tables.js", writeData, "utf-8", (error) => {
if (error) throw error;
console.log("table json writing complete");
});
});
})
}
// writeTableJson()
module.exports = writeTableJson
// console.log(JSON.stringify(relations));