@@ -6,7 +6,8 @@ const dotenv = require('dotenv');
6
6
const pgQuery = async function ( fetchedTables ) {
7
7
//reads the .env config after latest update, allowing us to use new tables if a previous import has run
8
8
dotenv . config ( ) ;
9
-
9
+ const URI = process . env . DB_URI
10
+
10
11
const tableQuery = `
11
12
SELECT table_name,
12
13
ordinal_position as position,
@@ -28,18 +29,13 @@ const pgQuery = async function(fetchedTables) {
28
29
FROM information_schema.constraint_column_usage
29
30
WHERE table_schema not in ('information_schema', 'pg_catalog') AND
30
31
table_name not in ('pg_stat_statements')
31
- ORDER BY table_name;`
32
-
33
- let tableColumns = { } ;
34
-
35
- //this is not updating!!
36
- const URI = process . env . DB_URI
32
+ ORDER BY table_name;`
37
33
38
34
const pool = new Pool ( {
39
35
connectionString : URI ,
40
36
ssl : true ,
41
37
} )
42
- console . log ( 'db uri pre pool.connect:' , URI )
38
+ // console.log('db uri pre pool.connect:', URI)
43
39
44
40
pool . connect ( ( err , client , done ) => {
45
41
if ( err ) return console . log ( `Error connecting to db, ${ err } ` ) ;
@@ -48,92 +44,90 @@ const pgQuery = async function(fetchedTables) {
48
44
} )
49
45
50
46
let queryResults = await pool . query ( tableQuery )
51
- . then ( res => {
52
- console . log ( "pool: " , pool )
53
- console . log ( 'db URI: ' , URI )
54
- console . log ( 'tableQuery: ' , res . rows )
55
- tableColumns = res . rows ;
56
- return tableColumns ;
57
- } )
58
- . then ( tableColumns => {
59
- return pool . query ( constraintQuery )
60
- . then ( res => {
61
- // console.log('constraintQuery: ',res.rows)
62
- const result = res . rows ;
63
- for ( let item of result ) {
64
- // console.log("tablecolumns: ", tableColumns)
65
- let table = item . table_name ;
66
- let col = item . column_name ;
67
- for ( let column of tableColumns ) {
68
- // console.log('column: ', column, "col: ", col)
69
- if ( column . table_name == table && column . column_name == col ) {
70
- column . constraint = item . constraint_name ;
47
+ . then ( res => {
48
+ // console.log("pool: ",pool)
49
+ // console.log('db URI: ', URI)
50
+ // console.log('tableQuery: ',res.rows)
51
+ const tableColumns = res . rows ;
52
+ return tableColumns ;
53
+ } )
54
+ . then ( tableColumns => {
55
+ return pool . query ( constraintQuery )
56
+ . then ( res => {
57
+ // console.log('constraintQuery: ',res.rows)
58
+ const result = res . rows ;
59
+ for ( let item of result ) {
60
+ // console.log("tablecolumns: ", tableColumns)
61
+ let table = item . table_name ;
62
+ let col = item . column_name ;
63
+ for ( let column of tableColumns ) {
64
+ // console.log('column: ', column, "col: ", col)
65
+ if ( column . table_name == table && column . column_name == col ) {
66
+ column . constraint = item . constraint_name ;
67
+ }
71
68
}
72
69
}
73
- }
74
- const tables = { } ;
75
- let index = - 1 ;
76
- tableColumns . forEach ( ( item ) => {
77
- //if table type matches index, do not create new table index
78
- if ( ! tables [ index ] || tables [ index ] . type !== item . table_name ) {
79
- //increment index to move on to new table
80
- index ++
81
- tables [ index ] = { } ;
82
- tables [ index ] . type = item . table_name ;
83
- tables [ index ] . fields = { } ;
84
- tables [ index ] . fieldIndex = 0 ;
85
- tables [ index ] . tableID = index . toString ( ) ;
86
- tables [ index ] . fields = [ ] ;
87
- }
88
- tables [ index ] . fieldIndex ++ ;
89
-
90
- // tables[index].fields[position] = {};
91
- const column = { } ;
92
- column . name = item . column_name ;
93
-
94
- //assigns column data types from Postgres to match our app's data types
95
- switch ( item . data_type ) {
96
- case "character varying" :
97
- column . type = "String" ;
98
- break ;
99
- case "integer" :
100
- column . type = "Int" ;
101
- break ;
102
- case "serial" :
103
- column . type = "ID" ;
104
- break ;
105
- case "boolean" :
106
- column . type = "Boolean"
70
+ const tables = { } ;
71
+ let index = - 1 ;
72
+ tableColumns . forEach ( ( item ) => {
73
+ //if table type matches index, do not create new table index
74
+ if ( ! tables [ index ] || tables [ index ] . type !== item . table_name ) {
75
+ //increment index to move on to new table
76
+ index ++
77
+ tables [ index ] = { } ;
78
+ tables [ index ] . type = item . table_name ;
79
+ tables [ index ] . fields = { } ;
80
+ tables [ index ] . fieldIndex = 0 ;
81
+ tables [ index ] . tableID = index . toString ( ) ;
82
+ tables [ index ] . fields = [ ] ;
107
83
}
108
- column . required = item . is_nullable == "YES" ? true : false ;
109
- column . tableNum = index . toString ( ) ;
110
- column . fieldNum = item . position ;
111
- column . defaultValue = item . default_value ? item . default_value : "" ;
112
- //values that need to be fetched from db
113
- column . primaryKey = false ;
114
- column . unique = false ;
115
- column . relationSelected = false ;
116
- column . relation = {
117
- tableIndex : - 1 ,
118
- fieldIndex : - 1 ,
119
- refType : ''
120
- } ,
121
- column . queryable = true ;
84
+ tables [ index ] . fieldIndex ++ ;
122
85
123
- tables [ index ] . fields . push ( column )
124
- } )
125
- console . log ( "tables (pgquery): " , tables )
126
- fetchedTables = tables ;
127
- return fetchedTables ;
128
- // console.log('final table column obj: ', tableColumns)
86
+ // tables[index].fields[position] = {};
87
+ const column = { } ;
88
+ column . name = item . column_name ;
89
+
90
+ //assigns column data types from Postgres to match our app's data types
91
+ switch ( item . data_type ) {
92
+ case "character varying" :
93
+ column . type = "String" ;
94
+ break ;
95
+ case "integer" :
96
+ column . type = "Int" ;
97
+ break ;
98
+ case "serial" :
99
+ column . type = "ID" ;
100
+ break ;
101
+ case "boolean" :
102
+ column . type = "Boolean"
103
+ }
104
+ column . required = item . is_nullable == "YES" ? true : false ;
105
+ column . tableNum = index . toString ( ) ;
106
+ column . fieldNum = item . position ;
107
+ column . defaultValue = item . default_value ? item . default_value : "" ;
108
+ //values that need to be fetched from db
109
+ column . primaryKey = false ;
110
+ column . unique = false ;
111
+ column . relationSelected = false ;
112
+ column . relation = {
113
+ tableIndex : - 1 ,
114
+ fieldIndex : - 1 ,
115
+ refType : ''
116
+ } ,
117
+ column . queryable = true ;
118
+
119
+ tables [ index ] . fields . push ( column )
120
+ } )
121
+ // console.log("tables (pgquery): ", tables)
122
+ fetchedTables = tables ;
123
+ return fetchedTables ;
124
+ // console.log('final table column obj: ', tableColumns)
125
+ } )
126
+
129
127
} )
130
-
131
- } )
132
- . catch ( err => console . error ( 'Error is: ' , err ) )
133
- let results = queryResults ;
134
- return results ;
128
+ . catch ( err => console . error ( 'Error is: ' , err ) )
129
+ return queryResults ;
135
130
}
136
131
137
132
module . exports = pgQuery ;
138
133
139
- // console.log("tableRes: ", tableRes)
0 commit comments