[TYPES] UniversalEntity JsonbProperty and SerializedRelation (#17396)

# Introduction

In this PR we're introducing mainly two branded type signatures for both
`JsonbProperty` entities properties and `SerializedRelation` (jsonb
serialized property storing another entity id).

Allowing to dynamically map over them later in order to build universal
`jsonb` `serialized` relations.

## `JsonbProperty`

A branded wrapper type that marks entity properties stored as PostgreSQL
JSONB columns. It adds a phantom brand `__JsonbPropertyBrand__` to
object types while leaving primitives unchanged. The branded key is
optional and typed as never, also omitted when transpiled to
`UniversalFlat`

**Should be used at entities lvl only:**
```typescript
@Column({ type: 'jsonb', nullable: false })
gridPosition: JsonbProperty<GridPosition>;

@Column({ nullable: false, type: 'jsonb', default: [] })
publishedVersions: JsonbProperty<string[]>;
```

## `SerializedRelation`

A branded string type that marks foreign key IDs stored inside JSONB
objects. These are entity references serialized within a JSONB column
rather than being a regular database foreign key.

**Usage in jsonb property generic***
```ts
type FieldMetadataRelationSettings = {
  relationType: RelationType;
  onDelete?: RelationOnDeleteAction;
  joinColumnName?: string | null;
  junctionTargetFieldId?: SerializedRelation;
};
```

## `FormatJsonbSerializedRelation<T>`

A transformation type that processes JSONB properties for universal
entity mapping. It:
1. Detects properties with the `JsonbProperty` brand
2. Finds `SerializedRelation` properties
3. Renames them from `*Id` to `*UniversalIdentifier`
4. Removes the brand from the output type ( optional though )

```typescript
// Input: JsonbProperty<{ targetFieldMetadataId: SerializedRelation }>
// Output: { targetFieldMetadataUniversalIdentifier: SerializedRelation }
```

## Result
An example of the dynamic type mapping, through a type-test example
```ts
type SettingsTestCase = UniversalFlatFieldMetadata<
    | FieldMetadataType.RELATION
    | FieldMetadataType.NUMBER
    | FieldMetadataType.TEXT
  >['settings']

type SettingsExpectedResult =
  | {
      relationType: RelationType;
      onDelete?: RelationOnDeleteAction | undefined;
      joinColumnName?: string | null | undefined;
      junctionTargetFieldUniversalIdentifier?: SerializedRelation | undefined;
    }
  | {
      dataType?: NumberDataType | undefined;
      decimals?: number | undefined;
      type?: FieldNumberVariant | undefined;
    }
  | {
      displayedMaxRows?: number | undefined;
    }
  | null;

type Assertions = [
  Expect<Equal<SettingsTestCase, SettingsExpectedResult>>,
]
```

## Remarks

- Removed duplicated twenty-server and twenty-shared typed
- Removed class validator instances for default value that were not used
at runtime, we will refactor that to add validation across all entities
following a same pattern
This commit is contained in:
Paul Rastoin
2026-01-26 15:25:43 +01:00
committed by GitHub
parent d0bc9a94c0
commit 44202668fd
62 changed files with 1082 additions and 1070 deletions
@@ -17,6 +17,7 @@ import {
} from 'class-validator';
import { GraphQLJSON } from 'graphql-type-json';
import { CalendarStartDay } from 'twenty-shared/constants';
import { SerializedRelation } from 'twenty-shared/types';
import { AggregateOperations } from 'src/engine/api/graphql/graphql-query-runner/constants/aggregate-operations.constant';
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
@@ -37,7 +38,7 @@ export class AggregateChartConfigurationDTO
@Field(() => UUIDScalarType)
@IsUUID()
@IsNotEmpty()
aggregateFieldMetadataId: string;
aggregateFieldMetadataId: SerializedRelation;
@Field(() => AggregateOperations)
@IsEnum(AggregateOperations)
@@ -17,6 +17,7 @@ import {
} from 'class-validator';
import { GraphQLJSON } from 'graphql-type-json';
import { CalendarStartDay } from 'twenty-shared/constants';
import { SerializedRelation } from 'twenty-shared/types';
import { AggregateOperations } from 'src/engine/api/graphql/graphql-query-runner/constants/aggregate-operations.constant';
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
@@ -41,7 +42,7 @@ export class BarChartConfigurationDTO
@Field(() => UUIDScalarType)
@IsUUID()
@IsNotEmpty()
aggregateFieldMetadataId: string;
aggregateFieldMetadataId: SerializedRelation;
@Field(() => AggregateOperations)
@IsEnum(AggregateOperations)
@@ -51,7 +52,7 @@ export class BarChartConfigurationDTO
@Field(() => UUIDScalarType)
@IsUUID()
@IsNotEmpty()
primaryAxisGroupByFieldMetadataId: string;
primaryAxisGroupByFieldMetadataId: SerializedRelation;
@Field(() => String, { nullable: true })
@IsString()
@@ -80,7 +81,7 @@ export class BarChartConfigurationDTO
@Field(() => UUIDScalarType, { nullable: true })
@IsUUID()
@IsOptional()
secondaryAxisGroupByFieldMetadataId?: string;
secondaryAxisGroupByFieldMetadataId?: SerializedRelation;
@Field(() => String, { nullable: true })
@IsString()
@@ -15,6 +15,7 @@ import {
} from 'class-validator';
import { GraphQLJSON } from 'graphql-type-json';
import { CalendarStartDay } from 'twenty-shared/constants';
import { SerializedRelation } from 'twenty-shared/types';
import { AggregateOperations } from 'src/engine/api/graphql/graphql-query-runner/constants/aggregate-operations.constant';
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
@@ -34,7 +35,7 @@ export class GaugeChartConfigurationDTO
@Field(() => UUIDScalarType)
@IsUUID()
@IsNotEmpty()
aggregateFieldMetadataId: string;
aggregateFieldMetadataId: SerializedRelation;
@Field(() => AggregateOperations)
@IsEnum(AggregateOperations)
@@ -17,6 +17,7 @@ import {
} from 'class-validator';
import { GraphQLJSON } from 'graphql-type-json';
import { CalendarStartDay } from 'twenty-shared/constants';
import { SerializedRelation } from 'twenty-shared/types';
import { AggregateOperations } from 'src/engine/api/graphql/graphql-query-runner/constants/aggregate-operations.constant';
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
@@ -39,7 +40,7 @@ export class LineChartConfigurationDTO
@Field(() => UUIDScalarType)
@IsUUID()
@IsNotEmpty()
aggregateFieldMetadataId: string;
aggregateFieldMetadataId: SerializedRelation;
@Field(() => AggregateOperations)
@IsEnum(AggregateOperations)
@@ -49,7 +50,7 @@ export class LineChartConfigurationDTO
@Field(() => UUIDScalarType)
@IsUUID()
@IsNotEmpty()
primaryAxisGroupByFieldMetadataId: string;
primaryAxisGroupByFieldMetadataId: SerializedRelation;
@Field(() => String, { nullable: true })
@IsString()
@@ -81,7 +82,7 @@ export class LineChartConfigurationDTO
@Field(() => UUIDScalarType, { nullable: true })
@IsUUID()
@IsOptional()
secondaryAxisGroupByFieldMetadataId?: string;
secondaryAxisGroupByFieldMetadataId?: SerializedRelation;
@Field(() => String, { nullable: true })
@IsString()
@@ -16,6 +16,7 @@ import {
} from 'class-validator';
import { GraphQLJSON } from 'graphql-type-json';
import { CalendarStartDay } from 'twenty-shared/constants';
import { SerializedRelation } from 'twenty-shared/types';
import { AggregateOperations } from 'src/engine/api/graphql/graphql-query-runner/constants/aggregate-operations.constant';
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
@@ -37,7 +38,7 @@ export class PieChartConfigurationDTO
@Field(() => UUIDScalarType)
@IsUUID()
@IsNotEmpty()
aggregateFieldMetadataId: string;
aggregateFieldMetadataId: SerializedRelation;
@Field(() => AggregateOperations)
@IsEnum(AggregateOperations)
@@ -47,7 +48,7 @@ export class PieChartConfigurationDTO
@Field(() => UUIDScalarType)
@IsUUID()
@IsNotEmpty()
groupByFieldMetadataId: string;
groupByFieldMetadataId: SerializedRelation;
@Field(() => String, { nullable: true })
@IsString()
@@ -1,6 +1,7 @@
import { Field, ObjectType } from '@nestjs/graphql';
import { IsNotEmpty, IsString, IsUUID } from 'class-validator';
import { SerializedRelation } from 'twenty-shared/types';
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
@@ -9,7 +10,7 @@ export class RatioAggregateConfigDTO {
@Field(() => UUIDScalarType)
@IsUUID()
@IsNotEmpty()
fieldMetadataId: string;
fieldMetadataId: SerializedRelation;
@Field(() => String)
@IsString()
@@ -20,6 +20,7 @@ import { WidgetType } from 'src/engine/metadata-modules/page-layout-widget/enums
import { type GridPosition } from 'src/engine/metadata-modules/page-layout-widget/types/grid-position.type';
import { PageLayoutWidgetConfigurationTypeSettings } from 'src/engine/metadata-modules/page-layout-widget/types/page-layout-widget-configuration.type';
import { SyncableEntity } from 'src/engine/workspace-manager/types/syncable-entity.interface';
import { type JsonbProperty } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/jsonb-property.type';
@Entity({ name: 'pageLayoutWidget', schema: 'core' })
@ObjectType('PageLayoutWidget')
@@ -70,10 +71,12 @@ export class PageLayoutWidgetEntity<
objectMetadata: Relation<ObjectMetadataEntity> | null;
@Column({ type: 'jsonb', nullable: false })
gridPosition: GridPosition;
gridPosition: JsonbProperty<GridPosition>;
@Column({ type: 'jsonb', nullable: false })
configuration: PageLayoutWidgetConfigurationTypeSettings<TWidgetConfigurationType>;
configuration: JsonbProperty<
PageLayoutWidgetConfigurationTypeSettings<TWidgetConfigurationType>
>;
@CreateDateColumn({ type: 'timestamptz' })
createdAt: Date;