Skip to content

feat: GET single table by ID #40

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 1 commit into from
Jul 30, 2020
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
17 changes: 17 additions & 0 deletions src/api/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ router.get('/', async (req, res) => {
}
})

router.get('/:id', async (req, res) => {
try {
const id = parseInt(req.params.id)

const sql = selectSingleSql(sqlTemplates, id)
const table = (await RunQuery(req.headers.pg, sql)).data[0]
if (typeof table === 'undefined') {
return res.status(404).json({ error: `No table exists with ID ${id}.` })
}

return res.status(200).json(table)
} catch (error) {
console.log('throwing error', error)
res.status(500).json({ error: 'Database error', status: 500 })
}
})

router.post('/', async (req, res) => {
try {
const pcConnection: string = req.headers.pg.toString()
Expand Down
7 changes: 7 additions & 0 deletions test/integration/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ describe('/tables', async () => {
assert.equal(datum.policies.length == 0, true, 'Has no policies')
assert.equal(statusColumn.enums[0], 'new', 'Has enums')
})
it('GET single by ID', async () => {
const tables = await axios.get(`${URL}/tables`)
const { id } = tables.data.find((table) => `${table.schema}.${table.name}` === 'public.users')
const { data: table } = await axios.get(`${URL}/tables/${id}`)

assert.equal(`${table.schema}.${table.name}`, 'public.users')
})
it('/tables should return the relationships', async () => {
const tables = await axios.get(`${URL}/tables`)
const datum = tables.data.find((x) => `${x.schema}.${x.name}` === 'public.users')
Expand Down