Merge pull request #10352 from Budibase/fix/10349
Allow SQL formulas using related row information to be used as display columns
This commit is contained in:
commit
9c93bf91c0
|
@ -1,31 +1,32 @@
|
||||||
import {
|
import {
|
||||||
|
Datasource,
|
||||||
|
FieldSchema,
|
||||||
|
FieldType,
|
||||||
FilterType,
|
FilterType,
|
||||||
IncludeRelationship,
|
IncludeRelationship,
|
||||||
Operation,
|
Operation,
|
||||||
PaginationJson,
|
PaginationJson,
|
||||||
RelationshipsJson,
|
RelationshipsJson,
|
||||||
|
RelationshipTypes,
|
||||||
|
Row,
|
||||||
SearchFilters,
|
SearchFilters,
|
||||||
SortJson,
|
SortJson,
|
||||||
Datasource,
|
|
||||||
FieldSchema,
|
|
||||||
Row,
|
|
||||||
Table,
|
|
||||||
RelationshipTypes,
|
|
||||||
FieldType,
|
|
||||||
SortType,
|
SortType,
|
||||||
|
Table,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import {
|
import {
|
||||||
|
breakExternalTableId,
|
||||||
breakRowIdField,
|
breakRowIdField,
|
||||||
|
convertRowId,
|
||||||
generateRowIdField,
|
generateRowIdField,
|
||||||
isRowId,
|
isRowId,
|
||||||
convertRowId,
|
isSQL,
|
||||||
} from "../../../integrations/utils"
|
} from "../../../integrations/utils"
|
||||||
import { getDatasourceAndQuery } from "./utils"
|
import { getDatasourceAndQuery } from "./utils"
|
||||||
import { FieldTypes } from "../../../constants"
|
import { FieldTypes } from "../../../constants"
|
||||||
import { breakExternalTableId, isSQL } from "../../../integrations/utils"
|
|
||||||
import { processObjectSync } from "@budibase/string-templates"
|
import { processObjectSync } from "@budibase/string-templates"
|
||||||
import { cloneDeep } from "lodash/fp"
|
import { cloneDeep } from "lodash/fp"
|
||||||
import { processFormulas, processDates } from "../../../utilities/rowProcessor"
|
import { processDates, processFormulas } from "../../../utilities/rowProcessor"
|
||||||
import { db as dbCore } from "@budibase/backend-core"
|
import { db as dbCore } from "@budibase/backend-core"
|
||||||
import sdk from "../../../sdk"
|
import sdk from "../../../sdk"
|
||||||
|
|
||||||
|
@ -382,10 +383,18 @@ export class ExternalRequest {
|
||||||
}
|
}
|
||||||
const display = linkedTable.primaryDisplay
|
const display = linkedTable.primaryDisplay
|
||||||
for (let key of Object.keys(row[relationship.column])) {
|
for (let key of Object.keys(row[relationship.column])) {
|
||||||
const related: Row = row[relationship.column][key]
|
let relatedRow: Row = row[relationship.column][key]
|
||||||
|
// add this row as context for the relationship
|
||||||
|
for (let col of Object.values(linkedTable.schema)) {
|
||||||
|
if (col.type === FieldType.LINK && col.tableId === table._id) {
|
||||||
|
relatedRow[col.name] = [row]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
relatedRow = processFormulas(linkedTable, relatedRow)
|
||||||
|
const relatedDisplay = display ? relatedRow[display] : undefined
|
||||||
row[relationship.column][key] = {
|
row[relationship.column][key] = {
|
||||||
primaryDisplay: display ? related[display] : undefined,
|
primaryDisplay: relatedDisplay || "Invalid display column",
|
||||||
_id: related._id,
|
_id: relatedRow._id,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -921,6 +921,7 @@ describe("row api - postgres", () => {
|
||||||
[m2mFieldName]: [
|
[m2mFieldName]: [
|
||||||
{
|
{
|
||||||
_id: row._id,
|
_id: row._id,
|
||||||
|
primaryDisplay: "Invalid display column",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
@ -929,6 +930,7 @@ describe("row api - postgres", () => {
|
||||||
[m2mFieldName]: [
|
[m2mFieldName]: [
|
||||||
{
|
{
|
||||||
_id: row._id,
|
_id: row._id,
|
||||||
|
primaryDisplay: "Invalid display column",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|
|
@ -79,10 +79,17 @@ function generateSchema(
|
||||||
if (!relatedTable) {
|
if (!relatedTable) {
|
||||||
throw "Referenced table doesn't exist"
|
throw "Referenced table doesn't exist"
|
||||||
}
|
}
|
||||||
|
const relatedPrimary = relatedTable.primary[0]
|
||||||
|
const externalType = relatedTable.schema[relatedPrimary].externalType
|
||||||
|
if (externalType) {
|
||||||
|
schema.specificType(column.foreignKey, externalType)
|
||||||
|
} else {
|
||||||
schema.integer(column.foreignKey).unsigned()
|
schema.integer(column.foreignKey).unsigned()
|
||||||
|
}
|
||||||
|
|
||||||
schema
|
schema
|
||||||
.foreign(column.foreignKey)
|
.foreign(column.foreignKey)
|
||||||
.references(`${tableName}.${relatedTable.primary[0]}`)
|
.references(`${tableName}.${relatedPrimary}`)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import {
|
import {
|
||||||
FieldTypes,
|
|
||||||
FormulaTypes,
|
|
||||||
AutoFieldDefaultNames,
|
AutoFieldDefaultNames,
|
||||||
AutoFieldSubTypes,
|
AutoFieldSubTypes,
|
||||||
|
FieldTypes,
|
||||||
|
FormulaTypes,
|
||||||
} from "../../constants"
|
} from "../../constants"
|
||||||
import { processStringSync } from "@budibase/string-templates"
|
import { processStringSync } from "@budibase/string-templates"
|
||||||
import { FieldSchema, Table, Row } from "@budibase/types"
|
import { FieldSchema, FieldType, Row, Table } from "@budibase/types"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the subtype has been lost for any reason this works out what
|
* If the subtype has been lost for any reason this works out what
|
||||||
|
@ -50,6 +50,7 @@ export function processFormulas(
|
||||||
const isStatic = schema.formulaType === FormulaTypes.STATIC
|
const isStatic = schema.formulaType === FormulaTypes.STATIC
|
||||||
if (
|
if (
|
||||||
schema.type !== FieldTypes.FORMULA ||
|
schema.type !== FieldTypes.FORMULA ||
|
||||||
|
schema.formula == null ||
|
||||||
(dynamic && isStatic) ||
|
(dynamic && isStatic) ||
|
||||||
(!dynamic && !isStatic)
|
(!dynamic && !isStatic)
|
||||||
) {
|
) {
|
||||||
|
@ -57,7 +58,6 @@ export function processFormulas(
|
||||||
}
|
}
|
||||||
// iterate through rows and process formula
|
// iterate through rows and process formula
|
||||||
for (let i = 0; i < rowArray.length; i++) {
|
for (let i = 0; i < rowArray.length; i++) {
|
||||||
if (schema.formula) {
|
|
||||||
let row = rowArray[i]
|
let row = rowArray[i]
|
||||||
let context = contextRows ? contextRows[i] : row
|
let context = contextRows ? contextRows[i] : row
|
||||||
rowArray[i] = {
|
rowArray[i] = {
|
||||||
|
@ -66,7 +66,6 @@ export function processFormulas(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return single ? rowArray[0] : rowArray
|
return single ? rowArray[0] : rowArray
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue