Skip to content

Commit 29a48c4

Browse files
committed
fix: /tables selectSingle missing fields
selectSingleSql and selectSingleByName need to match the SQL of getTables.
1 parent 4b47889 commit 29a48c4

File tree

2 files changed

+75
-9
lines changed

2 files changed

+75
-9
lines changed

src/api/tables.ts

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,78 @@ const getTablesSql = (sqlTemplates) => {
155155
)}
156156
FROM tables;`.trim()
157157
}
158-
const selectSingleSql = (sqlTemplates, id: number) => {
159-
const { tables } = sqlTemplates
160-
return `${tables} and c.oid = ${id};`.trim()
158+
const selectSingleSql = (sqlTemplates: { [key: string]: string }, id: number) => {
159+
const { columns, grants, policies, primary_keys, relationships, tables } = sqlTemplates
160+
return `
161+
WITH tables AS ( ${tables} AND c.oid = ${id} ),
162+
columns AS ( ${columns} ),
163+
grants AS ( ${grants} ),
164+
policies AS ( ${policies} ),
165+
primary_keys AS ( ${primary_keys} ),
166+
relationships AS ( ${relationships} )
167+
SELECT
168+
*,
169+
${coalesceRowsToArray('columns', 'SELECT * FROM columns WHERE columns.table_id = tables.id')},
170+
${coalesceRowsToArray('grants', 'SELECT * FROM grants WHERE grants.table_id = tables.id')},
171+
${coalesceRowsToArray(
172+
'policies',
173+
'SELECT * FROM policies WHERE policies.table_id = tables.id'
174+
)},
175+
${coalesceRowsToArray(
176+
'primary_keys',
177+
'SELECT * FROM primary_keys WHERE primary_keys.table_id = tables.id'
178+
)},
179+
${coalesceRowsToArray(
180+
'relationships',
181+
`SELECT
182+
*
183+
FROM
184+
relationships
185+
WHERE
186+
(relationships.source_schema = tables.schema AND relationships.source_table_name = tables.name)
187+
OR (relationships.target_table_schema = tables.schema AND relationships.target_table_name = tables.name)`
188+
)}
189+
FROM tables;`.trim()
161190
}
162-
const selectSingleByName = (sqlTemplates, schema: string, name: string) => {
163-
const { tables } = sqlTemplates
164-
return `${tables} and table_schema = '${schema}' and table_name = '${name}';`.trim()
191+
192+
const selectSingleByName = (
193+
sqlTemplates: { [key: string]: string },
194+
schema: string,
195+
name: string
196+
) => {
197+
const { columns, grants, policies, primary_keys, relationships, tables } = sqlTemplates
198+
return `
199+
WITH tables AS ( ${tables} AND table_schema = '${schema}' AND table_name = '${name}' ),
200+
columns AS ( ${columns} ),
201+
grants AS ( ${grants} ),
202+
policies AS ( ${policies} ),
203+
primary_keys AS ( ${primary_keys} ),
204+
relationships AS ( ${relationships} )
205+
SELECT
206+
*,
207+
${coalesceRowsToArray('columns', 'SELECT * FROM columns WHERE columns.table_id = tables.id')},
208+
${coalesceRowsToArray('grants', 'SELECT * FROM grants WHERE grants.table_id = tables.id')},
209+
${coalesceRowsToArray(
210+
'policies',
211+
'SELECT * FROM policies WHERE policies.table_id = tables.id'
212+
)},
213+
${coalesceRowsToArray(
214+
'primary_keys',
215+
'SELECT * FROM primary_keys WHERE primary_keys.table_id = tables.id'
216+
)},
217+
${coalesceRowsToArray(
218+
'relationships',
219+
`SELECT
220+
*
221+
FROM
222+
relationships
223+
WHERE
224+
(relationships.source_schema = tables.schema AND relationships.source_table_name = tables.name)
225+
OR (relationships.target_table_schema = tables.schema AND relationships.target_table_name = tables.name)`
226+
)}
227+
FROM tables;`.trim()
165228
}
229+
166230
const createTableSqlize = ({
167231
name,
168232
schema = 'public',

test/integration/index.spec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,12 @@ describe('/tables', async () => {
189189
})
190190
it('GET single by ID', async () => {
191191
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}`)
192+
const tableFiltered = tables.data.find(
193+
(table) => `${table.schema}.${table.name}` === 'public.users'
194+
)
195+
const { data: tableById } = await axios.get(`${URL}/tables/${tableFiltered.id}`)
194196

195-
assert.equal(`${table.schema}.${table.name}`, 'public.users')
197+
assert.deepStrictEqual(tableById, tableFiltered)
196198
})
197199
it('/tables should return the relationships', async () => {
198200
const tables = await axios.get(`${URL}/tables`)

0 commit comments

Comments
 (0)