1
+ import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'
1
2
import { Type } from '@sinclair/typebox'
2
- import { FastifyInstance } from 'fastify'
3
3
import { PostgresMeta } from '../../lib/index.js'
4
4
import {
5
- PostgresSchemaCreate ,
6
- PostgresSchemaUpdate ,
7
5
postgresSchemaSchema ,
8
6
postgresSchemaCreateSchema ,
9
7
postgresSchemaUpdateSchema ,
10
8
} from '../../lib/types.js'
11
9
import { DEFAULT_POOL_CONFIG } from '../constants.js'
12
10
import { extractRequestForLogging } from '../utils.js'
13
11
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 (
23
14
'/' ,
24
15
{
25
16
schema : {
26
17
headers : Type . Object ( {
27
18
pg : Type . String ( ) ,
28
19
} ) ,
29
20
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 ( ) ) ,
33
24
} ) ,
34
25
response : {
35
26
200 : Type . Array ( postgresSchemaSchema ) ,
@@ -41,7 +32,7 @@ export default async (fastify: FastifyInstance) => {
41
32
} ,
42
33
async ( request , reply ) => {
43
34
const connectionString = request . headers . pg
44
- const includeSystemSchemas = request . query . include_system_schemas === 'true'
35
+ const includeSystemSchemas = request . query . include_system_schemas
45
36
const limit = request . query . limit
46
37
const offset = request . query . offset
47
38
@@ -58,20 +49,15 @@ export default async (fastify: FastifyInstance) => {
58
49
}
59
50
)
60
51
61
- fastify . get < {
62
- Headers : { pg : string }
63
- Params : {
64
- id : string
65
- }
66
- } > (
52
+ fastify . get (
67
53
'/:id(\\d+)' ,
68
54
{
69
55
schema : {
70
56
headers : Type . Object ( {
71
57
pg : Type . String ( ) ,
72
58
} ) ,
73
59
params : Type . Object ( {
74
- id : Type . RegEx ( / \d + / ) ,
60
+ id : Type . Integer ( ) ,
75
61
} ) ,
76
62
response : {
77
63
200 : postgresSchemaSchema ,
@@ -83,7 +69,7 @@ export default async (fastify: FastifyInstance) => {
83
69
} ,
84
70
async ( request , reply ) => {
85
71
const connectionString = request . headers . pg
86
- const id = Number ( request . params . id )
72
+ const id = request . params . id
87
73
88
74
const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
89
75
const { data, error } = await pgMeta . schemas . retrieve ( { id } )
@@ -98,10 +84,7 @@ export default async (fastify: FastifyInstance) => {
98
84
}
99
85
)
100
86
101
- fastify . post < {
102
- Headers : { pg : string }
103
- Body : PostgresSchemaCreate
104
- } > (
87
+ fastify . post (
105
88
'/' ,
106
89
{
107
90
schema : {
@@ -133,21 +116,15 @@ export default async (fastify: FastifyInstance) => {
133
116
}
134
117
)
135
118
136
- fastify . patch < {
137
- Headers : { pg : string }
138
- Params : {
139
- id : string
140
- }
141
- Body : PostgresSchemaUpdate
142
- } > (
119
+ fastify . patch (
143
120
'/:id(\\d+)' ,
144
121
{
145
122
schema : {
146
123
headers : Type . Object ( {
147
124
pg : Type . String ( ) ,
148
125
} ) ,
149
126
params : Type . Object ( {
150
- id : Type . RegEx ( / \d + / ) ,
127
+ id : Type . Integer ( ) ,
151
128
} ) ,
152
129
body : postgresSchemaUpdateSchema ,
153
130
response : {
@@ -163,7 +140,7 @@ export default async (fastify: FastifyInstance) => {
163
140
} ,
164
141
async ( request , reply ) => {
165
142
const connectionString = request . headers . pg
166
- const id = Number ( request . params . id )
143
+ const id = request . params . id
167
144
168
145
const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
169
146
const { data, error } = await pgMeta . schemas . update ( id , request . body )
@@ -179,26 +156,18 @@ export default async (fastify: FastifyInstance) => {
179
156
}
180
157
)
181
158
182
- fastify . delete < {
183
- Headers : { pg : string }
184
- Params : {
185
- id : string
186
- }
187
- Querystring : {
188
- cascade ?: string
189
- }
190
- } > (
159
+ fastify . delete (
191
160
'/:id(\\d+)' ,
192
161
{
193
162
schema : {
194
163
headers : Type . Object ( {
195
164
pg : Type . String ( ) ,
196
165
} ) ,
197
166
params : Type . Object ( {
198
- id : Type . RegEx ( / \d + / ) ,
167
+ id : Type . Integer ( ) ,
199
168
} ) ,
200
169
querystring : Type . Object ( {
201
- cascade : Type . Optional ( Type . String ( ) ) ,
170
+ cascade : Type . Optional ( Type . Boolean ( ) ) ,
202
171
} ) ,
203
172
response : {
204
173
200 : postgresSchemaSchema ,
@@ -213,8 +182,8 @@ export default async (fastify: FastifyInstance) => {
213
182
} ,
214
183
async ( request , reply ) => {
215
184
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
218
187
219
188
const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
220
189
const { data, error } = await pgMeta . schemas . remove ( id , { cascade } )
@@ -230,3 +199,4 @@ export default async (fastify: FastifyInstance) => {
230
199
}
231
200
)
232
201
}
202
+ export default route
0 commit comments