Adding error to catch scenario that caused tests to fail - fixing cases of table metadata not aligning with entityId
This commit is contained in:
parent
828d78f2af
commit
a33c2599b5
|
@ -374,38 +374,44 @@ export class ExternalRequest<T extends Operation> {
|
||||||
) {
|
) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
let tableId: string | undefined,
|
let relatedTableId: string | undefined,
|
||||||
lookupField: string | undefined,
|
lookupField: string | undefined,
|
||||||
fieldName: string | undefined
|
fieldName: string | undefined
|
||||||
if (isManyToMany(field)) {
|
if (isManyToMany(field)) {
|
||||||
tableId = field.through
|
relatedTableId = field.through
|
||||||
lookupField = primaryKey
|
lookupField = primaryKey
|
||||||
fieldName = field.throughTo || primaryKey
|
fieldName = field.throughTo || primaryKey
|
||||||
} else if (isManyToOne(field)) {
|
} else if (isManyToOne(field)) {
|
||||||
tableId = field.tableId
|
relatedTableId = field.tableId
|
||||||
lookupField = field.foreignKey
|
lookupField = field.foreignKey
|
||||||
fieldName = field.fieldName
|
fieldName = field.fieldName
|
||||||
}
|
}
|
||||||
if (!tableId || !lookupField || !fieldName) {
|
if (!relatedTableId || !lookupField || !fieldName) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"Unable to lookup relationships - undefined column properties."
|
"Unable to lookup relationships - undefined column properties."
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
const { tableName: relatedTableName } = breakExternalTableId(tableId)
|
const { tableName: relatedTableName } =
|
||||||
|
breakExternalTableId(relatedTableId)
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const linkPrimaryKey = this.tables[relatedTableName].primary[0]
|
const linkPrimaryKey = this.tables[relatedTableName].primary[0]
|
||||||
if (!lookupField || !row[lookupField]) {
|
if (!lookupField || !row[lookupField]) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
const endpoint = getEndpoint(relatedTableId, Operation.READ)
|
||||||
|
const relatedTable = this.tables[endpoint.entityId]
|
||||||
|
if (!relatedTable) {
|
||||||
|
throw new Error("unable to find related table")
|
||||||
|
}
|
||||||
const response = await getDatasourceAndQuery({
|
const response = await getDatasourceAndQuery({
|
||||||
endpoint: getEndpoint(tableId, Operation.READ),
|
endpoint: endpoint,
|
||||||
filters: {
|
filters: {
|
||||||
equal: {
|
equal: {
|
||||||
[fieldName]: row[lookupField],
|
[fieldName]: row[lookupField],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
meta: {
|
meta: {
|
||||||
table,
|
table: relatedTable,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
// this is the response from knex if no rows found
|
// this is the response from knex if no rows found
|
||||||
|
@ -414,7 +420,11 @@ export class ExternalRequest<T extends Operation> {
|
||||||
const storeTo = isManyToMany(field)
|
const storeTo = isManyToMany(field)
|
||||||
? field.throughFrom || linkPrimaryKey
|
? field.throughFrom || linkPrimaryKey
|
||||||
: fieldName
|
: fieldName
|
||||||
related[storeTo] = { rows, isMany: isManyToMany(field), tableId }
|
related[storeTo] = {
|
||||||
|
rows,
|
||||||
|
isMany: isManyToMany(field),
|
||||||
|
tableId: relatedTableId,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return related
|
return related
|
||||||
}
|
}
|
||||||
|
@ -484,7 +494,7 @@ export class ExternalRequest<T extends Operation> {
|
||||||
body,
|
body,
|
||||||
filters: buildFilters(id, {}, linkTable),
|
filters: buildFilters(id, {}, linkTable),
|
||||||
meta: {
|
meta: {
|
||||||
table,
|
table: linkTable,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
|
@ -2,6 +2,7 @@ import {
|
||||||
QueryJson,
|
QueryJson,
|
||||||
Datasource,
|
Datasource,
|
||||||
DatasourcePlusQueryResponse,
|
DatasourcePlusQueryResponse,
|
||||||
|
RowOperations,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { getIntegration } from "../index"
|
import { getIntegration } from "../index"
|
||||||
import sdk from "../../sdk"
|
import sdk from "../../sdk"
|
||||||
|
@ -10,6 +11,17 @@ export async function makeExternalQuery(
|
||||||
datasource: Datasource,
|
datasource: Datasource,
|
||||||
json: QueryJson
|
json: QueryJson
|
||||||
): Promise<DatasourcePlusQueryResponse> {
|
): Promise<DatasourcePlusQueryResponse> {
|
||||||
|
const entityId = json.endpoint.entityId,
|
||||||
|
tableName = json.meta.table.name,
|
||||||
|
tableId = json.meta.table._id
|
||||||
|
// case found during testing - make sure this doesn't happen again
|
||||||
|
if (
|
||||||
|
RowOperations.includes(json.endpoint.operation) &&
|
||||||
|
entityId !== tableId &&
|
||||||
|
entityId !== tableName
|
||||||
|
) {
|
||||||
|
throw new Error("Entity ID and table metadata do not align")
|
||||||
|
}
|
||||||
datasource = await sdk.datasources.enrich(datasource)
|
datasource = await sdk.datasources.enrich(datasource)
|
||||||
const Integration = await getIntegration(datasource.source)
|
const Integration = await getIntegration(datasource.source)
|
||||||
// query is the opinionated function
|
// query is the opinionated function
|
||||||
|
|
|
@ -14,6 +14,14 @@ export enum Operation {
|
||||||
DELETE_TABLE = "DELETE_TABLE",
|
DELETE_TABLE = "DELETE_TABLE",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const RowOperations = [
|
||||||
|
Operation.CREATE,
|
||||||
|
Operation.READ,
|
||||||
|
Operation.UPDATE,
|
||||||
|
Operation.DELETE,
|
||||||
|
Operation.BULK_CREATE,
|
||||||
|
]
|
||||||
|
|
||||||
export enum SortDirection {
|
export enum SortDirection {
|
||||||
ASCENDING = "ASCENDING",
|
ASCENDING = "ASCENDING",
|
||||||
DESCENDING = "DESCENDING",
|
DESCENDING = "DESCENDING",
|
||||||
|
|
Loading…
Reference in New Issue