Adding SQL relationship building.

This commit is contained in:
mike12345567 2021-10-28 22:44:31 +01:00
parent 246d08c804
commit 7b1114b7df
2 changed files with 35 additions and 12 deletions

View File

@ -89,6 +89,10 @@
if (field.type === AUTO_TYPE) { if (field.type === AUTO_TYPE) {
field = buildAutoColumn($tables.draft.name, field.name, field.subtype) field = buildAutoColumn($tables.draft.name, field.name, field.subtype)
} }
// for now you can't create other options, just many to many for SQL
if (field.type === LINK_TYPE && external) {
field.relationshipType = RelationshipTypes.ONE_TO_MANY
}
await tables.saveField({ await tables.saveField({
originalName, originalName,
field, field,
@ -324,7 +328,7 @@
getOptionLabel={table => table.name} getOptionLabel={table => table.name}
getOptionValue={table => table._id} getOptionValue={table => table._id}
/> />
{#if relationshipOptions && relationshipOptions.length > 0} {#if relationshipOptions && relationshipOptions.length > 0 && !external}
<RadioGroup <RadioGroup
disabled={linkEditDisabled} disabled={linkEditDisabled}
label="Define the relationship" label="Define the relationship"

View File

@ -4,7 +4,11 @@ const {
breakExternalTableId, breakExternalTableId,
} = require("../../../integrations/utils") } = require("../../../integrations/utils")
const { getTable } = require("./utils") const { getTable } = require("./utils")
const { DataSourceOperation, FieldTypes } = require("../../../constants") const {
DataSourceOperation,
FieldTypes,
RelationshipTypes,
} = require("../../../constants")
const { makeExternalQuery } = require("../../../integrations/base/utils") const { makeExternalQuery } = require("../../../integrations/base/utils")
const { cloneDeep } = require("lodash/fp") const { cloneDeep } = require("lodash/fp")
@ -42,6 +46,17 @@ function getDatasourceId(table) {
return breakExternalTableId(table._id).datasourceId return breakExternalTableId(table._id).datasourceId
} }
function generateRelatedSchema(linkColumn, table) {
// generate column for other table
const relatedSchema = cloneDeep(linkColumn)
relatedSchema.fieldName = linkColumn.foreignKey
relatedSchema.foreignKey = linkColumn.fieldName
relatedSchema.relationshipType = RelationshipTypes.MANY_TO_ONE
relatedSchema.tableId = table._id
delete relatedSchema.main
return relatedSchema
}
exports.save = async function (ctx) { exports.save = async function (ctx) {
const appId = ctx.appId const appId = ctx.appId
const table = ctx.request.body const table = ctx.request.body
@ -64,24 +79,28 @@ exports.save = async function (ctx) {
const tables = datasource.entities const tables = datasource.entities
// check if relations need setup // check if relations need setup
for (let [key, schema] of Object.entries(tableToSave.schema)) { for (let schema of Object.values(tableToSave.schema)) {
// TODO: this assumes all relationships are the same, need to handle cardinality and many to many // TODO: many to many handling
if (schema.type === FieldTypes.LINK) { if (schema.type === FieldTypes.LINK) {
const relatedTable = Object.values(tables).find( const relatedTable = Object.values(tables).find(
table => table._id === schema.tableId table => table._id === schema.tableId
) )
// setup the schema in this table
const relatedField = schema.fieldName const relatedField = schema.fieldName
const relatedPrimary = relatedTable.primary[0]
// generate a foreign key
const foreignKey = `fk_${relatedTable.name}_${schema.fieldName}` const foreignKey = `fk_${relatedTable.name}_${schema.fieldName}`
// create foreign key
tableToSave.schema[foreignKey] = { type: FieldTypes.NUMBER } schema.relationshipType = RelationshipTypes.ONE_TO_MANY
// setup the relation in other table and this one
schema.foreignKey = foreignKey schema.foreignKey = foreignKey
schema.fieldName = foreignKey schema.fieldName = relatedPrimary
schema.main = true schema.main = true
const relatedSchema = cloneDeep(schema)
relatedSchema.fieldName = key relatedTable.schema[relatedField] = generateRelatedSchema(schema, table)
delete relatedSchema.main tableToSave.schema[foreignKey] = {
relatedTable.schema[relatedField] = relatedSchema type: FieldTypes.NUMBER,
constraints: {},
}
} }
} }