From b1c55be1b7fb74ff8a89d590a7e617ed8a1d28fd Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 8 Nov 2021 18:23:48 +0000 Subject: [PATCH] Fixing some issues with multi-select coming back as string. --- .../api/controllers/row/ExternalRequest.ts | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index d4e8d475a2..7222fff0d9 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -163,8 +163,8 @@ module External { } } - function basicProcessing(row: Row, table: Table) { - const thisRow: { [key: string]: any } = {} + function basicProcessing(row: Row, table: Table): Row { + const thisRow: Row = {} // filter the row down to what is actually the row (not joined) for (let fieldName of Object.keys(table.schema)) { const value = row[`${table.name}.${fieldName}`] || row[fieldName] @@ -179,6 +179,20 @@ module External { return thisRow } + function fixArrayTypes(row: Row, table: Table) { + for (let [fieldName, schema] of Object.entries(table.schema)) { + if (schema.type === FieldTypes.ARRAY && typeof row[fieldName] === "string") { + try { + row[fieldName] = JSON.parse(row[fieldName]) + } catch (err) { + // couldn't convert back to array, ignore + delete row[fieldName] + } + } + } + return row + } + function isMany(field: FieldSchema) { return ( field.relationshipType && field.relationshipType.split("-")[0] === "many" @@ -358,7 +372,10 @@ module External { ) continue } - const thisRow = basicProcessing(row, table) + const thisRow = fixArrayTypes(basicProcessing(row, table), table) + if (thisRow._id == null) { + throw "Unable to generate row ID for SQL rows" + } finalRows[thisRow._id] = thisRow // do this at end once its been added to the final rows finalRows = this.updateRelationshipColumns(