From 0b4b59edcc31e93ecafda7ed2db5f81e9352ea33 Mon Sep 17 00:00:00 2001 From: mertmit Date: Sat, 11 Nov 2023 02:38:50 +0300 Subject: [PATCH] feat: env variables to control pagination of group by Signed-off-by: mertmit --- .../nc-gui/composables/useGlobal/types.ts | 4 ++ packages/nc-gui/composables/useViewGroupBy.ts | 42 ++++++++++++------- .../020.environment-variables.md | 2 + .../src/helpers/extractLimitAndOffset.ts | 5 +++ packages/nocodb/src/services/utils.service.ts | 6 ++- 5 files changed, 43 insertions(+), 16 deletions(-) diff --git a/packages/nc-gui/composables/useGlobal/types.ts b/packages/nc-gui/composables/useGlobal/types.ts index 4a4f9caeb1..f7e640cd7c 100644 --- a/packages/nc-gui/composables/useGlobal/types.ts +++ b/packages/nc-gui/composables/useGlobal/types.ts @@ -9,6 +9,10 @@ export interface AppInfo { authType: 'jwt' | 'none' connectToExternalDB: boolean defaultLimit: number + defaultGroupByLimit: { + limitGroup: number + limitRecord: number + } firstUser: boolean githubAuthEnabled: boolean googleAuthEnabled: boolean diff --git a/packages/nc-gui/composables/useViewGroupBy.ts b/packages/nc-gui/composables/useViewGroupBy.ts index 55bcfc4358..809578dd1c 100644 --- a/packages/nc-gui/composables/useViewGroupBy.ts +++ b/packages/nc-gui/composables/useViewGroupBy.ts @@ -6,6 +6,8 @@ import type { Group, GroupNestedIn, Row } from '#imports' export const useViewGroupBy = (view: Ref, where?: ComputedRef) => { const { api } = useApi() + const { appInfo } = useGlobal() + const { base } = storeToRefs(useBase()) const { sharedView, fetchSharedViewData } = useSharedView() @@ -42,7 +44,13 @@ export const useViewGroupBy = (view: Ref, where?: Computed const reloadViewDataHook = inject(ReloadViewDataHookInj, createEventHook()) - const groupByLimit = 10 + const groupByGroupLimit = computed(() => { + return appInfo.value.defaultGroupByLimit?.limitGroup || 10 + }) + + const groupByRecordLimit = computed(() => { + return appInfo.value.defaultGroupByLimit?.limitRecord || 10 + }) const rootGroup = ref({ key: 'root', @@ -50,7 +58,7 @@ export const useViewGroupBy = (view: Ref, where?: Computed count: 0, column: {} as any, nestedIn: [], - paginationData: { page: 1, pageSize: groupByLimit }, + paginationData: { page: 1, pageSize: groupByGroupLimit.value }, nested: true, children: [], root: true, @@ -64,7 +72,7 @@ export const useViewGroupBy = (view: Ref, where?: Computed groupWrapper.paginationData.page = page await loadGroups( { - offset: (page - 1) * (groupWrapper.paginationData.pageSize || groupByLimit), + offset: (page - 1) * (groupWrapper.paginationData.pageSize || groupByGroupLimit.value), } as any, groupWrapper, ) @@ -172,8 +180,8 @@ export const useViewGroupBy = (view: Ref, where?: Computed const response = !isPublic.value ? await api.dbViewRow.groupBy('noco', base.value.id, view.value.fk_model_id, view.value.id, { - offset: ((group.paginationData.page ?? 0) - 1) * (group.paginationData.pageSize ?? groupByLimit), - limit: group.paginationData.pageSize ?? groupByLimit, + offset: ((group.paginationData.page ?? 0) - 1) * (group.paginationData.pageSize ?? groupByGroupLimit.value), + limit: group.paginationData.pageSize ?? groupByGroupLimit.value, ...params, ...(isUIAllowed('sortSync') ? {} : { sortArrJson: JSON.stringify(sorts.value) }), ...(isUIAllowed('filterSync') ? {} : { filterArrJson: JSON.stringify(nestedFilters.value) }), @@ -182,8 +190,8 @@ export const useViewGroupBy = (view: Ref, where?: Computed column_name: groupby.column.title, } as any) : await api.public.dataGroupBy(sharedView.value!.uuid!, { - offset: ((group.paginationData.page ?? 0) - 1) * (group.paginationData.pageSize ?? groupByLimit), - limit: group.paginationData.pageSize ?? groupByLimit, + offset: ((group.paginationData.page ?? 0) - 1) * (group.paginationData.pageSize ?? groupByGroupLimit.value), + limit: group.paginationData.pageSize ?? groupByGroupLimit.value, ...params, where: nestedWhere, sort: `${groupby.sort === 'desc' ? '-' : ''}${groupby.column.title}`, @@ -198,7 +206,7 @@ export const useViewGroupBy = (view: Ref, where?: Computed ) if (keyExists) { keyExists.count += +curr.count - keyExists.paginationData = { page: 1, pageSize: groupByLimit, totalRows: keyExists.count } + keyExists.paginationData = { page: 1, pageSize: groupByGroupLimit.value, totalRows: keyExists.count } return acc } if (groupby.column.title && groupby.column.uidt) { @@ -216,7 +224,11 @@ export const useViewGroupBy = (view: Ref, where?: Computed column_uidt: groupby.column.uidt, }, ], - paginationData: { page: 1, pageSize: groupByLimit, totalRows: +curr.count }, + paginationData: { + page: 1, + pageSize: group!.nestedIn.length < groupBy.value.length - 1 ? groupByGroupLimit.value : groupByRecordLimit.value, + totalRows: +curr.count, + }, nested: group!.nestedIn.length < groupBy.value.length - 1, }) } @@ -244,7 +256,7 @@ export const useViewGroupBy = (view: Ref, where?: Computed // clear rest of the children group.children = group.children.filter((c) => tempList.find((t) => t.key === c.key)) - if (group.count <= (group.paginationData.pageSize ?? groupByLimit)) { + if (group.count <= (group.paginationData.pageSize ?? groupByGroupLimit.value)) { group.children.sort((a, b) => { const orderA = tempList.findIndex((t) => t.key === a.key) const orderB = tempList.findIndex((t) => t.key === b.key) @@ -268,14 +280,14 @@ export const useViewGroupBy = (view: Ref, where?: Computed if (group.children && !force) return if (!group.paginationData) { - group.paginationData = { page: 1, pageSize: groupByLimit } + group.paginationData = { page: 1, pageSize: groupByRecordLimit.value } } const nestedWhere = calculateNestedWhere(group.nestedIn, where?.value) const query = { - offset: ((group.paginationData.page ?? 0) - 1) * (group.paginationData.pageSize ?? groupByLimit), - limit: group.paginationData.pageSize ?? groupByLimit, + offset: ((group.paginationData.page ?? 0) - 1) * (group.paginationData.pageSize ?? groupByRecordLimit.value), + limit: group.paginationData.pageSize ?? groupByRecordLimit.value, where: `${nestedWhere}`, } @@ -294,7 +306,7 @@ export const useViewGroupBy = (view: Ref, where?: Computed const loadGroupPage = async (group: Group, p: number) => { if (!group.paginationData) { - group.paginationData = { page: 1, pageSize: groupByLimit } + group.paginationData = { page: 1, pageSize: groupByRecordLimit.value } } group.paginationData.page = p await loadGroupData(group, true) @@ -331,7 +343,7 @@ export const useViewGroupBy = (view: Ref, where?: Computed () => groupBy.value.length, async () => { if (groupBy.value.length > 0) { - rootGroup.value.paginationData = { page: 1, pageSize: groupByLimit } + rootGroup.value.paginationData = { page: 1, pageSize: groupByGroupLimit.value } rootGroup.value.column = {} as any await loadGroups() refreshNested() diff --git a/packages/noco-docs/docs/020.getting-started/050.self-hosted/020.environment-variables.md b/packages/noco-docs/docs/020.getting-started/050.self-hosted/020.environment-variables.md index 2b43efa81e..a22ba523a1 100644 --- a/packages/noco-docs/docs/020.getting-started/050.self-hosted/020.environment-variables.md +++ b/packages/noco-docs/docs/020.getting-started/050.self-hosted/020.environment-variables.md @@ -22,6 +22,8 @@ For production use-cases, it is **recommended** to configure | NC_AUTH_JWT_SECRET | JWT secret used for auth and storing other secrets | A random secret will be generated | | PORT | For setting app running port | `8080` | | DB_QUERY_LIMIT_DEFAULT | Pagination limit | 25 | +| DB_QUERY_LIMIT_GROUP_BY_GROUP | Group per page limit | 10 | +| DB_QUERY_LIMIT_GROUP_BY_RECORD | Record per group limit | 10 | | DB_QUERY_LIMIT_MAX | Maximum allowed pagination limit | 1000 | | DB_QUERY_LIMIT_MIN | Minimum allowed pagination limit | 1 | | NC_TOOL_DIR | App directory to keep metadata and app related files | Defaults to current working directory. In docker maps to `/usr/app/data/` for mounting volume. | diff --git a/packages/nocodb/src/helpers/extractLimitAndOffset.ts b/packages/nocodb/src/helpers/extractLimitAndOffset.ts index ac5243152d..d995d14bc0 100644 --- a/packages/nocodb/src/helpers/extractLimitAndOffset.ts +++ b/packages/nocodb/src/helpers/extractLimitAndOffset.ts @@ -4,6 +4,11 @@ export const defaultLimitConfig = { limitMax: Math.max(+process.env.DB_QUERY_LIMIT_MAX || 1000, 1), }; +export const defaultGroupByLimitConfig = { + limitGroup: Math.max(+process.env.DB_QUERY_LIMIT_GROUP_BY_GROUP || 10, 1), + limitRecord: Math.max(+process.env.DB_QUERY_LIMIT_GROUP_BY_RECORD || 10, 1), +}; + export function extractLimitAndOffset( args: { limit?: number | string; diff --git a/packages/nocodb/src/services/utils.service.ts b/packages/nocodb/src/services/utils.service.ts index 85a28bb1ff..9cd676aef3 100644 --- a/packages/nocodb/src/services/utils.service.ts +++ b/packages/nocodb/src/services/utils.service.ts @@ -14,7 +14,10 @@ import NcConnectionMgrv2 from '~/utils/common/NcConnectionMgrv2'; import { MetaTable } from '~/utils/globals'; import { jdbcToXcConfig } from '~/utils/nc-config/helpers'; import { packageVersion } from '~/utils/packageVersion'; -import { defaultLimitConfig } from '~/helpers/extractLimitAndOffset'; +import { + defaultGroupByLimitConfig, + defaultLimitConfig, +} from '~/helpers/extractLimitAndOffset'; const versionCache = { releaseVersion: null, @@ -395,6 +398,7 @@ export class UtilsService { Math.min(defaultLimitConfig.limitDefault, defaultLimitConfig.limitMax), defaultLimitConfig.limitMin, ), + defaultGroupByLimit: defaultGroupByLimitConfig, timezone: defaultConnectionConfig.timezone, ncMin: !!process.env.NC_MIN, teleEnabled: process.env.NC_DISABLE_TELE !== 'true',