From b507a1def3657dfeba1065f3970b4e9df0295fdf Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Sat, 4 Mar 2023 12:34:44 +0800 Subject: [PATCH] fix(nocodb): type errors --- .../lib/sql/genRollupSelectv2.ts | 4 ++-- .../nocodb/src/lib/models/RollupColumn.ts | 19 +++++++++++-------- .../ncProjectUpgraderV2_0090000.ts | 5 +++-- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/genRollupSelectv2.ts b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/genRollupSelectv2.ts index f1d52f357f..d5631ee3cb 100644 --- a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/genRollupSelectv2.ts +++ b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/genRollupSelectv2.ts @@ -29,7 +29,7 @@ export default async function ({ case RelationTypes.HAS_MANY: return { builder: knex(`${childModel?.table_name} as ${refTableAlias}`) - [columnOptions.rollup_function]?.( + [columnOptions.rollup_function as string]?.( knex.ref(`${refTableAlias}.${rollupColumn.column_name}`) ) .where( @@ -47,7 +47,7 @@ export default async function ({ return { builder: knex(`${parentModel?.table_name} as ${refTableAlias}`) - [columnOptions.rollup_function]?.( + [columnOptions.rollup_function as string]?.( knex.ref(`${refTableAlias}.${rollupColumn.column_name}`) ) .innerJoin( diff --git a/packages/nocodb/src/lib/models/RollupColumn.ts b/packages/nocodb/src/lib/models/RollupColumn.ts index a9194dd469..3781cc4a7e 100644 --- a/packages/nocodb/src/lib/models/RollupColumn.ts +++ b/packages/nocodb/src/lib/models/RollupColumn.ts @@ -5,19 +5,22 @@ import NocoCache from '../cache/NocoCache'; import { extractProps } from '../meta/helpers/extractProps'; import type { RollupType } from 'nocodb-sdk'; +export const ROLLUP_FUNCTIONS = [ + 'count', + 'min', + 'max', + 'avg', + 'countDistinct', + 'sumDistinct', + 'avgDistinct', +]; + export default class RollupColumn implements RollupType { id: string; fk_column_id; fk_relation_column_id; fk_rollup_column_id; - rollup_function: - | 'count' - | 'min' - | 'max' - | 'avg' - | 'countDistinct' - | 'sumDistinct' - | 'avgDistinct'; + rollup_function: typeof ROLLUP_FUNCTIONS[number]; constructor(data: Partial) { Object.assign(this, data); diff --git a/packages/nocodb/src/lib/version-upgrader/ncProjectUpgraderV2_0090000.ts b/packages/nocodb/src/lib/version-upgrader/ncProjectUpgraderV2_0090000.ts index 4bd94c8574..49433e8475 100644 --- a/packages/nocodb/src/lib/version-upgrader/ncProjectUpgraderV2_0090000.ts +++ b/packages/nocodb/src/lib/version-upgrader/ncProjectUpgraderV2_0090000.ts @@ -14,7 +14,7 @@ import { import Column from '../models/Column'; import LinkToAnotherRecordColumn from '../models/LinkToAnotherRecordColumn'; import NcHelp from '../utils/NcHelp'; -import RollupColumn from '../models/RollupColumn'; +import RollupColumn, { ROLLUP_FUNCTIONS } from '../models/RollupColumn'; import View from '../models/View'; import GridView from '../models/GridView'; import KanbanView from '../models/KanbanView'; @@ -613,7 +613,8 @@ async function migrateProjectModels( const colBody: Partial = { title: columnMeta._cn, - rollup_function: columnMeta.rl.fn, + rollup_function: columnMeta.rl + .fn as typeof ROLLUP_FUNCTIONS[number], }; colBody.fk_rollup_column_id =