@Embedded
@Embedded maps a nested object property onto a group of columns on the same table — as
opposed to @Link, which relates to rows in a different table. See
Embedded Objects for the full guide.
function Embedded(
type?: TypeThunk,
options?: EmbeddedFieldOptions,
): PropertyDecorator;
class PersonName {
@Column({ fieldName: 'given_name' })
declare given?: string;
@Column({ fieldName: 'family_name' })
declare family?: string;
}
@Entity('customers')
class Customer {
@Embedded(PersonName)
declare name: PersonName;
}
type can be the embedded class itself, or a thunk (() => Type | Promise<Type>) to avoid
circular imports; if omitted, it's inferred from the property's declared TS type via reflection
(throws "type" must be defined if that isn't a class). The embedded type does not need its own
@Entity decorator — only @Column-decorated properties.
fieldNamePrefix / fieldNameSuffix
@Embedded(Address, { fieldNamePrefix: 'address_' })
declare address: Address;
Because an embedded object's columns live on the same table as everything else, fieldNamePrefix
(prepended) and/or fieldNameSuffix (appended) are typically needed to avoid column-name
collisions — each is applied to every embedded column's own fieldName. See
Embedded Objects for a
full worked example.
EmbeddedFieldOptions reference
type EmbeddedFieldOptions = Partial<
Omit<EmbeddedFieldMetadata, 'entity' | 'name' | 'kind' | 'type'>
>;
| Option | Type | Description |
|---|---|---|
fieldNamePrefix | string | Prepended to every embedded column's fieldName. |
fieldNameSuffix | string | Appended to every embedded column's fieldName. |
hidden | boolean | Never returned, even if explicitly requested in projection. |
exclusive | boolean | Only returned when explicitly requested in projection. |
Embedding an @Entity class as JSON instead
Nesting another @Entity-decorated class as the declared type of a plain @Column (not
@Embedded) is a different feature — see @Column → Type inference.