Browse Source

refactor: remove duplicate logic

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

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

@ -1381,10 +1381,10 @@ class BaseModelSqlv2 {
} }
async hasLTARData(rowId, model: Model): Promise<any> { async hasLTARData(rowId, model: Model): Promise<any> {
const res = [];
const LTARColumns = (await model.getColumns()).filter( const LTARColumns = (await model.getColumns()).filter(
(c) => c.uidt === UITypes.LinkToAnotherRecord (c) => c.uidt === UITypes.LinkToAnotherRecord
); );
const res = [];
let i = 0; let i = 0;
for (const column of LTARColumns) { for (const column of LTARColumns) {
const colOptions = const colOptions =
@ -1395,32 +1395,28 @@ class BaseModelSqlv2 {
await childModel.getColumns(); await childModel.getColumns();
const parentModel = await parentColumn.getModel(); const parentModel = await parentColumn.getModel();
await parentModel.getColumns(); await parentModel.getColumns();
let cnt = 0;
if (colOptions.type === RelationTypes.HAS_MANY) { if (colOptions.type === RelationTypes.HAS_MANY) {
const selectHmCount = await this.dbDriver(childModel.table_name) cnt = +(
.count(childColumn.column_name, { as: 'cnt' }) await this.dbDriver(childModel.table_name)
.where(childColumn.column_name, rowId); .count(childColumn.column_name, { as: 'cnt' })
const cnt = (await selectHmCount)[0].cnt; .where(childColumn.column_name, rowId)
if (cnt) { )[0].cnt;
res.push(
`${i++ + 1}. ${model.title}.${
column.title
} is a LinkToAnotherRecord of ${childModel.title}`
);
}
} else if (colOptions.type === RelationTypes.MANY_TO_MANY) { } else if (colOptions.type === RelationTypes.MANY_TO_MANY) {
const mmModel = await colOptions.getMMModel(); const mmModel = await colOptions.getMMModel();
const mmChildColumn = await colOptions.getMMChildColumn(); const mmChildColumn = await colOptions.getMMChildColumn();
const selectMmCount = await this.dbDriver(mmModel.table_name) cnt = +(
.where(`${mmModel.table_name}.${mmChildColumn.column_name}`, rowId) await this.dbDriver(mmModel.table_name)
.count(mmChildColumn.column_name, { as: 'cnt' }); .where(`${mmModel.table_name}.${mmChildColumn.column_name}`, rowId)
const cnt = (await selectMmCount)[0].cnt; .count(mmChildColumn.column_name, { as: 'cnt' })
if (cnt) { )[0].cnt;
res.push( }
`${i++ + 1}. ${model.title}.${ if (cnt) {
column.title res.push(
} is a LinkToAnotherRecord of ${parentModel.title}` `${i++ + 1}. ${model.title}.${
); column.title
} } is a LinkToAnotherRecord of ${childModel.title}`
);
} }
} }
return res; return res;

Loading…
Cancel
Save