Skip to content

Commit 0af898b

Browse files
committed
feat(functions): allow include/exclude schema(s)
1 parent f0ad3e8 commit 0af898b

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/lib/PostgresMetaFunctions.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,33 @@ export default class PostgresMetaFunctions {
1212

1313
async list({
1414
includeSystemSchemas = false,
15+
includedSchemas,
16+
excludedSchemas,
1517
limit,
1618
offset,
1719
}: {
1820
includeSystemSchemas?: boolean
21+
includedSchemas?: string[]
22+
excludedSchemas?: string[]
1923
limit?: number
2024
offset?: number
2125
} = {}): Promise<PostgresMetaResult<PostgresFunction[]>> {
2226
let sql = enrichedFunctionsSql
23-
if (!includeSystemSchemas) {
27+
28+
if (includedSchemas?.length) {
29+
sql = `${sql} WHERE (schema IN (${includedSchemas.map(literal).join(',')}))`
30+
} else if (!includeSystemSchemas) {
2431
sql = `${sql} WHERE NOT (schema IN (${DEFAULT_SYSTEM_SCHEMAS.map(literal).join(',')}))`
2532
}
33+
34+
if (excludedSchemas?.length) {
35+
if (includedSchemas?.length || !includeSystemSchemas) {
36+
sql = `${sql} AND NOT (schema IN (${excludedSchemas.map(literal).join(',')}))`
37+
} else {
38+
sql = `${sql} WHERE NOT (schema IN (${excludedSchemas.map(literal).join(',')}))`
39+
}
40+
}
41+
2642
if (limit) {
2743
sql = `${sql} LIMIT ${limit}`
2844
}

src/server/routes/functions.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,22 @@ export default async (fastify: FastifyInstance) => {
88
Headers: { pg: string }
99
Querystring: {
1010
include_system_schemas?: string
11+
// Note: this only supports comma separated values (e.g., ".../functions?included_schemas=public,core")
12+
included_schemas?: string
13+
excluded_schemas?: string
1114
limit?: number
1215
offset?: number
1316
}
1417
}>('/', async (request, reply) => {
1518
const connectionString = request.headers.pg
1619
const includeSystemSchemas = request.query.include_system_schemas === 'true'
20+
const includedSchemas = request.query.included_schemas?.split(',')
21+
const excludedSchemas = request.query.excluded_schemas?.split(',')
1722
const limit = request.query.limit
1823
const offset = request.query.offset
1924

2025
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
21-
const { data, error } = await pgMeta.functions.list({ includeSystemSchemas, limit, offset })
26+
const { data, error } = await pgMeta.functions.list({ includeSystemSchemas, includedSchemas, excludedSchemas, limit, offset })
2227
await pgMeta.end()
2328
if (error) {
2429
request.log.error({ error, request: extractRequestForLogging(request) })

test/lib/functions.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,41 @@ test('list', async () => {
4040
)
4141
})
4242

43+
test('list functions with included schemas', async () => {
44+
let res = await pgMeta.functions.list({
45+
includedSchemas: ['public']
46+
})
47+
48+
expect(res.data?.length).toBeGreaterThan(0)
49+
50+
res.data?.forEach((func) => {
51+
expect(func.schema).toBe('public')
52+
})
53+
})
54+
55+
test('list functions with excluded schemas', async () => {
56+
let res = await pgMeta.functions.list({
57+
excludedSchemas: ['public'],
58+
})
59+
60+
res.data?.forEach((func) => {
61+
expect(func.schema).not.toBe('public')
62+
})
63+
})
64+
65+
test('list functions with excluded schemas and include System Schemas', async () => {
66+
let res = await pgMeta.functions.list({
67+
excludedSchemas: ['public'],
68+
includeSystemSchemas: true,
69+
})
70+
71+
expect(res.data?.length).toBeGreaterThan(0)
72+
73+
res.data?.forEach((func) => {
74+
expect(func.schema).not.toBe('public')
75+
})
76+
})
77+
4378
test('retrieve, create, update, delete', async () => {
4479
const {
4580
data: { id: testSchemaId },

0 commit comments

Comments
 (0)