custom columns

This commit is contained in:
Martin McKeaveney 2021-02-04 23:17:49 +00:00
parent 27c7f5697b
commit b1b02934ed
6 changed files with 67 additions and 38 deletions

View File

@ -5,6 +5,13 @@ import { fetchRelationshipData } from "./relationships"
import { executeQuery } from "./queries" import { executeQuery } from "./queries"
import { enrichRows } from "./rows" import { enrichRows } from "./rows"
export const searchTable = async ({ tableId, search, pageSize }) => {
const rows = await searchTableData(tableId, search, pageSize)
return rows
// Enrich rows so they can displayed properly
// return await enrichRows(rows, tableId)
}
/** /**
* Fetches all rows for a particular Budibase data source. * Fetches all rows for a particular Budibase data source.
*/ */
@ -14,15 +21,10 @@ export const fetchDatasource = async datasource => {
} }
// Fetch all rows in data source // Fetch all rows in data source
const { type, tableId, fieldName, search } = datasource const { type, tableId, fieldName } = datasource
let rows = [] let rows = []
if (type === "table") { if (type === "table") {
// TODO refactor
if (search) {
rows = await searchTableData(tableId, search)
} else {
rows = await fetchTableData(tableId) rows = await fetchTableData(tableId)
}
} else if (type === "view") { } else if (type === "view") {
rows = await fetchViewData(datasource) rows = await fetchViewData(datasource)
} else if (type === "query") { } else if (type === "query") {

View File

@ -22,11 +22,12 @@ export const fetchTableData = async tableId => {
* @param {String} tableId - id of the table to search * @param {String} tableId - id of the table to search
* @param {Object} search - Mango Compliant search object * @param {Object} search - Mango Compliant search object
*/ */
export const searchTableData = async (tableId, search) => { export const searchTableData = async (tableId, search, pageSize) => {
const rows = await API.post({ const rows = await API.post({
url: `/api/${tableId}/rows/search`, url: `/api/${tableId}/rows/search`,
body: { body: {
query: search, query: search,
pageSize,
}, },
}) })
return await enrichRows(rows, tableId) return await enrichRows(rows, tableId)

View File

@ -218,6 +218,8 @@ exports.fetchView = async function(ctx) {
exports.search = async function(ctx) { exports.search = async function(ctx) {
const appId = ctx.user.appId const appId = ctx.user.appId
const { pageSize = 10, cursor } = ctx.query
// special case for users, fetch through the user controller // special case for users, fetch through the user controller
// let rows // let rows
// SHOULD WE PREVENT SEARCHING FOR USERS? // SHOULD WE PREVENT SEARCHING FOR USERS?
@ -230,11 +232,15 @@ exports.search = async function(ctx) {
const query = ctx.request.body.query const query = ctx.request.body.query
query.tableId = ctx.params.tableId query.tableId = ctx.params.tableId
// query._id = { $gte: cursor }
const response = await db.find({ const response = await db.find({
selector: query, selector: query,
limit: pageSize,
}) })
ctx.body = response.docs ctx.body = response.docs
// TODO: probably attach relationships
// const rows = response.docs.map(row => row.doc) // const rows = response.docs.map(row => row.doc)
// ctx.body = await linkRows.attachLinkInfo(appId, rows) // ctx.body = await linkRows.attachLinkInfo(appId, rows)
} }

View File

@ -111,6 +111,11 @@
"type": "datasource", "type": "datasource",
"label": "Data", "label": "Data",
"key": "datasource" "key": "datasource"
},
{
"type": "text",
"label": "No Rows Message",
"key": "noRowsMessage"
} }
] ]
}, },
@ -123,9 +128,20 @@
"dataProvider": true, "dataProvider": true,
"settings": [ "settings": [
{ {
"type": "datasource", "type": "table",
"label": "Data", "label": "Table",
"key": "datasource" "key": "table"
},
{
"type": "multifield",
"label": "Columns",
"key": "valueColumns",
"dependsOn": "table"
},
{
"type": "text",
"label": "No Rows Message",
"key": "noRowsMessage"
} }
] ]
}, },

View File

@ -6,6 +6,7 @@
const component = getContext("component") const component = getContext("component")
export let datasource = [] export let datasource = []
export let noRowsMessage = "Feed me some data"
let rows = [] let rows = []
let loaded = false let loaded = false
@ -32,7 +33,7 @@
{/each} {/each}
{/if} {/if}
{:else if loaded && $builderStore.inBuilder} {:else if loaded && $builderStore.inBuilder}
<p>Feed me some data</p> <p>{noRowsMessage}</p>
{/if} {/if}
</div> </div>

View File

@ -6,29 +6,30 @@
const { API, styleable, DataProvider, builderStore } = getContext("sdk") const { API, styleable, DataProvider, builderStore } = getContext("sdk")
const component = getContext("component") const component = getContext("component")
export let datasource = [] export let table = []
export let columns = []
export let pageSize = 50
export let noRowsMessage = "Feed me some data"
let rows = [] let rows = []
let loaded = false let loaded = false
let table // let searchableFields = []
let searchableFields = []
let search = {} let search = {}
let tableDefinition
let schema = {}
$: schema = table?.schema || {} $: columns = Object.keys(schema).filter(key => schema[key].searchable)
$: searchableFields = Object.keys(schema).filter(
key => schema[key].searchable
)
$: console.log(search) $: fetchData(table)
$: fetchData(datasource) async function fetchData(table) {
if (!isEmpty(table)) {
async function fetchData(datasource) { const tableDef = await API.fetchTableDefinition(table)
if (!isEmpty(datasource)) { schema = tableDef.schema
table = await API.fetchTableDefinition(datasource.tableId) rows = await API.searchTable({
rows = await API.fetchDatasource({ tableId: table,
...datasource, search,
search pageSize,
}) })
} }
loaded = true loaded = true
@ -37,7 +38,7 @@
<div use:styleable={$component.styles}> <div use:styleable={$component.styles}>
<div class="query-builder"> <div class="query-builder">
{#each searchableFields as field} {#each columns as field}
<div class="form-field"> <div class="form-field">
<Label extraSmall grey>{schema[field].name}</Label> <Label extraSmall grey>{schema[field].name}</Label>
{#if schema[field].type === 'options'} {#if schema[field].type === 'options'}
@ -50,9 +51,7 @@
<!-- {:else if schema[field].type === 'datetime'} <!-- {:else if schema[field].type === 'datetime'}
<DatePicker bind:value={search[field]} /> ---> <DatePicker bind:value={search[field]} /> --->
{:else if schema[field].type === 'boolean'} {:else if schema[field].type === 'boolean'}
<Toggle <Toggle text={schema[field].name} bind:checked={search[field]} />
text={schema[field].name}
bind:checked={search[field]} />
{:else if schema[field].type === 'number'} {:else if schema[field].type === 'number'}
<Input type="number" bind:value={search[field]} /> <Input type="number" bind:value={search[field]} />
{:else if schema[field].type === 'string'} {:else if schema[field].type === 'string'}
@ -60,11 +59,15 @@
{/if} {/if}
</div> </div>
{/each} {/each}
<Button blue on:click={() => fetchData(datasource)}>Search</Button> <Button blue on:click={() => fetchData(table)}>Search</Button>
<Button red on:click={() => { <Button
red
on:click={() => {
search = {} search = {}
fetchData(datasource) fetchData(table)
}}>Reset</Button> }}>
Reset
</Button>
</div> </div>
{#if rows.length > 0} {#if rows.length > 0}
{#if $component.children === 0 && $builderStore.inBuilder} {#if $component.children === 0 && $builderStore.inBuilder}
@ -77,7 +80,7 @@
{/each} {/each}
{/if} {/if}
{:else if loaded && $builderStore.inBuilder} {:else if loaded && $builderStore.inBuilder}
<p>Feed me some data</p> <p>{noRowsMessage}</p>
{/if} {/if}
</div> </div>