-
Notifications
You must be signed in to change notification settings - Fork 3
/
pgSchemaTypeGen.js
285 lines (248 loc) · 10.6 KB
/
pgSchemaTypeGen.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { Parser } from 'node-sql-parser';
export class PgSchemaTypeGen {
constructor() {
this.parser = new Parser();
}
getColumnDefinitionNames(columnDefs) {
const columnDefinitionNames = new Map();
for (const columnDef of columnDefs) {
if (columnDef.column?.type === 'column_ref') {
const columnNameDef = columnDef.column.column.expr;
const actualColumnName =
columnNameDef.type === 'double_quote_string' ? `"${columnNameDef.value}"` : columnNameDef.value;
columnDefinitionNames.set(columnNameDef.value, actualColumnName);
}
}
return columnDefinitionNames;
}
retainOriginalQuoting(schema, tableName) {
const createTableQuotedRegex = `\\b(create|CREATE)\\s+(table|TABLE)\\s+"${tableName}"\\s*`;
return schema.match(new RegExp(createTableQuotedRegex, 'i')) ? `"${tableName}"` : tableName;
}
getTableNameToDefinitionNamesMapping(schema) {
let schemaSyntaxTree = this.parser.astify(schema, { database: 'Postgresql' });
schemaSyntaxTree = Array.isArray(schemaSyntaxTree) ? schemaSyntaxTree : [schemaSyntaxTree]; // Ensure iterable
const tableNameToDefinitionNamesMap = new Map();
for (const statement of schemaSyntaxTree) {
if (statement.type === 'create' && statement.keyword === 'table' && statement.table !== undefined) {
const tableName = statement.table[0].table;
if (tableNameToDefinitionNamesMap.has(tableName)) {
throw new Error(
`Table ${tableName} already exists in schema. Table names must be unique. Quotes are not allowed as a differentiator between table names.`,
);
}
const createDefs = statement.create_definitions ?? [];
const tableDefinitionNames = {
originalTableName: this.retainOriginalQuoting(schema, tableName),
originalColumnNames: this.getColumnDefinitionNames(createDefs),
};
tableNameToDefinitionNamesMap.set(tableName, tableDefinitionNames);
}
}
if (tableNameToDefinitionNamesMap.size === 0) {
throw new Error('Schema does not have any tables. There should be at least one table.');
}
return tableNameToDefinitionNamesMap;
}
sanitizeTableName(tableName) {
// Convert to PascalCase
let pascalCaseTableName = tableName
.replace(/[^a-zA-Z0-9_]/g, '_') // Replace special characters with underscores
.replace(/^([a-zA-Z])|_([a-zA-Z])/g, (match) => match.toUpperCase()) // PascalCase transformation
.replace(/_/g, ''); // Remove all underscores
// Add underscore if first character is a number
if (/^[0-9]/.test(pascalCaseTableName)) {
pascalCaseTableName = '_' + pascalCaseTableName;
}
return pascalCaseTableName;
}
generateTypes(sqlSchema) {
const schemaSyntaxTree = this.parser.astify(sqlSchema, { database: 'Postgresql' });
const dbSchema = {};
const statements = Array.isArray(schemaSyntaxTree) ? schemaSyntaxTree : [schemaSyntaxTree];
for (const statement of statements) {
if (statement.type === 'create' && statement.keyword === 'table') {
this.processCreateTableStatement(statement, dbSchema);
} else if (statement.type === 'alter') {
this.processAlterTableStatement(statement, dbSchema);
}
}
const tsTypes = this.generateTypeScriptDefinitions(dbSchema);
console.log(`Types successfully generated`);
return tsTypes;
}
processCreateTableStatement(statement, dbSchema) {
const tableName = statement.table[0].table;
if (Object.prototype.hasOwnProperty.call(dbSchema, tableName)) {
throw new Error(
`Table ${tableName} already exists in schema. Table names must be unique. Quotes are not allowed as a differentiator between table names.`,
);
}
let columns = {};
for (const columnSpec of statement.create_definitions) {
if (
Object.prototype.hasOwnProperty.call(columnSpec, 'column') &&
Object.prototype.hasOwnProperty.call(columnSpec, 'definition')
) {
this.addColumn(columnSpec, columns);
} else if (columnSpec.constraint_type === 'primary key') {
for (const foreignKeyDef of columnSpec.definition) {
columns[foreignKeyDef.column.expr.value].nullable = false;
}
}
}
dbSchema[tableName] = columns;
}
processAlterTableStatement(statement, dbSchema) {
const tableName = statement.table[0].table;
let newConstraint = {};
for (const alterSpec of statement.expr) {
switch (alterSpec.action) {
case 'add':
switch (alterSpec.resource) {
case 'column':
this.addColumn(alterSpec, dbSchema[tableName]);
break;
case 'constraint':
newConstraint = alterSpec.create_definitions;
if (newConstraint.constraint_type == 'primary key') {
for (const foreignKeyDef of newConstraint.definition) {
dbSchema[tableName][foreignKeyDef.column].nullable = false;
}
}
break;
}
break;
case 'drop':
delete dbSchema[tableName][alterSpec.column.column];
break;
}
}
}
addColumn(columnDef, columns) {
const columnName = columnDef.column.column.expr.value;
const columnType = this.getTypescriptType(columnDef.definition.dataType);
const nullable = this.getNullableStatus(columnDef);
const required = this.getRequiredStatus(columnDef, nullable);
if (Object.prototype.hasOwnProperty.call(columns, columnName)) {
console.warn(`Column ${columnName} already exists in table. Skipping.`);
return;
}
columns[columnName] = {
type: columnType,
nullable: nullable,
required: required,
};
}
getNullableStatus(columnDef) {
const isPrimaryKey =
Object.prototype.hasOwnProperty.call(columnDef, 'unique_or_primary') &&
columnDef.unique_or_primary &&
columnDef.unique_or_primary === 'primary key';
const isNullable =
Object.prototype.hasOwnProperty.call(columnDef, 'nullable') &&
columnDef.nullable &&
columnDef.nullable.value === 'not null';
return isPrimaryKey || isNullable ? false : true;
}
getRequiredStatus(columnDef, nullable) {
const hasDefaultValue =
Object.prototype.hasOwnProperty.call(columnDef, 'default_val') &&
columnDef.default_val &&
columnDef.default_val != null;
const isSerial = columnDef.definition.dataType.toLowerCase().includes('serial');
return hasDefaultValue || isSerial || nullable ? false : true;
}
generateTypeScriptDefinitions(schema) {
let tsDefinitions = '';
let contextObject = `declare const context: {
graphql: (operation, variables) => Promise<any>,
set: (key, value) => Promise<any>,
log: (...log) => Promise<any>,
fetchFromSocialApi: (path, options) => Promise<Response>,
error: (message: string) => Promise<void>,
db: {`;
const tableList = new Set();
for (const [tableName, columns] of Object.entries(schema)) {
const sanitizedTableName = this.sanitizeTableName(tableName);
if (tableList.has(sanitizedTableName)) {
throw new Error(
`Table '${tableName}' has the same name as another table in the generated types. Special characters are removed to generate context.db methods. Please rename the table.`,
);
}
tableList.add(sanitizedTableName);
let queryDefinition = `declare interface ${sanitizedTableName}Query {\n`;
let itemDefinition = `declare interface ${sanitizedTableName}Item {\n`;
let inputDefinition = `declare interface ${sanitizedTableName}Input {\n`;
for (const [columnName, columnDetails] of Object.entries(columns)) {
const tsType = columnDetails.nullable ? columnDetails.type + ' | null' : columnDetails.type;
const optional = columnDetails.required ? '' : '?';
queryDefinition += ` ${columnName}?: ${tsType} | ${tsType}[];\n`;
itemDefinition += ` ${columnName}?: ${tsType};\n`;
inputDefinition += ` ${columnName}${optional}: ${tsType};\n`;
}
queryDefinition += '}\n\n';
itemDefinition += '}\n\n';
inputDefinition += '}\n\n';
const columnNamesDef = `type ${sanitizedTableName}Columns = "${Object.keys(columns).join('" | "')}";\n\n`;
tsDefinitions += queryDefinition + itemDefinition + inputDefinition + columnNamesDef;
contextObject += `
${sanitizedTableName}: {
insert: (objectsToInsert: ${sanitizedTableName}Input | ${sanitizedTableName}Input[]) => Promise<${sanitizedTableName}Item[]>;
select: (filterObj: ${sanitizedTableName}Query, limit = null) => Promise<${sanitizedTableName}Item[]>;
update: (filterObj: ${sanitizedTableName}Query, updateObj: ${sanitizedTableName}Item) => Promise<${sanitizedTableName}Item[]>;
upsert: (objectsToInsert: ${sanitizedTableName}Input | ${sanitizedTableName}Input[], conflictColumns: ${sanitizedTableName}Columns[], updateColumns: ${sanitizedTableName}Columns[]) => Promise<${sanitizedTableName}Item[]>;
delete: (filterObj: ${sanitizedTableName}Query) => Promise<${sanitizedTableName}Item[]>;
},`;
}
contextObject += '\n }\n};';
return tsDefinitions + contextObject;
}
getTypescriptType(pgType) {
const typeMap = {
// Numeric types
smallint: 'number',
integer: 'number',
bigint: 'number',
decimal: 'number',
numeric: 'number',
real: 'number',
'double precision': 'number',
serial: 'number',
bigserial: 'number',
// Monetary types
money: 'number',
// Character types
'character varying': 'string',
varchar: 'string',
character: 'string',
char: 'string',
text: 'string',
// Binary data types
bytea: 'Buffer | string',
// Boolean type
boolean: 'boolean | string',
// Date/Time types
timestamp: 'Date | string',
'timestamp without time zone': 'Date | string',
'timestamp with time zone': 'Date | string',
date: 'Date | string',
time: 'Date | string',
'time without time zone': 'Date | string',
'time with time zone': 'Date | string',
interval: 'Date | string',
// UUID type
uuid: 'string',
// JSON types
json: 'string | any',
jsonb: 'string | any',
// Arrays
'integer[]': 'number[]',
'text[]': 'string[]',
// Default
default: 'any', // Replace with appropriate default type
};
const typeKey = pgType.toLowerCase();
return typeMap[typeKey] || typeMap['default'];
}
}