@@ -33,68 +33,62 @@ export default async (fastify: FastifyInstance) => {
33
33
return data
34
34
} )
35
35
36
- // HACK: Dark arts to get around https://github.com/delvedor/find-my-way/issues/285:
37
- // - this route has to be before /:tableId(^\\d+$)
38
- // - can't do :tableId(^\\d+$) instead of :tableId(^\\d+)
39
- // - need to separate :ordinalPosition as a 2nd param
40
- //
41
- // Anyhow, this probably just happens to work.
42
36
fastify . get < {
43
37
Headers : { pg : string }
44
38
Params : {
45
39
tableId : string
46
40
ordinalPosition : string
47
41
}
48
- } > ( '/:tableId(^\\d+).:ordinalPosition(^\\d+$)' , async ( request , reply ) => {
49
- const {
50
- headers : { pg : connectionString } ,
51
- params : { tableId, ordinalPosition } ,
52
- } = request
53
-
54
- const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
55
- const { data, error } = await pgMeta . columns . retrieve ( { id : `${ tableId } .${ ordinalPosition } ` } )
56
- await pgMeta . end ( )
57
- if ( error ) {
58
- request . log . error ( { error, request : extractRequestForLogging ( request ) } )
59
- reply . code ( 400 )
60
- if ( error . message . startsWith ( 'Cannot find' ) ) reply . code ( 404 )
61
- return { error : error . message }
62
- }
63
-
64
- return data
65
- } )
66
-
67
- fastify . get < {
68
- Headers : { pg : string }
69
- Params : { tableId : number }
70
42
Querystring : {
71
43
include_system_schemas ?: string
72
- limit ?: number
73
- offset ?: number
44
+ limit ?: string
45
+ offset ?: string
74
46
}
75
- } > ( '/:tableId(^\\d+$)' , async ( request , reply ) => {
76
- const {
77
- headers : { pg : connectionString } ,
78
- query : { limit, offset } ,
79
- params : { tableId } ,
80
- } = request
81
- const includeSystemSchemas = request . query . include_system_schemas === 'true'
82
-
83
- const pgMeta : PostgresMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
84
- const { data, error } = await pgMeta . columns . list ( {
85
- tableId,
86
- includeSystemSchemas,
87
- limit,
88
- offset,
89
- } )
90
- await pgMeta . end ( )
91
- if ( error ) {
92
- request . log . error ( { error, request : extractRequestForLogging ( request ) } )
93
- reply . code ( 500 )
94
- return { error : error . message }
47
+ } > ( '/:tableId(^\\d+):ordinalPosition' , async ( request , reply ) => {
48
+ if ( request . params . ordinalPosition === '' ) {
49
+ const {
50
+ headers : { pg : connectionString } ,
51
+ query : { limit, offset } ,
52
+ params : { tableId } ,
53
+ } = request
54
+ const includeSystemSchemas = request . query . include_system_schemas === 'true'
55
+
56
+ const pgMeta : PostgresMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
57
+ const { data, error } = await pgMeta . columns . list ( {
58
+ tableId : Number ( tableId ) ,
59
+ includeSystemSchemas,
60
+ limit : Number ( limit ) ,
61
+ offset : Number ( offset ) ,
62
+ } )
63
+ await pgMeta . end ( )
64
+ if ( error ) {
65
+ request . log . error ( { error, request : extractRequestForLogging ( request ) } )
66
+ reply . code ( 500 )
67
+ return { error : error . message }
68
+ }
69
+
70
+ return data
71
+ } else if ( / ^ \. \d + $ / . test ( request . params . ordinalPosition ) ) {
72
+ const {
73
+ headers : { pg : connectionString } ,
74
+ params : { tableId, ordinalPosition : ordinalPositionWithDot } ,
75
+ } = request
76
+ const ordinalPosition = ordinalPositionWithDot . slice ( 1 )
77
+
78
+ const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
79
+ const { data, error } = await pgMeta . columns . retrieve ( { id : `${ tableId } .${ ordinalPosition } ` } )
80
+ await pgMeta . end ( )
81
+ if ( error ) {
82
+ request . log . error ( { error, request : extractRequestForLogging ( request ) } )
83
+ reply . code ( 400 )
84
+ if ( error . message . startsWith ( 'Cannot find' ) ) reply . code ( 404 )
85
+ return { error : error . message }
86
+ }
87
+
88
+ return data
89
+ } else {
90
+ return reply . callNotFound ( )
95
91
}
96
-
97
- return data
98
92
} )
99
93
100
94
fastify . post < {
0 commit comments