tidy up and lint

This commit is contained in:
Martin McKeaveney 2020-08-18 17:14:26 +01:00
parent d08db301af
commit 7e2e90e23c
13 changed files with 53 additions and 52 deletions

View File

@ -83,7 +83,9 @@ export const getBackendUiStore = () => {
delete: async model => { delete: async model => {
await api.delete(`/api/models/${model._id}/${model._rev}`) await api.delete(`/api/models/${model._id}/${model._rev}`)
store.update(state => { store.update(state => {
state.models = state.models.filter(existing => existing._id !== model._id) state.models = state.models.filter(
existing => existing._id !== model._id
)
state.selectedModel = state.models[0] || {} state.selectedModel = state.models[0] || {}
return state return state
}) })
@ -105,7 +107,7 @@ export const getBackendUiStore = () => {
store.actions.models.save(state.draftModel) store.actions.models.save(state.draftModel)
return state return state
}) })
} },
}, },
views: { views: {
select: view => select: view =>
@ -126,7 +128,9 @@ export const getBackendUiStore = () => {
await api.post(`/api/views`, view) await api.post(`/api/views`, view)
store.update(state => { store.update(state => {
const viewModel = state.models.find(model => model._id === view.modelId) const viewModel = state.models.find(
model => model._id === view.modelId
)
// TODO: Cleaner? // TODO: Cleaner?
if (!viewModel.views) viewModel.views = {} if (!viewModel.views) viewModel.views = {}
if (view.originalName) delete viewModel.views[view.originalName] if (view.originalName) delete viewModel.views[view.originalName]
@ -136,7 +140,7 @@ export const getBackendUiStore = () => {
state.selectedView = view state.selectedView = view
return state return state
}) })
} },
}, },
users: { users: {
create: user => create: user =>

View File

@ -27,7 +27,10 @@
let search let search
$: { $: {
if ($backendUiStore.selectedView && $backendUiStore.selectedView.name.startsWith("all_")) { if (
$backendUiStore.selectedView &&
$backendUiStore.selectedView.name.startsWith("all_")
) {
api.fetchDataForView($backendUiStore.selectedView).then(records => { api.fetchDataForView($backendUiStore.selectedView).then(records => {
data = records || [] data = records || []
}) })

View File

@ -30,37 +30,36 @@
}, },
{ {
name: "min", name: "min",
key: "value.min" key: "value.min",
}, },
{ {
name: "max", name: "max",
key: "value.max" key: "value.max",
}, },
{ {
name: "sumsqr", name: "sumsqr",
key: "value.sumsqr" key: "value.sumsqr",
}, },
{ {
name: "count", name: "count",
key: "value.count" key: "value.count",
}, },
{ {
name: "avg", name: "avg",
key: "value.avg" key: "value.avg",
} },
] ]
export let view = {} export let view = {}
let data = [] let data = []
$: viewName = view.name $: !view.name.startsWith("all_") && fetchViewData(view)
$: !viewName.startsWith("all_") && fetchViewData(viewName)
async function fetchViewData(viewName) { async function fetchViewData({ name, groupBy }) {
let QUERY_VIEW_URL = `/api/views/${viewName}?stats=true` let QUERY_VIEW_URL = `/api/views/${name}?stats=true`
if (view.groupBy) { if (groupBy) {
QUERY_VIEW_URL += `&group=${view.groupBy}` QUERY_VIEW_URL += `&group=${groupBy}`
} }
const response = await api.get(QUERY_VIEW_URL) const response = await api.get(QUERY_VIEW_URL)
@ -68,11 +67,7 @@
} }
</script> </script>
<Table <Table title={decodeURI(view.name)} columns={COLUMNS} {data}>
title={decodeURI(view.name)} <CalculationPopover {view} />
columns={COLUMNS} <GroupByPopover {view} />
{data}
>
<CalculationPopover {view} />
<GroupByPopover {view} />
</Table> </Table>

View File

@ -19,8 +19,7 @@
$: viewModel = $backendUiStore.models.find( $: viewModel = $backendUiStore.models.find(
({ _id }) => _id === $backendUiStore.selectedView.modelId ({ _id }) => _id === $backendUiStore.selectedView.modelId
) )
$: fields = $: fields = viewModel && Object.keys(viewModel.schema)
viewModel && Object.keys(viewModel.schema)
function saveView() { function saveView() {
backendUiStore.actions.views.save(view) backendUiStore.actions.views.save(view)
@ -40,7 +39,7 @@
<div class="input-group-row"> <div class="input-group-row">
<p>Group By</p> <p>Group By</p>
<Select secondary thin bind:value={view.groupBy}> <Select secondary thin bind:value={view.groupBy}>
<option value={false}>None</option> <option value={false}>Remove Group By</option>
{#each fields as field} {#each fields as field}
<option value={field}>{field}</option> <option value={field}>{field}</option>
{/each} {/each}

View File

@ -14,12 +14,17 @@
$: fields = Object.keys($backendUiStore.selectedModel.schema).filter(key => { $: fields = Object.keys($backendUiStore.selectedModel.schema).filter(key => {
return $backendUiStore.selectedModel.schema[key].type === "number" return $backendUiStore.selectedModel.schema[key].type === "number"
}) })
$: views = $backendUiStore.models.flatMap(model => Object.keys(model.views))
function saveView() { function saveView() {
if (views.includes(name)) {
notifier.danger(`View exists with name ${name}.`)
return
}
backendUiStore.actions.views.save({ backendUiStore.actions.views.save({
name, name,
modelId: $backendUiStore.selectedModel._id, modelId: $backendUiStore.selectedModel._id,
field field,
}) })
notifier.success(`View ${name} created`) notifier.success(`View ${name} created`)
dropdown.hide() dropdown.hide()

View File

@ -40,7 +40,7 @@
function save() { function save() {
backendUiStore.actions.views.save({ backendUiStore.actions.views.save({
originalName, originalName,
...view ...view,
}) })
notifier.success("Renamed View Successfully.") notifier.success("Renamed View Successfully.")
hideEditor() hideEditor()

View File

@ -21,6 +21,7 @@ exports.save = async function(ctx) {
const modelToSave = { const modelToSave = {
type: "model", type: "model",
_id: newid(), _id: newid(),
views: {},
...ctx.request.body, ...ctx.request.body,
} }

View File

@ -83,14 +83,14 @@ exports.fetchView = async function(ctx) {
const { stats, group } = ctx.query const { stats, group } = ctx.query
const response = await db.query(`database/${ctx.params.viewName}`, { const response = await db.query(`database/${ctx.params.viewName}`, {
include_docs: !stats, include_docs: !stats,
group group,
}) })
if (stats) { if (stats) {
for (row of response.rows) { for (let row of response.rows) {
row.value = { row.value = {
...row.value, ...row.value,
avg: row.value.sum / row.value.count avg: row.value.sum / row.value.count,
} }
} }
} else { } else {
@ -98,7 +98,6 @@ exports.fetchView = async function(ctx) {
} }
ctx.body = response.rows ctx.body = response.rows
} }
exports.fetchModelRecords = async function(ctx) { exports.fetchModelRecords = async function(ctx) {

View File

@ -1,5 +1,5 @@
const CouchDB = require("../../../db") const CouchDB = require("../../../db")
const statsViewTemplate = require("./viewBuilder"); const statsViewTemplate = require("./viewBuilder")
const controller = { const controller = {
fetch: async ctx => { fetch: async ctx => {
@ -43,12 +43,11 @@ const controller = {
await db.put(designDoc) await db.put(designDoc)
// add views to model document // add views to model document
const model = await db.get(ctx.request.body.modelId) const model = await db.get(ctx.request.body.modelId)
model.views = { model.views = {
...(model.views ? model.views : {}), ...(model.views ? model.views : {}),
[newView.name]: view.meta [newView.name]: view.meta,
} }
if (originalName) { if (originalName) {

View File

@ -1,8 +1,4 @@
function statsViewTemplate({ function statsViewTemplate({ field, modelId, groupBy }) {
field,
modelId,
groupBy
}) {
return { return {
meta: { meta: {
field, field,
@ -14,15 +10,15 @@ function statsViewTemplate({
max: "number", max: "number",
count: "number", count: "number",
sumsqr: "number", sumsqr: "number",
avg: "number" avg: "number",
} },
}, },
map: `function (doc) { map: `function (doc) {
if (doc.modelId === "${modelId}") { if (doc.modelId === "${modelId}") {
emit(doc["${groupBy || "_id"}"], doc["${field}"]); emit(doc["${groupBy || "_id"}"], doc["${field}"]);
} }
}`, }`,
reduce: "_stats" reduce: "_stats",
} }
} }

View File

@ -45,7 +45,7 @@ const server = http.createServer(app.callback())
server.on("close", () => console.log("Server Closed")) server.on("close", () => console.log("Server Closed"))
process.on('SIGINT', () => process.exit(1)); process.on("SIGINT", () => process.exit(1))
module.exports = server.listen(env.PORT || 4001, () => { module.exports = server.listen(env.PORT || 4001, () => {
console.log(`Budibase running on ${JSON.stringify(server.address())}`) console.log(`Budibase running on ${JSON.stringify(server.address())}`)