1
+ import { Type } from '@sinclair/typebox'
1
2
import { FastifyInstance } from 'fastify'
2
3
import { PostgresMeta } from '../../lib'
4
+ import {
5
+ PostgresColumnCreate ,
6
+ postgresColumnSchema ,
7
+ postgresColumnCreateSchema ,
8
+ } from '../../lib/types'
3
9
import { DEFAULT_POOL_CONFIG } from '../constants'
4
10
import { extractRequestForLogging } from '../utils'
5
11
@@ -33,7 +39,6 @@ export default async (fastify: FastifyInstance) => {
33
39
return data
34
40
} )
35
41
36
- // deprecated: use GET /batch instead
37
42
fastify . get < {
38
43
Headers : { pg : string }
39
44
Params : {
@@ -55,32 +60,51 @@ export default async (fastify: FastifyInstance) => {
55
60
return data
56
61
} )
57
62
58
- // deprecated: use POST /batch instead
59
- // TODO (darora): specifying a schema on the routes would both allow for validation, and enable us to mark methods as deprecated
60
63
fastify . post < {
61
64
Headers : { pg : string }
62
- Body : any
63
- } > ( '/' , async ( request , reply ) => {
64
- const connectionString = request . headers . pg
65
- const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
66
- if ( ! Array . isArray ( request . body ) ) {
67
- request . body = [ request . body ]
68
- }
65
+ Body : PostgresColumnCreate | PostgresColumnCreate [ ]
66
+ } > (
67
+ '/' ,
68
+ {
69
+ schema : {
70
+ headers : Type . Object ( {
71
+ pg : Type . String ( ) ,
72
+ } ) ,
73
+ body : Type . Union ( [ postgresColumnCreateSchema , Type . Array ( postgresColumnCreateSchema ) ] ) ,
74
+ response : {
75
+ 200 : Type . Union ( [ postgresColumnSchema , Type . Array ( postgresColumnSchema ) ] ) ,
76
+ 400 : Type . Object ( {
77
+ error : Type . String ( ) ,
78
+ } ) ,
79
+ 404 : Type . Object ( {
80
+ error : Type . String ( ) ,
81
+ } ) ,
82
+ } ,
83
+ } ,
84
+ } ,
85
+ async ( request , reply ) => {
86
+ const connectionString = request . headers . pg
69
87
70
- const { data, error } = await pgMeta . columns . batchCreate ( request . body )
71
- await pgMeta . end ( )
72
- if ( error ) {
73
- request . log . error ( { error, request : extractRequestForLogging ( request ) } )
74
- reply . code ( 400 )
75
- if ( error . message . startsWith ( 'Cannot find' ) ) reply . code ( 404 )
76
- return { error : error . message }
77
- }
88
+ const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
89
+ if ( ! Array . isArray ( request . body ) ) {
90
+ request . body = [ request . body ]
91
+ }
92
+
93
+ const { data, error } = await pgMeta . columns . batchCreate ( request . body )
94
+ await pgMeta . end ( )
95
+ if ( error ) {
96
+ request . log . error ( { error, request : extractRequestForLogging ( request ) } )
97
+ reply . code ( 400 )
98
+ if ( error . message . startsWith ( 'Cannot find' ) ) reply . code ( 404 )
99
+ return { error : error . message }
100
+ }
78
101
79
- if ( Array . isArray ( request . body ) ) {
80
- return data
102
+ if ( Array . isArray ( request . body ) ) {
103
+ return data
104
+ }
105
+ return data [ 0 ]
81
106
}
82
- return data [ 0 ]
83
- } )
107
+ )
84
108
85
109
fastify . patch < {
86
110
Headers : { pg : string }
0 commit comments