-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
115 lines (106 loc) · 3.1 KB
/
index.ts
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
import { DatabaseConfigType, DBClassType, DefinitionsType, DialectType, ObjectType } from './types';
import DB from './dialects/postgres/DB';
import { createDirectory, joinPaths, writeJsonToFile, writeJsToFile, writeTsToFile } from './utils/fileUtils';
type FilesPathsType = { name: string; path: string; data: Object; }[];
class SQLToJsonSchemaConvertor {
private db: DBClassType;
private readonly dbName: string;
private _schema: DefinitionsType;
constructor(dialect: DialectType, databaseConfig: DatabaseConfigType) {
switch (dialect) {
case 'postgres':
this.db = new DB(databaseConfig);
this.dbName = databaseConfig.database;
break;
default:
throw `Dialect ${dialect} not supported!`;
}
}
public generateJsonSchemas = async () => {
await this.db.connect();
this._schema = this.db.getColumns();
};
get schema() {
return this._schema;
}
public writeJsonSchemas = (path: string, granularity: 'single-file' | 'schema' | 'table' | 'field' = 'single-file', format: 'json' | 'js' | 'ts' = 'json') => {
createDirectory(path);
let files: FilesPathsType = [];
let hasMultipleSchemas = false;
switch (granularity) {
case 'single-file':
files = [
{
name: this.dbName,
path,
data: this._schema
}
];
break;
case 'schema':
files = Object.entries(this._schema).map(([ schema, data ]) => ({
name: schema,
path,
data
})
);
break;
case 'table':
files = [];
hasMultipleSchemas = Object.keys(this._schema).length > 1;
for (const [ schema, tables ] of Object.entries(this._schema)) {
const p = hasMultipleSchemas ? joinPaths(path, schema) : path;
createDirectory(p);
files = [
...files,
...Object.entries(tables).map(([ table, data ]) => ({
name: table,
path: p,
data: data as ObjectType
}))
];
}
break;
case 'field':
files = [];
hasMultipleSchemas = Object.keys(this._schema).length > 1;
for (const [ schema, tables ] of Object.entries(this._schema)) {
for (const [ table, fields ] of Object.entries(tables)) {
const p = hasMultipleSchemas ? joinPaths(path, schema, table) : joinPaths(path, table);
createDirectory(p);
files = [
...files,
...Object.entries(fields as ObjectType).map(([ field, data ]) => ({
name: field,
path: p,
data: data as ObjectType
}))
];
}
}
break;
default:
throw `Format ${granularity} not supported`;
}
this.writeFiles(files, format);
};
private writeFiles = (files: FilesPathsType, format: 'json' | 'js' | 'ts') => {
for (const file of files) {
console.log(`Sqema: Writing ${file.name}.${format} to ${joinPaths(process.cwd(), file.path)} ...`);
switch (format) {
case 'js':
writeJsToFile(file.path, file.name, file.data);
break;
case 'json':
writeJsonToFile(file.path, file.name, file.data);
break;
case 'ts':
writeTsToFile(file.path, file.name, file.data);
break;
default:
throw `Format ${format} not supported`;
}
}
};
}
export default SQLToJsonSchemaConvertor;