1+ import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'
12import { Type } from '@sinclair/typebox'
2- import { FastifyInstance } from 'fastify'
33import { PostgresMeta } from '../../lib/index.js'
44import {
5- PostgresSchemaCreate ,
6- PostgresSchemaUpdate ,
75 postgresSchemaSchema ,
86 postgresSchemaCreateSchema ,
97 postgresSchemaUpdateSchema ,
108} from '../../lib/types.js'
119import { DEFAULT_POOL_CONFIG } from '../constants.js'
1210import { extractRequestForLogging } from '../utils.js'
1311
14- export default async ( fastify : FastifyInstance ) => {
15- fastify . get < {
16- Headers : { pg : string }
17- Querystring : {
18- include_system_schemas ?: string
19- limit ?: number
20- offset ?: number
21- }
22- } > (
12+ const route : FastifyPluginAsyncTypebox = async ( fastify ) => {
13+ fastify . get (
2314 '/' ,
2415 {
2516 schema : {
2617 headers : Type . Object ( {
2718 pg : Type . String ( ) ,
2819 } ) ,
2920 querystring : Type . Object ( {
30- include_system_schemas : Type . Optional ( Type . String ( ) ) ,
31- limit : Type . Optional ( Type . String ( ) ) ,
32- offset : Type . Optional ( Type . String ( ) ) ,
21+ include_system_schemas : Type . Optional ( Type . Boolean ( ) ) ,
22+ limit : Type . Optional ( Type . Integer ( ) ) ,
23+ offset : Type . Optional ( Type . Integer ( ) ) ,
3324 } ) ,
3425 response : {
3526 200 : Type . Array ( postgresSchemaSchema ) ,
@@ -41,7 +32,7 @@ export default async (fastify: FastifyInstance) => {
4132 } ,
4233 async ( request , reply ) => {
4334 const connectionString = request . headers . pg
44- const includeSystemSchemas = request . query . include_system_schemas === 'true'
35+ const includeSystemSchemas = request . query . include_system_schemas
4536 const limit = request . query . limit
4637 const offset = request . query . offset
4738
@@ -58,20 +49,15 @@ export default async (fastify: FastifyInstance) => {
5849 }
5950 )
6051
61- fastify . get < {
62- Headers : { pg : string }
63- Params : {
64- id : string
65- }
66- } > (
52+ fastify . get (
6753 '/:id(\\d+)' ,
6854 {
6955 schema : {
7056 headers : Type . Object ( {
7157 pg : Type . String ( ) ,
7258 } ) ,
7359 params : Type . Object ( {
74- id : Type . RegEx ( / \d + / ) ,
60+ id : Type . Integer ( ) ,
7561 } ) ,
7662 response : {
7763 200 : postgresSchemaSchema ,
@@ -83,7 +69,7 @@ export default async (fastify: FastifyInstance) => {
8369 } ,
8470 async ( request , reply ) => {
8571 const connectionString = request . headers . pg
86- const id = Number ( request . params . id )
72+ const id = request . params . id
8773
8874 const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
8975 const { data, error } = await pgMeta . schemas . retrieve ( { id } )
@@ -98,10 +84,7 @@ export default async (fastify: FastifyInstance) => {
9884 }
9985 )
10086
101- fastify . post < {
102- Headers : { pg : string }
103- Body : PostgresSchemaCreate
104- } > (
87+ fastify . post (
10588 '/' ,
10689 {
10790 schema : {
@@ -133,21 +116,15 @@ export default async (fastify: FastifyInstance) => {
133116 }
134117 )
135118
136- fastify . patch < {
137- Headers : { pg : string }
138- Params : {
139- id : string
140- }
141- Body : PostgresSchemaUpdate
142- } > (
119+ fastify . patch (
143120 '/:id(\\d+)' ,
144121 {
145122 schema : {
146123 headers : Type . Object ( {
147124 pg : Type . String ( ) ,
148125 } ) ,
149126 params : Type . Object ( {
150- id : Type . RegEx ( / \d + / ) ,
127+ id : Type . Integer ( ) ,
151128 } ) ,
152129 body : postgresSchemaUpdateSchema ,
153130 response : {
@@ -163,7 +140,7 @@ export default async (fastify: FastifyInstance) => {
163140 } ,
164141 async ( request , reply ) => {
165142 const connectionString = request . headers . pg
166- const id = Number ( request . params . id )
143+ const id = request . params . id
167144
168145 const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
169146 const { data, error } = await pgMeta . schemas . update ( id , request . body )
@@ -179,26 +156,18 @@ export default async (fastify: FastifyInstance) => {
179156 }
180157 )
181158
182- fastify . delete < {
183- Headers : { pg : string }
184- Params : {
185- id : string
186- }
187- Querystring : {
188- cascade ?: string
189- }
190- } > (
159+ fastify . delete (
191160 '/:id(\\d+)' ,
192161 {
193162 schema : {
194163 headers : Type . Object ( {
195164 pg : Type . String ( ) ,
196165 } ) ,
197166 params : Type . Object ( {
198- id : Type . RegEx ( / \d + / ) ,
167+ id : Type . Integer ( ) ,
199168 } ) ,
200169 querystring : Type . Object ( {
201- cascade : Type . Optional ( Type . String ( ) ) ,
170+ cascade : Type . Optional ( Type . Boolean ( ) ) ,
202171 } ) ,
203172 response : {
204173 200 : postgresSchemaSchema ,
@@ -213,8 +182,8 @@ export default async (fastify: FastifyInstance) => {
213182 } ,
214183 async ( request , reply ) => {
215184 const connectionString = request . headers . pg
216- const id = Number ( request . params . id )
217- const cascade = request . query . cascade === 'true'
185+ const id = request . params . id
186+ const cascade = request . query . cascade
218187
219188 const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
220189 const { data, error } = await pgMeta . schemas . remove ( id , { cascade } )
@@ -230,3 +199,4 @@ export default async (fastify: FastifyInstance) => {
230199 }
231200 )
232201}
202+ export default route
0 commit comments