-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-config.ts
167 lines (159 loc) · 5.28 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
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
161
162
163
164
165
166
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 "BigInt";
case "BIT":
return "String";
case "BOOLEAN":
return "Boolean";
case "BLOB":
return "String";
case "DATE":
return "String";
case "DOUBLE":
return "Float";
case "HUGEINT":
return "HugeInt";
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 "Timestamp";
case "TIMESTAMP WITH TIME ZONE":
return "TimestampTz";
case "TINYINT":
return "Int";
case "UBIGINT":
return "UBigInt";
case "UHUGEINT":
return "UHugeInt"
case "UINTEGER":
return "Int";
case "USMALLINT":
return "Int";
case "UTINYINT":
return "Int";
case "UUID":
return "String";
case "VARCHAR":
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");
// Get table comments
const tableComments = await queryAll(con, `
SELECT table_name, comment
FROM duckdb_tables()
WHERE schema_name = 'main'
`);
// Create a map of table comments for easier lookup
const tableCommentMap = new Map(
tableComments.map(row => [row.table_name, row.comment || "No description available"])
);
// Get all column comments upfront
const columnComments = await queryAll(con, `
SELECT table_name, column_name, comment
FROM duckdb_columns()
WHERE schema_name = 'main'
`);
// Create a nested map for column comments: table_name -> column_name -> comment
const columnCommentMap = new Map();
for (const row of columnComments) {
if (!columnCommentMap.has(row.table_name)) {
columnCommentMap.set(row.table_name, new Map());
}
columnCommentMap.get(row.table_name).set(row.column_name, row.comment || "No description available");
}
for (let table of tables){
const tableName = table.name;
const aliasName = `${table.database}.${table.schema}.${table.name}`;
tableNames.push(tableName);
tableAliases[tableName] = aliasName;
if (!objectTypes[tableName]){
objectTypes[tableName] = {
fields: {},
description: tableCommentMap.get(tableName) || "No Description Available"
};
}
for (let i = 0; i < table.column_names.length; i++){
const columnName = table.column_names[i];
objectTypes[tableName]["fields"][columnName] = {
type: {
type: "nullable",
underlying_type: {
type: "named",
name: determineType(table.column_types[i]),
},
},
description: columnCommentMap.get(tableName)?.get(columnName) || "No description available"
}
}
}
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();