-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
160 lines (136 loc) · 3.76 KB
/
index.js
File metadata and controls
160 lines (136 loc) · 3.76 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
const inquirer = require("inquirer");
const chalk = require("chalk");
const figlet = require("figlet");
const shell = require("shelljs");
const fs = require('fs');
const Sequelize = {
UUID: 'varchar(255)',
STRING: 'varchar(255)',
CHAR: 'char(255)',
TINYINT: 'TINYINT(8)',
SMALLINT: 'SMALLINT(16)',
MEDIUMINT: 'MEDIUMINT(24)',
INTEGER: 'INTEGER',
BIGINT: 'BIGINT',
FLOAT: 'FLOAT',
REAL: 'REAL',
DOUBLE: 'DOUBLE',
DECIMAL: 'DECIMAL',
BLOB: 'TEXT',
DATE: 'DATE',
DATEONLY: 'DATE',
TIME: 'DATETIME',
TEXT: 'TEXT',
BOOLEAN: 'BOOL',
ENUM: 'enum'
}
const init = () => {
console.log(
chalk.green(
figlet.textSync("Sequelize Inverse Model", {
font: "Standard",
horizontalLayout: "default",
verticalLayout: "default"
})
)
);
};
const askQuestions = () => {
const questions = [
{
name: "DB",
type: "input",
message: "What is the name of the database?"
},
{
name: "TABLE",
type: "input",
message: "What is the name of the table?"
},
{
name: "MODEL",
type: "editor",
message: "Paste the fields of the model you want to transform here:"
},
{
name: "QUERY",
type: "list",
message: "Do you want to create the .sql with the obtained result?",
choices: ["Yes", "No"]
}
];
return inquirer.prompt(questions);
};
const createFile = (filename, Query) => {
const filePath = `${process.cwd()}/${filename}.sql`
shell.touch(filePath);
fs.appendFile(`${filename}.sql`, Query, function (err) {
if (err) throw err;
});
return filePath;
};
const successFile = filepath => {
console.log(
chalk.bgMagentaBright(`Done! File created at ${filepath}`)
);
};
const successTransform = (DB, Table, { Query, Keys }) => {
const result = `
CREATE TABLE ${DB}.${Table} (
${Query} ${Keys}
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci;
`;
console.log(
chalk.magenta(`${result}`)
);
return result;
};
const transform = (Table, Model) => {
let fields = JSON.parse(JSON.stringify(eval("(" + Model + ")")));
let Query = '';
let Keys = '';
for (const key in fields) {
if (fields.hasOwnProperty(key)) {
const value = fields[key];
let extra = '';
Keys += 'primaryKey' in value ? `CONSTRAINT ${Table}_PK PRIMARY KEY (${key})
`: '';
if (value.type === 'enum') {
value.type = `enum(${"'" + value.values.join(",").replace(/,/g, "','") + "'"})`
}
extra += 'defaultValue' in value ? ` DEFAULT ${value.defaultValue} ` : '';
extra += 'allowNull' in value ? 'NOT NULL' : 'NULL';
Query += `${key} ${value.type} ${extra},
`;
}
}
/*Query += `cretatedAt DATETIME NOT NULL,
updatedAt DATETIME NOT NULL,
`;*/
return { Query, Keys };
}
const run = async () => {
try {
// show script introduction
init();
// ask questions
const answers = await askQuestions();
const { DB, TABLE, MODEL, QUERY } = answers;
// Transform
const TRANSFORM = transform(TABLE, MODEL);
// Success Transform
const RESULT = await successTransform(DB, TABLE, TRANSFORM);
if (QUERY === 'Yes') {
// create the file
const filePath = createFile(TABLE, RESULT);
// show success message
successFile(filePath);
}
} catch (error) {
console.log({ error });
}
};
run();