|
1 | | -import { Router } from 'express' |
2 | | -import logger from '../logger' |
| 1 | +import { FastifyInstance } from 'fastify' |
3 | 2 | import { PostgresMeta } from '../../lib' |
4 | 3 |
|
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 |
14 | 26 | }) |
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 | | - } |
52 | 27 |
|
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 | + }) |
74 | 48 |
|
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 | + }) |
84 | 67 |
|
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 | + }) |
87 | 89 |
|
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 | +} |
0 commit comments