-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-config.ts
134 lines (129 loc) · 4.03 KB
/
generate-config.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { NotSupported, ObjectType } from '@hasura/ndc-sdk-typescript';
import { Configuration } from './src';
import * as duckdb from 'duckdb';
import * as fs from 'fs';
import { promisify } from "util";
const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);
let HASURA_CONFIGURATION_DIRECTORY = process.env["HASURA_CONFIGURATION_DIRECTORY"] as string | undefined;
if (HASURA_CONFIGURATION_DIRECTORY === undefined || HASURA_CONFIGURATION_DIRECTORY.length === 0){
HASURA_CONFIGURATION_DIRECTORY = ".";
}
const DUCKDB_URL = process.env["DUCKDB_URL"] as string;
const db = new duckdb.Database(DUCKDB_URL);
const con = db.connect();
const determineType = (t: string): string => {
switch (t) {
case "BIGINT":
return "Int";
case "BIT":
return "String";
case "BOOLEAN":
return "Boolean";
case "BLOB":
return "String";
case "DATE":
return "String";
case "DOUBLE":
return "Float";
case "HUGEINT":
return "String";
case "INTEGER":
return "Int";
case "INTERVAL":
return "String";
case "REAL":
return "Float";
case "FLOAT":
return "Float";
case "SMALLINT":
return "Int";
case "TIME":
return "String";
case "TIMESTAMP":
return "String";
case "TIMESTAMP WITH TIME ZONE":
return "String";
case "TINYINT":
return "Int";
case "UBIGINT":
return "String";
case "UINTEGER":
return "Int";
case "USMALLINT":
return "Int";
case "UTINYINT":
return "Int";
case "UUID":
return "String";
case "VARCHAR":
return "String";
case "HUGEINT":
return "String";
default:
if (t.startsWith("DECIMAL")){
return "Float";
}
throw new NotSupported("Unsupported type", {});
}
};
async function queryAll(con: any, query: any): Promise<any[]> {
return new Promise((resolve, reject) => {
con.all(query, function(err: any, res: any){
if (err){
reject(err);
} else {
resolve(res);
}
})
})
};
async function main() {
const tableNames: string[] = [];
const tableAliases: {[k: string]: string} = {};
const objectTypes: { [k: string]: ObjectType } = {};
const tables = await queryAll(con, "SHOW ALL TABLES");
for (let table of tables){
const tableName = `${table.database}_${table.schema}_${table.name}`;
const aliasName = `${table.database}.${table.schema}.${table.name}`;
tableNames.push(tableName);
tableAliases[tableName] = aliasName;
if (!objectTypes[tableName]){
objectTypes[tableName] = {
fields: {}
};
}
for (let i = 0; i < table.column_names.length; i++){
objectTypes[tableName]['fields'][table.column_names[i]] = {
type: {
type: "named",
name: determineType(table.column_types[i])
}
}
}
}
const res: Configuration = {
config: {
collection_names: tableNames,
collection_aliases: tableAliases,
object_types: objectTypes,
functions: [],
procedures: []
}
};
const jsonString = JSON.stringify(res, null, 4);
let filePath = `${HASURA_CONFIGURATION_DIRECTORY}/config.json`;
try {
const existingData = await readFile(filePath, 'utf8');
if (existingData !== jsonString) {
await writeFile(filePath, jsonString);
console.log('File updated.');
} else {
console.log('No changes detected. File not updated.');
}
} catch (error) {
await writeFile(filePath, jsonString);
console.log('New file written.');
}
};
main();