Skip to content

Commit

Permalink
update types
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Aug 10, 2023
1 parent 04aeb3b commit 2397150
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
6 changes: 3 additions & 3 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const postgresColumnCreateSchema = Type.Object({
table_id: Type.Integer(),
name: Type.String(),
type: Type.String(),
default_value: Type.Optional(Type.String()),
default_value: Type.Optional(Type.Unknown()),
default_value_format: Type.Optional(
Type.Union([Type.Literal('expression'), Type.Literal('literal')])
),
Expand All @@ -67,7 +67,7 @@ export const postgresColumnUpdateSchema = Type.Object({
name: Type.Optional(Type.String()),
type: Type.Optional(Type.String()),
drop_default: Type.Optional(Type.Boolean()),
default_value: Type.Optional(Type.String()),
default_value: Type.Optional(Type.Unknown()),
default_value_format: Type.Optional(
Type.Union([Type.Literal('expression'), Type.Literal('literal')])
),
Expand All @@ -78,7 +78,7 @@ export const postgresColumnUpdateSchema = Type.Object({
is_nullable: Type.Optional(Type.Boolean()),
is_unique: Type.Optional(Type.Boolean()),
comment: Type.Optional(Type.String()),
check: Type.Optional(Type.String()),
check: Type.Optional(Type.Union([Type.String(), Type.Null()])),
})
export type PostgresColumnUpdate = Static<typeof postgresColumnUpdateSchema>

Expand Down
69 changes: 36 additions & 33 deletions src/server/routes/columns.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FastifyInstance } from 'fastify'
import { PostgresMeta } from '../../lib/index.js'
import { DEFAULT_POOL_CONFIG } from '../constants.js'
import { extractRequestForLogging } from '../utils.js'
Expand Down Expand Up @@ -59,38 +58,7 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
return data
}
)
fastify.post(
'/',
{
schema: {
headers: Type.Object({
pg: Type.String(),
}),
body: postgresColumnCreateSchema,
response: {
200: postgresColumnSchema,
400: Type.Object({
error: Type.String(),
}),
},
},
},
async (request, reply) => {
const connectionString = request.headers.pg

const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
const { data, error } = await pgMeta.columns.create(request.body as any)
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 }
}

return data
}
)
fastify.get(
'/:tableId(^\\d+):ordinalPosition',
{
Expand Down Expand Up @@ -164,6 +132,40 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
}
}
)

fastify.post(
'/',
{
schema: {
headers: Type.Object({
pg: Type.String(),
}),
body: postgresColumnCreateSchema,
response: {
200: postgresColumnSchema,
400: Type.Object({
error: Type.String(),
}),
},
},
},
async (request, reply) => {
const connectionString = request.headers.pg

const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
const { data, error } = await pgMeta.columns.create(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 }
}

return data
}
)

fastify.patch(
'/:id(\\d+\\.\\d+)',
{
Expand All @@ -187,7 +189,7 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
const connectionString = request.headers.pg

const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
const { data, error } = await pgMeta.columns.update(request.params.id, request.body as any)
const { data, error } = await pgMeta.columns.update(request.params.id, request.body)
await pgMeta.end()
if (error) {
request.log.error({ error, request: extractRequestForLogging(request) })
Expand All @@ -199,6 +201,7 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
return data
}
)

fastify.delete(
'/:id(\\d+\\.\\d+)',
{
Expand Down

0 comments on commit 2397150

Please sign in to comment.