Skip to content

Commit 017fa63

Browse files
authored
Merge pull request #40 from supabase/feature/get-single-table
feat: GET single table by ID
2 parents 7526740 + 697ad9b commit 017fa63

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/api/tables.ts

+17
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ router.get('/', async (req, res) => {
2929
}
3030
})
3131

32+
router.get('/:id', async (req, res) => {
33+
try {
34+
const id = parseInt(req.params.id)
35+
36+
const sql = selectSingleSql(sqlTemplates, id)
37+
const table = (await RunQuery(req.headers.pg, sql)).data[0]
38+
if (typeof table === 'undefined') {
39+
return res.status(404).json({ error: `No table exists with ID ${id}.` })
40+
}
41+
42+
return res.status(200).json(table)
43+
} catch (error) {
44+
console.log('throwing error', error)
45+
res.status(500).json({ error: 'Database error', status: 500 })
46+
}
47+
})
48+
3249
router.post('/', async (req, res) => {
3350
try {
3451
const pcConnection: string = req.headers.pg.toString()

test/integration/index.spec.js

+7
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ describe('/tables', async () => {
187187
assert.equal(datum.policies.length == 0, true, 'Has no policies')
188188
assert.equal(statusColumn.enums[0], 'new', 'Has enums')
189189
})
190+
it('GET single by ID', async () => {
191+
const tables = await axios.get(`${URL}/tables`)
192+
const { id } = tables.data.find((table) => `${table.schema}.${table.name}` === 'public.users')
193+
const { data: table } = await axios.get(`${URL}/tables/${id}`)
194+
195+
assert.equal(`${table.schema}.${table.name}`, 'public.users')
196+
})
190197
it('/tables should return the relationships', async () => {
191198
const tables = await axios.get(`${URL}/tables`)
192199
const datum = tables.data.find((x) => `${x.schema}.${x.name}` === 'public.users')

0 commit comments

Comments
 (0)