Merge branch 'master' of github.com:Budibase/budibase into aggregate-all-sql-dbs
This commit is contained in:
commit
09824f9a4d
|
@ -21,6 +21,7 @@ jobs:
|
||||||
l_max_size: "1000"
|
l_max_size: "1000"
|
||||||
fail_if_xl: "false"
|
fail_if_xl: "false"
|
||||||
files_to_ignore: "yarn.lock"
|
files_to_ignore: "yarn.lock"
|
||||||
|
message_if_xl: ""
|
||||||
|
|
||||||
team-labeler:
|
team-labeler:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
|
@ -65,7 +65,7 @@ Budibase is open-source - licensed as GPL v3. This should fill you with confiden
|
||||||
<br /><br />
|
<br /><br />
|
||||||
|
|
||||||
### Load data or start from scratch
|
### Load data or start from scratch
|
||||||
Budibase pulls data from multiple sources, including MongoDB, CouchDB, PostgreSQL, MySQL, Airtable, S3, DynamoDB, or a REST API. And unlike other platforms, with Budibase you can start from scratch and create business apps with no data sources. [Request new datasources](https://github.com/Budibase/budibase/discussions?discussions_q=category%3AIdeas).
|
Budibase pulls data from multiple sources, including MongoDB, CouchDB, PostgreSQL, MariaDB, MySQL, Airtable, S3, DynamoDB, or a REST API. And unlike other platforms, with Budibase you can start from scratch and create business apps with no data sources. [Request new datasources](https://github.com/Budibase/budibase/discussions?discussions_q=category%3AIdeas).
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<img alt="Budibase data" src="https://res.cloudinary.com/daog6scxm/image/upload/v1680281798/ui/data_klbuna.png">
|
<img alt="Budibase data" src="https://res.cloudinary.com/daog6scxm/image/upload/v1680281798/ui/data_klbuna.png">
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
|
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
|
||||||
"version": "2.31.4",
|
"version": "2.31.7",
|
||||||
"npmClient": "yarn",
|
"npmClient": "yarn",
|
||||||
"packages": [
|
"packages": [
|
||||||
"packages/*",
|
"packages/*",
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import env from "../environment"
|
import env from "../environment"
|
||||||
import * as context from "../context"
|
import * as context from "../context"
|
||||||
import { PostHog, PostHogOptions } from "posthog-node"
|
import { PostHog, PostHogOptions } from "posthog-node"
|
||||||
import { IdentityType, UserCtx } from "@budibase/types"
|
import { FeatureFlag, IdentityType, UserCtx } from "@budibase/types"
|
||||||
import tracer from "dd-trace"
|
import tracer from "dd-trace"
|
||||||
|
|
||||||
let posthog: PostHog | undefined
|
let posthog: PostHog | undefined
|
||||||
|
@ -268,4 +268,5 @@ export class FlagSet<V extends Flag<any>, T extends { [key: string]: V }> {
|
||||||
export const flags = new FlagSet({
|
export const flags = new FlagSet({
|
||||||
DEFAULT_VALUES: Flag.boolean(env.isDev()),
|
DEFAULT_VALUES: Flag.boolean(env.isDev()),
|
||||||
SQS: Flag.boolean(env.isDev()),
|
SQS: Flag.boolean(env.isDev()),
|
||||||
|
[FeatureFlag.ENRICHED_RELATIONSHIPS]: Flag.boolean(false),
|
||||||
})
|
})
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
import GridCreateEditRowModal from "components/backend/DataTable/modals/grid/GridCreateEditRowModal.svelte"
|
import GridCreateEditRowModal from "components/backend/DataTable/modals/grid/GridCreateEditRowModal.svelte"
|
||||||
import GridFilterButton from "components/backend/DataTable/buttons/grid/GridFilterButton.svelte"
|
import GridFilterButton from "components/backend/DataTable/buttons/grid/GridFilterButton.svelte"
|
||||||
import GridManageAccessButton from "components/backend/DataTable/buttons/grid/GridManageAccessButton.svelte"
|
import GridManageAccessButton from "components/backend/DataTable/buttons/grid/GridManageAccessButton.svelte"
|
||||||
|
import { isEnabled } from "helpers/featureFlags"
|
||||||
|
import { FeatureFlag } from "@budibase/types"
|
||||||
|
|
||||||
$: id = $viewsV2.selected?.id
|
$: id = $viewsV2.selected?.id
|
||||||
$: datasource = {
|
$: datasource = {
|
||||||
|
@ -29,6 +31,7 @@
|
||||||
on:updatedatasource={handleGridViewUpdate}
|
on:updatedatasource={handleGridViewUpdate}
|
||||||
isCloud={$admin.cloud}
|
isCloud={$admin.cloud}
|
||||||
allowViewReadonlyColumns={$licensing.isViewReadonlyColumnsEnabled}
|
allowViewReadonlyColumns={$licensing.isViewReadonlyColumnsEnabled}
|
||||||
|
canSetRelationshipSchemas={isEnabled(FeatureFlag.ENRICHED_RELATIONSHIPS)}
|
||||||
>
|
>
|
||||||
<svelte:fragment slot="filter">
|
<svelte:fragment slot="filter">
|
||||||
<GridFilterButton />
|
<GridFilterButton />
|
||||||
|
|
|
@ -84,7 +84,7 @@
|
||||||
}
|
}
|
||||||
let relationshipOpts1 = Object.values(PrettyRelationshipDefinitions)
|
let relationshipOpts1 = Object.values(PrettyRelationshipDefinitions)
|
||||||
let relationshipOpts2 = Object.values(PrettyRelationshipDefinitions)
|
let relationshipOpts2 = Object.values(PrettyRelationshipDefinitions)
|
||||||
let relationshipMap = {
|
const relationshipMap = {
|
||||||
[RelationshipType.ONE_TO_MANY]: {
|
[RelationshipType.ONE_TO_MANY]: {
|
||||||
part1: PrettyRelationshipDefinitions.MANY,
|
part1: PrettyRelationshipDefinitions.MANY,
|
||||||
part2: PrettyRelationshipDefinitions.ONE,
|
part2: PrettyRelationshipDefinitions.ONE,
|
||||||
|
@ -98,7 +98,7 @@
|
||||||
part2: PrettyRelationshipDefinitions.MANY,
|
part2: PrettyRelationshipDefinitions.MANY,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
let autoColumnInfo = getAutoColumnInformation()
|
const autoColumnInfo = getAutoColumnInformation()
|
||||||
let optionsValid = true
|
let optionsValid = true
|
||||||
|
|
||||||
$: rowGoldenSample = RowUtils.generateGoldenSample($rows)
|
$: rowGoldenSample = RowUtils.generateGoldenSample($rows)
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
<script>
|
<script>
|
||||||
import {
|
import {
|
||||||
|
AbsTooltip,
|
||||||
Layout,
|
Layout,
|
||||||
Heading,
|
Heading,
|
||||||
Body,
|
Body,
|
||||||
Button,
|
Button,
|
||||||
Divider,
|
Divider,
|
||||||
|
Icon,
|
||||||
Tags,
|
Tags,
|
||||||
Tag,
|
Tag,
|
||||||
} from "@budibase/bbui"
|
} from "@budibase/bbui"
|
||||||
|
@ -15,6 +17,8 @@
|
||||||
export let description
|
export let description
|
||||||
export let enabled
|
export let enabled
|
||||||
export let upgradeButtonClick
|
export let upgradeButtonClick
|
||||||
|
|
||||||
|
$: upgradeDisabled = !$auth.accountPortalAccess && $admin.cloud
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Layout noPadding>
|
<Layout noPadding>
|
||||||
|
@ -36,8 +40,9 @@
|
||||||
{:else}
|
{:else}
|
||||||
<div class="buttons">
|
<div class="buttons">
|
||||||
<Button
|
<Button
|
||||||
primary
|
primary={!upgradeDisabled}
|
||||||
disabled={!$auth.accountPortalAccess && $admin.cloud}
|
secondary={upgradeDisabled}
|
||||||
|
disabled={upgradeDisabled}
|
||||||
on:click={async () => upgradeButtonClick()}
|
on:click={async () => upgradeButtonClick()}
|
||||||
>
|
>
|
||||||
Upgrade
|
Upgrade
|
||||||
|
@ -51,6 +56,16 @@
|
||||||
>
|
>
|
||||||
View Plans
|
View Plans
|
||||||
</Button>
|
</Button>
|
||||||
|
{#if upgradeDisabled}
|
||||||
|
<AbsTooltip
|
||||||
|
text={"Please contact the account holder to upgrade"}
|
||||||
|
position={"right"}
|
||||||
|
>
|
||||||
|
<div class="icon" on:focus>
|
||||||
|
<Icon name="InfoOutline" size="L" disabled hoverable />
|
||||||
|
</div>
|
||||||
|
</AbsTooltip>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
</Layout>
|
</Layout>
|
||||||
|
@ -67,7 +82,11 @@
|
||||||
justify-content: flex-start;
|
justify-content: flex-start;
|
||||||
gap: var(--spacing-m);
|
gap: var(--spacing-m);
|
||||||
}
|
}
|
||||||
|
.icon {
|
||||||
|
position: relative;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
.buttons {
|
.buttons {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: row;
|
flex-direction: row;
|
||||||
|
|
|
@ -127,7 +127,10 @@
|
||||||
name: user.firstName ? user.firstName + " " + user.lastName : "",
|
name: user.firstName ? user.firstName + " " + user.lastName : "",
|
||||||
userGroups,
|
userGroups,
|
||||||
__selectable:
|
__selectable:
|
||||||
role.value === Constants.BudibaseRoles.Owner ? false : undefined,
|
role.value === Constants.BudibaseRoles.Owner ||
|
||||||
|
$auth.user?.email === user.email
|
||||||
|
? false
|
||||||
|
: true,
|
||||||
apps: [...new Set(Object.keys(user.roles))],
|
apps: [...new Set(Object.keys(user.roles))],
|
||||||
access: role.sortOrder,
|
access: role.sortOrder,
|
||||||
}
|
}
|
||||||
|
@ -392,7 +395,7 @@
|
||||||
allowSelectRows={!readonly}
|
allowSelectRows={!readonly}
|
||||||
{customRenderers}
|
{customRenderers}
|
||||||
loading={!$fetch.loaded || !groupsLoaded}
|
loading={!$fetch.loaded || !groupsLoaded}
|
||||||
defaultSortColumn={"access"}
|
defaultSortColumn={"__selectable"}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div class="pagination">
|
<div class="pagination">
|
||||||
|
|
|
@ -153,7 +153,7 @@
|
||||||
$: builderInteractive =
|
$: builderInteractive =
|
||||||
$builderStore.inBuilder && insideScreenslot && !isBlock && !instance.static
|
$builderStore.inBuilder && insideScreenslot && !isBlock && !instance.static
|
||||||
$: devToolsInteractive = $devToolsStore.allowSelection && !isBlock
|
$: devToolsInteractive = $devToolsStore.allowSelection && !isBlock
|
||||||
$: interactive = !isRoot && (builderInteractive || devToolsInteractive)
|
$: interactive = builderInteractive || devToolsInteractive
|
||||||
$: editing = editable && selected && $builderStore.editMode
|
$: editing = editable && selected && $builderStore.editMode
|
||||||
$: draggable =
|
$: draggable =
|
||||||
!inDragPath &&
|
!inDragPath &&
|
||||||
|
@ -189,12 +189,6 @@
|
||||||
// Scroll the selected element into view
|
// Scroll the selected element into view
|
||||||
$: selected && scrollIntoView()
|
$: selected && scrollIntoView()
|
||||||
|
|
||||||
// When dragging and dropping, pad components to allow dropping between
|
|
||||||
// nested layers. Only reset this when dragging stops.
|
|
||||||
let pad = false
|
|
||||||
$: pad = pad || (interactive && hasChildren && inDndPath)
|
|
||||||
$: $dndIsDragging, (pad = false)
|
|
||||||
|
|
||||||
// Themes
|
// Themes
|
||||||
$: currentTheme = $context?.device?.theme
|
$: currentTheme = $context?.device?.theme
|
||||||
$: darkMode = !currentTheme?.includes("light")
|
$: darkMode = !currentTheme?.includes("light")
|
||||||
|
@ -206,8 +200,10 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Metadata to pass into grid action to apply CSS
|
// Metadata to pass into grid action to apply CSS
|
||||||
const insideGrid =
|
const checkGrid = x =>
|
||||||
parent?._component.endsWith("/container") && parent?.layout === "grid"
|
x?._component?.endsWith("/container") && x?.layout === "grid"
|
||||||
|
$: insideGrid = checkGrid(parent)
|
||||||
|
$: isGrid = checkGrid(instance)
|
||||||
$: gridMetadata = {
|
$: gridMetadata = {
|
||||||
insideGrid,
|
insideGrid,
|
||||||
ignoresLayout: definition?.ignoresLayout === true,
|
ignoresLayout: definition?.ignoresLayout === true,
|
||||||
|
@ -219,6 +215,12 @@
|
||||||
errored: errorState,
|
errored: errorState,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// When dragging and dropping, pad components to allow dropping between
|
||||||
|
// nested layers. Only reset this when dragging stops.
|
||||||
|
let pad = false
|
||||||
|
$: pad = pad || (!isGrid && interactive && hasChildren && inDndPath)
|
||||||
|
$: $dndIsDragging, (pad = false)
|
||||||
|
|
||||||
// Update component context
|
// Update component context
|
||||||
$: store.set({
|
$: store.set({
|
||||||
id,
|
id,
|
||||||
|
@ -231,12 +233,14 @@
|
||||||
empty: emptyState,
|
empty: emptyState,
|
||||||
selected,
|
selected,
|
||||||
interactive,
|
interactive,
|
||||||
|
isRoot,
|
||||||
draggable,
|
draggable,
|
||||||
editable,
|
editable,
|
||||||
isBlock,
|
isBlock,
|
||||||
},
|
},
|
||||||
empty: emptyState,
|
empty: emptyState,
|
||||||
selected,
|
selected,
|
||||||
|
isRoot,
|
||||||
inSelectedPath,
|
inSelectedPath,
|
||||||
name,
|
name,
|
||||||
editing,
|
editing,
|
||||||
|
@ -672,6 +676,7 @@
|
||||||
class:parent={hasChildren}
|
class:parent={hasChildren}
|
||||||
class:block={isBlock}
|
class:block={isBlock}
|
||||||
class:error={errorState}
|
class:error={errorState}
|
||||||
|
class:root={isRoot}
|
||||||
data-id={id}
|
data-id={id}
|
||||||
data-name={name}
|
data-name={name}
|
||||||
data-icon={icon}
|
data-icon={icon}
|
||||||
|
|
|
@ -14,7 +14,6 @@
|
||||||
|
|
||||||
// Get the screen definition for the current route
|
// Get the screen definition for the current route
|
||||||
$: screen = $screenStore.activeScreen
|
$: screen = $screenStore.activeScreen
|
||||||
$: screenDefinition = { ...screen?.props, addEmptyRows: true }
|
|
||||||
$: onLoadActions.set(screen?.onLoad)
|
$: onLoadActions.set(screen?.onLoad)
|
||||||
$: runOnLoadActions($onLoadActions, params)
|
$: runOnLoadActions($onLoadActions, params)
|
||||||
|
|
||||||
|
@ -42,10 +41,10 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Ensure to fully remount when screen changes -->
|
<!-- Ensure to fully remount when screen changes -->
|
||||||
{#if $routeStore.routerLoaded}
|
{#if $routeStore.routerLoaded && screen?.props}
|
||||||
{#key screenDefinition?._id}
|
{#key screen.props._id}
|
||||||
<Provider key="url" data={params}>
|
<Provider key="url" data={params}>
|
||||||
<Component isRoot instance={screenDefinition} />
|
<Component isRoot instance={screen.props} />
|
||||||
</Provider>
|
</Provider>
|
||||||
{/key}
|
{/key}
|
||||||
{/if}
|
{/if}
|
||||||
|
|
|
@ -18,9 +18,11 @@
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: row;
|
flex-direction: row;
|
||||||
justify-content: flex-start;
|
justify-content: flex-start;
|
||||||
align-items: center;
|
align-items: flex-start;
|
||||||
color: var(--spectrum-global-color-gray-600);
|
color: var(--spectrum-global-color-gray-600);
|
||||||
font-size: var(--font-size-s);
|
font-size: var(--font-size-s);
|
||||||
gap: var(--spacing-s);
|
gap: var(--spacing-s);
|
||||||
|
grid-column: 1 / -1;
|
||||||
|
grid-row: 1 / -1;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
@ -23,6 +23,8 @@
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: var(--spacing-s);
|
gap: var(--spacing-s);
|
||||||
flex: 1 1 auto;
|
flex: 1 1 auto;
|
||||||
|
grid-column: 1 / -1;
|
||||||
|
grid-row: 1 / -1;
|
||||||
}
|
}
|
||||||
.placeholder :global(.spectrum-Button) {
|
.placeholder :global(.spectrum-Button) {
|
||||||
margin-top: var(--spacing-m);
|
margin-top: var(--spacing-m);
|
||||||
|
|
|
@ -4,8 +4,6 @@
|
||||||
import { GridRowHeight, GridColumns } from "constants"
|
import { GridRowHeight, GridColumns } from "constants"
|
||||||
import { memo } from "@budibase/frontend-core"
|
import { memo } from "@budibase/frontend-core"
|
||||||
|
|
||||||
export let addEmptyRows = false
|
|
||||||
|
|
||||||
const component = getContext("component")
|
const component = getContext("component")
|
||||||
const { styleable, builderStore } = getContext("sdk")
|
const { styleable, builderStore } = getContext("sdk")
|
||||||
const context = getContext("context")
|
const context = getContext("context")
|
||||||
|
@ -18,16 +16,12 @@
|
||||||
let styles = memo({})
|
let styles = memo({})
|
||||||
|
|
||||||
$: inBuilder = $builderStore.inBuilder
|
$: inBuilder = $builderStore.inBuilder
|
||||||
$: requiredRows = calculateRequiredRows(
|
$: addEmptyRows = $component.isRoot && inBuilder
|
||||||
$children,
|
$: requiredRows = calculateRequiredRows($children, mobile, addEmptyRows)
|
||||||
mobile,
|
|
||||||
addEmptyRows && inBuilder
|
|
||||||
)
|
|
||||||
$: requiredHeight = requiredRows * GridRowHeight
|
$: requiredHeight = requiredRows * GridRowHeight
|
||||||
$: availableRows = Math.floor(height / GridRowHeight)
|
$: availableRows = Math.floor(height / GridRowHeight)
|
||||||
$: rows = Math.max(requiredRows, availableRows)
|
$: rows = Math.max(requiredRows, availableRows)
|
||||||
$: mobile = $context.device.mobile
|
$: mobile = $context.device.mobile
|
||||||
$: empty = $component.empty
|
|
||||||
$: colSize = width / GridColumns
|
$: colSize = width / GridColumns
|
||||||
$: styles.set({
|
$: styles.set({
|
||||||
...$component.styles,
|
...$component.styles,
|
||||||
|
@ -40,7 +34,6 @@
|
||||||
"--col-size": colSize,
|
"--col-size": colSize,
|
||||||
"--row-size": GridRowHeight,
|
"--row-size": GridRowHeight,
|
||||||
},
|
},
|
||||||
empty: false,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Calculates the minimum number of rows required to render all child
|
// Calculates the minimum number of rows required to render all child
|
||||||
|
@ -145,9 +138,7 @@
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
{#if mounted}
|
||||||
<!-- Only render the slot if not empty, as we don't want the placeholder -->
|
|
||||||
{#if !empty && mounted}
|
|
||||||
<slot />
|
<slot />
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -6,8 +6,11 @@
|
||||||
let left, top, height, width
|
let left, top, height, width
|
||||||
|
|
||||||
const updatePosition = () => {
|
const updatePosition = () => {
|
||||||
const node =
|
let node = document.getElementsByClassName(DNDPlaceholderID)[0]
|
||||||
document.getElementsByClassName(DNDPlaceholderID)[0]?.childNodes[0]
|
const insideGrid = node?.dataset.insideGrid === "true"
|
||||||
|
if (!insideGrid) {
|
||||||
|
node = document.getElementsByClassName(`${DNDPlaceholderID}-dom`)[0]
|
||||||
|
}
|
||||||
if (!node) {
|
if (!node) {
|
||||||
height = 0
|
height = 0
|
||||||
width = 0
|
width = 0
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
newId = e.target.dataset.id
|
newId = e.target.dataset.id
|
||||||
} else {
|
} else {
|
||||||
// Handle normal components
|
// Handle normal components
|
||||||
const element = e.target.closest(".interactive.component")
|
const element = e.target.closest(".interactive.component:not(.root)")
|
||||||
newId = element?.dataset?.id
|
newId = element?.dataset?.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { writable } from "svelte/store"
|
import { writable } from "svelte/store"
|
||||||
import { computed } from "../utils/computed.js"
|
import { derivedMemo } from "@budibase/frontend-core"
|
||||||
|
|
||||||
const createDndStore = () => {
|
const createDndStore = () => {
|
||||||
const initialState = {
|
const initialState = {
|
||||||
|
@ -78,11 +78,11 @@ export const dndStore = createDndStore()
|
||||||
// performance by deriving any state that needs to be externally observed.
|
// performance by deriving any state that needs to be externally observed.
|
||||||
// By doing this and using primitives, we can avoid invalidating other stores
|
// By doing this and using primitives, we can avoid invalidating other stores
|
||||||
// or components which depend on DND state unless values actually change.
|
// or components which depend on DND state unless values actually change.
|
||||||
export const dndParent = computed(dndStore, x => x.drop?.parent)
|
export const dndParent = derivedMemo(dndStore, x => x.drop?.parent)
|
||||||
export const dndIndex = computed(dndStore, x => x.drop?.index)
|
export const dndIndex = derivedMemo(dndStore, x => x.drop?.index)
|
||||||
export const dndBounds = computed(dndStore, x => x.source?.bounds)
|
export const dndBounds = derivedMemo(dndStore, x => x.source?.bounds)
|
||||||
export const dndIsDragging = computed(dndStore, x => !!x.source)
|
export const dndIsDragging = derivedMemo(dndStore, x => !!x.source)
|
||||||
export const dndIsNewComponent = computed(
|
export const dndIsNewComponent = derivedMemo(
|
||||||
dndStore,
|
dndStore,
|
||||||
x => x.source?.newComponentType != null
|
x => x.source?.newComponentType != null
|
||||||
)
|
)
|
||||||
|
|
|
@ -92,6 +92,8 @@ const createScreenStore = () => {
|
||||||
width: `${$dndBounds?.width || 400}px`,
|
width: `${$dndBounds?.width || 400}px`,
|
||||||
height: `${$dndBounds?.height || 200}px`,
|
height: `${$dndBounds?.height || 200}px`,
|
||||||
opacity: 0,
|
opacity: 0,
|
||||||
|
"--default-width": $dndBounds?.width || 400,
|
||||||
|
"--default-height": $dndBounds?.height || 200,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
static: true,
|
static: true,
|
||||||
|
|
|
@ -1,38 +0,0 @@
|
||||||
import { writable } from "svelte/store"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extension of Svelte's built in "derived" stores, which the addition of deep
|
|
||||||
* comparison of non-primitives. Falls back to using shallow comparison for
|
|
||||||
* primitive types to avoid performance penalties.
|
|
||||||
* Useful for instances where a deep comparison is cheaper than an additional
|
|
||||||
* store invalidation.
|
|
||||||
* @param store the store to observer
|
|
||||||
* @param deriveValue the derivation function
|
|
||||||
* @returns {Writable<*>} a derived svelte store containing just the derived value
|
|
||||||
*/
|
|
||||||
export const computed = (store, deriveValue) => {
|
|
||||||
const initialValue = deriveValue(store)
|
|
||||||
const computedStore = writable(initialValue)
|
|
||||||
let lastKey = getKey(initialValue)
|
|
||||||
|
|
||||||
store.subscribe(state => {
|
|
||||||
const value = deriveValue(state)
|
|
||||||
const key = getKey(value)
|
|
||||||
if (key !== lastKey) {
|
|
||||||
lastKey = key
|
|
||||||
computedStore.set(value)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return computedStore
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper function to serialise any value into a primitive which can be cheaply
|
|
||||||
// and shallowly compared
|
|
||||||
const getKey = value => {
|
|
||||||
if (value == null || typeof value !== "object") {
|
|
||||||
return value
|
|
||||||
} else {
|
|
||||||
return JSON.stringify(value)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -92,8 +92,12 @@ export const gridLayout = (node, metadata) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine default width and height of component
|
// Determine default width and height of component
|
||||||
let width = errored ? 500 : definition?.size?.width || 200
|
let width = styles["--default-width"] ?? definition?.size?.width ?? 200
|
||||||
let height = errored ? 60 : definition?.size?.height || 200
|
let height = styles["--default-height"] ?? definition?.size?.height ?? 200
|
||||||
|
if (errored) {
|
||||||
|
width = 500
|
||||||
|
height = 60
|
||||||
|
}
|
||||||
width += 2 * GridSpacing
|
width += 2 * GridSpacing
|
||||||
height += 2 * GridSpacing
|
height += 2 * GridSpacing
|
||||||
let vars = {
|
let vars = {
|
||||||
|
|
|
@ -93,7 +93,7 @@ export const styleable = (node, styles = {}) => {
|
||||||
node.addEventListener("mouseout", applyNormalStyles)
|
node.addEventListener("mouseout", applyNormalStyles)
|
||||||
|
|
||||||
// Add builder preview click listener
|
// Add builder preview click listener
|
||||||
if (newStyles.interactive) {
|
if (newStyles.interactive && !newStyles.isRoot) {
|
||||||
node.addEventListener("click", selectComponent, false)
|
node.addEventListener("click", selectComponent, false)
|
||||||
node.addEventListener("dblclick", editComponent, false)
|
node.addEventListener("dblclick", editComponent, false)
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
let searching = false
|
let searching = false
|
||||||
let container
|
let container
|
||||||
let anchor
|
let anchor
|
||||||
|
let relationshipFields
|
||||||
|
|
||||||
$: fieldValue = parseValue(value)
|
$: fieldValue = parseValue(value)
|
||||||
$: oneRowOnly = schema?.relationshipType === "one-to-many"
|
$: oneRowOnly = schema?.relationshipType === "one-to-many"
|
||||||
|
@ -41,6 +42,26 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$: relationFields = fieldValue?.reduce((acc, f) => {
|
||||||
|
const fields = {}
|
||||||
|
for (const [column] of Object.entries(schema?.columns || {}).filter(
|
||||||
|
([key, column]) =>
|
||||||
|
column.visible !== false && f[key] !== null && f[key] !== undefined
|
||||||
|
)) {
|
||||||
|
fields[column] = f[column]
|
||||||
|
}
|
||||||
|
if (Object.keys(fields).length) {
|
||||||
|
acc[f._id] = fields
|
||||||
|
}
|
||||||
|
return acc
|
||||||
|
}, {})
|
||||||
|
|
||||||
|
$: showRelationshipFields =
|
||||||
|
relationshipFields &&
|
||||||
|
Object.keys(relationshipFields).length &&
|
||||||
|
focused &&
|
||||||
|
!isOpen
|
||||||
|
|
||||||
const parseValue = value => {
|
const parseValue = value => {
|
||||||
if (Array.isArray(value) && value.every(x => x?._id)) {
|
if (Array.isArray(value) && value.every(x => x?._id)) {
|
||||||
return value
|
return value
|
||||||
|
@ -221,6 +242,14 @@
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const displayRelationshipFields = relationship => {
|
||||||
|
relationshipFields = relationFields[relationship._id]
|
||||||
|
}
|
||||||
|
|
||||||
|
const hideRelationshipFields = () => {
|
||||||
|
relationshipFields = undefined
|
||||||
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
api = {
|
api = {
|
||||||
focus: open,
|
focus: open,
|
||||||
|
@ -244,11 +273,18 @@
|
||||||
<div
|
<div
|
||||||
class="values"
|
class="values"
|
||||||
class:wrap={editable || contentLines > 1}
|
class:wrap={editable || contentLines > 1}
|
||||||
|
class:disabled={!focused}
|
||||||
on:wheel={e => (focused ? e.stopPropagation() : null)}
|
on:wheel={e => (focused ? e.stopPropagation() : null)}
|
||||||
>
|
>
|
||||||
{#each fieldValue || [] as relationship}
|
{#each fieldValue || [] as relationship}
|
||||||
{#if relationship[primaryDisplay] || relationship.primaryDisplay}
|
{#if relationship[primaryDisplay] || relationship.primaryDisplay}
|
||||||
<div class="badge">
|
<div
|
||||||
|
class="badge"
|
||||||
|
class:extra-info={!!relationFields[relationship._id]}
|
||||||
|
on:mouseover={() => displayRelationshipFields(relationship)}
|
||||||
|
on:focus={() => {}}
|
||||||
|
on:mouseleave={() => hideRelationshipFields()}
|
||||||
|
>
|
||||||
<span>
|
<span>
|
||||||
{readable(
|
{readable(
|
||||||
relationship[primaryDisplay] || relationship.primaryDisplay
|
relationship[primaryDisplay] || relationship.primaryDisplay
|
||||||
|
@ -322,6 +358,21 @@
|
||||||
</GridPopover>
|
</GridPopover>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
{#if showRelationshipFields}
|
||||||
|
<GridPopover {anchor} minWidth={300} maxWidth={400}>
|
||||||
|
<div class="relationship-fields">
|
||||||
|
{#each Object.entries(relationshipFields) as [fieldName, fieldValue]}
|
||||||
|
<div class="relationship-field-name">
|
||||||
|
{fieldName}
|
||||||
|
</div>
|
||||||
|
<div class="relationship-field-value">
|
||||||
|
{fieldValue}
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
</GridPopover>
|
||||||
|
{/if}
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
.wrapper {
|
.wrapper {
|
||||||
flex: 1 1 auto;
|
flex: 1 1 auto;
|
||||||
|
@ -376,6 +427,9 @@
|
||||||
padding: var(--cell-padding);
|
padding: var(--cell-padding);
|
||||||
flex-wrap: nowrap;
|
flex-wrap: nowrap;
|
||||||
}
|
}
|
||||||
|
.values.disabled {
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
.values.wrap {
|
.values.wrap {
|
||||||
flex-wrap: wrap;
|
flex-wrap: wrap;
|
||||||
}
|
}
|
||||||
|
@ -407,6 +461,13 @@
|
||||||
height: 20px;
|
height: 20px;
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
}
|
}
|
||||||
|
.values.wrap .badge:hover {
|
||||||
|
filter: brightness(1.25);
|
||||||
|
}
|
||||||
|
.values.wrap .badge.extra-info {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
.badge span {
|
.badge span {
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
|
@ -478,4 +539,25 @@
|
||||||
.search :global(.spectrum-Form-item) {
|
.search :global(.spectrum-Form-item) {
|
||||||
flex: 1 1 auto;
|
flex: 1 1 auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.relationship-fields {
|
||||||
|
margin: var(--spacing-m) var(--spacing-l);
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: minmax(auto, 50%) auto;
|
||||||
|
grid-row-gap: var(--spacing-m);
|
||||||
|
grid-column-gap: var(--spacing-m);
|
||||||
|
}
|
||||||
|
|
||||||
|
.relationship-field-name {
|
||||||
|
text-transform: uppercase;
|
||||||
|
color: var(--spectrum-global-color-gray-600);
|
||||||
|
font-size: var(--font-size-xs);
|
||||||
|
}
|
||||||
|
.relationship-field-value {
|
||||||
|
overflow: hidden;
|
||||||
|
display: -webkit-box;
|
||||||
|
-webkit-box-orient: vertical;
|
||||||
|
-webkit-line-clamp: 3;
|
||||||
|
line-clamp: 3;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
@ -2,16 +2,29 @@
|
||||||
import { getContext } from "svelte"
|
import { getContext } from "svelte"
|
||||||
import { ActionButton, Popover } from "@budibase/bbui"
|
import { ActionButton, Popover } from "@budibase/bbui"
|
||||||
import ColumnsSettingContent from "./ColumnsSettingContent.svelte"
|
import ColumnsSettingContent from "./ColumnsSettingContent.svelte"
|
||||||
|
import { FieldPermissions } from "../../../constants"
|
||||||
|
|
||||||
export let allowViewReadonlyColumns = false
|
export let allowViewReadonlyColumns = false
|
||||||
|
|
||||||
const { columns } = getContext("grid")
|
const { columns, datasource } = getContext("grid")
|
||||||
|
|
||||||
let open = false
|
let open = false
|
||||||
let anchor
|
let anchor
|
||||||
|
|
||||||
$: anyRestricted = $columns.filter(col => !col.visible || col.readonly).length
|
$: anyRestricted = $columns.filter(col => !col.visible || col.readonly).length
|
||||||
$: text = anyRestricted ? `Columns (${anyRestricted} restricted)` : "Columns"
|
$: text = anyRestricted ? `Columns (${anyRestricted} restricted)` : "Columns"
|
||||||
|
|
||||||
|
$: permissions =
|
||||||
|
$datasource.type === "viewV2"
|
||||||
|
? [
|
||||||
|
FieldPermissions.WRITABLE,
|
||||||
|
FieldPermissions.READONLY,
|
||||||
|
FieldPermissions.HIDDEN,
|
||||||
|
]
|
||||||
|
: [FieldPermissions.WRITABLE, FieldPermissions.HIDDEN]
|
||||||
|
$: disabledPermissions = allowViewReadonlyColumns
|
||||||
|
? []
|
||||||
|
: [FieldPermissions.READONLY]
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div bind:this={anchor}>
|
<div bind:this={anchor}>
|
||||||
|
@ -28,5 +41,9 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Popover bind:open {anchor} align="left">
|
<Popover bind:open {anchor} align="left">
|
||||||
<ColumnsSettingContent columns={$columns} {allowViewReadonlyColumns} />
|
<ColumnsSettingContent
|
||||||
|
columns={$columns}
|
||||||
|
{permissions}
|
||||||
|
{disabledPermissions}
|
||||||
|
/>
|
||||||
</Popover>
|
</Popover>
|
||||||
|
|
|
@ -1,22 +1,162 @@
|
||||||
<script>
|
<script>
|
||||||
import { getContext } from "svelte"
|
import { getContext } from "svelte"
|
||||||
import { Icon, notifications } from "@budibase/bbui"
|
import { Icon, notifications, ActionButton, Popover } from "@budibase/bbui"
|
||||||
import { getColumnIcon } from "../lib/utils"
|
import { getColumnIcon } from "../lib/utils"
|
||||||
import ToggleActionButtonGroup from "./ToggleActionButtonGroup.svelte"
|
import ToggleActionButtonGroup from "./ToggleActionButtonGroup.svelte"
|
||||||
import { helpers } from "@budibase/shared-core"
|
import { helpers } from "@budibase/shared-core"
|
||||||
|
import { FieldType } from "@budibase/types"
|
||||||
|
import { FieldPermissions } from "../../../constants"
|
||||||
|
|
||||||
export let allowViewReadonlyColumns = false
|
export let permissions = [FieldPermissions.WRITABLE, FieldPermissions.HIDDEN]
|
||||||
|
export let disabledPermissions = []
|
||||||
|
export let columns
|
||||||
|
export let fromRelationshipField
|
||||||
|
|
||||||
const { columns, datasource, dispatch } = getContext("grid")
|
const { datasource, dispatch, cache, config } = getContext("grid")
|
||||||
|
|
||||||
const toggleColumn = async (column, permission) => {
|
$: canSetRelationshipSchemas = $config.canSetRelationshipSchemas
|
||||||
const visible = permission !== PERMISSION_OPTIONS.HIDDEN
|
|
||||||
const readonly = permission === PERMISSION_OPTIONS.READONLY
|
|
||||||
|
|
||||||
|
let relationshipPanelAnchor
|
||||||
|
let relationshipFieldName
|
||||||
|
|
||||||
|
$: relationshipField = columns.find(
|
||||||
|
c => c.name === relationshipFieldName
|
||||||
|
)?.schema
|
||||||
|
$: permissionsObj = permissions.reduce(
|
||||||
|
(acc, c) => ({
|
||||||
|
...acc,
|
||||||
|
[c]: {
|
||||||
|
disabled: disabledPermissions.includes(c),
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
{}
|
||||||
|
)
|
||||||
|
|
||||||
|
$: displayColumns = columns.map(c => {
|
||||||
|
const isRequired =
|
||||||
|
c.primaryDisplay || helpers.schema.isRequired(c.schema.constraints)
|
||||||
|
|
||||||
|
const defaultPermission = permissions[0]
|
||||||
|
const requiredTooltips = {
|
||||||
|
[FieldPermissions.WRITABLE]: (() => {
|
||||||
|
if (defaultPermission === FieldPermissions.WRITABLE) {
|
||||||
|
if (c.primaryDisplay) {
|
||||||
|
return "Display column must be writable"
|
||||||
|
}
|
||||||
|
if (isRequired) {
|
||||||
|
return "Required columns must be writable"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})(),
|
||||||
|
[FieldPermissions.READONLY]: (() => {
|
||||||
|
if (defaultPermission === FieldPermissions.WRITABLE) {
|
||||||
|
if (c.primaryDisplay) {
|
||||||
|
return "Display column cannot be read-only"
|
||||||
|
}
|
||||||
|
if (isRequired) {
|
||||||
|
return "Required columns cannot be read-only"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (defaultPermission === FieldPermissions.READONLY) {
|
||||||
|
if (c.primaryDisplay) {
|
||||||
|
return "Display column must be read-only"
|
||||||
|
}
|
||||||
|
if (isRequired) {
|
||||||
|
return "Required columns must be read-only"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})(),
|
||||||
|
[FieldPermissions.HIDDEN]: (() => {
|
||||||
|
if (c.primaryDisplay) {
|
||||||
|
return "Display column cannot be hidden"
|
||||||
|
}
|
||||||
|
if (isRequired) {
|
||||||
|
return "Required columns cannot be hidden"
|
||||||
|
}
|
||||||
|
})(),
|
||||||
|
}
|
||||||
|
|
||||||
|
const options = []
|
||||||
|
|
||||||
|
let permission
|
||||||
|
if ((permission = permissionsObj[FieldPermissions.WRITABLE])) {
|
||||||
|
const tooltip = requiredTooltips[FieldPermissions.WRITABLE] || "Writable"
|
||||||
|
options.push({
|
||||||
|
icon: "Edit",
|
||||||
|
value: FieldPermissions.WRITABLE,
|
||||||
|
tooltip,
|
||||||
|
disabled: isRequired || permission.disabled,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((permission = permissionsObj[FieldPermissions.READONLY])) {
|
||||||
|
const tooltip =
|
||||||
|
(requiredTooltips[FieldPermissions.READONLY] || "Read-only") +
|
||||||
|
(permission.disabled ? " (premium feature)" : "")
|
||||||
|
options.push({
|
||||||
|
icon: "Visibility",
|
||||||
|
value: FieldPermissions.READONLY,
|
||||||
|
tooltip,
|
||||||
|
disabled: permission.disabled || isRequired,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((permission = permissionsObj[FieldPermissions.HIDDEN])) {
|
||||||
|
const tooltip = requiredTooltips[FieldPermissions.HIDDEN] || "Hidden"
|
||||||
|
options.push({
|
||||||
|
icon: "VisibilityOff",
|
||||||
|
value: FieldPermissions.HIDDEN,
|
||||||
|
disabled: permission.disabled || isRequired,
|
||||||
|
tooltip,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return { ...c, options }
|
||||||
|
})
|
||||||
|
|
||||||
|
let relationshipPanelColumns = []
|
||||||
|
async function fetchRelationshipPanelColumns(relationshipField) {
|
||||||
|
relationshipPanelColumns = []
|
||||||
|
if (!relationshipField) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const table = await cache.actions.getTable(relationshipField.tableId)
|
||||||
|
relationshipPanelColumns = Object.entries(
|
||||||
|
relationshipField?.columns || {}
|
||||||
|
).map(([name, column]) => {
|
||||||
|
return {
|
||||||
|
name: name,
|
||||||
|
label: name,
|
||||||
|
schema: {
|
||||||
|
type: table.schema[name].type,
|
||||||
|
visible: column.visible,
|
||||||
|
readonly: column.readonly,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
$: fetchRelationshipPanelColumns(relationshipField)
|
||||||
|
|
||||||
|
async function toggleColumn(column, permission) {
|
||||||
|
const visible = permission !== FieldPermissions.HIDDEN
|
||||||
|
const readonly = permission === FieldPermissions.READONLY
|
||||||
|
|
||||||
|
if (!fromRelationshipField) {
|
||||||
await datasource.actions.addSchemaMutation(column.name, {
|
await datasource.actions.addSchemaMutation(column.name, {
|
||||||
visible,
|
visible,
|
||||||
readonly,
|
readonly,
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
await datasource.actions.addSubSchemaMutation(
|
||||||
|
column.name,
|
||||||
|
fromRelationshipField.name,
|
||||||
|
{
|
||||||
|
visible,
|
||||||
|
readonly,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
await datasource.actions.saveSchemaMutations()
|
await datasource.actions.saveSchemaMutations()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -28,60 +168,16 @@
|
||||||
dispatch(visible ? "show-column" : "hide-column")
|
dispatch(visible ? "show-column" : "hide-column")
|
||||||
}
|
}
|
||||||
|
|
||||||
const PERMISSION_OPTIONS = {
|
|
||||||
WRITABLE: "writable",
|
|
||||||
READONLY: "readonly",
|
|
||||||
HIDDEN: "hidden",
|
|
||||||
}
|
|
||||||
|
|
||||||
$: displayColumns = $columns.map(c => {
|
|
||||||
const isRequired = helpers.schema.isRequired(c.schema.constraints)
|
|
||||||
const requiredTooltip = isRequired && "Required columns must be writable"
|
|
||||||
const editEnabled =
|
|
||||||
!isRequired ||
|
|
||||||
columnToPermissionOptions(c) !== PERMISSION_OPTIONS.WRITABLE
|
|
||||||
const options = [
|
|
||||||
{
|
|
||||||
icon: "Edit",
|
|
||||||
value: PERMISSION_OPTIONS.WRITABLE,
|
|
||||||
tooltip: (!editEnabled && requiredTooltip) || "Writable",
|
|
||||||
disabled: !editEnabled,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
if ($datasource.type === "viewV2") {
|
|
||||||
options.push({
|
|
||||||
icon: "Visibility",
|
|
||||||
value: PERMISSION_OPTIONS.READONLY,
|
|
||||||
tooltip: allowViewReadonlyColumns
|
|
||||||
? requiredTooltip || "Read only"
|
|
||||||
: "Read only (premium feature)",
|
|
||||||
disabled: !allowViewReadonlyColumns || isRequired,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
options.push({
|
|
||||||
icon: "VisibilityOff",
|
|
||||||
value: PERMISSION_OPTIONS.HIDDEN,
|
|
||||||
disabled: c.primaryDisplay || isRequired,
|
|
||||||
tooltip:
|
|
||||||
(c.primaryDisplay && "Display column cannot be hidden") ||
|
|
||||||
requiredTooltip ||
|
|
||||||
"Hidden",
|
|
||||||
})
|
|
||||||
|
|
||||||
return { ...c, options }
|
|
||||||
})
|
|
||||||
|
|
||||||
function columnToPermissionOptions(column) {
|
function columnToPermissionOptions(column) {
|
||||||
if (column.schema.visible === false) {
|
if (column.schema.visible === false) {
|
||||||
return PERMISSION_OPTIONS.HIDDEN
|
return FieldPermissions.HIDDEN
|
||||||
}
|
}
|
||||||
|
|
||||||
if (column.schema.readonly) {
|
if (column.schema.readonly) {
|
||||||
return PERMISSION_OPTIONS.READONLY
|
return FieldPermissions.READONLY
|
||||||
}
|
}
|
||||||
|
|
||||||
return PERMISSION_OPTIONS.WRITABLE
|
return FieldPermissions.WRITABLE
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -94,16 +190,56 @@
|
||||||
{column.label}
|
{column.label}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="column-options">
|
||||||
<ToggleActionButtonGroup
|
<ToggleActionButtonGroup
|
||||||
on:click={e => toggleColumn(column, e.detail)}
|
on:click={e => toggleColumn(column, e.detail)}
|
||||||
value={columnToPermissionOptions(column)}
|
value={columnToPermissionOptions(column)}
|
||||||
options={column.options}
|
options={column.options}
|
||||||
/>
|
/>
|
||||||
|
{#if canSetRelationshipSchemas && column.schema.type === FieldType.LINK && columnToPermissionOptions(column) !== FieldPermissions.HIDDEN}
|
||||||
|
<div class="relationship-columns">
|
||||||
|
<ActionButton
|
||||||
|
on:click={e => {
|
||||||
|
relationshipFieldName = column.name
|
||||||
|
relationshipPanelAnchor = e.currentTarget
|
||||||
|
}}
|
||||||
|
size="S"
|
||||||
|
icon="ChevronRight"
|
||||||
|
quiet
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{#if canSetRelationshipSchemas}
|
||||||
|
<Popover
|
||||||
|
on:close={() => (relationshipFieldName = null)}
|
||||||
|
open={relationshipFieldName}
|
||||||
|
anchor={relationshipPanelAnchor}
|
||||||
|
align="right-outside"
|
||||||
|
>
|
||||||
|
{#if relationshipPanelColumns.length}
|
||||||
|
<div class="relationship-header">
|
||||||
|
{relationshipFieldName} columns
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
<svelte:self
|
||||||
|
columns={relationshipPanelColumns}
|
||||||
|
permissions={[FieldPermissions.READONLY, FieldPermissions.HIDDEN]}
|
||||||
|
fromRelationshipField={relationshipField}
|
||||||
|
/>
|
||||||
|
</Popover>
|
||||||
|
{/if}
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
.relationship-columns :global(.spectrum-ActionButton) {
|
||||||
|
width: 28px;
|
||||||
|
height: 28px;
|
||||||
|
}
|
||||||
|
|
||||||
.content {
|
.content {
|
||||||
padding: 12px 12px;
|
padding: 12px 12px;
|
||||||
display: flex;
|
display: flex;
|
||||||
|
@ -131,4 +267,12 @@
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
|
.column-options {
|
||||||
|
display: flex;
|
||||||
|
gap: var(--spacing-xs);
|
||||||
|
}
|
||||||
|
.relationship-header {
|
||||||
|
color: var(--spectrum-global-color-gray-600);
|
||||||
|
padding: 12px 12px 0 12px;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
@ -43,6 +43,7 @@
|
||||||
export let canDeleteRows = true
|
export let canDeleteRows = true
|
||||||
export let canEditColumns = true
|
export let canEditColumns = true
|
||||||
export let canSaveSchema = true
|
export let canSaveSchema = true
|
||||||
|
export let canSetRelationshipSchemas = false
|
||||||
export let stripeRows = false
|
export let stripeRows = false
|
||||||
export let quiet = false
|
export let quiet = false
|
||||||
export let collaboration = true
|
export let collaboration = true
|
||||||
|
@ -99,6 +100,7 @@
|
||||||
canDeleteRows,
|
canDeleteRows,
|
||||||
canEditColumns,
|
canEditColumns,
|
||||||
canSaveSchema,
|
canSaveSchema,
|
||||||
|
canSetRelationshipSchemas,
|
||||||
stripeRows,
|
stripeRows,
|
||||||
quiet,
|
quiet,
|
||||||
collaboration,
|
collaboration,
|
||||||
|
|
|
@ -4,35 +4,40 @@ export const createActions = context => {
|
||||||
// Cache for the primary display columns of different tables.
|
// Cache for the primary display columns of different tables.
|
||||||
// If we ever need to cache table definitions for other purposes then we can
|
// If we ever need to cache table definitions for other purposes then we can
|
||||||
// expand this to be a more generic cache.
|
// expand this to be a more generic cache.
|
||||||
let primaryDisplayCache = {}
|
let tableCache = {}
|
||||||
|
|
||||||
const resetPrimaryDisplayCache = () => {
|
const resetCache = () => {
|
||||||
primaryDisplayCache = {}
|
tableCache = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const getPrimaryDisplayForTableId = async tableId => {
|
const fetchTable = async tableId => {
|
||||||
// If we've never encountered this tableId before then store a promise that
|
// If we've never encountered this tableId before then store a promise that
|
||||||
// resolves to the primary display so that subsequent invocations before the
|
// resolves to the primary display so that subsequent invocations before the
|
||||||
// promise completes can reuse this promise
|
// promise completes can reuse this promise
|
||||||
if (!primaryDisplayCache[tableId]) {
|
if (!tableCache[tableId]) {
|
||||||
primaryDisplayCache[tableId] = new Promise(resolve => {
|
tableCache[tableId] = API.fetchTableDefinition(tableId)
|
||||||
API.fetchTableDefinition(tableId).then(def => {
|
}
|
||||||
const display = def?.primaryDisplay || def?.schema?.[0]?.name
|
// We await the result so that we account for both promises and primitives
|
||||||
primaryDisplayCache[tableId] = display
|
return await tableCache[tableId]
|
||||||
resolve(display)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We await the result so that we account for both promises and primitives
|
const getPrimaryDisplayForTableId = async tableId => {
|
||||||
return await primaryDisplayCache[tableId]
|
const table = await fetchTable(tableId)
|
||||||
|
const display = table?.primaryDisplay || table?.schema?.[0]?.name
|
||||||
|
return display
|
||||||
|
}
|
||||||
|
|
||||||
|
const getTable = async tableId => {
|
||||||
|
const table = await fetchTable(tableId)
|
||||||
|
return table
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
cache: {
|
cache: {
|
||||||
actions: {
|
actions: {
|
||||||
getPrimaryDisplayForTableId,
|
getPrimaryDisplayForTableId,
|
||||||
resetPrimaryDisplayCache,
|
getTable,
|
||||||
|
resetCache,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -43,5 +48,5 @@ export const initialise = context => {
|
||||||
|
|
||||||
// Wipe the caches whenever the datasource changes to ensure we aren't
|
// Wipe the caches whenever the datasource changes to ensure we aren't
|
||||||
// storing any stale information
|
// storing any stale information
|
||||||
datasource.subscribe(cache.actions.resetPrimaryDisplayCache)
|
datasource.subscribe(cache.actions.resetCache)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,16 +5,24 @@ import { memo } from "../../../utils"
|
||||||
export const createStores = () => {
|
export const createStores = () => {
|
||||||
const definition = memo(null)
|
const definition = memo(null)
|
||||||
const schemaMutations = memo({})
|
const schemaMutations = memo({})
|
||||||
|
const subSchemaMutations = memo({})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
definition,
|
definition,
|
||||||
schemaMutations,
|
schemaMutations,
|
||||||
|
subSchemaMutations,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const deriveStores = context => {
|
export const deriveStores = context => {
|
||||||
const { API, definition, schemaOverrides, datasource, schemaMutations } =
|
const {
|
||||||
context
|
API,
|
||||||
|
definition,
|
||||||
|
schemaOverrides,
|
||||||
|
datasource,
|
||||||
|
schemaMutations,
|
||||||
|
subSchemaMutations,
|
||||||
|
} = context
|
||||||
|
|
||||||
const schema = derived(definition, $definition => {
|
const schema = derived(definition, $definition => {
|
||||||
let schema = getDatasourceSchema({
|
let schema = getDatasourceSchema({
|
||||||
|
@ -40,8 +48,8 @@ export const deriveStores = context => {
|
||||||
// Derives the total enriched schema, made up of the saved schema and any
|
// Derives the total enriched schema, made up of the saved schema and any
|
||||||
// prop and user overrides
|
// prop and user overrides
|
||||||
const enrichedSchema = derived(
|
const enrichedSchema = derived(
|
||||||
[schema, schemaOverrides, schemaMutations],
|
[schema, schemaOverrides, schemaMutations, subSchemaMutations],
|
||||||
([$schema, $schemaOverrides, $schemaMutations]) => {
|
([$schema, $schemaOverrides, $schemaMutations, $subSchemaMutations]) => {
|
||||||
if (!$schema) {
|
if (!$schema) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
@ -52,6 +60,18 @@ export const deriveStores = context => {
|
||||||
...$schemaOverrides?.[field],
|
...$schemaOverrides?.[field],
|
||||||
...$schemaMutations[field],
|
...$schemaMutations[field],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($subSchemaMutations[field]) {
|
||||||
|
enrichedSchema[field].columns ??= {}
|
||||||
|
for (const [fieldName, mutation] of Object.entries(
|
||||||
|
$subSchemaMutations[field]
|
||||||
|
)) {
|
||||||
|
enrichedSchema[field].columns[fieldName] = {
|
||||||
|
...enrichedSchema[field].columns[fieldName],
|
||||||
|
...mutation,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
return enrichedSchema
|
return enrichedSchema
|
||||||
}
|
}
|
||||||
|
@ -83,6 +103,7 @@ export const createActions = context => {
|
||||||
viewV2,
|
viewV2,
|
||||||
nonPlus,
|
nonPlus,
|
||||||
schemaMutations,
|
schemaMutations,
|
||||||
|
subSchemaMutations,
|
||||||
schema,
|
schema,
|
||||||
notifications,
|
notifications,
|
||||||
} = context
|
} = context
|
||||||
|
@ -162,6 +183,25 @@ export const createActions = context => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Adds a nested schema mutation for a single field
|
||||||
|
const addSubSchemaMutation = (field, fromField, mutation) => {
|
||||||
|
if (!field || !fromField || !mutation) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
subSchemaMutations.update($subSchemaMutations => {
|
||||||
|
return {
|
||||||
|
...$subSchemaMutations,
|
||||||
|
[fromField]: {
|
||||||
|
...$subSchemaMutations[fromField],
|
||||||
|
[field]: {
|
||||||
|
...($subSchemaMutations[fromField] || {})[field],
|
||||||
|
...mutation,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Adds schema mutations for multiple fields at once
|
// Adds schema mutations for multiple fields at once
|
||||||
const addSchemaMutations = mutations => {
|
const addSchemaMutations = mutations => {
|
||||||
const fields = Object.keys(mutations || {})
|
const fields = Object.keys(mutations || {})
|
||||||
|
@ -188,6 +228,7 @@ export const createActions = context => {
|
||||||
}
|
}
|
||||||
const $definition = get(definition)
|
const $definition = get(definition)
|
||||||
const $schemaMutations = get(schemaMutations)
|
const $schemaMutations = get(schemaMutations)
|
||||||
|
const $subSchemaMutations = get(subSchemaMutations)
|
||||||
const $schema = get(schema)
|
const $schema = get(schema)
|
||||||
let newSchema = {}
|
let newSchema = {}
|
||||||
|
|
||||||
|
@ -197,6 +238,17 @@ export const createActions = context => {
|
||||||
...$schema[column],
|
...$schema[column],
|
||||||
...$schemaMutations[column],
|
...$schemaMutations[column],
|
||||||
}
|
}
|
||||||
|
if ($subSchemaMutations[column]) {
|
||||||
|
newSchema[column].columns ??= {}
|
||||||
|
for (const [fieldName, mutation] of Object.entries(
|
||||||
|
$subSchemaMutations[column]
|
||||||
|
)) {
|
||||||
|
newSchema[column].columns[fieldName] = {
|
||||||
|
...newSchema[column].columns[fieldName],
|
||||||
|
...mutation,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Save the changes, then reset our local mutations
|
// Save the changes, then reset our local mutations
|
||||||
|
@ -209,6 +261,7 @@ export const createActions = context => {
|
||||||
|
|
||||||
const resetSchemaMutations = () => {
|
const resetSchemaMutations = () => {
|
||||||
schemaMutations.set({})
|
schemaMutations.set({})
|
||||||
|
subSchemaMutations.set({})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds a row to the datasource
|
// Adds a row to the datasource
|
||||||
|
@ -255,6 +308,7 @@ export const createActions = context => {
|
||||||
canUseColumn,
|
canUseColumn,
|
||||||
changePrimaryDisplay,
|
changePrimaryDisplay,
|
||||||
addSchemaMutation,
|
addSchemaMutation,
|
||||||
|
addSubSchemaMutation,
|
||||||
addSchemaMutations,
|
addSchemaMutations,
|
||||||
saveSchemaMutations,
|
saveSchemaMutations,
|
||||||
resetSchemaMutations,
|
resetSchemaMutations,
|
||||||
|
|
|
@ -172,3 +172,9 @@ export const TypeIconMap = {
|
||||||
export const OptionColours = [...new Array(12).keys()].map(idx => {
|
export const OptionColours = [...new Array(12).keys()].map(idx => {
|
||||||
return `hsla(${((idx + 1) * 222) % 360}, 90%, 75%, 0.3)`
|
return `hsla(${((idx + 1) * 222) % 360}, 90%, 75%, 0.3)`
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export const FieldPermissions = {
|
||||||
|
WRITABLE: "writable",
|
||||||
|
READONLY: "readonly",
|
||||||
|
HIDDEN: "hidden",
|
||||||
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ export async function handleRequest<T extends Operation>(
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
|
export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
|
||||||
const { tableId } = utils.getSourceId(ctx)
|
const { tableId, viewId } = utils.getSourceId(ctx)
|
||||||
|
|
||||||
const { _id, ...rowData } = ctx.request.body
|
const { _id, ...rowData } = ctx.request.body
|
||||||
const table = await sdk.tables.getTable(tableId)
|
const table = await sdk.tables.getTable(tableId)
|
||||||
|
@ -77,6 +77,7 @@ export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
|
||||||
outputProcessing(table, row, {
|
outputProcessing(table, row, {
|
||||||
squash: true,
|
squash: true,
|
||||||
preserveLinks: true,
|
preserveLinks: true,
|
||||||
|
fromViewId: viewId,
|
||||||
}),
|
}),
|
||||||
outputProcessing(table, beforeRow, {
|
outputProcessing(table, beforeRow, {
|
||||||
squash: true,
|
squash: true,
|
||||||
|
@ -163,14 +164,10 @@ export async function fetchEnrichedRow(ctx: UserCtx) {
|
||||||
},
|
},
|
||||||
includeSqlRelationships: IncludeRelationship.INCLUDE,
|
includeSqlRelationships: IncludeRelationship.INCLUDE,
|
||||||
})
|
})
|
||||||
row[fieldName] = await outputProcessing<Row[]>(
|
row[fieldName] = await outputProcessing(linkedTable, relatedRows.rows, {
|
||||||
linkedTable,
|
|
||||||
relatedRows.rows,
|
|
||||||
{
|
|
||||||
squash: true,
|
squash: true,
|
||||||
preserveLinks: true,
|
preserveLinks: true,
|
||||||
}
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
return row
|
return row
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,8 +71,10 @@ export async function patch(
|
||||||
}
|
}
|
||||||
|
|
||||||
export const save = async (ctx: UserCtx<Row, Row>) => {
|
export const save = async (ctx: UserCtx<Row, Row>) => {
|
||||||
|
const { tableId, viewId } = utils.getSourceId(ctx)
|
||||||
|
const sourceId = viewId || tableId
|
||||||
|
|
||||||
const appId = ctx.appId
|
const appId = ctx.appId
|
||||||
const { tableId } = utils.getSourceId(ctx)
|
|
||||||
const body = ctx.request.body
|
const body = ctx.request.body
|
||||||
|
|
||||||
// user metadata doesn't exist yet - don't allow creation
|
// user metadata doesn't exist yet - don't allow creation
|
||||||
|
@ -85,9 +87,9 @@ export const save = async (ctx: UserCtx<Row, Row>) => {
|
||||||
return patch(ctx as UserCtx<PatchRowRequest, PatchRowResponse>)
|
return patch(ctx as UserCtx<PatchRowRequest, PatchRowResponse>)
|
||||||
}
|
}
|
||||||
const { row, table, squashed } = tableId.includes("datasource_plus")
|
const { row, table, squashed } = tableId.includes("datasource_plus")
|
||||||
? await sdk.rows.save(tableId, ctx.request.body, ctx.user?._id)
|
? await sdk.rows.save(sourceId, ctx.request.body, ctx.user?._id)
|
||||||
: await quotas.addRow(() =>
|
: await quotas.addRow(() =>
|
||||||
sdk.rows.save(tableId, ctx.request.body, ctx.user?._id)
|
sdk.rows.save(sourceId, ctx.request.body, ctx.user?._id)
|
||||||
)
|
)
|
||||||
ctx.status = 200
|
ctx.status = 200
|
||||||
ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:save`, appId, row, table)
|
ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:save`, appId, row, table)
|
||||||
|
@ -115,10 +117,12 @@ export async function fetch(ctx: any) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function find(ctx: UserCtx<void, GetRowResponse>) {
|
export async function find(ctx: UserCtx<void, GetRowResponse>) {
|
||||||
const { tableId } = utils.getSourceId(ctx)
|
const { tableId, viewId } = utils.getSourceId(ctx)
|
||||||
|
const sourceId = viewId || tableId
|
||||||
const rowId = ctx.params.rowId
|
const rowId = ctx.params.rowId
|
||||||
|
|
||||||
ctx.body = await sdk.rows.find(tableId, rowId)
|
const response = await sdk.rows.find(sourceId, rowId)
|
||||||
|
ctx.body = response
|
||||||
}
|
}
|
||||||
|
|
||||||
function isDeleteRows(input: any): input is DeleteRows {
|
function isDeleteRows(input: any): input is DeleteRows {
|
||||||
|
|
|
@ -23,7 +23,7 @@ import { getLinkedTableIDs } from "../../../db/linkedRows/linkUtils"
|
||||||
import { flatten } from "lodash"
|
import { flatten } from "lodash"
|
||||||
|
|
||||||
export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
|
export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
|
||||||
const { tableId } = utils.getSourceId(ctx)
|
const { tableId, viewId } = utils.getSourceId(ctx)
|
||||||
const inputs = ctx.request.body
|
const inputs = ctx.request.body
|
||||||
const isUserTable = tableId === InternalTables.USER_METADATA
|
const isUserTable = tableId === InternalTables.USER_METADATA
|
||||||
let oldRow
|
let oldRow
|
||||||
|
@ -90,6 +90,7 @@ export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
|
||||||
const result = await finaliseRow(table, row, {
|
const result = await finaliseRow(table, row, {
|
||||||
oldTable: dbTable,
|
oldTable: dbTable,
|
||||||
updateFormula: true,
|
updateFormula: true,
|
||||||
|
fromViewId: viewId,
|
||||||
})
|
})
|
||||||
|
|
||||||
return { ...result, oldRow }
|
return { ...result, oldRow }
|
||||||
|
|
|
@ -123,7 +123,11 @@ export async function updateAllFormulasInTable(table: Table) {
|
||||||
export async function finaliseRow(
|
export async function finaliseRow(
|
||||||
table: Table,
|
table: Table,
|
||||||
row: Row,
|
row: Row,
|
||||||
{ oldTable, updateFormula }: { oldTable?: Table; updateFormula: boolean } = {
|
{
|
||||||
|
oldTable,
|
||||||
|
updateFormula,
|
||||||
|
fromViewId,
|
||||||
|
}: { oldTable?: Table; updateFormula: boolean; fromViewId?: string } = {
|
||||||
updateFormula: true,
|
updateFormula: true,
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
|
@ -154,9 +158,8 @@ export async function finaliseRow(
|
||||||
if (updateFormula) {
|
if (updateFormula) {
|
||||||
await updateRelatedFormula(table, enrichedRow)
|
await updateRelatedFormula(table, enrichedRow)
|
||||||
}
|
}
|
||||||
const squashed = await linkRows.squashLinksToPrimaryDisplay(
|
const squashed = await linkRows.squashLinks(table, enrichedRow, {
|
||||||
table,
|
fromViewId,
|
||||||
enrichedRow
|
})
|
||||||
)
|
|
||||||
return { row: enrichedRow, squashed, table }
|
return { row: enrichedRow, squashed, table }
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,8 +75,11 @@ export async function searchView(
|
||||||
})
|
})
|
||||||
|
|
||||||
const searchOptions: RequiredKeys<SearchViewRowRequest> &
|
const searchOptions: RequiredKeys<SearchViewRowRequest> &
|
||||||
RequiredKeys<Pick<RowSearchParams, "tableId" | "query" | "fields">> = {
|
RequiredKeys<
|
||||||
|
Pick<RowSearchParams, "tableId" | "viewId" | "query" | "fields">
|
||||||
|
> = {
|
||||||
tableId: view.tableId,
|
tableId: view.tableId,
|
||||||
|
viewId: view.id,
|
||||||
query: enrichedQuery,
|
query: enrichedQuery,
|
||||||
fields: viewFields,
|
fields: viewFields,
|
||||||
...getSortOptions(body, view),
|
...getSortOptions(body, view),
|
||||||
|
|
|
@ -85,14 +85,19 @@ export async function fetch(ctx: UserCtx<void, FetchTablesResponse>) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
ctx.body = [...internal, ...external].map(sdk.tables.enrichViewSchemas)
|
const result: FetchTablesResponse = []
|
||||||
|
for (const table of [...internal, ...external]) {
|
||||||
|
result.push(await sdk.tables.enrichViewSchemas(table))
|
||||||
|
}
|
||||||
|
ctx.body = result
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function find(ctx: UserCtx<void, TableResponse>) {
|
export async function find(ctx: UserCtx<void, TableResponse>) {
|
||||||
const tableId = ctx.params.tableId
|
const tableId = ctx.params.tableId
|
||||||
const table = await sdk.tables.getTable(tableId)
|
const table = await sdk.tables.getTable(tableId)
|
||||||
|
|
||||||
ctx.body = sdk.tables.enrichViewSchemas(table)
|
const result = await sdk.tables.enrichViewSchemas(table)
|
||||||
|
ctx.body = result
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
|
export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
|
||||||
|
@ -106,11 +111,14 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
|
||||||
const api = pickApi({ table })
|
const api = pickApi({ table })
|
||||||
let savedTable = await api.save(ctx, renaming)
|
let savedTable = await api.save(ctx, renaming)
|
||||||
if (!table._id) {
|
if (!table._id) {
|
||||||
savedTable = sdk.tables.enrichViewSchemas(savedTable)
|
savedTable = await sdk.tables.enrichViewSchemas(savedTable)
|
||||||
await events.table.created(savedTable)
|
await events.table.created(savedTable)
|
||||||
} else {
|
} else {
|
||||||
await events.table.updated(savedTable)
|
await events.table.updated(savedTable)
|
||||||
}
|
}
|
||||||
|
if (renaming) {
|
||||||
|
await sdk.views.renameLinkedViews(savedTable, renaming)
|
||||||
|
}
|
||||||
if (isImport) {
|
if (isImport) {
|
||||||
await events.table.imported(savedTable)
|
await events.table.imported(savedTable)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,12 @@ import {
|
||||||
CreateViewRequest,
|
CreateViewRequest,
|
||||||
Ctx,
|
Ctx,
|
||||||
RequiredKeys,
|
RequiredKeys,
|
||||||
ViewUIFieldMetadata,
|
|
||||||
UpdateViewRequest,
|
UpdateViewRequest,
|
||||||
ViewResponse,
|
ViewResponse,
|
||||||
ViewResponseEnriched,
|
ViewResponseEnriched,
|
||||||
ViewV2,
|
ViewV2,
|
||||||
|
ViewFieldMetadata,
|
||||||
|
RelationSchemaField,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { builderSocket, gridSocket } from "../../../websockets"
|
import { builderSocket, gridSocket } from "../../../websockets"
|
||||||
|
|
||||||
|
@ -18,21 +19,41 @@ async function parseSchema(view: CreateViewRequest) {
|
||||||
const finalViewSchema =
|
const finalViewSchema =
|
||||||
view.schema &&
|
view.schema &&
|
||||||
Object.entries(view.schema).reduce((p, [fieldName, schemaValue]) => {
|
Object.entries(view.schema).reduce((p, [fieldName, schemaValue]) => {
|
||||||
const fieldSchema: RequiredKeys<ViewUIFieldMetadata> = {
|
let fieldRelatedSchema:
|
||||||
|
| Record<string, RequiredKeys<RelationSchemaField>>
|
||||||
|
| undefined
|
||||||
|
if (schemaValue.columns) {
|
||||||
|
fieldRelatedSchema = Object.entries(schemaValue.columns).reduce<
|
||||||
|
NonNullable<typeof fieldRelatedSchema>
|
||||||
|
>((acc, [key, fieldSchema]) => {
|
||||||
|
acc[key] = {
|
||||||
|
visible: fieldSchema.visible,
|
||||||
|
readonly: fieldSchema.readonly,
|
||||||
|
}
|
||||||
|
return acc
|
||||||
|
}, {})
|
||||||
|
}
|
||||||
|
|
||||||
|
const fieldSchema: RequiredKeys<
|
||||||
|
ViewFieldMetadata & {
|
||||||
|
columns: typeof fieldRelatedSchema
|
||||||
|
}
|
||||||
|
> = {
|
||||||
order: schemaValue.order,
|
order: schemaValue.order,
|
||||||
width: schemaValue.width,
|
width: schemaValue.width,
|
||||||
visible: schemaValue.visible,
|
visible: schemaValue.visible,
|
||||||
readonly: schemaValue.readonly,
|
readonly: schemaValue.readonly,
|
||||||
icon: schemaValue.icon,
|
icon: schemaValue.icon,
|
||||||
|
columns: fieldRelatedSchema,
|
||||||
}
|
}
|
||||||
Object.entries(fieldSchema)
|
Object.entries(fieldSchema)
|
||||||
.filter(([, val]) => val === undefined)
|
.filter(([, val]) => val === undefined)
|
||||||
.forEach(([key]) => {
|
.forEach(([key]) => {
|
||||||
delete fieldSchema[key as keyof ViewUIFieldMetadata]
|
delete fieldSchema[key as keyof ViewFieldMetadata]
|
||||||
})
|
})
|
||||||
p[fieldName] = fieldSchema
|
p[fieldName] = fieldSchema
|
||||||
return p
|
return p
|
||||||
}, {} as Record<string, RequiredKeys<ViewUIFieldMetadata>>)
|
}, {} as Record<string, RequiredKeys<ViewFieldMetadata>>)
|
||||||
return finalViewSchema
|
return finalViewSchema
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ import { Automation, FieldType, Table } from "@budibase/types"
|
||||||
import { mocks } from "@budibase/backend-core/tests"
|
import { mocks } from "@budibase/backend-core/tests"
|
||||||
import { FilterConditions } from "../../../automations/steps/filter"
|
import { FilterConditions } from "../../../automations/steps/filter"
|
||||||
import { removeDeprecated } from "../../../automations/utils"
|
import { removeDeprecated } from "../../../automations/utils"
|
||||||
|
import { createAutomationBuilder } from "../../../automations/tests/utilities/AutomationTestBuilder"
|
||||||
|
|
||||||
const MAX_RETRIES = 4
|
const MAX_RETRIES = 4
|
||||||
let {
|
let {
|
||||||
|
@ -121,6 +122,104 @@ describe("/automations", () => {
|
||||||
expect(events.automation.stepCreated).toHaveBeenCalledTimes(2)
|
expect(events.automation.stepCreated).toHaveBeenCalledTimes(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("Should ensure you can't have a branch as not a last step", async () => {
|
||||||
|
const automation = createAutomationBuilder({
|
||||||
|
name: "String Equality Branching",
|
||||||
|
appId: config.getAppId(),
|
||||||
|
})
|
||||||
|
.appAction({ fields: { status: "active" } })
|
||||||
|
.branch({
|
||||||
|
activeBranch: {
|
||||||
|
steps: stepBuilder =>
|
||||||
|
stepBuilder.serverLog({ text: "Active user" }),
|
||||||
|
condition: {
|
||||||
|
equal: { "trigger.fields.status": "active" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.serverLog({ text: "Inactive user" })
|
||||||
|
.build()
|
||||||
|
|
||||||
|
await config.api.automation.post(automation, {
|
||||||
|
status: 400,
|
||||||
|
body: {
|
||||||
|
message:
|
||||||
|
"Invalid body - Branch steps are only allowed as the last step",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Should check validation on an automation that has a branch step with no children", async () => {
|
||||||
|
const automation = createAutomationBuilder({
|
||||||
|
name: "String Equality Branching",
|
||||||
|
appId: config.getAppId(),
|
||||||
|
})
|
||||||
|
.appAction({ fields: { status: "active" } })
|
||||||
|
.branch({})
|
||||||
|
.serverLog({ text: "Inactive user" })
|
||||||
|
.build()
|
||||||
|
|
||||||
|
await config.api.automation.post(automation, {
|
||||||
|
status: 400,
|
||||||
|
body: {
|
||||||
|
message:
|
||||||
|
'Invalid body - "definition.steps[0].inputs.branches" must contain at least 1 items',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Should check validation on a branch step with empty conditions", async () => {
|
||||||
|
const automation = createAutomationBuilder({
|
||||||
|
name: "String Equality Branching",
|
||||||
|
appId: config.getAppId(),
|
||||||
|
})
|
||||||
|
.appAction({ fields: { status: "active" } })
|
||||||
|
.branch({
|
||||||
|
activeBranch: {
|
||||||
|
steps: stepBuilder =>
|
||||||
|
stepBuilder.serverLog({ text: "Active user" }),
|
||||||
|
condition: {},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.build()
|
||||||
|
|
||||||
|
await config.api.automation.post(automation, {
|
||||||
|
status: 400,
|
||||||
|
body: {
|
||||||
|
message:
|
||||||
|
'Invalid body - "definition.steps[0].inputs.branches[0].condition" must have at least 1 key',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Should check validation on an branch that has a condition that is not valid", async () => {
|
||||||
|
const automation = createAutomationBuilder({
|
||||||
|
name: "String Equality Branching",
|
||||||
|
appId: config.getAppId(),
|
||||||
|
})
|
||||||
|
.appAction({ fields: { status: "active" } })
|
||||||
|
.branch({
|
||||||
|
activeBranch: {
|
||||||
|
steps: stepBuilder =>
|
||||||
|
stepBuilder.serverLog({ text: "Active user" }),
|
||||||
|
condition: {
|
||||||
|
//@ts-ignore
|
||||||
|
INCORRECT: { "trigger.fields.status": "active" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.serverLog({ text: "Inactive user" })
|
||||||
|
.build()
|
||||||
|
|
||||||
|
await config.api.automation.post(automation, {
|
||||||
|
status: 400,
|
||||||
|
body: {
|
||||||
|
message:
|
||||||
|
'Invalid body - "definition.steps[0].inputs.branches[0].condition.INCORRECT" is not allowed',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it("should apply authorization to endpoint", async () => {
|
it("should apply authorization to endpoint", async () => {
|
||||||
const automation = newAutomation()
|
const automation = newAutomation()
|
||||||
await checkBuilderEndpoint({
|
await checkBuilderEndpoint({
|
||||||
|
|
|
@ -40,6 +40,8 @@ import {
|
||||||
TableSchema,
|
TableSchema,
|
||||||
JsonFieldSubType,
|
JsonFieldSubType,
|
||||||
RowExportFormat,
|
RowExportFormat,
|
||||||
|
FeatureFlag,
|
||||||
|
RelationSchemaField,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { generator, mocks } from "@budibase/backend-core/tests"
|
import { generator, mocks } from "@budibase/backend-core/tests"
|
||||||
import _, { merge } from "lodash"
|
import _, { merge } from "lodash"
|
||||||
|
@ -1231,7 +1233,7 @@ describe.each([
|
||||||
})
|
})
|
||||||
|
|
||||||
it.each([{ not: "valid" }, { rows: 123 }, "invalid"])(
|
it.each([{ not: "valid" }, { rows: 123 }, "invalid"])(
|
||||||
"Should ignore malformed/invalid delete request: %s",
|
"should ignore malformed/invalid delete request: %s",
|
||||||
async (request: any) => {
|
async (request: any) => {
|
||||||
const rowUsage = await getRowUsage()
|
const rowUsage = await getRowUsage()
|
||||||
|
|
||||||
|
@ -2426,6 +2428,376 @@ describe.each([
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Upserting isn't yet supported in MSSQL or Oracle, see:
|
||||||
|
// https://github.com/knex/knex/pull/6050
|
||||||
|
!isMSSQL &&
|
||||||
|
!isOracle &&
|
||||||
|
describe("relationships", () => {
|
||||||
|
let tableId: string
|
||||||
|
let viewId: string
|
||||||
|
|
||||||
|
let auxData: Row[] = []
|
||||||
|
|
||||||
|
let flagCleanup: (() => void) | undefined
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
flagCleanup = setCoreEnv({
|
||||||
|
TENANT_FEATURE_FLAGS: `*:${FeatureFlag.ENRICHED_RELATIONSHIPS}`,
|
||||||
|
})
|
||||||
|
|
||||||
|
const aux2Table = await config.api.table.save(saveTableRequest())
|
||||||
|
const aux2Data = await config.api.row.save(aux2Table._id!, {})
|
||||||
|
|
||||||
|
const auxTable = await config.api.table.save(
|
||||||
|
saveTableRequest({
|
||||||
|
primaryDisplay: "name",
|
||||||
|
schema: {
|
||||||
|
name: {
|
||||||
|
name: "name",
|
||||||
|
type: FieldType.STRING,
|
||||||
|
constraints: { presence: true },
|
||||||
|
},
|
||||||
|
age: {
|
||||||
|
name: "age",
|
||||||
|
type: FieldType.NUMBER,
|
||||||
|
constraints: { presence: true },
|
||||||
|
},
|
||||||
|
address: {
|
||||||
|
name: "address",
|
||||||
|
type: FieldType.STRING,
|
||||||
|
constraints: { presence: true },
|
||||||
|
visible: false,
|
||||||
|
},
|
||||||
|
link: {
|
||||||
|
name: "link",
|
||||||
|
type: FieldType.LINK,
|
||||||
|
tableId: aux2Table._id!,
|
||||||
|
relationshipType: RelationshipType.MANY_TO_MANY,
|
||||||
|
fieldName: "fk_aux",
|
||||||
|
constraints: { presence: true },
|
||||||
|
},
|
||||||
|
formula: {
|
||||||
|
name: "formula",
|
||||||
|
type: FieldType.FORMULA,
|
||||||
|
formula: "{{ any }}",
|
||||||
|
constraints: { presence: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
const auxTableId = auxTable._id!
|
||||||
|
|
||||||
|
for (const name of generator.unique(() => generator.name(), 10)) {
|
||||||
|
auxData.push(
|
||||||
|
await config.api.row.save(auxTableId, {
|
||||||
|
name,
|
||||||
|
age: generator.age(),
|
||||||
|
address: generator.address(),
|
||||||
|
link: [aux2Data],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const table = await config.api.table.save(
|
||||||
|
saveTableRequest({
|
||||||
|
schema: {
|
||||||
|
title: {
|
||||||
|
name: "title",
|
||||||
|
type: FieldType.STRING,
|
||||||
|
constraints: { presence: true },
|
||||||
|
},
|
||||||
|
relWithNoSchema: {
|
||||||
|
name: "relWithNoSchema",
|
||||||
|
relationshipType: RelationshipType.ONE_TO_MANY,
|
||||||
|
type: FieldType.LINK,
|
||||||
|
tableId: auxTableId,
|
||||||
|
fieldName: "fk_relWithNoSchema",
|
||||||
|
constraints: { presence: true },
|
||||||
|
},
|
||||||
|
relWithEmptySchema: {
|
||||||
|
name: "relWithEmptySchema",
|
||||||
|
relationshipType: RelationshipType.ONE_TO_MANY,
|
||||||
|
type: FieldType.LINK,
|
||||||
|
tableId: auxTableId,
|
||||||
|
fieldName: "fk_relWithEmptySchema",
|
||||||
|
constraints: { presence: true },
|
||||||
|
},
|
||||||
|
relWithFullSchema: {
|
||||||
|
name: "relWithFullSchema",
|
||||||
|
relationshipType: RelationshipType.ONE_TO_MANY,
|
||||||
|
type: FieldType.LINK,
|
||||||
|
tableId: auxTableId,
|
||||||
|
fieldName: "fk_relWithFullSchema",
|
||||||
|
constraints: { presence: true },
|
||||||
|
},
|
||||||
|
relWithHalfSchema: {
|
||||||
|
name: "relWithHalfSchema",
|
||||||
|
relationshipType: RelationshipType.ONE_TO_MANY,
|
||||||
|
type: FieldType.LINK,
|
||||||
|
tableId: auxTableId,
|
||||||
|
fieldName: "fk_relWithHalfSchema",
|
||||||
|
constraints: { presence: true },
|
||||||
|
},
|
||||||
|
relWithIllegalSchema: {
|
||||||
|
name: "relWithIllegalSchema",
|
||||||
|
relationshipType: RelationshipType.ONE_TO_MANY,
|
||||||
|
type: FieldType.LINK,
|
||||||
|
tableId: auxTableId,
|
||||||
|
fieldName: "fk_relWithIllegalSchema",
|
||||||
|
constraints: { presence: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
tableId = table._id!
|
||||||
|
const view = await config.api.viewV2.create({
|
||||||
|
name: generator.guid(),
|
||||||
|
tableId,
|
||||||
|
schema: {
|
||||||
|
title: {
|
||||||
|
visible: true,
|
||||||
|
},
|
||||||
|
relWithNoSchema: {
|
||||||
|
visible: true,
|
||||||
|
},
|
||||||
|
relWithEmptySchema: {
|
||||||
|
visible: true,
|
||||||
|
columns: {},
|
||||||
|
},
|
||||||
|
relWithFullSchema: {
|
||||||
|
visible: true,
|
||||||
|
columns: Object.keys(auxTable.schema).reduce<
|
||||||
|
Record<string, RelationSchemaField>
|
||||||
|
>((acc, c) => ({ ...acc, [c]: { visible: true } }), {}),
|
||||||
|
},
|
||||||
|
relWithHalfSchema: {
|
||||||
|
visible: true,
|
||||||
|
columns: {
|
||||||
|
name: { visible: true },
|
||||||
|
age: { visible: false, readonly: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
relWithIllegalSchema: {
|
||||||
|
visible: true,
|
||||||
|
columns: {
|
||||||
|
name: { visible: true },
|
||||||
|
address: { visible: true },
|
||||||
|
unexisting: { visible: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
viewId = view.id
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
flagCleanup?.()
|
||||||
|
})
|
||||||
|
|
||||||
|
const testScenarios: [string, (row: Row) => Promise<Row> | Row][] = [
|
||||||
|
["get row", (row: Row) => config.api.row.get(viewId, row._id!)],
|
||||||
|
[
|
||||||
|
"from view search",
|
||||||
|
async (row: Row) => {
|
||||||
|
const { rows } = await config.api.viewV2.search(viewId)
|
||||||
|
return rows.find(r => r._id === row._id!)
|
||||||
|
},
|
||||||
|
],
|
||||||
|
["from original saved row", (row: Row) => row],
|
||||||
|
["from updated row", (row: Row) => config.api.row.save(viewId, row)],
|
||||||
|
]
|
||||||
|
|
||||||
|
it.each(testScenarios)(
|
||||||
|
"can retrieve rows with populated relationships (via %s)",
|
||||||
|
async (__, retrieveDelegate) => {
|
||||||
|
const otherRows = _.sampleSize(auxData, 5)
|
||||||
|
|
||||||
|
const row = await config.api.row.save(viewId, {
|
||||||
|
title: generator.word(),
|
||||||
|
relWithNoSchema: [otherRows[0]],
|
||||||
|
relWithEmptySchema: [otherRows[1]],
|
||||||
|
relWithFullSchema: [otherRows[2]],
|
||||||
|
relWithHalfSchema: [otherRows[3]],
|
||||||
|
relWithIllegalSchema: [otherRows[4]],
|
||||||
|
})
|
||||||
|
|
||||||
|
const retrieved = await retrieveDelegate(row)
|
||||||
|
|
||||||
|
expect(retrieved).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
title: row.title,
|
||||||
|
relWithNoSchema: [
|
||||||
|
{
|
||||||
|
_id: otherRows[0]._id,
|
||||||
|
primaryDisplay: otherRows[0].name,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
relWithEmptySchema: [
|
||||||
|
{
|
||||||
|
_id: otherRows[1]._id,
|
||||||
|
primaryDisplay: otherRows[1].name,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
relWithFullSchema: [
|
||||||
|
{
|
||||||
|
_id: otherRows[2]._id,
|
||||||
|
primaryDisplay: otherRows[2].name,
|
||||||
|
name: otherRows[2].name,
|
||||||
|
age: otherRows[2].age,
|
||||||
|
id: otherRows[2].id,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
relWithHalfSchema: [
|
||||||
|
{
|
||||||
|
_id: otherRows[3]._id,
|
||||||
|
primaryDisplay: otherRows[3].name,
|
||||||
|
name: otherRows[3].name,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
relWithIllegalSchema: [
|
||||||
|
{
|
||||||
|
_id: otherRows[4]._id,
|
||||||
|
primaryDisplay: otherRows[4].name,
|
||||||
|
name: otherRows[4].name,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it.each(testScenarios)(
|
||||||
|
"does not enrich relationships when not enabled (via %s)",
|
||||||
|
async (__, retrieveDelegate) => {
|
||||||
|
await withCoreEnv(
|
||||||
|
{
|
||||||
|
TENANT_FEATURE_FLAGS: ``,
|
||||||
|
},
|
||||||
|
async () => {
|
||||||
|
const otherRows = _.sampleSize(auxData, 5)
|
||||||
|
|
||||||
|
const row = await config.api.row.save(viewId, {
|
||||||
|
title: generator.word(),
|
||||||
|
relWithNoSchema: [otherRows[0]],
|
||||||
|
relWithEmptySchema: [otherRows[1]],
|
||||||
|
relWithFullSchema: [otherRows[2]],
|
||||||
|
relWithHalfSchema: [otherRows[3]],
|
||||||
|
relWithIllegalSchema: [otherRows[4]],
|
||||||
|
})
|
||||||
|
|
||||||
|
const retrieved = await retrieveDelegate(row)
|
||||||
|
|
||||||
|
expect(retrieved).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
title: row.title,
|
||||||
|
relWithNoSchema: [
|
||||||
|
{
|
||||||
|
_id: otherRows[0]._id,
|
||||||
|
primaryDisplay: otherRows[0].name,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
relWithEmptySchema: [
|
||||||
|
{
|
||||||
|
_id: otherRows[1]._id,
|
||||||
|
primaryDisplay: otherRows[1].name,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
relWithFullSchema: [
|
||||||
|
{
|
||||||
|
_id: otherRows[2]._id,
|
||||||
|
primaryDisplay: otherRows[2].name,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
relWithHalfSchema: [
|
||||||
|
{
|
||||||
|
_id: otherRows[3]._id,
|
||||||
|
primaryDisplay: otherRows[3].name,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
relWithIllegalSchema: [
|
||||||
|
{
|
||||||
|
_id: otherRows[4]._id,
|
||||||
|
primaryDisplay: otherRows[4].name,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
[
|
||||||
|
"from table fetch",
|
||||||
|
async (row: Row) => {
|
||||||
|
const rows = await config.api.row.fetch(tableId)
|
||||||
|
return rows.find(r => r._id === row._id!)
|
||||||
|
},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"from table search",
|
||||||
|
async (row: Row) => {
|
||||||
|
const { rows } = await config.api.row.search(tableId)
|
||||||
|
return rows.find(r => r._id === row._id!)
|
||||||
|
},
|
||||||
|
],
|
||||||
|
])(
|
||||||
|
"does not enrich when fetching from the table (via %s)",
|
||||||
|
async (__, retrieveDelegate) => {
|
||||||
|
const otherRows = _.sampleSize(auxData, 5)
|
||||||
|
|
||||||
|
const row = await config.api.row.save(viewId, {
|
||||||
|
title: generator.word(),
|
||||||
|
relWithNoSchema: [otherRows[0]],
|
||||||
|
relWithEmptySchema: [otherRows[1]],
|
||||||
|
relWithFullSchema: [otherRows[2]],
|
||||||
|
relWithHalfSchema: [otherRows[3]],
|
||||||
|
relWithIllegalSchema: [otherRows[4]],
|
||||||
|
})
|
||||||
|
|
||||||
|
const retrieved = await retrieveDelegate(row)
|
||||||
|
|
||||||
|
expect(retrieved).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
title: row.title,
|
||||||
|
relWithNoSchema: [
|
||||||
|
{
|
||||||
|
_id: otherRows[0]._id,
|
||||||
|
primaryDisplay: otherRows[0].name,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
relWithEmptySchema: [
|
||||||
|
{
|
||||||
|
_id: otherRows[1]._id,
|
||||||
|
primaryDisplay: otherRows[1].name,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
relWithFullSchema: [
|
||||||
|
{
|
||||||
|
_id: otherRows[2]._id,
|
||||||
|
primaryDisplay: otherRows[2].name,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
relWithHalfSchema: [
|
||||||
|
{
|
||||||
|
_id: otherRows[3]._id,
|
||||||
|
primaryDisplay: otherRows[3].name,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
relWithIllegalSchema: [
|
||||||
|
{
|
||||||
|
_id: otherRows[4]._id,
|
||||||
|
primaryDisplay: otherRows[4].name,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
describe("Formula fields", () => {
|
describe("Formula fields", () => {
|
||||||
let table: Table
|
let table: Table
|
||||||
let otherTable: Table
|
let otherTable: Table
|
||||||
|
|
|
@ -5,11 +5,10 @@ import {
|
||||||
CreateRowActionRequest,
|
CreateRowActionRequest,
|
||||||
DocumentType,
|
DocumentType,
|
||||||
PermissionLevel,
|
PermissionLevel,
|
||||||
Row,
|
|
||||||
RowActionResponse,
|
RowActionResponse,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import * as setup from "./utilities"
|
import * as setup from "./utilities"
|
||||||
import { generator } from "@budibase/backend-core/tests"
|
import { generator, mocks } from "@budibase/backend-core/tests"
|
||||||
import { Expectations } from "../../../tests/utilities/api/base"
|
import { Expectations } from "../../../tests/utilities/api/base"
|
||||||
import { roles } from "@budibase/backend-core"
|
import { roles } from "@budibase/backend-core"
|
||||||
import { automations } from "@budibase/pro"
|
import { automations } from "@budibase/pro"
|
||||||
|
@ -651,13 +650,27 @@ describe("/rowsActions", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("trigger", () => {
|
describe("trigger", () => {
|
||||||
let row: Row
|
let viewId: string
|
||||||
|
let rowId: string
|
||||||
let rowAction: RowActionResponse
|
let rowAction: RowActionResponse
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
row = await config.api.row.save(tableId, {})
|
const row = await config.api.row.save(tableId, {})
|
||||||
|
rowId = row._id!
|
||||||
rowAction = await createRowAction(tableId, createRowActionRequest())
|
rowAction = await createRowAction(tableId, createRowActionRequest())
|
||||||
|
|
||||||
|
viewId = (
|
||||||
|
await config.api.viewV2.create(
|
||||||
|
setup.structures.viewV2.createRequest(tableId)
|
||||||
|
)
|
||||||
|
).id
|
||||||
|
|
||||||
|
await config.api.rowAction.setViewPermission(
|
||||||
|
tableId,
|
||||||
|
viewId,
|
||||||
|
rowAction.id
|
||||||
|
)
|
||||||
|
|
||||||
await config.publish()
|
await config.publish()
|
||||||
tk.travel(Date.now() + 100)
|
tk.travel(Date.now() + 100)
|
||||||
})
|
})
|
||||||
|
@ -672,21 +685,29 @@ describe("/rowsActions", () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
it("can trigger an automation given valid data", async () => {
|
it("can trigger an automation given valid data", async () => {
|
||||||
await config.api.rowAction.trigger(tableId, rowAction.id, {
|
expect(await getAutomationLogs()).toBeEmpty()
|
||||||
rowId: row._id!,
|
await config.api.rowAction.trigger(viewId, rowAction.id, { rowId })
|
||||||
})
|
|
||||||
|
|
||||||
const automationLogs = await getAutomationLogs()
|
const automationLogs = await getAutomationLogs()
|
||||||
expect(automationLogs).toEqual([
|
expect(automationLogs).toEqual([
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
automationId: rowAction.automationId,
|
automationId: rowAction.automationId,
|
||||||
trigger: expect.objectContaining({
|
trigger: {
|
||||||
|
id: "trigger",
|
||||||
|
stepId: "ROW_ACTION",
|
||||||
|
inputs: null,
|
||||||
outputs: {
|
outputs: {
|
||||||
fields: {},
|
fields: {},
|
||||||
row: await config.api.row.get(tableId, row._id!),
|
row: await config.api.row.get(tableId, rowId),
|
||||||
table: await config.api.table.get(tableId),
|
table: {
|
||||||
|
...(await config.api.table.get(tableId)),
|
||||||
|
views: expect.anything(),
|
||||||
},
|
},
|
||||||
|
automation: expect.objectContaining({
|
||||||
|
_id: rowAction.automationId,
|
||||||
}),
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
@ -702,9 +723,7 @@ describe("/rowsActions", () => {
|
||||||
await config.api.rowAction.trigger(
|
await config.api.rowAction.trigger(
|
||||||
viewId,
|
viewId,
|
||||||
rowAction.id,
|
rowAction.id,
|
||||||
{
|
{ rowId },
|
||||||
rowId: row._id!,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
status: 403,
|
status: 403,
|
||||||
body: {
|
body: {
|
||||||
|
@ -731,9 +750,9 @@ describe("/rowsActions", () => {
|
||||||
)
|
)
|
||||||
|
|
||||||
await config.publish()
|
await config.publish()
|
||||||
await config.api.rowAction.trigger(viewId, rowAction.id, {
|
|
||||||
rowId: row._id!,
|
expect(await getAutomationLogs()).toBeEmpty()
|
||||||
})
|
await config.api.rowAction.trigger(viewId, rowAction.id, { rowId })
|
||||||
|
|
||||||
const automationLogs = await getAutomationLogs()
|
const automationLogs = await getAutomationLogs()
|
||||||
expect(automationLogs).toEqual([
|
expect(automationLogs).toEqual([
|
||||||
|
@ -742,5 +761,170 @@ describe("/rowsActions", () => {
|
||||||
}),
|
}),
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("role permission checks", () => {
|
||||||
|
beforeAll(() => {
|
||||||
|
mocks.licenses.useViewPermissions()
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
mocks.licenses.useCloudFree()
|
||||||
|
})
|
||||||
|
|
||||||
|
function createUser(role: string) {
|
||||||
|
return config.createUser({
|
||||||
|
admin: { global: false },
|
||||||
|
builder: {},
|
||||||
|
roles: { [config.getProdAppId()]: role },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const allowedRoleConfig = (() => {
|
||||||
|
function getRolesLowerThan(role: string) {
|
||||||
|
const result = Object.values(roles.BUILTIN_ROLE_IDS).filter(
|
||||||
|
r => r !== role && roles.lowerBuiltinRoleID(r, role) === r
|
||||||
|
)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
return Object.values(roles.BUILTIN_ROLE_IDS).flatMap(r =>
|
||||||
|
[r, ...getRolesLowerThan(r)].map(p => [r, p])
|
||||||
|
)
|
||||||
|
})()
|
||||||
|
|
||||||
|
const disallowedRoleConfig = (() => {
|
||||||
|
function getRolesHigherThan(role: string) {
|
||||||
|
const result = Object.values(roles.BUILTIN_ROLE_IDS).filter(
|
||||||
|
r => r !== role && roles.lowerBuiltinRoleID(r, role) === role
|
||||||
|
)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
return Object.values(roles.BUILTIN_ROLE_IDS).flatMap(r =>
|
||||||
|
getRolesHigherThan(r).map(p => [r, p])
|
||||||
|
)
|
||||||
|
})()
|
||||||
|
|
||||||
|
describe.each([
|
||||||
|
[
|
||||||
|
"view (with implicit views)",
|
||||||
|
async () => {
|
||||||
|
const viewId = (
|
||||||
|
await config.api.viewV2.create(
|
||||||
|
setup.structures.viewV2.createRequest(tableId)
|
||||||
|
)
|
||||||
|
).id
|
||||||
|
|
||||||
|
await config.api.rowAction.setViewPermission(
|
||||||
|
tableId,
|
||||||
|
viewId,
|
||||||
|
rowAction.id
|
||||||
|
)
|
||||||
|
return { permissionResource: viewId, triggerResouce: viewId }
|
||||||
|
},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"view (without implicit views)",
|
||||||
|
async () => {
|
||||||
|
const viewId = (
|
||||||
|
await config.api.viewV2.create(
|
||||||
|
setup.structures.viewV2.createRequest(tableId)
|
||||||
|
)
|
||||||
|
).id
|
||||||
|
|
||||||
|
await config.api.rowAction.setViewPermission(
|
||||||
|
tableId,
|
||||||
|
viewId,
|
||||||
|
rowAction.id
|
||||||
|
)
|
||||||
|
return { permissionResource: tableId, triggerResouce: viewId }
|
||||||
|
},
|
||||||
|
],
|
||||||
|
])("checks for %s", (_, getResources) => {
|
||||||
|
it.each(allowedRoleConfig)(
|
||||||
|
"allows triggering if the user has read permission (user %s, table %s)",
|
||||||
|
async (userRole, resourcePermission) => {
|
||||||
|
const { permissionResource, triggerResouce } = await getResources()
|
||||||
|
|
||||||
|
await config.api.permission.add({
|
||||||
|
level: PermissionLevel.READ,
|
||||||
|
resourceId: permissionResource,
|
||||||
|
roleId: resourcePermission,
|
||||||
|
})
|
||||||
|
|
||||||
|
const normalUser = await createUser(userRole)
|
||||||
|
|
||||||
|
await config.withUser(normalUser, async () => {
|
||||||
|
await config.publish()
|
||||||
|
await config.api.rowAction.trigger(
|
||||||
|
triggerResouce,
|
||||||
|
rowAction.id,
|
||||||
|
{ rowId },
|
||||||
|
{ status: 200 }
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it.each(disallowedRoleConfig)(
|
||||||
|
"rejects if the user does not have table read permission (user %s, table %s)",
|
||||||
|
async (userRole, resourcePermission) => {
|
||||||
|
const { permissionResource, triggerResouce } = await getResources()
|
||||||
|
await config.api.permission.add({
|
||||||
|
level: PermissionLevel.READ,
|
||||||
|
resourceId: permissionResource,
|
||||||
|
roleId: resourcePermission,
|
||||||
|
})
|
||||||
|
|
||||||
|
const normalUser = await createUser(userRole)
|
||||||
|
|
||||||
|
await config.withUser(normalUser, async () => {
|
||||||
|
await config.publish()
|
||||||
|
await config.api.rowAction.trigger(
|
||||||
|
triggerResouce,
|
||||||
|
rowAction.id,
|
||||||
|
{ rowId },
|
||||||
|
{
|
||||||
|
status: 403,
|
||||||
|
body: { message: "User does not have permission" },
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const automationLogs = await getAutomationLogs()
|
||||||
|
expect(automationLogs).toBeEmpty()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it.each(allowedRoleConfig)(
|
||||||
|
"does not allow running row actions for tables by default even",
|
||||||
|
async (userRole, resourcePermission) => {
|
||||||
|
await config.api.permission.add({
|
||||||
|
level: PermissionLevel.READ,
|
||||||
|
resourceId: tableId,
|
||||||
|
roleId: resourcePermission,
|
||||||
|
})
|
||||||
|
|
||||||
|
const normalUser = await createUser(userRole)
|
||||||
|
|
||||||
|
await config.withUser(normalUser, async () => {
|
||||||
|
await config.publish()
|
||||||
|
await config.api.rowAction.trigger(
|
||||||
|
tableId,
|
||||||
|
rowAction.id,
|
||||||
|
{ rowId },
|
||||||
|
{
|
||||||
|
status: 403,
|
||||||
|
body: {
|
||||||
|
message: `Row action '${rowAction.id}' is not enabled for table '${tableId}'`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const automationLogs = await getAutomationLogs()
|
||||||
|
expect(automationLogs).toBeEmpty()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -18,6 +18,10 @@ import {
|
||||||
ViewV2,
|
ViewV2,
|
||||||
SearchResponse,
|
SearchResponse,
|
||||||
BasicOperator,
|
BasicOperator,
|
||||||
|
RelationshipType,
|
||||||
|
TableSchema,
|
||||||
|
ViewFieldMetadata,
|
||||||
|
RenameColumn,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { generator, mocks } from "@budibase/backend-core/tests"
|
import { generator, mocks } from "@budibase/backend-core/tests"
|
||||||
import { DatabaseName, getDatasource } from "../../../integrations/tests/utils"
|
import { DatabaseName, getDatasource } from "../../../integrations/tests/utils"
|
||||||
|
@ -1177,6 +1181,263 @@ describe.each([
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("foreign relationship columns", () => {
|
||||||
|
const createAuxTable = () =>
|
||||||
|
config.api.table.save(
|
||||||
|
saveTableRequest({
|
||||||
|
primaryDisplay: "name",
|
||||||
|
schema: {
|
||||||
|
name: { name: "name", type: FieldType.STRING },
|
||||||
|
age: { name: "age", type: FieldType.NUMBER },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
const createMainTable = async (
|
||||||
|
links: {
|
||||||
|
name: string
|
||||||
|
tableId: string
|
||||||
|
fk: string
|
||||||
|
}[]
|
||||||
|
) => {
|
||||||
|
const table = await config.api.table.save(
|
||||||
|
saveTableRequest({
|
||||||
|
schema: {},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
await config.api.table.save({
|
||||||
|
...table,
|
||||||
|
schema: {
|
||||||
|
...table.schema,
|
||||||
|
...links.reduce<TableSchema>((acc, c) => {
|
||||||
|
acc[c.name] = {
|
||||||
|
name: c.name,
|
||||||
|
relationshipType: RelationshipType.ONE_TO_MANY,
|
||||||
|
type: FieldType.LINK,
|
||||||
|
tableId: c.tableId,
|
||||||
|
fieldName: c.fk,
|
||||||
|
constraints: { type: "array" },
|
||||||
|
}
|
||||||
|
return acc
|
||||||
|
}, {}),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return table
|
||||||
|
}
|
||||||
|
|
||||||
|
const createView = async (
|
||||||
|
tableId: string,
|
||||||
|
schema: Record<string, ViewFieldMetadata>
|
||||||
|
) =>
|
||||||
|
await config.api.viewV2.create({
|
||||||
|
name: generator.guid(),
|
||||||
|
tableId,
|
||||||
|
schema,
|
||||||
|
})
|
||||||
|
|
||||||
|
const renameColumn = async (table: Table, renaming: RenameColumn) => {
|
||||||
|
const newSchema = { ...table.schema }
|
||||||
|
newSchema[renaming.updated] = {
|
||||||
|
...table.schema[renaming.old],
|
||||||
|
name: renaming.updated,
|
||||||
|
}
|
||||||
|
delete newSchema[renaming.old]
|
||||||
|
|
||||||
|
await config.api.table.save({
|
||||||
|
...table,
|
||||||
|
schema: newSchema,
|
||||||
|
_rename: renaming,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
it("updating a column will update link columns configuration", async () => {
|
||||||
|
let auxTable = await createAuxTable()
|
||||||
|
|
||||||
|
const table = await createMainTable([
|
||||||
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux" },
|
||||||
|
])
|
||||||
|
// Refetch auxTable
|
||||||
|
auxTable = await config.api.table.get(auxTable._id!)
|
||||||
|
|
||||||
|
const view = await createView(table._id!, {
|
||||||
|
aux: {
|
||||||
|
visible: true,
|
||||||
|
columns: {
|
||||||
|
name: { visible: true, readonly: true },
|
||||||
|
age: { visible: true, readonly: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await renameColumn(auxTable, { old: "age", updated: "dob" })
|
||||||
|
|
||||||
|
const updatedView = await config.api.viewV2.get(view.id)
|
||||||
|
expect(updatedView).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
schema: expect.objectContaining({
|
||||||
|
aux: expect.objectContaining({
|
||||||
|
columns: {
|
||||||
|
id: { visible: false, readonly: false },
|
||||||
|
name: { visible: true, readonly: true },
|
||||||
|
dob: { visible: true, readonly: true },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("handles multiple fields using the same table", async () => {
|
||||||
|
let auxTable = await createAuxTable()
|
||||||
|
|
||||||
|
const table = await createMainTable([
|
||||||
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux" },
|
||||||
|
{ name: "aux2", tableId: auxTable._id!, fk: "fk_aux2" },
|
||||||
|
])
|
||||||
|
// Refetch auxTable
|
||||||
|
auxTable = await config.api.table.get(auxTable._id!)
|
||||||
|
|
||||||
|
const view = await createView(table._id!, {
|
||||||
|
aux: {
|
||||||
|
visible: true,
|
||||||
|
columns: {
|
||||||
|
name: { visible: true, readonly: true },
|
||||||
|
age: { visible: true, readonly: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
aux2: {
|
||||||
|
visible: true,
|
||||||
|
columns: {
|
||||||
|
name: { visible: true, readonly: true },
|
||||||
|
age: { visible: true, readonly: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await renameColumn(auxTable, { old: "age", updated: "dob" })
|
||||||
|
|
||||||
|
const updatedView = await config.api.viewV2.get(view.id)
|
||||||
|
expect(updatedView).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
schema: expect.objectContaining({
|
||||||
|
aux: expect.objectContaining({
|
||||||
|
columns: {
|
||||||
|
id: { visible: false, readonly: false },
|
||||||
|
name: { visible: true, readonly: true },
|
||||||
|
dob: { visible: true, readonly: true },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
aux2: expect.objectContaining({
|
||||||
|
columns: {
|
||||||
|
id: { visible: false, readonly: false },
|
||||||
|
name: { visible: true, readonly: true },
|
||||||
|
dob: { visible: true, readonly: true },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("does not rename columns with the same name but from other tables", async () => {
|
||||||
|
let auxTable = await createAuxTable()
|
||||||
|
let aux2Table = await createAuxTable()
|
||||||
|
|
||||||
|
const table = await createMainTable([
|
||||||
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux" },
|
||||||
|
{ name: "aux2", tableId: aux2Table._id!, fk: "fk_aux2" },
|
||||||
|
])
|
||||||
|
|
||||||
|
// Refetch auxTable
|
||||||
|
auxTable = await config.api.table.get(auxTable._id!)
|
||||||
|
|
||||||
|
const view = await createView(table._id!, {
|
||||||
|
aux: {
|
||||||
|
visible: true,
|
||||||
|
columns: {
|
||||||
|
name: { visible: true, readonly: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
aux2: {
|
||||||
|
visible: true,
|
||||||
|
columns: {
|
||||||
|
name: { visible: true, readonly: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await renameColumn(auxTable, { old: "name", updated: "fullName" })
|
||||||
|
|
||||||
|
const updatedView = await config.api.viewV2.get(view.id)
|
||||||
|
expect(updatedView).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
schema: expect.objectContaining({
|
||||||
|
aux: expect.objectContaining({
|
||||||
|
columns: {
|
||||||
|
id: { visible: false, readonly: false },
|
||||||
|
fullName: { visible: true, readonly: true },
|
||||||
|
age: { visible: false, readonly: false },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
aux2: expect.objectContaining({
|
||||||
|
columns: {
|
||||||
|
id: { visible: false, readonly: false },
|
||||||
|
name: { visible: true, readonly: true },
|
||||||
|
age: { visible: false, readonly: false },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("updates all views references", async () => {
|
||||||
|
let auxTable = await createAuxTable()
|
||||||
|
|
||||||
|
const table1 = await createMainTable([
|
||||||
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux_table1" },
|
||||||
|
])
|
||||||
|
const table2 = await createMainTable([
|
||||||
|
{ name: "aux", tableId: auxTable._id!, fk: "fk_aux_table2" },
|
||||||
|
])
|
||||||
|
|
||||||
|
// Refetch auxTable
|
||||||
|
auxTable = await config.api.table.get(auxTable._id!)
|
||||||
|
|
||||||
|
const viewSchema = {
|
||||||
|
aux: {
|
||||||
|
visible: true,
|
||||||
|
columns: {
|
||||||
|
name: { visible: true, readonly: true },
|
||||||
|
age: { visible: true, readonly: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
const view1 = await createView(table1._id!, viewSchema)
|
||||||
|
const view2 = await createView(table1._id!, viewSchema)
|
||||||
|
const view3 = await createView(table2._id!, viewSchema)
|
||||||
|
|
||||||
|
await renameColumn(auxTable, { old: "age", updated: "dob" })
|
||||||
|
|
||||||
|
for (const view of [view1, view2, view3]) {
|
||||||
|
const updatedView = await config.api.viewV2.get(view.id)
|
||||||
|
expect(updatedView).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
schema: expect.objectContaining({
|
||||||
|
aux: expect.objectContaining({
|
||||||
|
columns: {
|
||||||
|
id: { visible: false, readonly: false },
|
||||||
|
name: { visible: true, readonly: true },
|
||||||
|
dob: { visible: true, readonly: true },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
import { auth, permissions } from "@budibase/backend-core"
|
import { auth, permissions } from "@budibase/backend-core"
|
||||||
import { DataSourceOperation } from "../../../constants"
|
import { DataSourceOperation } from "../../../constants"
|
||||||
import {
|
import {
|
||||||
|
AutomationActionStepId,
|
||||||
|
AutomationStep,
|
||||||
|
AutomationStepType,
|
||||||
EmptyFilterOption,
|
EmptyFilterOption,
|
||||||
SearchFilters,
|
SearchFilters,
|
||||||
Table,
|
Table,
|
||||||
|
@ -88,7 +91,8 @@ export function datasourceValidator() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function filterObject() {
|
function filterObject(opts?: { unknown: boolean }) {
|
||||||
|
const { unknown = true } = opts || {}
|
||||||
const conditionalFilteringObject = () =>
|
const conditionalFilteringObject = () =>
|
||||||
Joi.object({
|
Joi.object({
|
||||||
conditions: Joi.array().items(Joi.link("#schema")).required(),
|
conditions: Joi.array().items(Joi.link("#schema")).required(),
|
||||||
|
@ -115,7 +119,7 @@ function filterObject() {
|
||||||
fuzzyOr: Joi.forbidden(),
|
fuzzyOr: Joi.forbidden(),
|
||||||
documentType: Joi.forbidden(),
|
documentType: Joi.forbidden(),
|
||||||
}
|
}
|
||||||
return Joi.object(filtersValidators).unknown(true).id("schema")
|
return Joi.object(filtersValidators).unknown(unknown).id("schema")
|
||||||
}
|
}
|
||||||
|
|
||||||
export function internalSearchValidator() {
|
export function internalSearchValidator() {
|
||||||
|
@ -259,6 +263,11 @@ export function screenValidator() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateStepSchema(allowStepTypes: string[]) {
|
function generateStepSchema(allowStepTypes: string[]) {
|
||||||
|
const branchSchema = Joi.object({
|
||||||
|
name: Joi.string().required(),
|
||||||
|
condition: filterObject({ unknown: false }).required().min(1),
|
||||||
|
})
|
||||||
|
|
||||||
return Joi.object({
|
return Joi.object({
|
||||||
stepId: Joi.string().required(),
|
stepId: Joi.string().required(),
|
||||||
id: Joi.string().required(),
|
id: Joi.string().required(),
|
||||||
|
@ -267,11 +276,35 @@ function generateStepSchema(allowStepTypes: string[]) {
|
||||||
tagline: Joi.string().required(),
|
tagline: Joi.string().required(),
|
||||||
icon: Joi.string().required(),
|
icon: Joi.string().required(),
|
||||||
params: Joi.object(),
|
params: Joi.object(),
|
||||||
|
inputs: Joi.when("stepId", {
|
||||||
|
is: AutomationActionStepId.BRANCH,
|
||||||
|
then: Joi.object({
|
||||||
|
branches: Joi.array().items(branchSchema).min(1).required(),
|
||||||
|
children: Joi.object()
|
||||||
|
.pattern(Joi.string(), Joi.array().items(Joi.link("#step")))
|
||||||
|
.required(),
|
||||||
|
}).required(),
|
||||||
|
otherwise: Joi.object(),
|
||||||
|
}),
|
||||||
|
|
||||||
args: Joi.object(),
|
args: Joi.object(),
|
||||||
type: Joi.string()
|
type: Joi.string()
|
||||||
.required()
|
.required()
|
||||||
.valid(...allowStepTypes),
|
.valid(...allowStepTypes),
|
||||||
}).unknown(true)
|
})
|
||||||
|
.unknown(true)
|
||||||
|
.id("step")
|
||||||
|
}
|
||||||
|
|
||||||
|
const validateStepsArray = (
|
||||||
|
steps: AutomationStep[],
|
||||||
|
helpers: Joi.CustomHelpers
|
||||||
|
) => {
|
||||||
|
for (const step of steps.slice(0, -1)) {
|
||||||
|
if (step.stepId === AutomationActionStepId.BRANCH) {
|
||||||
|
return helpers.error("branchStepPosition")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function automationValidator(existing = false) {
|
export function automationValidator(existing = false) {
|
||||||
|
@ -284,9 +317,20 @@ export function automationValidator(existing = false) {
|
||||||
definition: Joi.object({
|
definition: Joi.object({
|
||||||
steps: Joi.array()
|
steps: Joi.array()
|
||||||
.required()
|
.required()
|
||||||
.items(generateStepSchema(["ACTION", "LOGIC"])),
|
.items(
|
||||||
trigger: generateStepSchema(["TRIGGER"]).allow(null),
|
generateStepSchema([
|
||||||
|
AutomationStepType.ACTION,
|
||||||
|
AutomationStepType.LOGIC,
|
||||||
|
])
|
||||||
|
)
|
||||||
|
.custom(validateStepsArray)
|
||||||
|
.messages({
|
||||||
|
branchStepPosition:
|
||||||
|
"Branch steps are only allowed as the last step",
|
||||||
|
}),
|
||||||
|
trigger: generateStepSchema([AutomationStepType.TRIGGER]).allow(null),
|
||||||
})
|
})
|
||||||
|
|
||||||
.required()
|
.required()
|
||||||
.unknown(true),
|
.unknown(true),
|
||||||
}).unknown(true)
|
}).unknown(true)
|
||||||
|
|
|
@ -63,8 +63,8 @@ describe("Automation Scenarios", () => {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.run()
|
.run()
|
||||||
|
expect(results.steps[3].outputs.status).toContain("branch1 branch taken")
|
||||||
expect(results.steps[2].outputs.message).toContain("Branch 1.1")
|
expect(results.steps[4].outputs.message).toContain("Branch 1.1")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should execute correct branch based on string equality", async () => {
|
it("should execute correct branch based on string equality", async () => {
|
||||||
|
@ -91,8 +91,10 @@ describe("Automation Scenarios", () => {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.run()
|
.run()
|
||||||
|
expect(results.steps[0].outputs.status).toContain(
|
||||||
expect(results.steps[0].outputs.message).toContain("Active user")
|
"activeBranch branch taken"
|
||||||
|
)
|
||||||
|
expect(results.steps[1].outputs.message).toContain("Active user")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should handle multiple conditions with AND operator", async () => {
|
it("should handle multiple conditions with AND operator", async () => {
|
||||||
|
@ -124,7 +126,7 @@ describe("Automation Scenarios", () => {
|
||||||
})
|
})
|
||||||
.run()
|
.run()
|
||||||
|
|
||||||
expect(results.steps[0].outputs.message).toContain("Active admin user")
|
expect(results.steps[1].outputs.message).toContain("Active admin user")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should handle multiple conditions with OR operator", async () => {
|
it("should handle multiple conditions with OR operator", async () => {
|
||||||
|
@ -162,7 +164,7 @@ describe("Automation Scenarios", () => {
|
||||||
})
|
})
|
||||||
.run()
|
.run()
|
||||||
|
|
||||||
expect(results.steps[0].outputs.message).toContain("Special user")
|
expect(results.steps[1].outputs.message).toContain("Special user")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -362,6 +364,32 @@ describe("Automation Scenarios", () => {
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should run an automation where a loop is used twice to ensure context correctness further down the tree", async () => {
|
||||||
|
const builder = createAutomationBuilder({
|
||||||
|
name: "Test Trigger with Loop and Create Row",
|
||||||
|
})
|
||||||
|
|
||||||
|
const results = await builder
|
||||||
|
.appAction({ fields: {} })
|
||||||
|
.loop({
|
||||||
|
option: LoopStepType.ARRAY,
|
||||||
|
binding: [1, 2, 3],
|
||||||
|
})
|
||||||
|
.serverLog({ text: "Message {{loop.currentItem}}" })
|
||||||
|
.serverLog({ text: "{{steps.1.iterations}}" })
|
||||||
|
.loop({
|
||||||
|
option: LoopStepType.ARRAY,
|
||||||
|
binding: [1, 2, 3],
|
||||||
|
})
|
||||||
|
.serverLog({ text: "{{loop.currentItem}}" })
|
||||||
|
.serverLog({ text: "{{steps.3.iterations}}" })
|
||||||
|
.run()
|
||||||
|
|
||||||
|
// We want to ensure that bindings are corr
|
||||||
|
expect(results.steps[1].outputs.message).toContain("- 3")
|
||||||
|
expect(results.steps[3].outputs.message).toContain("- 3")
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("Row Automations", () => {
|
describe("Row Automations", () => {
|
||||||
|
|
|
@ -179,7 +179,7 @@ class AutomationBuilder extends BaseStepBuilder {
|
||||||
private triggerOutputs: any
|
private triggerOutputs: any
|
||||||
private triggerSet: boolean = false
|
private triggerSet: boolean = false
|
||||||
|
|
||||||
constructor(options: { name?: string } = {}) {
|
constructor(options: { name?: string; appId?: string } = {}) {
|
||||||
super()
|
super()
|
||||||
this.automationConfig = {
|
this.automationConfig = {
|
||||||
name: options.name || `Test Automation ${uuidv4()}`,
|
name: options.name || `Test Automation ${uuidv4()}`,
|
||||||
|
@ -188,7 +188,7 @@ class AutomationBuilder extends BaseStepBuilder {
|
||||||
trigger: {} as AutomationTrigger,
|
trigger: {} as AutomationTrigger,
|
||||||
},
|
},
|
||||||
type: "automation",
|
type: "automation",
|
||||||
appId: setup.getConfig().getAppId(),
|
appId: options.appId ?? setup.getConfig().getAppId(),
|
||||||
}
|
}
|
||||||
this.config = setup.getConfig()
|
this.config = setup.getConfig()
|
||||||
}
|
}
|
||||||
|
@ -261,13 +261,14 @@ class AutomationBuilder extends BaseStepBuilder {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
branch(branchConfig: BranchConfig): {
|
branch(branchConfig: BranchConfig): this {
|
||||||
run: () => Promise<AutomationResults>
|
|
||||||
} {
|
|
||||||
this.addBranchStep(branchConfig)
|
this.addBranchStep(branchConfig)
|
||||||
return {
|
return this
|
||||||
run: () => this.run(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
build(): Automation {
|
||||||
|
this.automationConfig.definition.steps = this.steps
|
||||||
|
return this.automationConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
async run() {
|
async run() {
|
||||||
|
@ -275,7 +276,7 @@ class AutomationBuilder extends BaseStepBuilder {
|
||||||
throw new Error("Please add a trigger to this automation test")
|
throw new Error("Please add a trigger to this automation test")
|
||||||
}
|
}
|
||||||
this.automationConfig.definition.steps = this.steps
|
this.automationConfig.definition.steps = this.steps
|
||||||
const automation = await this.config.createAutomation(this.automationConfig)
|
const automation = await this.config.createAutomation(this.build())
|
||||||
const results = await testAutomation(
|
const results = await testAutomation(
|
||||||
this.config,
|
this.config,
|
||||||
automation,
|
automation,
|
||||||
|
@ -295,6 +296,9 @@ class AutomationBuilder extends BaseStepBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createAutomationBuilder(options?: { name?: string }) {
|
export function createAutomationBuilder(options?: {
|
||||||
|
name?: string
|
||||||
|
appId?: string
|
||||||
|
}) {
|
||||||
return new AutomationBuilder(options)
|
return new AutomationBuilder(options)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,14 +11,17 @@ import { USER_METDATA_PREFIX } from "../utils"
|
||||||
import partition from "lodash/partition"
|
import partition from "lodash/partition"
|
||||||
import { getGlobalUsersFromMetadata } from "../../utilities/global"
|
import { getGlobalUsersFromMetadata } from "../../utilities/global"
|
||||||
import { processFormulas } from "../../utilities/rowProcessor"
|
import { processFormulas } from "../../utilities/rowProcessor"
|
||||||
import { context } from "@budibase/backend-core"
|
import { context, features } from "@budibase/backend-core"
|
||||||
import {
|
import {
|
||||||
ContextUser,
|
ContextUser,
|
||||||
|
FeatureFlag,
|
||||||
FieldType,
|
FieldType,
|
||||||
LinkDocumentValue,
|
LinkDocumentValue,
|
||||||
Row,
|
Row,
|
||||||
Table,
|
Table,
|
||||||
TableSchema,
|
TableSchema,
|
||||||
|
ViewFieldMetadata,
|
||||||
|
ViewV2,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import sdk from "../../sdk"
|
import sdk from "../../sdk"
|
||||||
|
|
||||||
|
@ -241,34 +244,78 @@ function getPrimaryDisplayValue(row: Row, table?: Table) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type SquashTableFields = Record<string, { visibleFieldNames: string[] }>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function will take the given enriched rows and squash the links to only contain the primary display field.
|
* This function will take the given enriched rows and squash the links to only contain the primary display field.
|
||||||
* @param table The table from which the rows originated.
|
* @param table The table from which the rows originated.
|
||||||
* @param enriched The pre-enriched rows (full docs) which are to be squashed.
|
* @param enriched The pre-enriched rows (full docs) which are to be squashed.
|
||||||
|
* @param squashFields Per link column (key) define which columns are allowed while squashing.
|
||||||
* @returns The rows after having their links squashed to only contain the ID and primary display.
|
* @returns The rows after having their links squashed to only contain the ID and primary display.
|
||||||
*/
|
*/
|
||||||
export async function squashLinksToPrimaryDisplay(
|
export async function squashLinks<T = Row[] | Row>(
|
||||||
table: Table,
|
table: Table,
|
||||||
enriched: Row[] | Row
|
enriched: T,
|
||||||
) {
|
options?: {
|
||||||
|
fromViewId?: string
|
||||||
|
}
|
||||||
|
): Promise<T> {
|
||||||
|
const allowRelationshipSchemas = await features.flags.isEnabled(
|
||||||
|
FeatureFlag.ENRICHED_RELATIONSHIPS
|
||||||
|
)
|
||||||
|
|
||||||
|
let viewSchema: Record<string, ViewFieldMetadata> = {}
|
||||||
|
if (options?.fromViewId && allowRelationshipSchemas) {
|
||||||
|
const view = Object.values(table.views || {}).find(
|
||||||
|
(v): v is ViewV2 => sdk.views.isV2(v) && v.id === options?.fromViewId
|
||||||
|
)
|
||||||
|
viewSchema = view?.schema || {}
|
||||||
|
}
|
||||||
|
|
||||||
// will populate this as we find them
|
// will populate this as we find them
|
||||||
const linkedTables = [table]
|
const linkedTables = [table]
|
||||||
const isArray = Array.isArray(enriched)
|
const isArray = Array.isArray(enriched)
|
||||||
let enrichedArray = !isArray ? [enriched] : enriched
|
const enrichedArray = !isArray ? [enriched] : enriched
|
||||||
for (let row of enrichedArray) {
|
for (const row of enrichedArray) {
|
||||||
// this only fetches the table if its not already in array
|
// this only fetches the table if its not already in array
|
||||||
const rowTable = await getLinkedTable(row.tableId!, linkedTables)
|
const rowTable = await getLinkedTable(row.tableId!, linkedTables)
|
||||||
for (let [column, schema] of Object.entries(rowTable?.schema || {})) {
|
for (let [column, schema] of Object.entries(rowTable.schema)) {
|
||||||
if (schema.type !== FieldType.LINK || !Array.isArray(row[column])) {
|
if (schema.type !== FieldType.LINK || !Array.isArray(row[column])) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
const newLinks = []
|
const newLinks = []
|
||||||
for (let link of row[column]) {
|
for (const link of row[column]) {
|
||||||
const linkTblId =
|
const linkTblId =
|
||||||
link.tableId || getRelatedTableForField(table.schema, column)
|
link.tableId || getRelatedTableForField(table.schema, column)
|
||||||
const linkedTable = await getLinkedTable(linkTblId!, linkedTables)
|
const linkedTable = await getLinkedTable(linkTblId!, linkedTables)
|
||||||
const obj: any = { _id: link._id }
|
const obj: any = { _id: link._id }
|
||||||
obj.primaryDisplay = getPrimaryDisplayValue(link, linkedTable)
|
obj.primaryDisplay = getPrimaryDisplayValue(link, linkedTable)
|
||||||
|
|
||||||
|
if (viewSchema[column]?.columns) {
|
||||||
|
const squashFields = Object.entries(viewSchema[column].columns)
|
||||||
|
.filter(([columnName, viewColumnConfig]) => {
|
||||||
|
const tableColumn = linkedTable.schema[columnName]
|
||||||
|
if (!tableColumn) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
[FieldType.LINK, FieldType.FORMULA].includes(tableColumn.type)
|
||||||
|
) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
tableColumn.visible !== false &&
|
||||||
|
viewColumnConfig.visible !== false
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
.map(([columnName]) => columnName)
|
||||||
|
|
||||||
|
for (const relField of squashFields) {
|
||||||
|
obj[relField] = link[relField]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
newLinks.push(obj)
|
newLinks.push(obj)
|
||||||
}
|
}
|
||||||
row[column] = newLinks
|
row[column] = newLinks
|
||||||
|
|
|
@ -88,7 +88,7 @@ const authorized =
|
||||||
opts = { schema: false },
|
opts = { schema: false },
|
||||||
resourcePath?: string
|
resourcePath?: string
|
||||||
) =>
|
) =>
|
||||||
async (ctx: any, next: any) => {
|
async (ctx: UserCtx, next: any) => {
|
||||||
// webhooks don't need authentication, each webhook unique
|
// webhooks don't need authentication, each webhook unique
|
||||||
// also internal requests (between services) don't need authorized
|
// also internal requests (between services) don't need authorized
|
||||||
if (isWebhookEndpoint(ctx) || ctx.internal) {
|
if (isWebhookEndpoint(ctx) || ctx.internal) {
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
import { Next } from "koa"
|
import { Next } from "koa"
|
||||||
import { Ctx } from "@budibase/types"
|
import { PermissionLevel, PermissionType, UserCtx } from "@budibase/types"
|
||||||
import { paramSubResource } from "./resourceId"
|
|
||||||
import { docIds } from "@budibase/backend-core"
|
import { docIds } from "@budibase/backend-core"
|
||||||
import * as utils from "../db/utils"
|
import * as utils from "../db/utils"
|
||||||
import sdk from "../sdk"
|
import sdk from "../sdk"
|
||||||
|
import { authorizedResource } from "./authorized"
|
||||||
|
|
||||||
export function triggerRowActionAuthorised(
|
export function triggerRowActionAuthorised(
|
||||||
sourcePath: string,
|
sourcePath: string,
|
||||||
actionPath: string
|
actionPath: string
|
||||||
) {
|
) {
|
||||||
return async (ctx: Ctx, next: Next) => {
|
return async (ctx: UserCtx, next: Next) => {
|
||||||
// Reusing the existing middleware to extract the value
|
async function getResourceIds() {
|
||||||
paramSubResource(sourcePath, actionPath)(ctx, () => {})
|
const sourceId: string = ctx.params[sourcePath]
|
||||||
const { resourceId: sourceId, subResourceId: rowActionId } = ctx
|
const rowActionId: string = ctx.params[actionPath]
|
||||||
|
|
||||||
const isTableId = docIds.isTableId(sourceId)
|
const isTableId = docIds.isTableId(sourceId)
|
||||||
const isViewId = utils.isViewID(sourceId)
|
const isViewId = utils.isViewID(sourceId)
|
||||||
|
@ -23,18 +23,30 @@ export function triggerRowActionAuthorised(
|
||||||
const tableId = isTableId
|
const tableId = isTableId
|
||||||
? sourceId
|
? sourceId
|
||||||
: utils.extractViewInfoFromID(sourceId).tableId
|
: utils.extractViewInfoFromID(sourceId).tableId
|
||||||
|
const viewId = isTableId ? undefined : sourceId
|
||||||
|
return { tableId, viewId, rowActionId }
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tableId, viewId, rowActionId } = await getResourceIds()
|
||||||
|
|
||||||
|
// Check if the user has permissions to the table/view
|
||||||
|
await authorizedResource(
|
||||||
|
!viewId ? PermissionType.TABLE : PermissionType.VIEW,
|
||||||
|
PermissionLevel.READ,
|
||||||
|
sourcePath
|
||||||
|
)(ctx, () => {})
|
||||||
|
|
||||||
|
// Check is the row action can run for the given view/table
|
||||||
const rowAction = await sdk.rowActions.get(tableId, rowActionId)
|
const rowAction = await sdk.rowActions.get(tableId, rowActionId)
|
||||||
|
if (!viewId && !rowAction.permissions.table.runAllowed) {
|
||||||
if (isTableId && !rowAction.permissions.table.runAllowed) {
|
|
||||||
ctx.throw(
|
ctx.throw(
|
||||||
403,
|
403,
|
||||||
`Row action '${rowActionId}' is not enabled for table '${sourceId}'`
|
`Row action '${rowActionId}' is not enabled for table '${tableId}'`
|
||||||
)
|
)
|
||||||
} else if (isViewId && !rowAction.permissions.views[sourceId]?.runAllowed) {
|
} else if (viewId && !rowAction.permissions.views[viewId]?.runAllowed) {
|
||||||
ctx.throw(
|
ctx.throw(
|
||||||
403,
|
403,
|
||||||
`Row action '${rowActionId}' is not enabled for view '${sourceId}'`
|
`Row action '${rowActionId}' is not enabled for view '${viewId}'`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ export default async (ctx: Ctx<Row, Row>, next: Next) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// have to mutate the koa context, can't return
|
// have to mutate the koa context, can't return
|
||||||
export async function trimNonViewFields(
|
async function trimNonViewFields(
|
||||||
row: Row,
|
row: Row,
|
||||||
view: ViewV2,
|
view: ViewV2,
|
||||||
permission: "WRITE" | "READ"
|
permission: "WRITE" | "READ"
|
||||||
|
|
|
@ -75,7 +75,7 @@ export async function create(tableId: string, rowAction: { name: string }) {
|
||||||
name: action.name,
|
name: action.name,
|
||||||
automationId: automation._id!,
|
automationId: automation._id!,
|
||||||
permissions: {
|
permissions: {
|
||||||
table: { runAllowed: true },
|
table: { runAllowed: false },
|
||||||
views: {},
|
views: {},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -216,11 +216,15 @@ export async function run(tableId: any, rowActionId: any, rowId: string) {
|
||||||
const automation = await sdk.automations.get(rowAction.automationId)
|
const automation = await sdk.automations.get(rowAction.automationId)
|
||||||
|
|
||||||
const row = await sdk.rows.find(tableId, rowId)
|
const row = await sdk.rows.find(tableId, rowId)
|
||||||
await triggers.externalTrigger(automation, {
|
await triggers.externalTrigger(
|
||||||
|
automation,
|
||||||
|
{
|
||||||
fields: {
|
fields: {
|
||||||
row,
|
row,
|
||||||
table,
|
table,
|
||||||
},
|
},
|
||||||
appId: context.getAppId(),
|
appId: context.getAppId(),
|
||||||
})
|
},
|
||||||
|
{ getResponses: true }
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import {
|
||||||
} from "../../../utilities/rowProcessor"
|
} from "../../../utilities/rowProcessor"
|
||||||
import cloneDeep from "lodash/fp/cloneDeep"
|
import cloneDeep from "lodash/fp/cloneDeep"
|
||||||
import isEqual from "lodash/fp/isEqual"
|
import isEqual from "lodash/fp/isEqual"
|
||||||
|
import { tryExtractingTableAndViewId } from "./utils"
|
||||||
|
|
||||||
export async function getRow(
|
export async function getRow(
|
||||||
tableId: string,
|
tableId: string,
|
||||||
|
@ -26,10 +27,11 @@ export async function getRow(
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function save(
|
export async function save(
|
||||||
tableId: string,
|
tableOrViewId: string,
|
||||||
inputs: Row,
|
inputs: Row,
|
||||||
userId: string | undefined
|
userId: string | undefined
|
||||||
) {
|
) {
|
||||||
|
const { tableId, viewId } = tryExtractingTableAndViewId(tableOrViewId)
|
||||||
const table = await sdk.tables.getTable(tableId)
|
const table = await sdk.tables.getTable(tableId)
|
||||||
const { table: updatedTable, row } = await inputProcessing(
|
const { table: updatedTable, row } = await inputProcessing(
|
||||||
userId,
|
userId,
|
||||||
|
@ -63,6 +65,7 @@ export async function save(
|
||||||
row: await outputProcessing(table, row, {
|
row: await outputProcessing(table, row, {
|
||||||
preserveLinks: true,
|
preserveLinks: true,
|
||||||
squash: true,
|
squash: true,
|
||||||
|
fromViewId: viewId,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -70,7 +73,9 @@ export async function save(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function find(tableId: string, rowId: string): Promise<Row> {
|
export async function find(tableOrViewId: string, rowId: string): Promise<Row> {
|
||||||
|
const { tableId, viewId } = tryExtractingTableAndViewId(tableOrViewId)
|
||||||
|
|
||||||
const row = await getRow(tableId, rowId, {
|
const row = await getRow(tableId, rowId, {
|
||||||
relationships: true,
|
relationships: true,
|
||||||
})
|
})
|
||||||
|
@ -84,5 +89,6 @@ export async function find(tableId: string, rowId: string): Promise<Row> {
|
||||||
return await outputProcessing(table, row, {
|
return await outputProcessing(table, row, {
|
||||||
squash: true,
|
squash: true,
|
||||||
preserveLinks: true,
|
preserveLinks: true,
|
||||||
|
fromViewId: viewId,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,12 +10,14 @@ import {
|
||||||
import * as linkRows from "../../../db/linkedRows"
|
import * as linkRows from "../../../db/linkedRows"
|
||||||
import { InternalTables } from "../../../db/utils"
|
import { InternalTables } from "../../../db/utils"
|
||||||
import { getFullUser } from "../../../utilities/users"
|
import { getFullUser } from "../../../utilities/users"
|
||||||
|
import { tryExtractingTableAndViewId } from "./utils"
|
||||||
|
|
||||||
export async function save(
|
export async function save(
|
||||||
tableId: string,
|
tableOrViewId: string,
|
||||||
inputs: Row,
|
inputs: Row,
|
||||||
userId: string | undefined
|
userId: string | undefined
|
||||||
) {
|
) {
|
||||||
|
const { tableId, viewId } = tryExtractingTableAndViewId(tableOrViewId)
|
||||||
inputs.tableId = tableId
|
inputs.tableId = tableId
|
||||||
|
|
||||||
if (!inputs._rev && !inputs._id) {
|
if (!inputs._rev && !inputs._id) {
|
||||||
|
@ -50,14 +52,17 @@ export async function save(
|
||||||
return finaliseRow(table, row, {
|
return finaliseRow(table, row, {
|
||||||
oldTable: dbTable,
|
oldTable: dbTable,
|
||||||
updateFormula: true,
|
updateFormula: true,
|
||||||
|
fromViewId: viewId,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function find(tableId: string, rowId: string): Promise<Row> {
|
export async function find(tableOrViewId: string, rowId: string): Promise<Row> {
|
||||||
|
const { tableId, viewId } = tryExtractingTableAndViewId(tableOrViewId)
|
||||||
|
|
||||||
const table = await sdk.tables.getTable(tableId)
|
const table = await sdk.tables.getTable(tableId)
|
||||||
let row = await findRow(tableId, rowId)
|
let row = await findRow(tableId, rowId)
|
||||||
|
|
||||||
row = await outputProcessing(table, row)
|
row = await outputProcessing(table, row, { squash: true, fromViewId: viewId })
|
||||||
return row
|
return row
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
import { db as dbCore, context } from "@budibase/backend-core"
|
import { db as dbCore, context } from "@budibase/backend-core"
|
||||||
import { Database, Row } from "@budibase/types"
|
import { Database, Row } from "@budibase/types"
|
||||||
import { getRowParams } from "../../../db/utils"
|
import {
|
||||||
|
extractViewInfoFromID,
|
||||||
|
getRowParams,
|
||||||
|
isViewID,
|
||||||
|
} from "../../../db/utils"
|
||||||
import { isExternalTableID } from "../../../integrations/utils"
|
import { isExternalTableID } from "../../../integrations/utils"
|
||||||
import * as internal from "./internal"
|
import * as internal from "./internal"
|
||||||
import * as external from "./external"
|
import * as external from "./external"
|
||||||
|
@ -20,7 +24,12 @@ export async function getAllInternalRows(appId?: string) {
|
||||||
return response.rows.map(row => row.doc) as Row[]
|
return response.rows.map(row => row.doc) as Row[]
|
||||||
}
|
}
|
||||||
|
|
||||||
function pickApi(tableId: any) {
|
function pickApi(tableOrViewId: string) {
|
||||||
|
let tableId = tableOrViewId
|
||||||
|
if (isViewID(tableOrViewId)) {
|
||||||
|
tableId = extractViewInfoFromID(tableOrViewId).tableId
|
||||||
|
}
|
||||||
|
|
||||||
if (isExternalTableID(tableId)) {
|
if (isExternalTableID(tableId)) {
|
||||||
return external
|
return external
|
||||||
}
|
}
|
||||||
|
@ -28,13 +37,13 @@ function pickApi(tableId: any) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function save(
|
export async function save(
|
||||||
tableId: string,
|
tableOrViewId: string,
|
||||||
row: Row,
|
row: Row,
|
||||||
userId: string | undefined
|
userId: string | undefined
|
||||||
) {
|
) {
|
||||||
return pickApi(tableId).save(tableId, row, userId)
|
return pickApi(tableOrViewId).save(tableOrViewId, row, userId)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function find(tableId: string, rowId: string) {
|
export async function find(tableOrViewId: string, rowId: string) {
|
||||||
return pickApi(tableId).find(tableId, rowId)
|
return pickApi(tableOrViewId).find(tableOrViewId, rowId)
|
||||||
}
|
}
|
||||||
|
|
|
@ -112,9 +112,10 @@ export async function search(
|
||||||
: Promise.resolve(undefined),
|
: Promise.resolve(undefined),
|
||||||
])
|
])
|
||||||
|
|
||||||
let processed = await outputProcessing<Row[]>(table, rows, {
|
let processed = await outputProcessing(table, rows, {
|
||||||
preserveLinks: true,
|
preserveLinks: true,
|
||||||
squash: true,
|
squash: true,
|
||||||
|
fromViewId: options.viewId,
|
||||||
})
|
})
|
||||||
|
|
||||||
let hasNextPage = false
|
let hasNextPage = false
|
||||||
|
@ -260,7 +261,7 @@ export async function fetch(tableId: string): Promise<Row[]> {
|
||||||
includeSqlRelationships: IncludeRelationship.INCLUDE,
|
includeSqlRelationships: IncludeRelationship.INCLUDE,
|
||||||
})
|
})
|
||||||
const table = await sdk.tables.getTable(tableId)
|
const table = await sdk.tables.getTable(tableId)
|
||||||
return await outputProcessing<Row[]>(table, response.rows, {
|
return await outputProcessing(table, response.rows, {
|
||||||
preserveLinks: true,
|
preserveLinks: true,
|
||||||
squash: true,
|
squash: true,
|
||||||
})
|
})
|
||||||
|
|
|
@ -61,7 +61,7 @@ export async function exportRows(
|
||||||
})
|
})
|
||||||
).rows.map(row => row.doc!)
|
).rows.map(row => row.doc!)
|
||||||
|
|
||||||
result = await outputProcessing<Row[]>(table, response)
|
result = await outputProcessing(table, response)
|
||||||
} else if (query) {
|
} else if (query) {
|
||||||
let searchResponse = await sdk.rows.search({
|
let searchResponse = await sdk.rows.search({
|
||||||
tableId,
|
tableId,
|
||||||
|
|
|
@ -59,7 +59,10 @@ export async function search(
|
||||||
response.rows = response.rows.map((r: any) => pick(r, fields))
|
response.rows = response.rows.map((r: any) => pick(r, fields))
|
||||||
}
|
}
|
||||||
|
|
||||||
response.rows = await outputProcessing(table, response.rows)
|
response.rows = await outputProcessing(table, response.rows, {
|
||||||
|
squash: true,
|
||||||
|
fromViewId: options.viewId,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
|
@ -390,9 +390,10 @@ export async function search(
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the rows
|
// get the rows
|
||||||
let finalRows = await outputProcessing<Row[]>(table, processed, {
|
let finalRows = await outputProcessing(table, processed, {
|
||||||
preserveLinks: true,
|
preserveLinks: true,
|
||||||
squash: true,
|
squash: true,
|
||||||
|
fromViewId: options.viewId,
|
||||||
})
|
})
|
||||||
|
|
||||||
// check if we need to pick specific rows out
|
// check if we need to pick specific rows out
|
||||||
|
|
|
@ -17,7 +17,11 @@ import {
|
||||||
import { makeExternalQuery } from "../../../integrations/base/query"
|
import { makeExternalQuery } from "../../../integrations/base/query"
|
||||||
import { Format } from "../../../api/controllers/view/exporters"
|
import { Format } from "../../../api/controllers/view/exporters"
|
||||||
import sdk from "../.."
|
import sdk from "../.."
|
||||||
import { isRelationshipColumn } from "../../../db/utils"
|
import {
|
||||||
|
extractViewInfoFromID,
|
||||||
|
isRelationshipColumn,
|
||||||
|
isViewID,
|
||||||
|
} from "../../../db/utils"
|
||||||
import { isSQL } from "../../../integrations/utils"
|
import { isSQL } from "../../../integrations/utils"
|
||||||
|
|
||||||
const SQL_CLIENT_SOURCE_MAP: Record<SourceName, SqlClient | undefined> = {
|
const SQL_CLIENT_SOURCE_MAP: Record<SourceName, SqlClient | undefined> = {
|
||||||
|
@ -317,3 +321,14 @@ function validateTimeOnlyField(
|
||||||
export function isArrayFilter(operator: any): operator is ArrayOperator {
|
export function isArrayFilter(operator: any): operator is ArrayOperator {
|
||||||
return Object.values(ArrayOperator).includes(operator)
|
return Object.values(ArrayOperator).includes(operator)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function tryExtractingTableAndViewId(tableOrViewId: string) {
|
||||||
|
if (isViewID(tableOrViewId)) {
|
||||||
|
return {
|
||||||
|
tableId: extractViewInfoFromID(tableOrViewId).tableId,
|
||||||
|
viewId: tableOrViewId,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { tableId: tableOrViewId }
|
||||||
|
}
|
||||||
|
|
|
@ -143,14 +143,17 @@ export async function getTables(tableIds: string[]): Promise<Table[]> {
|
||||||
return await processTables(tables)
|
return await processTables(tables)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function enrichViewSchemas(table: Table): TableResponse {
|
export async function enrichViewSchemas(table: Table): Promise<TableResponse> {
|
||||||
|
const views = []
|
||||||
|
for (const view of Object.values(table.views ?? [])) {
|
||||||
|
if (sdk.views.isV2(view)) {
|
||||||
|
views.push(await sdk.views.enrichSchema(view, table.schema))
|
||||||
|
} else views.push(view)
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...table,
|
...table,
|
||||||
views: Object.values(table.views ?? [])
|
views: views.reduce((p, v) => {
|
||||||
.map(v =>
|
|
||||||
sdk.views.isV2(v) ? sdk.views.enrichSchema(v, table.schema) : v
|
|
||||||
)
|
|
||||||
.reduce((p, v) => {
|
|
||||||
p[v.name!] = v
|
p[v.name!] = v
|
||||||
return p
|
return p
|
||||||
}, {} as TableViewsResponse),
|
}, {} as TableViewsResponse),
|
||||||
|
|
|
@ -75,7 +75,7 @@ describe("table sdk", () => {
|
||||||
const view1 = getTable()
|
const view1 = getTable()
|
||||||
const view2 = getTable()
|
const view2 = getTable()
|
||||||
const view3 = getTable()
|
const view3 = getTable()
|
||||||
const res = sdk.tables.enrichViewSchemas({
|
const res = await sdk.tables.enrichViewSchemas({
|
||||||
...basicTable,
|
...basicTable,
|
||||||
views: {
|
views: {
|
||||||
[view1.name]: view1,
|
[view1.name]: view1,
|
||||||
|
|
|
@ -33,7 +33,7 @@ export async function getEnriched(viewId: string): Promise<ViewV2Enriched> {
|
||||||
if (!found) {
|
if (!found) {
|
||||||
throw new Error("No view found")
|
throw new Error("No view found")
|
||||||
}
|
}
|
||||||
return enrichSchema(found, table.schema)
|
return await enrichSchema(found, table.schema)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function create(
|
export async function create(
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
import {
|
import {
|
||||||
|
FieldType,
|
||||||
|
RelationSchemaField,
|
||||||
RenameColumn,
|
RenameColumn,
|
||||||
|
Table,
|
||||||
TableSchema,
|
TableSchema,
|
||||||
View,
|
View,
|
||||||
ViewUIFieldMetadata,
|
ViewFieldMetadata,
|
||||||
ViewV2,
|
ViewV2,
|
||||||
ViewV2Enriched,
|
ViewV2Enriched,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
|
@ -57,7 +60,7 @@ async function guardViewSchema(
|
||||||
if (viewSchema[field].readonly) {
|
if (viewSchema[field].readonly) {
|
||||||
if (
|
if (
|
||||||
!(await features.isViewReadonlyColumnsEnabled()) &&
|
!(await features.isViewReadonlyColumnsEnabled()) &&
|
||||||
!(tableSchemaField as ViewUIFieldMetadata).readonly
|
!(tableSchemaField as ViewFieldMetadata).readonly
|
||||||
) {
|
) {
|
||||||
throw new HTTPError(`Readonly fields are not enabled`, 400)
|
throw new HTTPError(`Readonly fields are not enabled`, 400)
|
||||||
}
|
}
|
||||||
|
@ -158,24 +161,64 @@ export function allowedFields(
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
export function enrichSchema(
|
export async function enrichSchema(
|
||||||
view: ViewV2,
|
view: ViewV2,
|
||||||
tableSchema: TableSchema
|
tableSchema: TableSchema
|
||||||
): ViewV2Enriched {
|
): Promise<ViewV2Enriched> {
|
||||||
let schema: TableSchema = {}
|
const tableCache: Record<string, Table> = {}
|
||||||
const anyViewOrder = Object.values(view.schema || {}).some(
|
|
||||||
ui => ui.order != null
|
async function populateRelTableSchema(
|
||||||
)
|
tableId: string,
|
||||||
|
viewFields: Record<string, RelationSchemaField>
|
||||||
|
) {
|
||||||
|
if (!tableCache[tableId]) {
|
||||||
|
tableCache[tableId] = await sdk.tables.getTable(tableId)
|
||||||
|
}
|
||||||
|
const relTable = tableCache[tableId]
|
||||||
|
|
||||||
|
const result: Record<string, RelationSchemaField> = {}
|
||||||
|
|
||||||
|
for (const relTableFieldName of Object.keys(relTable.schema)) {
|
||||||
|
const relTableField = relTable.schema[relTableFieldName]
|
||||||
|
if ([FieldType.LINK, FieldType.FORMULA].includes(relTableField.type)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (relTableField.visible === false) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
const isVisible = !!viewFields[relTableFieldName]?.visible
|
||||||
|
const isReadonly = !!viewFields[relTableFieldName]?.readonly
|
||||||
|
result[relTableFieldName] = {
|
||||||
|
visible: isVisible,
|
||||||
|
readonly: isReadonly,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
let schema: ViewV2Enriched["schema"] = {}
|
||||||
|
|
||||||
|
const viewSchema = view.schema || {}
|
||||||
|
const anyViewOrder = Object.values(viewSchema).some(ui => ui.order != null)
|
||||||
for (const key of Object.keys(tableSchema).filter(
|
for (const key of Object.keys(tableSchema).filter(
|
||||||
key => tableSchema[key].visible !== false
|
k => tableSchema[k].visible !== false
|
||||||
)) {
|
)) {
|
||||||
// if nothing specified in view, then it is not visible
|
// if nothing specified in view, then it is not visible
|
||||||
const ui = view.schema?.[key] || { visible: false }
|
const ui = viewSchema[key] || { visible: false }
|
||||||
schema[key] = {
|
schema[key] = {
|
||||||
...tableSchema[key],
|
...tableSchema[key],
|
||||||
...ui,
|
...ui,
|
||||||
order: anyViewOrder ? ui?.order ?? undefined : tableSchema[key].order,
|
order: anyViewOrder ? ui?.order ?? undefined : tableSchema[key].order,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (schema[key].type === FieldType.LINK) {
|
||||||
|
schema[key].columns = await populateRelTableSchema(
|
||||||
|
schema[key].tableId,
|
||||||
|
viewSchema[key]?.columns || {}
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -209,3 +252,48 @@ export function syncSchema(
|
||||||
|
|
||||||
return view
|
return view
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function renameLinkedViews(table: Table, renaming: RenameColumn) {
|
||||||
|
const relatedTables: Record<string, Table> = {}
|
||||||
|
|
||||||
|
for (const field of Object.values(table.schema)) {
|
||||||
|
if (field.type !== FieldType.LINK) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
relatedTables[field.tableId] ??= await sdk.tables.getTable(field.tableId)
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const relatedTable of Object.values(relatedTables)) {
|
||||||
|
let toSave = false
|
||||||
|
const viewsV2 = Object.values(relatedTable.views || {}).filter(
|
||||||
|
sdk.views.isV2
|
||||||
|
)
|
||||||
|
if (!viewsV2) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const view of viewsV2) {
|
||||||
|
for (const relField of Object.keys(view.schema || {}).filter(f => {
|
||||||
|
const tableField = relatedTable.schema[f]
|
||||||
|
if (!tableField || tableField.type !== FieldType.LINK) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return tableField.tableId === table._id
|
||||||
|
})) {
|
||||||
|
const columns = view.schema?.[relField]?.columns
|
||||||
|
|
||||||
|
if (columns && columns[renaming.old]) {
|
||||||
|
columns[renaming.updated] = columns[renaming.old]
|
||||||
|
delete columns[renaming.old]
|
||||||
|
toSave = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (toSave) {
|
||||||
|
await sdk.tables.saveTable(relatedTable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ export async function getEnriched(viewId: string): Promise<ViewV2Enriched> {
|
||||||
if (!found) {
|
if (!found) {
|
||||||
throw new Error("No view found")
|
throw new Error("No view found")
|
||||||
}
|
}
|
||||||
return enrichSchema(found, table.schema)
|
return await enrichSchema(found, table.schema)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function create(
|
export async function create(
|
||||||
|
|
|
@ -3,6 +3,7 @@ import {
|
||||||
FieldSchema,
|
FieldSchema,
|
||||||
FieldType,
|
FieldType,
|
||||||
INTERNAL_TABLE_SOURCE_ID,
|
INTERNAL_TABLE_SOURCE_ID,
|
||||||
|
RelationshipType,
|
||||||
Table,
|
Table,
|
||||||
TableSchema,
|
TableSchema,
|
||||||
TableSourceType,
|
TableSourceType,
|
||||||
|
@ -10,6 +11,7 @@ import {
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { generator } from "@budibase/backend-core/tests"
|
import { generator } from "@budibase/backend-core/tests"
|
||||||
import { enrichSchema, syncSchema } from ".."
|
import { enrichSchema, syncSchema } from ".."
|
||||||
|
import sdk from "../../../../sdk"
|
||||||
|
|
||||||
describe("table sdk", () => {
|
describe("table sdk", () => {
|
||||||
const basicTable: Table = {
|
const basicTable: Table = {
|
||||||
|
@ -58,7 +60,7 @@ describe("table sdk", () => {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("enrichViewSchemas", () => {
|
describe("enrichSchema", () => {
|
||||||
it("should fetch the default schema if not overridden", async () => {
|
it("should fetch the default schema if not overridden", async () => {
|
||||||
const tableId = basicTable._id!
|
const tableId = basicTable._id!
|
||||||
const view: ViewV2 = {
|
const view: ViewV2 = {
|
||||||
|
@ -68,7 +70,7 @@ describe("table sdk", () => {
|
||||||
tableId,
|
tableId,
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = enrichSchema(view, basicTable.schema)
|
const res = await enrichSchema(view, basicTable.schema)
|
||||||
|
|
||||||
expect(res).toEqual({
|
expect(res).toEqual({
|
||||||
...view,
|
...view,
|
||||||
|
@ -118,7 +120,7 @@ describe("table sdk", () => {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = enrichSchema(view, basicTable.schema)
|
const res = await enrichSchema(view, basicTable.schema)
|
||||||
|
|
||||||
expect(res).toEqual({
|
expect(res).toEqual({
|
||||||
...view,
|
...view,
|
||||||
|
@ -152,7 +154,7 @@ describe("table sdk", () => {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = enrichSchema(view, basicTable.schema)
|
const res = await enrichSchema(view, basicTable.schema)
|
||||||
|
|
||||||
expect(res).toEqual({
|
expect(res).toEqual({
|
||||||
...view,
|
...view,
|
||||||
|
@ -187,7 +189,7 @@ describe("table sdk", () => {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = enrichSchema(view, basicTable.schema)
|
const res = await enrichSchema(view, basicTable.schema)
|
||||||
|
|
||||||
expect(res).toEqual(
|
expect(res).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
|
@ -241,7 +243,7 @@ describe("table sdk", () => {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = enrichSchema(view, basicTable.schema)
|
const res = await enrichSchema(view, basicTable.schema)
|
||||||
|
|
||||||
expect(res).toEqual(
|
expect(res).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
|
@ -280,6 +282,92 @@ describe("table sdk", () => {
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should include related fields", async () => {
|
||||||
|
const table: Table = {
|
||||||
|
...basicTable,
|
||||||
|
schema: {
|
||||||
|
name: {
|
||||||
|
name: "name",
|
||||||
|
type: FieldType.STRING,
|
||||||
|
},
|
||||||
|
other: {
|
||||||
|
name: "other",
|
||||||
|
type: FieldType.LINK,
|
||||||
|
relationshipType: RelationshipType.ONE_TO_MANY,
|
||||||
|
fieldName: "table",
|
||||||
|
tableId: "otherTableId",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const otherTable: Table = {
|
||||||
|
...basicTable,
|
||||||
|
primaryDisplay: "title",
|
||||||
|
schema: {
|
||||||
|
title: {
|
||||||
|
name: "title",
|
||||||
|
type: FieldType.STRING,
|
||||||
|
},
|
||||||
|
age: {
|
||||||
|
name: "age",
|
||||||
|
type: FieldType.NUMBER,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const tableId = table._id!
|
||||||
|
|
||||||
|
const getTableSpy = jest.spyOn(sdk.tables, "getTable")
|
||||||
|
getTableSpy.mockResolvedValueOnce(otherTable)
|
||||||
|
|
||||||
|
const view: ViewV2 = {
|
||||||
|
version: 2,
|
||||||
|
id: generator.guid(),
|
||||||
|
name: generator.guid(),
|
||||||
|
tableId,
|
||||||
|
schema: {
|
||||||
|
name: { visible: true },
|
||||||
|
other: {
|
||||||
|
visible: true,
|
||||||
|
columns: {
|
||||||
|
title: {
|
||||||
|
visible: true,
|
||||||
|
readonly: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const res = await enrichSchema(view, table.schema)
|
||||||
|
|
||||||
|
expect(res).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
...view,
|
||||||
|
schema: {
|
||||||
|
name: {
|
||||||
|
...table.schema.name,
|
||||||
|
visible: true,
|
||||||
|
},
|
||||||
|
other: {
|
||||||
|
...table.schema.other,
|
||||||
|
visible: true,
|
||||||
|
columns: {
|
||||||
|
title: {
|
||||||
|
visible: true,
|
||||||
|
readonly: true,
|
||||||
|
},
|
||||||
|
age: {
|
||||||
|
visible: false,
|
||||||
|
readonly: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("syncSchema", () => {
|
describe("syncSchema", () => {
|
||||||
|
|
|
@ -14,4 +14,14 @@ export class AutomationAPI extends TestAPI {
|
||||||
)
|
)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
post = async (
|
||||||
|
body: Automation,
|
||||||
|
expectations?: Expectations
|
||||||
|
): Promise<Automation> => {
|
||||||
|
const result = await this._post<Automation>(`/api/automations`, {
|
||||||
|
body,
|
||||||
|
expectations,
|
||||||
|
})
|
||||||
|
return result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -449,7 +449,11 @@ class Orchestrator {
|
||||||
outputs: tempOutput,
|
outputs: tempOutput,
|
||||||
inputs: steps[stepToLoopIndex].inputs,
|
inputs: steps[stepToLoopIndex].inputs,
|
||||||
})
|
})
|
||||||
this.context.steps[currentIndex + 1] = tempOutput
|
this.context.steps[this.context.steps.length] = tempOutput
|
||||||
|
this.context.steps = this.context.steps.filter(
|
||||||
|
item => !item.hasOwnProperty.call(item, "currentItem")
|
||||||
|
)
|
||||||
|
|
||||||
this.loopStepOutputs = []
|
this.loopStepOutputs = []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -461,6 +465,19 @@ class Orchestrator {
|
||||||
for (const branch of branches) {
|
for (const branch of branches) {
|
||||||
const condition = await this.evaluateBranchCondition(branch.condition)
|
const condition = await this.evaluateBranchCondition(branch.condition)
|
||||||
if (condition) {
|
if (condition) {
|
||||||
|
let branchStatus = {
|
||||||
|
status: `${branch.name} branch taken`,
|
||||||
|
success: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
this.updateExecutionOutput(
|
||||||
|
branchStep.id,
|
||||||
|
branchStep.stepId,
|
||||||
|
branchStep.inputs,
|
||||||
|
branchStatus
|
||||||
|
)
|
||||||
|
this.context.steps[this.context.steps.length] = branchStatus
|
||||||
|
|
||||||
const branchSteps = children?.[branch.name] || []
|
const branchSteps = children?.[branch.name] || []
|
||||||
await this.executeSteps(branchSteps)
|
await this.executeSteps(branchSteps)
|
||||||
break
|
break
|
||||||
|
@ -569,10 +586,10 @@ class Orchestrator {
|
||||||
this.loopStepOutputs.push(outputs)
|
this.loopStepOutputs.push(outputs)
|
||||||
} else {
|
} else {
|
||||||
this.updateExecutionOutput(step.id, step.stepId, step.inputs, outputs)
|
this.updateExecutionOutput(step.id, step.stepId, step.inputs, outputs)
|
||||||
}
|
|
||||||
this.context.steps[this.context.steps.length] = outputs
|
this.context.steps[this.context.steps.length] = outputs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function execute(job: Job<AutomationData>, callback: WorkerCallback) {
|
export function execute(job: Job<AutomationData>, callback: WorkerCallback) {
|
||||||
const appId = job.data.event.appId
|
const appId = job.data.event.appId
|
||||||
|
|
|
@ -247,6 +247,7 @@ export async function outputProcessing<T extends Row[] | Row>(
|
||||||
preserveLinks?: boolean
|
preserveLinks?: boolean
|
||||||
fromRow?: Row
|
fromRow?: Row
|
||||||
skipBBReferences?: boolean
|
skipBBReferences?: boolean
|
||||||
|
fromViewId?: string
|
||||||
} = {
|
} = {
|
||||||
squash: true,
|
squash: true,
|
||||||
preserveLinks: false,
|
preserveLinks: false,
|
||||||
|
@ -350,10 +351,9 @@ export async function outputProcessing<T extends Row[] | Row>(
|
||||||
enriched = await processFormulas(table, enriched, { dynamic: true })
|
enriched = await processFormulas(table, enriched, { dynamic: true })
|
||||||
|
|
||||||
if (opts.squash) {
|
if (opts.squash) {
|
||||||
enriched = (await linkRows.squashLinksToPrimaryDisplay(
|
enriched = await linkRows.squashLinks(table, enriched, {
|
||||||
table,
|
fromViewId: opts?.fromViewId,
|
||||||
enriched
|
})
|
||||||
)) as Row[]
|
|
||||||
}
|
}
|
||||||
// remove null properties to match internal API
|
// remove null properties to match internal API
|
||||||
const isExternal = isExternalTableID(table._id!)
|
const isExternal = isExternalTableID(table._id!)
|
||||||
|
|
|
@ -33,7 +33,13 @@ export interface View {
|
||||||
groupBy?: string
|
groupBy?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ViewUIFieldMetadata = UIFieldMetadata & {
|
export type ViewFieldMetadata = UIFieldMetadata & {
|
||||||
|
readonly?: boolean
|
||||||
|
columns?: Record<string, RelationSchemaField>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type RelationSchemaField = {
|
||||||
|
visible?: boolean
|
||||||
readonly?: boolean
|
readonly?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +51,7 @@ export enum CalculationType {
|
||||||
MAX = "max",
|
MAX = "max",
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ViewCalculationFieldMetadata = ViewUIFieldMetadata & {
|
export type ViewCalculationFieldMetadata = ViewFieldMetadata & {
|
||||||
calculationType: CalculationType
|
calculationType: CalculationType
|
||||||
field: string
|
field: string
|
||||||
}
|
}
|
||||||
|
@ -62,7 +68,7 @@ export interface ViewV2 {
|
||||||
order?: SortOrder
|
order?: SortOrder
|
||||||
type?: SortType
|
type?: SortType
|
||||||
}
|
}
|
||||||
schema?: Record<string, ViewUIFieldMetadata | ViewCalculationFieldMetadata>
|
schema?: Record<string, ViewFieldMetadata | ViewCalculationFieldMetadata>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ViewSchema = ViewCountOrSumSchema | ViewStatisticsSchema
|
export type ViewSchema = ViewCountOrSumSchema | ViewStatisticsSchema
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
export enum FeatureFlag {
|
export enum FeatureFlag {
|
||||||
PER_CREATOR_PER_USER_PRICE = "PER_CREATOR_PER_USER_PRICE",
|
PER_CREATOR_PER_USER_PRICE = "PER_CREATOR_PER_USER_PRICE",
|
||||||
PER_CREATOR_PER_USER_PRICE_ALERT = "PER_CREATOR_PER_USER_PRICE_ALERT",
|
PER_CREATOR_PER_USER_PRICE_ALERT = "PER_CREATOR_PER_USER_PRICE_ALERT",
|
||||||
|
ENRICHED_RELATIONSHIPS = "ENRICHED_RELATIONSHIPS",
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TenantFeatureFlags {
|
export interface TenantFeatureFlags {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { WithRequired } from "../shared"
|
||||||
|
|
||||||
export interface SearchParams {
|
export interface SearchParams {
|
||||||
tableId?: string
|
tableId?: string
|
||||||
|
viewId?: string
|
||||||
query?: SearchFilters
|
query?: SearchFilters
|
||||||
paginate?: boolean
|
paginate?: boolean
|
||||||
bookmark?: string | number
|
bookmark?: string | number
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
import { TableSchema, ViewV2 } from "../documents"
|
import { FieldSchema, RelationSchemaField, ViewV2 } from "../documents"
|
||||||
|
|
||||||
export interface ViewV2Enriched extends ViewV2 {
|
export interface ViewV2Enriched extends ViewV2 {
|
||||||
schema?: TableSchema
|
schema?: {
|
||||||
|
[key: string]: FieldSchema & {
|
||||||
|
columns?: Record<string, RelationSchemaField>
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue