Skip to content

feat: batch endpoints for column creation and retrieval #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore(server): validate batch column create
  • Loading branch information
soedirgo committed Mar 20, 2022
commit cafcf99da15b87488fce5dcda7bd5dcea53afda3
68 changes: 46 additions & 22 deletions src/server/routes/columns.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Type } from '@sinclair/typebox'
import { FastifyInstance } from 'fastify'
import { PostgresMeta } from '../../lib'
import {
PostgresColumnCreate,
postgresColumnSchema,
postgresColumnCreateSchema,
} from '../../lib/types'
import { DEFAULT_POOL_CONFIG } from '../constants'
import { extractRequestForLogging } from '../utils'

Expand Down Expand Up @@ -33,7 +39,6 @@ export default async (fastify: FastifyInstance) => {
return data
})

// deprecated: use GET /batch instead
fastify.get<{
Headers: { pg: string }
Params: {
Expand All @@ -55,32 +60,51 @@ export default async (fastify: FastifyInstance) => {
return data
})

// deprecated: use POST /batch instead
// TODO (darora): specifying a schema on the routes would both allow for validation, and enable us to mark methods as deprecated
fastify.post<{
Headers: { pg: string }
Body: any
}>('/', async (request, reply) => {
const connectionString = request.headers.pg
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
if (!Array.isArray(request.body)) {
request.body = [request.body]
}
Body: PostgresColumnCreate | PostgresColumnCreate[]
}>(
'/',
{
schema: {
headers: Type.Object({
pg: Type.String(),
}),
body: Type.Union([postgresColumnCreateSchema, Type.Array(postgresColumnCreateSchema)]),
response: {
200: Type.Union([postgresColumnSchema, Type.Array(postgresColumnSchema)]),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we wanted to be really nice about it, would be cool to pluck the table name out of the array, since we don't allow multiple different values for the field here anyway

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to make the response format consistent: T | T[] for everything

400: Type.Object({
error: Type.String(),
}),
404: Type.Object({
error: Type.String(),
}),
},
},
},
async (request, reply) => {
const connectionString = request.headers.pg

const { data, error } = await pgMeta.columns.batchCreate(request.body)
await pgMeta.end()
if (error) {
request.log.error({ error, request: extractRequestForLogging(request) })
reply.code(400)
if (error.message.startsWith('Cannot find')) reply.code(404)
return { error: error.message }
}
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
if (!Array.isArray(request.body)) {
request.body = [request.body]
}

const { data, error } = await pgMeta.columns.batchCreate(request.body)
await pgMeta.end()
if (error) {
request.log.error({ error, request: extractRequestForLogging(request) })
reply.code(400)
if (error.message.startsWith('Cannot find')) reply.code(404)
return { error: error.message }
}

if (Array.isArray(request.body)) {
return data
if (Array.isArray(request.body)) {
return data
}
return data[0]
}
return data[0]
})
)

fastify.patch<{
Headers: { pg: string }
Expand Down