1
0
Fork 0
mirror of synced 2024-10-01 17:47:46 +13:00

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, {
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 = {},
manyRelationships = []
for (let [key, field] of Object.entries(table.schema)) {
// currently excludes empty strings
if (!row[key]) {
// if set already, or not set just skip it
if (!row[key] || newRow[key]) {
continue
}
const isLink = field.type === FieldTypes.LINK
if (isLink && !field.through) {
// if its not a link then just copy it over
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
newRow[key] = breakRowIdField(row[key][0])[0]
} else if (isLink && field.through) {
const { tableName: linkTableName } = breakExternalTableId(field.tableId)
// table has to exist for many to many
if (!allTables[linkTableName]) {
continue
}
const linkTable = allTables[linkTableName]
newRow[field.foreignKey || linkTable.primary] = breakRowIdField(row[key][0])[0]
} else {
row[key].map(relationship => {
// we don't really support composite keys for relationships, this is why [0] is used
manyRelationships.push({
tableId: field.through,
[linkTable.primary]: breakRowIdField(relationship)[0],
// 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 }
}

View file

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