Skip to content

Commit 837e62d

Browse files
committed
feat(columns): allow getting columns from a single table
1 parent 2711927 commit 837e62d

File tree

3 files changed

+118
-4
lines changed

3 files changed

+118
-4
lines changed

Diff for: src/lib/PostgresMetaColumns.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,36 @@ export default class PostgresMetaColumns {
1414
}
1515

1616
async list({
17+
tableId,
1718
includeSystemSchemas = false,
1819
limit,
1920
offset,
2021
}: {
22+
tableId?: number
2123
includeSystemSchemas?: boolean
2224
limit?: number
2325
offset?: number
2426
} = {}): Promise<PostgresMetaResult<PostgresColumn[]>> {
25-
let sql = columnsSql
27+
let sql = `
28+
WITH
29+
columns AS (${columnsSql})
30+
SELECT
31+
*
32+
FROM
33+
columns
34+
WHERE
35+
true`
2636
if (!includeSystemSchemas) {
27-
sql = `${sql} AND NOT (nc.nspname IN (${DEFAULT_SYSTEM_SCHEMAS.map(literal).join(',')}))`
37+
sql += ` AND schema NOT IN (${DEFAULT_SYSTEM_SCHEMAS.map(literal).join(',')})`
38+
}
39+
if (tableId !== undefined) {
40+
sql += ` AND table_id = ${literal(tableId)}`
2841
}
2942
if (limit) {
30-
sql = `${sql} LIMIT ${limit}`
43+
sql += ` LIMIT ${limit}`
3144
}
3245
if (offset) {
33-
sql = `${sql} OFFSET ${offset}`
46+
sql += ` OFFSET ${offset}`
3447
}
3548
return await this.query(sql)
3649
}

Diff for: src/server/routes/columns.ts

+33
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,39 @@ export default async (fastify: FastifyInstance) => {
3333
return data
3434
})
3535

36+
fastify.get<{
37+
Headers: { pg: string }
38+
Params: { tableId: number }
39+
Querystring: {
40+
include_system_schemas?: string
41+
limit?: number
42+
offset?: number
43+
}
44+
}>('/:tableId(^\\d+$)', async (request, reply) => {
45+
const {
46+
headers: { pg: connectionString },
47+
query: { limit, offset },
48+
params: { tableId },
49+
} = request
50+
const includeSystemSchemas = request.query.include_system_schemas === 'true'
51+
52+
const pgMeta: PostgresMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
53+
const { data, error } = await pgMeta.columns.list({
54+
tableId,
55+
includeSystemSchemas,
56+
limit,
57+
offset,
58+
})
59+
await pgMeta.end()
60+
if (error) {
61+
request.log.error({ error, request: extractRequestForLogging(request) })
62+
reply.code(500)
63+
return { error: error.message }
64+
}
65+
66+
return data
67+
})
68+
3669
fastify.get<{
3770
Headers: { pg: string }
3871
Params: {

Diff for: test/lib/columns.ts

+68
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,74 @@ test('list', async () => {
3131
)
3232
})
3333

34+
test('list from a single table', async () => {
35+
const { data: testTable }: any = await pgMeta.tables.create({ name: 't' })
36+
await pgMeta.query('alter table t add c1 text, add c2 text')
37+
38+
const res = await pgMeta.columns.list({ tableId: testTable!.id })
39+
expect(res).toMatchInlineSnapshot(
40+
{
41+
data: [
42+
{
43+
id: expect.stringMatching(/^\d+\.\d+$/),
44+
table_id: expect.any(Number),
45+
},
46+
{
47+
id: expect.stringMatching(/^\d+\.\d+$/),
48+
table_id: expect.any(Number),
49+
},
50+
],
51+
},
52+
`
53+
Object {
54+
"data": Array [
55+
Object {
56+
"comment": null,
57+
"data_type": "text",
58+
"default_value": null,
59+
"enums": Array [],
60+
"format": "text",
61+
"id": StringMatching /\\^\\\\d\\+\\\\\\.\\\\d\\+\\$/,
62+
"identity_generation": null,
63+
"is_generated": false,
64+
"is_identity": false,
65+
"is_nullable": true,
66+
"is_unique": false,
67+
"is_updatable": true,
68+
"name": "c1",
69+
"ordinal_position": 1,
70+
"schema": "public",
71+
"table": "t",
72+
"table_id": Any<Number>,
73+
},
74+
Object {
75+
"comment": null,
76+
"data_type": "text",
77+
"default_value": null,
78+
"enums": Array [],
79+
"format": "text",
80+
"id": StringMatching /\\^\\\\d\\+\\\\\\.\\\\d\\+\\$/,
81+
"identity_generation": null,
82+
"is_generated": false,
83+
"is_identity": false,
84+
"is_nullable": true,
85+
"is_unique": false,
86+
"is_updatable": true,
87+
"name": "c2",
88+
"ordinal_position": 2,
89+
"schema": "public",
90+
"table": "t",
91+
"table_id": Any<Number>,
92+
},
93+
],
94+
"error": null,
95+
}
96+
`
97+
)
98+
99+
await pgMeta.tables.remove(testTable!.id)
100+
})
101+
34102
test('retrieve, create, update, delete', async () => {
35103
const { data: testTable }: any = await pgMeta.tables.create({ name: 't' })
36104

0 commit comments

Comments
 (0)