Browse Source

fix(nocodb): convert attachment type in api response

pull/4488/head
Wing-Kam Wong 2 years ago
parent
commit
1cce9c3ddb
  1. 46
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

46
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)); qb.where(_wherePk(this.model.primaryKeys, id));
const data = (await this.extractRawQueryAndExec(qb))?.[0]; let data = (await this.extractRawQueryAndExec(qb))?.[0];
if (data) { if (data) {
data = this.convertAttachmentType(data);
const proto = await this.getProto(); const proto = await this.getProto();
data.__proto__ = proto; data.__proto__ = proto;
} }
@ -251,7 +252,8 @@ class BaseModelSqlv2 {
if (!ignoreViewFilterAndSort) applyPaginate(qb, rest); if (!ignoreViewFilterAndSort) applyPaginate(qb, rest);
const proto = await this.getProto(); 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) => { return data?.map((d) => {
d.__proto__ = proto; d.__proto__ = proto;
@ -423,7 +425,8 @@ class BaseModelSqlv2 {
.as('list') .as('list')
); );
const children = await this.extractRawQueryAndExec(childQb); let children = await this.extractRawQueryAndExec(childQb);
children = this.convertAttachmentType(children);
const proto = await ( const proto = await (
await Model.getBaseModelSQL({ await Model.getBaseModelSQL({
id: childTable.id, id: childTable.id,
@ -550,7 +553,8 @@ class BaseModelSqlv2 {
await childModel.selectObject({ qb }); await childModel.selectObject({ qb });
const children = await this.extractRawQueryAndExec(qb); let children = await this.extractRawQueryAndExec(qb);
children = this.convertAttachmentType(children);
const proto = await ( const proto = await (
await Model.getBaseModelSQL({ await Model.getBaseModelSQL({
@ -668,6 +672,7 @@ class BaseModelSqlv2 {
); );
let children = await this.extractRawQueryAndExec(finalQb); let children = await this.extractRawQueryAndExec(finalQb);
children = this.convertAttachmentType(children);
if (this.isMySQL) { if (this.isMySQL) {
children = children[0]; children = children[0];
} }
@ -735,7 +740,8 @@ class BaseModelSqlv2 {
qb.limit(+rest?.limit || 25); qb.limit(+rest?.limit || 25);
qb.offset(+rest?.offset || 0); qb.offset(+rest?.offset || 0);
const children = await this.extractRawQueryAndExec(qb); let children = await this.extractRawQueryAndExec(qb);
children = this.convertAttachmentType(children);
const proto = await ( const proto = await (
await Model.getBaseModelSQL({ id: rtnId, dbDriver: this.dbDriver }) await Model.getBaseModelSQL({ id: rtnId, dbDriver: this.dbDriver })
).getProto(); ).getProto();
@ -1076,7 +1082,8 @@ class BaseModelSqlv2 {
applyPaginate(qb, rest); applyPaginate(qb, rest);
const proto = await childModel.getProto(); 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) => { return data.map((c) => {
c.__proto__ = proto; c.__proto__ = proto;
@ -1194,7 +1201,8 @@ class BaseModelSqlv2 {
applyPaginate(qb, rest); applyPaginate(qb, rest);
const proto = await parentModel.getProto(); 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) => { return data.map((c) => {
c.__proto__ = proto; c.__proto__ = proto;
@ -2726,6 +2734,30 @@ class BaseModelSqlv2 {
) )
: await this.dbDriver.raw(query); : 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( function extractSortsObject(

Loading…
Cancel
Save