fixing JSON and CSV import/export

This commit is contained in:
Martin McKeaveney 2021-02-04 08:18:33 +00:00
parent 909ccac598
commit 1f6a755cb6
7 changed files with 151 additions and 25 deletions

View File

@ -62,13 +62,13 @@
{/each} {/each}
</Select> </Select>
{:else if value.customType === 'password'} {:else if value.customType === 'password'}
<Input type="password" extraThin bind:value={block.inputs[key]}/> <Input type="password" extraThin bind:value={block.inputs[key]} />
{:else if value.customType === 'email'} {:else if value.customType === 'email'}
<BindableInput <BindableInput
type={"email"} type={'email'}
extraThin extraThin
bind:value={block.inputs[key]} bind:value={block.inputs[key]}
{bindings} /> {bindings} />
{:else if value.customType === 'table'} {:else if value.customType === 'table'}
<TableSelector bind:value={block.inputs[key]} /> <TableSelector bind:value={block.inputs[key]} />
{:else if value.customType === 'row'} {:else if value.customType === 'row'}
@ -82,7 +82,7 @@
type={value.customType} type={value.customType}
extraThin extraThin
bind:value={block.inputs[key]} bind:value={block.inputs[key]}
{bindings} /> {bindings} />
{/if} {/if}
</div> </div>
{/each} {/each}

View File

@ -20,13 +20,12 @@
let exportFormat = FORMATS[0].key let exportFormat = FORMATS[0].key
async function exportView() { async function exportView() {
const response = await api.post( download(
`/api/views/export?format=${exportFormat}`, `/api/views/export?view=${encodeURIComponent(
view view.name
)}&format=${exportFormat}`
) )
const downloadInfo = await response.json()
onClosed() onClosed()
window.location = downloadInfo.url
} }
</script> </script>

View File

@ -30,13 +30,13 @@
<Spacer medium /> <Spacer medium />
<div class="card-footer"> <div class="card-footer">
<TextButton text medium blue href="/_builder/{_id}"> <TextButton text medium blue href="/_builder/{_id}">
Open {name} Open
{name}
</TextButton> </TextButton>
{#if appExportLoading} {#if appExportLoading}
<Spinner size="10" /> <Spinner size="10" />
{:else} {:else}<i class="ri-folder-download-line" on:click={exportApp} />{/if}
<i class="ri-folder-download-line" on:click={exportApp} />
{/if}
</div> </div>
</div> </div>

View File

@ -83,23 +83,42 @@ const controller = {
ctx.message = `View ${ctx.params.viewName} saved successfully.` ctx.message = `View ${ctx.params.viewName} saved successfully.`
}, },
exportView: async ctx => { exportView: async ctx => {
const view = ctx.query.view const db = new CouchDB(ctx.user.appId)
const designDoc = await db.get("_design/database")
const viewName = decodeURI(ctx.query.view)
const view = designDoc.views[viewName]
const format = ctx.query.format const format = ctx.query.format
// Fetch view rows if (view) {
ctx.params.viewName = view.name ctx.params.viewName = viewName
ctx.query.group = view.groupBy // Fetch view rows
if (view.field) { ctx.query = {
ctx.query.stats = true group: view.meta.groupBy,
ctx.query.field = view.field calculation: view.meta.calculation,
stats: !!view.meta.field,
field: view.meta.field,
}
} else {
// table all_ view
ctx.params.viewName = viewName
} }
await fetchView(ctx) await fetchView(ctx)
let schema = view && view.meta && view.meta.schema
if (!schema) {
const tableId = ctx.params.tableId || view.meta.tableId
const table = await db.get(tableId)
schema = table.schema
}
// Export part // Export part
let headers = Object.keys(view.schema) let headers = Object.keys(schema)
const exporter = exporters[format] const exporter = exporters[format]
const exportedFile = exporter(headers, ctx.body) const exportedFile = exporter(headers, ctx.body)
const filename = `${view.name}.${format}` const filename = `${viewName}.${format}`
fs.writeFileSync(join(os.tmpdir(), filename), exportedFile) fs.writeFileSync(join(os.tmpdir(), filename), exportedFile)
ctx.attachment(filename) ctx.attachment(filename)

View File

@ -12,6 +12,7 @@ const usage = require("../../middleware/usageQuota")
const router = Router() const router = Router()
router router
.get("/api/views/export", authorized(BUILDER), viewController.exportView)
.get( .get(
"/api/views/:viewName", "/api/views/:viewName",
authorized(PermissionTypes.VIEW, PermissionLevels.READ), authorized(PermissionTypes.VIEW, PermissionLevels.READ),
@ -25,6 +26,5 @@ router
viewController.destroy viewController.destroy
) )
.post("/api/views", authorized(BUILDER), usage, viewController.save) .post("/api/views", authorized(BUILDER), usage, viewController.save)
.post("/api/views/export", authorized(BUILDER), viewController.exportView)
module.exports = router module.exports = router

View File

@ -60,13 +60,18 @@
.nav__menu { .nav__menu {
display: flex; display: flex;
margin-top: 40px; margin-top: 40px;
gap: 16px;
flex-direction: row; flex-direction: row;
justify-content: flex-start; justify-content: flex-start;
align-items: center; align-items: center;
} }
.nav__menu > a {
.nav__menu > * {
margin-right: 16px;
}
:global(.nav__menu > a) {
font-size: 1.5em; font-size: 1.5em;
text-decoration: none; text-decoration: none;
margin-right: 16px;
} }
</style> </style>

View File

@ -0,0 +1,103 @@
<script>
import { getContext } from "svelte"
import { isEmpty } from "lodash/fp"
import { Button, Label, Select, Toggle, Input } from "@budibase/bbui"
const { API, styleable, DataProvider, builderStore } = getContext("sdk")
const component = getContext("component")
export let datasource = []
export let fields = []
let rows = []
let loaded = false
let table
let searchableFields = []
let search = {}
$: schema = table?.schema || {}
$: searchableFields = Object.keys(schema).filter(
key => schema[key].searchable
)
$: console.log(search)
$: fetchData(datasource)
async function fetchData(datasource) {
if (!isEmpty(datasource)) {
table = await API.fetchTableDefinition(datasource.tableId)
rows = await API.fetchDatasource({
...datasource,
search,
})
}
loaded = true
}
</script>
<div use:styleable={$component.styles}>
<div class="query-builder">
{#each searchableFields as field}
<div class="form-field">
<Label extraSmall grey>{schema[field].name}</Label>
{#if schema[field].type === 'options'}
<Select secondary bind:value={search[field]}>
<option value="">Choose an option</option>
{#each schema[field].constraints.inclusion as opt}
<option>{opt}</option>
{/each}
</Select>
{:else if schema[field].type === 'boolean'}
<Toggle text={schema[field].name} bind:checked={search[field]} />
{:else if schema[field].type === 'number'}
<Input type="number" bind:value={search[field]} />
{:else if schema[field].type === 'string'}
<Input bind:value={search[field]} />
{/if}
</div>
{/each}
<Button blue on:click={() => fetchData(datasource)}>Search</Button>
<Button
red
on:click={() => {
search = {}
fetchData(datasource)
}}>
Reset
</Button>
</div>
{#if rows.length > 0}
{#if $component.children === 0 && $builderStore.inBuilder}
<p>Add some components too</p>
{:else}
{#each rows as row}
<DataProvider {row}>
<slot />
</DataProvider>
{/each}
{/if}
{:else if loaded && $builderStore.inBuilder}
<p>Feed me some data</p>
{/if}
</div>
<style>
p {
display: grid;
place-items: center;
background: #f5f5f5;
border: #ccc 1px solid;
padding: var(--spacing-m);
}
.query-builder {
padding: var(--spacing-m);
background: var(--background);
border-radius: var(--border-radius-s);
}
.form-field {
margin-bottom: var(--spacing-m);
}
</style>