Merge master.

This commit is contained in:
Sam Rose 2024-05-30 14:32:19 +01:00
commit 3570322805
No known key found for this signature in database
42 changed files with 1453 additions and 307 deletions

View File

@ -53,6 +53,11 @@ done
if [[ -z "${COUCH_DB_URL}" ]]; then
export COUCH_DB_URL=http://$COUCHDB_USER:$COUCHDB_PASSWORD@127.0.0.1:5984
fi
if [[ -z "${COUCH_DB_SQL_URL}" ]]; then
export COUCH_DB_SQL_URL=http://127.0.0.1:4984
fi
if [ ! -f "${DATA_DIR}/.env" ]; then
touch ${DATA_DIR}/.env
for ENV_VAR in "${ENV_VARS[@]}"

View File

@ -1,5 +1,5 @@
{
"version": "2.27.4",
"version": "2.27.5",
"npmClient": "yarn",
"packages": [
"packages/*",

View File

@ -40,7 +40,7 @@
"build:oss": "NODE_OPTIONS=--max-old-space-size=1500 lerna run build --stream --ignore @budibase/account-portal --ignore @budibase/account-portal-server --ignore @budibase/account-portal-ui",
"build:account-portal": "NODE_OPTIONS=--max-old-space-size=1500 lerna run build --stream --scope @budibase/account-portal --scope @budibase/account-portal-server --scope @budibase/account-portal-ui",
"build:dev": "lerna run --stream prebuild && yarn nx run-many --target=build --output-style=dynamic --watch --preserveWatchOutput",
"check:types": "lerna run check:types",
"check:types": "lerna run --concurrency 2 check:types",
"build:sdk": "lerna run --stream build:sdk",
"deps:circular": "madge packages/server/dist/index.js packages/worker/src/index.ts packages/backend-core/dist/src/index.js packages/cli/src/index.js --circular",
"release": "lerna publish from-package --yes --force-publish --no-git-tag-version --no-push --no-git-reset",

View File

@ -1,6 +1,11 @@
import { Knex, knex } from "knex"
import * as dbCore from "../db"
import { isIsoDateString, isValidFilter, getNativeSql } from "./utils"
import {
isIsoDateString,
isValidFilter,
getNativeSql,
isExternalTable,
} from "./utils"
import { SqlStatements } from "./sqlStatements"
import SqlTableQueryBuilder from "./sqlTable"
import {
@ -21,6 +26,7 @@ import {
SqlClient,
QueryOptions,
JsonTypes,
prefixed,
} from "@budibase/types"
import environment from "../environment"
import { helpers } from "@budibase/shared-core"
@ -391,6 +397,16 @@ class InternalBuilder {
contains(filters.containsAny, true)
}
// when searching internal tables make sure long looking for rows
if (filters.documentType && !isExternalTable(table)) {
const tableRef = opts?.aliases?.[table._id!] || table._id
// has to be its own option, must always be AND onto the search
query.andWhereLike(
`${tableRef}._id`,
`${prefixed(filters.documentType)}%`
)
}
return query
}
@ -592,6 +608,7 @@ class InternalBuilder {
query = this.addFilters(query, filters, json.meta.table, {
aliases: tableAliases,
})
// add sorting to pre-query
query = this.addSorting(query, json)
const alias = tableAliases?.[tableName] || tableName

View File

@ -106,6 +106,10 @@ export const useViewPermissions = () => {
return useFeature(Feature.VIEW_PERMISSIONS)
}
export const useViewReadonlyColumns = () => {
return useFeature(Feature.VIEW_READONLY_COLUMNS)
}
// QUOTAS
export const setAutomationLogsQuota = (value: number) => {

View File

@ -57,6 +57,7 @@
class:fullWidth
class="spectrum-ActionButton spectrum-ActionButton--size{size}"
class:active
class:disabled
{disabled}
on:longPress
on:click|preventDefault
@ -109,19 +110,22 @@
background: var(--spectrum-global-color-gray-300);
border-color: var(--spectrum-global-color-gray-500);
}
.noPadding {
padding: 0;
min-width: 0;
}
.spectrum-ActionButton--quiet {
padding: 0 8px;
}
.spectrum-ActionButton--quiet.is-selected {
color: var(--spectrum-global-color-gray-900);
}
.noPadding {
padding: 0;
min-width: 0;
}
.is-selected:not(.emphasized) .spectrum-Icon {
color: var(--spectrum-global-color-gray-900);
}
.is-selected.disabled .spectrum-Icon {
color: var(--spectrum-global-color-gray-500);
}
.tooltip {
position: absolute;
pointer-events: none;

View File

@ -11,6 +11,7 @@
import {
decodeJSBinding,
encodeJSBinding,
processObjectSync,
processStringSync,
} from "@budibase/string-templates"
import { readableToRuntimeBinding } from "dataBinding"
@ -153,13 +154,6 @@
debouncedEval(expression, context, snippets)
}
const getBindingValue = (binding, context, snippets) => {
const js = `return $("${binding.runtimeBinding}")`
const hbs = encodeJSBinding(js)
const res = processStringSync(hbs, { ...context, snippets })
return JSON.stringify(res, null, 2)
}
const highlightJSON = json => {
return formatHighlight(json, {
keyColor: "#e06c75",
@ -172,11 +166,27 @@
}
const enrichBindings = (bindings, context, snippets) => {
return bindings.map(binding => {
// Create a single big array to enrich in one go
const bindingStrings = bindings.map(binding => {
if (binding.runtimeBinding.startsWith('trim "')) {
// Account for nasty hardcoded HBS bindings for roles, for legacy
// compatibility
return `{{ ${binding.runtimeBinding} }}`
} else {
return `{{ literal ${binding.runtimeBinding} }}`
}
})
const bindingEvauations = processObjectSync(bindingStrings, {
...context,
snippets,
})
// Enrich bindings with evaluations and highlighted HTML
return bindings.map((binding, idx) => {
if (!context) {
return binding
}
const value = getBindingValue(binding, context, snippets)
const value = JSON.stringify(bindingEvauations[idx], null, 2)
return {
...binding,
value,

View File

@ -75,13 +75,6 @@
if (!context || !binding.value || binding.value === "") {
return
}
// Roles have always been broken for JS. We need to exclude them from
// showing a popover as it will show "Error while executing JS".
if (binding.category === "Role") {
return
}
stopHidingPopover()
popoverAnchor = target
hoverTarget = {

View File

@ -32,8 +32,14 @@
onboarding = true
try {
const { password, firstName, lastName } = formData
await users.acceptInvite(inviteCode, password, firstName, lastName)
const user = await users.acceptInvite(
inviteCode,
password,
firstName,
lastName
)
notifications.success("Invitation accepted successfully")
auth.setOrg(user.tenantId)
await login()
} catch (error) {
notifications.error(error.message)
@ -66,7 +72,7 @@
notifications.success("Logged in successfully")
$goto("../portal")
} catch (err) {
notifications.error(err.message ? err.message : "Invalid credentials") //not likely, considering.
notifications.error(err.message ? err.message : "Something went wrong")
}
}
@ -141,12 +147,19 @@
password: e.detail,
}
}}
validateOn="blur"
validate={() => {
let fieldError = {}
fieldError["password"] = !formData.password
? "Please enter a password"
: undefined
function validatePassword() {
if (!formData.password) {
return "Please enter a password"
} else if (formData.password.length < 8) {
return "Please enter at least 8 characters"
}
return undefined
}
fieldError["password"] = validatePassword()
fieldError["confirmationPassword"] =
!passwordsMatch(

View File

@ -127,7 +127,7 @@
flex-direction: column;
}
label {
white-space: nowrap;
word-wrap: break-word;
}
label.hidden {
padding: 0;

View File

@ -1,7 +1,8 @@
<script>
import { getContext } from "svelte"
import { ActionButton, Popover, Toggle, Icon } from "@budibase/bbui"
import { ActionButton, Popover, Icon } from "@budibase/bbui"
import { getColumnIcon } from "../lib/utils"
import ToggleActionButtonGroup from "./ToggleActionButtonGroup.svelte"
const { columns, datasource, stickyColumn, dispatch } = getContext("grid")
@ -11,31 +12,45 @@
$: anyHidden = $columns.some(col => !col.visible)
$: text = getText($columns)
const toggleColumn = async (column, visible) => {
datasource.actions.addSchemaMutation(column.name, { visible })
await datasource.actions.saveSchemaMutations()
dispatch(visible ? "show-column" : "hide-column")
}
const toggleColumn = async (column, permission) => {
const visible = permission !== PERMISSION_OPTIONS.HIDDEN
const toggleAll = async visible => {
let mutations = {}
$columns.forEach(column => {
mutations[column.name] = { visible }
})
datasource.actions.addSchemaMutations(mutations)
datasource.actions.addSchemaMutation(column.name, { visible })
await datasource.actions.saveSchemaMutations()
dispatch(visible ? "show-column" : "hide-column")
}
const getText = columns => {
const hidden = columns.filter(col => !col.visible).length
return hidden ? `Hide columns (${hidden})` : "Hide columns"
return hidden ? `Columns (${hidden} restricted)` : "Columns"
}
const PERMISSION_OPTIONS = {
WRITABLE: "writable",
HIDDEN: "hidden",
}
const options = [
{ icon: "Edit", value: PERMISSION_OPTIONS.WRITABLE, tooltip: "Writable" },
{
icon: "VisibilityOff",
value: PERMISSION_OPTIONS.HIDDEN,
tooltip: "Hidden",
},
]
function columnToPermissionOptions(column) {
if (!column.visible) {
return PERMISSION_OPTIONS.HIDDEN
}
return PERMISSION_OPTIONS.WRITABLE
}
</script>
<div bind:this={anchor}>
<ActionButton
icon="VisibilityOff"
icon="ColumnSettings"
quiet
size="M"
on:click={() => (open = !open)}
@ -54,25 +69,25 @@
<Icon size="S" name={getColumnIcon($stickyColumn)} />
{$stickyColumn.label}
</div>
<Toggle disabled size="S" value={true} />
<ToggleActionButtonGroup
disabled
value={PERMISSION_OPTIONS.WRITABLE}
{options}
/>
{/if}
{#each $columns as column}
<div class="column">
<Icon size="S" name={getColumnIcon(column)} />
{column.label}
</div>
<Toggle
size="S"
value={column.visible}
on:change={e => toggleColumn(column, e.detail)}
disabled={column.primaryDisplay}
<ToggleActionButtonGroup
on:click={e => toggleColumn(column, e.detail)}
value={columnToPermissionOptions(column)}
{options}
/>
{/each}
</div>
<div class="buttons">
<ActionButton on:click={() => toggleAll(true)}>Show all</ActionButton>
<ActionButton on:click={() => toggleAll(false)}>Hide all</ActionButton>
</div>
</div>
</Popover>
@ -83,15 +98,11 @@
flex-direction: column;
gap: 12px;
}
.buttons {
display: flex;
flex-direction: row;
gap: 8px;
}
.columns {
display: grid;
align-items: center;
grid-template-columns: 1fr auto;
gap: 8px;
}
.columns :global(.spectrum-Switch) {
margin-right: 0;

View File

@ -0,0 +1,43 @@
<script>
import { createEventDispatcher } from "svelte"
let dispatch = createEventDispatcher()
import { ActionButton, AbsTooltip, TooltipType } from "@budibase/bbui"
export let value
export let options
export let disabled
</script>
<div class="permissionPicker">
{#each options as option}
<AbsTooltip text={option.tooltip} type={TooltipType.Info}>
<ActionButton
on:click={() => dispatch("click", option.value)}
{disabled}
size="S"
icon={option.icon}
quiet
selected={option.value === value}
noPadding
/>
</AbsTooltip>
{/each}
</div>
<style>
.permissionPicker {
display: flex;
gap: var(--spacing-xs);
padding-left: calc(var(--spacing-xl) * 2);
}
.permissionPicker :global(.spectrum-Icon) {
width: 14px;
}
.permissionPicker :global(.spectrum-ActionButton) {
width: 28px;
height: 28px;
}
</style>

View File

@ -16,9 +16,10 @@
scroll,
isDragging,
buttonColumnWidth,
showVScrollbar,
} = getContext("grid")
let measureContainer
let container
$: buttons = $props.buttons?.slice(0, 3) || []
$: columnsWidth = $visibleColumns.reduce(
@ -39,7 +40,7 @@
const width = entries?.[0]?.contentRect?.width ?? 0
buttonColumnWidth.set(width)
})
observer.observe(measureContainer)
observer.observe(container)
})
</script>
@ -50,7 +51,7 @@
class:hidden={$buttonColumnWidth === 0}
>
<div class="content" on:mouseleave={() => ($hoveredRowId = null)}>
<GridScrollWrapper scrollVertically attachHandlers>
<GridScrollWrapper scrollVertically attachHandlers bind:ref={container}>
{#each $renderedRows as row}
{@const rowSelected = !!$selectedRows[row._id]}
{@const rowHovered = $hoveredRowId === row._id}
@ -59,7 +60,6 @@
class="row"
on:mouseenter={$isDragging ? null : () => ($hoveredRowId = row._id)}
on:mouseleave={$isDragging ? null : () => ($hoveredRowId = null)}
bind:this={measureContainer}
>
<GridCell
width="auto"
@ -67,7 +67,7 @@
selected={rowSelected}
highlighted={rowHovered || rowFocused}
>
<div class="buttons">
<div class="buttons" class:offset={$showVScrollbar}>
{#each buttons as button}
<Button
newStyles
@ -121,6 +121,9 @@
gap: var(--cell-padding);
height: inherit;
}
.buttons.offset {
padding-right: calc(var(--cell-padding) + 2 * var(--scroll-bar-size) - 2px);
}
.buttons :global(.spectrum-Button-Label) {
display: flex;
align-items: center;

View File

@ -18,7 +18,7 @@
import UserAvatars from "./UserAvatars.svelte"
import KeyboardManager from "../overlays/KeyboardManager.svelte"
import SortButton from "../controls/SortButton.svelte"
import HideColumnsButton from "../controls/HideColumnsButton.svelte"
import ColumnsSettingButton from "../controls/ColumnsSettingButton.svelte"
import SizeButton from "../controls/SizeButton.svelte"
import NewRow from "./NewRow.svelte"
import { createGridWebsocket } from "../lib/websocket"
@ -29,6 +29,7 @@
Padding,
SmallRowHeight,
ControlsHeight,
ScrollBarSize,
} from "../lib/constants"
export let API = null
@ -145,14 +146,14 @@
class:quiet
on:mouseenter={() => gridFocused.set(true)}
on:mouseleave={() => gridFocused.set(false)}
style="--row-height:{$rowHeight}px; --default-row-height:{DefaultRowHeight}px; --gutter-width:{GutterWidth}px; --max-cell-render-overflow:{MaxCellRenderOverflow}px; --content-lines:{$contentLines}; --min-height:{$minHeight}px; --controls-height:{ControlsHeight}px;"
style="--row-height:{$rowHeight}px; --default-row-height:{DefaultRowHeight}px; --gutter-width:{GutterWidth}px; --max-cell-render-overflow:{MaxCellRenderOverflow}px; --content-lines:{$contentLines}; --min-height:{$minHeight}px; --controls-height:{ControlsHeight}px; --scroll-bar-size:{ScrollBarSize}px;"
>
{#if showControls}
<div class="controls">
<div class="controls-left">
<slot name="filter" />
<SortButton />
<HideColumnsButton />
<ColumnsSettingButton />
<SizeButton />
<slot name="controls" />
</div>

View File

@ -18,6 +18,7 @@
export let scrollVertically = false
export let scrollHorizontally = false
export let attachHandlers = false
export let ref
// Used for tracking touch events
let initialTouchX
@ -109,7 +110,7 @@
on:touchmove={attachHandlers ? handleTouchMove : null}
on:click|self={() => ($focusedCellId = null)}
>
<div {style} class="inner">
<div {style} class="inner" bind:this={ref}>
<slot />
</div>
</div>

View File

@ -119,7 +119,7 @@
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
class="v-scrollbar"
style="--size:{ScrollBarSize}px; top:{barTop}px; height:{barHeight}px;"
style="top:{barTop}px; height:{barHeight}px;"
on:mousedown={startVDragging}
on:touchstart={startVDragging}
class:dragging={isDraggingV}
@ -129,7 +129,7 @@
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
class="h-scrollbar"
style="--size:{ScrollBarSize}px; left:{barLeft}px; width:{barWidth}px;"
style="left:{barLeft}px; width:{barWidth}px;"
on:mousedown={startHDragging}
on:touchstart={startHDragging}
class:dragging={isDraggingH}
@ -149,11 +149,11 @@
opacity: 1;
}
.v-scrollbar {
width: var(--size);
right: var(--size);
width: var(--scroll-bar-size);
right: var(--scroll-bar-size);
}
.h-scrollbar {
height: var(--size);
bottom: var(--size);
height: var(--scroll-bar-size);
bottom: var(--scroll-bar-size);
}
</style>

View File

@ -404,8 +404,11 @@ export const createActions = context => {
// Save change
try {
// Mark as in progress
inProgressChanges.update(state => ({ ...state, [rowId]: true }))
// Increment change count for this row
inProgressChanges.update(state => ({
...state,
[rowId]: (state[rowId] || 0) + 1,
}))
// Update row
const changes = get(rowChangeCache)[rowId]
@ -423,17 +426,25 @@ export const createActions = context => {
await refreshRow(saved.id)
}
// Wipe row change cache now that we've saved the row
// Wipe row change cache for any values which have been saved
const liveChanges = get(rowChangeCache)[rowId]
rowChangeCache.update(state => {
delete state[rowId]
Object.keys(changes || {}).forEach(key => {
if (changes[key] === liveChanges?.[key]) {
delete state[rowId][key]
}
})
return state
})
} catch (error) {
handleValidationError(rowId, error)
}
// Mark as completed
inProgressChanges.update(state => ({ ...state, [rowId]: false }))
// Decrement change count for this row
inProgressChanges.update(state => ({
...state,
[rowId]: (state[rowId] || 1) - 1,
}))
}
// Updates a value of a row
@ -553,7 +564,6 @@ export const initialise = context => {
previousFocusedCellId,
rows,
validation,
focusedCellId,
} = context
// Wipe the row change cache when changing row
@ -571,20 +581,12 @@ export const initialise = context => {
if (!id) {
return
}
// Stop if we changed row
const split = parseCellID(id)
const oldRowId = split.id
const oldColumn = split.field
const { id: newRowId } = parseCellID(get(focusedCellId))
if (oldRowId !== newRowId) {
return
}
// Otherwise we just changed cell in the same row
const hasChanges = oldColumn in (get(rowChangeCache)[oldRowId] || {})
const hasErrors = validation.actions.rowHasErrors(oldRowId)
const isSavingChanges = get(inProgressChanges)[oldRowId]
if (oldRowId && !hasErrors && hasChanges && !isSavingChanges) {
await rows.actions.applyRowChanges(oldRowId)
const { id: rowId, field } = parseCellID(id)
const hasChanges = field in (get(rowChangeCache)[rowId] || {})
const hasErrors = validation.actions.rowHasErrors(rowId)
const isSavingChanges = get(inProgressChanges)[rowId]
if (rowId && !hasErrors && hasChanges && !isSavingChanges) {
await rows.actions.applyRowChanges(rowId)
}
})
}

View File

@ -109,6 +109,7 @@ export const initialise = context => {
maxScrollTop,
scrollLeft,
maxScrollLeft,
buttonColumnWidth,
} = context
// Ensure scroll state never goes invalid, which can happen when changing
@ -194,8 +195,10 @@ export const initialise = context => {
// Ensure column is not cutoff on right edge
else {
const $buttonColumnWidth = get(buttonColumnWidth)
const rightEdge = column.left + column.width
const rightBound = $bounds.width + $scroll.left - FocusedCellMinOffset
const rightBound =
$bounds.width + $scroll.left - FocusedCellMinOffset - $buttonColumnWidth
delta = rightEdge - rightBound
if (delta > 0) {
scroll.update(state => ({

View File

@ -3,7 +3,7 @@ import {
CreateViewRequest,
Ctx,
RequiredKeys,
UIFieldMetadata,
ViewUIFieldMetadata,
UpdateViewRequest,
ViewResponse,
ViewResponseEnriched,
@ -18,22 +18,23 @@ async function parseSchema(view: CreateViewRequest) {
const finalViewSchema =
view.schema &&
Object.entries(view.schema).reduce((p, [fieldName, schemaValue]) => {
const fieldSchema: RequiredKeys<UIFieldMetadata> = {
const fieldSchema: RequiredKeys<ViewUIFieldMetadata> = {
order: schemaValue.order,
width: schemaValue.width,
visible: schemaValue.visible,
readonly: schemaValue.readonly,
icon: schemaValue.icon,
}
Object.entries(fieldSchema)
.filter(([, val]) => val === undefined)
.forEach(([key]) => {
delete fieldSchema[key as keyof UIFieldMetadata]
delete fieldSchema[key as keyof ViewUIFieldMetadata]
})
p[fieldName] = fieldSchema
return p
}, {} as Record<string, RequiredKeys<UIFieldMetadata>>)
}, {} as Record<string, RequiredKeys<ViewUIFieldMetadata>>)
for (let [key, column] of Object.entries(finalViewSchema)) {
if (!column.visible) {
if (!column.visible && !column.readonly) {
delete finalViewSchema[key]
}
}

View File

@ -1,24 +0,0 @@
const setup = require("./utilities")
describe("/templates", () => {
let request = setup.getRequest()
let config = setup.getConfig()
afterAll(setup.afterAll)
beforeAll(async () => {
await config.init()
})
describe("fetch", () => {
it("should be able to fetch templates", async () => {
const res = await request
.get(`/api/templates`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
// this test is quite light right now, templates aren't heavily utilised yet
expect(Array.isArray(res.body)).toEqual(true)
})
})
})

View File

@ -0,0 +1,125 @@
import * as setup from "./utilities"
import path from "path"
import nock from "nock"
import { generator } from "@budibase/backend-core/tests"
interface App {
background: string
icon: string
category: string
description: string
name: string
url: string
type: string
key: string
image: string
}
interface Manifest {
templates: {
app: { [key: string]: App }
}
}
function setManifest(manifest: Manifest) {
nock("https://prod-budi-templates.s3-eu-west-1.amazonaws.com")
.get("/manifest.json")
.reply(200, manifest)
}
function mockApp(key: string, tarPath: string) {
nock("https://prod-budi-templates.s3-eu-west-1.amazonaws.com")
.get(`/templates/app/${key}.tar.gz`)
.replyWithFile(200, tarPath)
}
function mockAgencyClientPortal() {
setManifest({
templates: {
app: {
"Agency Client Portal": {
background: "#20a3a8",
icon: "Project",
category: "Portals",
description:
"Manage clients, streamline communications, and securely share files.",
name: "Agency Client Portal",
url: "https://budibase.com/portals/templates/agency-client-portal-template/",
type: "app",
key: "app/agency-client-portal",
image:
"https://prod-budi-templates.s3.eu-west-1.amazonaws.com/images/agency-client-portal.png",
},
},
},
})
mockApp(
"agency-client-portal",
path.resolve(__dirname, "data", "agency-client-portal.tar.gz")
)
}
describe("/templates", () => {
let config = setup.getConfig()
afterAll(setup.afterAll)
beforeAll(async () => {
await config.init()
})
beforeEach(() => {
nock.cleanAll()
mockAgencyClientPortal()
})
describe("fetch", () => {
it("should be able to fetch templates", async () => {
const templates = await config.api.templates.fetch()
expect(templates).toHaveLength(1)
expect(templates[0].name).toBe("Agency Client Portal")
})
})
describe("create app from template", () => {
it.each(["sqs", "lucene"])(
`should be able to create an app from a template (%s)`,
async source => {
const env = {
SQS_SEARCH_ENABLE: source === "sqs" ? "true" : "false",
}
await config.withEnv(env, async () => {
const name = generator.guid().replaceAll("-", "")
const url = `/${name}`
const app = await config.api.application.create({
name,
url,
useTemplate: "true",
templateName: "Agency Client Portal",
templateKey: "app/agency-client-portal",
})
expect(app.name).toBe(name)
expect(app.url).toBe(url)
await config.withApp(app, async () => {
const tables = await config.api.table.fetch()
expect(tables).toHaveLength(2)
tables.sort((a, b) => a.name.localeCompare(b.name))
const [agencyProjects, users] = tables
expect(agencyProjects.name).toBe("Agency Projects")
expect(users.name).toBe("Users")
const { rows } = await config.api.row.search(agencyProjects._id!, {
tableId: agencyProjects._id!,
query: {},
})
expect(rows).toHaveLength(3)
})
})
}
)
})
})

View File

@ -14,8 +14,8 @@ import {
StaticQuotaName,
Table,
TableSourceType,
UIFieldMetadata,
UpdateViewRequest,
ViewUIFieldMetadata,
ViewV2,
} from "@budibase/types"
import { generator, mocks } from "@budibase/backend-core/tests"
@ -23,6 +23,9 @@ import { DatabaseName, getDatasource } from "../../../integrations/tests/utils"
import merge from "lodash/merge"
import { quotas } from "@budibase/pro"
import { roles } from "@budibase/backend-core"
import * as schemaUtils from "../../../utilities/schema"
jest.mock("../../../utilities/schema")
describe.each([
["internal", undefined],
@ -96,6 +99,10 @@ describe.each([
setup.afterAll()
})
beforeEach(() => {
mocks.licenses.useCloudFree()
})
const getRowUsage = async () => {
const { total } = await config.doInContext(undefined, () =>
quotas.getCurrentUsageValues(QuotaUsageType.STATIC, StaticQuotaName.ROWS)
@ -141,7 +148,7 @@ describe.each([
type: SortType.STRING,
},
schema: {
name: {
Price: {
visible: true,
},
},
@ -150,7 +157,11 @@ describe.each([
expect(res).toEqual({
...newView,
schema: newView.schema,
schema: {
Price: {
visible: true,
},
},
id: expect.any(String),
version: 2,
})
@ -214,6 +225,211 @@ describe.each([
status: 201,
})
})
it("does not persist non-visible fields", async () => {
const newView: CreateViewRequest = {
name: generator.name(),
tableId: table._id!,
primaryDisplay: generator.word(),
schema: {
Price: { visible: true },
Category: { visible: false },
},
}
const res = await config.api.viewV2.create(newView)
expect(res).toEqual({
...newView,
schema: {
Price: {
visible: true,
},
},
id: expect.any(String),
version: 2,
})
})
it("throws bad request when the schema fields are not valid", async () => {
const newView: CreateViewRequest = {
name: generator.name(),
tableId: table._id!,
schema: {
nonExisting: {
visible: true,
},
},
}
await config.api.viewV2.create(newView, {
status: 400,
body: {
message: 'Field "nonExisting" is not valid for the requested table',
},
})
})
describe("readonly fields", () => {
beforeEach(() => {
mocks.licenses.useViewReadonlyColumns()
})
it("readonly fields are persisted", async () => {
const table = await config.api.table.save(
saveTableRequest({
schema: {
name: {
name: "name",
type: FieldType.STRING,
},
description: {
name: "description",
type: FieldType.STRING,
},
},
})
)
const newView: CreateViewRequest = {
name: generator.name(),
tableId: table._id!,
schema: {
name: {
visible: true,
readonly: true,
},
description: {
visible: true,
readonly: true,
},
},
}
const res = await config.api.viewV2.create(newView)
expect(res.schema).toEqual({
name: {
visible: true,
readonly: true,
},
description: {
visible: true,
readonly: true,
},
})
})
it("required fields cannot be marked as readonly", async () => {
const isRequiredSpy = jest.spyOn(schemaUtils, "isRequired")
isRequiredSpy.mockReturnValueOnce(true)
const table = await config.api.table.save(
saveTableRequest({
schema: {
name: {
name: "name",
type: FieldType.STRING,
},
description: {
name: "description",
type: FieldType.STRING,
},
},
})
)
const newView: CreateViewRequest = {
name: generator.name(),
tableId: table._id!,
schema: {
name: {
readonly: true,
},
},
}
await config.api.viewV2.create(newView, {
status: 400,
body: {
message:
'Field "name" cannot be readonly as it is a required field',
status: 400,
},
})
})
it("readonly fields must be visible", async () => {
const table = await config.api.table.save(
saveTableRequest({
schema: {
name: {
name: "name",
type: FieldType.STRING,
},
description: {
name: "description",
type: FieldType.STRING,
},
},
})
)
const newView: CreateViewRequest = {
name: generator.name(),
tableId: table._id!,
schema: {
name: {
visible: false,
readonly: true,
},
},
}
await config.api.viewV2.create(newView, {
status: 400,
body: {
message:
'Field "name" must be visible if you want to make it readonly',
status: 400,
},
})
})
it("readonly fields cannot be used on free license", async () => {
mocks.licenses.useCloudFree()
const table = await config.api.table.save(
saveTableRequest({
schema: {
name: {
name: "name",
type: FieldType.STRING,
},
description: {
name: "description",
type: FieldType.STRING,
},
},
})
)
const newView: CreateViewRequest = {
name: generator.name(),
tableId: table._id!,
schema: {
name: {
visible: true,
readonly: true,
},
},
}
await config.api.viewV2.create(newView, {
status: 400,
body: {
message: "Readonly fields are not enabled for your tenant",
status: 400,
},
})
})
})
})
describe("update", () => {
@ -251,6 +467,7 @@ describe.each([
})
it("can update all fields", async () => {
mocks.licenses.useViewReadonlyColumns()
const tableId = table._id!
const updatedData: Required<UpdateViewRequest> = {
@ -275,6 +492,10 @@ describe.each([
Category: {
visible: false,
},
Price: {
visible: true,
readonly: true,
},
},
}
await config.api.viewV2.update(updatedData)
@ -291,7 +512,8 @@ describe.each([
visible: false,
}),
Price: expect.objectContaining({
visible: false,
visible: true,
readonly: true,
}),
},
},
@ -450,6 +672,67 @@ describe.each([
}
)
})
it("cannot update views with readonly on on free license", async () => {
mocks.licenses.useViewReadonlyColumns()
view = await config.api.viewV2.update({
...view,
schema: {
Price: {
visible: true,
readonly: true,
},
},
})
mocks.licenses.useCloudFree()
await config.api.viewV2.update(view, {
status: 400,
body: {
message: "Readonly fields are not enabled for your tenant",
},
})
})
it("can remove readonly config after license downgrade", async () => {
mocks.licenses.useViewReadonlyColumns()
view = await config.api.viewV2.update({
...view,
schema: {
Price: {
visible: true,
readonly: true,
},
Category: {
visible: true,
readonly: true,
},
},
})
mocks.licenses.useCloudFree()
const res = await config.api.viewV2.update({
...view,
schema: {
Price: {
visible: true,
readonly: false,
},
},
})
expect(res).toEqual(
expect.objectContaining({
...view,
schema: {
Price: {
visible: true,
readonly: false,
},
},
})
)
})
})
describe("delete", () => {
@ -491,15 +774,35 @@ describe.each([
const updatedTable = await config.api.table.get(table._id!)
const viewSchema = updatedTable.views![view!.name!].schema as Record<
string,
UIFieldMetadata
ViewUIFieldMetadata
>
expect(viewSchema.Price?.visible).toEqual(false)
expect(viewSchema.Category?.visible).toEqual(true)
})
it("should be able to fetch readonly config after downgrades", async () => {
mocks.licenses.useViewReadonlyColumns()
const res = await config.api.viewV2.create({
name: generator.name(),
tableId: table._id!,
schema: {
Price: { visible: true, readonly: true },
},
})
mocks.licenses.useCloudFree()
const view = await config.api.viewV2.get(res.id)
expect(view.schema?.Price).toEqual(
expect.objectContaining({ visible: true, readonly: true })
)
})
})
describe("read", () => {
it("views have extra data trimmed", async () => {
const table = await config.api.table.save(
let view: ViewV2
beforeAll(async () => {
table = await config.api.table.save(
saveTableRequest({
schema: {
Country: {
@ -514,7 +817,7 @@ describe.each([
})
)
const view = await config.api.viewV2.create({
view = await config.api.viewV2.create({
tableId: table._id!,
name: generator.guid(),
schema: {
@ -523,7 +826,9 @@ describe.each([
},
},
})
})
it("views have extra data trimmed", async () => {
let row = await config.api.row.save(view.id, {
Country: "Aussy",
Story: "aaaaa",
@ -568,6 +873,27 @@ describe.each([
expect(row.one).toBeUndefined()
expect(row.two).toEqual("bar")
})
it("can't persist readonly columns", async () => {
mocks.licenses.useViewReadonlyColumns()
const view = await config.api.viewV2.create({
tableId: table._id!,
name: generator.guid(),
schema: {
one: { visible: true, readonly: true },
two: { visible: true },
},
})
const row = await config.api.row.save(view.id, {
tableId: table!._id,
_viewId: view.id,
one: "foo",
two: "bar",
})
expect(row.one).toBeUndefined()
expect(row.two).toEqual("bar")
})
})
describe("patch", () => {
@ -588,6 +914,33 @@ describe.each([
expect(row.one).toEqual("foo")
expect(row.two).toEqual("newBar")
})
it("can't update readonly columns", async () => {
mocks.licenses.useViewReadonlyColumns()
const view = await config.api.viewV2.create({
tableId: table._id!,
name: generator.guid(),
schema: {
one: { visible: true, readonly: true },
two: { visible: true },
},
})
const newRow = await config.api.row.save(table._id!, {
one: "foo",
two: "bar",
})
await config.api.row.patch(view.id, {
tableId: table._id!,
_id: newRow._id!,
_rev: newRow._rev!,
one: "newFoo",
two: "newBar",
})
const row = await config.api.row.get(table._id!, newRow._id!)
expect(row.one).toEqual("foo")
expect(row.two).toEqual("newBar")
})
})
describe("destroy", () => {

View File

@ -144,8 +144,12 @@ describe("trimViewRowInfo middleware", () => {
name: generator.guid(),
tableId: table._id!,
schema: {
name: {},
address: {},
name: {
visible: true,
},
address: {
visible: true,
},
},
})

View File

@ -1,4 +1,5 @@
import {
DocumentType,
FieldType,
Operation,
QueryJson,
@ -148,7 +149,10 @@ export async function search(
entityId: table._id!,
operation: Operation.READ,
},
filters: cleanupFilters(query, allTables),
filters: {
...cleanupFilters(query, allTables),
documentType: DocumentType.ROW,
},
table,
meta: {
table,

View File

@ -2,10 +2,12 @@ import {
RenameColumn,
TableSchema,
View,
ViewUIFieldMetadata,
ViewV2,
ViewV2Enriched,
} from "@budibase/types"
import { db as dbCore } from "@budibase/backend-core"
import { HTTPError, db as dbCore } from "@budibase/backend-core"
import { features } from "@budibase/pro"
import { cloneDeep } from "lodash"
import * as utils from "../../../db/utils"
@ -13,6 +15,8 @@ import { isExternalTableID } from "../../../integrations/utils"
import * as internal from "./internal"
import * as external from "./external"
import sdk from "../../../sdk"
import { isRequired } from "../../../utilities/schema"
function pickApi(tableId: any) {
if (isExternalTableID(tableId)) {
@ -31,14 +35,61 @@ export async function getEnriched(viewId: string): Promise<ViewV2Enriched> {
return pickApi(tableId).getEnriched(viewId)
}
async function guardViewSchema(
tableId: string,
viewSchema?: Record<string, ViewUIFieldMetadata>
) {
if (!viewSchema || !Object.keys(viewSchema).length) {
return
}
const table = await sdk.tables.getTable(tableId)
for (const field of Object.keys(viewSchema)) {
const tableSchemaField = table.schema[field]
if (!tableSchemaField) {
throw new HTTPError(
`Field "${field}" is not valid for the requested table`,
400
)
}
if (viewSchema[field].readonly) {
if (!(await features.isViewReadonlyColumnsEnabled())) {
throw new HTTPError(
`Readonly fields are not enabled for your tenant`,
400
)
}
if (isRequired(tableSchemaField.constraints)) {
throw new HTTPError(
`Field "${field}" cannot be readonly as it is a required field`,
400
)
}
if (!viewSchema[field].visible) {
throw new HTTPError(
`Field "${field}" must be visible if you want to make it readonly`,
400
)
}
}
}
}
export async function create(
tableId: string,
viewRequest: Omit<ViewV2, "id" | "version">
): Promise<ViewV2> {
await guardViewSchema(tableId, viewRequest.schema)
return pickApi(tableId).create(tableId, viewRequest)
}
export async function update(tableId: string, view: ViewV2): Promise<ViewV2> {
await guardViewSchema(tableId, view.schema)
return pickApi(tableId).update(tableId, view)
}
@ -53,7 +104,13 @@ export async function remove(viewId: string): Promise<ViewV2> {
export function allowedFields(view: View | ViewV2) {
return [
...Object.keys(view?.schema || {}),
...Object.keys(view?.schema || {}).filter(key => {
if (!isV2(view)) {
return true
}
const fieldSchema = view.schema![key]
return fieldSchema.visible && !fieldSchema.readonly
}),
...dbCore.CONSTANT_EXTERNAL_ROW_COLS,
...dbCore.CONSTANT_INTERNAL_ROW_COLS,
]

View File

@ -314,6 +314,16 @@ export default class TestConfiguration {
}
}
async withApp(app: App | string, f: () => Promise<void>) {
const oldAppId = this.appId
this.appId = typeof app === "string" ? app : app.appId
try {
return await f()
} finally {
this.appId = oldAppId
}
}
// UTILS
_req<Req extends Record<string, any> | void, Res>(

View File

@ -12,6 +12,7 @@ import { AttachmentAPI } from "./attachment"
import { UserAPI } from "./user"
import { QueryAPI } from "./query"
import { RoleAPI } from "./role"
import { TemplateAPI } from "./template"
export default class API {
table: TableAPI
@ -27,6 +28,7 @@ export default class API {
user: UserAPI
query: QueryAPI
roles: RoleAPI
templates: TemplateAPI
constructor(config: TestConfiguration) {
this.table = new TableAPI(config)
@ -42,5 +44,6 @@ export default class API {
this.user = new UserAPI(config)
this.query = new QueryAPI(config)
this.roles = new RoleAPI(config)
this.templates = new TemplateAPI(config)
}
}

View File

@ -0,0 +1,8 @@
import { Template } from "@budibase/types"
import { Expectations, TestAPI } from "./base"
export class TemplateAPI extends TestAPI {
fetch = async (expectations?: Expectations): Promise<Template[]> => {
return await this._get<Template[]>("/api/templates", { expectations })
}
}

View File

@ -4,6 +4,7 @@ import {
TableSchema,
FieldSchema,
Row,
FieldConstraints,
} from "@budibase/types"
import { ValidColumnNameRegex, utils } from "@budibase/shared-core"
import { db } from "@budibase/backend-core"
@ -40,6 +41,15 @@ export function isRows(rows: any): rows is Rows {
return Array.isArray(rows) && rows.every(row => typeof row === "object")
}
export function isRequired(constraints: FieldConstraints | undefined) {
const isRequired =
!!constraints &&
((typeof constraints.presence !== "boolean" &&
constraints.presence?.allowEmpty === false) ||
constraints.presence === true)
return isRequired
}
export function validate(rows: Rows, schema: TableSchema): ValidationResults {
const results: ValidationResults = {
schemaValidation: {},
@ -62,12 +72,6 @@ export function validate(rows: Rows, schema: TableSchema): ValidationResults {
return
}
const isRequired =
!!constraints &&
((typeof constraints.presence !== "boolean" &&
!constraints.presence?.allowEmpty) ||
constraints.presence === true)
// If the columnType is not a string, then it's not present in the schema, and should be added to the invalid columns array
if (typeof columnType !== "string") {
results.invalidColumns.push(columnName)
@ -101,7 +105,12 @@ export function validate(rows: Rows, schema: TableSchema): ValidationResults {
} else if (
(columnType === FieldType.BB_REFERENCE ||
columnType === FieldType.BB_REFERENCE_SINGLE) &&
!isValidBBReference(columnData, columnType, columnSubtype, isRequired)
!isValidBBReference(
columnData,
columnType,
columnSubtype,
isRequired(constraints)
)
) {
results.schemaValidation[columnName] = false
} else {

View File

@ -0,0 +1,35 @@
import { isRequired } from "../schema"
describe("schema utilities", () => {
describe("isRequired", () => {
it("not required by default", () => {
const result = isRequired(undefined)
expect(result).toBe(false)
})
it("required when presence is true", () => {
const result = isRequired({ presence: true })
expect(result).toBe(true)
})
it("not required when presence is false", () => {
const result = isRequired({ presence: false })
expect(result).toBe(false)
})
it("not required when presence is an empty object", () => {
const result = isRequired({ presence: {} })
expect(result).toBe(false)
})
it("not required when allowEmpty is true", () => {
const result = isRequired({ presence: { allowEmpty: true } })
expect(result).toBe(false)
})
it("required when allowEmpty is false", () => {
const result = isRequired({ presence: { allowEmpty: false } })
expect(result).toBe(true)
})
})
})

View File

@ -1,10 +1,5 @@
import {
Row,
Table,
TableRequest,
View,
ViewV2Enriched,
} from "../../../documents"
import { Row, Table, TableRequest, View } from "../../../documents"
import { ViewV2Enriched } from "../../../sdk"
export type TableViewsResponse = { [key: string]: View | ViewV2Enriched }

View File

@ -1,4 +1,5 @@
import { ViewV2, ViewV2Enriched } from "../../../documents"
import { ViewV2 } from "../../../documents"
import { ViewV2Enriched } from "../../../sdk/view"
export interface ViewResponse {
data: ViewV2

View File

@ -92,6 +92,7 @@ export interface AcceptUserInviteResponse {
_id: string
_rev: string
email: string
tenantId: string
}
export interface SyncUserRequest {

View File

@ -1,5 +1,5 @@
import { SearchFilter, SortOrder, SortType } from "../../api"
import { TableSchema, UIFieldMetadata } from "./table"
import { UIFieldMetadata } from "./table"
import { Document } from "../document"
import { DBView } from "../../sdk"
@ -33,6 +33,10 @@ export interface View {
groupBy?: string
}
export type ViewUIFieldMetadata = UIFieldMetadata & {
readonly?: boolean
}
export interface ViewV2 {
version: 2
id: string
@ -45,11 +49,7 @@ export interface ViewV2 {
order?: SortOrder
type?: SortType
}
schema?: Record<string, UIFieldMetadata>
}
export interface ViewV2Enriched extends ViewV2 {
schema?: TableSchema
schema?: Record<string, ViewUIFieldMetadata>
}
export type ViewSchema = ViewCountOrSumSchema | ViewStatisticsSchema

View File

@ -21,3 +21,4 @@ export * from "./websocket"
export * from "./permissions"
export * from "./row"
export * from "./vm"
export * from "./view"

View File

@ -14,6 +14,7 @@ export enum Feature {
OFFLINE = "offline",
EXPANDED_PUBLIC_API = "expandedPublicApi",
VIEW_PERMISSIONS = "viewPermissions",
VIEW_READONLY_COLUMNS = "viewReadonlyColumns",
}
export type PlanFeatures = { [key in PlanType]: Feature[] | undefined }

View File

@ -1,5 +1,5 @@
import { Operation, SortDirection } from "./datasources"
import { Row, Table } from "../documents"
import { Row, Table, DocumentType } from "../documents"
import { SortType } from "../api"
import { Knex } from "knex"
@ -62,11 +62,15 @@ export interface SearchFilters {
[SearchFilterOperator.CONTAINS_ANY]?: {
[key: string]: any[]
}
// specific to SQS/SQLite search on internal tables this can be used
// to make sure the documents returned are always filtered down to a
// specific document type (such as just rows)
documentType?: DocumentType
}
export type SearchFilterKey = keyof Omit<
SearchFilters,
"allOr" | "onEmptyFilter" | "fuzzyOr"
"allOr" | "onEmptyFilter" | "fuzzyOr" | "documentType"
>
export type SearchQueryFields = Omit<SearchFilters, "allOr" | "onEmptyFilter">

View File

@ -0,0 +1,5 @@
import { TableSchema, ViewV2 } from "../documents"
export interface ViewV2Enriched extends ViewV2 {
schema?: TableSchema
}

View File

@ -35,6 +35,7 @@ import {
} from "@budibase/backend-core"
import { checkAnyUserExists } from "../../../utilities/users"
import { isEmailConfigured } from "../../../utilities/email"
import { BpmStatusKey, BpmStatusValue } from "@budibase/shared-core"
const MAX_USERS_UPLOAD_LIMIT = 1000
@ -444,10 +445,16 @@ export const inviteAccept = async (
await cache.invite.deleteCode(inviteCode)
// make sure onboarding flow is cleared
ctx.cookies.set(BpmStatusKey.ONBOARDING, BpmStatusValue.COMPLETED, {
expires: new Date(0),
})
ctx.body = {
_id: user._id!,
_rev: user._rev!,
email: user.email,
tenantId: user.tenantId,
}
}
)

View File

@ -105,11 +105,6 @@ const NO_TENANCY_ENDPOINTS = [
route: "/api/admin/auth/oidc/callback",
method: "GET",
},
// tenant is determined from code in redis
{
route: "/api/global/users/invite/accept",
method: "POST",
},
// global user search - no tenancy
// :id is user id
// TODO: this should really be `/api/system/users/:id`
@ -117,6 +112,15 @@ const NO_TENANCY_ENDPOINTS = [
route: "/api/global/users/tenant/:id",
method: "GET",
},
// tenant is determined from code in redis
{
route: "/api/global/users/invite/accept",
method: "POST",
},
{
route: "/api/global/users/invite/:code",
method: "GET",
},
]
// most public endpoints are gets, but some are posts

716
yarn.lock

File diff suppressed because it is too large Load Diff