Repository navigation
Expand file tree
/
Copy pathschema-parser.ts
More file actions
145 lines (131 loc) · 4.62 KB
/
Copy pathschema-parser.ts
File metadata and controls
145 lines (131 loc) · 4.62 KB
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
import ts from 'typescript';
export interface ParsedColumn {
name: string;
databaseName: string;
type: string;
constraints: string[];
}
export interface ParsedTable {
name: string;
columns: ParsedColumn[];
}
function nodeName(node: ts.PropertyName | ts.BindingName): string | undefined {
if (ts.isIdentifier(node) || ts.isStringLiteral(node) || ts.isNumericLiteral(node)) {
return node.text;
}
return undefined;
}
function callName(expression: ts.Expression): string | undefined {
if (ts.isIdentifier(expression)) return expression.text;
if (ts.isPropertyAccessExpression(expression)) return expression.name.text;
return undefined;
}
function baseFactoryName(expression: ts.Expression): string | undefined {
let current: ts.Expression = expression;
while (ts.isPropertyAccessExpression(current) || ts.isCallExpression(current)) {
current = ts.isPropertyAccessExpression(current) ? current.expression : current.expression;
}
return ts.isIdentifier(current) ? current.text : undefined;
}
function baseFactoryCall(expression: ts.Expression): ts.CallExpression | undefined {
if (ts.isCallExpression(expression)) {
const name = callName(expression.expression);
const columnFactories = new Set([
'text',
'integer',
'real',
'blob',
'boolean',
'timestamp',
'serial',
'bigint',
'varchar',
'char',
'uuid',
'json',
'jsonb',
'numeric',
'decimal',
'date',
'time',
'vector',
]);
if (name && columnFactories.has(name)) {
return expression;
}
return baseFactoryCall(expression.expression);
}
if (ts.isPropertyAccessExpression(expression)) return baseFactoryCall(expression.expression);
return undefined;
}
function stringArgument(call: ts.CallExpression): string | undefined {
if (call.arguments.length === 0) return undefined;
const first = call.arguments[0];
return ts.isStringLiteral(first) || ts.isNoSubstitutionTemplateLiteral(first)
? first.text
: undefined;
}
function columnConstraints(initializer: ts.Expression): string[] {
const constraints = new Set<string>();
const visit = (node: ts.Node): void => {
if (ts.isPropertyAccessExpression(node)) {
const name = node.name.text;
if (name === 'primaryKey') constraints.add('PRIMARY KEY');
if (name === 'notNull') constraints.add('NOT NULL');
if (name === 'unique') constraints.add('UNIQUE');
if (name === 'references') constraints.add('FK');
if (name === '$defaultFn' || name === '$default') constraints.add('DEFAULT');
}
ts.forEachChild(node, visit);
};
visit(initializer);
return [...constraints];
}
function parseTable(statement: ts.VariableStatement): ParsedTable | undefined {
for (const declaration of statement.declarationList.declarations) {
const initializer = declaration.initializer;
if (!initializer || !ts.isCallExpression(initializer)) continue;
const factory = callName(initializer.expression);
if (factory !== 'sqliteTable' && factory !== 'pgTable') continue;
const name = stringArgument(initializer);
const columnsArgument = initializer.arguments[1];
if (!name || initializer.arguments.length < 2 || !ts.isObjectLiteralExpression(columnsArgument))
continue;
const columns: ParsedColumn[] = [];
for (const property of columnsArgument.properties) {
if (!ts.isPropertyAssignment(property)) continue;
const columnName = nodeName(property.name);
const value = property.initializer;
if (!columnName || !ts.isCallExpression(value)) continue;
const type = baseFactoryName(value.expression);
if (!type) continue;
const databaseName = stringArgument(baseFactoryCall(value) ?? value) ?? columnName;
const constraints = columnConstraints(value);
const order = ['PRIMARY KEY', 'NOT NULL', 'UNIQUE', 'FK', 'DEFAULT'];
constraints.sort((a, b) => order.indexOf(a) - order.indexOf(b));
columns.push({ name: columnName, databaseName, type, constraints });
}
return { name, columns };
}
return undefined;
}
/** Parse Drizzle sqliteTable/pgTable declarations without relying on formatting. */
export function parseDatabaseSchema(content: string, fileName = 'schema.ts'): ParsedTable[] {
const sourceFile = ts.createSourceFile(
fileName,
content,
ts.ScriptTarget.Latest,
true,
ts.ScriptKind.TS,
);
const tables: ParsedTable[] = [];
const visit = (node: ts.Node): void => {
if (ts.isVariableStatement(node)) {
const table = parseTable(node);
if (table) tables.push(table);
}
ts.forEachChild(node, visit);
};
visit(sourceFile);
return tables;
}