Split out logic for getting rows from datasources

This commit is contained in:
Andrew Kingston 2023-08-03 13:22:49 +01:00
parent d443bf3616
commit 3eeb945934
4 changed files with 37 additions and 19 deletions

View File

@ -98,6 +98,10 @@ export const createActions = context => {
return await getAPI()?.actions.deleteRows(rows)
}
const getRow = async id => {
return await getAPI()?.actions.getRow(id)
}
return {
datasource: {
...datasource,
@ -107,6 +111,7 @@ export const createActions = context => {
addRow,
updateRow,
deleteRows,
getRow,
},
},
}

View File

@ -3,8 +3,6 @@ import { fetchData } from "../../../fetch/fetchData"
import { NewRowID, RowPageSize } from "../lib/constants"
import { tick } from "svelte"
const SuppressErrors = true
export const createStores = () => {
const rows = writable([])
const loading = writable(false)
@ -280,21 +278,6 @@ export const createActions = context => {
}
}
// Fetches a row by ID using the search endpoint
const fetchRow = async id => {
const res = await API.searchTable({
tableId: get(datasource).tableId,
limit: 1,
query: {
equal: {
_id: id,
},
},
paginate: false,
})
return res?.rows?.[0]
}
// Replaces a row in state with the newly defined row, handling updates,
// addition and deletion
const replaceRow = (id, row) => {
@ -323,7 +306,7 @@ export const createActions = context => {
// Refreshes a specific row
const refreshRow = async id => {
const row = await fetchRow(id)
const row = await datasource.actions.getRow(id)
replaceRow(id, row)
}
@ -336,7 +319,6 @@ export const createActions = context => {
const updateRow = async (rowId, changes) => {
const $rows = get(rows)
const $rowLookupMap = get(rowLookupMap)
const $datasource = get(datasource)
const index = $rowLookupMap[rowId]
const row = $rows[index]
if (index == null || !Object.keys(changes || {}).length) {

View File

@ -25,6 +25,20 @@ export const createActions = context => {
})
}
const getRow = async id => {
const res = await API.searchTable({
tableId: get(datasource).tableId,
limit: 1,
query: {
equal: {
_id: id,
},
},
paginate: false,
})
return res?.rows?.[0]
}
return {
table: {
actions: {
@ -33,6 +47,7 @@ export const createActions = context => {
addRow: saveRow,
updateRow: saveRow,
deleteRows,
getRow,
},
},
}

View File

@ -45,6 +45,21 @@ export const createActions = context => {
})
}
// TODO: update in future. We can't depend on having table read access.
const getRow = async id => {
const res = await API.searchTable({
tableId: get(datasource).tableId,
limit: 1,
query: {
equal: {
_id: id,
},
},
paginate: false,
})
return res?.rows?.[0]
}
return {
viewV2: {
actions: {
@ -53,6 +68,7 @@ export const createActions = context => {
addRow,
updateRow,
deleteRows,
getRow,
},
},
}