1
- import { db } from './db' ;
2
1
import { TableSchema , ColumnType } from './types' ;
3
2
import { Table } from '..' ;
4
3
import Prettier from 'prettier' ;
5
4
import { defaults , flatMap } from 'lodash' ;
6
5
import { makeIntrospectionQuery } from './introspectionQuery' ;
7
6
import { xByY , xByYAndZ , parseTags } from './utils' ;
7
+ import { IDatabase } from 'pg-promise' ;
8
8
9
9
// @ts -ignore
10
10
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -215,13 +215,44 @@ type PgIntrospectionResultsByKind = PgIntrospectionOriginalResultsByKind & {
215
215
typeById : { [ typeId : string ] : PgType } ;
216
216
} ;
217
217
218
- export async function getPostgresVersion ( ) : Promise < number > {
218
+ export function schemaToTableObj ( schema : TableSchema ) : Table {
219
+ return {
220
+ name : schema . tableName ,
221
+ columns : Object . fromEntries (
222
+ schema . schema . map ( ( s ) => {
223
+ return [ s . column_name , { type : s . udt_name , notNull : s . is_nullable === 'NO' } ] ;
224
+ } ) ,
225
+ ) ,
226
+ } ;
227
+ }
228
+
229
+ export function tableObjToTSCode ( table : Table , opts ?: Prettier . Options ) : string {
230
+ const typeName = table . name . slice ( 0 , 1 ) . toLocaleUpperCase ( ) + table . name . slice ( 1 ) ;
231
+ const code = `export const ${ typeName } = ${ JSON . stringify ( table ) } as const;` ;
232
+ const defaultOptions : Prettier . Options = {
233
+ semi : true ,
234
+ singleQuote : true ,
235
+ trailingComma : 'all' ,
236
+ printWidth : 100 ,
237
+ tabWidth : 2 ,
238
+ useTabs : false ,
239
+ } as const ;
240
+ const options = defaults ( { } , opts , defaultOptions ) ;
241
+ return Prettier . format ( code , {
242
+ ...options ,
243
+ parser : 'typescript' ,
244
+ } ) ;
245
+ }
246
+
247
+ export async function getPostgresVersion ( db : IDatabase < any > ) : Promise < number > {
219
248
const versionResult = await db . one ( 'show server_version_num;' ) ;
220
249
return Number . parseInt ( versionResult . server_version_num , 10 ) ;
221
250
}
222
251
223
- export async function runIntrospectionQuery ( ) : Promise < PgIntrospectionResultsByKind > {
224
- const version = await getPostgresVersion ( ) ;
252
+ export async function runIntrospectionQuery (
253
+ db : IDatabase < any > ,
254
+ ) : Promise < PgIntrospectionResultsByKind > {
255
+ const version = await getPostgresVersion ( db ) ;
225
256
const sql = makeIntrospectionQuery ( version ) ;
226
257
const kinds = [
227
258
'namespace' ,
@@ -293,15 +324,15 @@ export async function runIntrospectionQuery(): Promise<PgIntrospectionResultsByK
293
324
return Object . freeze ( result ) ;
294
325
}
295
326
296
- export async function introspectSchemas ( ) {
297
- const intro = await runIntrospectionQuery ( ) ;
327
+ export async function introspectSchemas ( db : IDatabase < any > ) {
328
+ const intro = await runIntrospectionQuery ( db ) ;
298
329
const namespaceIds = intro . namespace . map ( ( n ) => n . id ) ;
299
330
const classes = intro . class . filter ( ( c ) => namespaceIds . includes ( c . namespaceId ) ) ;
300
331
return { classes, intro } ;
301
332
}
302
333
303
- export async function getTablesSchemas ( ) : Promise < Array < TableSchema > > {
304
- const { classes, intro } = await introspectSchemas ( ) ;
334
+ export async function getTablesSchemas ( db : IDatabase < any > ) : Promise < Array < TableSchema > > {
335
+ const { classes, intro } = await introspectSchemas ( db ) ;
305
336
306
337
return classes . map ( ( klass ) => {
307
338
return {
@@ -317,39 +348,10 @@ export async function getTablesSchemas(): Promise<Array<TableSchema>> {
317
348
} ) ;
318
349
}
319
350
320
- export function schemaToTableObj ( schema : TableSchema ) : Table {
321
- return {
322
- name : schema . tableName ,
323
- columns : Object . fromEntries (
324
- schema . schema . map ( ( s ) => {
325
- return [ s . column_name , { type : s . udt_name , notNull : s . is_nullable === 'NO' } ] ;
326
- } ) ,
327
- ) ,
328
- } ;
329
- }
330
-
331
- export function tableObjToTSCode ( table : Table , opts ?: Prettier . Options ) : string {
332
- const typeName = table . name . slice ( 0 , 1 ) . toLocaleUpperCase ( ) + table . name . slice ( 1 ) ;
333
- const code = `export const ${ typeName } = ${ JSON . stringify ( table ) } as const;` ;
334
- const defaultOptions : Prettier . Options = {
335
- semi : true ,
336
- singleQuote : true ,
337
- trailingComma : 'all' ,
338
- printWidth : 100 ,
339
- tabWidth : 2 ,
340
- useTabs : false ,
341
- } as const ;
342
- const options = defaults ( { } , opts , defaultOptions ) ;
343
- return Prettier . format ( code , {
344
- ...options ,
345
- parser : 'typescript' ,
346
- } ) ;
347
- }
348
-
349
- export async function generateTSCodeForAllSchemas ( ) {
351
+ export async function generateTSCodeForAllSchemas ( db : IDatabase < any > ) {
350
352
// @ts -ignore
351
353
// eslint-disable-next-line @typescript-eslint/no-unused-vars
352
- const schemas = await getTablesSchemas ( ) ;
354
+ const schemas = await getTablesSchemas ( db ) ;
353
355
const allModelsCode = schemas
354
356
. map ( schemaToTableObj )
355
357
. map ( ( obj ) => tableObjToTSCode ( obj ) )
0 commit comments