|
| 1 | +import { runtimeSchema, NodeSpec, FieldSpec } from '../../utils/src/runtime-schema'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | + |
| 5 | +interface FieldMetadata { |
| 6 | + nullable: boolean; |
| 7 | + tags: string[]; |
| 8 | + isArray: boolean; |
| 9 | +} |
| 10 | + |
| 11 | +interface NodeFieldMetadata { |
| 12 | + [fieldName: string]: FieldMetadata; |
| 13 | +} |
| 14 | + |
| 15 | +interface AllFieldMetadata { |
| 16 | + [nodeName: string]: NodeFieldMetadata; |
| 17 | +} |
| 18 | + |
| 19 | +const schemaMap = new Map<string, NodeSpec>( |
| 20 | + runtimeSchema.map((spec: NodeSpec) => [spec.name, spec]) |
| 21 | +); |
| 22 | + |
| 23 | +const primitiveTypeMap: Record<string, string> = { |
| 24 | + 'string': 'string', |
| 25 | + 'bool': 'boolean', |
| 26 | + 'int32': 'number', |
| 27 | + 'int64': 'number', |
| 28 | + 'uint32': 'number', |
| 29 | + 'uint64': 'number', |
| 30 | + 'float': 'number', |
| 31 | + 'double': 'number', |
| 32 | + 'bytes': 'Uint8Array', |
| 33 | +}; |
| 34 | + |
| 35 | +function isPrimitiveType(type: string): boolean { |
| 36 | + return type in primitiveTypeMap; |
| 37 | +} |
| 38 | + |
| 39 | +function isEnumType(type: string): boolean { |
| 40 | + return !isPrimitiveType(type) && !schemaMap.has(type) && type !== 'Node'; |
| 41 | +} |
| 42 | + |
| 43 | +function getTsType(type: string): string { |
| 44 | + return primitiveTypeMap[type] || type; |
| 45 | +} |
| 46 | + |
| 47 | +function collectEnumTypes(): Set<string> { |
| 48 | + const enumTypes = new Set<string>(); |
| 49 | + for (const nodeSpec of runtimeSchema) { |
| 50 | + for (const field of nodeSpec.fields) { |
| 51 | + if (isEnumType(field.type)) { |
| 52 | + enumTypes.add(field.type); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + return enumTypes; |
| 57 | +} |
| 58 | + |
| 59 | +function generateWrappedUnion(tags: string[]): string { |
| 60 | + if (tags.length === 0) { |
| 61 | + return 'Node'; |
| 62 | + } |
| 63 | + |
| 64 | + const sortedTags = [...tags].sort(); |
| 65 | + return sortedTags.map(tag => `{ ${tag}: ${tag} }`).join(' | '); |
| 66 | +} |
| 67 | + |
| 68 | +function generateTypeAlias(nodeName: string, fieldName: string, tags: string[]): string { |
| 69 | + const aliasName = `${nodeName}_${fieldName}`; |
| 70 | + const union = generateWrappedUnion(tags); |
| 71 | + return `type ${aliasName} = ${union};`; |
| 72 | +} |
| 73 | + |
| 74 | +function generateInterface( |
| 75 | + nodeSpec: NodeSpec, |
| 76 | + fieldMetadata: NodeFieldMetadata | undefined |
| 77 | +): string { |
| 78 | + const lines: string[] = []; |
| 79 | + lines.push(`export interface ${nodeSpec.name} {`); |
| 80 | + |
| 81 | + for (const field of nodeSpec.fields) { |
| 82 | + const tsType = getFieldType(nodeSpec.name, field, fieldMetadata); |
| 83 | + const optional = field.optional ? '?' : ''; |
| 84 | + lines.push(` ${field.name}${optional}: ${tsType};`); |
| 85 | + } |
| 86 | + |
| 87 | + lines.push('}'); |
| 88 | + return lines.join('\n'); |
| 89 | +} |
| 90 | + |
| 91 | +function getFieldType( |
| 92 | + nodeName: string, |
| 93 | + field: FieldSpec, |
| 94 | + fieldMetadata: NodeFieldMetadata | undefined |
| 95 | +): string { |
| 96 | + let baseType: string; |
| 97 | + |
| 98 | + if (field.type === 'Node') { |
| 99 | + const meta = fieldMetadata?.[field.name]; |
| 100 | + if (meta && meta.tags.length > 0) { |
| 101 | + baseType = `${nodeName}_${field.name}`; |
| 102 | + } else { |
| 103 | + baseType = 'Node'; |
| 104 | + } |
| 105 | + } else if (isPrimitiveType(field.type)) { |
| 106 | + baseType = getTsType(field.type); |
| 107 | + } else { |
| 108 | + if (schemaMap.has(field.type)) { |
| 109 | + baseType = `{ ${field.type}: ${field.type} }`; |
| 110 | + } else { |
| 111 | + baseType = field.type; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (field.isArray) { |
| 116 | + if (baseType.includes('|') || baseType.includes('{')) { |
| 117 | + return `(${baseType})[]`; |
| 118 | + } |
| 119 | + return `${baseType}[]`; |
| 120 | + } |
| 121 | + |
| 122 | + return baseType; |
| 123 | +} |
| 124 | + |
| 125 | +function generateTypes(metadata: AllFieldMetadata): string { |
| 126 | + const lines: string[] = []; |
| 127 | + |
| 128 | + lines.push('/**'); |
| 129 | + lines.push(' * This file was automatically generated by pgsql-types.'); |
| 130 | + lines.push(' * DO NOT MODIFY IT BY HAND.'); |
| 131 | + lines.push(' * '); |
| 132 | + lines.push(' * These types provide narrowed Node unions based on actual usage'); |
| 133 | + lines.push(' * patterns discovered by parsing SQL fixtures.'); |
| 134 | + lines.push(' */'); |
| 135 | + lines.push(''); |
| 136 | + |
| 137 | + const enumTypes = collectEnumTypes(); |
| 138 | + const sortedEnums = [...enumTypes].sort(); |
| 139 | + |
| 140 | + lines.push("import type { Node } from '@pgsql/types';"); |
| 141 | + if (sortedEnums.length > 0) { |
| 142 | + lines.push(`import { ${sortedEnums.join(', ')} } from '@pgsql/enums';`); |
| 143 | + } |
| 144 | + lines.push("export type { Node } from '@pgsql/types';"); |
| 145 | + lines.push("export * from '@pgsql/enums';"); |
| 146 | + lines.push(''); |
| 147 | + |
| 148 | + const typeAliases: string[] = []; |
| 149 | + for (const nodeName of Object.keys(metadata).sort()) { |
| 150 | + const nodeMetadata = metadata[nodeName]; |
| 151 | + for (const fieldName of Object.keys(nodeMetadata).sort()) { |
| 152 | + const fieldMeta = nodeMetadata[fieldName]; |
| 153 | + if (fieldMeta.tags.length > 0) { |
| 154 | + typeAliases.push(generateTypeAlias(nodeName, fieldName, fieldMeta.tags)); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + if (typeAliases.length > 0) { |
| 160 | + lines.push('// Internal type aliases for narrowed Node-typed fields (not exported)'); |
| 161 | + lines.push(typeAliases.join('\n')); |
| 162 | + lines.push(''); |
| 163 | + } |
| 164 | + |
| 165 | + lines.push('// Interfaces with narrowed Node types'); |
| 166 | + for (const nodeSpec of runtimeSchema) { |
| 167 | + const nodeMetadata = metadata[nodeSpec.name]; |
| 168 | + lines.push(generateInterface(nodeSpec, nodeMetadata)); |
| 169 | + lines.push(''); |
| 170 | + } |
| 171 | + |
| 172 | + return lines.join('\n'); |
| 173 | +} |
| 174 | + |
| 175 | +async function main() { |
| 176 | + const metadataPath = path.resolve(__dirname, '../src/field-metadata.json'); |
| 177 | + const outputPath = path.resolve(__dirname, '../src/types.ts'); |
| 178 | + |
| 179 | + if (!fs.existsSync(metadataPath)) { |
| 180 | + console.error('Field metadata not found. Run "npm run infer" first.'); |
| 181 | + process.exit(1); |
| 182 | + } |
| 183 | + |
| 184 | + const metadata: AllFieldMetadata = JSON.parse(fs.readFileSync(metadataPath, 'utf-8')); |
| 185 | + |
| 186 | + console.log('Generating narrowed types...'); |
| 187 | + const typesContent = generateTypes(metadata); |
| 188 | + |
| 189 | + fs.writeFileSync(outputPath, typesContent); |
| 190 | + console.log(`Wrote narrowed types to ${outputPath}`); |
| 191 | + |
| 192 | + let totalAliases = 0; |
| 193 | + for (const nodeName of Object.keys(metadata)) { |
| 194 | + for (const fieldName of Object.keys(metadata[nodeName])) { |
| 195 | + if (metadata[nodeName][fieldName].tags.length > 0) { |
| 196 | + totalAliases++; |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + console.log(`Generated ${totalAliases} narrowed type aliases`); |
| 202 | +} |
| 203 | + |
| 204 | +main().catch(console.error); |
0 commit comments