Browse Source

Merge pull request #4612 from nocodb/refactor/convertAttachmentType

refactor(nocodb): move convertAttachmentType to extractRawQueryAndExec
pull/4692/head
աɨռɢӄաօռɢ 2 years ago committed by GitHub
parent
commit
80d2faf1f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 66
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

66
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

@ -100,10 +100,9 @@ class BaseModelSqlv2 {
qb.where(_wherePk(this.model.primaryKeys, id)); qb.where(_wherePk(this.model.primaryKeys, id));
let data = (await this.extractRawQueryAndExec(qb))?.[0]; let data = (await this.execAndParse(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;
} }
@ -162,7 +161,6 @@ class BaseModelSqlv2 {
let data = await qb.first(); let data = await qb.first();
if (data) { if (data) {
data = this.convertAttachmentType(data);
const proto = await this.getProto(); const proto = await this.getProto();
data.__proto__ = proto; data.__proto__ = proto;
} }
@ -254,8 +252,7 @@ class BaseModelSqlv2 {
if (!ignoreViewFilterAndSort) applyPaginate(qb, rest); if (!ignoreViewFilterAndSort) applyPaginate(qb, rest);
const proto = await this.getProto(); const proto = await this.getProto();
let data = await this.extractRawQueryAndExec(qb); let data = await this.execAndParse(qb);
data = this.convertAttachmentType(data);
return data?.map((d) => { return data?.map((d) => {
d.__proto__ = proto; d.__proto__ = proto;
@ -367,7 +364,7 @@ class BaseModelSqlv2 {
qb.groupBy(args.column_name); qb.groupBy(args.column_name);
if (sorts) await sortV2(sorts, qb, this.dbDriver); if (sorts) await sortV2(sorts, qb, this.dbDriver);
applyPaginate(qb, rest); applyPaginate(qb, rest);
return this.convertAttachmentType(await qb); return await qb;
} }
async multipleHmList({ colId, ids }, args: { limit?; offset? } = {}) { async multipleHmList({ colId, ids }, args: { limit?; offset? } = {}) {
@ -426,8 +423,7 @@ class BaseModelSqlv2 {
.as('list') .as('list')
); );
let children = await this.extractRawQueryAndExec(childQb); let children = await this.execAndParse(childQb, childTable);
children = this.convertAttachmentType(children);
const proto = await ( const proto = await (
await Model.getBaseModelSQL({ await Model.getBaseModelSQL({
id: childTable.id, id: childTable.id,
@ -554,8 +550,7 @@ class BaseModelSqlv2 {
await childModel.selectObject({ qb }); await childModel.selectObject({ qb });
let children = await this.extractRawQueryAndExec(qb); let children = await this.execAndParse(qb, childTable);
children = this.convertAttachmentType(children);
const proto = await ( const proto = await (
await Model.getBaseModelSQL({ await Model.getBaseModelSQL({
@ -672,11 +667,10 @@ class BaseModelSqlv2 {
!this.isSqlite !this.isSqlite
); );
let children = await this.extractRawQueryAndExec(finalQb); let children = await this.execAndParse(finalQb, childTable);
if (this.isMySQL) { if (this.isMySQL) {
children = children[0]; children = children[0];
} }
children = this.convertAttachmentType(children);
const proto = await ( const proto = await (
await Model.getBaseModelSQL({ await Model.getBaseModelSQL({
id: rtnId, id: rtnId,
@ -741,8 +735,7 @@ class BaseModelSqlv2 {
qb.limit(+rest?.limit || 25); qb.limit(+rest?.limit || 25);
qb.offset(+rest?.offset || 0); qb.offset(+rest?.offset || 0);
let children = await this.extractRawQueryAndExec(qb); let children = await this.execAndParse(qb, childTable);
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();
@ -969,7 +962,6 @@ class BaseModelSqlv2 {
const proto = await childModel.getProto(); const proto = await childModel.getProto();
let data = await qb; let data = await qb;
data = this.convertAttachmentType(data);
return data.map((c) => { return data.map((c) => {
c.__proto__ = proto; c.__proto__ = proto;
return c; return c;
@ -1083,8 +1075,7 @@ class BaseModelSqlv2 {
applyPaginate(qb, rest); applyPaginate(qb, rest);
const proto = await childModel.getProto(); const proto = await childModel.getProto();
let data = await this.extractRawQueryAndExec(qb); let data = await this.execAndParse(qb, childTable);
data = this.convertAttachmentType(data);
return data.map((c) => { return data.map((c) => {
c.__proto__ = proto; c.__proto__ = proto;
@ -1202,8 +1193,7 @@ class BaseModelSqlv2 {
applyPaginate(qb, rest); applyPaginate(qb, rest);
const proto = await parentModel.getProto(); const proto = await parentModel.getProto();
let data = await this.extractRawQueryAndExec(qb); let data = await this.execAndParse(qb, childTable);
data = this.convertAttachmentType(data);
return data.map((c) => { return data.map((c) => {
c.__proto__ = proto; c.__proto__ = proto;
@ -1535,7 +1525,7 @@ class BaseModelSqlv2 {
query.returning( query.returning(
`${this.model.primaryKey.column_name} as ${this.model.primaryKey.title}` `${this.model.primaryKey.column_name} as ${this.model.primaryKey.title}`
); );
response = await this.extractRawQueryAndExec(query); response = await this.execAndParse(query);
} }
const ai = this.model.columns.find((c) => c.ai); const ai = this.model.columns.find((c) => c.ai);
@ -1545,7 +1535,7 @@ class BaseModelSqlv2 {
// handle if autogenerated primary key is used // handle if autogenerated primary key is used
if (ag) { if (ag) {
if (!response) await this.extractRawQueryAndExec(query); if (!response) await this.execAndParse(query);
response = await this.readByPk(data[ag.title]); response = await this.readByPk(data[ag.title]);
} else if ( } else if (
!response || !response ||
@ -1555,7 +1545,7 @@ class BaseModelSqlv2 {
if (response?.length) { if (response?.length) {
id = response[0]; id = response[0];
} else { } else {
const res = await this.extractRawQueryAndExec(query); const res = await this.execAndParse(query);
id = res?.id ?? res[0]?.insertId; id = res?.id ?? res[0]?.insertId;
} }
@ -1660,7 +1650,7 @@ class BaseModelSqlv2 {
.update(updateObj) .update(updateObj)
.where(await this._wherePk(id)); .where(await this._wherePk(id));
await this.extractRawQueryAndExec(query); await this.execAndParse(query);
const response = await this.readByPk(id); const response = await this.readByPk(id);
await this.afterUpdate(response, trx, cookie); await this.afterUpdate(response, trx, cookie);
@ -2667,7 +2657,6 @@ class BaseModelSqlv2 {
const proto = await this.getProto(); const proto = await this.getProto();
let data = await groupedQb; let data = await groupedQb;
data = this.convertAttachmentType(data);
const result = data?.map((d) => { const result = data?.map((d) => {
d.__proto__ = proto; d.__proto__ = proto;
return d; return d;
@ -2770,38 +2759,51 @@ class BaseModelSqlv2 {
return await qb; return await qb;
} }
private async extractRawQueryAndExec(qb: Knex.QueryBuilder) { private async execAndParse(
qb: Knex.QueryBuilder,
childTable?: Model
) {
let query = qb.toQuery(); let query = qb.toQuery();
if (!this.isPg && !this.isMssql) { if (!this.isPg && !this.isMssql) {
query = unsanitize(qb.toQuery()); query = unsanitize(qb.toQuery());
} else { } else {
query = sanitize(query); query = sanitize(query);
} }
return this.isPg return this.convertAttachmentType(
this.isPg
? (await this.dbDriver.raw(query))?.rows ? (await this.dbDriver.raw(query))?.rows
: query.slice(0, 6) === 'select' && !this.isMssql : query.slice(0, 6) === 'select' && !this.isMssql
? await this.dbDriver.from( ? await this.dbDriver.from(
this.dbDriver.raw(query).wrap('(', ') __nc_alias') this.dbDriver.raw(query).wrap('(', ') __nc_alias')
) )
: await this.dbDriver.raw(query); : await this.dbDriver.raw(query),
childTable
);
} }
private _convertAttachmentType(attachmentColumns, d) { private _convertAttachmentType(
attachmentColumns: Record<string, any>[],
d: Record<string, any>
) {
try {
if (d) {
attachmentColumns.forEach((col) => { attachmentColumns.forEach((col) => {
if (d[col.title] && typeof d[col.title] === 'string') { if (d[col.title] && typeof d[col.title] === 'string') {
d[col.title] = JSON.parse(d[col.title]); d[col.title] = JSON.parse(d[col.title]);
} }
}); });
}
} catch {}
return d; return d;
} }
private convertAttachmentType(data) { private convertAttachmentType(data: Record<string, any>, childTable?: Model) {
// attachment is stored in text and parse in UI // 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 // convertAttachmentType is used to convert the response in string to array of object in API response
if (data) { if (data) {
const attachmentColumns = this.model.columns.filter( const attachmentColumns = (
(c) => c.uidt === UITypes.Attachment childTable ? childTable.columns : this.model.columns
); ).filter((c) => c.uidt === UITypes.Attachment);
if (attachmentColumns.length) { if (attachmentColumns.length) {
if (Array.isArray(data)) { if (Array.isArray(data)) {
data = data.map((d) => data = data.map((d) =>

Loading…
Cancel
Save