Skip to main content

@Serialize

@Serialize sets a column's serialize transform — run over a JS property value before it's sent to the database when writing rows. See Data Columns → parse / serialize for the full guide.

function Serialize(fn: ColumnTransformFunction): PropertyDecorator;

type ColumnTransformFunction = (value: any, name: string) => any;

A property decorator, applicable to string-keyed properties only (throws You can define a Column for only string properties otherwise). It sets the same underlying metadata field as ColumnFieldOptions.serialize (see ColumnFieldOptions) — calling @Serialize on a property that has no @Column of its own yet still registers (an implicit) column field for it.

const GenderMap = { M: 'Male', F: 'Female' };

@Column(DataType.CHAR)
@Serialize(v => ('' + v).charAt(0))
declare gender: string;

See also @Parse, the inverse transform (database value → JS value).