Skip to content

Commit 9658445

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

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/lib/PostgresMetaColumns.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ export default class PostgresMetaColumns {
1616
async list({
1717
tableId,
1818
includeSystemSchemas = false,
19+
includedSchemas,
20+
excludedSchemas,
1921
limit,
2022
offset,
2123
}: {
2224
tableId?: number
2325
includeSystemSchemas?: boolean
26+
includedSchemas?: string[]
27+
excludedSchemas?: string[]
2428
limit?: number
2529
offset?: number
2630
} = {}): Promise<PostgresMetaResult<PostgresColumn[]>> {
@@ -33,9 +37,17 @@ FROM
3337
columns
3438
WHERE
3539
true`
36-
if (!includeSystemSchemas) {
37-
sql += ` AND schema NOT IN (${DEFAULT_SYSTEM_SCHEMAS.map(literal).join(',')})`
40+
41+
if (includedSchemas?.length) {
42+
sql = `${sql} AND (schema IN (${includedSchemas.map(literal).join(',')}))`
43+
} else if (!includeSystemSchemas) {
44+
sql = `${sql} AND NOT (schema IN (${DEFAULT_SYSTEM_SCHEMAS.map(literal).join(',')}))`
45+
}
46+
47+
if (excludedSchemas?.length) {
48+
sql = `${sql} AND NOT (schema IN (${excludedSchemas.map(literal).join(',')}))`
3849
}
50+
3951
if (tableId !== undefined) {
4052
sql += ` AND table_id = ${literal(tableId)}`
4153
}

src/server/routes/columns.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,25 @@ 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., ".../columns?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 })
2126
const { data, error } = await pgMeta.columns.list({
2227
includeSystemSchemas,
28+
includedSchemas,
29+
excludedSchemas,
2330
limit,
2431
offset,
2532
})

test/lib/columns.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,41 @@ test('list from a single table', async () => {
9999
await pgMeta.tables.remove(testTable!.id)
100100
})
101101

102+
test('list columns with included schemas', async () => {
103+
let res = await pgMeta.columns.list({
104+
includedSchemas: ['public']
105+
})
106+
107+
expect(res.data?.length).toBeGreaterThan(0)
108+
109+
res.data?.forEach((column) => {
110+
expect(column.schema).toBe('public')
111+
})
112+
})
113+
114+
test('list columns with excluded schemas', async () => {
115+
let res = await pgMeta.columns.list({
116+
excludedSchemas: ['public'],
117+
})
118+
119+
res.data?.forEach((column) => {
120+
expect(column.schema).not.toBe('public')
121+
})
122+
})
123+
124+
test('list columns with excluded schemas and include System Schemas', async () => {
125+
let res = await pgMeta.columns.list({
126+
excludedSchemas: ['public'],
127+
includeSystemSchemas: true,
128+
})
129+
130+
expect(res.data?.length).toBeGreaterThan(0)
131+
132+
res.data?.forEach((column) => {
133+
expect(column.schema).not.toBe('public')
134+
})
135+
})
136+
102137
test('retrieve, create, update, delete', async () => {
103138
const { data: testTable }: any = await pgMeta.tables.create({ name: 't' })
104139

0 commit comments

Comments
 (0)