Browse Source

refactor: return value as number in pg

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/6470/head
Pranav C 1 year ago
parent
commit
d6dfe95462
  1. 29
      packages/nocodb/src/db/CustomKnex.ts

29
packages/nocodb/src/db/CustomKnex.ts

@ -1,25 +1,46 @@
import { Knex, knex } from 'knex'; import { Knex, knex } from 'knex';
import { SnowflakeClient } from 'nc-help'; import { SnowflakeClient } from 'nc-help';
import { types } from 'pg'; import { defaults, types } from 'pg';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import type { FilterType } from 'nocodb-sdk'; import type { FilterType } from 'nocodb-sdk';
import type { BaseModelSql } from '~/db/BaseModelSql'; import type { BaseModelSql } from '~/db/BaseModelSql';
import Filter from '~/models/Filter'; import Filter from '~/models/Filter';
// refer : https://github.com/brianc/node-pg-types/blob/master/lib/builtins.js
const pgTypes = {
FLOAT4: 700,
FLOAT8: 701,
DATE: 1082,
TIMESTAMP: 1114,
TIMESTAMPTZ: 1184,
NUMERIC: 1700,
};
// For the code, check out // For the code, check out
// https://raw.githubusercontent.com/brianc/node-pg-types/master/lib/builtins.js // https://raw.githubusercontent.com/brianc/node-pg-types/master/lib/builtins.js
// override parsing date column to Date() // override parsing date column to Date()
types.setTypeParser(1082, (val) => val); types.setTypeParser(pgTypes.DATE, (val) => val);
// override timestamp // override timestamp
types.setTypeParser(1114, (val) => { types.setTypeParser(pgTypes.TIMESTAMP, (val) => {
return dayjs.utc(val).format('YYYY-MM-DD HH:mm:ssZ'); return dayjs.utc(val).format('YYYY-MM-DD HH:mm:ssZ');
}); });
// override timestampz // override timestampz
types.setTypeParser(1184, (val) => { types.setTypeParser(pgTypes.TIMESTAMPTZ, (val) => {
return dayjs(val).utc().format('YYYY-MM-DD HH:mm:ssZ'); return dayjs(val).utc().format('YYYY-MM-DD HH:mm:ssZ');
}); });
const parseFloatVal = (value: string) => {
return parseFloat(value);
};
// parse integer values
defaults.parseInt8 = true;
// parse float values
types.setTypeParser(pgTypes.FLOAT8, parseFloatVal);
types.setTypeParser(pgTypes.NUMERIC, parseFloatVal);
const opMappingGen = { const opMappingGen = {
eq: '=', eq: '=',
lt: '<', lt: '<',

Loading…
Cancel
Save