Skip to content

[WIP] feat(/functions): add remaining functions routes #117

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 4 commits into from
Jun 18, 2021
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
37 changes: 37 additions & 0 deletions src/lib/PostgresMetaFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,41 @@ export default class PostgresMetaFunctions {
)}));`
return await this.query(sql)
}

async retrieve({ id }: { id: number }): Promise<PostgresMetaResult<PostgresFunction>>
async retrieve({ name }: { name: string }): Promise<PostgresMetaResult<PostgresFunction>>
async retrieve({
id,
name,
}: {
id?: number
name?: string
}): Promise<PostgresMetaResult<PostgresFunction>> {
if (id) {
const sql = `${functionsSql} WHERE p.oid = ${literal(id)};`
const { data, error } = await this.query(sql)
if (error) {
return { data, error }
} else if (data.length === 0) {
return { data: null, error: { message: `Cannot find a function with ID ${id}` } }
} else {
return { data: data[0], error }
}
} else if (name) {
const sql = `${functionsSql} WHERE p.proname = ${literal(name)};`
const { data, error } = await this.query(sql)
if (error) {
return { data, error }
} else if (data.length === 0) {
return {
data: null,
error: { message: `Cannot find a function named ${name}` },
}
} else {
return { data: data[0], error }
}
} else {
return { data: null, error: { message: 'Invalid parameters on function retrieve' } }
}
}
}
21 changes: 21 additions & 0 deletions src/server/routes/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,25 @@ export default async (fastify: FastifyInstance) => {

return data
})

fastify.get<{
Headers: { pg: string }
Params: {
id: string
}
}>('/:id(\\d+)', async (request, reply) => {
const connectionString = request.headers.pg
const id = Number(request.params.id)

const pgMeta = new PostgresMeta({ connectionString, max: 1 })
const { data, error } = await pgMeta.functions.retrieve({ id })
await pgMeta.end()
if (error) {
request.log.error(JSON.stringify({ error, req: request.body }))
reply.code(404)
return { error: error.message }
}

return data
})
}
62 changes: 40 additions & 22 deletions test/integration/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ describe('/functions', () => {
assert.equal(true, !!datum)
assert.equal(true, !!included)
})
it('GET single by ID', async () => {
const functions = await axios.get(`${URL}/functions`)
const functionFiltered = functions.data.find(
(func) => `${func.schema}.${func.name}` === 'public.add'
)
const { data: functionById } = await axios.get(`${URL}/functions/${functionFiltered.id}`)

assert.deepStrictEqual(functionById, functionFiltered)
})
})
describe('/tables', async () => {
it('GET', async () => {
Expand Down Expand Up @@ -312,7 +321,7 @@ describe('/tables', async () => {
type: 'int2',
default_value: 42,
is_nullable: false,
comment: 'foo'
comment: 'foo',
})

const { data: columns } = await axios.get(`${URL}/columns`)
Expand All @@ -337,17 +346,16 @@ describe('/tables', async () => {
})

// https://wiki.postgresql.org/wiki/Retrieve_primary_key_columns
const { data: primaryKeys } = await axios.post(
`${URL}/query`,
{ query: `
const { data: primaryKeys } = await axios.post(`${URL}/query`, {
query: `
SELECT a.attname
FROM pg_index i
JOIN pg_attribute a ON a.attrelid = i.indrelid
AND a.attnum = ANY(i.indkey)
WHERE i.indrelid = '${newTable.name}'::regclass
AND i.indisprimary;
` }
)
`,
})
assert.equal(primaryKeys.length, 1)
assert.equal(primaryKeys[0].attname, 'bar')

Expand All @@ -363,18 +371,17 @@ describe('/tables', async () => {
is_unique: true,
})

const { data: uniqueColumns } = await axios.post(
`${URL}/query`,
{ query: `
const { data: uniqueColumns } = await axios.post(`${URL}/query`, {
query: `
SELECT a.attname
FROM pg_index i
JOIN pg_constraint c ON c.conindid = i.indexrelid
JOIN pg_attribute a ON a.attrelid = i.indrelid
AND a.attnum = ANY(i.indkey)
WHERE i.indrelid = '${newTable.name}'::regclass
AND i.indisunique;
` }
)
`,
})
assert.equal(uniqueColumns.length, 1)
assert.equal(uniqueColumns[0].attname, 'bar')

Expand Down Expand Up @@ -423,16 +430,15 @@ describe('/tables', async () => {
check: "description <> ''",
})

const { data: constraints } = await axios.post(
`${URL}/query`,
{ query: `
const { data: constraints } = await axios.post(`${URL}/query`, {
query: `
SELECT pg_get_constraintdef((
SELECT c.oid
FROM pg_constraint c
WHERE c.conrelid = '${newTable.name}'::regclass
));
` }
)
`,
})
assert.equal(constraints.length, 1)
assert.equal(constraints[0].pg_get_constraintdef, "CHECK ((description <> ''::text))")

Expand Down Expand Up @@ -499,7 +505,7 @@ describe('/tables', async () => {

const { data: updatedColumn } = await axios.patch(`${URL}/columns/${newTable.id}.1`, {
type: 'int4',
default_value: 0
default_value: 0,
})

assert.strictEqual(updatedColumn.format, 'int4')
Expand Down Expand Up @@ -737,7 +743,10 @@ describe('/publications with tables', () => {
assert.equal(newPublication.publish_update, publication.publish_update)
assert.equal(newPublication.publish_delete, publication.publish_delete)
assert.equal(newPublication.publish_truncate, publication.publish_truncate)
assert.equal(newPublication.tables.some(table => `${table.schema}.${table.name}` === 'public.users'), true)
assert.equal(
newPublication.tables.some((table) => `${table.schema}.${table.name}` === 'public.users'),
true
)
})
it('GET', async () => {
const res = await axios.get(`${URL}/publications`)
Expand All @@ -755,7 +764,10 @@ describe('/publications with tables', () => {
})
assert.equal(updated.name, 'b')
assert.equal(updated.publish_insert, false)
assert.equal(updated.tables.some(table => `${table.schema}.${table.name}` === 'public.users'), false)
assert.equal(
updated.tables.some((table) => `${table.schema}.${table.name}` === 'public.users'),
false
)
})
it('DELETE', async () => {
const res = await axios.get(`${URL}/publications`)
Expand All @@ -768,9 +780,15 @@ describe('/publications with tables', () => {
})
it('/publications for tables with uppercase', async () => {
const { data: table } = await axios.post(`${URL}/tables`, { name: 'T' })
const { data: publication } = await axios.post(`${URL}/publications`, { name: 'pub', tables: ['T'] })
const { data: publication } = await axios.post(`${URL}/publications`, {
name: 'pub',
tables: ['T'],
})
assert.equal(publication.name, 'pub')
const { data: alteredPublication } = await axios.patch(`${URL}/publications/${publication.id}`, { tables: ['T'] })
const { data: alteredPublication } = await axios.patch(
`${URL}/publications/${publication.id}`,
{ tables: ['T'] }
)
assert.equal(alteredPublication.name, 'pub')

await axios.delete(`${URL}/publications/${publication.id}`)
Expand All @@ -784,7 +802,7 @@ describe('/publications FOR ALL TABLES', () => {
publish_insert: true,
publish_update: true,
publish_delete: true,
publish_truncate: false
publish_truncate: false,
}
it('POST', async () => {
const { data: newPublication } = await axios.post(`${URL}/publications`, publication)
Expand Down