@axonsdev/hateoas-nestjs-typeorm
v0.1.0
Published
Assisted TypeORM integration for `@axonsdev/hateoas-nestjs`.
Readme
@axonsdev/hateoas-nestjs-typeorm
Assisted TypeORM integration for @axonsdev/hateoas-nestjs.
This package helps build Siren resource definitions from TypeORM entities while keeping explicit control over exposed fields.
Responsibilities
- Build a
HypermediaResourceDefinitionfor a TypeORM entity. - Generate property mappings from an explicit
exposelist. - Optionally validate exposed columns against TypeORM metadata.
- Preserve the same links, actions, profiles, and context model as
@axonsdev/hateoas-nestjs.
Main Exports
defineTypeOrmSirenResource(entity, options)
TypeOrmSirenResourceOptionsBasic Usage
export const articleResource = defineTypeOrmSirenResource<Article, { user: User }>(Article, {
dataSource,
name: 'article',
classes: ['article'],
id: (entity) => entity.id,
expose: ['id', 'title', 'status'],
profiles: {
list: {
expose: ['id', 'title', 'status'],
links: ['self'],
actions: [],
},
},
links: {
self: ({ entity, url }) => sirenLink('self', url.route('articles.findOne', { id: entity.id })),
},
});expose Is Mandatory
The package intentionally does not expose every TypeORM column automatically.
expose: ['id', 'title', 'status']This prevents accidental leaks of sensitive fields such as passwords, tokens, internal flags, or audit data.
Custom Properties
You can add computed properties or override generated mappings:
defineTypeOrmSirenResource(UserEntity, {
expose: ['id', 'email'],
properties: {
displayName: ({ entity }) => `${entity.firstName} ${entity.lastName}`,
},
// links, actions, profiles...
});Generated mappings from expose are merged first. properties are merged afterward.
Metadata Validation
When dataSource is provided, every exposed field is checked against TypeORM column metadata.
defineTypeOrmSirenResource(UserEntity, {
dataSource,
expose: ['id', 'email'],
// ...
});If an exposed field is not a known column, the function throws an error.
This is a guard against typos and stale resource definitions.
Relationship To @axonsdev/hateoas-nestjs
The result is a normal HypermediaResourceDefinition, so it can be registered in HateoasModule.forRoot(...):
HateoasModule.forRoot({
resources: [articleResource],
routes,
});Current Limitations
- It does not automatically expose relations.
- It does not automatically generate links from TypeORM relations.
- It does not infer profiles.
- It does not infer actions.
- It validates columns only when a
DataSourceis supplied.
These constraints are intentional for the first version: representation exposure should stay explicit.
