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 d565819494
commit 50eff577f9
5 changed files with 47 additions and 23 deletions

View File

@ -435,6 +435,7 @@
error={errors.relatedName} error={errors.relatedName}
/> />
{:else if field.type === FORMULA_TYPE} {:else if field.type === FORMULA_TYPE}
{#if !table.sql}
<Select <Select
label="Formula type" label="Formula type"
bind:value={field.formulaType} bind:value={field.formulaType}
@ -447,6 +448,7 @@
tooltip="Dynamic formula are calculated when retrieved, but cannot be filtered, tooltip="Dynamic formula are calculated when retrieved, but cannot be filtered,
while static formula are calculated when the row is saved." 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) {
if (limit != null) {
this.limit = limit this.limit = limit
}
return this return this
} }
setSort(sort) { setSort(sort) {
if (sort != null) {
this.sort = sort this.sort = sort
}
return this return this
} }
setSortOrder(sortOrder) { setSortOrder(sortOrder) {
if (sortOrder != null) {
this.sortOrder = sortOrder this.sortOrder = sortOrder
}
return this return this
} }
setSortType(sortType) { setSortType(sortType) {
if (sortType != null) {
this.sortType = sortType 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