Services - Entity
An Eicrud Entity is a database schema as well as a DTO (data transfer object) for CRUD operations. It represents "what the data can be".
Schema
Fields that you annotate with Mikro-orm's decorators are part of your database schema.
import { Entity, PrimaryKey, Property } from "@mikro-orm/core";
@Entity()
export class Profile implements CrudEntity {
@PrimaryKey({ name: '_id' })
id: string;
@OneToOne(() => User, user => user.profile)
owner: User | string;
@Property()
createdAt: Date;
@Property()
updatedAt: Date;
notPersisted: number;
}
Note
Non annotated fields are not persisted in the database.
Learn how to define entities with Mikro-orm's documentation.
Validation
Fields that you annotate with class-validator's decorators are part of your DTO.
import { IsString, IsOptional } from "class-validator";
export class Profile implements CrudEntity {
@IsOptional()
@IsString()
id: string;
@IsString()
owner: User | string;
createdAt: Date;
updatedAt: Date;
}
Note
Non-annotated fields are not allowed in the DTO, and thus cannot be queried by the client.
Transform
Fields that you annotate with Eicrud's transform decorators will be transformed at the controller level.
import { $Transform } from '@eicrud/core/validation';
export class Profile implements CrudEntity {
id: string;
@$Transform((value ) => {
return value.toLowerCase().trim()
})
username: User | string;
createdAt: Date;
updatedAt: Date;
}
Warning
The class-transformer
package isn't compatible with Eicrud's validation, make sure to use Eicrud's decorators.
Mix and match
You can combine all of the above in a single entity class.
import { Entity, PrimaryKey, Property } from "@mikro-orm/core";
import { IsString, IsOptional, IsInt } from "class-validator";
import { $Transform } from '@eicrud/core/validation';
@Entity()
export class Profile implements CrudEntity {
@IsOptional()
@IsString()
@PrimaryKey({ name: '_id' })
id: string;
@IsString()
@OneToOne(() => User, user => user.profile)
owner: User | string;
@$Transform((value ) => {
return value.toLowerCase().trim()
})
@IsString()
username: User | string;
@Property()
createdAt: Date;
@Property()
updatedAt: Date;
@IsInt()
notPersisted: number;
}