|
| 1 | +/** |
| 2 | + * Post-processes bundled .d.ts to expand Drizzle $inferSelect types. |
| 3 | + */ |
| 4 | + |
| 5 | +import { readFileSync, writeFileSync } from 'fs'; |
| 6 | + |
| 7 | +const DTS_PATH = './dist/index.d.ts'; |
| 8 | + |
| 9 | +interface ColumnInfo { |
| 10 | + name: string; |
| 11 | + dataType: string; |
| 12 | + notNull: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +interface TableInfo { |
| 16 | + columns: Record<string, ColumnInfo>; |
| 17 | +} |
| 18 | + |
| 19 | +// Store expanded types for later reference |
| 20 | +const expandedTypes = new Map<string, TableInfo>(); |
| 21 | + |
| 22 | +/** |
| 23 | + * Parse a table definition from the .d.ts content to extract column info |
| 24 | + */ |
| 25 | +function parseTableDefinition(content: string, tableName: string): TableInfo | null { |
| 26 | + // Match: declare const tableName: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{ |
| 27 | + const tableRegex = new RegExp( |
| 28 | + `declare const ${tableName}:\\s*import\\("drizzle-orm/sqlite-core"\\)\\.SQLiteTableWithColumns<\\{[^}]*columns:\\s*\\{`, |
| 29 | + 's' |
| 30 | + ); |
| 31 | + |
| 32 | + const tableMatch = content.match(tableRegex); |
| 33 | + if (!tableMatch) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + const startIndex = tableMatch.index! + tableMatch[0].length; |
| 38 | + |
| 39 | + // Find the columns block by tracking brace depth |
| 40 | + let braceDepth = 1; |
| 41 | + let endIndex = startIndex; |
| 42 | + for (let i = startIndex; i < content.length && braceDepth > 0; i++) { |
| 43 | + if (content[i] === '{') braceDepth++; |
| 44 | + if (content[i] === '}') braceDepth--; |
| 45 | + endIndex = i; |
| 46 | + } |
| 47 | + |
| 48 | + const columnsBlock = content.slice(startIndex, endIndex); |
| 49 | + |
| 50 | + // Parse each column |
| 51 | + const columns: Record<string, ColumnInfo> = {}; |
| 52 | + |
| 53 | + // Match column definitions: columnName: import("drizzle-orm/sqlite-core").SQLiteColumn<{...}> |
| 54 | + const columnRegex = /(\w+):\s*import\("drizzle-orm\/sqlite-core"\)\.SQLiteColumn<\{([^>]+)\}>/gs; |
| 55 | + let match; |
| 56 | + |
| 57 | + while ((match = columnRegex.exec(columnsBlock)) !== null) { |
| 58 | + const columnName = match[1]; |
| 59 | + const columnDef = match[2]; |
| 60 | + |
| 61 | + // Extract data type |
| 62 | + const dataMatch = columnDef.match(/data:\s*([^;]+);/); |
| 63 | + // Extract notNull |
| 64 | + const notNullMatch = columnDef.match(/notNull:\s*(true|false)/); |
| 65 | + |
| 66 | + if (dataMatch) { |
| 67 | + const dataType = dataMatch[1].trim(); |
| 68 | + |
| 69 | + columns[columnName] = { |
| 70 | + name: columnName, |
| 71 | + dataType, |
| 72 | + notNull: notNullMatch ? notNullMatch[1] === 'true' : false, |
| 73 | + }; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return { columns }; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Generate an expanded type string from column info |
| 82 | + * @param serializeDates - if true, convert Date to string (for Serialized<T> types) |
| 83 | + */ |
| 84 | +function generateExpandedType(tableInfo: TableInfo, serializeDates = false): string { |
| 85 | + const fields = Object.entries(tableInfo.columns) |
| 86 | + .map(([name, col]) => { |
| 87 | + let dataType = col.dataType; |
| 88 | + |
| 89 | + // Convert Date to string for serialized types |
| 90 | + if (serializeDates && dataType === 'Date') { |
| 91 | + dataType = 'string'; |
| 92 | + } |
| 93 | + |
| 94 | + const type = col.notNull ? dataType : `${dataType} | null`; |
| 95 | + return `\t${name}: ${type};`; |
| 96 | + }) |
| 97 | + .join('\n'); |
| 98 | + |
| 99 | + return `{\n${fields}\n}`; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Find all tables referenced by $inferSelect in the content |
| 104 | + */ |
| 105 | +function findInferSelectReferences(content: string): Map<string, string[]> { |
| 106 | + const refs = new Map<string, string[]>(); |
| 107 | + |
| 108 | + // Match: type TypeName = typeof tableName.$inferSelect; |
| 109 | + const regex = /type\s+(\w+)\s*=\s*typeof\s+(\w+)\.\$inferSelect;/g; |
| 110 | + let match; |
| 111 | + |
| 112 | + while ((match = regex.exec(content)) !== null) { |
| 113 | + const typeName = match[1]; |
| 114 | + const tableName = match[2]; |
| 115 | + |
| 116 | + if (!refs.has(tableName)) { |
| 117 | + refs.set(tableName, []); |
| 118 | + } |
| 119 | + refs.get(tableName)!.push(typeName); |
| 120 | + } |
| 121 | + |
| 122 | + return refs; |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Find Serialized<TypeName> patterns where TypeName is an expanded type |
| 127 | + */ |
| 128 | +function expandSerializedTypes(content: string): string { |
| 129 | + // Find patterns like: type Foo = Serialized<Bar>; |
| 130 | + // where Bar is one of our expanded types |
| 131 | + |
| 132 | + for (const [typeName, tableInfo] of expandedTypes) { |
| 133 | + // Match: Serialized<TypeName> |
| 134 | + const serializedRegex = new RegExp(`Serialized<${typeName}>`, 'g'); |
| 135 | + |
| 136 | + if (serializedRegex.test(content)) { |
| 137 | + const serializedExpanded = generateExpandedType(tableInfo, true); |
| 138 | + content = content.replace(serializedRegex, serializedExpanded); |
| 139 | + console.log(` Expanded Serialized<${typeName}>`); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return content; |
| 144 | +} |
| 145 | + |
| 146 | +function main() { |
| 147 | + console.log('Expanding Drizzle $inferSelect types...'); |
| 148 | + |
| 149 | + let content = readFileSync(DTS_PATH, 'utf-8'); |
| 150 | + |
| 151 | + // Find all $inferSelect references |
| 152 | + const inferSelectRefs = findInferSelectReferences(content); |
| 153 | + console.log(`Found ${inferSelectRefs.size} tables with $inferSelect references`); |
| 154 | + |
| 155 | + // Process each table |
| 156 | + for (const [tableName, typeNames] of inferSelectRefs) { |
| 157 | + console.log(` Processing table: ${tableName} (types: ${typeNames.join(', ')})`); |
| 158 | + |
| 159 | + const tableInfo = parseTableDefinition(content, tableName); |
| 160 | + if (!tableInfo) { |
| 161 | + console.warn(` Warning: Could not parse table definition for ${tableName}`); |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + const columnCount = Object.keys(tableInfo.columns).length; |
| 166 | + console.log(` Found ${columnCount} columns`); |
| 167 | + |
| 168 | + const expandedType = generateExpandedType(tableInfo); |
| 169 | + |
| 170 | + // Replace each type alias with the expanded type |
| 171 | + for (const typeName of typeNames) { |
| 172 | + const oldDecl = `type ${typeName} = typeof ${tableName}.$inferSelect;`; |
| 173 | + const newDecl = `type ${typeName} = ${expandedType}`; |
| 174 | + |
| 175 | + if (content.includes(oldDecl)) { |
| 176 | + content = content.replace(oldDecl, newDecl); |
| 177 | + console.log(` Replaced: ${typeName}`); |
| 178 | + |
| 179 | + // Store for Serialized expansion |
| 180 | + expandedTypes.set(typeName, tableInfo); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + // Also remove $inferInsert references (replace with unknown for now) |
| 186 | + const inferInsertRegex = /type\s+(\w+)\s*=\s*typeof\s+(\w+)\.\$inferInsert;/g; |
| 187 | + content = content.replace(inferInsertRegex, (match, typeName, tableName) => { |
| 188 | + console.log(` Removing $inferInsert reference: ${typeName}`); |
| 189 | + return `type ${typeName} = Record<string, unknown>;`; |
| 190 | + }); |
| 191 | + |
| 192 | + // Now expand Serialized<T> types |
| 193 | + console.log('\nExpanding Serialized<T> types...'); |
| 194 | + content = expandSerializedTypes(content); |
| 195 | + |
| 196 | + writeFileSync(DTS_PATH, content); |
| 197 | + console.log('\nDone!'); |
| 198 | +} |
| 199 | + |
| 200 | +main(); |
0 commit comments