Create new type position (#4336)
* Create new type position * Remove position filter type --------- Co-authored-by: Thomas Trompette <thomast@twenty.com>
This commit is contained in:
@ -192,6 +192,7 @@ export enum FieldMetadataType {
|
||||
Number = 'NUMBER',
|
||||
Numeric = 'NUMERIC',
|
||||
Phone = 'PHONE',
|
||||
Position = 'POSITION',
|
||||
Probability = 'PROBABILITY',
|
||||
Rating = 'RATING',
|
||||
Relation = 'RELATION',
|
||||
|
||||
@ -168,6 +168,7 @@ export enum FieldMetadataType {
|
||||
Number = 'NUMBER',
|
||||
Numeric = 'NUMERIC',
|
||||
Phone = 'PHONE',
|
||||
Position = 'POSITION',
|
||||
Probability = 'PROBABILITY',
|
||||
Rating = 'RATING',
|
||||
Relation = 'RELATION',
|
||||
|
||||
@ -32,6 +32,7 @@ export const generateEmptyFieldValue = (
|
||||
}
|
||||
case FieldMetadataType.Number:
|
||||
case FieldMetadataType.Rating:
|
||||
case FieldMetadataType.Position:
|
||||
case FieldMetadataType.Numeric: {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -35,6 +35,7 @@ export enum FieldMetadataType {
|
||||
SELECT = 'SELECT',
|
||||
MULTI_SELECT = 'MULTI_SELECT',
|
||||
RELATION = 'RELATION',
|
||||
POSITION = 'POSITION',
|
||||
}
|
||||
|
||||
@Entity('fieldMetadata')
|
||||
|
||||
@ -41,6 +41,7 @@ type FieldMetadataDefaultValueMapping = {
|
||||
| FieldMetadataDynamicDefaultValueNow;
|
||||
[FieldMetadataType.BOOLEAN]: FieldMetadataDefaultValueBoolean;
|
||||
[FieldMetadataType.NUMBER]: FieldMetadataDefaultValueNumber;
|
||||
[FieldMetadataType.POSITION]: FieldMetadataDefaultValueNumber;
|
||||
[FieldMetadataType.NUMERIC]: FieldMetadataDefaultValueString;
|
||||
[FieldMetadataType.PROBABILITY]: FieldMetadataDefaultValueNumber;
|
||||
[FieldMetadataType.LINK]: FieldMetadataDefaultValueLink;
|
||||
|
||||
@ -34,6 +34,7 @@ export function generateTargetColumnMap(
|
||||
case FieldMetadataType.RATING:
|
||||
case FieldMetadataType.SELECT:
|
||||
case FieldMetadataType.MULTI_SELECT:
|
||||
case FieldMetadataType.POSITION:
|
||||
return {
|
||||
value: columnName,
|
||||
};
|
||||
|
||||
@ -23,6 +23,7 @@ export type BasicFieldMetadataType =
|
||||
| FieldMetadataType.NUMBER
|
||||
| FieldMetadataType.PROBABILITY
|
||||
| FieldMetadataType.BOOLEAN
|
||||
| FieldMetadataType.POSITION
|
||||
| FieldMetadataType.DATE_TIME;
|
||||
|
||||
@Injectable()
|
||||
|
||||
@ -19,6 +19,7 @@ export const fieldMetadataTypeToColumnType = <Type extends FieldMetadataType>(
|
||||
return 'numeric';
|
||||
case FieldMetadataType.NUMBER:
|
||||
case FieldMetadataType.PROBABILITY:
|
||||
case FieldMetadataType.POSITION:
|
||||
return 'float';
|
||||
case FieldMetadataType.BOOLEAN:
|
||||
return 'boolean';
|
||||
|
||||
@ -66,6 +66,7 @@ export class WorkspaceMigrationFactory {
|
||||
],
|
||||
[FieldMetadataType.NUMERIC, { factory: this.basicColumnActionFactory }],
|
||||
[FieldMetadataType.NUMBER, { factory: this.basicColumnActionFactory }],
|
||||
[FieldMetadataType.POSITION, { factory: this.basicColumnActionFactory }],
|
||||
[
|
||||
FieldMetadataType.PROBABILITY,
|
||||
{ factory: this.basicColumnActionFactory },
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import { PositionScalarType } from './position.scalar';
|
||||
import { CursorScalarType } from './cursor.scalar';
|
||||
import { BigFloatScalarType } from './big-float.scalar';
|
||||
import { BigIntScalarType } from './big-int.scalar';
|
||||
@ -22,4 +23,5 @@ export const scalars = [
|
||||
TimeScalarType,
|
||||
UUIDScalarType,
|
||||
CursorScalarType,
|
||||
PositionScalarType,
|
||||
];
|
||||
|
||||
@ -0,0 +1,38 @@
|
||||
import { GraphQLScalarType, Kind } from 'graphql';
|
||||
|
||||
type PositionType = 'first' | 'last' | number;
|
||||
|
||||
const isValidStringPosition = (value: string): boolean =>
|
||||
typeof value === 'string' && (value === 'first' || value === 'last');
|
||||
|
||||
const isValidNumberPosition = (value: number): boolean =>
|
||||
typeof value === 'number' && value >= 0;
|
||||
|
||||
const checkPosition = (value: any): PositionType => {
|
||||
if (isValidNumberPosition(value) || isValidStringPosition(value)) {
|
||||
return value;
|
||||
}
|
||||
throw new Error('Invalid position found');
|
||||
};
|
||||
|
||||
export const PositionScalarType = new GraphQLScalarType({
|
||||
name: 'Position',
|
||||
description:
|
||||
'A custom scalar type for representing record position in a list',
|
||||
serialize: checkPosition,
|
||||
parseValue: checkPosition,
|
||||
parseLiteral(ast): PositionType {
|
||||
if (
|
||||
ast.kind == Kind.STRING &&
|
||||
(ast.value === 'first' || ast.value === 'last')
|
||||
) {
|
||||
return ast.value;
|
||||
}
|
||||
|
||||
if (ast.kind == Kind.INT || ast.kind == Kind.FLOAT) {
|
||||
return parseFloat(ast.value);
|
||||
}
|
||||
|
||||
throw new Error('Invalid position found');
|
||||
},
|
||||
});
|
||||
@ -34,6 +34,7 @@ import {
|
||||
} from 'src/workspace/workspace-schema-builder/graphql-types/input';
|
||||
import { OrderByDirectionType } from 'src/workspace/workspace-schema-builder/graphql-types/enum';
|
||||
import { BigFloatScalarType } from 'src/workspace/workspace-schema-builder/graphql-types/scalars';
|
||||
import { PositionScalarType } from 'src/workspace/workspace-schema-builder/graphql-types/scalars/position.scalar';
|
||||
|
||||
export interface TypeOptions<T = any> {
|
||||
nullable?: boolean;
|
||||
@ -66,6 +67,7 @@ export class TypeMapperService {
|
||||
[FieldMetadataType.NUMERIC, BigFloatScalarType],
|
||||
[FieldMetadataType.PROBABILITY, GraphQLFloat],
|
||||
[FieldMetadataType.RELATION, GraphQLID],
|
||||
[FieldMetadataType.POSITION, PositionScalarType],
|
||||
]);
|
||||
|
||||
return typeScalarMapping.get(fieldMetadataType);
|
||||
@ -96,6 +98,7 @@ export class TypeMapperService {
|
||||
[FieldMetadataType.NUMERIC, BigFloatFilterType],
|
||||
[FieldMetadataType.PROBABILITY, FloatFilterType],
|
||||
[FieldMetadataType.RELATION, UUIDFilterType],
|
||||
[FieldMetadataType.POSITION, FloatFilterType],
|
||||
]);
|
||||
|
||||
return typeFilterMapping.get(fieldMetadataType);
|
||||
@ -118,6 +121,7 @@ export class TypeMapperService {
|
||||
[FieldMetadataType.RATING, OrderByDirectionType],
|
||||
[FieldMetadataType.SELECT, OrderByDirectionType],
|
||||
[FieldMetadataType.MULTI_SELECT, OrderByDirectionType],
|
||||
[FieldMetadataType.POSITION, OrderByDirectionType],
|
||||
]);
|
||||
|
||||
return typeOrderByMapping.get(fieldMetadataType);
|
||||
|
||||
Reference in New Issue
Block a user