@@ -44,6 +44,34 @@ import type {
4444import { type DB , escapeSingleQuotes , isPgArrayType } from '../utils' ;
4545import { getColumnCasing , sqlToStr } from './utils' ;
4646
47+ export type PgIntrospectOptions = {
48+ tableConcurrency ?: number ;
49+ } ;
50+
51+ async function mapWithConcurrency < T > (
52+ items : T [ ] ,
53+ concurrency : number | undefined ,
54+ fn : ( item : T ) => Promise < unknown > ,
55+ ) {
56+ if ( concurrency === undefined || concurrency < 1 ) {
57+ await Promise . all ( items . map ( fn ) ) ;
58+ return ;
59+ }
60+
61+ let nextIndex = 0 ;
62+ const workerCount = Math . min ( Math . floor ( concurrency ) , items . length ) ;
63+
64+ await Promise . all (
65+ Array . from ( { length : workerCount } , async ( ) => {
66+ while ( nextIndex < items . length ) {
67+ const currentIndex = nextIndex ;
68+ nextIndex += 1 ;
69+ await fn ( items [ currentIndex ] ) ;
70+ }
71+ } ) ,
72+ ) ;
73+ }
74+
4775export const indexName = ( tableName : string , columns : string [ ] ) => {
4876 return `${ tableName } _${ columns . join ( '_' ) } _index` ;
4977} ;
@@ -983,6 +1011,7 @@ export const fromDatabase = async (
9831011 status : IntrospectStatus ,
9841012 ) => void ,
9851013 tsSchema ?: PgSchemaInternal ,
1014+ options : PgIntrospectOptions = { } ,
9861015) : Promise < PgSchemaInternal > => {
9871016 const result : Record < string , Table > = { } ;
9881017 const views : Record < string , View > = { } ;
@@ -1209,13 +1238,20 @@ WHERE
12091238
12101239 const sequencesInColumns : string[ ] = [ ] ;
12111240
1212- const all = allTables
1213- . filter ( ( it ) => it . type === 'table' )
1214- . map ( ( row ) => {
1241+ const tableRows = allTables . filter ( ( it ) => it . type === 'table' ) ;
1242+ tableCount = tableRows . filter ( ( row ) => tablesFilter ( row . table_name as string ) ) . length ;
1243+
1244+ if ( progressCallback ) {
1245+ progressCallback ( 'tables' , tableCount , 'done' ) ;
1246+ }
1247+
1248+ await mapWithConcurrency (
1249+ tableRows ,
1250+ options . tableConcurrency ,
1251+ async ( row ) => {
12151252 return new Promise ( async ( res , rej ) => {
12161253 const tableName = row . table_name as string ;
12171254 if ( ! tablesFilter ( tableName ) ) return res ( '' ) ;
1218- tableCount += 1 ;
12191255 const tableSchema = row . table_schema ;
12201256
12211257 try {
@@ -1668,18 +1704,14 @@ WHERE
16681704 }
16691705 res ( '' ) ;
16701706 } ) ;
1671- } ) ;
1672-
1673- if ( progressCallback ) {
1674- progressCallback ( 'tables' , tableCount , 'done' ) ;
1675- }
1676-
1677- for await ( const _ of all ) {
1678- }
1707+ } ,
1708+ ) ;
16791709
1680- const allViews = allTables
1681- . filter ( ( it ) => it . type === 'view' || it . type === 'materialized_view' )
1682- . map ( ( row ) => {
1710+ const viewRows = allTables . filter ( ( it ) => it . type === 'view' || it . type === 'materialized_view' ) ;
1711+ const allViews = mapWithConcurrency (
1712+ viewRows ,
1713+ options . tableConcurrency ,
1714+ async ( row ) => {
16831715 return new Promise ( async ( res , rej ) => {
16841716 const viewName = row . table_name as string ;
16851717 if ( ! tablesFilter ( viewName ) ) return res ( '' ) ;
@@ -1899,12 +1931,12 @@ WHERE
18991931 }
19001932 res ( '' ) ;
19011933 } ) ;
1902- } ) ;
1934+ } ,
1935+ ) ;
19031936
1904- viewsCount = allViews . length ;
1937+ viewsCount = viewRows . length ;
19051938
1906- for await ( const _ of allViews ) {
1907- }
1939+ await allViews ;
19081940
19091941 if ( progressCallback ) {
19101942 progressCallback ( 'columns' , columnsCount , 'done' ) ;
0 commit comments