1+ import { Type } from '@sinclair/typebox'
12import { FastifyInstance } from 'fastify'
23import { PostgresMeta } from '../../lib'
4+ import {
5+ PostgresColumnCreate ,
6+ postgresColumnSchema ,
7+ postgresColumnCreateSchema ,
8+ } from '../../lib/types'
39import { DEFAULT_POOL_CONFIG } from '../constants'
410import { extractRequestForLogging } from '../utils'
511
@@ -33,7 +39,6 @@ export default async (fastify: FastifyInstance) => {
3339 return data
3440 } )
3541
36- // deprecated: use GET /batch instead
3742 fastify . get < {
3843 Headers : { pg : string }
3944 Params : {
@@ -55,32 +60,51 @@ export default async (fastify: FastifyInstance) => {
5560 return data
5661 } )
5762
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
6063 fastify . post < {
6164 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
6987
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+ }
78101
79- if ( Array . isArray ( request . body ) ) {
80- return data
102+ if ( Array . isArray ( request . body ) ) {
103+ return data
104+ }
105+ return data [ 0 ]
81106 }
82- return data [ 0 ]
83- } )
107+ )
84108
85109 fastify . patch < {
86110 Headers : { pg : string }
0 commit comments