Formatted Fields
Alapa allows you to format specific fields of a model after the entity is loaded.
This is useful for post-processing values before they are exposed or used elsewhere in your application.
import { Model, Column, TableModel, ModelFormattedFields } from "alapa";
@TableModel()
export class Users extends Model {
  @Column()
  firstName: string;
  @Column()
  lastName: string;
  @Column()
  profilePhoto: string;
  protected formattedFields: ModelFormattedFields<Users> = {
    profilePhoto: (user: Users) =>
      "https://example.com/images/" + user.profilePhoto,
  };
}
@FormattedField Decorator
The @FormattedField decorator allows you to automatically apply formatting logic to a model field using a function.
Syntax
@FormattedField<Model, fieldName>(formatterFunction)
Example
import { Model, Column, TableModel, FormattedField, md5 } from "alapa";
@TableModel()
export class Users extends Model {
  @Column()
  firstName: string;
  @Column()
  lastName: string;
  @FormattedField<Users, "secret">(md5)
  @Column()
  secret: string;
}
Parameters
| Parameter | Description | Type | 
|---|---|---|
| Model | The class representing your model | class | 
| fieldName | The field (non-method) to be formatted | string | 
| function | A function that transforms the field's value appropriately | Function | 
The formatting function must return the correct type for the target field. If it does not, a type error will be thrown during compilation.
Example: Invalid Return Type
// Type 'number' is not assignable to type 'string'.
@FormattedField<Users, "firstName">(NumberOnly)
@Column()
firstName: string;
@FormattedField Using a Method Name
You can also pass a method name (as a string) instead of a direct function. This is useful for referencing instance methods, including inherited ones.
Syntax
@FormattedField<Model, fieldName, methodName>("methodName")
Example
import { Model, FormattedField, Column, TableModel } from "alapa";
@TableModel()
export class Users extends Model {
  @Column()
  firstName: string;
  @Column()
  lastName: string;
  @FormattedField<Users, "fullName", "getFullName">("getFullName")
  fullName: string;
  getFullName() {
    return `${this.firstName} ${this.lastName}`;
  }
}
Parameters
| Parameter | Description | Type | 
|---|---|---|
| Model | The class representing your model | class | 
| fieldName | The field (non-method) to be formatted | string | 
| methodName | The name of the method used to format the field | string | 
@FormattedFieldMethod Decorator
The @FormattedFieldMethod decorator is an alternative syntax to bind a model method to a field as a formatter.
Syntax
@FormattedFieldMethod<Model, "fieldName">("fieldName")
Example
import { Model, Column, TableModel, FormattedFieldMethod } from "alapa";
@TableModel()
export class Users extends Model {
  @Column()
  firstName: string;
  @Column()
  lastName: string;
  fullName: string;
  @FormattedFieldMethod<Users, "fullName">("fullName")
  protected getFullName() {
    return `${this.firstName} ${this.lastName}`;
  }
}
Parameters
| Parameter | Description | Type | 
|---|---|---|
| Model | The class representing your model | class | 
| fieldName | The field (non-method) to bind the formatting logic to | string | 
@AfterLoad() Hook
For more complex scenarios, Alapa provides the @AfterLoad lifecycle hook.
This method is called automatically after a model is loaded from the database.
Example
import { Model, PrimaryColumn, Column, AfterLoad } from "alapa";
@TableModel()
export class Users extends Model {
  @PrimaryColumn()
  id: number;
  @Column()
  profilePhoto: string;
  @AfterLoad()
  protected afterLoad() {
    this.profilePhoto = "https://example.com/images/" + this.profilePhoto;
  }
}
This guide shows various ways you can format your model fields in Alapa, offering both declarative decorators and lifecycle methods for full flexibility.