Skip to main content

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.

src/app/models/user.ts
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

src/app/models/user.ts
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

ParameterDescriptionType
ModelThe class representing your modelclass
fieldNameThe field (non-method) to be formattedstring
functionA function that transforms the field's value appropriatelyFunction

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

src/app/models/user.ts
// 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

src/app/models/user.ts
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

ParameterDescriptionType
ModelThe class representing your modelclass
fieldNameThe field (non-method) to be formattedstring
methodNameThe name of the method used to format the fieldstring

@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

src/app/models/user.ts
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

ParameterDescriptionType
ModelThe class representing your modelclass
fieldNameThe field (non-method) to bind the formatting logic tostring

@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

src/app/models/user.ts
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.