Skip to content

Commit 6d090b6

Browse files
committed
feat(types): allow include/exclude schema(s)
1 parent 9658445 commit 6d090b6

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

src/lib/PostgresMetaTypes.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,29 @@ export default class PostgresMetaTypes {
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<PostgresType[]>> {
2226
let sql = typesSql
23-
if (!includeSystemSchemas) {
27+
28+
if (includedSchemas?.length) {
29+
sql = `${sql} AND (n.nspname IN (${includedSchemas.map(literal).join(',')}))`
30+
} else if (!includeSystemSchemas) {
2431
sql = `${sql} AND NOT (n.nspname IN (${DEFAULT_SYSTEM_SCHEMAS.map(literal).join(',')}))`
2532
}
33+
34+
if (excludedSchemas?.length) {
35+
sql = `${sql} AND NOT (n.nspname IN (${excludedSchemas.map(literal).join(',')}))`
36+
}
37+
2638
if (limit) {
2739
sql = `${sql} LIMIT ${limit}`
2840
}

src/server/routes/types.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., ".../types?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.types.list({ includeSystemSchemas, limit, offset })
26+
const { data, error } = await pgMeta.types.list({ includeSystemSchemas, includedSchemas, excludedSchemas, limit, offset })
2227
await pgMeta.end()
2328
if (error) {
2429
request.log.error({ error, request: extractRequestForLogging(request) })

test/lib/types.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,39 @@ test('list', async () => {
1919
`
2020
)
2121
})
22+
23+
24+
test('list types with included schemas', async () => {
25+
let res = await pgMeta.types.list({
26+
includedSchemas: ['public']
27+
})
28+
29+
expect(res.data?.length).toBeGreaterThan(0)
30+
31+
res.data?.forEach((type) => {
32+
expect(type.schema).toBe('public')
33+
})
34+
})
35+
36+
test('list types with excluded schemas', async () => {
37+
let res = await pgMeta.types.list({
38+
excludedSchemas: ['public'],
39+
})
40+
41+
res.data?.forEach((type) => {
42+
expect(type.schema).not.toBe('public')
43+
})
44+
})
45+
46+
test('list types with excluded schemas and include System Schemas', async () => {
47+
let res = await pgMeta.types.list({
48+
excludedSchemas: ['public'],
49+
includeSystemSchemas: true,
50+
})
51+
52+
expect(res.data?.length).toBeGreaterThan(0)
53+
54+
res.data?.forEach((type) => {
55+
expect(type.schema).not.toBe('public')
56+
})
57+
})

0 commit comments

Comments
 (0)