-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (44 loc) · 1.58 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
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import Parser from 'node-sql-parser';
try {
const parser = new Parser.Parser();
const inputDir = path.resolve(`${__dirname}/input`);
const outputDir = path.resolve(path.join(__dirname, './output'));
const sqlFileList = fs.readdirSync(inputDir, (err, files) =>
files.filter((e) => path.extname(e).toLowerCase() === '.sql')
);
sqlFileList.forEach((sqlFile) => {
const data = fs.readFileSync(`${inputDir}/${sqlFile}`, 'utf8');
const ast = parser.astify(data);
ast.forEach((astElement) => {
if (astElement.columns.length) {
let FQLOutput = ``;
switch (astElement.type) {
case 'insert':
astElement.values.forEach((insertValue) => {
FQLOutput += `${astElement.table[0].table}.create({${insertValue.value
.map((column, idx) => `${astElement.columns[idx]}: '${column.value}'`)
.join(',')}})\n`;
});
// console.log(insertFQL);
break;
case 'select':
console.log('\nSelect Queries from the file');
break;
default:
console.log('Not a correct type');
}
// Write the output to file in Output Directory
fs.writeFileSync(`${outputDir}/${sqlFile.replace('.sql', '.fql')}`, FQLOutput);
} else {
console.log('Column Names are missing insert');
}
});
});
} catch (err) {
console.error(err);
}