DEV Community

James Harrison
James Harrison

Posted on

What we're working on: Database Entities

Hey there! Today at Vast we're working on a big new feature that allows programmers to create and edit database entities within Vast Studio.

Database Entities in Vast Studio

Vast already has support for Schemas (which are like NestJS DTOs), and Entities are similar; they have a name, description, inheritance and properties. But Entities come with additional challenges:

  1. While schema properties can be "optional", entity properties are always defined but can be nullable
  2. Entities need special data types like primary generated columns
  3. Like schemas, Entities can reference other entities (relationships) but require special decorators
  4. Entities need to be added to the TypeORM module registration
  5. Certain column types should be excluded from payloads, like generated IDs and relationships
  6. Entity columns have more data types than Typescript types (e.g. number could be int or float)

Current State

Vast's schemas are structured as follows:

export interface IProperty {
  name: string;
  typeSignature: PropertyType;
  isOptional: boolean;
  validators: Validator[];
}

export interface IClass<P extends IProperty = IProperty>
  extends OptionalTypeGraphNode {
  description: string | null;
  parentId: string;
  properties: P[];
}

export interface ISchema extends IClass {
  inheritance: TypeRefPropertyType | null;
}

Enter fullscreen mode Exit fullscreen mode

Future State

In order to support entities, we need to create a new interface that extends IClass:

export interface IEntityProperty extends IProperty {}

export interface IEntity extends IClass<IEntityProperty> {
  properties: IEntityProperty[];
}
Enter fullscreen mode Exit fullscreen mode

This allows us to customize the behaviour of Entities but still extend from the same base interface.

Here's a look at how an Entity looks in Vast Studio and how it gets generated as NestJS code.

Entity in Vast Studio

Entity in Vast Studio

Entity in NestJS

@Entity()
export class AddressEntity {
  @PrimaryGeneratedColumn("uuid")
  id: string;

  @IsString()
  @Column()
  country: string;

  @IsNumber({})
  @Column({ nullable: true, type: "float" })
  phone: number | null;

  @IsString()
  @Column()
  state: string;

  @IsString()
  @Column()
  street: string;

  @IsString()
  @Column()
  suburb: string;

  @ValidateNested()
  @ManyToOne(() => UserEntity)
  user: UserEntity;
}
Enter fullscreen mode Exit fullscreen mode

Have thoughts, suggestions or questions? Let us know below. Or download Vast Studio for free and try it for yourself:

https://www.getvast.app/

Top comments (0)