From 33ec0cfa7570e77020da0519e565e557d3621586 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 18 Aug 2020 16:05:15 +0100 Subject: [PATCH] group by complete --- .../builder/src/builderStore/store/backend.js | 15 ++-- .../database/DataTable/ViewDataTable.svelte | 43 ++++++---- .../DataTable/modals/DeleteView.svelte | 4 +- .../DataTable/popovers/GroupBy.svelte | 80 +++++++++++++++++++ .../nav/ModelNavigator/EditTable.svelte | 2 +- .../nav/ModelNavigator/EditView.svelte | 12 ++- .../nav/ModelNavigator/ModelNavigator.svelte | 18 +++-- .../backend/view/[selectedView]/index.svelte | 2 +- packages/server/src/api/controllers/record.js | 4 +- .../server/src/api/controllers/view/index.js | 28 +++---- packages/server/src/api/routes/view.js | 1 - 11 files changed, 155 insertions(+), 54 deletions(-) create mode 100644 packages/builder/src/components/database/DataTable/popovers/GroupBy.svelte diff --git a/packages/builder/src/builderStore/store/backend.js b/packages/builder/src/builderStore/store/backend.js index 0100d77934..25eb24d79b 100644 --- a/packages/builder/src/builderStore/store/backend.js +++ b/packages/builder/src/builderStore/store/backend.js @@ -93,10 +93,8 @@ export const getBackendUiStore = () => { // delete the original if renaming delete state.draftModel.schema[originalName] - state.draftModel.schema = { - ...state.draftModel.schema, - [field.name]: cloneDeep(field), - } + state.draftModel.schema[field.name] = cloneDeep(field) + store.actions.models.save(state.draftModel) return state }) @@ -126,8 +124,15 @@ export const getBackendUiStore = () => { }, save: async view => { await api.post(`/api/views`, view) + store.update(state => { - state.selectedModel.views[view.name] = view + const viewModel = state.models.find(model => model._id === view.modelId) + // TODO: Cleaner? + if (!viewModel.views) viewModel.views = {} + if (view.originalName) delete viewModel.views[view.originalName] + viewModel.views[view.name] = view + + state.models = state.models state.selectedView = view return state }) diff --git a/packages/builder/src/components/database/DataTable/ViewDataTable.svelte b/packages/builder/src/components/database/DataTable/ViewDataTable.svelte index 804bec6810..5c1317dd24 100644 --- a/packages/builder/src/components/database/DataTable/ViewDataTable.svelte +++ b/packages/builder/src/components/database/DataTable/ViewDataTable.svelte @@ -17,47 +17,62 @@ import ColumnHeaderPopover from "./popovers/ColumnHeader.svelte" import EditRowPopover from "./popovers/EditRow.svelte" import CalculationPopover from "./popovers/Calculate.svelte" + import GroupByPopover from "./popovers/GroupBy.svelte" - const COLUMNS = [ + let COLUMNS = [ + { + name: "Group", + key: "key", + }, { name: "sum", key: "value.sum", }, { name: "min", - key: "value.min", + key: "value.min" }, { name: "max", - key: "value.max", + key: "value.max" }, { name: "sumsqr", - key: "value.sumsqr", + key: "value.sumsqr" }, { name: "count", - key: "value.count", + key: "value.count" }, { name: "avg", - key: "value.avg", - }, + key: "value.avg" + } ] + export let view = {} + let data = [] - $: selectedView = $backendUiStore.selectedView - $: !selectedView.name.startsWith("all_") && fetchViewData(selectedView) + $: viewName = view.name + $: !viewName.startsWith("all_") && fetchViewData(viewName) - async function fetchViewData() { - const QUERY_VIEW_URL = `/api/views/${$backendUiStore.selectedView.name}?stats=true` + async function fetchViewData(viewName) { + let QUERY_VIEW_URL = `/api/views/${viewName}?stats=true` + if (view.groupBy) { + QUERY_VIEW_URL += `&group=${view.groupBy}` + } const response = await api.get(QUERY_VIEW_URL) data = await response.json() } - - -
+ + + +
\ No newline at end of file diff --git a/packages/builder/src/components/database/DataTable/modals/DeleteView.svelte b/packages/builder/src/components/database/DataTable/modals/DeleteView.svelte index f845b18425..5eef223013 100644 --- a/packages/builder/src/components/database/DataTable/modals/DeleteView.svelte +++ b/packages/builder/src/components/database/DataTable/modals/DeleteView.svelte @@ -4,7 +4,7 @@ import { store, backendUiStore } from "builderStore" import * as api from "../api" - export let view + export let viewName export let onClosed @@ -24,7 +24,7 @@ { - await backendUiStore.actions.views.delete(view) + await backendUiStore.actions.views.delete(viewName) notifier.danger('View deleted') onClosed() }}> diff --git a/packages/builder/src/components/database/DataTable/popovers/GroupBy.svelte b/packages/builder/src/components/database/DataTable/popovers/GroupBy.svelte new file mode 100644 index 0000000000..241933ebb3 --- /dev/null +++ b/packages/builder/src/components/database/DataTable/popovers/GroupBy.svelte @@ -0,0 +1,80 @@ + + +
+ +
+ +
Group By
+
+

Group By

+ +
+
+ + +
+
+ + diff --git a/packages/builder/src/components/nav/ModelNavigator/EditTable.svelte b/packages/builder/src/components/nav/ModelNavigator/EditTable.svelte index e4893e3a3f..e7926063dd 100644 --- a/packages/builder/src/components/nav/ModelNavigator/EditTable.svelte +++ b/packages/builder/src/components/nav/ModelNavigator/EditTable.svelte @@ -28,7 +28,7 @@ open( DeleteTableModal, { - onClosed: hideEditor, + onClosed: close, table, }, { styleContent: { padding: "0" } } diff --git a/packages/builder/src/components/nav/ModelNavigator/EditView.svelte b/packages/builder/src/components/nav/ModelNavigator/EditView.svelte index 4cd14e454c..e7599824e1 100644 --- a/packages/builder/src/components/nav/ModelNavigator/EditView.svelte +++ b/packages/builder/src/components/nav/ModelNavigator/EditView.svelte @@ -1,6 +1,7 @@ @@ -48,7 +54,7 @@ {#if editing}
Edit View
- +