-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathcolumn-privileges.ts
131 lines (122 loc) · 3.92 KB
/
column-privileges.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'
import { Type } from '@sinclair/typebox'
import { PostgresMeta } from '../../lib/index.js'
import {
postgresColumnPrivilegesGrantSchema,
postgresColumnPrivilegesRevokeSchema,
postgresColumnPrivilegesSchema,
} from '../../lib/types.js'
import { DEFAULT_POOL_CONFIG } from '../constants.js'
import { extractRequestForLogging, translateErrorToResponseCode } from '../utils.js'
const route: FastifyPluginAsyncTypebox = async (fastify) => {
fastify.get(
'/',
{
schema: {
headers: Type.Object({
pg: Type.String(),
}),
querystring: Type.Object({
include_system_schemas: Type.Optional(Type.Boolean()),
// Note: this only supports comma separated values (e.g., "...?included_schemas=public,core")
included_schemas: Type.Optional(Type.String()),
excluded_schemas: Type.Optional(Type.String()),
limit: Type.Optional(Type.Integer()),
offset: Type.Optional(Type.Integer()),
}),
response: {
200: Type.Array(postgresColumnPrivilegesSchema),
500: Type.Object({
error: Type.String(),
}),
},
},
},
async (request, reply) => {
const connectionString = request.headers.pg
const includeSystemSchemas = request.query.include_system_schemas
const includedSchemas = request.query.included_schemas?.split(',')
const excludedSchemas = request.query.excluded_schemas?.split(',')
const limit = request.query.limit
const offset = request.query.offset
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
const { data, error } = await pgMeta.columnPrivileges.list({
includeSystemSchemas,
includedSchemas,
excludedSchemas,
limit,
offset,
})
await pgMeta.end()
if (error) {
request.log.error({ error, request: extractRequestForLogging(request) })
reply.code(translateErrorToResponseCode(error, 500))
return { error: error.message }
}
return data
}
)
fastify.post(
'/',
{
schema: {
headers: Type.Object({
pg: Type.String(),
}),
body: Type.Array(postgresColumnPrivilegesGrantSchema),
response: {
200: Type.Array(postgresColumnPrivilegesSchema),
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.columnPrivileges.grant(request.body)
await pgMeta.end()
if (error) {
request.log.error({ error, request: extractRequestForLogging(request) })
reply.code(400)
return { error: error.message }
}
return data
}
)
fastify.delete(
'/',
{
schema: {
headers: Type.Object({
pg: Type.String(),
}),
body: Type.Array(postgresColumnPrivilegesRevokeSchema),
response: {
200: Type.Array(postgresColumnPrivilegesSchema),
400: Type.Object({
error: Type.String(),
}),
404: 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.columnPrivileges.revoke(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
}
)
}
export default route