|
1 | | -import { FastifyInstance } from 'fastify' |
2 | | -import prettier from 'prettier' |
3 | | -import { PostgresMeta, PostgresType } from '../../../lib' |
| 1 | +import type { FastifyInstance } from 'fastify' |
| 2 | +import { PostgresMeta } from '../../../lib' |
4 | 3 | import { DEFAULT_POOL_CONFIG } from '../../constants' |
5 | 4 | import { extractRequestForLogging } from '../../utils' |
| 5 | +import { apply as applyTypescriptTemplate } from '../../templates/typescript' |
6 | 6 |
|
7 | 7 | export default async (fastify: FastifyInstance) => { |
8 | 8 | fastify.get<{ |
@@ -45,156 +45,11 @@ export default async (fastify: FastifyInstance) => { |
45 | 45 | return { error: typesError.message } |
46 | 46 | } |
47 | 47 |
|
48 | | - let output = ` |
49 | | -export type Json = string | number | boolean | null | { [key: string]: Json } | Json[] |
50 | | -
|
51 | | -export interface Database { |
52 | | - ${schemas |
53 | | - .filter(({ name }) => !excludedSchemas.includes(name)) |
54 | | - .map( |
55 | | - (schema) => |
56 | | - `${JSON.stringify(schema.name)}: { |
57 | | - Tables: { |
58 | | - ${tables |
59 | | - .filter((table) => table.schema === schema.name) |
60 | | - .map( |
61 | | - (table) => `${JSON.stringify(table.name)}: { |
62 | | - Row: { |
63 | | - ${table.columns.map( |
64 | | - (column) => |
65 | | - `${JSON.stringify(column.name)}: ${pgTypeToTsType(column.format, types)} ${ |
66 | | - column.is_nullable ? '| null' : '' |
67 | | - }` |
68 | | - )} |
69 | | - } |
70 | | - Insert: { |
71 | | - ${table.columns.map((column) => { |
72 | | - let output = JSON.stringify(column.name) |
73 | | -
|
74 | | - if (column.identity_generation === 'ALWAYS') { |
75 | | - return `${output}?: never` |
76 | | - } |
77 | | -
|
78 | | - if ( |
79 | | - column.is_nullable || |
80 | | - column.is_identity || |
81 | | - column.default_value !== null |
82 | | - ) { |
83 | | - output += '?:' |
84 | | - } else { |
85 | | - output += ':' |
86 | | - } |
87 | | -
|
88 | | - output += pgTypeToTsType(column.format, types) |
89 | | -
|
90 | | - if (column.is_nullable) { |
91 | | - output += '| null' |
92 | | - } |
93 | | -
|
94 | | - return output |
95 | | - })} |
96 | | - } |
97 | | - Update: { |
98 | | - ${table.columns.map((column) => { |
99 | | - let output = JSON.stringify(column.name) |
100 | | -
|
101 | | - if (column.identity_generation === 'ALWAYS') { |
102 | | - return `${output}?: never` |
103 | | - } |
104 | | -
|
105 | | - output += `?: ${pgTypeToTsType(column.format, types)}` |
106 | | -
|
107 | | - if (column.is_nullable) { |
108 | | - output += '| null' |
109 | | - } |
110 | | -
|
111 | | - return output |
112 | | - })} |
113 | | - } |
114 | | - }` |
115 | | - )} |
116 | | - } |
117 | | - Functions: { |
118 | | - ${functions |
119 | | - .filter( |
120 | | - (function_) => |
121 | | - function_.schema === schema.name && function_.return_type !== 'trigger' |
122 | | - ) |
123 | | - .map( |
124 | | - (function_) => `${JSON.stringify(function_.name)}: { |
125 | | - Args: ${(() => { |
126 | | - if (function_.argument_types === '') { |
127 | | - return 'Record<PropertyKey, never>' |
128 | | - } |
129 | | -
|
130 | | - const splitArgs = function_.argument_types.split(',').map((arg) => arg.trim()) |
131 | | - if (splitArgs.some((arg) => arg.includes('"') || !arg.includes(' '))) { |
132 | | - return 'Record<string, unknown>' |
133 | | - } |
134 | | -
|
135 | | - const argsNameAndType = splitArgs.map((arg) => { |
136 | | - const [name, ...rest] = arg.split(' ') |
137 | | - const type = types.find((_type) => _type.format === rest.join(' ')) |
138 | | - if (!type) { |
139 | | - return { name, type: 'unknown' } |
140 | | - } |
141 | | - return { name, type: pgTypeToTsType(type.name, types) } |
142 | | - }) |
143 | | -
|
144 | | - return `{ ${argsNameAndType.map( |
145 | | - ({ name, type }) => `${JSON.stringify(name)}: ${type}` |
146 | | - )} }` |
147 | | - })()} |
148 | | - Returns: ${pgTypeToTsType(function_.return_type, types)} |
149 | | - }` |
150 | | - )} |
151 | | - } |
152 | | - }` |
153 | | - )} |
154 | | -}` |
155 | | - |
156 | | - output = prettier.format(output, { |
157 | | - parser: 'typescript', |
| 48 | + return applyTypescriptTemplate({ |
| 49 | + schemas: schemas.filter(({ name }) => !excludedSchemas.includes(name)), |
| 50 | + tables, |
| 51 | + functions, |
| 52 | + types, |
158 | 53 | }) |
159 | | - return output |
160 | 54 | }) |
161 | 55 | } |
162 | | - |
163 | | -// TODO: Make this more robust. Currently doesn't handle composite types - returns them as unknown. |
164 | | -const pgTypeToTsType = (pgType: string, types: PostgresType[]): string => { |
165 | | - if (pgType === 'bool') { |
166 | | - return 'boolean' |
167 | | - } else if (['int2', 'int4', 'int8', 'float4', 'float8', 'numeric'].includes(pgType)) { |
168 | | - return 'number' |
169 | | - } else if ( |
170 | | - [ |
171 | | - 'bytea', |
172 | | - 'bpchar', |
173 | | - 'varchar', |
174 | | - 'date', |
175 | | - 'text', |
176 | | - 'time', |
177 | | - 'timetz', |
178 | | - 'timestamp', |
179 | | - 'timestamptz', |
180 | | - 'uuid', |
181 | | - ].includes(pgType) |
182 | | - ) { |
183 | | - return 'string' |
184 | | - } else if (['json', 'jsonb'].includes(pgType)) { |
185 | | - return 'Json' |
186 | | - } else if (pgType === 'void') { |
187 | | - return 'undefined' |
188 | | - } else if (pgType === 'record') { |
189 | | - return 'Record<string, unknown>[]' |
190 | | - } else if (pgType.startsWith('_')) { |
191 | | - return pgTypeToTsType(pgType.substring(1), types) + '[]' |
192 | | - } else { |
193 | | - const type = types.find((type) => type.name === pgType && type.enums.length > 0) |
194 | | - if (type) { |
195 | | - return type.enums.map((variant) => JSON.stringify(variant)).join('|') |
196 | | - } |
197 | | - |
198 | | - return 'unknown' |
199 | | - } |
200 | | -} |
0 commit comments