Browse Source

feat: formula - record_id support

pull/7019/head
Pranav C 1 year ago
parent
commit
24980e6a58
  1. 10
      packages/nc-gui/utils/formulaUtils.ts
  2. 28
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts
  3. 16
      packages/nocodb/src/db/functionMappings/commonFns.ts
  4. 2
      packages/nocodb/src/db/mapFunctionName.ts

10
packages/nc-gui/utils/formulaUtils.ts

@ -519,6 +519,16 @@ const formulas: Record<string, any> = {
syntax: 'ODD(value)',
examples: ['ODD({column})'],
},
RECORD_ID: {
validation: {
args: {
rqd: 0,
},
},
description: 'Returns ID of the current record.',
syntax: 'RECORD_ID()',
examples: ['RECORD_ID()'],
},
}
const formulaList = Object.keys(formulas)

28
packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts

@ -724,6 +724,7 @@ async function _formulaQueryBuilder(
fn,
colAlias,
prevBinaryOp,
model,
});
if (res) return res;
}
@ -774,22 +775,19 @@ async function _formulaQueryBuilder(
}
return { builder: knex.raw(`??${colAlias}`, [builder || pt.name]) };
} else if (pt.type === 'BinaryExpression') {
if(pt.operator === '&'){
return fn(
{
type: 'CallExpression',
arguments: [
pt.left,
pt.right
],
callee: {
type: 'Identifier',
name: 'CONCAT',
},
// treat `&` as shortcut for concat
if (pt.operator === '&') {
return fn(
{
type: 'CallExpression',
arguments: [pt.left, pt.right],
callee: {
type: 'Identifier',
name: 'CONCAT',
},
alias,
prevBinaryOp,
},
alias,
prevBinaryOp,
);
}

16
packages/nocodb/src/db/functionMappings/commonFns.ts

@ -1,4 +1,5 @@
import type { MapFnArgs } from '../mapFunctionName';
import { NcError } from '~/helpers/catchError';
export default {
// todo: handle default case
@ -135,4 +136,19 @@ export default {
),
};
},
RECORD_ID: async (args: MapFnArgs) => {
const pkCol = args.model?.primaryKey;
if (!pkCol) {
NcError.badRequest('Primary key not found');
}
return {
builder: args.knex.raw(
`${
(await args.fn({ type: 'Identifier', name: pkCol.id }, args.a))
.builder
} ${args.colAlias}`,
),
};
},
};

2
packages/nocodb/src/db/mapFunctionName.ts

@ -1,5 +1,6 @@
import type { XKnex } from '~/db/CustomKnex';
import type { Knex } from 'knex';
import type { Model } from '~/models';
import mssql from '~/db/functionMappings/mssql';
import mysql from '~/db/functionMappings/mysql';
import pg from '~/db/functionMappings/pg';
@ -17,6 +18,7 @@ export interface MapFnArgs {
fn: (...args: any) => Promise<{ builder: Knex.QueryBuilder | any }>;
colAlias: string;
prevBinaryOp?: any;
model: Model;
}
const mapFunctionName = async (args: MapFnArgs): Promise<any> => {

Loading…
Cancel
Save