1
0
Fork 0
mirror of synced 2024-09-28 23:31:43 +12:00

Getting the static formulas being processed on input, not re-processing on output.

This commit is contained in:
mike12345567 2022-01-20 18:04:44 +00:00
parent d66b57a44c
commit 8115f6eef2
5 changed files with 47 additions and 23 deletions

View file

@ -435,18 +435,20 @@
error={errors.relatedName} error={errors.relatedName}
/> />
{:else if field.type === FORMULA_TYPE} {:else if field.type === FORMULA_TYPE}
<Select {#if !table.sql}
label="Formula type" <Select
bind:value={field.formulaType} label="Formula type"
options={[ bind:value={field.formulaType}
{ label: "Dynamic", value: "dynamic" }, options={[
{ label: "Static", value: "static" }, { label: "Dynamic", value: "dynamic" },
]} { label: "Static", value: "static" },
getOptionLabel={option => option.label} ]}
getOptionValue={option => option.value} getOptionLabel={option => option.label}
tooltip="Dynamic formula are calculated when retrieved, but cannot be filtered, getOptionValue={option => option.value}
while static formula are calculated when the row is saved." tooltip="Dynamic formula are calculated when retrieved, but cannot be filtered,
/> while static formula are calculated when the row is saved."
/>
{/if}
<ModalBindableInput <ModalBindableInput
title="Formula" title="Formula"
label="Formula" label="Formula"

View file

@ -37,22 +37,30 @@ class QueryBuilder {
} }
setLimit(limit) { setLimit(limit) {
this.limit = limit if (limit != null) {
this.limit = limit
}
return this return this
} }
setSort(sort) { setSort(sort) {
this.sort = sort if (sort != null) {
this.sort = sort
}
return this return this
} }
setSortOrder(sortOrder) { setSortOrder(sortOrder) {
this.sortOrder = sortOrder if (sortOrder != null) {
this.sortOrder = sortOrder
}
return this return this
} }
setSortType(sortType) { setSortType(sortType) {
this.sortType = sortType if (sortType != null) {
this.sortType = sortType
}
return this return this
} }

View file

@ -58,6 +58,11 @@ exports.RelationshipTypes = {
MANY_TO_MANY: "many-to-many", MANY_TO_MANY: "many-to-many",
} }
exports.FormulaTypes = {
STATIC: "static",
DYNAMIC: "dynamic",
}
exports.AuthTypes = { exports.AuthTypes = {
APP: "app", APP: "app",
BUILDER: "builder", BUILDER: "builder",

View file

@ -229,11 +229,12 @@ exports.inputProcessing = (
} }
continue continue
} }
// specific case to delete formula values if they get saved // remove any formula values, they are to be generated
// type coercion cannot completely remove the field, so have to do it here
if (field.type === FieldTypes.FORMULA) { if (field.type === FieldTypes.FORMULA) {
delete clonedRow[key] delete clonedRow[key]
} else { }
// otherwise coerce what is there to correct types
else {
clonedRow[key] = exports.coerce(value, field.type) clonedRow[key] = exports.coerce(value, field.type)
} }
} }
@ -243,6 +244,9 @@ exports.inputProcessing = (
clonedRow._rev = row._rev clonedRow._rev = row._rev
} }
// now process the static formulas
clonedRow = processFormulas(table, clonedRow, { dynamic: false })
// handle auto columns - this returns an object like {table, row} // handle auto columns - this returns an object like {table, row}
return processAutoColumn(user, copiedTable, clonedRow, opts) return processAutoColumn(user, copiedTable, clonedRow, opts)
} }
@ -273,7 +277,7 @@ exports.outputProcessing = async (
let enriched = await linkRows.attachFullLinkedDocs(ctx, table, rows) let enriched = await linkRows.attachFullLinkedDocs(ctx, table, rows)
// process formulas // process formulas
enriched = processFormulas(table, enriched) enriched = processFormulas(table, enriched, { dynamic: true })
// update the attachments URL depending on hosting // update the attachments URL depending on hosting
for (let [property, column] of Object.entries(table.schema)) { for (let [property, column] of Object.entries(table.schema)) {

View file

@ -1,16 +1,21 @@
const { FieldTypes } = require("../../constants") const { FieldTypes, FormulaTypes } = require("../../constants")
const { processStringSync } = require("@budibase/string-templates") const { processStringSync } = require("@budibase/string-templates")
/** /**
* Looks through the rows provided and finds formulas - which it then processes. * Looks through the rows provided and finds formulas - which it then processes.
*/ */
exports.processFormulas = (table, rows) => { exports.processFormulas = (table, rows, { dynamic } = { dynamic: true }) => {
const single = !Array.isArray(rows) const single = !Array.isArray(rows)
if (single) { if (single) {
rows = [rows] rows = [rows]
} }
for (let [column, schema] of Object.entries(table.schema)) { for (let [column, schema] of Object.entries(table.schema)) {
if (schema.type !== FieldTypes.FORMULA) { const isStatic = schema.formulaType === FormulaTypes.STATIC
if (
schema.type !== FieldTypes.FORMULA ||
(dynamic && isStatic) ||
(!dynamic && !isStatic)
) {
continue continue
} }
// iterate through rows and process formula // iterate through rows and process formula