diff --git a/packages/backend-core/src/cache/tests/docWritethrough.spec.ts b/packages/backend-core/src/cache/tests/docWritethrough.spec.ts index 5fe09b95ff..1ae85cfd0b 100644 --- a/packages/backend-core/src/cache/tests/docWritethrough.spec.ts +++ b/packages/backend-core/src/cache/tests/docWritethrough.spec.ts @@ -32,7 +32,7 @@ describe("docWritethrough", () => { describe("patch", () => { function generatePatchObject(fieldCount: number) { - const keys = generator.unique(() => generator.word(), fieldCount) + const keys = generator.unique(() => generator.guid(), fieldCount) return keys.reduce((acc, c) => { acc[c] = generator.word() return acc diff --git a/packages/bbui/src/Actions/click_outside.js b/packages/bbui/src/Actions/click_outside.js index 1961dca47c..62416ae88d 100644 --- a/packages/bbui/src/Actions/click_outside.js +++ b/packages/bbui/src/Actions/click_outside.js @@ -32,6 +32,13 @@ const handleClick = event => { return } + // Ignore clicks for drawers, unless the handler is registered from a drawer + const sourceInDrawer = handler.anchor.closest(".drawer-container") != null + const clickInDrawer = event.target.closest(".drawer-container") != null + if (clickInDrawer && !sourceInDrawer) { + return + } + handler.callback?.(event) }) } diff --git a/packages/bbui/src/Actions/position_dropdown.js b/packages/bbui/src/Actions/position_dropdown.js index d259b9197a..770d1bd507 100644 --- a/packages/bbui/src/Actions/position_dropdown.js +++ b/packages/bbui/src/Actions/position_dropdown.js @@ -15,6 +15,7 @@ export default function positionDropdown(element, opts) { align, maxHeight, maxWidth, + minWidth, useAnchorWidth, offset = 5, customUpdate, @@ -28,7 +29,7 @@ export default function positionDropdown(element, opts) { const elementBounds = element.getBoundingClientRect() let styles = { maxHeight: null, - minWidth: null, + minWidth, maxWidth, left: null, top: null, @@ -41,8 +42,13 @@ export default function positionDropdown(element, opts) { }) } else { // Determine vertical styles - if (align === "right-outside") { - styles.top = anchorBounds.top + if (align === "right-outside" || align === "left-outside") { + styles.top = + anchorBounds.top + anchorBounds.height / 2 - elementBounds.height / 2 + styles.maxHeight = maxHeight + if (styles.top + elementBounds.height > window.innerHeight) { + styles.top = window.innerHeight - elementBounds.height + } } else if ( window.innerHeight - anchorBounds.bottom < (maxHeight || 100) diff --git a/packages/bbui/src/Drawer/Drawer.svelte b/packages/bbui/src/Drawer/Drawer.svelte index 8976bfb81e..757f86b471 100644 --- a/packages/bbui/src/Drawer/Drawer.svelte +++ b/packages/bbui/src/Drawer/Drawer.svelte @@ -1,28 +1,109 @@ + + {#if visible} - -
- {#if !headless} + +
+
+
0} + class:modal={$modal} + transition:drawerSlide|local + {style} + >
-
- {title} - - - -
+
{title || "Bindings"}
- {/if} - -
+ +
+
+
{/if} diff --git a/packages/builder/src/components/common/CodeEditor/index.js b/packages/builder/src/components/common/CodeEditor/index.js index 0d71a475f0..c104267aa4 100644 --- a/packages/builder/src/components/common/CodeEditor/index.js +++ b/packages/builder/src/components/common/CodeEditor/index.js @@ -1,4 +1,3 @@ -import { EditorView } from "@codemirror/view" import { getManifest } from "@budibase/string-templates" import sanitizeHtml from "sanitize-html" import { groupBy } from "lodash" @@ -27,123 +26,33 @@ export const SECTIONS = { }, } -export const getDefaultTheme = opts => { - const { height, resize, dark } = opts - return EditorView.theme( - { - "&.cm-focused .cm-cursor": { - borderLeftColor: "var(--spectrum-alias-text-color)", - }, - "&": { - height: height ? `${height}` : "", - lineHeight: "1.3", - border: - "var(--spectrum-alias-border-size-thin) solid var(--spectrum-alias-border-color)", - borderRadius: "var(--border-radius-s)", - backgroundColor: - "var( --spectrum-textfield-m-background-color, var(--spectrum-global-color-gray-50) )", - resize: resize ? `${resize}` : "", - overflow: "hidden", - color: "var(--spectrum-alias-text-color)", - }, - "& .cm-tooltip.cm-tooltip-autocomplete > ul": { - fontFamily: - "var(--spectrum-alias-body-text-font-family, var(--spectrum-global-font-family-base))", - maxHeight: "16em", - }, - "& .cm-placeholder": { - color: "var(--spectrum-alias-text-color)", - fontStyle: "italic", - }, - "&.cm-focused": { - outline: "none", - borderColor: "var(--spectrum-alias-border-color-mouse-focus)", - }, - // AUTO COMPLETE - "& .cm-completionDetail": { - fontStyle: "unset", - textTransform: "uppercase", - fontSize: "10px", - backgroundColor: "var(--spectrum-global-color-gray-100)", - color: "var(--spectrum-global-color-gray-600)", - }, - "& .cm-completionLabel": { - marginLeft: - "calc(var(--spectrum-alias-workflow-icon-size-m) + var(--spacing-m))", - }, - "& .info-bubble": { - fontSize: "var(--font-size-s)", - display: "grid", - gridGap: "var(--spacing-s)", - gridTemplateColumns: "1fr", - color: "var(--spectrum-global-color-gray-800)", - }, - "& .cm-tooltip": { - marginLeft: "var(--spacing-s)", - border: "1px solid var(--spectrum-global-color-gray-300)", - borderRadius: - "var( --spectrum-popover-border-radius, var(--spectrum-alias-border-radius-regular) )", - backgroundColor: "var(--spectrum-global-color-gray-50)", - }, - // Section header - "& .info-section": { - display: "flex", - padding: "var(--spacing-s)", - gap: "var(--spacing-m)", - borderBottom: "1px solid var(--spectrum-global-color-gray-200)", - color: "var(--spectrum-global-color-gray-800)", - fontWeight: "bold", - }, - "& .info-section .spectrum-Icon": { - color: "var(--spectrum-global-color-gray-600)", - }, - // Autocomplete Option - "& .cm-tooltip.cm-tooltip-autocomplete .autocomplete-option": { - display: "flex", - justifyContent: "space-between", - alignItems: "center", - fontSize: "var(--spectrum-alias-font-size-default)", - padding: "var(--spacing-s)", - color: "var(--spectrum-global-color-gray-800)", - }, - "& .cm-tooltip-autocomplete ul li[aria-selected].autocomplete-option": { - backgroundColor: "var(--spectrum-global-color-gray-200)", - }, - "& .binding-wrap": { - color: "var(--spectrum-global-color-blue-700)", - fontFamily: "monospace", - }, - }, - { dark } - ) -} - export const buildHelperInfoNode = (completion, helper) => { const ele = document.createElement("div") ele.classList.add("info-bubble") const exampleNodeHtml = helper.example - ? `
${helper.example}
` + ? `
${helper.example}
` : "" const descriptionMarkup = sanitizeHtml(helper.description, { allowedTags: [], allowedAttributes: {}, }) - const descriptionNodeHtml = `
${descriptionMarkup}
` + const descriptionNodeHtml = `
${descriptionMarkup}
` ele.innerHTML = ` - ${exampleNodeHtml} ${descriptionNodeHtml} + ${exampleNodeHtml} ` return ele } const toSpectrumIcon = name => { return ` ` @@ -152,7 +61,9 @@ const toSpectrumIcon = name => { export const buildSectionHeader = (type, sectionName, icon, rank) => { const ele = document.createElement("div") ele.classList.add("info-section") - ele.classList.add(type) + if (type) { + ele.classList.add(type) + } ele.innerHTML = `${toSpectrumIcon(icon)}${sectionName}` return { name: sectionName, @@ -174,7 +85,7 @@ export const helpersToCompletion = (helpers, mode) => { }, type: "helper", section: helperSection, - detail: "FUNCTION", + detail: "Function", apply: (view, completion, from, to) => { insertBinding(view, from, to, key, mode) }, @@ -252,21 +163,12 @@ export const jsAutocomplete = baseCompletions => { } export const buildBindingInfoNode = (completion, binding) => { + if (!binding.valueHTML || binding.value == null) { + return null + } const ele = document.createElement("div") ele.classList.add("info-bubble") - - const exampleNodeHtml = binding.readableBinding - ? `
{{ ${binding.readableBinding} }}
` - : "" - - const descriptionNodeHtml = binding.description - ? `
${binding.description}
` - : "" - - ele.innerHTML = ` - ${exampleNodeHtml} - ${descriptionNodeHtml} - ` + ele.innerHTML = `
${binding.valueHTML}
` return ele } diff --git a/packages/builder/src/components/common/bindings/BindingPanel.svelte b/packages/builder/src/components/common/bindings/BindingPanel.svelte index 7b567e052b..aa765c03f6 100644 --- a/packages/builder/src/components/common/bindings/BindingPanel.svelte +++ b/packages/builder/src/components/common/bindings/BindingPanel.svelte @@ -1,27 +1,19 @@ - - + +
- { - if (selectedMode == mode) { - return true - } - - //Get the current mode value - const editorValue = usingJS ? decodeJSBinding(jsValue) : hbsValue - - if (editorValue) { - targetMode = selectedMode - return false - } - return true - }} - > - -
-
-
- {#if targetMode} -
-
- - {`Switch to ${targetMode}?`} - - This will discard anything in your binding -
- - -
-
-
- {/if} - -
- -
- - {#if sidebar} -
- -
- {/if} -
-
- {#if allowJS} - -
-
-
- {#if targetMode} -
-
- - {`Switch to ${targetMode}?`} - - This will discard anything in your binding -
- - -
-
-
- {/if} - - -
- -
- - {#if sidebar} -
- -
- {/if} -
-
- {/if} -
- {#if typeof drawerActions?.hide === "function" && drawerActions?.headless} - - {/if} - {#if typeof bindingDrawerActions?.save === "function" && drawerActions?.headless} -
+
+ {#each sideTabs as tab} + changeSidePanel(tab)} > - Save - + + + {/each} + {#if drawerContext && get(drawerContext.resizable)} + drawerContext.modal.set(!drawerIsModal)} + > + + {/if}
-
+
+
+ {#if mode === Modes.Text} + {#key hbsCompletions} + + {/key} + {:else if mode === Modes.JavaScript} + {#key jsCompletions} + + {/key} + {/if} + {#if targetMode} +
+
+ + Switch to {targetMode}? + + This will discard anything in your binding +
+ + +
+
+
+ {/if} +
-
-
+
+ {#if sidePanel === SidePanels.Bindings} + + {:else if sidePanel === SidePanels.Evaluation} + + {/if} +
+ + diff --git a/packages/builder/src/components/common/bindings/BindingPicker.svelte b/packages/builder/src/components/common/bindings/BindingPicker.svelte deleted file mode 100644 index 42057b382b..0000000000 --- a/packages/builder/src/components/common/bindings/BindingPicker.svelte +++ /dev/null @@ -1,399 +0,0 @@ - - - - - -
- {#if hoverTarget.title} -
{hoverTarget.title}
- {/if} - {#if hoverTarget.description} -
- - {@html hoverTarget.description} -
- {/if} - {#if hoverTarget.example} -
{hoverTarget.example}
- {/if} -
-
-
-
- - - - - - {#if selectedCategory} -
- { - selectedCategory = null - }} - > - Back - -
- {/if} - - {#if !selectedCategory} - - {/if} - - {#if !selectedCategory && !search} - - {/if} - - {#if selectedCategory || search} - {#each filteredCategories as category} - {#if category.bindings?.length} -
-
- {category.name} -
- -
- {/if} - {/each} - - {#if selectedCategory === "Helpers" || search} - {#if filteredHelpers?.length} -
-
Helpers
- -
- {/if} - {/if} - {/if} -
- - diff --git a/packages/builder/src/components/common/bindings/BindingSidePanel.svelte b/packages/builder/src/components/common/bindings/BindingSidePanel.svelte new file mode 100644 index 0000000000..d990451005 --- /dev/null +++ b/packages/builder/src/components/common/bindings/BindingSidePanel.svelte @@ -0,0 +1,422 @@ + + + +
+ {#if hoverTarget.description} +
+ + {@html hoverTarget.description} +
+ {/if} + {#if hoverTarget.code} + +
{@html hoverTarget.code}
+ {/if} +
+
+ + + + +
+ + {#if selectedCategory} +
+ (selectedCategory = null)} + /> + {selectedCategory} +
+ {/if} + + {#if !selectedCategory} +
+ + + + { + search = null + }} + class:searching={search} + > + + +
+ {/if} + + {#if !selectedCategory && !search} + + {/if} + + {#if selectedCategory || search} + {#each filteredCategories as category} + {#if category.bindings?.length} +
+ {#if filteredCategories.length > 1} +
+ {category.name} +
+ {/if} +
    + {#each category.bindings as binding} +
  • showBindingPopover(binding, e.target)} + on:mouseleave={hidePopover} + on:click={() => addBinding(binding)} + > + + {#if binding.display?.name} + {binding.display.name} + {:else if binding.fieldSchema?.name} + {binding.fieldSchema?.name} + {:else} + {binding.readableBinding} + {/if} + + {#if binding.display?.type || binding.fieldSchema?.type} + + + {binding.display?.type || binding.fieldSchema?.type} + + + {/if} +
  • + {/each} +
+
+ {/if} + {/each} + + {#if selectedCategory === "Helpers" || search} + {#if filteredHelpers?.length} +
+
    + {#each filteredHelpers as helper} +
  • showHelperPopover(helper, e.target)} + on:mouseleave={hidePopover} + on:click={() => addHelper(helper, mode.name === "javascript")} + > + {helper.displayText} + + function + +
  • + {/each} +
+
+ {/if} + {/if} + {/if} +
+
+ + diff --git a/packages/builder/src/components/common/bindings/ClientBindingPanel.svelte b/packages/builder/src/components/common/bindings/ClientBindingPanel.svelte index 7888f2e486..843dec8c89 100644 --- a/packages/builder/src/components/common/bindings/ClientBindingPanel.svelte +++ b/packages/builder/src/components/common/bindings/ClientBindingPanel.svelte @@ -1,8 +1,9 @@ - - - Add the objects on the left to enrich your text. - - - + + (tempValue = event.detail)} {bindings} {allowJS} diff --git a/packages/builder/src/components/common/bindings/DrawerBindableInput.svelte b/packages/builder/src/components/common/bindings/DrawerBindableInput.svelte index 80996a484d..d11ebcf87a 100644 --- a/packages/builder/src/components/common/bindings/DrawerBindableInput.svelte +++ b/packages/builder/src/components/common/bindings/DrawerBindableInput.svelte @@ -13,21 +13,21 @@ export let panel = ClientBindingPanel export let value = "" export let bindings = [] - export let title = "Bindings" + export let title export let placeholder export let label export let disabled = false - export let fillWidth export let allowJS = true export let allowHelpers = true export let updateOnChange = true - export let drawerLeft export let key export let disableBindings = false + export let forceModal = false + export let context = null const dispatch = createEventDispatcher() + let bindingDrawer - let valid = true let currentVal = value $: readableValue = runtimeToReadableBinding(bindings, value) @@ -88,27 +88,20 @@ - - Add the objects on the left to enrich your text. - - + (tempValue = event.detail)} {bindings} {allowJS} {allowHelpers} + {context} /> diff --git a/packages/builder/src/components/common/bindings/DrawerBindableSlot.svelte b/packages/builder/src/components/common/bindings/DrawerBindableSlot.svelte index 4cac24660f..8ce9dda209 100644 --- a/packages/builder/src/components/common/bindings/DrawerBindableSlot.svelte +++ b/packages/builder/src/components/common/bindings/DrawerBindableSlot.svelte @@ -16,7 +16,6 @@ export let placeholder export let label export let disabled = false - export let fillWidth export let allowJS = true export let allowHelpers = true export let updateOnChange = true @@ -26,7 +25,6 @@ const dispatch = createEventDispatcher() let bindingDrawer - let valid = true let currentVal = value $: readableValue = runtimeToReadableBinding(bindings, value) @@ -173,22 +171,14 @@ - - Add the objects on the left to enrich your text. - - + (tempValue = event.detail)} {bindings} diff --git a/packages/builder/src/components/common/bindings/EvaluationSidePanel.svelte b/packages/builder/src/components/common/bindings/EvaluationSidePanel.svelte new file mode 100644 index 0000000000..82c3f80a6f --- /dev/null +++ b/packages/builder/src/components/common/bindings/EvaluationSidePanel.svelte @@ -0,0 +1,133 @@ + + +
+
+
+ {#if error} + +
Error
+ {#if evaluating} +
+ +
+ {/if} + + + {:else} +
Preview
+ {#if evaluating} +
+ +
+ {/if} + + {#if !empty} + + {/if} + {/if} +
+
+
+ {#if empty} + Your expression will be evaluated here + {:else} + + {@html highlightedResult} + {/if} +
+
+ + diff --git a/packages/builder/src/components/common/bindings/ModalBindableInput.svelte b/packages/builder/src/components/common/bindings/ModalBindableInput.svelte index 41245ceaec..3261dc6b74 100644 --- a/packages/builder/src/components/common/bindings/ModalBindableInput.svelte +++ b/packages/builder/src/components/common/bindings/ModalBindableInput.svelte @@ -1,115 +1,12 @@ - - -
- onChange(event.detail)} - {placeholder} - {updateOnChange} - /> -
- -
-
- - - - Add the objects on the left to enrich your text. - -
- (tempValue = e.detail)} - {bindings} - {allowJS} - /> -
-
-
- - + diff --git a/packages/builder/src/components/common/bindings/ServerBindingPanel.svelte b/packages/builder/src/components/common/bindings/ServerBindingPanel.svelte index 213e5bbf1d..5718d080f2 100644 --- a/packages/builder/src/components/common/bindings/ServerBindingPanel.svelte +++ b/packages/builder/src/components/common/bindings/ServerBindingPanel.svelte @@ -2,9 +2,9 @@ import BindingPanel from "./BindingPanel.svelte" export let bindings = [] - export let valid export let value = "" export let allowJS = false + export let context = null $: enrichedBindings = enrichBindings(bindings) @@ -19,9 +19,9 @@ diff --git a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/ButtonActionEditor.svelte b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/ButtonActionEditor.svelte index d445c98a1a..f009c975cf 100644 --- a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/ButtonActionEditor.svelte +++ b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/ButtonActionEditor.svelte @@ -12,6 +12,7 @@ export let bindings export let nested export let componentInstance + export let title = "Actions" let drawer let tmpValue @@ -37,7 +38,7 @@ {actionText} - + Define what actions to run. diff --git a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DeleteRow.svelte b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DeleteRow.svelte index 431368d28f..5b7844ce53 100644 --- a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DeleteRow.svelte +++ b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/DeleteRow.svelte @@ -31,7 +31,7 @@ (parameters.rowId = value.detail)} /> diff --git a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/FetchRow.svelte b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/FetchRow.svelte index 4478f1bb16..61b7494ab2 100644 --- a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/FetchRow.svelte +++ b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/FetchRow.svelte @@ -29,7 +29,7 @@ (parameters.rowId = value.detail)} /> diff --git a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/SaveFields.svelte b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/SaveFields.svelte index 52bd84c453..e03af75207 100644 --- a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/SaveFields.svelte +++ b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/SaveFields.svelte @@ -62,7 +62,7 @@ {/if} updateFieldValue(idx, event.detail)} diff --git a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/ShowNotification.svelte b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/ShowNotification.svelte index 55b00d215d..d95e13cb5f 100644 --- a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/ShowNotification.svelte +++ b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/ShowNotification.svelte @@ -40,6 +40,7 @@ diff --git a/packages/builder/src/components/design/settings/controls/PropertyControl.svelte b/packages/builder/src/components/design/settings/controls/PropertyControl.svelte index 0f0276823a..4db9a03d80 100644 --- a/packages/builder/src/components/design/settings/controls/PropertyControl.svelte +++ b/packages/builder/src/components/design/settings/controls/PropertyControl.svelte @@ -105,6 +105,7 @@ onChange={handleChange} bindings={allBindings} name={key} + title={label} {nested} {key} {type} diff --git a/packages/builder/src/components/integration/KeyValueBuilder.svelte b/packages/builder/src/components/integration/KeyValueBuilder.svelte index e71090f613..74636fc50c 100644 --- a/packages/builder/src/components/integration/KeyValueBuilder.svelte +++ b/packages/builder/src/components/integration/KeyValueBuilder.svelte @@ -143,7 +143,6 @@ value={field.value} allowJS={false} {allowHelpers} - fillWidth={true} drawerLeft={bindingDrawerLeft} /> {:else} diff --git a/packages/builder/src/pages/builder/_layout.svelte b/packages/builder/src/pages/builder/_layout.svelte index 62d3951fb5..95ca05b87b 100644 --- a/packages/builder/src/pages/builder/_layout.svelte +++ b/packages/builder/src/pages/builder/_layout.svelte @@ -71,6 +71,10 @@ await auth.getSelf() await admin.init() + if ($admin.maintenance.length > 0) { + $redirect("./maintenance") + } + if ($auth.user) { await licensing.init() } diff --git a/packages/builder/src/pages/builder/app/[application]/_layout.svelte b/packages/builder/src/pages/builder/app/[application]/_layout.svelte index d671371787..fd6a97560d 100644 --- a/packages/builder/src/pages/builder/app/[application]/_layout.svelte +++ b/packages/builder/src/pages/builder/app/[application]/_layout.svelte @@ -188,7 +188,7 @@ {/if} - + diff --git a/packages/builder/src/pages/builder/app/[application]/design/[screenId]/_components/AppPanel.svelte b/packages/builder/src/pages/builder/app/[application]/design/[screenId]/_components/AppPanel.svelte index 5867021386..4617814485 100644 --- a/packages/builder/src/pages/builder/app/[application]/design/[screenId]/_components/AppPanel.svelte +++ b/packages/builder/src/pages/builder/app/[application]/design/[screenId]/_components/AppPanel.svelte @@ -6,6 +6,7 @@
+
@@ -32,7 +33,17 @@ flex-direction: column; justify-content: flex-start; align-items: stretch; - padding: 9px var(--spacing-m); + padding: 9px 10px 12px 10px; + position: relative; + transition: width 360ms ease-out; + } + .drawer-container { + position: absolute; + height: 100%; + width: 100%; + overflow: hidden; + top: 0; + left: 0; } .header { display: flex; diff --git a/packages/builder/src/pages/builder/app/[application]/design/[screenId]/_components/AppPreview.svelte b/packages/builder/src/pages/builder/app/[application]/design/[screenId]/_components/AppPreview.svelte index bf66c28a09..a910036a4a 100644 --- a/packages/builder/src/pages/builder/app/[application]/design/[screenId]/_components/AppPreview.svelte +++ b/packages/builder/src/pages/builder/app/[application]/design/[screenId]/_components/AppPreview.svelte @@ -196,6 +196,16 @@ } else if (type === "add-parent-component") { const { componentId, parentType } = data await componentStore.addParent(componentId, parentType) + } else if (type === "provide-context") { + let context = data?.context + if (context) { + try { + context = JSON.parse(context) + } catch (error) { + context = null + } + } + previewStore.setSelectedComponentContext(context) } else { console.warn(`Client sent unknown event type: ${type}`) } diff --git a/packages/builder/src/pages/builder/maintenance/index.svelte b/packages/builder/src/pages/builder/maintenance/index.svelte new file mode 100644 index 0000000000..e4c379885a --- /dev/null +++ b/packages/builder/src/pages/builder/maintenance/index.svelte @@ -0,0 +1,83 @@ + + +
+
+
+ +
+
+ {#each $admin.maintenance as maintenance} + {#if maintenance.type === MaintenanceType.SQS_MISSING} + + Please upgrade your Budibase installation + + We've detected that the version of Budibase you're using depends + on a more recent version of the CouchDB database than what you + have installed. + + + To resolve this, you can either rollback to a previous version of + Budibase, or follow the migration guide to update to a later + version of CouchDB. + + + + {/if} + {/each} +
+
+
+ + diff --git a/packages/builder/src/pages/builder/portal/_components/BudibaseLogo.svelte b/packages/builder/src/pages/builder/portal/_components/BudibaseLogo.svelte new file mode 100644 index 0000000000..b0dc4cda50 --- /dev/null +++ b/packages/builder/src/pages/builder/portal/_components/BudibaseLogo.svelte @@ -0,0 +1,15 @@ + + + + +Budibase Logo $goto("./apps")} /> + + diff --git a/packages/builder/src/stores/builder/preview.js b/packages/builder/src/stores/builder/preview.js index ecdd4573de..4923185ee7 100644 --- a/packages/builder/src/stores/builder/preview.js +++ b/packages/builder/src/stores/builder/preview.js @@ -4,6 +4,7 @@ const INITIAL_PREVIEW_STATE = { previewDevice: "desktop", previewEventHandler: null, showPreview: false, + selectedComponentContext: null, } export const createPreviewStore = () => { @@ -52,6 +53,17 @@ export const createPreviewStore = () => { }) } + const setSelectedComponentContext = context => { + store.update(state => { + state.selectedComponentContext = context + return state + }) + } + + const requestComponentContext = () => { + sendEvent("request-context") + } + return { subscribe: store.subscribe, setDevice, @@ -60,6 +72,8 @@ export const createPreviewStore = () => { startDrag, stopDrag, showPreview, + setSelectedComponentContext, + requestComponentContext, } } diff --git a/packages/builder/src/stores/portal/admin.js b/packages/builder/src/stores/portal/admin.js index 2106acac27..29d4585c06 100644 --- a/packages/builder/src/stores/portal/admin.js +++ b/packages/builder/src/stores/portal/admin.js @@ -17,6 +17,7 @@ export const DEFAULT_CONFIG = { adminUser: { checked: false }, sso: { checked: false }, }, + maintenance: [], offlineMode: false, } @@ -48,6 +49,7 @@ export function createAdminStore() { store.isDev = environment.isDev store.baseUrl = environment.baseUrl store.offlineMode = environment.offlineMode + store.maintenance = environment.maintenance return store }) } diff --git a/packages/client/src/components/ClientApp.svelte b/packages/client/src/components/ClientApp.svelte index 4efa8af4e6..7e30e21ae8 100644 --- a/packages/client/src/components/ClientApp.svelte +++ b/packages/client/src/components/ClientApp.svelte @@ -17,6 +17,7 @@ appStore, devToolsStore, devToolsEnabled, + environmentStore, } from "stores" import NotificationDisplay from "components/overlay/NotificationDisplay.svelte" import ConfirmationDisplay from "components/overlay/ConfirmationDisplay.svelte" @@ -36,6 +37,7 @@ import DevToolsHeader from "components/devtools/DevToolsHeader.svelte" import DevTools from "components/devtools/DevTools.svelte" import FreeFooter from "components/FreeFooter.svelte" + import MaintenanceScreen from "components/MaintenanceScreen.svelte" import licensing from "../licensing" // Provide contexts @@ -111,122 +113,128 @@ class="spectrum spectrum--medium {$themeStore.baseTheme} {$themeStore.theme}" class:builder={$builderStore.inBuilder} > - - - - - - - - {#key $builderStore.selectedComponentId} - {#if $builderStore.inBuilder} - - {/if} - {/key} - - -
- -
- {#if showDevTools} - + {#if $environmentStore.maintenance.length > 0} + + {:else} + + + + + + + + {#key $builderStore.selectedComponentId} + {#if $builderStore.inBuilder} + {/if} + {/key} -
- {#if permissionError} -
- - - {@html ErrorSVG} - - You don't have permission to use this app - - - Ask your administrator to grant you access - - -
- {:else if !$screenStore.activeLayout} -
- - - {@html ErrorSVG} - - Something went wrong rendering your app - - - Get in touch with support if this issue persists - - -
- {:else if embedNoScreens} -
- - - {@html ErrorSVG} - - This Budibase app is not publicly accessible - - -
- {:else} - - {#key $screenStore.activeLayout._id} - - {/key} + +
+ +
+ {#if showDevTools} + + {/if} - + {@html ErrorSVG} + + You don't have permission to use this app + + + Ask your administrator to grant you access + + +
+ {:else if !$screenStore.activeLayout} +
+ + + {@html ErrorSVG} + + Something went wrong rendering your app + + + Get in touch with support if this issue persists + + +
+ {:else if embedNoScreens} +
+ + + {@html ErrorSVG} + + This Budibase app is not publicly accessible + + +
+ {:else} + + {#key $screenStore.activeLayout._id} + + {/key} + + -
+
- - - {#if !$builderStore.inBuilder && licensing.logoEnabled()} - + + {#if $appStore.isDevApp} + + {/if} + {#if $builderStore.inBuilder || $devToolsStore.allowSelection} + + {/if} + {#if $builderStore.inBuilder} + + {/if}
- - - {#if $appStore.isDevApp} - - {/if} - {#if $builderStore.inBuilder || $devToolsStore.allowSelection} - - {/if} - {#if $builderStore.inBuilder} - - - {/if} -
- - - - - + + + + + + {/if}
{/if} diff --git a/packages/client/src/components/Component.svelte b/packages/client/src/components/Component.svelte index 6d6af91dcc..3d267ec623 100644 --- a/packages/client/src/components/Component.svelte +++ b/packages/client/src/components/Component.svelte @@ -575,6 +575,15 @@ } } + const getDataContext = () => { + const normalContext = get(context) + const additionalContext = ref?.getAdditionalDataContext?.() + return { + ...normalContext, + ...additionalContext, + } + } + onMount(() => { // Register this component instance for external access if ($appStore.isDevApp) { @@ -583,7 +592,7 @@ component: instance._component, getSettings: () => cachedSettings, getRawSettings: () => ({ ...staticSettings, ...dynamicSettings }), - getDataContext: () => get(context), + getDataContext, reload: () => initialise(instance, true), setEphemeralStyles: styles => (ephemeralStyles = styles), state: store, diff --git a/packages/client/src/components/MaintenanceScreen.svelte b/packages/client/src/components/MaintenanceScreen.svelte new file mode 100644 index 0000000000..a44c1292dd --- /dev/null +++ b/packages/client/src/components/MaintenanceScreen.svelte @@ -0,0 +1,53 @@ + + + +
+ {#each maintenanceList as maintenance} + {#if maintenance.type === MaintenanceType.SQS_MISSING} + + Budibase installation requires maintenance + + The administrator of this Budibase installation needs to take actions + to update components that are out of date. Please contact them and + show them this warning. More information will be available when they + log into their account. + + + {/if} + {/each} +
+ + diff --git a/packages/client/src/components/app/GridBlock.svelte b/packages/client/src/components/app/GridBlock.svelte index 0a343cac51..46a507387d 100644 --- a/packages/client/src/components/app/GridBlock.svelte +++ b/packages/client/src/components/app/GridBlock.svelte @@ -30,6 +30,7 @@ ActionTypes, createContextStore, Provider, + generateGoldenSample, } = getContext("sdk") let grid @@ -48,6 +49,19 @@ }, ] + // Provide additional data context for live binding eval + export const getAdditionalDataContext = () => { + const rows = get(grid?.getContext()?.rows) + const goldenRow = generateGoldenSample(rows) + const id = get(component).id + return { + [id]: goldenRow, + eventContext: { + row: goldenRow, + }, + } + } + // Parses columns to fix older formats const getParsedColumns = columns => { // If the first element has an active key all elements should be in the new format diff --git a/packages/client/src/components/app/blocks/CardsBlock.svelte b/packages/client/src/components/app/blocks/CardsBlock.svelte index 008fa7e730..bd2b69d352 100644 --- a/packages/client/src/components/app/blocks/CardsBlock.svelte +++ b/packages/client/src/components/app/blocks/CardsBlock.svelte @@ -4,6 +4,7 @@ import BlockComponent from "components/BlockComponent.svelte" import { makePropSafe as safe } from "@budibase/string-templates" import { enrichSearchColumns, enrichFilter } from "utils/blocks.js" + import { get } from "svelte/store" export let title export let dataSource @@ -31,7 +32,9 @@ export let linkColumn export let noRowsMessage - const { fetchDatasourceSchema } = getContext("sdk") + const context = getContext("context") + const { fetchDatasourceSchema, generateGoldenSample } = getContext("sdk") + const component = getContext("component") let formId let dataProviderId @@ -62,6 +65,16 @@ }, ] + // Provide additional data context for live binding eval + export const getAdditionalDataContext = () => { + const rows = get(context)[dataProviderId]?.rows || [] + const goldenRow = generateGoldenSample(rows) + const id = get(component).id + return { + [`${id}-repeater`]: goldenRow, + } + } + // Builds a full details page URL for the card title const buildFullCardUrl = (link, url, repeaterId, linkColumn) => { if (!link || !url || !repeaterId) { diff --git a/packages/client/src/components/app/blocks/MultiStepFormblock.svelte b/packages/client/src/components/app/blocks/MultiStepFormblock.svelte index b24c2f418a..1a0fbbd50d 100644 --- a/packages/client/src/components/app/blocks/MultiStepFormblock.svelte +++ b/packages/client/src/components/app/blocks/MultiStepFormblock.svelte @@ -5,7 +5,7 @@ import { builderStore } from "stores" import { Utils } from "@budibase/frontend-core" import FormBlockWrapper from "./form/FormBlockWrapper.svelte" - import { writable } from "svelte/store" + import { get, writable } from "svelte/store" export let actionType export let rowId @@ -15,7 +15,7 @@ export let buttonPosition = "bottom" export let size - const { fetchDatasourceSchema } = getContext("sdk") + const { fetchDatasourceSchema, generateGoldenSample } = getContext("sdk") const component = getContext("component") const context = getContext("context") @@ -45,6 +45,16 @@ $: enrichedSteps = enrichSteps(steps, schema, $component.id, $currentStep) $: updateCurrentStep(enrichedSteps, $builderStore, $component) + // Provide additional data context for live binding eval + export const getAdditionalDataContext = () => { + const id = get(component).id + const rows = get(context)[`${id}-provider`]?.rows || [] + const goldenRow = generateGoldenSample(rows) + return { + [`${id}-repeater`]: goldenRow, + } + } + const updateCurrentStep = (steps, builderStore, component) => { const { componentId, step } = builderStore.metadata || {} diff --git a/packages/client/src/components/app/blocks/RepeaterBlock.svelte b/packages/client/src/components/app/blocks/RepeaterBlock.svelte index 30fbdddcdc..878b827c78 100644 --- a/packages/client/src/components/app/blocks/RepeaterBlock.svelte +++ b/packages/client/src/components/app/blocks/RepeaterBlock.svelte @@ -4,6 +4,7 @@ import Placeholder from "components/app/Placeholder.svelte" import { getContext } from "svelte" import { makePropSafe as safe } from "@budibase/string-templates" + import { get } from "svelte/store" export let dataSource export let filter @@ -18,8 +19,20 @@ export let gap const component = getContext("component") + const context = getContext("context") + const { generateGoldenSample } = getContext("sdk") let providerId + + // Provide additional data context for live binding eval + export const getAdditionalDataContext = () => { + const rows = get(context)[providerId]?.rows || [] + const goldenRow = generateGoldenSample(rows) + const id = get(component).id + return { + [`${id}-repeater`]: goldenRow, + } + } diff --git a/packages/client/src/components/app/blocks/RowExplorer.svelte b/packages/client/src/components/app/blocks/RowExplorer.svelte index 8fadcb5006..1e2357713a 100644 --- a/packages/client/src/components/app/blocks/RowExplorer.svelte +++ b/packages/client/src/components/app/blocks/RowExplorer.svelte @@ -3,25 +3,35 @@ import BlockComponent from "components/BlockComponent.svelte" import { makePropSafe as safe } from "@budibase/string-templates" import { generate } from "shortid" + import { get } from "svelte/store" + import { getContext } from "svelte" export let dataSource export let height - export let cardTitle export let cardSubtitle export let cardDescription export let cardImageURL export let cardSearchField - export let detailFields export let detailTitle - export let noRowsMessage const stateKey = generate() + const context = getContext("context") + const { generateGoldenSample } = getContext("sdk") let listDataProviderId let listRepeaterId + + // Provide additional data context for live binding eval + export const getAdditionalDataContext = () => { + const rows = get(context)[listDataProviderId]?.rows || [] + const goldenRow = generateGoldenSample(rows) + return { + [listRepeaterId]: goldenRow, + } + } diff --git a/packages/client/src/components/app/blocks/form/FormBlock.svelte b/packages/client/src/components/app/blocks/form/FormBlock.svelte index cdf1a05628..8c084a71ab 100644 --- a/packages/client/src/components/app/blocks/form/FormBlock.svelte +++ b/packages/client/src/components/app/blocks/form/FormBlock.svelte @@ -3,6 +3,7 @@ import InnerFormBlock from "./InnerFormBlock.svelte" import { Utils } from "@budibase/frontend-core" import FormBlockWrapper from "./FormBlockWrapper.svelte" + import { get } from "svelte/store" export let actionType export let dataSource @@ -11,7 +12,6 @@ export let fields export let buttons export let buttonPosition - export let title export let description export let rowId @@ -25,8 +25,56 @@ export let saveButtonLabel export let deleteButtonLabel - const { fetchDatasourceSchema } = getContext("sdk") + const { fetchDatasourceSchema, generateGoldenSample } = getContext("sdk") const component = getContext("component") + const context = getContext("context") + + let schema + + $: formattedFields = convertOldFieldFormat(fields) + $: fieldsOrDefault = getDefaultFields(formattedFields, schema) + $: fetchSchema(dataSource) + // We could simply spread $$props into the inner form and append our + // additions, but that would create svelte warnings about unused props and + // make maintenance in future more confusing as we typically always have a + // proper mapping of schema settings to component exports, without having to + // search multiple files + $: innerProps = { + dataSource, + actionUrl, + actionType, + size, + disabled, + fields: fieldsOrDefault, + title, + description, + schema, + notificationOverride, + buttons: + buttons || + Utils.buildFormBlockButtonConfig({ + _id: $component.id, + showDeleteButton, + showSaveButton, + saveButtonLabel, + deleteButtonLabel, + notificationOverride, + actionType, + actionUrl, + dataSource, + }), + buttonPosition: buttons ? buttonPosition : "top", + } + + // Provide additional data context for live binding eval + export const getAdditionalDataContext = () => { + const id = get(component).id + const rows = get(context)[`${id}-provider`]?.rows || [] + const goldenRow = generateGoldenSample(rows) + return { + [`${id}-repeater`]: goldenRow, + } + } const convertOldFieldFormat = fields => { if (!fields) { @@ -68,42 +116,6 @@ return [...fields, ...defaultFields].filter(field => field.active) } - let schema - - $: formattedFields = convertOldFieldFormat(fields) - $: fieldsOrDefault = getDefaultFields(formattedFields, schema) - $: fetchSchema(dataSource) - // We could simply spread $$props into the inner form and append our - // additions, but that would create svelte warnings about unused props and - // make maintenance in future more confusing as we typically always have a - // proper mapping of schema settings to component exports, without having to - // search multiple files - $: innerProps = { - dataSource, - actionUrl, - actionType, - size, - disabled, - fields: fieldsOrDefault, - title, - description, - schema, - notificationOverride, - buttons: - buttons || - Utils.buildFormBlockButtonConfig({ - _id: $component.id, - showDeleteButton, - showSaveButton, - saveButtonLabel, - deleteButtonLabel, - notificationOverride, - actionType, - actionUrl, - dataSource, - }), - buttonPosition: buttons ? buttonPosition : "top", - } const fetchSchema = async () => { schema = (await fetchDatasourceSchema(dataSource)) || {} } diff --git a/packages/client/src/components/app/dynamic-filter/DynamicFilter.svelte b/packages/client/src/components/app/dynamic-filter/DynamicFilter.svelte index 549574e89b..2f7af56744 100644 --- a/packages/client/src/components/app/dynamic-filter/DynamicFilter.svelte +++ b/packages/client/src/components/app/dynamic-filter/DynamicFilter.svelte @@ -58,13 +58,13 @@ } let clonedSchema = {} if (!allowedFields?.length) { - clonedSchema = schema + clonedSchema = schema || {} } else { allowedFields?.forEach(field => { - if (schema[field.name]) { + if (schema?.[field.name]) { clonedSchema[field.name] = schema[field.name] clonedSchema[field.name].displayName = field.displayName - } else if (schema[field]) { + } else if (schema?.[field]) { clonedSchema[field] = schema[field] } }) diff --git a/packages/client/src/index.js b/packages/client/src/index.js index 2c8d310619..1ce7101466 100644 --- a/packages/client/src/index.js +++ b/packages/client/src/index.js @@ -84,6 +84,18 @@ const loadBudibase = async () => { } else { dndStore.actions.reset() } + } else if (type === "request-context") { + const { selectedComponentInstance } = get(componentStore) + const context = selectedComponentInstance?.getDataContext() + let stringifiedContext = null + try { + stringifiedContext = JSON.stringify(context) + } catch (error) { + // Ignore - invalid context + } + eventStore.actions.dispatchEvent("provide-context", { + context: stringifiedContext, + }) } else if (type === "hover-component") { hoverStore.actions.hoverComponent(data) } else if (type === "builder-meta") { diff --git a/packages/client/src/sdk.js b/packages/client/src/sdk.js index 557d5ca8da..d86d635970 100644 --- a/packages/client/src/sdk.js +++ b/packages/client/src/sdk.js @@ -29,7 +29,12 @@ import { fetchDatasourceSchema } from "./utils/schema.js" import { getAPIKey } from "./utils/api.js" import { enrichButtonActions } from "./utils/buttonActions.js" import { processStringSync, makePropSafe } from "@budibase/string-templates" -import { fetchData, LuceneUtils, Constants } from "@budibase/frontend-core" +import { + fetchData, + LuceneUtils, + Constants, + RowUtils, +} from "@budibase/frontend-core" export default { API, @@ -65,6 +70,7 @@ export default { processStringSync, makePropSafe, createContextStore, + generateGoldenSample: RowUtils.generateGoldenSample, // Components Provider, diff --git a/packages/frontend-core/src/utils/index.js b/packages/frontend-core/src/utils/index.js index 3f00c00e47..98998b7f0e 100644 --- a/packages/frontend-core/src/utils/index.js +++ b/packages/frontend-core/src/utils/index.js @@ -3,6 +3,7 @@ export * as JSONUtils from "./json" export * as CookieUtils from "./cookies" export * as RoleUtils from "./roles" export * as Utils from "./utils" +export * as RowUtils from "./rows" export { memo, derivedMemo } from "./memo" export { createWebsocket } from "./websocket" export * from "./download" diff --git a/packages/frontend-core/src/utils/rows.js b/packages/frontend-core/src/utils/rows.js new file mode 100644 index 0000000000..ea43d63734 --- /dev/null +++ b/packages/frontend-core/src/utils/rows.js @@ -0,0 +1,48 @@ +/** + * Util to check is a given value is "better" than another. "Betterness" is + * defined as presence and length. + */ +const isBetterSample = (newValue, oldValue) => { + // Prefer non-null values + if (oldValue == null && newValue != null) { + return true + } + + // Don't change type + const oldType = typeof oldValue + const newType = typeof newValue + if (oldType !== newType) { + return false + } + + // Prefer longer values + if (newType === "string" && newValue.length > oldValue.length) { + return true + } + if ( + newType === "object" && + Object.keys(newValue).length > Object.keys(oldValue).length + ) { + return true + } + + return false +} + +/** + * Generates a best-case example object of the provided samples. + * The generated sample does not necessarily exist - it simply is a sample that + * contains "good" examples for every property of all the samples. + * The generate sample will have a value for all keys across all samples. + */ +export const generateGoldenSample = samples => { + let goldenSample = {} + samples?.slice(0, 100).forEach(sample => { + Object.keys(sample).forEach(key => { + if (isBetterSample(sample[key], goldenSample[key])) { + goldenSample[key] = sample[key] + } + }) + }) + return goldenSample +} diff --git a/packages/server/src/api/routes/tests/__snapshots__/view.spec.js.snap b/packages/server/src/api/routes/tests/__snapshots__/view.spec.js.snap deleted file mode 100644 index 357285a69a..0000000000 --- a/packages/server/src/api/routes/tests/__snapshots__/view.spec.js.snap +++ /dev/null @@ -1,38 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`/views query returns data for the created view 1`] = ` -[ - { - "avg": 2333.3333333333335, - "count": 3, - "group": null, - "max": 4000, - "min": 1000, - "sum": 7000, - "sumsqr": 21000000, - }, -] -`; - -exports[`/views query returns data for the created view using a group by 1`] = ` -[ - { - "avg": 1500, - "count": 2, - "group": "One", - "max": 2000, - "min": 1000, - "sum": 3000, - "sumsqr": 5000000, - }, - { - "avg": 4000, - "count": 1, - "group": "Two", - "max": 4000, - "min": 4000, - "sum": 4000, - "sumsqr": 16000000, - }, -] -`; diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index d50dd8a3d9..371045687b 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -7,6 +7,7 @@ import { context, InternalTable, roles, tenancy } from "@budibase/backend-core" import { quotas } from "@budibase/pro" import { AutoFieldSubType, + Datasource, DeleteRow, FieldSchema, FieldType, @@ -24,15 +25,15 @@ import { StaticQuotaName, Table, TableSourceType, + ViewV2, } from "@budibase/types" import { expectAnyExternalColsAttributes, expectAnyInternalColsAttributes, generator, mocks, - structures, } from "@budibase/backend-core/tests" -import _ from "lodash" +import _, { merge } from "lodash" import * as uuid from "uuid" const timestamp = new Date("2023-01-26T11:48:57.597Z").toISOString() @@ -42,8 +43,6 @@ jest.unmock("mysql2") jest.unmock("mysql2/promise") jest.unmock("mssql") -const { basicRow } = setup.structures - describe.each([ ["internal", undefined], ["postgres", databaseTestProviders.postgres], @@ -51,12 +50,20 @@ describe.each([ ["mssql", databaseTestProviders.mssql], ["mariadb", databaseTestProviders.mariadb], ])("/rows (%s)", (__, dsProvider) => { - const isInternal = !dsProvider - - const request = setup.getRequest() + const isInternal = dsProvider === undefined const config = setup.getConfig() + let table: Table - let tableId: string + let datasource: Datasource | undefined + + beforeAll(async () => { + await config.init() + if (dsProvider) { + datasource = await config.createDatasource({ + datasource: await dsProvider.datasource(), + }) + } + }) afterAll(async () => { if (dsProvider) { @@ -65,24 +72,17 @@ describe.each([ setup.afterAll() }) - beforeAll(async () => { - await config.init() - - if (dsProvider) { - await config.createDatasource({ - datasource: await dsProvider.datasource(), - }) - } - }) - - const generateTableConfig: () => SaveTableRequest = () => { - return { + function saveTableRequest( + ...overrides: Partial[] + ): SaveTableRequest { + const req: SaveTableRequest = { name: uuid.v4().substring(0, 16), type: "table", + sourceType: datasource + ? TableSourceType.EXTERNAL + : TableSourceType.INTERNAL, + sourceId: datasource ? datasource._id! : INTERNAL_TABLE_SOURCE_ID, primary: ["id"], - primaryDisplay: "name", - sourceType: TableSourceType.INTERNAL, - sourceId: INTERNAL_TABLE_SOURCE_ID, schema: { id: { type: FieldType.AUTO, @@ -92,22 +92,36 @@ describe.each([ presence: true, }, }, - name: { - type: FieldType.STRING, - name: "name", - constraints: { - type: "string", + }, + } + return merge(req, ...overrides) + } + + function defaultTable( + ...overrides: Partial[] + ): SaveTableRequest { + return saveTableRequest( + { + primaryDisplay: "name", + schema: { + name: { + type: FieldType.STRING, + name: "name", + constraints: { + type: "string", + }, }, - }, - description: { - type: FieldType.STRING, - name: "description", - constraints: { - type: "string", + description: { + type: FieldType.STRING, + name: "description", + constraints: { + type: "string", + }, }, }, }, - } + ...overrides + ) } beforeEach(async () => { @@ -134,53 +148,28 @@ describe.each([ } : undefined - async function createTable( - cfg: Omit, - opts?: { skipReassigning: boolean } - ) { - let table - if (dsProvider) { - table = await config.createExternalTable(cfg, opts) - } else { - table = await config.createTable(cfg, opts) - } - return table - } - beforeAll(async () => { - const tableConfig = generateTableConfig() - let table = await createTable(tableConfig) - tableId = table._id! + table = await config.api.table.save(defaultTable()) }) describe("save, load, update", () => { it("returns a success message when the row is created", async () => { const rowUsage = await getRowUsage() - - const res = await request - .post(`/api/${tableId}/rows`) - .send(basicRow(tableId)) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect((res as any).res.statusMessage).toEqual( - `${config.table!.name} saved successfully` - ) - expect(res.body.name).toEqual("Test Contact") - expect(res.body._rev).toBeDefined() + const row = await config.api.row.save(table._id!, { + name: "Test Contact", + }) + expect(row.name).toEqual("Test Contact") + expect(row._rev).toBeDefined() await assertRowUsage(rowUsage + 1) }) it("Increment row autoId per create row request", async () => { const rowUsage = await getRowUsage() - const tableConfig = generateTableConfig() - const newTable = await createTable( - { - ...tableConfig, + const newTable = await config.api.table.save( + saveTableRequest({ name: "TestTableAuto", schema: { - ...tableConfig.schema, "Row ID": { name: "Row ID", type: FieldType.NUMBER, @@ -197,37 +186,25 @@ describe.each([ }, }, }, - }, - { skipReassigning: true } + }) ) - const ids = [1, 2, 3] - - // Performing several create row requests should increment the autoID fields accordingly - const createRow = async (id: number) => { - const res = await config.api.row.save(newTable._id!, { - name: "row_" + id, - }) - expect(res.name).toEqual("row_" + id) - expect(res._rev).toBeDefined() - expect(res["Row ID"]).toEqual(id) + let previousId = 0 + for (let i = 0; i < 10; i++) { + const row = await config.api.row.save(newTable._id!, {}) + expect(row["Row ID"]).toBeGreaterThan(previousId) + previousId = row["Row ID"] } - - for (let i = 0; i < ids.length; i++) { - await createRow(ids[i]) - } - - await assertRowUsage(rowUsage + ids.length) + await assertRowUsage(rowUsage + 10) }) it("updates a row successfully", async () => { - const existing = await config.createRow() + const existing = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() - const res = await config.api.row.save(tableId, { + const res = await config.api.row.save(table._id!, { _id: existing._id, _rev: existing._rev, - tableId, name: "Updated Name", }) @@ -236,9 +213,9 @@ describe.each([ }) it("should load a row", async () => { - const existing = await config.createRow() + const existing = await config.api.row.save(table._id!, {}) - const res = await config.api.row.get(tableId, existing._id!) + const res = await config.api.row.get(table._id!, existing._id!) expect(res).toEqual({ ...existing, @@ -247,29 +224,22 @@ describe.each([ }) it("should list all rows for given tableId", async () => { - const table = await createTable(generateTableConfig(), { - skipReassigning: true, - }) - const tableId = table._id! - const newRow = { - tableId, - name: "Second Contact", - description: "new", - } - const firstRow = await config.createRow({ tableId }) - await config.createRow(newRow) + const table = await config.api.table.save(defaultTable()) + const rows = await Promise.all([ + config.api.row.save(table._id!, {}), + config.api.row.save(table._id!, {}), + ]) - const res = await config.api.row.fetch(tableId) - - expect(res.length).toBe(2) - expect(res.find((r: Row) => r.name === newRow.name)).toBeDefined() - expect(res.find((r: Row) => r.name === firstRow.name)).toBeDefined() + const res = await config.api.row.fetch(table._id!) + expect(res.map(r => r._id)).toEqual( + expect.arrayContaining(rows.map(r => r._id)) + ) }) it("load should return 404 when row does not exist", async () => { - await config.createRow() - - await config.api.row.get(tableId, "1234567", { + const table = await config.api.table.save(defaultTable()) + await config.api.row.save(table._id!, {}) + await config.api.row.get(table._id!, "1234567", { status: 404, }) }) @@ -324,45 +294,49 @@ describe.each([ inclusion: ["Alpha", "Beta", "Gamma"], }, } - const table = await createTable({ - name: "TestTable2", - type: "table", - schema: { - name: str, - stringUndefined: str, - stringNull: str, - stringString: str, - numberEmptyString: number, - numberNull: number, - numberUndefined: number, - numberString: number, - numberNumber: number, - datetimeEmptyString: datetime, - datetimeNull: datetime, - datetimeUndefined: datetime, - datetimeString: datetime, - datetimeDate: datetime, - boolNull: bool, - boolEmpty: bool, - boolUndefined: bool, - boolString: bool, - boolBool: bool, - attachmentNull: attachment, - attachmentUndefined: attachment, - attachmentEmpty: attachment, - attachmentEmptyArrayStr: attachment, - arrayFieldEmptyArrayStr: arrayField, - arrayFieldArrayStrKnown: arrayField, - arrayFieldNull: arrayField, - arrayFieldUndefined: arrayField, - optsFieldEmptyStr: optsField, - optsFieldUndefined: optsField, - optsFieldNull: optsField, - optsFieldStrKnown: optsField, - }, - }) + const table = await config.api.table.save( + saveTableRequest({ + name: "TestTable2", + type: "table", + schema: { + name: str, + stringUndefined: str, + stringNull: str, + stringString: str, + numberEmptyString: number, + numberNull: number, + numberUndefined: number, + numberString: number, + numberNumber: number, + datetimeEmptyString: datetime, + datetimeNull: datetime, + datetimeUndefined: datetime, + datetimeString: datetime, + datetimeDate: datetime, + boolNull: bool, + boolEmpty: bool, + boolUndefined: bool, + boolString: bool, + boolBool: bool, + attachmentNull: attachment, + attachmentUndefined: attachment, + attachmentEmpty: attachment, + attachmentEmptyArrayStr: attachment, + arrayFieldEmptyArrayStr: arrayField, + arrayFieldArrayStrKnown: arrayField, + arrayFieldNull: arrayField, + arrayFieldUndefined: arrayField, + optsFieldEmptyStr: optsField, + optsFieldUndefined: optsField, + optsFieldNull: optsField, + optsFieldStrKnown: optsField, + }, + }) + ) - const row = { + const datetimeStr = "1984-04-20T00:00:00.000Z" + + const row = await config.api.row.save(table._id!, { name: "Test Row", stringUndefined: undefined, stringNull: null, @@ -375,8 +349,8 @@ describe.each([ datetimeEmptyString: "", datetimeNull: null, datetimeUndefined: undefined, - datetimeString: "1984-04-20T00:00:00.000Z", - datetimeDate: new Date("1984-04-20"), + datetimeString: datetimeStr, + datetimeDate: new Date(datetimeStr), boolNull: null, boolEmpty: "", boolUndefined: undefined, @@ -395,79 +369,66 @@ describe.each([ optsFieldUndefined: undefined, optsFieldNull: null, optsFieldStrKnown: "Alpha", - } + }) - const createdRow = await config.createRow(row) - const id = createdRow._id! - - const saved = await config.api.row.get(table._id!, id) - - expect(saved.stringUndefined).toBe(undefined) - expect(saved.stringNull).toBe(null) - expect(saved.stringString).toBe("i am a string") - expect(saved.numberEmptyString).toBe(null) - expect(saved.numberNull).toBe(null) - expect(saved.numberUndefined).toBe(undefined) - expect(saved.numberString).toBe(123) - expect(saved.numberNumber).toBe(123) - expect(saved.datetimeEmptyString).toBe(null) - expect(saved.datetimeNull).toBe(null) - expect(saved.datetimeUndefined).toBe(undefined) - expect(saved.datetimeString).toBe( - new Date(row.datetimeString).toISOString() - ) - expect(saved.datetimeDate).toBe(row.datetimeDate.toISOString()) - expect(saved.boolNull).toBe(null) - expect(saved.boolEmpty).toBe(null) - expect(saved.boolUndefined).toBe(undefined) - expect(saved.boolString).toBe(true) - expect(saved.boolBool).toBe(true) - expect(saved.attachmentNull).toEqual([]) - expect(saved.attachmentUndefined).toBe(undefined) - expect(saved.attachmentEmpty).toEqual([]) - expect(saved.attachmentEmptyArrayStr).toEqual([]) - expect(saved.arrayFieldEmptyArrayStr).toEqual([]) - expect(saved.arrayFieldNull).toEqual([]) - expect(saved.arrayFieldUndefined).toEqual(undefined) - expect(saved.optsFieldEmptyStr).toEqual(null) - expect(saved.optsFieldUndefined).toEqual(undefined) - expect(saved.optsFieldNull).toEqual(null) - expect(saved.arrayFieldArrayStrKnown).toEqual(["One"]) - expect(saved.optsFieldStrKnown).toEqual("Alpha") + expect(row.stringUndefined).toBe(undefined) + expect(row.stringNull).toBe(null) + expect(row.stringString).toBe("i am a string") + expect(row.numberEmptyString).toBe(null) + expect(row.numberNull).toBe(null) + expect(row.numberUndefined).toBe(undefined) + expect(row.numberString).toBe(123) + expect(row.numberNumber).toBe(123) + expect(row.datetimeEmptyString).toBe(null) + expect(row.datetimeNull).toBe(null) + expect(row.datetimeUndefined).toBe(undefined) + expect(row.datetimeString).toBe(new Date(datetimeStr).toISOString()) + expect(row.datetimeDate).toBe(new Date(datetimeStr).toISOString()) + expect(row.boolNull).toBe(null) + expect(row.boolEmpty).toBe(null) + expect(row.boolUndefined).toBe(undefined) + expect(row.boolString).toBe(true) + expect(row.boolBool).toBe(true) + expect(row.attachmentNull).toEqual([]) + expect(row.attachmentUndefined).toBe(undefined) + expect(row.attachmentEmpty).toEqual([]) + expect(row.attachmentEmptyArrayStr).toEqual([]) + expect(row.arrayFieldEmptyArrayStr).toEqual([]) + expect(row.arrayFieldNull).toEqual([]) + expect(row.arrayFieldUndefined).toEqual(undefined) + expect(row.optsFieldEmptyStr).toEqual(null) + expect(row.optsFieldUndefined).toEqual(undefined) + expect(row.optsFieldNull).toEqual(null) + expect(row.arrayFieldArrayStrKnown).toEqual(["One"]) + expect(row.optsFieldStrKnown).toEqual("Alpha") }) }) describe("view save", () => { it("views have extra data trimmed", async () => { - const table = await createTable({ - type: "table", - name: "orders", - primary: ["OrderID"], - schema: { - Country: { - type: FieldType.STRING, - name: "Country", + const table = await config.api.table.save( + saveTableRequest({ + name: "orders", + schema: { + Country: { + type: FieldType.STRING, + name: "Country", + }, + Story: { + type: FieldType.STRING, + name: "Story", + }, }, - OrderID: { - type: FieldType.NUMBER, - name: "OrderID", - }, - Story: { - type: FieldType.STRING, - name: "Story", - }, - }, - }) + }) + ) - const createViewResponse = await config.createView({ + const createViewResponse = await config.api.viewV2.create({ + tableId: table._id!, name: uuid.v4(), schema: { Country: { visible: true, }, - OrderID: { - visible: true, - }, }, }) @@ -483,8 +444,8 @@ describe.each([ expect(row.Story).toBeUndefined() expect(row).toEqual({ ...defaultRowFields, - OrderID: createRowResponse.OrderID, Country: "Aussy", + id: createRowResponse.id, _id: createRowResponse._id, _rev: createRowResponse._rev, tableId: table._id, @@ -496,25 +457,25 @@ describe.each([ let otherTable: Table beforeAll(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) - const otherTableConfig = generateTableConfig() - // need a short name of table here - for relationship tests - otherTableConfig.name = "a" - otherTableConfig.schema.relationship = { - name: "relationship", - relationshipType: RelationshipType.ONE_TO_MANY, - type: FieldType.LINK, - tableId: table._id!, - fieldName: "relationship", - } - otherTable = await createTable(otherTableConfig) - // need to set the config back to the original table - config.table = table + table = await config.api.table.save(defaultTable()) + otherTable = await config.api.table.save( + defaultTable({ + name: "a", + schema: { + relationship: { + name: "relationship", + relationshipType: RelationshipType.ONE_TO_MANY, + type: FieldType.LINK, + tableId: table._id!, + fieldName: "relationship", + }, + }, + }) + ) }) it("should update only the fields that are supplied", async () => { - const existing = await config.createRow() + const existing = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() @@ -536,7 +497,7 @@ describe.each([ }) it("should throw an error when given improper types", async () => { - const existing = await config.createRow() + const existing = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() await config.api.row.patch( @@ -628,12 +589,11 @@ describe.each([ describe("destroy", () => { beforeAll(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) + table = await config.api.table.save(defaultTable()) }) it("should be able to delete a row", async () => { - const createdRow = await config.createRow() + const createdRow = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() const res = await config.api.row.bulkDelete(table._id!, { @@ -644,7 +604,7 @@ describe.each([ }) it("should be able to bulk delete rows, including a row that doesn't exist", async () => { - const createdRow = await config.createRow() + const createdRow = await config.api.row.save(table._id!, {}) const res = await config.api.row.bulkDelete(table._id!, { rows: [createdRow, { _id: "9999999" }], @@ -657,8 +617,7 @@ describe.each([ describe("validate", () => { beforeAll(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) + table = await config.api.table.save(defaultTable()) }) it("should return no errors on valid row", async () => { @@ -690,13 +649,12 @@ describe.each([ describe("bulkDelete", () => { beforeAll(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) + table = await config.api.table.save(defaultTable()) }) it("should be able to delete a bulk set of rows", async () => { - const row1 = await config.createRow() - const row2 = await config.createRow() + const row1 = await config.api.row.save(table._id!, {}) + const row2 = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() const res = await config.api.row.bulkDelete(table._id!, { @@ -710,9 +668,9 @@ describe.each([ it("should be able to delete a variety of row set types", async () => { const [row1, row2, row3] = await Promise.all([ - config.createRow(), - config.createRow(), - config.createRow(), + config.api.row.save(table._id!, {}), + config.api.row.save(table._id!, {}), + config.api.row.save(table._id!, {}), ]) const rowUsage = await getRowUsage() @@ -726,7 +684,7 @@ describe.each([ }) it("should accept a valid row object and delete the row", async () => { - const row1 = await config.createRow() + const row1 = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() const res = await config.api.row.delete(table._id!, row1 as DeleteRow) @@ -768,12 +726,11 @@ describe.each([ isInternal && describe("fetchView", () => { beforeEach(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) + table = await config.api.table.save(defaultTable()) }) it("should be able to fetch tables contents via 'view'", async () => { - const row = await config.createRow() + const row = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() const rows = await config.api.legacyView.get(table._id!) @@ -785,7 +742,7 @@ describe.each([ it("should throw an error if view doesn't exist", async () => { const rowUsage = await getRowUsage() - await config.api.legacyView.get("derp", { status: 404 }) + await config.api.legacyView.get("derp", undefined, { status: 404 }) await assertRowUsage(rowUsage) }) @@ -797,7 +754,7 @@ describe.each([ filters: [], schema: {}, }) - const row = await config.createRow() + const row = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() const rows = await config.api.legacyView.get(view.name) @@ -810,45 +767,34 @@ describe.each([ describe("fetchEnrichedRows", () => { beforeAll(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) + table = await config.api.table.save(defaultTable()) }) it("should allow enriching some linked rows", async () => { const { linkedTable, firstRow, secondRow } = await tenancy.doInTenant( config.getTenantId(), async () => { - const linkedTable = await config.createLinkedTable( - RelationshipType.ONE_TO_MANY, - ["link"], - { - // Making sure that the combined table name + column name is within postgres limits - name: uuid.v4().replace(/-/g, "").substring(0, 16), - type: "table", - primary: ["id"], - primaryDisplay: "id", + const linkedTable = await config.api.table.save( + defaultTable({ schema: { - id: { - type: FieldType.AUTO, - name: "id", - autocolumn: true, - constraints: { - presence: true, - }, + link: { + name: "link", + fieldName: "link", + type: FieldType.LINK, + relationshipType: RelationshipType.ONE_TO_MANY, + tableId: table._id!, }, }, - } + }) ) - const firstRow = await config.createRow({ + const firstRow = await config.api.row.save(table._id!, { name: "Test Contact", description: "original description", - tableId: table._id, }) - const secondRow = await config.createRow({ + const secondRow = await config.api.row.save(linkedTable._id!, { name: "Test 2", description: "og desc", link: [{ _id: firstRow._id }], - tableId: linkedTable._id, }) return { linkedTable, firstRow, secondRow } } @@ -882,14 +828,23 @@ describe.each([ isInternal && describe("attachments", () => { beforeAll(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) + table = await config.api.table.save(defaultTable()) }) it("should allow enriching attachment rows", async () => { - const table = await config.createAttachmentTable() - const attachmentId = `${structures.uuid()}.csv` - const row = await config.createRow({ + const table = await config.api.table.save( + defaultTable({ + schema: { + attachment: { + type: FieldType.ATTACHMENT, + name: "attachment", + constraints: { type: "array", presence: false }, + }, + }, + }) + ) + const attachmentId = `${uuid.v4()}.csv` + const row = await config.api.row.save(table._id!, { name: "test", description: "test", attachment: [ @@ -912,12 +867,11 @@ describe.each([ describe("exportData", () => { beforeAll(async () => { - const tableConfig = generateTableConfig() - table = await createTable(tableConfig) + table = await config.api.table.save(defaultTable()) }) it("should allow exporting all columns", async () => { - const existing = await config.createRow() + const existing = await config.api.row.save(table._id!, {}) const res = await config.api.row.exportRows(table._id!, { rows: [existing._id!], }) @@ -935,7 +889,7 @@ describe.each([ }) it("should allow exporting only certain columns", async () => { - const existing = await config.createRow() + const existing = await config.api.row.save(table._id!, {}) const res = await config.api.row.exportRows(table._id!, { rows: [existing._id!], columns: ["_id"], @@ -952,21 +906,10 @@ describe.each([ describe("view 2.0", () => { async function userTable(): Promise { - return { + return saveTableRequest({ name: `users_${uuid.v4()}`, - sourceId: INTERNAL_TABLE_SOURCE_ID, - sourceType: TableSourceType.INTERNAL, type: "table", - primary: ["id"], schema: { - id: { - type: FieldType.AUTO, - name: "id", - autocolumn: true, - constraints: { - presence: true, - }, - }, name: { type: FieldType.STRING, name: "name", @@ -988,7 +931,7 @@ describe.each([ name: "jobTitle", }, }, - } + }) } const randomRowData = () => ({ @@ -1001,8 +944,9 @@ describe.each([ describe("create", () => { it("should persist a new row with only the provided view fields", async () => { - const table = await createTable(await userTable()) - const view = await config.createView({ + const table = await config.api.table.save(await userTable()) + const view = await config.api.viewV2.create({ + tableId: table._id!, schema: { name: { visible: true }, surname: { visible: true }, @@ -1036,9 +980,10 @@ describe.each([ describe("patch", () => { it("should update only the view fields for a row", async () => { - const table = await createTable(await userTable()) + const table = await config.api.table.save(await userTable()) const tableId = table._id! - const view = await config.createView({ + const view = await config.api.viewV2.create({ + tableId: tableId, schema: { name: { visible: true }, address: { visible: true }, @@ -1077,16 +1022,17 @@ describe.each([ describe("destroy", () => { it("should be able to delete a row", async () => { - const table = await createTable(await userTable()) + const table = await config.api.table.save(await userTable()) const tableId = table._id! - const view = await config.createView({ + const view = await config.api.viewV2.create({ + tableId: tableId, schema: { name: { visible: true }, address: { visible: true }, }, }) - const createdRow = await config.createRow() + const createdRow = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() await config.api.row.bulkDelete(view.id, { rows: [createdRow] }) @@ -1099,9 +1045,10 @@ describe.each([ }) it("should be able to delete multiple rows", async () => { - const table = await createTable(await userTable()) + const table = await config.api.table.save(await userTable()) const tableId = table._id! - const view = await config.createView({ + const view = await config.api.viewV2.create({ + tableId: tableId, schema: { name: { visible: true }, address: { visible: true }, @@ -1109,9 +1056,9 @@ describe.each([ }) const rows = await Promise.all([ - config.createRow(), - config.createRow(), - config.createRow(), + config.api.row.save(table._id!, {}), + config.api.row.save(table._id!, {}), + config.api.row.save(table._id!, {}), ]) const rowUsage = await getRowUsage() @@ -1130,46 +1077,39 @@ describe.each([ }) describe("view search", () => { + let table: Table const viewSchema = { age: { visible: true }, name: { visible: true } } - async function userTable(): Promise
{ - return { - name: `users_${uuid.v4()}`, - sourceId: INTERNAL_TABLE_SOURCE_ID, - sourceType: TableSourceType.INTERNAL, - type: "table", - primary: ["id"], - schema: { - id: { - type: FieldType.AUTO, - name: "id", - autocolumn: true, - constraints: { - presence: true, + + beforeAll(async () => { + table = await config.api.table.save( + saveTableRequest({ + name: `users_${uuid.v4()}`, + schema: { + name: { + type: FieldType.STRING, + name: "name", + constraints: { type: "string" }, + }, + age: { + type: FieldType.NUMBER, + name: "age", + constraints: {}, }, }, - name: { - type: FieldType.STRING, - name: "name", - constraints: { type: "string" }, - }, - age: { - type: FieldType.NUMBER, - name: "age", - constraints: {}, - }, - }, - } - } + }) + ) + }) it("returns empty rows from view when no schema is passed", async () => { - const table = await createTable(await userTable()) const rows = await Promise.all( Array.from({ length: 10 }, () => config.api.row.save(table._id!, { tableId: table._id }) ) ) - const createViewResponse = await config.createView() + const createViewResponse = await config.api.viewV2.create({ + tableId: table._id!, + }) const response = await config.api.viewV2.search(createViewResponse.id) expect(response.rows).toHaveLength(10) @@ -1193,8 +1133,6 @@ describe.each([ }) it("searching respects the view filters", async () => { - const table = await createTable(await userTable()) - await Promise.all( Array.from({ length: 10 }, () => config.api.row.save(table._id!, { @@ -1215,7 +1153,8 @@ describe.each([ ) ) - const createViewResponse = await config.createView({ + const createViewResponse = await config.api.viewV2.create({ + tableId: table._id!, query: [ { operator: SearchQueryOperators.EQUAL, field: "age", value: 40 }, ], @@ -1316,8 +1255,9 @@ describe.each([ ] describe("sorting", () => { + let table: Table beforeAll(async () => { - const table = await createTable(await userTable()) + table = await config.api.table.save(await userTable()) const users = [ { name: "Alice", age: 25 }, { name: "Bob", age: 30 }, @@ -1337,7 +1277,8 @@ describe.each([ it.each(sortTestOptions)( "allow sorting (%s)", async (sortParams, expected) => { - const createViewResponse = await config.createView({ + const createViewResponse = await config.api.viewV2.create({ + tableId: table._id!, sort: sortParams, schema: viewSchema, }) @@ -1356,7 +1297,8 @@ describe.each([ it.each(sortTestOptions)( "allow override the default view sorting (%s)", async (sortParams, expected) => { - const createViewResponse = await config.createView({ + const createViewResponse = await config.api.viewV2.create({ + tableId: table._id!, sort: { field: "name", order: SortOrder.ASCENDING, @@ -1384,7 +1326,7 @@ describe.each([ }) it("when schema is defined, defined columns and row attributes are returned", async () => { - const table = await createTable(await userTable()) + const table = await config.api.table.save(await userTable()) const rows = await Promise.all( Array.from({ length: 10 }, () => config.api.row.save(table._id!, { @@ -1395,7 +1337,8 @@ describe.each([ ) ) - const view = await config.createView({ + const view = await config.api.viewV2.create({ + tableId: table._id!, schema: { name: { visible: true } }, }) const response = await config.api.viewV2.search(view.id) @@ -1415,21 +1358,25 @@ describe.each([ }) it("views without data can be returned", async () => { - const table = await createTable(await userTable()) - - const createViewResponse = await config.createView() + const table = await config.api.table.save(await userTable()) + const createViewResponse = await config.api.viewV2.create({ + tableId: table._id!, + }) const response = await config.api.viewV2.search(createViewResponse.id) - expect(response.rows).toHaveLength(0) }) it("respects the limit parameter", async () => { - await createTable(await userTable()) - await Promise.all(Array.from({ length: 10 }, () => config.createRow())) + const table = await config.api.table.save(await userTable()) + await Promise.all( + Array.from({ length: 10 }, () => config.api.row.save(table._id!, {})) + ) const limit = generator.integer({ min: 1, max: 8 }) - const createViewResponse = await config.createView() + const createViewResponse = await config.api.viewV2.create({ + tableId: table._id!, + }) const response = await config.api.viewV2.search(createViewResponse.id, { limit, query: {}, @@ -1439,56 +1386,49 @@ describe.each([ }) it("can handle pagination", async () => { - await createTable(await userTable()) - await Promise.all(Array.from({ length: 10 }, () => config.createRow())) - - const createViewResponse = await config.createView() - const allRows = (await config.api.viewV2.search(createViewResponse.id)) - .rows - - const firstPageResponse = await config.api.viewV2.search( - createViewResponse.id, - { - paginate: true, - limit: 4, - query: {}, - } + const table = await config.api.table.save(await userTable()) + await Promise.all( + Array.from({ length: 10 }, () => config.api.row.save(table._id!, {})) ) - expect(firstPageResponse).toEqual({ - rows: expect.arrayContaining(allRows.slice(0, 4)), + const view = await config.api.viewV2.create({ + tableId: table._id!, + }) + const rows = (await config.api.viewV2.search(view.id)).rows + + const page1 = await config.api.viewV2.search(view.id, { + paginate: true, + limit: 4, + query: {}, + }) + expect(page1).toEqual({ + rows: expect.arrayContaining(rows.slice(0, 4)), totalRows: isInternal ? 10 : undefined, hasNextPage: true, bookmark: expect.anything(), }) - const secondPageResponse = await config.api.viewV2.search( - createViewResponse.id, - { - paginate: true, - limit: 4, - bookmark: firstPageResponse.bookmark, + const page2 = await config.api.viewV2.search(view.id, { + paginate: true, + limit: 4, + bookmark: page1.bookmark, - query: {}, - } - ) - expect(secondPageResponse).toEqual({ - rows: expect.arrayContaining(allRows.slice(4, 8)), + query: {}, + }) + expect(page2).toEqual({ + rows: expect.arrayContaining(rows.slice(4, 8)), totalRows: isInternal ? 10 : undefined, hasNextPage: true, bookmark: expect.anything(), }) - const lastPageResponse = await config.api.viewV2.search( - createViewResponse.id, - { - paginate: true, - limit: 4, - bookmark: secondPageResponse.bookmark, - query: {}, - } - ) - expect(lastPageResponse).toEqual({ - rows: expect.arrayContaining(allRows.slice(8)), + const page3 = await config.api.viewV2.search(view.id, { + paginate: true, + limit: 4, + bookmark: page2.bookmark, + query: {}, + }) + expect(page3).toEqual({ + rows: expect.arrayContaining(rows.slice(8)), totalRows: isInternal ? 10 : undefined, hasNextPage: false, bookmark: expect.anything(), @@ -1513,19 +1453,20 @@ describe.each([ }) describe("permissions", () => { - let viewId: string - let tableId: string + let table: Table + let view: ViewV2 beforeAll(async () => { - await createTable(await userTable()) + table = await config.api.table.save(await userTable()) await Promise.all( - Array.from({ length: 10 }, () => config.createRow()) + Array.from({ length: 10 }, () => + config.api.row.save(table._id!, {}) + ) ) - const createViewResponse = await config.createView() - - tableId = table._id! - viewId = createViewResponse.id + view = await config.api.viewV2.create({ + tableId: table._id!, + }) }) beforeEach(() => { @@ -1534,7 +1475,7 @@ describe.each([ it("does not allow public users to fetch by default", async () => { await config.publish() - await config.api.viewV2.publicSearch(viewId, undefined, { + await config.api.viewV2.publicSearch(view.id, undefined, { status: 403, }) }) @@ -1543,11 +1484,11 @@ describe.each([ await config.api.permission.add({ roleId: roles.BUILTIN_ROLE_IDS.PUBLIC, level: PermissionLevel.READ, - resourceId: viewId, + resourceId: view.id, }) await config.publish() - const response = await config.api.viewV2.publicSearch(viewId) + const response = await config.api.viewV2.publicSearch(view.id) expect(response.rows).toHaveLength(10) }) @@ -1556,11 +1497,11 @@ describe.each([ await config.api.permission.add({ roleId: roles.BUILTIN_ROLE_IDS.PUBLIC, level: PermissionLevel.READ, - resourceId: tableId, + resourceId: table._id!, }) await config.publish() - const response = await config.api.viewV2.publicSearch(viewId) + const response = await config.api.viewV2.publicSearch(view.id) expect(response.rows).toHaveLength(10) }) @@ -1569,16 +1510,16 @@ describe.each([ await config.api.permission.add({ roleId: roles.BUILTIN_ROLE_IDS.PUBLIC, level: PermissionLevel.READ, - resourceId: tableId, + resourceId: table._id!, }) await config.api.permission.add({ roleId: roles.BUILTIN_ROLE_IDS.POWER, level: PermissionLevel.READ, - resourceId: viewId, + resourceId: view.id, }) await config.publish() - await config.api.viewV2.publicSearch(viewId, undefined, { + await config.api.viewV2.publicSearch(view.id, undefined, { status: 403, }) }) @@ -1589,18 +1530,8 @@ describe.each([ let o2mTable: Table let m2mTable: Table beforeAll(async () => { - o2mTable = await createTable( - { ...generateTableConfig(), name: "o2m" }, - { - skipReassigning: true, - } - ) - m2mTable = await createTable( - { ...generateTableConfig(), name: "m2m" }, - { - skipReassigning: true, - } - ) + o2mTable = await config.api.table.save(defaultTable({ name: "o2m" })) + m2mTable = await config.api.table.save(defaultTable({ name: "m2m" })) }) describe.each([ @@ -1662,21 +1593,9 @@ describe.each([ let m2mData: Row[] beforeAll(async () => { - const tableConfig = generateTableConfig() - - if (config.datasource) { - tableConfig.sourceId = config.datasource._id! - if (config.datasource.plus) { - tableConfig.sourceType = TableSourceType.EXTERNAL - } - } - const table = await config.api.table.save({ - ...tableConfig, - schema: { - ...tableConfig.schema, - ...relSchema(), - }, - }) + const table = await config.api.table.save( + defaultTable({ schema: relSchema() }) + ) tableId = table._id! o2mData = [ @@ -1695,37 +1614,33 @@ describe.each([ }) it("can save a row when relationship fields are empty", async () => { - const rowData = { - ...basicRow(tableId), - name: generator.name(), - description: generator.name(), - } - const row = await config.api.row.save(tableId, rowData) + const row = await config.api.row.save(tableId, { + name: "foo", + description: "bar", + }) expect(row).toEqual({ - name: rowData.name, - description: rowData.description, - tableId, _id: expect.any(String), _rev: expect.any(String), id: isInternal ? undefined : expect.any(Number), type: isInternal ? "row" : undefined, + name: "foo", + description: "bar", + tableId, }) }) it("can save a row with a single relationship field", async () => { const user = _.sample(o2mData)! - const rowData = { - ...basicRow(tableId), - name: generator.name(), - description: generator.name(), + const row = await config.api.row.save(tableId, { + name: "foo", + description: "bar", user: [user], - } - const row = await config.api.row.save(tableId, rowData) + }) expect(row).toEqual({ - name: rowData.name, - description: rowData.description, + name: "foo", + description: "bar", tableId, user: [user].map(u => resultMapper(u)), _id: expect.any(String), @@ -1738,17 +1653,15 @@ describe.each([ it("can save a row with a multiple relationship field", async () => { const selectedUsers = _.sampleSize(m2mData, 2) - const rowData = { - ...basicRow(tableId), - name: generator.name(), - description: generator.name(), + const row = await config.api.row.save(tableId, { + name: "foo", + description: "bar", users: selectedUsers, - } - const row = await config.api.row.save(tableId, rowData) + }) expect(row).toEqual({ - name: rowData.name, - description: rowData.description, + name: "foo", + description: "bar", tableId, users: expect.arrayContaining(selectedUsers.map(u => resultMapper(u))), _id: expect.any(String), @@ -1759,17 +1672,15 @@ describe.each([ }) it("can retrieve rows with no populated relationships", async () => { - const rowData = { - ...basicRow(tableId), - name: generator.name(), - description: generator.name(), - } - const row = await config.api.row.save(tableId, rowData) + const row = await config.api.row.save(tableId, { + name: "foo", + description: "bar", + }) const retrieved = await config.api.row.get(tableId, row._id!) expect(retrieved).toEqual({ - name: rowData.name, - description: rowData.description, + name: "foo", + description: "bar", tableId, user: undefined, users: undefined, @@ -1784,19 +1695,17 @@ describe.each([ const user1 = _.sample(o2mData)! const [user2, user3] = _.sampleSize(m2mData, 2) - const rowData = { - ...basicRow(tableId), - name: generator.name(), - description: generator.name(), + const row = await config.api.row.save(tableId, { + name: "foo", + description: "bar", users: [user2, user3], user: [user1], - } - const row = await config.api.row.save(tableId, rowData) + }) const retrieved = await config.api.row.get(tableId, row._id!) expect(retrieved).toEqual({ - name: rowData.name, - description: rowData.description, + name: "foo", + description: "bar", tableId, user: expect.arrayContaining([user1].map(u => resultMapper(u))), users: expect.arrayContaining([user2, user3].map(u => resultMapper(u))), @@ -1812,13 +1721,11 @@ describe.each([ const user = _.sample(o2mData)! const [users1, users2, users3] = _.sampleSize(m2mData, 3) - const rowData = { - ...basicRow(tableId), - name: generator.name(), - description: generator.name(), + const row = await config.api.row.save(tableId, { + name: "foo", + description: "bar", users: [users1, users2], - } - const row = await config.api.row.save(tableId, rowData) + }) const updatedRow = await config.api.row.save(tableId, { ...row, @@ -1826,8 +1733,8 @@ describe.each([ users: [users3, users1], }) expect(updatedRow).toEqual({ - name: rowData.name, - description: rowData.description, + name: "foo", + description: "bar", tableId, user: expect.arrayContaining([user].map(u => resultMapper(u))), users: expect.arrayContaining( @@ -1843,14 +1750,11 @@ describe.each([ it("can wipe an existing populated relationships in row", async () => { const [user1, user2] = _.sampleSize(m2mData, 2) - - const rowData = { - ...basicRow(tableId), - name: generator.name(), - description: generator.name(), + const row = await config.api.row.save(tableId, { + name: "foo", + description: "bar", users: [user1, user2], - } - const row = await config.api.row.save(tableId, rowData) + }) const updatedRow = await config.api.row.save(tableId, { ...row, @@ -1858,8 +1762,8 @@ describe.each([ users: null, }) expect(updatedRow).toEqual({ - name: rowData.name, - description: rowData.description, + name: "foo", + description: "bar", tableId, _id: row._id, _rev: expect.any(String), @@ -1872,28 +1776,19 @@ describe.each([ const [user1] = _.sampleSize(o2mData, 1) const [users1, users2, users3] = _.sampleSize(m2mData, 3) - const rows: { - name: string - description: string - user?: Row[] - users?: Row[] - tableId: string - }[] = [ + const rows = [ { - ...basicRow(tableId), name: generator.name(), description: generator.name(), users: [users1, users2], }, { - ...basicRow(tableId), name: generator.name(), description: generator.name(), user: [user1], users: [users1, users3], }, { - ...basicRow(tableId), name: generator.name(), description: generator.name(), users: [users3], @@ -1931,28 +1826,19 @@ describe.each([ const [user1] = _.sampleSize(o2mData, 1) const [users1, users2, users3] = _.sampleSize(m2mData, 3) - const rows: { - name: string - description: string - user?: Row[] - users?: Row[] - tableId: string - }[] = [ + const rows = [ { - ...basicRow(tableId), name: generator.name(), description: generator.name(), users: [users1, users2], }, { - ...basicRow(tableId), name: generator.name(), description: generator.name(), user: [user1], users: [users1, users3], }, { - ...basicRow(tableId), name: generator.name(), description: generator.name(), users: [users3], @@ -1994,20 +1880,23 @@ describe.each([ }) describe("Formula fields", () => { - let relationshipTable: Table, tableId: string, relatedRow: Row + let table: Table + let otherTable: Table + let relatedRow: Row beforeAll(async () => { - const otherTableId = config.table!._id! - const cfg = generateTableConfig() - relationshipTable = await config.createLinkedTable( - RelationshipType.ONE_TO_MANY, - ["links"], - { - ...cfg, - // needs to be a short name + otherTable = await config.api.table.save(defaultTable()) + table = await config.api.table.save( + saveTableRequest({ name: "b", schema: { - ...cfg.schema, + links: { + name: "links", + fieldName: "links", + type: FieldType.LINK, + tableId: otherTable._id!, + relationshipType: RelationshipType.ONE_TO_MANY, + }, formula: { name: "formula", type: FieldType.FORMULA, @@ -2015,25 +1904,23 @@ describe.each([ formulaType: FormulaType.DYNAMIC, }, }, - } + }) ) - tableId = relationshipTable._id! - - relatedRow = await config.api.row.save(otherTableId, { + relatedRow = await config.api.row.save(otherTable._id!, { name: generator.word(), description: generator.paragraph(), }) - await config.api.row.save(tableId, { + await config.api.row.save(table._id!, { name: generator.word(), description: generator.paragraph(), - tableId, + tableId: table._id!, links: [relatedRow._id], }) }) it("should be able to search for rows containing formulas", async () => { - const { rows } = await config.api.row.search(tableId) + const { rows } = await config.api.row.search(table._id!) expect(rows.length).toBe(1) expect(rows[0].links.length).toBe(1) const row = rows[0] @@ -2054,22 +1941,22 @@ describe.each([ ` ).toString("base64") - const table = await config.createTable({ - name: "table", - type: "table", - schema: { - text: { - name: "text", - type: FieldType.STRING, + const table = await config.api.table.save( + saveTableRequest({ + schema: { + text: { + name: "text", + type: FieldType.STRING, + }, + formula: { + name: "formula", + type: FieldType.FORMULA, + formula: `{{ js "${js}"}}`, + formulaType: FormulaType.DYNAMIC, + }, }, - formula: { - name: "formula", - type: FieldType.FORMULA, - formula: `{{ js "${js}"}}`, - formulaType: FormulaType.DYNAMIC, - }, - }, - }) + }) + ) await config.api.row.save(table._id!, { text: "foo" }) const { rows } = await config.api.row.search(table._id!) @@ -2097,22 +1984,23 @@ describe.each([ ` ).toString("base64") - const table = await config.createTable({ - name: "table", - type: "table", - schema: { - text: { - name: "text", - type: FieldType.STRING, + const table = await config.api.table.save( + saveTableRequest({ + name: "table", + schema: { + text: { + name: "text", + type: FieldType.STRING, + }, + formula: { + name: "formula", + type: FieldType.FORMULA, + formula: `{{ js "${js}"}}`, + formulaType: FormulaType.DYNAMIC, + }, }, - formula: { - name: "formula", - type: FieldType.FORMULA, - formula: `{{ js "${js}"}}`, - formulaType: FormulaType.DYNAMIC, - }, - }, - }) + }) + ) for (let i = 0; i < 10; i++) { await config.api.row.save(table._id!, { text: "foo" }) @@ -2150,22 +2038,22 @@ describe.each([ it("should not carry over context between formulas", async () => { const js = Buffer.from(`return $("[text]");`).toString("base64") - const table = await config.createTable({ - name: "table", - type: "table", - schema: { - text: { - name: "text", - type: FieldType.STRING, + const table = await config.api.table.save( + saveTableRequest({ + schema: { + text: { + name: "text", + type: FieldType.STRING, + }, + formula: { + name: "formula", + type: FieldType.FORMULA, + formula: `{{ js "${js}"}}`, + formulaType: FormulaType.DYNAMIC, + }, }, - formula: { - name: "formula", - type: FieldType.FORMULA, - formula: `{{ js "${js}"}}`, - formulaType: FormulaType.DYNAMIC, - }, - }, - }) + }) + ) for (let i = 0; i < 10; i++) { await config.api.row.save(table._id!, { text: `foo${i}` }) diff --git a/packages/server/src/api/routes/tests/view.spec.js b/packages/server/src/api/routes/tests/view.spec.ts similarity index 59% rename from packages/server/src/api/routes/tests/view.spec.js rename to packages/server/src/api/routes/tests/view.spec.ts index 92ff097899..2e8c71b812 100644 --- a/packages/server/src/api/routes/tests/view.spec.js +++ b/packages/server/src/api/routes/tests/view.spec.ts @@ -1,30 +1,38 @@ -const setup = require("./utilities") -const { events } = require("@budibase/backend-core") +import { events } from "@budibase/backend-core" +import * as setup from "./utilities" +import { + FieldType, + INTERNAL_TABLE_SOURCE_ID, + SaveTableRequest, + Table, + TableSourceType, + View, + ViewCalculation, +} from "@budibase/types" -function priceTable() { - return { - name: "table", - type: "table", - key: "name", - schema: { - Price: { - type: "number", - constraints: {}, - }, - Category: { +const priceTable: SaveTableRequest = { + name: "table", + type: "table", + sourceId: INTERNAL_TABLE_SOURCE_ID, + sourceType: TableSourceType.INTERNAL, + schema: { + Price: { + name: "Price", + type: FieldType.NUMBER, + }, + Category: { + name: "Category", + type: FieldType.STRING, + constraints: { type: "string", - constraints: { - type: "string", - }, }, }, - } + }, } describe("/views", () => { - let request = setup.getRequest() let config = setup.getConfig() - let table + let table: Table afterAll(setup.afterAll) @@ -33,38 +41,34 @@ describe("/views", () => { }) beforeEach(async () => { - table = await config.createTable(priceTable()) + table = await config.api.table.save(priceTable) }) - const saveView = async view => { - const viewToSave = { + const saveView = async (view?: Partial) => { + const viewToSave: View = { name: "TestView", field: "Price", - calculation: "stats", - tableId: table._id, + calculation: ViewCalculation.STATISTICS, + tableId: table._id!, + filters: [], + schema: {}, ...view, } - return request - .post(`/api/views`) - .send(viewToSave) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) + return config.api.legacyView.save(viewToSave) } describe("create", () => { it("returns a success message when the view is successfully created", async () => { const res = await saveView() - expect(res.body.tableId).toBe(table._id) expect(events.view.created).toBeCalledTimes(1) }) it("creates a view with a calculation", async () => { jest.clearAllMocks() - const res = await saveView({ calculation: "count" }) + const view = await saveView({ calculation: ViewCalculation.COUNT }) - expect(res.body.tableId).toBe(table._id) + expect(view.tableId).toBe(table._id) expect(events.view.created).toBeCalledTimes(1) expect(events.view.updated).not.toBeCalled() expect(events.view.calculationCreated).toBeCalledTimes(1) @@ -78,8 +82,8 @@ describe("/views", () => { it("creates a view with a filter", async () => { jest.clearAllMocks() - const res = await saveView({ - calculation: null, + const view = await saveView({ + calculation: undefined, filters: [ { value: "1", @@ -89,7 +93,7 @@ describe("/views", () => { ], }) - expect(res.body.tableId).toBe(table._id) + expect(view.tableId).toBe(table._id) expect(events.view.created).toBeCalledTimes(1) expect(events.view.updated).not.toBeCalled() expect(events.view.calculationCreated).not.toBeCalled() @@ -101,52 +105,41 @@ describe("/views", () => { }) it("updates the table row with the new view metadata", async () => { - const res = await request - .post(`/api/views`) - .send({ - name: "TestView", - field: "Price", - calculation: "stats", - tableId: table._id, + await saveView() + const updatedTable = await config.api.table.get(table._id!) + expect(updatedTable.views).toEqual( + expect.objectContaining({ + TestView: expect.objectContaining({ + field: "Price", + calculation: "stats", + tableId: table._id, + filters: [], + schema: { + sum: { + type: "number", + }, + min: { + type: "number", + }, + max: { + type: "number", + }, + count: { + type: "number", + }, + sumsqr: { + type: "number", + }, + avg: { + type: "number", + }, + field: { + type: "string", + }, + }, + }), }) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect(res.body.tableId).toBe(table._id) - - const updatedTable = await config.getTable(table._id) - const expectedObj = expect.objectContaining({ - TestView: expect.objectContaining({ - field: "Price", - calculation: "stats", - tableId: table._id, - filters: [], - schema: { - sum: { - type: "number", - }, - min: { - type: "number", - }, - max: { - type: "number", - }, - count: { - type: "number", - }, - sumsqr: { - type: "number", - }, - avg: { - type: "number", - }, - field: { - type: "string", - }, - }, - }), - }) - expect(updatedTable.views).toEqual(expectedObj) + ) }) }) @@ -168,10 +161,10 @@ describe("/views", () => { }) it("updates a view calculation", async () => { - await saveView({ calculation: "sum" }) + await saveView({ calculation: ViewCalculation.SUM }) jest.clearAllMocks() - await saveView({ calculation: "count" }) + await saveView({ calculation: ViewCalculation.COUNT }) expect(events.view.created).not.toBeCalled() expect(events.view.updated).toBeCalledTimes(1) @@ -184,10 +177,10 @@ describe("/views", () => { }) it("deletes a view calculation", async () => { - await saveView({ calculation: "sum" }) + await saveView({ calculation: ViewCalculation.SUM }) jest.clearAllMocks() - await saveView({ calculation: null }) + await saveView({ calculation: undefined }) expect(events.view.created).not.toBeCalled() expect(events.view.updated).toBeCalledTimes(1) @@ -258,100 +251,98 @@ describe("/views", () => { describe("fetch", () => { beforeEach(async () => { - table = await config.createTable(priceTable()) + table = await config.api.table.save(priceTable) }) it("returns only custom views", async () => { - await config.createLegacyView({ + await saveView({ name: "TestView", field: "Price", - calculation: "stats", + calculation: ViewCalculation.STATISTICS, tableId: table._id, }) - const res = await request - .get(`/api/views`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect(res.body.length).toBe(1) - expect(res.body.find(({ name }) => name === "TestView")).toBeDefined() + const views = await config.api.legacyView.fetch() + expect(views.length).toBe(1) + expect(views.find(({ name }) => name === "TestView")).toBeDefined() }) }) describe("query", () => { it("returns data for the created view", async () => { - await config.createLegacyView({ + await saveView({ name: "TestView", field: "Price", - calculation: "stats", - tableId: table._id, + calculation: ViewCalculation.STATISTICS, + tableId: table._id!, }) - await config.createRow({ - tableId: table._id, + await config.api.row.save(table._id!, { Price: 1000, }) - await config.createRow({ - tableId: table._id, + await config.api.row.save(table._id!, { Price: 2000, }) - await config.createRow({ - tableId: table._id, + await config.api.row.save(table._id!, { Price: 4000, }) - const res = await request - .get(`/api/views/TestView?calculation=stats`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect(res.body.length).toBe(1) - expect(res.body).toMatchSnapshot() + const rows = await config.api.legacyView.get("TestView", { + calculation: ViewCalculation.STATISTICS, + }) + expect(rows.length).toBe(1) + expect(rows[0]).toEqual({ + avg: 2333.3333333333335, + count: 3, + group: null, + max: 4000, + min: 1000, + sum: 7000, + sumsqr: 21000000, + }) }) it("returns data for the created view using a group by", async () => { - await config.createLegacyView({ - calculation: "stats", + await saveView({ + calculation: ViewCalculation.STATISTICS, name: "TestView", field: "Price", groupBy: "Category", tableId: table._id, }) - await config.createRow({ - tableId: table._id, + await config.api.row.save(table._id!, { Price: 1000, Category: "One", }) - await config.createRow({ - tableId: table._id, + await config.api.row.save(table._id!, { Price: 2000, Category: "One", }) - await config.createRow({ - tableId: table._id, + await config.api.row.save(table._id!, { Price: 4000, Category: "Two", }) - const res = await request - .get(`/api/views/TestView?calculation=stats&group=Category`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - - expect(res.body.length).toBe(2) - expect(res.body).toMatchSnapshot() + const rows = await config.api.legacyView.get("TestView", { + calculation: ViewCalculation.STATISTICS, + group: "Category", + }) + expect(rows.length).toBe(2) + expect(rows[0]).toEqual({ + avg: 1500, + count: 2, + group: "One", + max: 2000, + min: 1000, + sum: 3000, + sumsqr: 5000000, + }) }) }) describe("destroy", () => { it("should be able to delete a view", async () => { - const table = await config.createTable(priceTable()) - const view = await config.createLegacyView() - const res = await request - .delete(`/api/views/${view.name}`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) - expect(res.body.map).toBeDefined() - expect(res.body.meta.tableId).toEqual(table._id) + const table = await config.api.table.save(priceTable) + const view = await saveView({ tableId: table._id }) + const deletedView = await config.api.legacyView.destroy(view.name!) + expect(deletedView.map).toBeDefined() + expect(deletedView.meta?.tableId).toEqual(table._id) expect(events.view.deleted).toBeCalledTimes(1) }) }) @@ -362,33 +353,44 @@ describe("/views", () => { }) const setupExport = async () => { - const table = await config.createTable() - await config.createRow({ name: "test-name", description: "ùúûü" }) + const table = await config.api.table.save({ + name: "test-table", + type: "table", + sourceId: INTERNAL_TABLE_SOURCE_ID, + sourceType: TableSourceType.INTERNAL, + schema: { + name: { + name: "name", + type: FieldType.STRING, + }, + description: { + name: "description", + type: FieldType.STRING, + }, + }, + }) + await config.api.row.save(table._id!, { + name: "test-name", + description: "ùúûü", + }) return table } - const exportView = async (viewName, format) => { - return request - .get(`/api/views/export?view=${viewName}&format=${format}`) - .set(config.defaultHeaders()) - .expect(200) - } - - const assertJsonExport = res => { - const rows = JSON.parse(res.text) + const assertJsonExport = (res: string) => { + const rows = JSON.parse(res) expect(rows.length).toBe(1) expect(rows[0].name).toBe("test-name") expect(rows[0].description).toBe("ùúûü") } - const assertCSVExport = res => { - expect(res.text).toBe(`"name","description"\n"test-name","ùúûü"`) + const assertCSVExport = (res: string) => { + expect(res).toBe(`"name","description"\n"test-name","ùúûü"`) } it("should be able to export a table as JSON", async () => { const table = await setupExport() - const res = await exportView(table._id, "json") + const res = await config.api.legacyView.export(table._id!, "json") assertJsonExport(res) expect(events.table.exported).toBeCalledTimes(1) @@ -398,7 +400,7 @@ describe("/views", () => { it("should be able to export a table as CSV", async () => { const table = await setupExport() - const res = await exportView(table._id, "csv") + const res = await config.api.legacyView.export(table._id!, "csv") assertCSVExport(res) expect(events.table.exported).toBeCalledTimes(1) @@ -407,10 +409,15 @@ describe("/views", () => { it("should be able to export a view as JSON", async () => { let table = await setupExport() - const view = await config.createLegacyView() - table = await config.getTable(table._id) + const view = await config.api.legacyView.save({ + name: "test-view", + tableId: table._id!, + filters: [], + schema: {}, + }) + table = await config.api.table.get(table._id!) - let res = await exportView(view.name, "json") + let res = await config.api.legacyView.export(view.name!, "json") assertJsonExport(res) expect(events.view.exported).toBeCalledTimes(1) @@ -419,10 +426,15 @@ describe("/views", () => { it("should be able to export a view as CSV", async () => { let table = await setupExport() - const view = await config.createLegacyView() - table = await config.getTable(table._id) + const view = await config.api.legacyView.save({ + name: "test-view", + tableId: table._id!, + filters: [], + schema: {}, + }) + table = await config.api.table.get(table._id!) - let res = await exportView(view.name, "csv") + let res = await config.api.legacyView.export(view.name!, "csv") assertCSVExport(res) expect(events.view.exported).toBeCalledTimes(1) diff --git a/packages/server/src/tests/utilities/api/legacyView.ts b/packages/server/src/tests/utilities/api/legacyView.ts index 38ef70d62a..b018988670 100644 --- a/packages/server/src/tests/utilities/api/legacyView.ts +++ b/packages/server/src/tests/utilities/api/legacyView.ts @@ -1,8 +1,36 @@ import { Expectations, TestAPI } from "./base" -import { Row } from "@budibase/types" +import { Row, View, ViewCalculation } from "@budibase/types" export class LegacyViewAPI extends TestAPI { - get = async (id: string, expectations?: Expectations) => { - return await this._get(`/api/views/${id}`, { expectations }) + get = async ( + id: string, + query?: { calculation: ViewCalculation; group?: string }, + expectations?: Expectations + ) => { + return await this._get(`/api/views/${id}`, { query, expectations }) + } + + save = async (body: View, expectations?: Expectations) => { + return await this._post(`/api/views/`, { body, expectations }) + } + + fetch = async (expectations?: Expectations) => { + return await this._get(`/api/views`, { expectations }) + } + + destroy = async (id: string, expectations?: Expectations) => { + return await this._delete(`/api/views/${id}`, { expectations }) + } + + export = async ( + viewName: string, + format: "json" | "csv" | "jsonWithSchema", + expectations?: Expectations + ) => { + const response = await this._requestRaw("get", `/api/views/export`, { + query: { view: viewName, format }, + expectations, + }) + return response.text } } diff --git a/packages/types/src/core/installation.ts b/packages/types/src/core/installation.ts index 7679290f36..ec89e439d9 100644 --- a/packages/types/src/core/installation.ts +++ b/packages/types/src/core/installation.ts @@ -2,3 +2,7 @@ export enum ServiceType { WORKER = "worker", APPS = "apps", } + +export enum MaintenanceType { + SQS_MISSING = "sqs_missing", +} diff --git a/packages/types/src/documents/app/view.ts b/packages/types/src/documents/app/view.ts index b5a22ec592..7b93d24f3d 100644 --- a/packages/types/src/documents/app/view.ts +++ b/packages/types/src/documents/app/view.ts @@ -30,6 +30,7 @@ export interface View { map?: string reduce?: any meta?: ViewTemplateOpts + groupBy?: string } export interface ViewV2 { diff --git a/packages/worker/src/api/controllers/system/environment.ts b/packages/worker/src/api/controllers/system/environment.ts index bf9270607f..203d3d41ff 100644 --- a/packages/worker/src/api/controllers/system/environment.ts +++ b/packages/worker/src/api/controllers/system/environment.ts @@ -1,10 +1,18 @@ -import { Ctx } from "@budibase/types" +import { Ctx, MaintenanceType } from "@budibase/types" import env from "../../../environment" import { env as coreEnv } from "@budibase/backend-core" import nodeFetch from "node-fetch" +// When we come to move to SQS fully and move away from Clouseau, we will need +// to flip this to true (or remove it entirely). This will then be used to +// determine if we should show the maintenance page that links to the SQS +// migration docs. +const sqsRequired = false + let sqsAvailable: boolean async function isSqsAvailable() { + // We cache this value for the duration of the Node process because we don't + // want every page load to be making this relatively expensive check. if (sqsAvailable !== undefined) { return sqsAvailable } @@ -21,6 +29,10 @@ async function isSqsAvailable() { } } +async function isSqsMissing() { + return sqsRequired && !(await isSqsAvailable()) +} + export const fetch = async (ctx: Ctx) => { ctx.body = { multiTenancy: !!env.MULTI_TENANCY, @@ -30,11 +42,12 @@ export const fetch = async (ctx: Ctx) => { disableAccountPortal: env.DISABLE_ACCOUNT_PORTAL, baseUrl: env.PLATFORM_URL, isDev: env.isDev() && !env.isTest(), + maintenance: [], } if (env.SELF_HOSTED) { - ctx.body.infrastructure = { - sqs: await isSqsAvailable(), + if (await isSqsMissing()) { + ctx.body.maintenance.push({ type: MaintenanceType.SQS_MISSING }) } } } diff --git a/packages/worker/src/api/routes/system/tests/environment.spec.ts b/packages/worker/src/api/routes/system/tests/environment.spec.ts index 2efbfa07c9..dbe9be7374 100644 --- a/packages/worker/src/api/routes/system/tests/environment.spec.ts +++ b/packages/worker/src/api/routes/system/tests/environment.spec.ts @@ -27,6 +27,7 @@ describe("/api/system/environment", () => { multiTenancy: true, baseUrl: "http://localhost:10000", offlineMode: false, + maintenance: [], }) }) @@ -40,9 +41,7 @@ describe("/api/system/environment", () => { multiTenancy: true, baseUrl: "http://localhost:10000", offlineMode: false, - infrastructure: { - sqs: false, - }, + maintenance: [], }) }) }) diff --git a/yarn.lock b/yarn.lock index 235e28e85e..53bf1ac017 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8242,11 +8242,16 @@ codemirror-spell-checker@1.1.2: dependencies: typo-js "*" -codemirror@^5.59.0, codemirror@^5.63.1: +codemirror@^5.63.1: version "5.65.12" resolved "https://registry.yarnpkg.com/codemirror/-/codemirror-5.65.12.tgz#294fdf097d10ac5b56a9e011a91eff252afc73ae" integrity sha512-z2jlHBocElRnPYysN2HAuhXbO3DNB0bcSKmNz3hcWR2Js2Dkhc1bEOxG93Z3DeUrnm+qx56XOY5wQmbP5KY0sw== +codemirror@^5.65.16: + version "5.65.16" + resolved "https://registry.yarnpkg.com/codemirror/-/codemirror-5.65.16.tgz#efc0661be6bf4988a6a1c2fe6893294638cdb334" + integrity sha512-br21LjYmSlVL0vFCPWPfhzUCT34FM/pAdK7rRIZwa0rrtrIdotvP4Oh4GUHsu2E3IrQMCfRkL/fN3ytMNxVQvg== + collect-v8-coverage@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/collect-v8-coverage/-/collect-v8-coverage-1.0.1.tgz#cc2c8e94fc18bbdffe64d6534570c8a673b27f59" @@ -13944,6 +13949,11 @@ json-buffer@3.0.1: resolved "https://registry.yarnpkg.com/json-buffer/-/json-buffer-3.0.1.tgz#9338802a30d3b6605fbe0613e094008ca8c05a13" integrity sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ== +json-format-highlight@^1.0.4: + version "1.0.4" + resolved "https://registry.yarnpkg.com/json-format-highlight/-/json-format-highlight-1.0.4.tgz#2e44277edabcec79a3d2c84e984c62e2258037b9" + integrity sha512-RqenIjKr1I99XfXPAml9G7YlEZg/GnsH7emWyWJh2yuGXqHW8spN7qx6/ME+MoIBb35/fxrMC9Jauj6nvGe4Mg== + json-parse-better-errors@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9"