Skip to content

feat(lib): omit policies from /tables and allow listing columns from a single table #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/lib/PostgresMetaColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,36 @@ export default class PostgresMetaColumns {
}

async list({
tableId,
includeSystemSchemas = false,
limit,
offset,
}: {
tableId?: number
includeSystemSchemas?: boolean
limit?: number
offset?: number
} = {}): Promise<PostgresMetaResult<PostgresColumn[]>> {
let sql = columnsSql
let sql = `
WITH
columns AS (${columnsSql})
SELECT
*
FROM
columns
WHERE
true`
if (!includeSystemSchemas) {
sql = `${sql} AND NOT (nc.nspname IN (${DEFAULT_SYSTEM_SCHEMAS.map(literal).join(',')}))`
sql += ` AND schema NOT IN (${DEFAULT_SYSTEM_SCHEMAS.map(literal).join(',')})`
}
if (tableId !== undefined) {
sql += ` AND table_id = ${literal(tableId)}`
}
if (limit) {
sql = `${sql} LIMIT ${limit}`
sql += ` LIMIT ${limit}`
}
if (offset) {
sql = `${sql} OFFSET ${offset}`
sql += ` OFFSET ${offset}`
}
return await this.query(sql)
}
Expand Down
4 changes: 1 addition & 3 deletions src/lib/PostgresMetaTables.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ident, literal } from 'pg-format'
import { DEFAULT_SYSTEM_SCHEMAS } from './constants'
import { coalesceRowsToArray } from './helpers'
import { columnsSql, policiesSql, primaryKeysSql, relationshipsSql, tablesSql } from './sql'
import { columnsSql, primaryKeysSql, relationshipsSql, tablesSql } from './sql'
import { PostgresMetaResult, PostgresTable } from './types'

export default class PostgresMetaTables {
Expand Down Expand Up @@ -229,13 +229,11 @@ COMMIT;`
const enrichedTablesSql = `
WITH tables AS (${tablesSql}),
columns AS (${columnsSql}),
policies AS (${policiesSql}),
primary_keys AS (${primaryKeysSql}),
relationships AS (${relationshipsSql})
SELECT
*,
${coalesceRowsToArray('columns', 'columns.table_id = tables.id')},
${coalesceRowsToArray('policies', 'policies.table_id = tables.id')},
${coalesceRowsToArray('primary_keys', 'primary_keys.table_id = tables.id')},
${coalesceRowsToArray(
'relationships',
Expand Down
1 change: 0 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ export const postgresTableSchema = Type.Object({
dead_rows_estimate: Type.Integer(),
comment: Type.Union([Type.String(), Type.Null()]),
columns: Type.Array(postgresColumnSchema),
policies: Type.Array(postgresPolicySchema),
primary_keys: Type.Array(postgresPrimaryKeySchema),
relationships: Type.Array(postgresRelationshipSchema),
})
Expand Down
33 changes: 33 additions & 0 deletions src/server/routes/columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ export default async (fastify: FastifyInstance) => {
return data
})

fastify.get<{
Headers: { pg: string }
Params: { tableId: number }
Querystring: {
include_system_schemas?: string
limit?: number
offset?: number
}
}>('/:tableId(^\\d+$)', async (request, reply) => {
const {
headers: { pg: connectionString },
query: { limit, offset },
params: { tableId },
} = request
const includeSystemSchemas = request.query.include_system_schemas === 'true'

const pgMeta: PostgresMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
const { data, error } = await pgMeta.columns.list({
tableId,
includeSystemSchemas,
limit,
offset,
})
await pgMeta.end()
if (error) {
request.log.error({ error, request: extractRequestForLogging(request) })
reply.code(500)
return { error: error.message }
}

return data
})

fastify.get<{
Headers: { pg: string }
Params: {
Expand Down
68 changes: 68 additions & 0 deletions test/lib/columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,74 @@ test('list', async () => {
)
})

test('list from a single table', async () => {
const { data: testTable }: any = await pgMeta.tables.create({ name: 't' })
await pgMeta.query('alter table t add c1 text, add c2 text')

const res = await pgMeta.columns.list({ tableId: testTable!.id })
expect(res).toMatchInlineSnapshot(
{
data: [
{
id: expect.stringMatching(/^\d+\.\d+$/),
table_id: expect.any(Number),
},
{
id: expect.stringMatching(/^\d+\.\d+$/),
table_id: expect.any(Number),
},
],
},
`
Object {
"data": Array [
Object {
"comment": null,
"data_type": "text",
"default_value": null,
"enums": Array [],
"format": "text",
"id": StringMatching /\\^\\\\d\\+\\\\\\.\\\\d\\+\\$/,
"identity_generation": null,
"is_generated": false,
"is_identity": false,
"is_nullable": true,
"is_unique": false,
"is_updatable": true,
"name": "c1",
"ordinal_position": 1,
"schema": "public",
"table": "t",
"table_id": Any<Number>,
},
Object {
"comment": null,
"data_type": "text",
"default_value": null,
"enums": Array [],
"format": "text",
"id": StringMatching /\\^\\\\d\\+\\\\\\.\\\\d\\+\\$/,
"identity_generation": null,
"is_generated": false,
"is_identity": false,
"is_nullable": true,
"is_unique": false,
"is_updatable": true,
"name": "c2",
"ordinal_position": 2,
"schema": "public",
"table": "t",
"table_id": Any<Number>,
},
],
"error": null,
}
`
)

await pgMeta.tables.remove(testTable!.id)
})

test('retrieve, create, update, delete', async () => {
const { data: testTable }: any = await pgMeta.tables.create({ name: 't' })

Expand Down
14 changes: 2 additions & 12 deletions test/lib/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { pgMeta } from './utils'

const cleanNondet = (x: any) => {
const {
data: { columns, policies, primary_keys, relationships, ...rest2 },
data: { columns, primary_keys, relationships, ...rest2 },
...rest1
} = x

return {
data: {
columns: columns.map(({ id, table_id, ...rest }: any) => rest),
policies: policies.map(({ id, table_id, ...rest }: any) => rest),
primary_keys: primary_keys.map(({ table_id, ...rest }: any) => rest),
relationships: relationships.map(({ id, ...rest }: any) => rest),
...rest2,
Expand All @@ -21,13 +20,12 @@ const cleanNondet = (x: any) => {
test('list', async () => {
const res = await pgMeta.tables.list()

const { columns, policies, primary_keys, relationships, ...rest }: any = res.data?.find(
const { columns, primary_keys, relationships, ...rest }: any = res.data?.find(
({ name }) => name === 'users'
)

expect({
columns: columns.map(({ id, table_id, ...rest }: any) => rest),
policies: policies.map(({ id, table_id, ...rest }: any) => rest),
primary_keys: primary_keys.map(({ table_id, ...rest }: any) => rest),
relationships: relationships.map(({ id, ...rest }: any) => rest),
...rest,
Expand Down Expand Up @@ -103,7 +101,6 @@ test('list', async () => {
"id": Any<Number>,
"live_rows_estimate": Any<Number>,
"name": "users",
"policies": Array [],
"primary_keys": Array [
Object {
"name": "id",
Expand Down Expand Up @@ -146,7 +143,6 @@ test('retrieve, create, update, delete', async () => {
"id": Any<Number>,
"live_rows_estimate": 0,
"name": "test",
"policies": Array [],
"primary_keys": Array [],
"relationships": Array [],
"replica_identity": "DEFAULT",
Expand All @@ -172,7 +168,6 @@ test('retrieve, create, update, delete', async () => {
"id": Any<Number>,
"live_rows_estimate": 0,
"name": "test",
"policies": Array [],
"primary_keys": Array [],
"relationships": Array [],
"replica_identity": "DEFAULT",
Expand Down Expand Up @@ -204,7 +199,6 @@ test('retrieve, create, update, delete', async () => {
"id": Any<Number>,
"live_rows_estimate": 0,
"name": "test a",
"policies": Array [],
"primary_keys": Array [],
"relationships": Array [],
"replica_identity": "NOTHING",
Expand All @@ -230,7 +224,6 @@ test('retrieve, create, update, delete', async () => {
"id": Any<Number>,
"live_rows_estimate": 0,
"name": "test a",
"policies": Array [],
"primary_keys": Array [],
"relationships": Array [],
"replica_identity": "NOTHING",
Expand Down Expand Up @@ -269,7 +262,6 @@ test('update with name unchanged', async () => {
"id": Any<Number>,
"live_rows_estimate": 0,
"name": "t",
"policies": Array [],
"primary_keys": Array [],
"relationships": Array [],
"replica_identity": "DEFAULT",
Expand Down Expand Up @@ -299,7 +291,6 @@ test("allow ' in comments", async () => {
"id": Any<Number>,
"live_rows_estimate": 0,
"name": "t",
"policies": Array [],
"primary_keys": Array [],
"relationships": Array [],
"replica_identity": "DEFAULT",
Expand Down Expand Up @@ -377,7 +368,6 @@ test('primary keys', async () => {
"id": Any<Number>,
"live_rows_estimate": Any<Number>,
"name": "t",
"policies": Array [],
"primary_keys": Array [
Object {
"name": "c",
Expand Down