Skip to content
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
2 changes: 1 addition & 1 deletion docs/user/ff-tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Once you have created a database you can use the wizard to create some tables. T
![Screenshot of create tables wizard](./images/tables/create-table-wizard.png){data-zoomable}
_Screenshot of create tables wizard_

Once the tables have data in them then the first 10 rows will be displayed in the Explorer tab.
Once the tables have data in them the rows will be displayed in the Explorer tab, 25 at a time. On a longer table, use the controls under the grid to move between pages or change how many rows are shown per page.

![Screenshot of Tables Explorer](./images/tables/tables-ui-screenshot.png){data-zoomable}
_Screenshot of Tables Explorer_
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/api/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ const getTableSchema = (teamId, databaseId, tableName, schemaName) => {
.then(res => res.data)
}

const getTableData = (teamId, databaseId, tableName, schemaName) => {
const getTableData = (teamId, databaseId, tableName, schemaName, { page, limit } = {}) => {
const suffix = schemaName ? `/${schemaName}` : ''
return client.get(`/api/v1/teams/${teamId}/databases/${databaseId}/tables/${tableName}/data${suffix}`)
.then(res => res.data.rows)
return client.get(`/api/v1/teams/${teamId}/databases/${databaseId}/tables/${tableName}/data${suffix}`, { params: { page, limit } })
.then(res => res.data)
}

const createTable = (teamId, databaseId, payload) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
<ff-data-table
:loading="isLoading"
:columns="columns" :rows="rows"
:pagination="pagination"
class="h-full overflow-auto"
tableClass="table-auto overflow-auto"
@update:page="onPageChange"
@update:page-size="onPageSizeChange"
/>
</div>

Expand Down Expand Up @@ -53,14 +56,13 @@ export default defineComponent({
name: 'RowsList',
components: { EmptyState, PlusIcon },
computed: {
...mapState(useProductTablesStore, ['tables', 'tableSelection', 'isLoading', 'selectedTable']),
...mapState(useProductTablesStore, ['tables', 'tableSelection', 'isLoading', 'selectedTable', 'page', 'pageSize']),
...mapState(useContextStore, ['team']),
columns () {
return (this.selectedTable?.schema ?? []).map((row) => {
return {
key: row.safeName,
html: `<span >${escapeHtml(row.name)}</span> <span class="text-gray-400">${escapeHtml(row.type)}</span>`,
sortable: true,
component: {
is: this.getTableComponent(row.type),
extraProps: {},
Expand All @@ -80,6 +82,13 @@ export default defineComponent({
},
rows () {
return (this.selectedTable?.payload?.safe ?? [])
},
pagination () {
return {
page: this.page,
pageSize: this.pageSize,
total: this.selectedTable?.payload?.total ?? 0
}
}
},
watch: {
Expand All @@ -104,7 +113,7 @@ export default defineComponent({
this.updateTableSelection(null)
},
methods: {
...mapActions(useProductTablesStore, ['getTableSchema', 'getTableData', 'updateTableSelection']),
...mapActions(useProductTablesStore, ['getTableSchema', 'getTableData', 'updateTableSelection', 'setPage', 'setPageSize', 'setTableLoadingState']),
...mapActions(useUxDrawersStore, ['openRightDrawer']),
onCreateTable () {
this.openRightDrawer({
Expand All @@ -113,6 +122,29 @@ export default defineComponent({
overlay: true
})
},
onPageChange (page) {
this.setPage(page)
this.loadPage()
},
onPageSizeChange (pageSize) {
this.setPageSize(pageSize)
this.loadPage()
},
loadPage () {
Comment thread
cstns marked this conversation as resolved.
Resolved
const load = this.latestPageLoad = {}
this.setTableLoadingState(true)
return this.getTableData({
teamId: this.team.id,
databaseId: this.$route.params.id,
tableName: this.tableSelection.name,
schemaName: this.tableSelection.dbSchema
})
.catch(e => e)
.finally(() => {
// a page picked in the meantime is still loading, so leave the state to it
if (this.latestPageLoad === load) this.setTableLoadingState(false)
})
},
getTableComponent (type) {
const componentMap = {
text: TextCell
Expand Down
35 changes: 31 additions & 4 deletions frontend/src/stores/product-tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const useProductTablesStore = defineStore('product-tables', {
databaseSelection: null,
tables: {},
tableSelection: null,
page: 1,
pageSize: 25,
newTable: { name: '', schema: 'public', columns: [{ ...emptyColumn }] },
isLoading: false
}),
Expand Down Expand Up @@ -63,11 +65,21 @@ export const useProductTablesStore = defineStore('product-tables', {
tables = tables.map(({ schema, ...table }) => ({ ...table, dbSchema: schema }))
tables = [...tables].sort((a, b) => a.name.localeCompare(b.name, undefined, { numeric: true, sensitivity: 'base' }))
this.tables[databaseId] = tables
if (tables.length > 0) this.tableSelection = { name: tables[0].name, dbSchema: tables[0].dbSchema }
if (tables.length > 0) this.updateTableSelection({ name: tables[0].name, dbSchema: tables[0].dbSchema })
return tables
},
clearState () { this.$reset() },
updateTableSelection (table) { this.tableSelection = table },
updateTableSelection (table) {
this.tableSelection = table
this.page = 1
},
setPage (page) {
this.page = page
},
setPageSize (pageSize) {
this.pageSize = pageSize
this.page = 1
},
updateDatabaseSelection (databaseId) { this.databaseSelection = databaseId },
async getTableSchema ({ databaseId, tableName, teamId, schemaName }) {
const schema = await tablesApi.getTableSchema(teamId, databaseId, tableName, schemaName)
Expand All @@ -80,10 +92,25 @@ export const useProductTablesStore = defineStore('product-tables', {
})
},
async getTableData ({ databaseId, tableName, teamId, schemaName }) {
const data = await tablesApi.getTableData(teamId, databaseId, tableName, schemaName)
const pageSize = this.pageSize
let page = this.page
// another page or page size picked while this one was loading makes its rows out of date
const isStale = () => this.page !== page || this.pageSize !== pageSize
const fetchPage = () => tablesApi.getTableData(teamId, databaseId, tableName, schemaName, { page, limit: pageSize })
let res = await fetchPage()
if (isStale()) return
const lastPage = Math.max(1, Math.ceil(res.count / pageSize))
if (page > lastPage) {
// the table shrank and this page is gone, so fall back to the last one
page = this.page = lastPage
res = await fetchPage()
if (isStale()) return
}
const { rows: data, count: total } = res
const payload = {
data,
safe: data.map(row => Object.fromEntries(Object.entries(row).map(([k, v]) => [hashString(k), v])))
safe: data.map(row => Object.fromEntries(Object.entries(row).map(([k, v]) => [hashString(k), v]))),
total
}
Object.keys(this.tables[databaseId]).forEach(key => {
const table = this.tables[databaseId][key]
Expand Down
73 changes: 70 additions & 3 deletions test/unit/frontend/pages/team/Tables/RowsList.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import { mount } from '@vue/test-utils'
import { describe, expect, test, vi } from 'vitest'
import { flushPromises, mount } from '@vue/test-utils'
import { beforeEach, describe, expect, test, vi } from 'vitest'

import RowsList from '../../../../../../frontend/src/pages/team/Tables/Table/TableExplorer/components/RowsList.vue'
import ForgeUIComponents from '../../../../../../frontend/src/ui-components/index.js'

const mocks = vi.hoisted(() => ({
tablesStore: { tables: {}, tableSelection: null, isLoading: false, selectedTable: null },
tablesStore: {
tables: {},
tableSelection: null,
isLoading: false,
selectedTable: null,
page: 1,
pageSize: 25,
setPage: vi.fn(),
setPageSize: vi.fn(),
setTableLoadingState: vi.fn(),
getTableData: vi.fn(() => Promise.resolve()),
getTableSchema: vi.fn(() => Promise.resolve()),
updateTableSelection: vi.fn()
},
contextStore: { team: { id: 't1' } },
drawersStore: { openRightDrawer: vi.fn() }
}))
Expand Down Expand Up @@ -72,4 +85,58 @@ describe('RowsList', () => {
expect(mountRowsList().find('[data-el="no-tables"]').exists()).toBe(false)
})
})

describe('paging', () => {
const schema = [{ name: 'id', safeName: 'col', type: 'bigint' }]

beforeEach(() => {
vi.clearAllMocks()
mocks.tablesStore.page = 2
mocks.tablesStore.pageSize = 25
mocks.tablesStore.tableSelection = { name: 'readings', dbSchema: 'public' }
mocks.tablesStore.selectedTable = { schema, payload: { safe: [{ col: 26 }], total: 1284 } }
})

test('shows where the current page sits in the whole table', () => {
const wrapper = mountRowsList()
expect(wrapper.find('[data-el="pagination"]').text()).toContain('26–50 of 1284')
})

test('shows no paging controls when the table fits on one page', () => {
mocks.tablesStore.page = 1
mocks.tablesStore.selectedTable = { schema, payload: { safe: [{ col: 1 }], total: 1 } }
const wrapper = mountRowsList()
expect(wrapper.find('[data-el="pagination"]').exists()).toBe(false)
})

test('loads the chosen page for the selected table', async () => {
const wrapper = mountRowsList()
await wrapper.find('[data-action="page-next"]').trigger('click')
expect(mocks.tablesStore.setPage).toHaveBeenCalledWith(3)
expect(mocks.tablesStore.getTableData).toHaveBeenCalledWith({ teamId: 't1', databaseId: 'db1', tableName: 'readings', schemaName: 'public' })
})

test('keeps showing the loading state until the most recently chosen page has loaded', async () => {
const loads = []
const pendingLoad = () => new Promise(resolve => loads.push(resolve))
mocks.tablesStore.getTableData.mockImplementationOnce(pendingLoad).mockImplementationOnce(pendingLoad)
const wrapper = mountRowsList()
await wrapper.find('[data-action="page-next"]').trigger('click')
await wrapper.find('[data-action="page-next"]').trigger('click')
expect(loads).toHaveLength(2)

loads[0]()
await flushPromises()
expect(mocks.tablesStore.setTableLoadingState).not.toHaveBeenCalledWith(false)

loads[1]()
await flushPromises()
expect(mocks.tablesStore.setTableLoadingState).toHaveBeenLastCalledWith(false)
})

test('does not offer to sort by column, since only the current page is loaded', () => {
const wrapper = mountRowsList()
expect(wrapper.find('thead .sortable').exists()).toBe(false)
})
})
})
Loading
Loading