diff --git a/docs/user/ff-tables.md b/docs/user/ff-tables.md index a0c04e4367..3076840805 100644 --- a/docs/user/ff-tables.md +++ b/docs/user/ff-tables.md @@ -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_ diff --git a/frontend/src/api/tables.js b/frontend/src/api/tables.js index e9dd20bccc..54a30260dd 100644 --- a/frontend/src/api/tables.js +++ b/frontend/src/api/tables.js @@ -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) => { diff --git a/frontend/src/pages/team/Tables/Table/TableExplorer/components/RowsList.vue b/frontend/src/pages/team/Tables/Table/TableExplorer/components/RowsList.vue index f3b2ba4d78..a4491b327b 100644 --- a/frontend/src/pages/team/Tables/Table/TableExplorer/components/RowsList.vue +++ b/frontend/src/pages/team/Tables/Table/TableExplorer/components/RowsList.vue @@ -22,8 +22,11 @@ @@ -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: `${escapeHtml(row.name)} ${escapeHtml(row.type)}`, - sortable: true, component: { is: this.getTableComponent(row.type), extraProps: {}, @@ -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: { @@ -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({ @@ -113,6 +122,29 @@ export default defineComponent({ overlay: true }) }, + onPageChange (page) { + this.setPage(page) + this.loadPage() + }, + onPageSizeChange (pageSize) { + this.setPageSize(pageSize) + this.loadPage() + }, + loadPage () { + 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 diff --git a/frontend/src/stores/product-tables.js b/frontend/src/stores/product-tables.js index 418eaedbb3..d82f7919a0 100644 --- a/frontend/src/stores/product-tables.js +++ b/frontend/src/stores/product-tables.js @@ -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 }), @@ -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) @@ -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] diff --git a/test/unit/frontend/pages/team/Tables/RowsList.spec.js b/test/unit/frontend/pages/team/Tables/RowsList.spec.js index 12fc781345..7ac35d4ea1 100644 --- a/test/unit/frontend/pages/team/Tables/RowsList.spec.js +++ b/test/unit/frontend/pages/team/Tables/RowsList.spec.js @@ -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() } })) @@ -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) + }) + }) }) diff --git a/test/unit/frontend/stores/product-tables.spec.js b/test/unit/frontend/stores/product-tables.spec.js index 468b3e8fc5..99e3c45f7e 100644 --- a/test/unit/frontend/stores/product-tables.spec.js +++ b/test/unit/frontend/stores/product-tables.spec.js @@ -193,27 +193,168 @@ describe('product-tables store', () => { }) describe('getTableData', () => { - it('stores data and safe-keyed data on the table', async () => { + it('stores data, safe-keyed data and the table total on the table', async () => { const store = useProductTablesStore() store.tables = { 'db-1': [{ name: 'users' }] } - tablesApi.getTableData.mockResolvedValue([{ id: 1, name: 'Alice' }]) + tablesApi.getTableData.mockResolvedValue({ count: 42, rows: [{ id: 1, name: 'Alice' }] }) await store.getTableData({ databaseId: 'db-1', tableName: 'users', teamId: 'team-1' }) const table = store.tables['db-1'][0] expect(table.payload.data).toEqual([{ id: 1, name: 'Alice' }]) expect(table.payload.safe[0].safe_id).toBe(1) + expect(table.payload.total).toBe(42) + }) + + it('asks for the first page of 25 rows by default', async () => { + const store = useProductTablesStore() + store.tables = { 'db-1': [{ name: 'users' }] } + tablesApi.getTableData.mockResolvedValue({ count: 0, rows: [] }) + await store.getTableData({ databaseId: 'db-1', tableName: 'users', teamId: 'team-1' }) + expect(tablesApi.getTableData).toHaveBeenCalledWith('team-1', 'db-1', 'users', undefined, { page: 1, limit: 25 }) + }) + + it('asks for the current page at the current page size', async () => { + const store = useProductTablesStore() + store.tables = { 'db-1': [{ name: 'users' }] } + tablesApi.getTableData.mockResolvedValue({ count: 0, rows: [] }) + store.setPageSize(50) + store.setPage(3) + await store.getTableData({ databaseId: 'db-1', tableName: 'users', teamId: 'team-1' }) + expect(tablesApi.getTableData).toHaveBeenCalledWith('team-1', 'db-1', 'users', undefined, { page: 3, limit: 50 }) }) it('only updates the table matching both name and schemaName when schemaName is given', async () => { const store = useProductTablesStore() store.tables = { 'db-1': [{ name: 'users', dbSchema: 'public' }, { name: 'users', dbSchema: 'reports' }] } - tablesApi.getTableData.mockResolvedValue([{ id: 1, name: 'Alice' }]) + tablesApi.getTableData.mockResolvedValue({ count: 1, rows: [{ id: 1, name: 'Alice' }] }) await store.getTableData({ databaseId: 'db-1', tableName: 'users', teamId: 'team-1', schemaName: 'reports' }) - expect(tablesApi.getTableData).toHaveBeenCalledWith('team-1', 'db-1', 'users', 'reports') + expect(tablesApi.getTableData).toHaveBeenCalledWith('team-1', 'db-1', 'users', 'reports', { page: 1, limit: 25 }) expect(store.tables['db-1'][0].payload).toBeUndefined() expect(store.tables['db-1'][1].payload.data).toEqual([{ id: 1, name: 'Alice' }]) }) }) + describe('getTableData when the table or the page changes underneath it', () => { + const args = { databaseId: 'db-1', tableName: 'users', teamId: 'team-1' } + const deferred = () => { + let resolve + const promise = new Promise(_resolve => { resolve = _resolve }) + return { promise, resolve } + } + + it('falls back to the last page when the table shrank and the current page is gone', async () => { + const store = useProductTablesStore() + store.tables = { 'db-1': [{ name: 'users' }] } + store.setPage(5) + tablesApi.getTableData + .mockResolvedValueOnce({ count: 60, rows: [] }) + .mockResolvedValueOnce({ count: 60, rows: [{ id: 51 }] }) + await store.getTableData(args) + expect(tablesApi.getTableData).toHaveBeenNthCalledWith(1, 'team-1', 'db-1', 'users', undefined, { page: 5, limit: 25 }) + expect(tablesApi.getTableData).toHaveBeenNthCalledWith(2, 'team-1', 'db-1', 'users', undefined, { page: 3, limit: 25 }) + expect(store.page).toBe(3) + expect(store.tables['db-1'][0].payload.data).toEqual([{ id: 51 }]) + expect(store.tables['db-1'][0].payload.total).toBe(60) + }) + + it('falls back to the first page, without asking again and again, when the table is now empty', async () => { + const store = useProductTablesStore() + store.tables = { 'db-1': [{ name: 'users' }] } + store.setPage(4) + tablesApi.getTableData.mockResolvedValue({ count: 0, rows: [] }) + await store.getTableData(args) + expect(tablesApi.getTableData).toHaveBeenCalledTimes(2) + expect(store.page).toBe(1) + expect(store.tables['db-1'][0].payload.data).toEqual([]) + }) + + it('does not ask twice when the current page still exists', async () => { + const store = useProductTablesStore() + store.tables = { 'db-1': [{ name: 'users' }] } + store.setPage(3) + tablesApi.getTableData.mockResolvedValue({ count: 60, rows: [{ id: 51 }] }) + await store.getTableData(args) + expect(tablesApi.getTableData).toHaveBeenCalledTimes(1) + expect(store.page).toBe(3) + }) + + it('ignores a page that arrives after a newer page was asked for', async () => { + const store = useProductTablesStore() + store.tables = { 'db-1': [{ name: 'users' }] } + const page2 = deferred() + const page3 = deferred() + tablesApi.getTableData.mockReturnValueOnce(page2.promise).mockReturnValueOnce(page3.promise) + + store.setPage(2) + const loadingPage2 = store.getTableData(args) + store.setPage(3) + const loadingPage3 = store.getTableData(args) + + page3.resolve({ count: 120, rows: [{ id: 51 }] }) + await loadingPage3 + page2.resolve({ count: 120, rows: [{ id: 26 }] }) + await loadingPage2 + + expect(store.page).toBe(3) + expect(store.tables['db-1'][0].payload.data).toEqual([{ id: 51 }]) + }) + + it('ignores a page that arrives after the page size was changed', async () => { + const store = useProductTablesStore() + store.tables = { 'db-1': [{ name: 'users' }] } + const small = deferred() + const large = deferred() + tablesApi.getTableData.mockReturnValueOnce(small.promise).mockReturnValueOnce(large.promise) + + const loadingSmall = store.getTableData(args) + store.setPageSize(50) + const loadingLarge = store.getTableData(args) + + large.resolve({ count: 120, rows: [{ id: 1 }, { id: 2 }] }) + await loadingLarge + small.resolve({ count: 120, rows: [{ id: 1 }] }) + await loadingSmall + + expect(store.tables['db-1'][0].payload.data).toEqual([{ id: 1 }, { id: 2 }]) + }) + + it('does not move the page back when a stale response reports a smaller table', async () => { + const store = useProductTablesStore() + store.tables = { 'db-1': [{ name: 'users' }] } + const page5 = deferred() + tablesApi.getTableData.mockReturnValueOnce(page5.promise).mockResolvedValueOnce({ count: 120, rows: [{ id: 26 }] }) + + store.setPage(5) + const loadingPage5 = store.getTableData(args) + store.setPage(2) + await store.getTableData(args) + page5.resolve({ count: 10, rows: [] }) + await loadingPage5 + + expect(tablesApi.getTableData).toHaveBeenCalledTimes(2) + expect(store.page).toBe(2) + expect(store.tables['db-1'][0].payload.data).toEqual([{ id: 26 }]) + }) + }) + + describe('paging', () => { + it('goes back to the first page when the page size changes', () => { + const store = useProductTablesStore() + store.setPage(4) + store.setPageSize(100) + expect(store.pageSize).toBe(100) + expect(store.page).toBe(1) + }) + + it('goes back to the first page when another table is selected, keeping the page size', () => { + const store = useProductTablesStore() + store.setPageSize(50) + store.setPage(4) + store.updateTableSelection({ name: 'orders', dbSchema: 'public' }) + expect(store.page).toBe(1) + expect(store.pageSize).toBe(50) + }) + }) + describe('deleteTable', () => { it('passes schemaName through to the API', async () => { const store = useProductTablesStore()