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

Loading…
Cancel
Save