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;