Browse Source

feat: when connecting external db populate rollup for LTAR and hide LTAR by default

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5848/head
Pranav C 2 years ago
parent
commit
423ff2265b
  1. 48
      packages/nocodb/src/helpers/populateMeta.ts
  2. 3
      packages/nocodb/src/services/bases.service.ts
  3. 3
      packages/nocodb/src/services/projects.service.ts

48
packages/nocodb/src/helpers/populateMeta.ts

@ -1,5 +1,6 @@
import { ModelTypes, UITypes, ViewTypes } from 'nocodb-sdk'; import { ModelTypes, UITypes, ViewTypes } from 'nocodb-sdk';
import { isVirtualCol, RelationTypes } from 'nocodb-sdk'; import { isVirtualCol, RelationTypes } from 'nocodb-sdk';
import { GridViewColumn } from '../models';
import Column from '../models/Column'; import Column from '../models/Column';
import Model from '../models/Model'; import Model from '../models/Model';
import NcConnectionMgrv2 from '../utils/common/NcConnectionMgrv2'; import NcConnectionMgrv2 from '../utils/common/NcConnectionMgrv2';
@ -9,6 +10,7 @@ import getTableNameAlias, { getColumnNameAlias } from '../helpers/getTableName';
import getColumnUiType from '../helpers/getColumnUiType'; import getColumnUiType from '../helpers/getColumnUiType';
import mapDefaultDisplayValue from '../helpers/mapDefaultDisplayValue'; import mapDefaultDisplayValue from '../helpers/mapDefaultDisplayValue';
import { getUniqueColumnAliasName } from './getUniqueName'; import { getUniqueColumnAliasName } from './getUniqueName';
import type { RollupColumn } from '../models';
import type LinkToAnotherRecordColumn from '../models/LinkToAnotherRecordColumn'; import type LinkToAnotherRecordColumn from '../models/LinkToAnotherRecordColumn';
import type Base from '../models/Base'; import type Base from '../models/Base';
import type Project from '../models/Project'; import type Project from '../models/Project';
@ -435,3 +437,49 @@ export async function populateMeta(base: Base, project: Project): Promise<any> {
return info; return info;
} }
export async function populateRollupColumnAndHideLTAR(
base: Base,
project: Project,
) {
for (const model of await Model.list({
project_id: project.id,
base_id: base.id,
})) {
const columns = await model.getColumns();
const hmAndMmLTARColumns = columns.filter(
(c) =>
c.uidt === UITypes.LinkToAnotherRecord &&
c.colOptions.type !== RelationTypes.BELONGS_TO &&
!c.system,
);
const views = await model.getViews();
for (const column of hmAndMmLTARColumns) {
const relatedModel = await column
.getColOptions<LinkToAnotherRecordColumn>()
.then((colOpt) => colOpt.getRelatedTable());
await relatedModel.getColumns();
const pkId =
relatedModel.primaryKey?.id || (await relatedModel.getColumns())[0]?.id;
await Column.insert<RollupColumn>({
uidt: 'Rollup',
title: getUniqueColumnAliasName(
await model.getColumns(),
`${relatedModel.title} Count`,
),
fk_rollup_column_id: pkId,
fk_model_id: model.id,
rollup_function: 'count',
fk_relation_column_id: column.id,
});
const viewCol = await GridViewColumn.list(views[0].id).then((cols) =>
cols.find((c) => c.fk_column_id === column.id),
);
await GridViewColumn.update(viewCol.id, { show: false });
}
}
}

3
packages/nocodb/src/services/bases.service.ts

@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { T } from 'nc-help'; import { T } from 'nc-help';
import { populateMeta, validatePayload } from '../helpers'; import { populateMeta, validatePayload } from '../helpers';
import { populateRollupColumnAndHideLTAR } from '../helpers/populateMeta'
import { syncBaseMigration } from '../helpers/syncMigration'; import { syncBaseMigration } from '../helpers/syncMigration';
import { Base, Project } from '../models'; import { Base, Project } from '../models';
import type { BaseReqType } from 'nocodb-sdk'; import type { BaseReqType } from 'nocodb-sdk';
@ -69,6 +70,8 @@ export class BasesService {
const info = await populateMeta(base, project); const info = await populateMeta(base, project);
await populateRollupColumnAndHideLTAR(base, project);
T.emit('evt_api_created', info); T.emit('evt_api_created', info);
delete base.config; delete base.config;

3
packages/nocodb/src/services/projects.service.ts

@ -7,6 +7,7 @@ import { OrgUserRoles } from 'nocodb-sdk';
import { populateMeta, validatePayload } from '../helpers'; import { populateMeta, validatePayload } from '../helpers';
import { NcError } from '../helpers/catchError'; import { NcError } from '../helpers/catchError';
import { extractPropsAndSanitize } from '../helpers/extractProps'; import { extractPropsAndSanitize } from '../helpers/extractProps';
import { populateRollupColumnAndHideLTAR } from '../helpers/populateMeta'
import syncMigration from '../helpers/syncMigration'; import syncMigration from '../helpers/syncMigration';
import { Project, ProjectUser } from '../models'; import { Project, ProjectUser } from '../models';
import Noco from '../Noco'; import Noco from '../Noco';
@ -167,6 +168,8 @@ export class ProjectsService {
for (const base of await project.getBases()) { for (const base of await project.getBases()) {
const info = await populateMeta(base, project); const info = await populateMeta(base, project);
await populateRollupColumnAndHideLTAR(base, project);
T.emit('evt_api_created', info); T.emit('evt_api_created', info);
delete base.config; delete base.config;
} }

Loading…
Cancel
Save