From 79ae6416b1801cbdb71cc1b186e33776a508b075 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 22 Nov 2023 07:17:56 +0000 Subject: [PATCH] feat: formula - add counta function --- packages/nc-gui/utils/formulaUtils.ts | 20 +++++++++++++ packages/nocodb/src/db/functionMappings/pg.ts | 28 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/packages/nc-gui/utils/formulaUtils.ts b/packages/nc-gui/utils/formulaUtils.ts index 940f9391ac..6e4a676857 100644 --- a/packages/nc-gui/utils/formulaUtils.ts +++ b/packages/nc-gui/utils/formulaUtils.ts @@ -529,6 +529,26 @@ const formulas: Record = { syntax: 'RECORD_ID()', examples: ['RECORD_ID()'], }, + COUNTA: { + validation: { + args: { + min:1, + }, + }, + description: '', + syntax: 'COUNTA()', + examples: ['COUNTA()'], + }, + COUNT: { + validation: { + args: { + rqd: 0, + }, + }, + description: '', + syntax: 'COUNT()', + examples: ['COUNT()'], + }, } const formulaList = Object.keys(formulas) diff --git a/packages/nocodb/src/db/functionMappings/pg.ts b/packages/nocodb/src/db/functionMappings/pg.ts index 6d373fee0b..594ba775d3 100644 --- a/packages/nocodb/src/db/functionMappings/pg.ts +++ b/packages/nocodb/src/db/functionMappings/pg.ts @@ -225,6 +225,34 @@ const pg = { builder: knex.raw(`${args.join(' # ')} ${colAlias}`), }; }, + COUNT: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { + return { + builder: knex.raw( + `${( + await Promise.all( + pt.arguments.map(async (arg) => { + const { builder } = await fn(arg); + return `CASE WHEN ${builder} IS NOT NULL AND ${builder}::text != '' THEN 1 ELSE 0 END`; + }), + ) + ).join(' + ')} ${colAlias}`, + ), + }; + }, + COUNTA: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { + return { + builder: knex.raw( + `${( + await Promise.all( + pt.arguments.map(async (arg) => { + const { builder } = await fn(arg); + return `CASE WHEN ${builder} IS NOT NULL AND ${builder}::text != '' THEN 1 ELSE 0 END`; + }), + ) + ).join(' + ')} ${colAlias}`, + ), + }; + }, }; export default pg;