2022-01-21 07:04:44 +13:00
|
|
|
const { FieldTypes, FormulaTypes } = require("../../constants")
|
2021-09-03 05:33:41 +12:00
|
|
|
const { processStringSync } = require("@budibase/string-templates")
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Looks through the rows provided and finds formulas - which it then processes.
|
|
|
|
*/
|
2022-01-21 07:04:44 +13:00
|
|
|
exports.processFormulas = (table, rows, { dynamic } = { dynamic: true }) => {
|
2021-09-03 05:33:41 +12:00
|
|
|
const single = !Array.isArray(rows)
|
|
|
|
if (single) {
|
|
|
|
rows = [rows]
|
|
|
|
}
|
|
|
|
for (let [column, schema] of Object.entries(table.schema)) {
|
2022-01-21 07:04:44 +13:00
|
|
|
const isStatic = schema.formulaType === FormulaTypes.STATIC
|
|
|
|
if (
|
|
|
|
schema.type !== FieldTypes.FORMULA ||
|
|
|
|
(dynamic && isStatic) ||
|
|
|
|
(!dynamic && !isStatic)
|
|
|
|
) {
|
2021-09-03 05:33:41 +12:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// iterate through rows and process formula
|
|
|
|
rows = rows.map(row => ({
|
|
|
|
...row,
|
|
|
|
[column]: processStringSync(schema.formula, row),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
return single ? rows[0] : rows
|
|
|
|
}
|