Some work towards supporting many SQL relationships.

This commit is contained in:
mike12345567 2021-06-28 17:42:39 +01:00
parent af71bf478a
commit 5dd53f67ac
3 changed files with 19 additions and 18 deletions

View File

@ -53,7 +53,7 @@
let deletion let deletion
$: tableOptions = $tables.list.filter( $: tableOptions = $tables.list.filter(
table => table._id !== $tables.draft._id table => table._id !== $tables.draft._id && table.type !== "external"
) )
$: required = !!field?.constraints?.presence || primaryDisplay $: required = !!field?.constraints?.presence || primaryDisplay
$: uneditable = $: uneditable =

View File

@ -3,7 +3,6 @@ const {
DataSourceOperation, DataSourceOperation,
SortDirection, SortDirection,
FieldTypes, FieldTypes,
RelationshipTypes,
} = require("../../../constants") } = require("../../../constants")
const { getAllExternalTables } = require("../table/utils") const { getAllExternalTables } = require("../table/utils")
const { const {
@ -137,22 +136,29 @@ function buildFilters(id, filters, table) {
} }
} }
function buildRelationships(table) { function buildRelationships(table, allTables) {
const relationships = [] const relationships = []
for (let [fieldName, field] of Object.entries(table.schema)) { for (let [fieldName, field] of Object.entries(table.schema)) {
if (field.type !== FieldTypes.LINK) { if (field.type !== FieldTypes.LINK) {
continue continue
} }
// TODO: through field const { tableName: linkTableName } = breakExternalTableId(field.tableId)
if (field.relationshipType === RelationshipTypes.MANY_TO_MANY) { const linkTable = allTables.find(table => table._id === field.tableId)
// no table to link to, this is not a valid relationships
if (!linkTable) {
continue continue
} }
const broken = breakExternalTableId(field.tableId) const definition = {
relationships.push({ from: fieldName || table.primary,
from: fieldName, to: field.fieldName || linkTable.primary,
to: field.fieldName, tableName: linkTableName,
tableName: broken.tableName, through: undefined,
}) }
if (field.through) {
const { tableName: throughTableName } = breakExternalTableId(field.through)
definition.through = throughTableName
}
relationships.push(definition)
} }
return relationships return relationships
} }
@ -171,7 +177,7 @@ async function handleRequest(
} }
// clean up row on ingress using schema // clean up row on ingress using schema
filters = buildFilters(id, filters, table) filters = buildFilters(id, filters, table)
const relationships = buildRelationships(table) const relationships = buildRelationships(table, tables)
row = inputProcessing(row, table) row = inputProcessing(row, table)
if ( if (
operation === DataSourceOperation.DELETE && operation === DataSourceOperation.DELETE &&

View File

@ -1,12 +1,7 @@
const handlebars = require("handlebars") const handlebars = require("handlebars")
const { registerAll } = require("./helpers/index") const { registerAll } = require("./helpers/index")
const processors = require("./processors") const processors = require("./processors")
const { cloneDeep } = require("lodash/fp") const { removeHandlebarsStatements } = require("./utilities")
const {
removeNull,
updateContext,
removeHandlebarsStatements,
} = require("./utilities")
const manifest = require("../manifest.json") const manifest = require("../manifest.json")
const hbsInstance = handlebars.create() const hbsInstance = handlebars.create()