From 1cce9c3ddb6d95d200eb2b2cea9812cc3fcfc979 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Fri, 25 Nov 2022 12:37:48 +0800 Subject: [PATCH 1/2] fix(nocodb): convert attachment type in api response --- .../sql-data-mapper/lib/sql/BaseModelSqlv2.ts | 46 ++++++++++++++++--- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts index 5b2ebf9818..3181cf6ae0 100644 --- a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts +++ b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts @@ -99,9 +99,10 @@ class BaseModelSqlv2 { qb.where(_wherePk(this.model.primaryKeys, id)); - const data = (await this.extractRawQueryAndExec(qb))?.[0]; + let data = (await this.extractRawQueryAndExec(qb))?.[0]; if (data) { + data = this.convertAttachmentType(data); const proto = await this.getProto(); data.__proto__ = proto; } @@ -251,7 +252,8 @@ class BaseModelSqlv2 { if (!ignoreViewFilterAndSort) applyPaginate(qb, rest); const proto = await this.getProto(); - const data = await this.extractRawQueryAndExec(qb); + let data = await this.extractRawQueryAndExec(qb); + data = this.convertAttachmentType(data); return data?.map((d) => { d.__proto__ = proto; @@ -423,7 +425,8 @@ class BaseModelSqlv2 { .as('list') ); - const children = await this.extractRawQueryAndExec(childQb); + let children = await this.extractRawQueryAndExec(childQb); + children = this.convertAttachmentType(children); const proto = await ( await Model.getBaseModelSQL({ id: childTable.id, @@ -550,7 +553,8 @@ class BaseModelSqlv2 { await childModel.selectObject({ qb }); - const children = await this.extractRawQueryAndExec(qb); + let children = await this.extractRawQueryAndExec(qb); + children = this.convertAttachmentType(children); const proto = await ( await Model.getBaseModelSQL({ @@ -668,6 +672,7 @@ class BaseModelSqlv2 { ); let children = await this.extractRawQueryAndExec(finalQb); + children = this.convertAttachmentType(children); if (this.isMySQL) { children = children[0]; } @@ -735,7 +740,8 @@ class BaseModelSqlv2 { qb.limit(+rest?.limit || 25); qb.offset(+rest?.offset || 0); - const children = await this.extractRawQueryAndExec(qb); + let children = await this.extractRawQueryAndExec(qb); + children = this.convertAttachmentType(children); const proto = await ( await Model.getBaseModelSQL({ id: rtnId, dbDriver: this.dbDriver }) ).getProto(); @@ -1076,7 +1082,8 @@ class BaseModelSqlv2 { applyPaginate(qb, rest); const proto = await childModel.getProto(); - const data = await this.extractRawQueryAndExec(qb); + let data = await this.extractRawQueryAndExec(qb); + data = this.convertAttachmentType(data); return data.map((c) => { c.__proto__ = proto; @@ -1194,7 +1201,8 @@ class BaseModelSqlv2 { applyPaginate(qb, rest); const proto = await parentModel.getProto(); - const data = await this.extractRawQueryAndExec(qb); + let data = await this.extractRawQueryAndExec(qb); + data = this.convertAttachmentType(data); return data.map((c) => { c.__proto__ = proto; @@ -2726,6 +2734,30 @@ class BaseModelSqlv2 { ) : await this.dbDriver.raw(query); } + + private convertAttachmentType(data) { + // attachment is stored in text and parse in UI + // convertAttachmentType is used to convert the response in string to array of object in API response + if (data) { + const attachmentColumns = this.model.columns.filter( + (c) => c.uidt === UITypes.Attachment + ); + if (attachmentColumns.length) { + if (!Array.isArray(data)) { + data = [data]; + } + data = data.map((d) => { + attachmentColumns.forEach((col) => { + if (d[col.column_name] && typeof d[col.column_name] === 'string') { + d[col.column_name] = JSON.parse(d[col.column_name]); + } + }); + return d; + }); + } + } + return data; + } } function extractSortsObject( From 76148e92e79836ae8e266625163bb362366269f3 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Fri, 25 Nov 2022 14:04:24 +0800 Subject: [PATCH 2/2] fix(nocodb): column_name -> title --- .../src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts index 3181cf6ae0..b620d9e4df 100644 --- a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts +++ b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts @@ -2748,8 +2748,8 @@ class BaseModelSqlv2 { } data = data.map((d) => { attachmentColumns.forEach((col) => { - if (d[col.column_name] && typeof d[col.column_name] === 'string') { - d[col.column_name] = JSON.parse(d[col.column_name]); + if (d[col.title] && typeof d[col.title] === 'string') { + d[col.title] = JSON.parse(d[col.title]); } }); return d;