Skip to content

Commit 7847af5

Browse files
committed
feat: migrate express -> fastify
1 parent 784f7c6 commit 7847af5

File tree

14 files changed

+841
-762
lines changed

14 files changed

+841
-762
lines changed

src/server/app.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1-
import express from 'express'
1+
import fastify from 'fastify'
22
import { PG_META_PORT } from './constants'
33
import routes from './routes'
44
import pkg from '../../package.json'
5-
import logger from './logger'
65

7-
const app = express()
8-
app.use(express.json())
9-
app.use(routes)
10-
app.get('/', (_req, res) =>
11-
res.status(200).json({
6+
const app = fastify({ logger: true, disableRequestLogging: true })
7+
8+
app.setErrorHandler((error, request, reply) => {
9+
app.log.error(JSON.stringify({ error, req: request.body }))
10+
reply.code(500).send({ error: error.message })
11+
})
12+
13+
app.setNotFoundHandler((request, reply) => {
14+
app.log.error(JSON.stringify({ error: 'Not found', req: request.body }))
15+
reply.code(404).send({ error: 'Not found' })
16+
})
17+
18+
app.register(require('fastify-cors'))
19+
app.register(routes)
20+
21+
app.get('/', async (_request, _reply) => {
22+
return {
1223
status: 200,
1324
name: pkg.name,
1425
version: pkg.version,
1526
documentation: 'https://github.com/supabase/postgres-meta',
16-
})
17-
)
18-
app.get('/health', (_req, res) => res.status(200).json({ date: new Date() }))
27+
}
28+
})
29+
30+
app.get('/health', async (_request, _reply) => {
31+
return { date: new Date() }
32+
})
33+
1934
app.listen(PG_META_PORT, () => {
20-
logger.info(`App started on port ${PG_META_PORT}`)
35+
app.log.info(`App started on port ${PG_META_PORT}`)
2136
})

src/server/logger.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/server/routes/columns.ts

Lines changed: 106 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,113 @@
1-
import { Router } from 'express'
2-
import logger from '../logger'
1+
import { FastifyInstance } from 'fastify'
32
import { PostgresMeta } from '../../lib'
43

5-
const router = Router()
6-
7-
router.get('/', async (req, res) => {
8-
const connectionString = req.headers?.pg?.toString() ?? ''
9-
const includeSystemSchemas = req.query?.include_system_schemas === 'true'
10-
11-
const pgMeta = new PostgresMeta({ connectionString, max: 1 })
12-
const { data, error } = await pgMeta.columns.list({
13-
includeSystemSchemas,
4+
export default async (fastify: FastifyInstance) => {
5+
fastify.get<{
6+
Headers: { pg: string }
7+
Querystring: {
8+
include_system_schemas?: string
9+
}
10+
}>('/', async (request, reply) => {
11+
const connectionString = request.headers.pg
12+
const includeSystemSchemas = request.query.include_system_schemas === 'true'
13+
14+
const pgMeta = new PostgresMeta({ connectionString, max: 1 })
15+
const { data, error } = await pgMeta.columns.list({
16+
includeSystemSchemas,
17+
})
18+
await pgMeta.end()
19+
if (error) {
20+
request.log.error(JSON.stringify({ error, req: request.body }))
21+
reply.code(500)
22+
return { error: error.message }
23+
}
24+
25+
return data
1426
})
15-
await pgMeta.end()
16-
if (error) {
17-
logger.error({ error, req: req.body })
18-
return res.status(500).json({ error: error.message })
19-
}
20-
21-
return res.status(200).json(data)
22-
})
23-
24-
router.get('/:id', async (req, res) => {
25-
const connectionString = req.headers?.pg?.toString() ?? ''
26-
27-
const pgMeta = new PostgresMeta({ connectionString, max: 1 })
28-
const { data, error } = await pgMeta.columns.retrieve({ id: req.params.id })
29-
await pgMeta.end()
30-
if (error) {
31-
logger.error({ error, req: req.body })
32-
let statusCode = 400
33-
if (error.message.startsWith('Cannot find')) statusCode = 404
34-
return res.status(statusCode).json({ error: error.message })
35-
}
36-
37-
return res.status(200).json(data)
38-
})
39-
40-
router.post('/', async (req, res) => {
41-
const connectionString = req.headers?.pg?.toString() ?? ''
42-
43-
const pgMeta = new PostgresMeta({ connectionString, max: 1 })
44-
const { data, error } = await pgMeta.columns.create(req.body)
45-
await pgMeta.end()
46-
if (error) {
47-
logger.error({ error, req: req.body })
48-
let statusCode = 400
49-
if (error.message.startsWith('Cannot find')) statusCode = 404
50-
return res.status(statusCode).json({ error: error.message })
51-
}
5227

53-
return res.status(200).json(data)
54-
})
55-
56-
router.patch('/:id', async (req, res) => {
57-
const connectionString = req.headers?.pg?.toString() ?? ''
58-
59-
const pgMeta = new PostgresMeta({ connectionString, max: 1 })
60-
const { data, error } = await pgMeta.columns.update(req.params.id, req.body)
61-
await pgMeta.end()
62-
if (error) {
63-
logger.error({ error, req: req.body })
64-
let statusCode = 400
65-
if (error.message.startsWith('Cannot find')) statusCode = 404
66-
return res.status(statusCode).json({ error: error.message })
67-
}
68-
69-
return res.status(200).json(data)
70-
})
71-
72-
router.delete('/:id', async (req, res) => {
73-
const connectionString = req.headers?.pg?.toString() ?? ''
28+
fastify.get<{
29+
Headers: { pg: string }
30+
Params: {
31+
id: string
32+
}
33+
}>('/:id(\\d+\\.\\d+)', async (request, reply) => {
34+
const connectionString = request.headers.pg
35+
36+
const pgMeta = new PostgresMeta({ connectionString, max: 1 })
37+
const { data, error } = await pgMeta.columns.retrieve({ id: request.params.id })
38+
await pgMeta.end()
39+
if (error) {
40+
request.log.error(JSON.stringify({ error, req: request.body }))
41+
reply.code(400)
42+
if (error.message.startsWith('Cannot find')) reply.code(404)
43+
return { error: error.message }
44+
}
45+
46+
return data
47+
})
7448

75-
const pgMeta = new PostgresMeta({ connectionString, max: 1 })
76-
const { data, error } = await pgMeta.columns.remove(req.params.id)
77-
await pgMeta.end()
78-
if (error) {
79-
logger.error({ error, req: req.body })
80-
let statusCode = 400
81-
if (error.message.startsWith('Cannot find')) statusCode = 404
82-
return res.status(statusCode).json({ error: error.message })
83-
}
49+
fastify.post<{
50+
Headers: { pg: string }
51+
Body: any
52+
}>('/', async (request, reply) => {
53+
const connectionString = request.headers.pg
54+
55+
const pgMeta = new PostgresMeta({ connectionString, max: 1 })
56+
const { data, error } = await pgMeta.columns.create(request.body)
57+
await pgMeta.end()
58+
if (error) {
59+
request.log.error(JSON.stringify({ error, req: request.body }))
60+
reply.code(400)
61+
if (error.message.startsWith('Cannot find')) reply.code(404)
62+
return { error: error.message }
63+
}
64+
65+
return data
66+
})
8467

85-
return res.status(200).json(data)
86-
})
68+
fastify.patch<{
69+
Headers: { pg: string }
70+
Params: {
71+
id: string
72+
}
73+
Body: any
74+
}>('/:id(\\d+\\.\\d+)', async (request, reply) => {
75+
const connectionString = request.headers.pg
76+
77+
const pgMeta = new PostgresMeta({ connectionString, max: 1 })
78+
const { data, error } = await pgMeta.columns.update(request.params.id, request.body)
79+
await pgMeta.end()
80+
if (error) {
81+
request.log.error({ error, req: request.body })
82+
reply.code(400)
83+
if (error.message.startsWith('Cannot find')) reply.code(404)
84+
return { error: error.message }
85+
}
86+
87+
return data
88+
})
8789

88-
export = router
90+
fastify.delete<{
91+
Headers: { pg: string }
92+
Params: {
93+
id: string
94+
}
95+
Querystring: {
96+
cascade?: string
97+
}
98+
}>('/:id(\\d+\\.\\d+)', async (request, reply) => {
99+
const connectionString = request.headers.pg
100+
101+
const pgMeta = new PostgresMeta({ connectionString, max: 1 })
102+
const { data, error } = await pgMeta.columns.remove(request.params.id)
103+
await pgMeta.end()
104+
if (error) {
105+
request.log.error(JSON.stringify({ error, req: request.body }))
106+
reply.code(400)
107+
if (error.message.startsWith('Cannot find')) reply.code(404)
108+
return { error: error.message }
109+
}
110+
111+
return data
112+
})
113+
}

src/server/routes/config.ts

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
1-
import { Router } from 'express'
2-
import logger from '../logger'
1+
import { FastifyInstance } from 'fastify'
32
import { PostgresMeta } from '../../lib'
43

5-
const router = Router()
6-
7-
router.get('/', async (req, res) => {
8-
const connectionString = req.headers?.pg?.toString() ?? ''
9-
10-
const pgMeta = new PostgresMeta({ connectionString, max: 1 })
11-
const { data, error } = await pgMeta.config.list()
12-
await pgMeta.end()
13-
if (error) {
14-
logger.error({ error, req: req.body })
15-
return res.status(500).json({ error: error.message })
16-
}
17-
18-
return res.status(200).json(data)
19-
})
20-
21-
router.get('/version', async (req, res) => {
22-
const connectionString = req.headers?.pg?.toString() ?? ''
23-
24-
const pgMeta = new PostgresMeta({ connectionString, max: 1 })
25-
const { data, error } = await pgMeta.version.retrieve()
26-
await pgMeta.end()
27-
if (error) {
28-
logger.error({ error, req: req.body })
29-
return res.status(500).json({ error: error.message })
30-
}
31-
32-
return res.status(200).json(data)
33-
})
34-
35-
export = router
4+
export default async (fastify: FastifyInstance) => {
5+
fastify.get<{
6+
Headers: { pg: string }
7+
}>('/', async (request, reply) => {
8+
const connectionString = request.headers.pg
9+
10+
const pgMeta = new PostgresMeta({ connectionString, max: 1 })
11+
const { data, error } = await pgMeta.config.list()
12+
await pgMeta.end()
13+
if (error) {
14+
request.log.error(JSON.stringify({ error, req: request.body }))
15+
reply.code(500)
16+
return { error: error.message }
17+
}
18+
19+
return data
20+
})
21+
22+
fastify.get<{
23+
Headers: { pg: string }
24+
}>('/version', async (request, reply) => {
25+
const connectionString = request.headers.pg
26+
27+
const pgMeta = new PostgresMeta({ connectionString, max: 1 })
28+
const { data, error } = await pgMeta.version.retrieve()
29+
await pgMeta.end()
30+
if (error) {
31+
request.log.error(JSON.stringify({ error, req: request.body }))
32+
reply.code(500)
33+
return { error: error.message }
34+
}
35+
36+
return data
37+
})
38+
}

0 commit comments

Comments
 (0)