2022-11-22 07:33:34 +13:00
|
|
|
import {
|
2022-04-27 01:24:51 +12:00
|
|
|
AutoFieldDefaultNames,
|
|
|
|
AutoFieldSubTypes,
|
2023-04-20 04:17:37 +12:00
|
|
|
FieldTypes,
|
|
|
|
FormulaTypes,
|
2022-11-22 07:33:34 +13:00
|
|
|
} from "../../constants"
|
|
|
|
import { processStringSync } from "@budibase/string-templates"
|
2023-08-01 22:52:00 +12:00
|
|
|
import { FieldSchema, Row, Table } from "@budibase/types"
|
2021-09-03 05:33:41 +12:00
|
|
|
|
2022-04-27 01:24:51 +12:00
|
|
|
/**
|
|
|
|
* If the subtype has been lost for any reason this works out what
|
|
|
|
* subtype the auto column should be.
|
|
|
|
*/
|
2022-11-22 07:33:34 +13:00
|
|
|
export function fixAutoColumnSubType(column: FieldSchema) {
|
2022-04-27 01:24:51 +12:00
|
|
|
if (!column.autocolumn || !column.name || column.subtype) {
|
|
|
|
return column
|
|
|
|
}
|
|
|
|
// the columns which get auto generated
|
|
|
|
if (column.name.endsWith(AutoFieldDefaultNames.CREATED_BY)) {
|
|
|
|
column.subtype = AutoFieldSubTypes.CREATED_BY
|
|
|
|
} else if (column.name.endsWith(AutoFieldDefaultNames.UPDATED_BY)) {
|
|
|
|
column.subtype = AutoFieldSubTypes.UPDATED_BY
|
|
|
|
} else if (column.name.endsWith(AutoFieldDefaultNames.CREATED_AT)) {
|
|
|
|
column.subtype = AutoFieldSubTypes.CREATED_AT
|
|
|
|
} else if (column.name.endsWith(AutoFieldDefaultNames.UPDATED_AT)) {
|
|
|
|
column.subtype = AutoFieldSubTypes.UPDATED_AT
|
|
|
|
} else if (column.name.endsWith(AutoFieldDefaultNames.AUTO_ID)) {
|
|
|
|
column.subtype = AutoFieldSubTypes.AUTO_ID
|
|
|
|
}
|
|
|
|
return column
|
|
|
|
}
|
|
|
|
|
2021-09-03 05:33:41 +12:00
|
|
|
/**
|
|
|
|
* Looks through the rows provided and finds formulas - which it then processes.
|
|
|
|
*/
|
2022-11-22 07:33:34 +13:00
|
|
|
export function processFormulas(
|
|
|
|
table: Table,
|
2022-11-23 07:49:19 +13:00
|
|
|
rows: Row[] | Row,
|
2022-11-22 07:33:34 +13:00
|
|
|
{ dynamic, contextRows }: any = { dynamic: true }
|
|
|
|
) {
|
2021-09-03 05:33:41 +12:00
|
|
|
const single = !Array.isArray(rows)
|
2022-11-23 07:49:19 +13:00
|
|
|
let rowArray: Row[]
|
2021-09-03 05:33:41 +12:00
|
|
|
if (single) {
|
2022-11-23 07:49:19 +13:00
|
|
|
rowArray = [rows]
|
2022-01-22 05:24:24 +13:00
|
|
|
contextRows = contextRows ? [contextRows] : contextRows
|
2022-11-23 07:49:19 +13:00
|
|
|
} else {
|
|
|
|
rowArray = rows
|
2021-09-03 05:33:41 +12:00
|
|
|
}
|
|
|
|
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 ||
|
2023-04-20 04:17:37 +12:00
|
|
|
schema.formula == null ||
|
2022-01-21 07:04:44 +13:00
|
|
|
(dynamic && isStatic) ||
|
|
|
|
(!dynamic && !isStatic)
|
|
|
|
) {
|
2021-09-03 05:33:41 +12:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// iterate through rows and process formula
|
2022-11-23 07:49:19 +13:00
|
|
|
for (let i = 0; i < rowArray.length; i++) {
|
2023-04-20 04:17:37 +12:00
|
|
|
let row = rowArray[i]
|
|
|
|
let context = contextRows ? contextRows[i] : row
|
|
|
|
rowArray[i] = {
|
|
|
|
...row,
|
|
|
|
[column]: processStringSync(schema.formula, context),
|
2022-01-22 05:24:24 +13:00
|
|
|
}
|
|
|
|
}
|
2021-09-03 05:33:41 +12:00
|
|
|
}
|
2022-11-23 07:49:19 +13:00
|
|
|
return single ? rowArray[0] : rowArray
|
2021-09-03 05:33:41 +12:00
|
|
|
}
|
2022-06-07 19:31:00 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes any date columns and ensures that those without the ignoreTimezones
|
|
|
|
* flag set are parsed as UTC rather than local time.
|
|
|
|
*/
|
2022-11-22 07:33:34 +13:00
|
|
|
export function processDates(table: Table, rows: Row[]) {
|
2022-06-07 19:31:00 +12:00
|
|
|
let datesWithTZ = []
|
|
|
|
for (let [column, schema] of Object.entries(table.schema)) {
|
|
|
|
if (schema.type !== FieldTypes.DATETIME) {
|
|
|
|
continue
|
|
|
|
}
|
2022-09-08 04:57:02 +12:00
|
|
|
if (!schema.timeOnly && !schema.ignoreTimezones) {
|
2022-06-07 19:31:00 +12:00
|
|
|
datesWithTZ.push(column)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let row of rows) {
|
|
|
|
for (let col of datesWithTZ) {
|
|
|
|
if (row[col] && typeof row[col] === "string" && !row[col].endsWith("Z")) {
|
|
|
|
row[col] = new Date(row[col]).toISOString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rows
|
|
|
|
}
|