Getting writing of one and many rows working.

This commit is contained in:
mike12345567 2021-06-30 14:46:44 +01:00
parent 9ca36893ad
commit 9780f2a788
3 changed files with 69 additions and 63 deletions

View File

@ -70,9 +70,10 @@ async function handleRequest(
makeExternalQuery(appId, { makeExternalQuery(appId, {
endpoint: { endpoint: {
...json.endpoint, ...json.endpoint,
entityId: processObjectSync(tableName, row), entityId: tableName,
}, },
body: toInsert, // if we're doing many relationships then we're writing, only one response
body: processObjectSync(toInsert, response[0]),
}) })
) )
} }

View File

@ -13,34 +13,39 @@ exports.inputProcessing = (row, table, allTables) => {
let newRow = {}, let newRow = {},
manyRelationships = [] manyRelationships = []
for (let [key, field] of Object.entries(table.schema)) { for (let [key, field] of Object.entries(table.schema)) {
// currently excludes empty strings // if set already, or not set just skip it
if (!row[key]) { if (!row[key] || newRow[key]) {
continue continue
} }
const isLink = field.type === FieldTypes.LINK // if its not a link then just copy it over
if (isLink && !field.through) { if (field.type !== FieldTypes.LINK) {
newRow[key] = row[key]
continue
}
const { tableName: linkTableName } = breakExternalTableId(field.tableId)
// table has to exist for many to many
if (!allTables[linkTableName]) {
continue
}
const linkTable = allTables[linkTableName]
if (!field.through) {
// we don't really support composite keys for relationships, this is why [0] is used // we don't really support composite keys for relationships, this is why [0] is used
newRow[key] = breakRowIdField(row[key][0])[0] newRow[field.foreignKey || linkTable.primary] = breakRowIdField(row[key][0])[0]
} else if (isLink && field.through) { } else {
const { tableName: linkTableName } = breakExternalTableId(field.tableId)
// table has to exist for many to many
if (!allTables[linkTableName]) {
continue
}
const linkTable = allTables[linkTableName]
row[key].map(relationship => { row[key].map(relationship => {
// we don't really support composite keys for relationships, this is why [0] is used // we don't really support composite keys for relationships, this is why [0] is used
manyRelationships.push({ manyRelationships.push({
tableId: field.through, tableId: field.through,
[linkTable.primary]: breakRowIdField(relationship)[0], [linkTable.primary]: breakRowIdField(relationship)[0],
// leave the ID for enrichment later // leave the ID for enrichment later
[table.primary]: `{{ _id }}`, [table.primary]: `{{ ${table.primary} }}`,
}) })
}) })
} else {
newRow[key] = row[key]
} }
} }
// we return the relationships that may need to be created in the through table
// we do this so that if the ID is generated by the DB it can be inserted
// after the fact
return { row: newRow, manyRelationships } return { row: newRow, manyRelationships }
} }

View File

@ -176,52 +176,52 @@ module PostgresModule {
} }
// TODO: hack for testing // TODO: hack for testing
if (tableName === "persons") { // if (tableName === "persons") {
tables[tableName].primaryDisplay = "firstname" // tables[tableName].primaryDisplay = "firstname"
} // }
if (tableName === "products") { // if (tableName === "products") {
tables[tableName].primaryDisplay = "productname" // tables[tableName].primaryDisplay = "productname"
} // }
if (tableName === "tasks") { // if (tableName === "tasks") {
tables[tableName].primaryDisplay = "taskname" // tables[tableName].primaryDisplay = "taskname"
} // }
if (tableName === "products") { // if (tableName === "products") {
tables[tableName].schema["tasks"] = { // tables[tableName].schema["tasks"] = {
name: "tasks", // name: "tasks",
type: "link", // type: "link",
tableId: buildExternalTableId(datasourceId, "tasks"), // tableId: buildExternalTableId(datasourceId, "tasks"),
relationshipType: "many-to-many", // relationshipType: "many-to-many",
through: buildExternalTableId(datasourceId, "products_tasks"), // through: buildExternalTableId(datasourceId, "products_tasks"),
fieldName: "taskid", // fieldName: "taskid",
} // }
} // }
if (tableName === "persons") { // if (tableName === "persons") {
tables[tableName].schema["tasks"] = { // tables[tableName].schema["tasks"] = {
name: "tasks", // name: "tasks",
type: "link", // type: "link",
tableId: buildExternalTableId(datasourceId, "tasks"), // tableId: buildExternalTableId(datasourceId, "tasks"),
relationshipType: "many-to-one", // relationshipType: "many-to-one",
fieldName: "personid", // fieldName: "personid",
} // }
} // }
if (tableName === "tasks") { // if (tableName === "tasks") {
tables[tableName].schema["products"] = { // tables[tableName].schema["products"] = {
name: "products", // name: "products",
type: "link", // type: "link",
tableId: buildExternalTableId(datasourceId, "products"), // tableId: buildExternalTableId(datasourceId, "products"),
relationshipType: "many-to-many", // relationshipType: "many-to-many",
through: buildExternalTableId(datasourceId, "products_tasks"), // through: buildExternalTableId(datasourceId, "products_tasks"),
fieldName: "productid", // fieldName: "productid",
} // }
tables[tableName].schema["people"] = { // tables[tableName].schema["people"] = {
name: "people", // name: "people",
type: "link", // type: "link",
tableId: buildExternalTableId(datasourceId, "persons"), // tableId: buildExternalTableId(datasourceId, "persons"),
relationshipType: "one-to-many", // relationshipType: "one-to-many",
fieldName: "personid", // fieldName: "personid",
foreignKey: "personid", // foreignKey: "personid",
} // }
} // }
} }
this.tables = tables this.tables = tables
} }