Browse Source

feat: on column LTAR creation - hide LTAR and add rollup(MM and HM)

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5848/head
Pranav C 1 year ago
parent
commit
b4727b5a18
  1. 40
      packages/nocodb/src/helpers/columnHelpers.ts
  2. 23
      packages/nocodb/src/services/columns.service.ts

40
packages/nocodb/src/helpers/columnHelpers.ts

@ -1,5 +1,6 @@
import { customAlphabet } from 'nanoid'; import { customAlphabet } from 'nanoid';
import { UITypes } from 'nocodb-sdk'; import { UITypes } from 'nocodb-sdk';
import { GridViewColumn, RollupColumn } from '../models'
import Column from '../models/Column'; import Column from '../models/Column';
import { getUniqueColumnAliasName } from '../helpers/getUniqueName'; import { getUniqueColumnAliasName } from '../helpers/getUniqueName';
import validateParams from '../helpers/validateParams'; import validateParams from '../helpers/validateParams';
@ -61,7 +62,7 @@ export async function createHmAndBtColumn(
await parent.getColumns(), await parent.getColumns(),
type === 'hm' ? alias : `${child.title} List`, type === 'hm' ? alias : `${child.title} List`,
); );
await Column.insert({ const col = await Column.insert({
title, title,
fk_model_id: parent.id, fk_model_id: parent.id,
uidt: UITypes.LinkToAnotherRecord, uidt: UITypes.LinkToAnotherRecord,
@ -74,6 +75,11 @@ export async function createHmAndBtColumn(
fk_col_name: fkColName, fk_col_name: fkColName,
fk_index_name: fkColName, fk_index_name: fkColName,
}); });
if (!isSystemCol)
await populateRollupForLTAR({
column: col,
});
} }
} }
@ -206,3 +212,35 @@ export const generateFkName = (parent: TableType, child: TableType) => {
.slice(0, 10)}_${randomID(15)}`; .slice(0, 10)}_${randomID(15)}`;
return constraintName; return constraintName;
}; };
export async function populateRollupForLTAR({ column }: { column: Column }) {
const model = await column.getModel();
const views = await model.getViews();
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 });
}

23
packages/nocodb/src/services/columns.service.ts

@ -12,13 +12,13 @@ import formulaQueryBuilderv2 from '../db/formulav2/formulaQueryBuilderv2';
import ProjectMgrv2 from '../db/sql-mgr/v2/ProjectMgrv2'; import ProjectMgrv2 from '../db/sql-mgr/v2/ProjectMgrv2';
import { import {
createHmAndBtColumn, createHmAndBtColumn,
generateFkName, generateFkName, populateRollupForLTAR,
randomID, randomID,
validateLookupPayload, validateLookupPayload,
validatePayload, validatePayload,
validateRequiredField, validateRequiredField,
validateRollupPayload, validateRollupPayload,
} from '../helpers'; } from '../helpers'
import { NcError } from '../helpers/catchError'; import { NcError } from '../helpers/catchError';
import getColumnPropsFromUIDT from '../helpers/getColumnPropsFromUIDT'; import getColumnPropsFromUIDT from '../helpers/getColumnPropsFromUIDT';
import { import {
@ -32,6 +32,7 @@ import {
Base, Base,
Column, Column,
FormulaColumn, FormulaColumn,
GridViewColumn,
KanbanView, KanbanView,
Model, Model,
} from '../models'; } from '../models';
@ -39,7 +40,11 @@ import Noco from '../Noco';
import NcConnectionMgrv2 from '../utils/common/NcConnectionMgrv2'; import NcConnectionMgrv2 from '../utils/common/NcConnectionMgrv2';
import { MetaTable } from '../utils/globals'; import { MetaTable } from '../utils/globals';
import { MetaService } from '../meta/meta.service'; import { MetaService } from '../meta/meta.service';
import type { LinkToAnotherRecordColumn, Project } from '../models'; import type {
LinkToAnotherRecordColumn,
Project,
RollupColumn,
} from '../models';
import type SqlMgrv2 from '../db/sql-mgr/v2/SqlMgrv2'; import type SqlMgrv2 from '../db/sql-mgr/v2/SqlMgrv2';
import type { import type {
ColumnReqType, ColumnReqType,
@ -1759,7 +1764,7 @@ export class ColumnsService {
true, true,
); );
await Column.insert({ const col1 = await Column.insert({
title: getUniqueColumnAliasName( title: getUniqueColumnAliasName(
await child.getColumns(), await child.getColumns(),
`${parent.title} List`, `${parent.title} List`,
@ -1780,7 +1785,7 @@ export class ColumnsService {
fk_related_model_id: parent.id, fk_related_model_id: parent.id,
virtual: (param.column as LinkToAnotherColumnReqType).virtual, virtual: (param.column as LinkToAnotherColumnReqType).virtual,
}); });
await Column.insert({ const col2 = await Column.insert({
title: getUniqueColumnAliasName( title: getUniqueColumnAliasName(
await parent.getColumns(), await parent.getColumns(),
param.column.title ?? `${child.title} List`, param.column.title ?? `${child.title} List`,
@ -1801,6 +1806,13 @@ export class ColumnsService {
virtual: (param.column as LinkToAnotherColumnReqType).virtual, virtual: (param.column as LinkToAnotherColumnReqType).virtual,
}); });
await populateRollupForLTAR({
column: col1,
})
await populateRollupForLTAR({
column: col2,
})
// todo: create index for virtual relations as well // todo: create index for virtual relations as well
// create index for foreign key in pg // create index for foreign key in pg
if (param.base.type === 'pg') { if (param.base.type === 'pg') {
@ -1869,4 +1881,5 @@ export class ColumnsService {
await Column.update(column.id, colBody); await Column.update(column.id, colBody);
} }
} }
} }

Loading…
Cancel
Save